fix(tests): more test coverage

This commit is contained in:
Łukasz Mierzwa
2020-06-18 14:36:25 +01:00
committed by Łukasz Mierzwa
parent 7a3da15b30
commit 71839b0504
3 changed files with 76 additions and 2 deletions

View File

@@ -61,7 +61,14 @@ func getViewURL(sub string) string {
if !strings.HasPrefix(sub, "/") {
fixedSub = "/" + sub
}
u := path.Join(config.Config.Listen.Prefix, fixedSub)
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

View File

@@ -1,6 +1,7 @@
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
@@ -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",
@@ -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)
}
})
}
}

View File

@@ -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",