2018-11-05 21:30:16 +00:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
2020-03-03 02:51:31 +00:00
|
|
|
"github.com/ericchiang/k8s"
|
2018-11-05 21:30:16 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-09-23 22:39:50 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2018-11-05 21:30:16 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
v1 "github.com/ericchiang/k8s/apis/core/v1"
|
|
|
|
metav1 "github.com/ericchiang/k8s/apis/meta/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestScrapeURLNoAnnotations(t *testing.T) {
|
|
|
|
p := &v1.Pod{Metadata: &metav1.ObjectMeta{}}
|
|
|
|
p.GetMetadata().Annotations = map[string]string{}
|
|
|
|
url := getScrapeURL(p)
|
|
|
|
assert.Nil(t, url)
|
|
|
|
}
|
2019-01-16 23:49:24 +00:00
|
|
|
|
2018-11-05 21:30:16 +00:00
|
|
|
func TestScrapeURLAnnotationsNoScrape(t *testing.T) {
|
|
|
|
p := &v1.Pod{Metadata: &metav1.ObjectMeta{}}
|
|
|
|
p.Metadata.Name = str("myPod")
|
|
|
|
p.Metadata.Annotations = map[string]string{"prometheus.io/scrape": "false"}
|
|
|
|
url := getScrapeURL(p)
|
|
|
|
assert.Nil(t, url)
|
|
|
|
}
|
2019-01-16 23:49:24 +00:00
|
|
|
|
2018-11-05 21:30:16 +00:00
|
|
|
func TestScrapeURLAnnotations(t *testing.T) {
|
|
|
|
p := pod()
|
|
|
|
p.Metadata.Annotations = map[string]string{"prometheus.io/scrape": "true"}
|
|
|
|
url := getScrapeURL(p)
|
|
|
|
assert.Equal(t, "http://127.0.0.1:9102/metrics", *url)
|
|
|
|
}
|
2019-01-16 23:49:24 +00:00
|
|
|
|
2018-11-05 21:30:16 +00:00
|
|
|
func TestScrapeURLAnnotationsCustomPort(t *testing.T) {
|
|
|
|
p := pod()
|
|
|
|
p.Metadata.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)
|
|
|
|
}
|
2019-01-16 23:49:24 +00:00
|
|
|
|
2018-11-05 21:30:16 +00:00
|
|
|
func TestScrapeURLAnnotationsCustomPath(t *testing.T) {
|
|
|
|
p := pod()
|
|
|
|
p.Metadata.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.Metadata.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) {
|
2019-09-23 22:39:50 +00:00
|
|
|
prom := &Prometheus{Log: testutil.Logger{}}
|
2019-01-16 23:49:24 +00:00
|
|
|
|
2018-11-05 21:30:16 +00:00
|
|
|
p := pod()
|
|
|
|
p.Metadata.Annotations = map[string]string{"prometheus.io/scrape": "true"}
|
|
|
|
registerPod(p, prom)
|
|
|
|
assert.Equal(t, 1, len(prom.kubernetesPods))
|
|
|
|
}
|
2019-01-16 23:49:24 +00:00
|
|
|
|
|
|
|
func TestAddMultipleDuplicatePods(t *testing.T) {
|
2019-09-23 22:39:50 +00:00
|
|
|
prom := &Prometheus{Log: testutil.Logger{}}
|
2019-01-16 23:49:24 +00:00
|
|
|
|
|
|
|
p := pod()
|
|
|
|
p.Metadata.Annotations = map[string]string{"prometheus.io/scrape": "true"}
|
|
|
|
registerPod(p, prom)
|
|
|
|
p.Metadata.Name = str("Pod2")
|
|
|
|
registerPod(p, prom)
|
|
|
|
assert.Equal(t, 1, len(prom.kubernetesPods))
|
|
|
|
}
|
|
|
|
|
2018-11-05 21:30:16 +00:00
|
|
|
func TestAddMultiplePods(t *testing.T) {
|
2019-09-23 22:39:50 +00:00
|
|
|
prom := &Prometheus{Log: testutil.Logger{}}
|
2018-11-05 21:30:16 +00:00
|
|
|
|
|
|
|
p := pod()
|
|
|
|
p.Metadata.Annotations = map[string]string{"prometheus.io/scrape": "true"}
|
|
|
|
registerPod(p, prom)
|
|
|
|
p.Metadata.Name = str("Pod2")
|
2019-01-16 23:49:24 +00:00
|
|
|
p.Status.PodIP = str("127.0.0.2")
|
2018-11-05 21:30:16 +00:00
|
|
|
registerPod(p, prom)
|
|
|
|
assert.Equal(t, 2, len(prom.kubernetesPods))
|
|
|
|
}
|
2019-01-16 23:49:24 +00:00
|
|
|
|
2018-11-05 21:30:16 +00:00
|
|
|
func TestDeletePods(t *testing.T) {
|
2019-09-23 22:39:50 +00:00
|
|
|
prom := &Prometheus{Log: testutil.Logger{}}
|
2018-11-05 21:30:16 +00:00
|
|
|
|
|
|
|
p := pod()
|
|
|
|
p.Metadata.Annotations = map[string]string{"prometheus.io/scrape": "true"}
|
|
|
|
registerPod(p, prom)
|
|
|
|
unregisterPod(p, prom)
|
|
|
|
assert.Equal(t, 0, len(prom.kubernetesPods))
|
|
|
|
}
|
|
|
|
|
2020-03-03 02:51:31 +00:00
|
|
|
func TestPodSelector(t *testing.T) {
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
expected []k8s.Option
|
|
|
|
labelselector string
|
|
|
|
fieldselector string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
expected: []k8s.Option{
|
|
|
|
k8s.QueryParam("labelSelector", "key1=val1,key2=val2,key3"),
|
|
|
|
k8s.QueryParam("fieldSelector", "spec.nodeName=ip-1-2-3-4.acme.com"),
|
|
|
|
},
|
|
|
|
labelselector: "key1=val1,key2=val2,key3",
|
|
|
|
fieldselector: "spec.nodeName=ip-1-2-3-4.acme.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: []k8s.Option{
|
|
|
|
k8s.QueryParam("labelSelector", "key1"),
|
|
|
|
k8s.QueryParam("fieldSelector", "spec.nodeName=ip-1-2-3-4.acme.com"),
|
|
|
|
},
|
|
|
|
labelselector: "key1",
|
|
|
|
fieldselector: "spec.nodeName=ip-1-2-3-4.acme.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expected: []k8s.Option{
|
|
|
|
k8s.QueryParam("labelSelector", "key1"),
|
|
|
|
k8s.QueryParam("fieldSelector", "somefield"),
|
|
|
|
},
|
|
|
|
labelselector: "key1",
|
|
|
|
fieldselector: "somefield",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
prom := &Prometheus{
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
KubernetesLabelSelector: c.labelselector,
|
|
|
|
KubernetesFieldSelector: c.fieldselector,
|
|
|
|
}
|
|
|
|
|
|
|
|
output := podSelector(prom)
|
|
|
|
|
|
|
|
assert.Equal(t, len(output), len(c.expected))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 21:30:16 +00:00
|
|
|
func pod() *v1.Pod {
|
2020-03-03 02:51:31 +00:00
|
|
|
p := &v1.Pod{Metadata: &metav1.ObjectMeta{}, Status: &v1.PodStatus{}, Spec: &v1.PodSpec{}}
|
2018-11-05 21:30:16 +00:00
|
|
|
p.Status.PodIP = str("127.0.0.1")
|
|
|
|
p.Metadata.Name = str("myPod")
|
|
|
|
p.Metadata.Namespace = str("default")
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func str(x string) *string {
|
|
|
|
return &x
|
|
|
|
}
|