From 0f3d8ea0752871b10ca3e91ca7123041bbf0683e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Cort=C3=A9s?= Date: Tue, 10 Oct 2017 12:29:15 +0200 Subject: [PATCH 1/2] config: use testutil package --- config/config_test.go | 98 ++++++++++--------------------------------- 1 file changed, 23 insertions(+), 75 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index 0417a2eb9..ff644d76b 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -18,13 +18,13 @@ import ( "io/ioutil" "net/url" "path/filepath" - "reflect" "regexp" "strings" "testing" "time" "github.com/prometheus/common/model" + "github.com/prometheus/prometheus/util/testutil" "gopkg.in/yaml.v2" ) @@ -517,73 +517,39 @@ 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 := LoadFile("testdata/global_timeout.good.yml"); err != nil { - t.Errorf("Error parsing %s: %s", "testdata/global_timeout.good.yml", err) - } + _, err := LoadFile("testdata/global_timeout.good.yml") + testutil.Ok(t, err) c, err := LoadFile("testdata/conf.good.yml") - if err != nil { - t.Fatalf("Error parsing %s: %s", "testdata/conf.good.yml", err) - } + testutil.Ok(t, 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 - - 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) - } + testutil.Equals(t, expectedConf, c) } // YAML marshalling must not reveal authentication credentials. func TestElideSecrets(t *testing.T) { c, err := LoadFile("testdata/conf.good.yml") - if err != nil { - t.Fatalf("Error parsing %s: %s", "testdata/conf.good.yml", err) - } + testutil.Ok(t, err) secretRe := regexp.MustCompile(`\\u003csecret\\u003e|`) config, err := yaml.Marshal(c) - if err != nil { - t.Fatal(err) - } + testutil.Ok(t, err) yamlConfig := string(config) matches := secretRe.FindAllStringIndex(yamlConfig, -1) - if len(matches) != 6 || strings.Contains(yamlConfig, "mysecret") { - t.Fatalf("yaml marshal reveals authentication credentials.") - } + testutil.Assert(t, len(matches) == 6 && !strings.Contains(yamlConfig, "mysecret"), + "yaml marshal reveals authentication credentials.") } func TestLoadConfigRuleFilesAbsolutePath(t *testing.T) { // Parse a valid file that sets a rule files with an absolute path c, err := LoadFile(ruleFilesConfigFile) - if err != nil { - t.Errorf("Error parsing %s: %s", ruleFilesConfigFile, err) - } + testutil.Ok(t, 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 - - if !reflect.DeepEqual(c, ruleFilesExpectedConf) { - t.Fatalf("%s: unexpected config result: \n\n%s\n expected\n\n%s", ruleFilesConfigFile, bgot, bexp) - } + testutil.Equals(t, ruleFilesExpectedConf, c) } var expectedErrors = []struct { @@ -692,51 +658,34 @@ var expectedErrors = []struct { func TestBadConfigs(t *testing.T) { for _, ee := range expectedErrors { _, err := LoadFile("testdata/" + ee.filename) - if err == nil { - t.Errorf("Expected error parsing %s but got none", ee.filename) - continue - } - if !strings.Contains(err.Error(), ee.errMsg) { - t.Errorf("Expected error for %s to contain %q but got: %s", ee.filename, ee.errMsg, err) - } + testutil.Assert(t, err != nil, + "Expected error parsing %s but got none", ee.filename) + testutil.Assert(t, strings.Contains(err.Error(), ee.errMsg), + "Expected error for %s to contain %q but got: %s", ee.filename, ee.errMsg, err) } } func TestBadStaticConfigs(t *testing.T) { content, err := ioutil.ReadFile("testdata/static_config.bad.json") - if err != nil { - t.Fatal(err) - } + testutil.Ok(t, err) var tg TargetGroup err = json.Unmarshal(content, &tg) - if err == nil { - t.Errorf("Expected unmarshal error but got none.") - } + testutil.Assert(t, err != nil, "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) - } + testutil.Ok(t, err) exp := DefaultConfig - - if !reflect.DeepEqual(*c, exp) { - t.Fatalf("want %v, got %v", exp, c) - } + testutil.Equals(t, 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) - } + testutil.Ok(t, err) exp := DefaultConfig exp.original = "global:\n" - - if !reflect.DeepEqual(*c, exp) { - t.Fatalf("want %v, got %v", exp, c) - } + testutil.Equals(t, exp, *c) } func TestTargetLabelValidity(t *testing.T) { @@ -761,9 +710,8 @@ func TestTargetLabelValidity(t *testing.T) { {"foo${bar}foo", true}, } for _, test := range tests { - if relabelTarget.Match([]byte(test.str)) != test.valid { - t.Fatalf("Expected %q to be %v", test.str, test.valid) - } + testutil.Assert(t, relabelTarget.Match([]byte(test.str)) == test.valid, + "Expected %q to be %v", test.str, test.valid) } } From 6c6729642376c04f175dffba7c1cbd9f63918071 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Cort=C3=A9s?= Date: Tue, 10 Oct 2017 12:34:03 +0200 Subject: [PATCH 2/2] config: fix error message for unexpected result of yaml marshal --- config/config_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/config_test.go b/config/config_test.go index ff644d76b..5f1a8d74e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -539,7 +539,8 @@ func TestElideSecrets(t *testing.T) { yamlConfig := string(config) matches := secretRe.FindAllStringIndex(yamlConfig, -1) - testutil.Assert(t, len(matches) == 6 && !strings.Contains(yamlConfig, "mysecret"), + testutil.Assert(t, len(matches) == 6, "wrong number of secret matches found") + testutil.Assert(t, !strings.Contains(yamlConfig, "mysecret"), "yaml marshal reveals authentication credentials.") }