2019-04-30 23:27:16 +00:00
|
|
|
package write
|
|
|
|
|
|
|
|
import (
|
2021-04-29 21:44:32 +00:00
|
|
|
"bytes"
|
2019-04-30 23:27:16 +00:00
|
|
|
"io"
|
2019-06-28 16:35:35 +00:00
|
|
|
"strings"
|
2019-04-30 23:27:16 +00:00
|
|
|
"testing"
|
2020-10-06 18:24:05 +00:00
|
|
|
|
2021-04-06 18:19:59 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-04-30 23:27:16 +00:00
|
|
|
"github.com/hashicorp/consul/agent"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfigWrite_noTabs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require.NotContains(t, New(cli.NewMockUi()).Help(), "\t")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigWrite(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:27:16 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, ``)
|
2019-04-30 23:27:16 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
client := a.Client()
|
|
|
|
|
|
|
|
t.Run("File", func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
|
|
|
f := testutil.TempFile(t, "config-write-svc-web.hcl")
|
|
|
|
_, err := f.WriteString(`
|
|
|
|
Kind = "service-defaults"
|
|
|
|
Name = "web"
|
|
|
|
Protocol = "udp"
|
|
|
|
`)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
f.Name(),
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
2020-06-29 20:47:40 +00:00
|
|
|
require.Contains(t, ui.OutputWriter.String(),
|
|
|
|
`Config entry written: service-defaults/web`)
|
2019-04-30 23:27:16 +00:00
|
|
|
require.Equal(t, 0, code)
|
|
|
|
|
|
|
|
entry, _, err := client.ConfigEntries().Get("service-defaults", "web", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
svc, ok := entry.(*api.ServiceConfigEntry)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, api.ServiceDefaults, svc.Kind)
|
|
|
|
require.Equal(t, "web", svc.Name)
|
|
|
|
require.Equal(t, "udp", svc.Protocol)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Stdin", func(t *testing.T) {
|
|
|
|
stdinR, stdinW := io.Pipe()
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
c.testStdin = stdinR
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
stdinW.Write([]byte(`{
|
|
|
|
"Kind": "proxy-defaults",
|
|
|
|
"Name": "global",
|
|
|
|
"Config": {
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": 1.0
|
|
|
|
}
|
|
|
|
}`))
|
|
|
|
stdinW.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
2020-06-29 20:47:40 +00:00
|
|
|
require.Contains(t, ui.OutputWriter.String(),
|
|
|
|
`Config entry written: proxy-defaults/global`)
|
2019-04-30 23:27:16 +00:00
|
|
|
require.Equal(t, 0, code)
|
|
|
|
|
|
|
|
entry, _, err := client.ConfigEntries().Get(api.ProxyDefaults, api.ProxyConfigGlobal, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
proxy, ok := entry.(*api.ProxyConfigEntry)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, api.ProxyDefaults, proxy.Kind)
|
|
|
|
require.Equal(t, api.ProxyConfigGlobal, proxy.Name)
|
|
|
|
require.Equal(t, map[string]interface{}{"foo": "bar", "bar": 1.0}, proxy.Config)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("No config", func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
|
|
|
code := c.Run([]string{})
|
|
|
|
require.NotEqual(t, 0, code)
|
|
|
|
require.NotEmpty(t, ui.ErrorWriter.String())
|
|
|
|
})
|
2021-04-29 21:44:32 +00:00
|
|
|
|
|
|
|
t.Run("mesh config entry", func(t *testing.T) {
|
|
|
|
stdin := new(bytes.Buffer)
|
|
|
|
stdin.WriteString(`
|
|
|
|
kind = "mesh"
|
|
|
|
meta {
|
|
|
|
"foo" = "bar"
|
|
|
|
"gir" = "zim"
|
|
|
|
}
|
|
|
|
transparent_proxy {
|
2021-06-14 20:15:09 +00:00
|
|
|
mesh_destinations_only = true
|
2021-04-29 21:44:32 +00:00
|
|
|
}
|
2022-05-03 20:29:45 +00:00
|
|
|
http {
|
|
|
|
sanitize_x_forwarded_client_cert = true
|
|
|
|
}
|
2021-04-29 21:44:32 +00:00
|
|
|
`)
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
c.testStdin = stdin
|
|
|
|
|
|
|
|
code := c.Run([]string{"-http-addr=" + a.HTTPAddr(), "-"})
|
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
|
|
|
require.Contains(t, ui.OutputWriter.String(),
|
|
|
|
`Config entry written: mesh/mesh`)
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
|
|
|
|
entry, _, err := client.ConfigEntries().Get(api.MeshConfig, api.MeshConfigMesh, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
proxy, ok := entry.(*api.MeshConfigEntry)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, map[string]string{"foo": "bar", "gir": "zim"}, proxy.Meta)
|
2022-05-03 20:29:45 +00:00
|
|
|
require.True(t, proxy.TransparentProxy.MeshDestinationsOnly)
|
|
|
|
|
|
|
|
require.True(t, proxy.HTTP.SanitizeXForwardedClientCert)
|
2021-04-29 21:44:32 +00:00
|
|
|
})
|
2019-06-28 16:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func requireContainsLower(t *testing.T, haystack, needle string) {
|
|
|
|
t.Helper()
|
|
|
|
require.Contains(t, strings.ToLower(haystack), strings.ToLower(needle))
|
2019-04-30 23:27:16 +00:00
|
|
|
}
|