github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/config_validate_test.go (about) 1 package command 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/hashicorp/nomad/ci" 9 "github.com/mitchellh/cli" 10 "github.com/shoenig/test/must" 11 ) 12 13 func TestConfigValidateCommand_FailWithEmptyDir(t *testing.T) { 14 ci.Parallel(t) 15 fh := t.TempDir() 16 17 ui := cli.NewMockUi() 18 cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}} 19 args := []string{fh} 20 21 code := cmd.Run(args) 22 must.One(t, code) 23 } 24 25 func TestConfigValidateCommand_SucceedWithMinimalConfigFile(t *testing.T) { 26 ci.Parallel(t) 27 fh := t.TempDir() 28 29 fp := filepath.Join(fh, "config.hcl") 30 err := os.WriteFile(fp, []byte(`data_dir="/" 31 client { 32 enabled = true 33 }`), 0644) 34 must.NoError(t, err) 35 36 ui := cli.NewMockUi() 37 cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}} 38 args := []string{fh} 39 40 code := cmd.Run(args) 41 must.Zero(t, code) 42 } 43 44 func TestConfigValidateCommand_FailOnParseBadConfigFile(t *testing.T) { 45 ci.Parallel(t) 46 fh := t.TempDir() 47 48 fp := filepath.Join(fh, "config.hcl") 49 err := os.WriteFile(fp, []byte(`a: b`), 0644) 50 must.NoError(t, err) 51 52 ui := cli.NewMockUi() 53 cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}} 54 args := []string{fh} 55 56 code := cmd.Run(args) 57 must.One(t, code) 58 } 59 60 func TestConfigValidateCommand_FailOnValidateParsableConfigFile(t *testing.T) { 61 ci.Parallel(t) 62 fh := t.TempDir() 63 64 fp := filepath.Join(fh, "config.hcl") 65 err := os.WriteFile(fp, []byte(`data_dir="../" 66 client { 67 enabled = true 68 }`), 0644) 69 must.NoError(t, err) 70 71 ui := cli.NewMockUi() 72 cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}} 73 args := []string{fh} 74 75 code := cmd.Run(args) 76 must.One(t, code) 77 }