From: Yuchen Ying Date: Thu, 7 Jan 2021 19:39:06 +0000 (-0800) Subject: Improve the test code. (#733) X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=68f37c9b2d9d5596a924481affadda9bffcae048;p=blackbox_exporter.git Improve the test code. (#733) Signed-off-by: Yuchen Ying --- diff --git a/config/config_test.go b/config/config_test.go index a47aa70..67bb409 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -36,59 +36,57 @@ func TestLoadBadConfigs(t *testing.T) { C: &Config{}, } tests := []struct { - ConfigFile string - ExpectedError string + input string + want string }{ { - ConfigFile: "testdata/blackbox-bad.yml", - ExpectedError: "error parsing config file: yaml: unmarshal errors:\n line 50: field invalid_extra_field not found in type config.plain", + input: "testdata/blackbox-bad.yml", + want: "error parsing config file: yaml: unmarshal errors:\n line 50: field invalid_extra_field not found in type config.plain", }, { - ConfigFile: "testdata/blackbox-bad2.yml", - ExpectedError: "error parsing config file: at most one of bearer_token & bearer_token_file must be configured", + input: "testdata/blackbox-bad2.yml", + want: "error parsing config file: at most one of bearer_token & bearer_token_file must be configured", }, { - ConfigFile: "testdata/invalid-dns-module.yml", - ExpectedError: "error parsing config file: query name must be set for DNS module", + input: "testdata/invalid-dns-module.yml", + want: "error parsing config file: query name must be set for DNS module", }, { - ConfigFile: "testdata/invalid-dns-class.yml", - ExpectedError: "error parsing config file: query class 'X' is not valid", + input: "testdata/invalid-dns-class.yml", + want: "error parsing config file: query class 'X' is not valid", }, { - ConfigFile: "testdata/invalid-dns-type.yml", - ExpectedError: "error parsing config file: query type 'X' is not valid", + input: "testdata/invalid-dns-type.yml", + want: "error parsing config file: query type 'X' is not valid", }, { - ConfigFile: "testdata/invalid-http-header-match.yml", - ExpectedError: "error parsing config file: regexp must be set for HTTP header matchers", + input: "testdata/invalid-http-header-match.yml", + want: "error parsing config file: regexp must be set for HTTP header matchers", }, { - ConfigFile: "testdata/invalid-http-body-match-regexp.yml", - ExpectedError: "error parsing config file: \"Could not compile regular expression\" regexp=\":[\"", + input: "testdata/invalid-http-body-match-regexp.yml", + want: `error parsing config file: "Could not compile regular expression" regexp=":["`, }, { - ConfigFile: "testdata/invalid-http-body-not-match-regexp.yml", - ExpectedError: "error parsing config file: \"Could not compile regular expression\" regexp=\":[\"", + input: "testdata/invalid-http-body-not-match-regexp.yml", + want: `error parsing config file: "Could not compile regular expression" regexp=":["`, }, { - ConfigFile: "testdata/invalid-http-header-match-regexp.yml", - ExpectedError: "error parsing config file: \"Could not compile regular expression\" regexp=\":[\"", + input: "testdata/invalid-http-header-match-regexp.yml", + want: `error parsing config file: "Could not compile regular expression" regexp=":["`, }, { - ConfigFile: "testdata/invalid-tcp-query-response-regexp.yml", - ExpectedError: "error parsing config file: \"Could not compile regular expression\" regexp=\":[\"", + input: "testdata/invalid-tcp-query-response-regexp.yml", + want: `error parsing config file: "Could not compile regular expression" regexp=":["`, }, } - for i, test := range tests { - err := sc.ReloadConfig(test.ConfigFile) - if err == nil { - t.Errorf("In case %v:\nExpected:\n%v\nGot:\nnil", i, test.ExpectedError) - continue - } - if err.Error() != test.ExpectedError { - t.Errorf("In case %v:\nExpected:\n%v\nGot:\n%v", i, test.ExpectedError, err.Error()) - } + for _, test := range tests { + t.Run(test.input, func(t *testing.T) { + got := sc.ReloadConfig(test.input) + if got == nil || got.Error() != test.want { + t.Fatalf("ReloadConfig(%q) = %v; want %q", test.input, got, test.want) + } + }) } }