From a2533d5d767b7ff3acbaa7d6990844932bd83206 Mon Sep 17 00:00:00 2001 From: Chris Cooper Date: Tue, 9 Feb 2016 10:35:39 -0500 Subject: [PATCH 1/2] fixes issue #1661 and adds supporting test --- command/agent/config.go | 4 ++++ command/configtest_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/command/agent/config.go b/command/agent/config.go index f56e3e740..ba72ddeba 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -1292,6 +1292,10 @@ func ReadConfigPaths(paths []string) (*Config, error) { if !strings.HasSuffix(fi.Name(), ".json") { continue } + // If the config file is empty, ignore it + if fi.Size() == 0 { + continue + } subpath := filepath.Join(path, fi.Name()) f, err := os.Open(subpath) diff --git a/command/configtest_test.go b/command/configtest_test.go index 43f629509..65e6e1753 100644 --- a/command/configtest_test.go +++ b/command/configtest_test.go @@ -103,3 +103,28 @@ func TestConfigTestCommandSucceedOnMinimalConfigDir(t *testing.T) { t.Fatalf("bad: %d", code) } } + +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) + } + + cmd := &ConfigTestCommand{ + Ui: new(cli.MockUi), + } + + args := []string{ + "-config-dir", td, + } + + if code := cmd.Run(args); code != 0 { + t.Fatalf("bad: %d", code) + } +} From 1327d9eff6856c2c3a1f0b38868918b84fee4a2c Mon Sep 17 00:00:00 2001 From: Chris Cooper Date: Tue, 9 Feb 2016 10:49:41 -0500 Subject: [PATCH 2/2] add missing test --- command/agent/config_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/command/agent/config_test.go b/command/agent/config_test.go index 89239fe74..6e0c446b2 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -1418,6 +1418,13 @@ func TestReadConfigPaths_dir(t *testing.T) { t.Fatalf("err: %s", err) } + // An empty file shouldn't be read + err = ioutil.WriteFile(filepath.Join(td, "d.json"), + []byte{}, 0664) + if err != nil { + t.Fatalf("err: %s", err) + } + config, err := ReadConfigPaths([]string{td}) if err != nil { t.Fatalf("err: %s", err)