diff --git a/cmd/karma/main.go b/cmd/karma/main.go index a1b3305d8..ef6ce5d8d 100644 --- a/cmd/karma/main.go +++ b/cmd/karma/main.go @@ -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 diff --git a/cmd/karma/main_test.go b/cmd/karma/main_test.go index d73954846..e6ef03a11 100644 --- a/cmd/karma/main_test.go +++ b/cmd/karma/main_test.go @@ -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) + } + }) + } +} diff --git a/cmd/karma/proxy_test.go b/cmd/karma/proxy_test.go index 55602051c..69fa522ef 100644 --- a/cmd/karma/proxy_test.go +++ b/cmd/karma/proxy_test.go @@ -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",