2020-04-13 20:02:27 +02:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
2020-04-24 16:50:21 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2020-04-13 20:02:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseEndpointsListFailure(t *testing.T) {
|
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
2021-02-26 15:54:03 +01:00
|
|
|
objectsByKey, _, err := parseEndpointsList([]byte(s))
|
2020-04-13 20:02:27 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
|
|
|
}
|
2021-02-26 15:54:03 +01:00
|
|
|
if len(objectsByKey) != 0 {
|
|
|
|
t.Fatalf("unexpected non-empty objectsByKey: %v", objectsByKey)
|
2020-04-13 20:02:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f(``)
|
|
|
|
f(`[1,23]`)
|
|
|
|
f(`{"items":[{"metadata":1}]}`)
|
|
|
|
f(`{"items":[{"metadata":{"labels":[1]}}]}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseEndpointsListSuccess(t *testing.T) {
|
|
|
|
data := `
|
|
|
|
{
|
|
|
|
"kind": "EndpointsList",
|
|
|
|
"apiVersion": "v1",
|
|
|
|
"metadata": {
|
|
|
|
"selfLink": "/api/v1/endpoints",
|
|
|
|
"resourceVersion": "128055"
|
|
|
|
},
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"metadata": {
|
|
|
|
"name": "kubernetes",
|
|
|
|
"namespace": "default",
|
|
|
|
"selfLink": "/api/v1/namespaces/default/endpoints/kubernetes",
|
|
|
|
"uid": "0972c7d9-c267-4b93-a090-a417eeb9b385",
|
|
|
|
"resourceVersion": "150",
|
2021-02-15 01:46:14 +01:00
|
|
|
"creationTimestamp": "2020-03-16T20:44:25Z",
|
|
|
|
"labels": {
|
|
|
|
"foo": "bar"
|
|
|
|
},
|
|
|
|
"annotations": {
|
|
|
|
"x": "y"
|
|
|
|
}
|
2020-04-13 20:02:27 +02:00
|
|
|
},
|
|
|
|
"subsets": [
|
|
|
|
{
|
|
|
|
"addresses": [
|
|
|
|
{
|
|
|
|
"hostname": "aaa.bbb",
|
|
|
|
"nodeName": "foobar",
|
|
|
|
"ip": "172.17.0.2",
|
|
|
|
"targetRef": {
|
|
|
|
"kind": "Pod",
|
|
|
|
"namespace": "kube-system",
|
|
|
|
"name": "coredns-6955765f44-lnp6t",
|
|
|
|
"uid": "cbddb2b6-5b85-40f1-8819-9a59385169bb",
|
|
|
|
"resourceVersion": "124878"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"ports": [
|
|
|
|
{
|
|
|
|
"name": "https",
|
|
|
|
"port": 8443,
|
|
|
|
"protocol": "TCP"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
2021-02-26 15:54:03 +01:00
|
|
|
objectsByKey, meta, err := parseEndpointsList([]byte(data))
|
2020-04-13 20:02:27 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
2021-02-26 15:54:03 +01:00
|
|
|
expectedResourceVersion := "128055"
|
|
|
|
if meta.ResourceVersion != expectedResourceVersion {
|
|
|
|
t.Fatalf("unexpected resource version; got %s; want %s", meta.ResourceVersion, expectedResourceVersion)
|
2020-04-13 20:02:27 +02:00
|
|
|
}
|
|
|
|
|
2021-02-26 19:21:27 +01:00
|
|
|
sortedLabelss := getSortedLabelss(objectsByKey)
|
2020-04-13 20:02:27 +02:00
|
|
|
expectedLabelss := [][]prompbmarshal.Label{
|
2020-04-24 16:50:21 +02:00
|
|
|
discoveryutils.GetSortedLabels(map[string]string{
|
2020-04-13 20:02:27 +02:00
|
|
|
"__address__": "172.17.0.2:8443",
|
2021-02-15 01:46:14 +01:00
|
|
|
"__meta_kubernetes_endpoint_address_target_kind": "Pod",
|
|
|
|
"__meta_kubernetes_endpoint_address_target_name": "coredns-6955765f44-lnp6t",
|
|
|
|
"__meta_kubernetes_endpoint_hostname": "aaa.bbb",
|
|
|
|
"__meta_kubernetes_endpoint_node_name": "foobar",
|
|
|
|
"__meta_kubernetes_endpoint_port_name": "https",
|
|
|
|
"__meta_kubernetes_endpoint_port_protocol": "TCP",
|
|
|
|
"__meta_kubernetes_endpoint_ready": "true",
|
|
|
|
"__meta_kubernetes_endpoints_name": "kubernetes",
|
|
|
|
"__meta_kubernetes_endpoints_annotation_x": "y",
|
|
|
|
"__meta_kubernetes_endpoints_annotationpresent_x": "true",
|
|
|
|
"__meta_kubernetes_endpoints_label_foo": "bar",
|
|
|
|
"__meta_kubernetes_endpoints_labelpresent_foo": "true",
|
|
|
|
"__meta_kubernetes_namespace": "default",
|
2020-04-13 20:02:27 +02:00
|
|
|
}),
|
|
|
|
}
|
2021-03-01 13:31:28 +01:00
|
|
|
if !areEqualLabelss(sortedLabelss, expectedLabelss) {
|
2020-04-13 20:02:27 +02:00
|
|
|
t.Fatalf("unexpected labels:\ngot\n%v\nwant\n%v", sortedLabelss, expectedLabelss)
|
|
|
|
}
|
|
|
|
}
|