2017-02-11 00:21:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/command/base"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testValidateCommand(t *testing.T) (*cli.MockUi, *ValidateCommand) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
return ui, &ValidateCommand{
|
|
|
|
Command: base.Command{
|
2017-04-21 00:02:42 +00:00
|
|
|
UI: ui,
|
2017-02-11 00:21:51 +00:00
|
|
|
Flags: base.FlagSetNone,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateCommand_implements(t *testing.T) {
|
|
|
|
var _ cli.Command = &ValidateCommand{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateCommandFailOnEmptyFile(t *testing.T) {
|
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())
|
|
|
|
|
|
|
|
_, cmd := testValidateCommand(t)
|
|
|
|
|
|
|
|
args := []string{tmpFile.Name()}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code == 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateCommandSucceedOnEmptyDir(t *testing.T) {
|
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)
|
|
|
|
|
|
|
|
ui, cmd := testValidateCommand(t)
|
|
|
|
|
|
|
|
args := []string{td}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d, %s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateCommandSucceedOnMinimalConfigFile(t *testing.T) {
|
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-05-12 13:41:13 +00:00
|
|
|
err := ioutil.WriteFile(fp, []byte(`{}`), 0644)
|
2017-02-11 00:21:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, cmd := testValidateCommand(t)
|
|
|
|
|
|
|
|
args := []string{fp}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateCommandSucceedOnMinimalConfigDir(t *testing.T) {
|
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-05-12 13:41:13 +00:00
|
|
|
err := ioutil.WriteFile(filepath.Join(td, "config.json"), []byte(`{}`), 0644)
|
2017-02-11 00:21:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, cmd := testValidateCommand(t)
|
|
|
|
|
|
|
|
args := []string{td}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateCommandSucceedOnConfigDirWithEmptyFile(t *testing.T) {
|
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-05-12 13:41:13 +00:00
|
|
|
err := ioutil.WriteFile(filepath.Join(td, "config.json"), []byte{}, 0644)
|
2017-02-11 00:21:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, cmd := testValidateCommand(t)
|
|
|
|
|
|
|
|
args := []string{td}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
2017-02-11 01:14:22 +00:00
|
|
|
|
|
|
|
func TestValidateCommandQuiet(t *testing.T) {
|
2017-05-12 13:41:13 +00:00
|
|
|
td := testutil.TempDir(t, "consul")
|
2017-02-11 01:14:22 +00:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
|
|
|
ui, cmd := testValidateCommand(t)
|
|
|
|
|
|
|
|
args := []string{"-quiet", td}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d, %s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
if ui.OutputWriter.String() != "<nil>" {
|
|
|
|
t.Fatalf("bad: %v", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|