Merge pull request #3269 from alcortesm/issue-3242-config

Fix issue 3242 for package config
This commit is contained in:
Tobias Schmidt 2017-10-13 00:11:07 +02:00 committed by GitHub
commit 7515a6de03

View file

@ -18,13 +18,13 @@ import (
"io/ioutil" "io/ioutil"
"net/url" "net/url"
"path/filepath" "path/filepath"
"reflect"
"regexp" "regexp"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/util/testutil"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@ -517,73 +517,40 @@ var expectedConf = &Config{
func TestLoadConfig(t *testing.T) { func TestLoadConfig(t *testing.T) {
// Parse a valid file that sets a global scrape timeout. This tests whether parsing // 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. // an overwritten default field in the global config permanently changes the default.
if _, err := LoadFile("testdata/global_timeout.good.yml"); err != nil { _, err := LoadFile("testdata/global_timeout.good.yml")
t.Errorf("Error parsing %s: %s", "testdata/global_timeout.good.yml", err) testutil.Ok(t, err)
}
c, err := LoadFile("testdata/conf.good.yml") c, err := LoadFile("testdata/conf.good.yml")
if err != nil { testutil.Ok(t, err)
t.Fatalf("Error parsing %s: %s", "testdata/conf.good.yml", err)
}
bgot, err := yaml.Marshal(c)
if err != nil {
t.Fatalf("%s", err)
}
bexp, err := yaml.Marshal(expectedConf)
if err != nil {
t.Fatalf("%s", err)
}
expectedConf.original = c.original expectedConf.original = c.original
testutil.Equals(t, expectedConf, c)
if !reflect.DeepEqual(c, expectedConf) {
t.Fatalf("%s: unexpected config result: \n\n%s\n expected\n\n%s", "testdata/conf.good.yml", bgot, bexp)
}
} }
// YAML marshalling must not reveal authentication credentials. // YAML marshalling must not reveal authentication credentials.
func TestElideSecrets(t *testing.T) { func TestElideSecrets(t *testing.T) {
c, err := LoadFile("testdata/conf.good.yml") c, err := LoadFile("testdata/conf.good.yml")
if err != nil { testutil.Ok(t, err)
t.Fatalf("Error parsing %s: %s", "testdata/conf.good.yml", err)
}
secretRe := regexp.MustCompile(`\\u003csecret\\u003e|<secret>`) secretRe := regexp.MustCompile(`\\u003csecret\\u003e|<secret>`)
config, err := yaml.Marshal(c) config, err := yaml.Marshal(c)
if err != nil { testutil.Ok(t, err)
t.Fatal(err)
}
yamlConfig := string(config) yamlConfig := string(config)
matches := secretRe.FindAllStringIndex(yamlConfig, -1) matches := secretRe.FindAllStringIndex(yamlConfig, -1)
if len(matches) != 6 || strings.Contains(yamlConfig, "mysecret") { testutil.Assert(t, len(matches) == 6, "wrong number of secret matches found")
t.Fatalf("yaml marshal reveals authentication credentials.") testutil.Assert(t, !strings.Contains(yamlConfig, "mysecret"),
} "yaml marshal reveals authentication credentials.")
} }
func TestLoadConfigRuleFilesAbsolutePath(t *testing.T) { func TestLoadConfigRuleFilesAbsolutePath(t *testing.T) {
// Parse a valid file that sets a rule files with an absolute path // Parse a valid file that sets a rule files with an absolute path
c, err := LoadFile(ruleFilesConfigFile) c, err := LoadFile(ruleFilesConfigFile)
if err != nil { testutil.Ok(t, err)
t.Errorf("Error parsing %s: %s", ruleFilesConfigFile, err)
}
bgot, err := yaml.Marshal(c)
if err != nil {
t.Fatalf("%s", err)
}
bexp, err := yaml.Marshal(ruleFilesExpectedConf)
if err != nil {
t.Fatalf("%s", err)
}
ruleFilesExpectedConf.original = c.original ruleFilesExpectedConf.original = c.original
testutil.Equals(t, ruleFilesExpectedConf, c)
if !reflect.DeepEqual(c, ruleFilesExpectedConf) {
t.Fatalf("%s: unexpected config result: \n\n%s\n expected\n\n%s", ruleFilesConfigFile, bgot, bexp)
}
} }
var expectedErrors = []struct { var expectedErrors = []struct {
@ -692,51 +659,34 @@ var expectedErrors = []struct {
func TestBadConfigs(t *testing.T) { func TestBadConfigs(t *testing.T) {
for _, ee := range expectedErrors { for _, ee := range expectedErrors {
_, err := LoadFile("testdata/" + ee.filename) _, err := LoadFile("testdata/" + ee.filename)
if err == nil { testutil.Assert(t, err != nil,
t.Errorf("Expected error parsing %s but got none", ee.filename) "Expected error parsing %s but got none", ee.filename)
continue testutil.Assert(t, strings.Contains(err.Error(), ee.errMsg),
} "Expected error for %s to contain %q but got: %s", ee.filename, ee.errMsg, err)
if !strings.Contains(err.Error(), ee.errMsg) {
t.Errorf("Expected error for %s to contain %q but got: %s", ee.filename, ee.errMsg, err)
}
} }
} }
func TestBadStaticConfigs(t *testing.T) { func TestBadStaticConfigs(t *testing.T) {
content, err := ioutil.ReadFile("testdata/static_config.bad.json") content, err := ioutil.ReadFile("testdata/static_config.bad.json")
if err != nil { testutil.Ok(t, err)
t.Fatal(err)
}
var tg TargetGroup var tg TargetGroup
err = json.Unmarshal(content, &tg) err = json.Unmarshal(content, &tg)
if err == nil { testutil.Assert(t, err != nil, "Expected unmarshal error but got none.")
t.Errorf("Expected unmarshal error but got none.")
}
} }
func TestEmptyConfig(t *testing.T) { func TestEmptyConfig(t *testing.T) {
c, err := Load("") c, err := Load("")
if err != nil { testutil.Ok(t, err)
t.Fatalf("Unexpected error parsing empty config file: %s", err)
}
exp := DefaultConfig exp := DefaultConfig
testutil.Equals(t, exp, *c)
if !reflect.DeepEqual(*c, exp) {
t.Fatalf("want %v, got %v", exp, c)
}
} }
func TestEmptyGlobalBlock(t *testing.T) { func TestEmptyGlobalBlock(t *testing.T) {
c, err := Load("global:\n") c, err := Load("global:\n")
if err != nil { testutil.Ok(t, err)
t.Fatalf("Unexpected error parsing empty config file: %s", err)
}
exp := DefaultConfig exp := DefaultConfig
exp.original = "global:\n" exp.original = "global:\n"
testutil.Equals(t, exp, *c)
if !reflect.DeepEqual(*c, exp) {
t.Fatalf("want %v, got %v", exp, c)
}
} }
func TestTargetLabelValidity(t *testing.T) { func TestTargetLabelValidity(t *testing.T) {
@ -761,9 +711,8 @@ func TestTargetLabelValidity(t *testing.T) {
{"foo${bar}foo", true}, {"foo${bar}foo", true},
} }
for _, test := range tests { for _, test := range tests {
if relabelTarget.Match([]byte(test.str)) != test.valid { testutil.Assert(t, relabelTarget.Match([]byte(test.str)) == test.valid,
t.Fatalf("Expected %q to be %v", test.str, test.valid) "Expected %q to be %v", test.str, test.valid)
}
} }
} }