2020-04-13 18:02:27 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2021-03-11 14:41:09 +00:00
|
|
|
"bytes"
|
2020-04-13 18:02:27 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2020-04-24 14:50:21 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2020-04-13 18:02:27 +00:00
|
|
|
)
|
|
|
|
|
2021-09-13 10:03:42 +00:00
|
|
|
func TestMatchesHostPattern(t *testing.T) {
|
|
|
|
f := func(pattern, host string, resultExpected bool) {
|
|
|
|
t.Helper()
|
|
|
|
result := matchesHostPattern(pattern, host)
|
|
|
|
if result != resultExpected {
|
|
|
|
t.Fatalf("unexpected result for matchesHostPattern(%q, %q); got %v; want %v", pattern, host, result, resultExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("", "", true)
|
|
|
|
f("", "foo", false)
|
|
|
|
f("foo", "", false)
|
|
|
|
f("localhost", "localhost", true)
|
|
|
|
f("localhost", "localhost2", false)
|
|
|
|
f("*.foo", "bar", false)
|
|
|
|
f("foo.bar", "foo.bar", true)
|
|
|
|
f("foo.baz", "foo.bar", false)
|
|
|
|
f("a.x.yyy", "b.x.yyy", false)
|
|
|
|
f("*.x.yyy", "b.x.yyy", true)
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:02:27 +00:00
|
|
|
func TestParseIngressListFailure(t *testing.T) {
|
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
2021-03-11 14:41:09 +00:00
|
|
|
r := bytes.NewBufferString(s)
|
|
|
|
objectsByKey, _, err := parseIngressList(r)
|
2020-04-13 18:02:27 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
|
|
|
}
|
2021-02-26 14:54:03 +00:00
|
|
|
if len(objectsByKey) != 0 {
|
|
|
|
t.Fatalf("unexpected non-empty IngressList: %v", objectsByKey)
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f(``)
|
|
|
|
f(`[1,23]`)
|
|
|
|
f(`{"items":[{"metadata":1}]}`)
|
|
|
|
f(`{"items":[{"metadata":{"labels":[1]}}]}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseIngressListSuccess(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"kind": "IngressList",
|
2021-08-29 08:16:40 +00:00
|
|
|
"apiVersion": "extensions/v1",
|
2020-04-13 18:02:27 +00:00
|
|
|
"metadata": {
|
2021-08-29 08:16:40 +00:00
|
|
|
"selfLink": "/apis/extensions/v1/ingresses",
|
2020-04-13 18:02:27 +00:00
|
|
|
"resourceVersion": "351452"
|
|
|
|
},
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"metadata": {
|
|
|
|
"name": "test-ingress",
|
|
|
|
"namespace": "default",
|
2021-08-29 08:16:40 +00:00
|
|
|
"selfLink": "/apis/extensions/v1/namespaces/default/ingresses/test-ingress",
|
2020-04-13 18:02:27 +00:00
|
|
|
"uid": "6d3f38f9-de89-4bc9-b273-c8faf74e8a27",
|
|
|
|
"resourceVersion": "351445",
|
|
|
|
"generation": 1,
|
|
|
|
"creationTimestamp": "2020-04-13T16:43:52Z",
|
|
|
|
"annotations": {
|
2021-08-29 08:16:40 +00:00
|
|
|
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"networking.k8s.io/v1\",\"kind\":\"Ingress\",\"metadata\":{\"annotations\":{},\"name\":\"test-ingress\",\"namespace\":\"default\"},\"spec\":{\"backend\":{\"serviceName\":\"testsvc\",\"servicePort\":80}}}\n"
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"spec": {
|
|
|
|
"backend": {
|
|
|
|
"serviceName": "testsvc",
|
|
|
|
"servicePort": 80
|
|
|
|
},
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"host": "foobar"
|
|
|
|
}
|
2022-08-05 17:54:57 +00:00
|
|
|
],
|
|
|
|
"ingressClassName": "foo-class"
|
2020-04-13 18:02:27 +00:00
|
|
|
},
|
|
|
|
"status": {
|
|
|
|
"loadBalancer": {
|
|
|
|
"ingress": [
|
|
|
|
{
|
|
|
|
"ip": "172.17.0.2"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`
|
2021-03-11 14:41:09 +00:00
|
|
|
r := bytes.NewBufferString(data)
|
|
|
|
objectsByKey, meta, err := parseIngressList(r)
|
2020-04-13 18:02:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2021-02-26 14:54:03 +00:00
|
|
|
expectedResourceVersion := "351452"
|
|
|
|
if meta.ResourceVersion != expectedResourceVersion {
|
|
|
|
t.Fatalf("unexpected resource version; got %s; want %s", meta.ResourceVersion, expectedResourceVersion)
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
2021-02-26 18:21:27 +00:00
|
|
|
sortedLabelss := getSortedLabelss(objectsByKey)
|
2020-04-13 18:02:27 +00:00
|
|
|
expectedLabelss := [][]prompbmarshal.Label{
|
2020-04-24 14:50:21 +00:00
|
|
|
discoveryutils.GetSortedLabels(map[string]string{
|
2020-04-13 18:02:27 +00:00
|
|
|
"__address__": "foobar",
|
2021-08-29 08:16:40 +00:00
|
|
|
"__meta_kubernetes_ingress_annotation_kubectl_kubernetes_io_last_applied_configuration": `{"apiVersion":"networking.k8s.io/v1","kind":"Ingress","metadata":{"annotations":{},"name":"test-ingress","namespace":"default"},"spec":{"backend":{"serviceName":"testsvc","servicePort":80}}}` + "\n",
|
2020-04-13 18:02:27 +00:00
|
|
|
"__meta_kubernetes_ingress_annotationpresent_kubectl_kubernetes_io_last_applied_configuration": "true",
|
2022-08-05 17:54:57 +00:00
|
|
|
"__meta_kubernetes_ingress_host": "foobar",
|
|
|
|
"__meta_kubernetes_ingress_name": "test-ingress",
|
|
|
|
"__meta_kubernetes_ingress_path": "/",
|
|
|
|
"__meta_kubernetes_ingress_scheme": "http",
|
|
|
|
"__meta_kubernetes_ingress_class_name": "foo-class",
|
|
|
|
"__meta_kubernetes_namespace": "default",
|
2020-04-13 18:02:27 +00:00
|
|
|
}),
|
|
|
|
}
|
2021-03-01 12:31:28 +00:00
|
|
|
if !areEqualLabelss(sortedLabelss, expectedLabelss) {
|
2020-04-13 18:02:27 +00:00
|
|
|
t.Fatalf("unexpected labels:\ngot\n%v\nwant\n%v", sortedLabelss, expectedLabelss)
|
|
|
|
}
|
|
|
|
}
|