2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-02-08 21:52:35 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-08-18 13:51:53 +00:00
|
|
|
"os"
|
2022-02-08 21:52:35 +00:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2022-02-08 21:52:35 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2022-08-18 13:51:53 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2022-02-08 21:52:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestConfigValidateCommand_FailWithEmptyDir(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2022-05-12 15:42:40 +00:00
|
|
|
fh := t.TempDir()
|
2022-02-08 21:52:35 +00:00
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}}
|
|
|
|
args := []string{fh}
|
|
|
|
|
|
|
|
code := cmd.Run(args)
|
2022-08-18 13:51:53 +00:00
|
|
|
must.One(t, code)
|
2022-02-08 21:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigValidateCommand_SucceedWithMinimalConfigFile(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2022-05-12 15:42:40 +00:00
|
|
|
fh := t.TempDir()
|
2022-02-08 21:52:35 +00:00
|
|
|
|
|
|
|
fp := filepath.Join(fh, "config.hcl")
|
2022-08-18 13:51:53 +00:00
|
|
|
err := os.WriteFile(fp, []byte(`data_dir="/"
|
2022-02-08 21:52:35 +00:00
|
|
|
client {
|
|
|
|
enabled = true
|
|
|
|
}`), 0644)
|
2022-08-18 13:51:53 +00:00
|
|
|
must.NoError(t, err)
|
2022-02-08 21:52:35 +00:00
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}}
|
|
|
|
args := []string{fh}
|
|
|
|
|
|
|
|
code := cmd.Run(args)
|
2022-08-18 13:51:53 +00:00
|
|
|
must.Zero(t, code)
|
2022-02-08 21:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigValidateCommand_FailOnParseBadConfigFile(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2022-05-12 15:42:40 +00:00
|
|
|
fh := t.TempDir()
|
2022-02-08 21:52:35 +00:00
|
|
|
|
|
|
|
fp := filepath.Join(fh, "config.hcl")
|
2022-08-18 13:51:53 +00:00
|
|
|
err := os.WriteFile(fp, []byte(`a: b`), 0644)
|
|
|
|
must.NoError(t, err)
|
2022-02-08 21:52:35 +00:00
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}}
|
|
|
|
args := []string{fh}
|
|
|
|
|
|
|
|
code := cmd.Run(args)
|
2022-08-18 13:51:53 +00:00
|
|
|
must.One(t, code)
|
2022-02-08 21:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigValidateCommand_FailOnValidateParsableConfigFile(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2022-05-12 15:42:40 +00:00
|
|
|
fh := t.TempDir()
|
2022-02-08 21:52:35 +00:00
|
|
|
|
|
|
|
fp := filepath.Join(fh, "config.hcl")
|
2022-08-18 13:51:53 +00:00
|
|
|
err := os.WriteFile(fp, []byte(`data_dir="../"
|
2022-02-08 21:52:35 +00:00
|
|
|
client {
|
2022-08-18 13:51:53 +00:00
|
|
|
enabled = true
|
2022-02-08 21:52:35 +00:00
|
|
|
}`), 0644)
|
2022-08-18 13:51:53 +00:00
|
|
|
must.NoError(t, err)
|
2022-02-08 21:52:35 +00:00
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &ConfigValidateCommand{Meta: Meta{Ui: ui}}
|
|
|
|
args := []string{fh}
|
|
|
|
|
|
|
|
code := cmd.Run(args)
|
2022-08-18 13:51:53 +00:00
|
|
|
must.One(t, code)
|
2022-02-08 21:52:35 +00:00
|
|
|
}
|