From bef5125e48085efe6bb8558345685e704f986405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Sun, 4 Oct 2020 11:48:44 +0100 Subject: [PATCH] fix(ci): improve test coverage --- internal/alertmanager/upstream_test.go | 40 ++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/internal/alertmanager/upstream_test.go b/internal/alertmanager/upstream_test.go index f37bfcd0f..a22b3fa2b 100644 --- a/internal/alertmanager/upstream_test.go +++ b/internal/alertmanager/upstream_test.go @@ -9,7 +9,8 @@ import ( ) type testCase struct { - config config.AlertmanagerConfig + config config.AlertmanagerConfig + shouldFail bool } var testCases = []testCase{ @@ -49,6 +50,32 @@ var testCases = []testCase{ Headers: map[string]string{}, }, }, + { + config: config.AlertmanagerConfig{ + Cluster: "cluster", + Name: "name", + URI: "%gh&%ij", + ExternalURI: "http://localhost:9095", + Timeout: time.Second * 30, + Proxy: false, + ReadOnly: true, + Headers: map[string]string{}, + }, + shouldFail: true, + }, + { + config: config.AlertmanagerConfig{ + Cluster: "cluster", + Name: "name", + URI: "http://localhost:9095", + ExternalURI: "%gh&%ij", + Timeout: time.Second * 30, + Proxy: false, + ReadOnly: true, + Headers: map[string]string{}, + }, + shouldFail: true, + }, } func TestOptions(t *testing.T) { @@ -72,9 +99,15 @@ func TestOptions(t *testing.T) { WithReadOnly(tc.config.ReadOnly), WithHTTPTransport(httpTransport), // we will pass a nil unless TLS.CA or TLS.Cert is set WithHTTPHeaders(tc.config.Headers), + WithCORSCredentials(tc.config.CORS.Credentials), ) + didFail := err != nil + if didFail != tc.shouldFail { + t.Errorf("Alertmanager '%s' error mismatch, shouldFail=%v, err=%v", tc.config.Name, tc.shouldFail, err) + } if err != nil { - t.Errorf("Failed to create Alertmanager '%s' with URI '%s': %s", tc.config.Name, tc.config.URI, err) + t.Logf("Alertmanager '%s' returned an error: %v", tc.config.Name, err) + continue } if am.Name != tc.config.Name { t.Errorf("AlertmanagerConfig with name '%s' returned Alertmanager with name '%s'", tc.config.Name, am.Name) @@ -88,5 +121,8 @@ func TestOptions(t *testing.T) { if am.ReadOnly != tc.config.ReadOnly { t.Errorf("AlertmanagerConfig with name '%s' and readonly '%v' returned Alertmanager with readonly '%v'", tc.config.Name, tc.config.ReadOnly, am.ReadOnly) } + if am.CORSCredentials != tc.config.CORS.Credentials { + t.Errorf("AlertmanagerConfig with name '%s' and cors:credentials '%v' returned Alertmanager with cors:credentials '%v'", tc.config.Name, tc.config.CORS.Credentials, am.CORSCredentials) + } } }