open-nomad/command/config_validate_test.go
Seth Hoenig c5d36eaa2f cleanup: fixing warnings and refactoring of command package, part 2
This PR continues the cleanup of the command package, removing linter
warnings, refactoring to use helpers, making tests easier to read, etc.
2022-08-18 09:43:39 -05:00

78 lines
1.5 KiB
Go

package command
import (
"os"
"path/filepath"
"testing"
"github.com/hashicorp/nomad/ci"
"github.com/mitchellh/cli"
"github.com/shoenig/test/must"
)
func TestConfigValidateCommand_FailWithEmptyDir(t *testing.T) {
ci.Parallel(t)
fh := t.TempDir()
ui := cli.NewMockUi()
cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}}
args := []string{fh}
code := cmd.Run(args)
must.One(t, code)
}
func TestConfigValidateCommand_SucceedWithMinimalConfigFile(t *testing.T) {
ci.Parallel(t)
fh := t.TempDir()
fp := filepath.Join(fh, "config.hcl")
err := os.WriteFile(fp, []byte(`data_dir="/"
client {
enabled = true
}`), 0644)
must.NoError(t, err)
ui := cli.NewMockUi()
cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}}
args := []string{fh}
code := cmd.Run(args)
must.Zero(t, code)
}
func TestConfigValidateCommand_FailOnParseBadConfigFile(t *testing.T) {
ci.Parallel(t)
fh := t.TempDir()
fp := filepath.Join(fh, "config.hcl")
err := os.WriteFile(fp, []byte(`a: b`), 0644)
must.NoError(t, err)
ui := cli.NewMockUi()
cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}}
args := []string{fh}
code := cmd.Run(args)
must.One(t, code)
}
func TestConfigValidateCommand_FailOnValidateParsableConfigFile(t *testing.T) {
ci.Parallel(t)
fh := t.TempDir()
fp := filepath.Join(fh, "config.hcl")
err := os.WriteFile(fp, []byte(`data_dir="../"
client {
enabled = true
}`), 0644)
must.NoError(t, err)
ui := cli.NewMockUi()
cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}}
args := []string{fh}
code := cmd.Run(args)
must.One(t, code)
}