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"
|
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-02-07 01:50:51 +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) {
|
|
|
|
tmpFile, err := ioutil.TempFile("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
td, err := ioutil.TempDir("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
td, err := ioutil.TempDir("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
|
|
|
fp := filepath.Join(td, "config.json")
|
|
|
|
err = ioutil.WriteFile(fp, []byte(`{}`), 0644)
|
|
|
|
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) {
|
|
|
|
td, err := ioutil.TempDir("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filepath.Join(td, "config.json"), []byte(`{}`), 0644)
|
|
|
|
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) {
|
|
|
|
td, err := ioutil.TempDir("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filepath.Join(td, "config.json"), []byte{}, 0644)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|