Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tests): add more test coverage #1863

Merged
merged 4 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions cmd/karma/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,20 @@ var (
)

func getViewURL(sub string) string {
u := path.Join(config.Config.Listen.Prefix, sub)
if strings.HasSuffix(sub, "/") && !strings.HasSuffix(u, "/") {
var fixedSub string
fixedSub = sub
if !strings.HasPrefix(sub, "/") {
fixedSub = "/" + sub
}

var fixedPrefix string
fixedPrefix = config.Config.Listen.Prefix
if config.Config.Listen.Prefix != "" && !strings.HasPrefix(config.Config.Listen.Prefix, "/") {
fixedPrefix = "/" + config.Config.Listen.Prefix
}

u := path.Join(fixedPrefix, fixedSub)
if strings.HasSuffix(fixedSub, "/") && !strings.HasSuffix(u, "/") {
// if sub path had trailing slash then add it here, since path.Join will
// skip it
return u + "/"
Expand Down
67 changes: 66 additions & 1 deletion cmd/karma/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -45,7 +46,6 @@ func TestMetrics(t *testing.T) {
}
body := resp.Body.String()
for _, s := range []string{
"karma_collected_alerts_count",
"karma_collected_alerts_count",
"karma_collect_cycles_total",
"karma_alertmanager_errors_total",
Expand All @@ -55,3 +55,68 @@ func TestMetrics(t *testing.T) {
}
}
}

func TestGetViewURL(t *testing.T) {
type testCaseT struct {
prefix string
view string
result string
}
tests := []testCaseT{
{
prefix: "",
view: "/",
result: "/",
},
{
prefix: "",
view: "foo",
result: "/foo",
},
{
prefix: "root",
view: "foo",
result: "/root/foo",
},
{
prefix: "root",
view: "foo/",
result: "/root/foo/",
},
{
prefix: "/root",
view: "foo",
result: "/root/foo",
},
{
prefix: "root/",
view: "foo",
result: "/root/foo",
},
{
prefix: "root/",
view: "foo/",
result: "/root/foo/",
},
{
prefix: "/root/",
view: "foo",
result: "/root/foo",
},
{
prefix: "/root/",
view: "/foo/",
result: "/root/foo/",
},
}

for _, testCase := range tests {
t.Run(fmt.Sprintf("prefix=%q view=%v result=%q", testCase.prefix, testCase.view, testCase.result), func(t *testing.T) {
config.Config.Listen.Prefix = testCase.prefix
result := getViewURL(testCase.view)
if result != testCase.result {
t.Errorf("getViewURL(%s) returned %q, expected %q", testCase.view, result, testCase.result)
}
})
}
}
2 changes: 2 additions & 0 deletions cmd/karma/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func TestProxy(t *testing.T) {
"matchers": [{ "isRegex": false, "name": "alertname", "value": "Fake Alert" }]
}`

config.Config.Listen.Prefix = ""

r := ginTestEngine()
am, err := alertmanager.NewAlertmanager(
"cluster",
Expand Down
4 changes: 4 additions & 0 deletions cmd/karma/tests/testscript/invalid_listen_prefix.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Raises an error if listen.prefix is invalid
karma.bin-should-fail --log.format=text --log.config=false --log.level=error --alertmanager.uri http://localhost --listen.prefix karma
! stdout .
stderr 'msg="listen.prefix must start with ''\/'', got \\"karma\\"'
144 changes: 144 additions & 0 deletions cmd/karma/views_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,150 @@ func TestUpstreamStatus(t *testing.T) {
},
},
},
{
Name: "Cluster with name and errors",
mocks: []mockT{
{
uri: "http://ha1.example.com/metrics",
code: 500,
body: `Error`,
},
{
uri: "http://ha2.example.com/metrics",
code: 200,
body: `alertmanager_build_info{version="0.21.0"} 1`,
},
{
uri: "http://ha1.example.com/api/v2/status",
code: 200,
body: `{
"cluster": {
"name": "AAAAAAAAAAAAAAAAAAAAAAAAAA",
"peers": [
{
"address": "10.16.0.1:9094",
"name": "AAAAAAAAAAAAAAAAAAAAAAAAAA"
},
{
"address": "10.16.0.1:9095",
"name": "BBBBBBBBBBBBBBBBBBBBBBBBBB"
}
],
"status": "ready"
},
"versionInfo": {
"version":"0.20.0"
}
}`,
},
{
uri: "http://ha2.example.com/api/v2/status",
code: 200,
body: `{
"cluster": {
"name": "BBBBBBBBBBBBBBBBBBBBBBBBBB",
"peers": [
{
"address": "10.16.0.1:9094",
"name": "AAAAAAAAAAAAAAAAAAAAAAAAAA"
},
{
"address": "10.16.0.2:9095",
"name": "BBBBBBBBBBBBBBBBBBBBBBBBBB"
}
],
"status": "ready"
},
"versionInfo": {
"version":"0.19.0"
}
}`,
},
{
uri: "http://ha1.example.com/api/v2/alerts/groups",
code: 200,
body: "[]",
},
{
uri: "http://ha1.example.com/api/v2/silences",
code: 200,
body: "[]",
},
{
uri: "http://ha2.example.com/api/v2/alerts/groups",
code: 200,
body: "[]",
},
{
uri: "http://ha2.example.com/api/v2/silences",
code: 500,
body: "[]",
},
},
upstreams: []config.AlertmanagerConfig{
{
Cluster: "Errors",
Name: "ha1",
URI: "http://ha1.example.com",
Proxy: false,
ReadOnly: false,
Headers: map[string]string{},
CORS: config.AlertmanagerCORS{
Credentials: "omit",
},
Timeout: time.Second * 10,
},
{
Cluster: "Errors",
Name: "ha2",
URI: "http://ha2.example.com",
Proxy: false,
ReadOnly: true,
Headers: map[string]string{},
CORS: config.AlertmanagerCORS{
Credentials: "omit",
},
Timeout: time.Second * 10,
},
},
status: models.AlertmanagerAPISummary{
Counters: models.AlertmanagerAPICounters{
Total: 2,
Healthy: 1,
Failed: 1,
},
Instances: []models.AlertmanagerAPIStatus{
{
Name: "ha1",
URI: "http://ha1.example.com",
PublicURI: "http://ha1.example.com",
ReadOnly: false,
Headers: map[string]string{},
CORSCredentials: "omit",
Error: "",
Version: "0.20.0",
Cluster: "ha1",
ClusterMembers: []string{"ha1"},
},
{
Name: "ha2",
URI: "http://ha2.example.com",
PublicURI: "http://ha2.example.com",
ReadOnly: true,
Headers: map[string]string{},
CORSCredentials: "omit",
Error: "json: cannot unmarshal array into Go value of type string",
Version: "",
Cluster: "ha2",
ClusterMembers: []string{"ha2"},
},
},
Clusters: map[string][]string{
"ha1": {"ha1"},
"ha2": {"ha2"},
},
},
},
}

for _, testCase := range testCases {
Expand Down
24 changes: 24 additions & 0 deletions internal/alertmanager/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,27 @@ func TestGetAlertMapper(t *testing.T) {
}
}
}

func TestGetInvalidAlertMapper(t *testing.T) {
versions := []string{
"0.16.0",
"0.10.0-rc.0",
}

for _, version := range versions {
_, err := mapper.GetAlertMapper(version)
if err == nil {
t.Errorf("mapper.GetAlertMapper(%s) didn't return an error", version)
}

_, err = mapper.GetSilenceMapper(version)
if err == nil {
t.Errorf("mapper.GetSilenceMapper(%s) didn't return an error", version)
}

_, err = mapper.GetStatusMapper(version)
if err == nil {
t.Errorf("mapper.GetStatusMapper(%s) didn't return an error", version)
}
}
}
Loading