From f6c33a234744f7598354bfe1106a50def51aff79 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Sun, 7 Jun 2015 17:40:22 +0200 Subject: [PATCH] config: prevent overwrite of DefaultGlobalConfig --- config/config.go | 6 +++--- config/config_test.go | 8 +++++++- config/testdata/global_timeout.good.yml | 2 ++ 3 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 config/testdata/global_timeout.good.yml diff --git a/config/config.go b/config/config.go index c94eda712..03aff55cc 100644 --- a/config/config.go +++ b/config/config.go @@ -46,7 +46,7 @@ func LoadFromFile(filename string) (*Config, error) { var ( // The default top-level configuration. DefaultConfig = Config{ - GlobalConfig: &DefaultGlobalConfig, + GlobalConfig: DefaultGlobalConfig, } // The default global configuration. @@ -56,7 +56,7 @@ var ( EvaluationInterval: Duration(1 * time.Minute), } - // Te default scrape configuration. + // The default scrape configuration. DefaultScrapeConfig = ScrapeConfig{ // ScrapeTimeout and ScrapeInterval default to the // configured globals. @@ -89,7 +89,7 @@ var ( // Config is the top-level configuration for Prometheus's config files. type Config struct { - GlobalConfig *GlobalConfig `yaml:"global"` + GlobalConfig GlobalConfig `yaml:"global"` RuleFiles []string `yaml:"rule_files,omitempty"` ScrapeConfigs []*ScrapeConfig `yaml:"scrape_configs,omitempty"` diff --git a/config/config_test.go b/config/config_test.go index 13a7ee54a..b72e2f97e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -13,7 +13,7 @@ import ( ) var expectedConf = &Config{ - GlobalConfig: &GlobalConfig{ + GlobalConfig: GlobalConfig{ ScrapeInterval: Duration(15 * time.Second), ScrapeTimeout: DefaultGlobalConfig.ScrapeTimeout, EvaluationInterval: Duration(30 * time.Second), @@ -118,6 +118,12 @@ var expectedConf = &Config{ } func TestLoadConfig(t *testing.T) { + // Parse a valid file that sets a global scrape timeout. This tests whether parsing + // an overwritten default field in the global config permanently changes the default. + if _, err := LoadFromFile("testdata/global_timeout.good.yml"); err != nil { + t.Errorf("Error parsing %s: %s", "testdata/conf.good.yml", err) + } + c, err := LoadFromFile("testdata/conf.good.yml") if err != nil { t.Errorf("Error parsing %s: %s", "testdata/conf.good.yml", err) diff --git a/config/testdata/global_timeout.good.yml b/config/testdata/global_timeout.good.yml new file mode 100644 index 000000000..2ad107136 --- /dev/null +++ b/config/testdata/global_timeout.good.yml @@ -0,0 +1,2 @@ +global: + scrape_timeout: 1h