2017-10-11 12:51:20 +00:00
|
|
|
package validate
|
2017-02-11 00:21:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-10-11 12:51:20 +00:00
|
|
|
"strings"
|
2017-02-11 00:21:51 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-03-30 14:55:21 +00:00
|
|
|
require "github.com/stretchr/testify/require"
|
2017-05-12 13:41:13 +00:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
2017-02-11 00:21:51 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2017-10-11 12:51:20 +00:00
|
|
|
func TestValidateCommand_noTabs(t *testing.T) {
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-10-11 12:51:20 +00:00
|
|
|
if strings.ContainsRune(New(nil).Help(), '\t') {
|
2017-10-17 13:44:20 +00:00
|
|
|
t.Fatal("help has tabs")
|
2017-02-11 00:21:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 12:04:51 +00:00
|
|
|
func TestValidateCommand_FailOnEmptyFile(t *testing.T) {
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-05-12 13:41:13 +00:00
|
|
|
tmpFile := testutil.TempFile(t, "consul")
|
2017-02-11 00:21:51 +00:00
|
|
|
defer os.RemoveAll(tmpFile.Name())
|
|
|
|
|
2017-10-11 12:51:20 +00:00
|
|
|
cmd := New(cli.NewMockUi())
|
2017-02-11 00:21:51 +00:00
|
|
|
args := []string{tmpFile.Name()}
|
|
|
|
|
2018-03-30 14:55:21 +00:00
|
|
|
code := cmd.Run(args)
|
2018-04-02 13:23:01 +00:00
|
|
|
require.NotEqual(t, 0, code)
|
2017-02-11 00:21:51 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 12:04:51 +00:00
|
|
|
func TestValidateCommand_SucceedOnMinimalConfigFile(t *testing.T) {
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-05-12 13:41:13 +00:00
|
|
|
td := testutil.TempDir(t, "consul")
|
2017-02-11 00:21:51 +00:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
|
|
|
fp := filepath.Join(td, "config.json")
|
2017-09-25 18:40:42 +00:00
|
|
|
err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644)
|
2018-03-30 14:55:21 +00:00
|
|
|
require.Nilf(t, err, "err: %s", err)
|
2017-02-11 00:21:51 +00:00
|
|
|
|
2017-10-11 12:51:20 +00:00
|
|
|
cmd := New(cli.NewMockUi())
|
2017-02-11 00:21:51 +00:00
|
|
|
args := []string{fp}
|
|
|
|
|
2018-03-30 14:55:21 +00:00
|
|
|
code := cmd.Run(args)
|
2018-04-02 13:23:01 +00:00
|
|
|
require.Equal(t, 0, code)
|
2017-02-11 00:21:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 18:35:49 +00:00
|
|
|
func TestValidateCommand_SucceedWithMinimalJSONConfigFormat(t *testing.T) {
|
2018-03-29 18:30:05 +00:00
|
|
|
t.Parallel()
|
|
|
|
td := testutil.TempDir(t, "consul")
|
|
|
|
defer os.RemoveAll(td)
|
2018-03-29 18:35:49 +00:00
|
|
|
|
2018-03-29 18:30:05 +00:00
|
|
|
fp := filepath.Join(td, "json.conf")
|
|
|
|
err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644)
|
2018-03-30 14:55:21 +00:00
|
|
|
require.Nilf(t, err, "err: %s", err)
|
2018-03-29 18:35:49 +00:00
|
|
|
|
2018-03-29 18:30:05 +00:00
|
|
|
cmd := New(cli.NewMockUi())
|
|
|
|
args := []string{"--config-format", "json", fp}
|
2018-03-29 18:35:49 +00:00
|
|
|
|
2018-03-30 14:55:21 +00:00
|
|
|
code := cmd.Run(args)
|
2018-04-02 13:23:01 +00:00
|
|
|
require.Equal(t, 0, code)
|
2018-03-29 18:30:05 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 18:35:49 +00:00
|
|
|
func TestValidateCommand_SucceedWithMinimalHCLConfigFormat(t *testing.T) {
|
2018-03-29 18:30:05 +00:00
|
|
|
t.Parallel()
|
|
|
|
td := testutil.TempDir(t, "consul")
|
|
|
|
defer os.RemoveAll(td)
|
2018-03-29 18:35:49 +00:00
|
|
|
|
2018-03-29 18:30:05 +00:00
|
|
|
fp := filepath.Join(td, "hcl.conf")
|
|
|
|
err := ioutil.WriteFile(fp, []byte("bind_addr = \"10.0.0.1\"\ndata_dir = \""+td+"\""), 0644)
|
2018-03-30 14:55:21 +00:00
|
|
|
require.Nilf(t, err, "err: %s", err)
|
|
|
|
|
2018-03-29 18:35:49 +00:00
|
|
|
|
2018-03-29 18:30:05 +00:00
|
|
|
cmd := New(cli.NewMockUi())
|
|
|
|
args := []string{"--config-format", "hcl", fp}
|
2018-03-29 18:35:49 +00:00
|
|
|
|
2018-03-30 14:55:21 +00:00
|
|
|
code := cmd.Run(args)
|
2018-04-02 13:23:01 +00:00
|
|
|
require.Equal(t, 0, code)
|
2018-03-29 18:30:05 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 18:35:49 +00:00
|
|
|
func TestValidateCommand_SucceedWithJSONAsHCL(t *testing.T) {
|
2018-03-29 18:30:05 +00:00
|
|
|
t.Parallel()
|
|
|
|
td := testutil.TempDir(t, "consul")
|
|
|
|
defer os.RemoveAll(td)
|
2018-03-29 18:35:49 +00:00
|
|
|
|
2018-03-29 18:30:05 +00:00
|
|
|
fp := filepath.Join(td, "json.conf")
|
|
|
|
err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644)
|
2018-03-30 14:55:21 +00:00
|
|
|
require.Nilf(t, err, "err: %s", err)
|
2018-03-29 18:35:49 +00:00
|
|
|
|
2018-03-29 18:30:05 +00:00
|
|
|
cmd := New(cli.NewMockUi())
|
|
|
|
args := []string{"--config-format", "hcl", fp}
|
2018-03-29 18:35:49 +00:00
|
|
|
|
2018-03-30 14:55:21 +00:00
|
|
|
code := cmd.Run(args)
|
2018-04-02 13:23:01 +00:00
|
|
|
require.Equal(t, 0, code)
|
2018-03-29 18:30:05 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 12:04:51 +00:00
|
|
|
func TestValidateCommand_SucceedOnMinimalConfigDir(t *testing.T) {
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-05-12 13:41:13 +00:00
|
|
|
td := testutil.TempDir(t, "consul")
|
2017-02-11 00:21:51 +00:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
2017-09-25 18:40:42 +00:00
|
|
|
err := ioutil.WriteFile(filepath.Join(td, "config.json"), []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644)
|
2018-03-30 14:55:21 +00:00
|
|
|
require.Nilf(t, err, "err: %s", err)
|
2017-02-11 00:21:51 +00:00
|
|
|
|
2017-10-11 12:51:20 +00:00
|
|
|
cmd := New(cli.NewMockUi())
|
2017-02-11 00:21:51 +00:00
|
|
|
args := []string{td}
|
|
|
|
|
2018-03-30 14:55:21 +00:00
|
|
|
code := cmd.Run(args)
|
2018-04-02 13:23:01 +00:00
|
|
|
require.Equal(t, 0, code)
|
2017-02-11 00:21:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 18:35:49 +00:00
|
|
|
func TestValidateCommand_FailForInvalidJSONConfigFormat(t *testing.T) {
|
2018-03-29 18:30:05 +00:00
|
|
|
t.Parallel()
|
|
|
|
td := testutil.TempDir(t, "consul")
|
|
|
|
defer os.RemoveAll(td)
|
2018-03-29 18:35:49 +00:00
|
|
|
|
2018-03-29 18:30:05 +00:00
|
|
|
fp := filepath.Join(td, "hcl.conf")
|
|
|
|
err := ioutil.WriteFile(fp, []byte(`bind_addr = "10.0.0.1"\ndata_dir = "`+td+`"`), 0644)
|
2018-03-30 14:55:21 +00:00
|
|
|
require.Nilf(t, err, "err: %s", err)
|
2018-03-29 18:35:49 +00:00
|
|
|
|
2018-03-29 18:30:05 +00:00
|
|
|
cmd := New(cli.NewMockUi())
|
|
|
|
args := []string{"--config-format", "json", fp}
|
2018-03-29 18:35:49 +00:00
|
|
|
|
2018-03-30 14:55:21 +00:00
|
|
|
code := cmd.Run(args)
|
2018-04-02 13:23:01 +00:00
|
|
|
require.NotEqual(t, 0, code)
|
2018-03-29 18:30:05 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 12:04:51 +00:00
|
|
|
func TestValidateCommand_Quiet(t *testing.T) {
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-05-12 13:41:13 +00:00
|
|
|
td := testutil.TempDir(t, "consul")
|
2017-02-11 00:21:51 +00:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
2017-09-25 18:40:42 +00:00
|
|
|
fp := filepath.Join(td, "config.json")
|
|
|
|
err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644)
|
2018-03-30 14:55:21 +00:00
|
|
|
require.Nilf(t, err, "err: %s", err)
|
2017-02-11 00:21:51 +00:00
|
|
|
|
2017-10-11 12:51:20 +00:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := New(ui)
|
2017-02-11 01:14:22 +00:00
|
|
|
args := []string{"-quiet", td}
|
|
|
|
|
2018-03-30 14:55:21 +00:00
|
|
|
code := cmd.Run(args)
|
2018-04-02 13:23:01 +00:00
|
|
|
require.Equalf(t, 0, code, "return code - expected: 0, bad: %d, %s", code, ui.ErrorWriter.String())
|
|
|
|
require.Equal(t, "", ui.OutputWriter.String())
|
2017-02-11 01:14:22 +00:00
|
|
|
}
|