forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes_test.go
166 lines (137 loc) · 4.97 KB
/
kubernetes_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package prometheus
import (
"testing"
"github.com/influxdata/telegraf/testutil"
"github.com/kubernetes/apimachinery/pkg/fields"
"github.com/kubernetes/apimachinery/pkg/labels"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestScrapeURLNoAnnotations(t *testing.T) {
p := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{}}
p.Annotations = map[string]string{}
url := getScrapeURL(p)
assert.Nil(t, url)
}
func TestScrapeURLAnnotationsNoScrape(t *testing.T) {
p := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{}}
p.Name = "myPod"
p.Annotations = map[string]string{"prometheus.io/scrape": "false"}
url := getScrapeURL(p)
assert.Nil(t, url)
}
func TestScrapeURLAnnotations(t *testing.T) {
p := pod()
p.Annotations = map[string]string{"prometheus.io/scrape": "true"}
url := getScrapeURL(p)
assert.Equal(t, "http://127.0.0.1:9102/metrics", *url)
}
func TestScrapeURLAnnotationsCustomPort(t *testing.T) {
p := pod()
p.Annotations = map[string]string{"prometheus.io/scrape": "true", "prometheus.io/port": "9000"}
url := getScrapeURL(p)
assert.Equal(t, "http://127.0.0.1:9000/metrics", *url)
}
func TestScrapeURLAnnotationsCustomPath(t *testing.T) {
p := pod()
p.Annotations = map[string]string{"prometheus.io/scrape": "true", "prometheus.io/path": "mymetrics"}
url := getScrapeURL(p)
assert.Equal(t, "http://127.0.0.1:9102/mymetrics", *url)
}
func TestScrapeURLAnnotationsCustomPathWithSep(t *testing.T) {
p := pod()
p.Annotations = map[string]string{"prometheus.io/scrape": "true", "prometheus.io/path": "/mymetrics"}
url := getScrapeURL(p)
assert.Equal(t, "http://127.0.0.1:9102/mymetrics", *url)
}
func TestAddPod(t *testing.T) {
prom := &Prometheus{Log: testutil.Logger{}}
p := pod()
p.Annotations = map[string]string{"prometheus.io/scrape": "true"}
registerPod(p, prom)
assert.Equal(t, 1, len(prom.kubernetesPods))
}
func TestAddMultipleDuplicatePods(t *testing.T) {
prom := &Prometheus{Log: testutil.Logger{}}
p := pod()
p.Annotations = map[string]string{"prometheus.io/scrape": "true"}
registerPod(p, prom)
p.Name = "Pod2"
registerPod(p, prom)
assert.Equal(t, 1, len(prom.kubernetesPods))
}
func TestAddMultiplePods(t *testing.T) {
prom := &Prometheus{Log: testutil.Logger{}}
p := pod()
p.Annotations = map[string]string{"prometheus.io/scrape": "true"}
registerPod(p, prom)
p.Name = "Pod2"
p.Status.PodIP = "127.0.0.2"
registerPod(p, prom)
assert.Equal(t, 2, len(prom.kubernetesPods))
}
func TestDeletePods(t *testing.T) {
prom := &Prometheus{Log: testutil.Logger{}}
p := pod()
p.Annotations = map[string]string{"prometheus.io/scrape": "true"}
registerPod(p, prom)
unregisterPod(p, prom)
assert.Equal(t, 0, len(prom.kubernetesPods))
}
func TestPodHasMatchingNamespace(t *testing.T) {
prom := &Prometheus{Log: testutil.Logger{}, PodNamespace: "default"}
pod := pod()
pod.Name = "Pod1"
pod.Namespace = "default"
shouldMatch := podHasMatchingNamespace(pod, prom)
assert.Equal(t, true, shouldMatch)
pod.Name = "Pod2"
pod.Namespace = "namespace"
shouldNotMatch := podHasMatchingNamespace(pod, prom)
assert.Equal(t, false, shouldNotMatch)
}
func TestPodHasMatchingLabelSelector(t *testing.T) {
labelSelectorString := "label0==label0,label1=label1,label2!=label,label3 in (label1,label2, label3),label4 notin (label1, label2,label3),label5,!label6"
prom := &Prometheus{Log: testutil.Logger{}, KubernetesLabelSelector: labelSelectorString}
pod := pod()
pod.Labels = make(map[string]string)
pod.Labels["label0"] = "label0"
pod.Labels["label1"] = "label1"
pod.Labels["label2"] = "label2"
pod.Labels["label3"] = "label3"
pod.Labels["label4"] = "label4"
pod.Labels["label5"] = "label5"
labelSelector, err := labels.Parse(prom.KubernetesLabelSelector)
assert.Equal(t, err, nil)
assert.Equal(t, true, podHasMatchingLabelSelector(pod, labelSelector))
}
func TestPodHasMatchingFieldSelector(t *testing.T) {
fieldSelectorString := "status.podIP=127.0.0.1,spec.restartPolicy=Always,spec.NodeName!=nodeName"
prom := &Prometheus{Log: testutil.Logger{}, KubernetesFieldSelector: fieldSelectorString}
pod := pod()
pod.Spec.RestartPolicy = "Always"
pod.Spec.NodeName = "node1000"
fieldSelector, err := fields.ParseSelector(prom.KubernetesFieldSelector)
assert.Equal(t, err, nil)
assert.Equal(t, true, podHasMatchingFieldSelector(pod, fieldSelector))
}
func TestInvalidFieldSelector(t *testing.T) {
fieldSelectorString := "status.podIP=127.0.0.1,spec.restartPolicy=Always,spec.NodeName!=nodeName,spec.nodeName"
prom := &Prometheus{Log: testutil.Logger{}, KubernetesFieldSelector: fieldSelectorString}
pod := pod()
pod.Spec.RestartPolicy = "Always"
pod.Spec.NodeName = "node1000"
_, err := fields.ParseSelector(prom.KubernetesFieldSelector)
assert.NotEqual(t, err, nil)
}
func pod() *corev1.Pod {
p := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{}, Status: corev1.PodStatus{}, Spec: corev1.PodSpec{}}
p.Status.PodIP = "127.0.0.1"
p.Name = "myPod"
p.Namespace = "default"
return p
}
func str(x string) *string {
return &x
}