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"
|
|
|
|
|
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()}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code == 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2017-02-11 00:21:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:51:20 +00:00
|
|
|
cmd := New(cli.NewMockUi())
|
2017-02-11 00:21:51 +00:00
|
|
|
args := []string{fp}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 18:30:05 +00:00
|
|
|
func TestValidateCommand_SucceedWithMinimalJSONConfigFormat(t * testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
td := testutil.TempDir(t, "consul")
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
|
|
|
fp := filepath.Join(td, "json.conf")
|
|
|
|
err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := New(cli.NewMockUi())
|
|
|
|
args := []string{"--config-format", "json", fp}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateCommand_SucceedWithMinimalHCLConfigFormat(t * testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
td := testutil.TempDir(t, "consul")
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
|
|
|
fp := filepath.Join(td, "hcl.conf")
|
|
|
|
err := ioutil.WriteFile(fp, []byte("bind_addr = \"10.0.0.1\"\ndata_dir = \""+td+"\""), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := New(cli.NewMockUi())
|
|
|
|
args := []string{"--config-format", "hcl", fp}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateCommand_SucceedWithJSONAsHCL(t * testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
td := testutil.TempDir(t, "consul")
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
|
|
|
fp := filepath.Join(td, "json.conf")
|
|
|
|
err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := New(cli.NewMockUi())
|
|
|
|
args := []string{"--config-format", "hcl", fp}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2017-02-11 00:21:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:51:20 +00:00
|
|
|
cmd := New(cli.NewMockUi())
|
2017-02-11 00:21:51 +00:00
|
|
|
args := []string{td}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 18:30:05 +00:00
|
|
|
func TestValidateCommand_FailForInvalidJSONConfigFormat(t * testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
td := testutil.TempDir(t, "consul")
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
|
|
|
fp := filepath.Join(td, "hcl.conf")
|
|
|
|
err := ioutil.WriteFile(fp, []byte(`bind_addr = "10.0.0.1"\ndata_dir = "`+td+`"`), 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := New(cli.NewMockUi())
|
|
|
|
args := []string{"--config-format", "json", fp}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code == 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2017-02-11 00:21:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d, %s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
2017-05-22 23:43:51 +00:00
|
|
|
if ui.OutputWriter.String() != "" {
|
2017-02-11 01:14:22 +00:00
|
|
|
t.Fatalf("bad: %v", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|