github.hscsec.cn/hashicorp/consul@v1.4.5/command/validate/validate_test.go (about) 1 package validate 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/hashicorp/consul/testutil" 11 "github.com/mitchellh/cli" 12 require "github.com/stretchr/testify/require" 13 ) 14 15 func TestValidateCommand_noTabs(t *testing.T) { 16 t.Parallel() 17 if strings.ContainsRune(New(nil).Help(), '\t') { 18 t.Fatal("help has tabs") 19 } 20 } 21 22 func TestValidateCommand_FailOnEmptyFile(t *testing.T) { 23 t.Parallel() 24 tmpFile := testutil.TempFile(t, "consul") 25 defer os.RemoveAll(tmpFile.Name()) 26 27 cmd := New(cli.NewMockUi()) 28 args := []string{tmpFile.Name()} 29 30 code := cmd.Run(args) 31 require.NotEqual(t, 0, code) 32 } 33 34 func TestValidateCommand_SucceedOnMinimalConfigFile(t *testing.T) { 35 t.Parallel() 36 td := testutil.TempDir(t, "consul") 37 defer os.RemoveAll(td) 38 39 fp := filepath.Join(td, "config.json") 40 err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644) 41 require.Nilf(t, err, "err: %s", err) 42 43 cmd := New(cli.NewMockUi()) 44 args := []string{fp} 45 46 code := cmd.Run(args) 47 require.Equal(t, 0, code) 48 } 49 50 func TestValidateCommand_SucceedWithMinimalJSONConfigFormat(t *testing.T) { 51 t.Parallel() 52 td := testutil.TempDir(t, "consul") 53 defer os.RemoveAll(td) 54 55 fp := filepath.Join(td, "json.conf") 56 err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644) 57 require.Nilf(t, err, "err: %s", err) 58 59 cmd := New(cli.NewMockUi()) 60 args := []string{"--config-format", "json", fp} 61 62 code := cmd.Run(args) 63 require.Equal(t, 0, code) 64 } 65 66 func TestValidateCommand_SucceedWithMinimalHCLConfigFormat(t *testing.T) { 67 t.Parallel() 68 td := testutil.TempDir(t, "consul") 69 defer os.RemoveAll(td) 70 71 fp := filepath.Join(td, "hcl.conf") 72 err := ioutil.WriteFile(fp, []byte("bind_addr = \"10.0.0.1\"\ndata_dir = \""+td+"\""), 0644) 73 require.Nilf(t, err, "err: %s", err) 74 75 cmd := New(cli.NewMockUi()) 76 args := []string{"--config-format", "hcl", fp} 77 78 code := cmd.Run(args) 79 require.Equal(t, 0, code) 80 } 81 82 func TestValidateCommand_SucceedWithJSONAsHCL(t *testing.T) { 83 t.Parallel() 84 td := testutil.TempDir(t, "consul") 85 defer os.RemoveAll(td) 86 87 fp := filepath.Join(td, "json.conf") 88 err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644) 89 require.Nilf(t, err, "err: %s", err) 90 91 cmd := New(cli.NewMockUi()) 92 args := []string{"--config-format", "hcl", fp} 93 94 code := cmd.Run(args) 95 require.Equal(t, 0, code) 96 } 97 98 func TestValidateCommand_SucceedOnMinimalConfigDir(t *testing.T) { 99 t.Parallel() 100 td := testutil.TempDir(t, "consul") 101 defer os.RemoveAll(td) 102 103 err := ioutil.WriteFile(filepath.Join(td, "config.json"), []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644) 104 require.Nilf(t, err, "err: %s", err) 105 106 cmd := New(cli.NewMockUi()) 107 args := []string{td} 108 109 code := cmd.Run(args) 110 require.Equal(t, 0, code) 111 } 112 113 func TestValidateCommand_FailForInvalidJSONConfigFormat(t *testing.T) { 114 t.Parallel() 115 td := testutil.TempDir(t, "consul") 116 defer os.RemoveAll(td) 117 118 fp := filepath.Join(td, "hcl.conf") 119 err := ioutil.WriteFile(fp, []byte(`bind_addr = "10.0.0.1"\ndata_dir = "`+td+`"`), 0644) 120 require.Nilf(t, err, "err: %s", err) 121 122 cmd := New(cli.NewMockUi()) 123 args := []string{"--config-format", "json", fp} 124 125 code := cmd.Run(args) 126 require.NotEqual(t, 0, code) 127 } 128 129 func TestValidateCommand_Quiet(t *testing.T) { 130 t.Parallel() 131 td := testutil.TempDir(t, "consul") 132 defer os.RemoveAll(td) 133 134 fp := filepath.Join(td, "config.json") 135 err := ioutil.WriteFile(fp, []byte(`{"bind_addr":"10.0.0.1", "data_dir":"`+td+`"}`), 0644) 136 require.Nilf(t, err, "err: %s", err) 137 138 ui := cli.NewMockUi() 139 cmd := New(ui) 140 args := []string{"-quiet", td} 141 142 code := cmd.Run(args) 143 require.Equalf(t, 0, code, "return code - expected: 0, bad: %d, %s", code, ui.ErrorWriter.String()) 144 require.Equal(t, "", ui.OutputWriter.String()) 145 }