From 2a53b107c18e97d6240e2e94ac63fae18e0c4277 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Fri, 17 Jul 2015 16:12:33 +0200 Subject: [PATCH] Fix missing defaults in empty configurations --- config/config.go | 5 +++++ config/config_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/config/config.go b/config/config.go index 888d5d628..eddd44087 100644 --- a/config/config.go +++ b/config/config.go @@ -26,6 +26,11 @@ var ( // Load parses the YAML input s into a Config. func Load(s string) (*Config, error) { cfg := &Config{} + // If the entire config body is empty the UnmarshalYAML method is + // never called. We thus have to set the DefaultConfig at the entry + // point as well. + *cfg = DefaultConfig + err := yaml.Unmarshal([]byte(s), cfg) if err != nil { return nil, err diff --git a/config/config_test.go b/config/config_test.go index bba5fea1a..d7aab0b48 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -249,3 +249,15 @@ func TestBadTargetGroup(t *testing.T) { t.Errorf("Expected unmarshal error but got none.") } } + +func TestEmptyConfig(t *testing.T) { + c, err := Load("") + if err != nil { + t.Fatalf("Unexpected error parsing empty config file: %s", err) + } + exp := DefaultConfig + + if !reflect.DeepEqual(*c, exp) { + t.Fatalf("want %v, got %v", exp, c) + } +} -- GitLab