Merge pull request #903 from prometheus/fabxc/cfgnil

Fix empty configuration file cases
This commit is contained in:
Fabian Reinartz 2015-07-18 08:05:55 +02:00
commit 5a3712fe1b
2 changed files with 44 additions and 0 deletions

View file

@ -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
@ -142,6 +147,12 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := unmarshal((*plain)(c)); err != nil {
return err
}
// If a global block was open but empty the default global config is overwritten.
// We have to restore it here.
if c.GlobalConfig.isZero() {
c.GlobalConfig = DefaultGlobalConfig
}
for _, rf := range c.RuleFiles {
if !patRulePath.MatchString(rf) {
return fmt.Errorf("invalid rule file path %q", rf)
@ -191,6 +202,14 @@ func (c *GlobalConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
return checkOverflow(c.XXX, "global config")
}
// isZero returns true iff the global config is the zero value.
func (c *GlobalConfig) isZero() bool {
return c.Labels == nil &&
c.ScrapeInterval == 0 &&
c.ScrapeTimeout == 0 &&
c.EvaluationInterval == 0
}
// ScrapeConfig configures a scraping unit for Prometheus.
type ScrapeConfig struct {
// The job name to which the job label is set by default.

View file

@ -249,3 +249,28 @@ 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)
}
}
func TestEmptyGlobalBlock(t *testing.T) {
c, err := Load("global:\n")
if err != nil {
t.Fatalf("Unexpected error parsing empty config file: %s", err)
}
exp := DefaultConfig
exp.original = "global:\n"
if !reflect.DeepEqual(*c, exp) {
t.Fatalf("want %v, got %v", exp, c)
}
}