2015-05-04 18:41:19 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
2017-05-12 13:41:13 +00:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
2015-05-04 18:41:19 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2017-02-07 01:50:51 +00:00
|
|
|
func testConfigTestCommand(t *testing.T) (*cli.MockUi, *ConfigTestCommand) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
return ui, &ConfigTestCommand{
|
2017-02-08 00:16:33 +00:00
|
|
|
Command: base.Command{
|
2017-04-21 00:02:42 +00:00
|
|
|
UI: ui,
|
2017-02-08 00:16:33 +00:00
|
|
|
Flags: base.FlagSetNone,
|
2017-02-07 01:50:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 18:41:19 +00:00
|
|
|
func TestConfigTestCommand_implements(t *testing.T) {
|
|
|
|
var _ cli.Command = &ConfigTestCommand{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigTestCommandFailOnEmptyFile(t *testing.T) {
|
2017-05-12 13:41:13 +00:00
|
|
|
tmpFile := testutil.TempFile(t, "consul")
|
2015-05-04 18:41:19 +00:00
|
|
|
defer os.RemoveAll(tmpFile.Name())
|
|
|
|
|
2017-02-07 01:50:51 +00:00
|
|
|
_, cmd := testConfigTestCommand(t)
|
2015-05-04 18:41:19 +00:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-config-file", tmpFile.Name(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code == 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigTestCommandSucceedOnEmptyDir(t *testing.T) {
|
2017-05-12 13:41:13 +00:00
|
|
|
td := testutil.TempDir(t, "consul")
|
2015-05-04 18:41:19 +00:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
2017-02-07 01:50:51 +00:00
|
|
|
ui, cmd := testConfigTestCommand(t)
|
2015-05-04 18:41:19 +00:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-config-dir", td,
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
2017-02-07 01:50:51 +00:00
|
|
|
t.Fatalf("bad: %d, %s", code, ui.ErrorWriter.String())
|
2015-05-04 18:41:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigTestCommandSucceedOnMinimalConfigFile(t *testing.T) {
|
2017-05-12 13:41:13 +00:00
|
|
|
td := testutil.TempDir(t, "consul")
|
2015-05-04 18:41:19 +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)
|
2015-05-04 18:41:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-02-07 01:50:51 +00:00
|
|
|
_, cmd := testConfigTestCommand(t)
|
2015-05-04 18:41:19 +00:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-config-file", fp,
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigTestCommandSucceedOnMinimalConfigDir(t *testing.T) {
|
2017-05-12 13:41:13 +00:00
|
|
|
td := testutil.TempDir(t, "consul")
|
2015-05-04 18:41:19 +00:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
err := ioutil.WriteFile(filepath.Join(td, "config.json"), []byte(`{}`), 0644)
|
2015-05-04 18:41:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-02-07 01:50:51 +00:00
|
|
|
_, cmd := testConfigTestCommand(t)
|
2015-05-04 18:41:19 +00:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-config-dir", td,
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|
2016-02-09 15:35:39 +00:00
|
|
|
|
|
|
|
func TestConfigTestCommandSucceedOnConfigDirWithEmptyFile(t *testing.T) {
|
2017-05-12 13:41:13 +00:00
|
|
|
td := testutil.TempDir(t, "consul")
|
2016-02-09 15:35:39 +00:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
err := ioutil.WriteFile(filepath.Join(td, "config.json"), []byte{}, 0644)
|
2016-02-09 15:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-02-07 01:50:51 +00:00
|
|
|
_, cmd := testConfigTestCommand(t)
|
2016-02-09 15:35:39 +00:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-config-dir", td,
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
|
|
|
}
|