2020-05-07 11:36:32 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-10-22 18:08:06 +02:00
|
|
|
"fmt"
|
2020-05-07 11:36:32 +02:00
|
|
|
"net/url"
|
2023-09-08 22:39:17 +02:00
|
|
|
"reflect"
|
2020-05-07 11:36:32 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-02-11 11:40:59 +01:00
|
|
|
func TestCreateTargetURLSuccess(t *testing.T) {
|
2023-09-08 22:39:17 +02:00
|
|
|
f := func(ui *UserInfo, requestURI, expectedTarget, expectedRequestHeaders, expectedResponseHeaders string, expectedRetryStatusCodes []int) {
|
2020-05-07 11:36:32 +02:00
|
|
|
t.Helper()
|
|
|
|
u, err := url.Parse(requestURI)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot parse %q: %s", requestURI, err)
|
|
|
|
}
|
2023-02-10 06:05:13 +01:00
|
|
|
u = normalizeURL(u)
|
2023-09-08 22:39:17 +02:00
|
|
|
up, hc, retryStatusCodes := ui.getURLPrefixAndHeaders(u)
|
2023-04-26 11:04:35 +02:00
|
|
|
if up == nil {
|
|
|
|
t.Fatalf("cannot determie backend: %s", err)
|
2021-02-11 11:40:59 +01:00
|
|
|
}
|
2023-02-11 09:27:40 +01:00
|
|
|
bu := up.getLeastLoadedBackendURL()
|
|
|
|
target := mergeURLs(bu.url, u)
|
|
|
|
bu.put()
|
2021-04-21 09:55:29 +02:00
|
|
|
if target.String() != expectedTarget {
|
2020-05-07 11:36:32 +02:00
|
|
|
t.Fatalf("unexpected target; got %q; want %q", target, expectedTarget)
|
|
|
|
}
|
2023-09-08 22:39:17 +02:00
|
|
|
headersStr := fmt.Sprintf("%q", hc.RequestHeaders)
|
2023-09-01 09:21:10 +02:00
|
|
|
if headersStr != expectedRequestHeaders {
|
2023-09-08 22:39:17 +02:00
|
|
|
t.Fatalf("unexpected request headers; got %s; want %s", headersStr, expectedRequestHeaders)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(retryStatusCodes, expectedRetryStatusCodes) {
|
|
|
|
t.Fatalf("unexpected retryStatusCodes; got %d; want %d", retryStatusCodes, expectedRetryStatusCodes)
|
2021-10-22 18:08:06 +02:00
|
|
|
}
|
2020-05-07 11:36:32 +02:00
|
|
|
}
|
2021-02-11 11:40:59 +01:00
|
|
|
// Simple routing with `url_prefix`
|
|
|
|
f(&UserInfo{
|
2021-04-21 09:55:29 +02:00
|
|
|
URLPrefix: mustParseURL("http://foo.bar"),
|
2023-09-08 22:39:17 +02:00
|
|
|
}, "", "http://foo.bar/.", "[]", "[]", nil)
|
2021-02-11 11:40:59 +01:00
|
|
|
f(&UserInfo{
|
2021-04-21 09:55:29 +02:00
|
|
|
URLPrefix: mustParseURL("http://foo.bar"),
|
2023-09-01 09:21:10 +02:00
|
|
|
HeadersConf: HeadersConf{
|
|
|
|
RequestHeaders: []Header{{
|
|
|
|
Name: "bb",
|
|
|
|
Value: "aaa",
|
|
|
|
}},
|
|
|
|
},
|
2023-09-08 22:39:17 +02:00
|
|
|
RetryStatusCodes: []int{503, 501},
|
|
|
|
}, "/", "http://foo.bar", `[{"bb" "aaa"}]`, `[]`, []int{503, 501})
|
2021-08-25 12:28:50 +02:00
|
|
|
f(&UserInfo{
|
|
|
|
URLPrefix: mustParseURL("http://foo.bar/federate"),
|
2023-09-08 22:39:17 +02:00
|
|
|
}, "/", "http://foo.bar/federate", "[]", "[]", nil)
|
2021-02-11 11:40:59 +01:00
|
|
|
f(&UserInfo{
|
2021-04-21 09:55:29 +02:00
|
|
|
URLPrefix: mustParseURL("http://foo.bar"),
|
2023-09-08 22:39:17 +02:00
|
|
|
}, "a/b?c=d", "http://foo.bar/a/b?c=d", "[]", "[]", nil)
|
2021-02-11 11:40:59 +01:00
|
|
|
f(&UserInfo{
|
2021-04-21 09:55:29 +02:00
|
|
|
URLPrefix: mustParseURL("https://sss:3894/x/y"),
|
2023-09-08 22:39:17 +02:00
|
|
|
}, "/z", "https://sss:3894/x/y/z", "[]", "[]", nil)
|
2021-02-11 11:40:59 +01:00
|
|
|
f(&UserInfo{
|
2021-04-21 09:55:29 +02:00
|
|
|
URLPrefix: mustParseURL("https://sss:3894/x/y"),
|
2023-09-08 22:39:17 +02:00
|
|
|
}, "/../../aaa", "https://sss:3894/x/y/aaa", "[]", "[]", nil)
|
2021-02-11 11:40:59 +01:00
|
|
|
f(&UserInfo{
|
2021-04-21 09:55:29 +02:00
|
|
|
URLPrefix: mustParseURL("https://sss:3894/x/y"),
|
2023-09-08 22:39:17 +02:00
|
|
|
}, "/./asd/../../aaa?a=d&s=s/../d", "https://sss:3894/x/y/aaa?a=d&s=s%2F..%2Fd", "[]", "[]", nil)
|
2021-02-11 11:40:59 +01:00
|
|
|
|
|
|
|
// Complex routing with `url_map`
|
|
|
|
ui := &UserInfo{
|
2023-01-27 09:18:58 +01:00
|
|
|
URLMaps: []URLMap{
|
2021-02-11 11:40:59 +01:00
|
|
|
{
|
2021-03-05 17:21:11 +01:00
|
|
|
SrcPaths: getSrcPaths([]string{"/api/v1/query"}),
|
2021-04-21 09:55:29 +02:00
|
|
|
URLPrefix: mustParseURL("http://vmselect/0/prometheus"),
|
2023-09-01 09:21:10 +02:00
|
|
|
HeadersConf: HeadersConf{
|
|
|
|
RequestHeaders: []Header{
|
|
|
|
{
|
|
|
|
Name: "xx",
|
|
|
|
Value: "aa",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "yy",
|
|
|
|
Value: "asdf",
|
|
|
|
},
|
2021-10-22 18:08:06 +02:00
|
|
|
},
|
2023-09-01 09:21:10 +02:00
|
|
|
ResponseHeaders: []Header{
|
|
|
|
{
|
|
|
|
Name: "qwe",
|
|
|
|
Value: "rty",
|
|
|
|
},
|
2021-10-22 18:08:06 +02:00
|
|
|
},
|
2023-09-01 09:21:10 +02:00
|
|
|
},
|
2023-09-08 22:39:17 +02:00
|
|
|
RetryStatusCodes: []int{503, 500, 501},
|
2021-02-11 11:40:59 +01:00
|
|
|
},
|
|
|
|
{
|
2021-03-05 17:21:11 +01:00
|
|
|
SrcPaths: getSrcPaths([]string{"/api/v1/write"}),
|
2021-04-21 09:55:29 +02:00
|
|
|
URLPrefix: mustParseURL("http://vminsert/0/prometheus"),
|
2021-02-11 11:40:59 +01:00
|
|
|
},
|
|
|
|
},
|
2021-04-21 09:55:29 +02:00
|
|
|
URLPrefix: mustParseURL("http://default-server"),
|
2023-09-01 09:21:10 +02:00
|
|
|
HeadersConf: HeadersConf{
|
|
|
|
RequestHeaders: []Header{{
|
|
|
|
Name: "bb",
|
|
|
|
Value: "aaa",
|
|
|
|
}},
|
|
|
|
ResponseHeaders: []Header{{
|
|
|
|
Name: "x",
|
|
|
|
Value: "y",
|
|
|
|
}},
|
|
|
|
},
|
2023-09-08 22:39:17 +02:00
|
|
|
RetryStatusCodes: []int{502},
|
2021-02-11 11:40:59 +01:00
|
|
|
}
|
2023-09-08 22:39:17 +02:00
|
|
|
f(ui, "/api/v1/query?query=up", "http://vmselect/0/prometheus/api/v1/query?query=up", `[{"xx" "aa"} {"yy" "asdf"}]`, `[{"qwe" "rty"}]`, []int{503, 500, 501})
|
|
|
|
f(ui, "/api/v1/write", "http://vminsert/0/prometheus/api/v1/write", "[]", "[]", nil)
|
|
|
|
f(ui, "/api/v1/query_range", "http://default-server/api/v1/query_range", `[{"bb" "aaa"}]`, `[{"x" "y"}]`, []int{502})
|
2021-03-05 17:21:11 +01:00
|
|
|
|
|
|
|
// Complex routing regexp paths in `url_map`
|
|
|
|
ui = &UserInfo{
|
2023-01-27 09:18:58 +01:00
|
|
|
URLMaps: []URLMap{
|
2021-03-05 17:21:11 +01:00
|
|
|
{
|
|
|
|
SrcPaths: getSrcPaths([]string{"/api/v1/query(_range)?", "/api/v1/label/[^/]+/values"}),
|
2021-04-21 09:55:29 +02:00
|
|
|
URLPrefix: mustParseURL("http://vmselect/0/prometheus"),
|
2021-03-05 17:21:11 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
SrcPaths: getSrcPaths([]string{"/api/v1/write"}),
|
2021-04-21 09:55:29 +02:00
|
|
|
URLPrefix: mustParseURL("http://vminsert/0/prometheus"),
|
2021-03-05 17:21:11 +01:00
|
|
|
},
|
|
|
|
},
|
2021-04-21 09:55:29 +02:00
|
|
|
URLPrefix: mustParseURL("http://default-server"),
|
2021-03-05 17:21:11 +01:00
|
|
|
}
|
2023-09-08 22:39:17 +02:00
|
|
|
f(ui, "/api/v1/query?query=up", "http://vmselect/0/prometheus/api/v1/query?query=up", "[]", "[]", nil)
|
|
|
|
f(ui, "/api/v1/query_range?query=up", "http://vmselect/0/prometheus/api/v1/query_range?query=up", "[]", "[]", nil)
|
|
|
|
f(ui, "/api/v1/label/foo/values", "http://vmselect/0/prometheus/api/v1/label/foo/values", "[]", "[]", nil)
|
|
|
|
f(ui, "/api/v1/write", "http://vminsert/0/prometheus/api/v1/write", "[]", "[]", nil)
|
|
|
|
f(ui, "/api/v1/foo/bar", "http://default-server/api/v1/foo/bar", "[]", "[]", nil)
|
2021-04-20 09:51:03 +02:00
|
|
|
f(&UserInfo{
|
2021-04-21 09:55:29 +02:00
|
|
|
URLPrefix: mustParseURL("http://foo.bar?extra_label=team=dev"),
|
2023-09-08 22:39:17 +02:00
|
|
|
}, "/api/v1/query", "http://foo.bar/api/v1/query?extra_label=team=dev", "[]", "[]", nil)
|
2021-04-20 09:51:03 +02:00
|
|
|
f(&UserInfo{
|
2021-04-21 09:55:29 +02:00
|
|
|
URLPrefix: mustParseURL("http://foo.bar?extra_label=team=mobile"),
|
2023-09-08 22:39:17 +02:00
|
|
|
}, "/api/v1/query?extra_label=team=dev", "http://foo.bar/api/v1/query?extra_label=team%3Dmobile", "[]", "[]", nil)
|
2021-02-11 11:40:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateTargetURLFailure(t *testing.T) {
|
|
|
|
f := func(ui *UserInfo, requestURI string) {
|
|
|
|
t.Helper()
|
|
|
|
u, err := url.Parse(requestURI)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot parse %q: %s", requestURI, err)
|
|
|
|
}
|
2023-02-10 06:05:13 +01:00
|
|
|
u = normalizeURL(u)
|
2023-09-08 22:39:17 +02:00
|
|
|
up, hc, retryStatusCodes := ui.getURLPrefixAndHeaders(u)
|
2023-02-10 06:05:13 +01:00
|
|
|
if up != nil {
|
2023-02-11 09:27:40 +01:00
|
|
|
t.Fatalf("unexpected non-empty up=%#v", up)
|
2021-02-11 11:40:59 +01:00
|
|
|
}
|
2023-09-08 22:39:17 +02:00
|
|
|
if hc.RequestHeaders != nil {
|
|
|
|
t.Fatalf("unexpected non-empty request headers=%q", hc.RequestHeaders)
|
|
|
|
}
|
|
|
|
if hc.ResponseHeaders != nil {
|
|
|
|
t.Fatalf("unexpected non-empty response headers=%q", hc.ResponseHeaders)
|
2023-08-31 14:26:51 +02:00
|
|
|
}
|
2023-09-08 22:39:17 +02:00
|
|
|
if retryStatusCodes != nil {
|
|
|
|
t.Fatalf("unexpected non-empty retryStatusCodes=%d", retryStatusCodes)
|
2021-10-22 18:08:06 +02:00
|
|
|
}
|
2021-02-11 11:40:59 +01:00
|
|
|
}
|
|
|
|
f(&UserInfo{}, "/foo/bar")
|
|
|
|
f(&UserInfo{
|
2023-01-27 09:18:58 +01:00
|
|
|
URLMaps: []URLMap{
|
2021-02-11 11:40:59 +01:00
|
|
|
{
|
2021-03-05 17:21:11 +01:00
|
|
|
SrcPaths: getSrcPaths([]string{"/api/v1/query"}),
|
2021-04-21 09:55:29 +02:00
|
|
|
URLPrefix: mustParseURL("http://foobar/baz"),
|
2021-02-11 11:40:59 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, "/api/v1/write")
|
2020-05-07 11:36:32 +02:00
|
|
|
}
|