-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathservicemonitor_test.go
102 lines (93 loc) · 2.54 KB
/
servicemonitor_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
package prometheus_test
import (
"context"
"testing"
"time"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/slok/k8s-webhook-example/internal/mutation/prometheus"
)
func TestServiceMonitorSafer(t *testing.T) {
tests := map[string]struct {
minScrapeInterval time.Duration
servMon *monitoringv1.ServiceMonitor
expServMon *monitoringv1.ServiceMonitor
}{
"Having a correct scrape interval should not mutate the service monitor.": {
minScrapeInterval: 10 * time.Second,
servMon: &monitoringv1.ServiceMonitor{
Spec: monitoringv1.ServiceMonitorSpec{
Endpoints: []monitoringv1.Endpoint{
{Interval: "15s"},
{Interval: "20s"},
{Interval: "30s"},
},
},
},
expServMon: &monitoringv1.ServiceMonitor{
Spec: monitoringv1.ServiceMonitorSpec{
Endpoints: []monitoringv1.Endpoint{
{Interval: "15s"},
{Interval: "20s"},
{Interval: "30s"},
},
},
},
},
"Having a incorrect scrape interval should not mutate the service monitor.": {
minScrapeInterval: 16 * time.Second,
servMon: &monitoringv1.ServiceMonitor{
Spec: monitoringv1.ServiceMonitorSpec{
Endpoints: []monitoringv1.Endpoint{
{Interval: "30s"},
{Interval: "15s"},
{Interval: "20s"},
},
},
},
expServMon: &monitoringv1.ServiceMonitor{
Spec: monitoringv1.ServiceMonitorSpec{
Endpoints: []monitoringv1.Endpoint{
{Interval: "30s"},
{Interval: "16s"},
{Interval: "20s"},
},
},
},
},
"Having a service monitor without interval should set the minimum one.": {
minScrapeInterval: 11 * time.Second,
servMon: &monitoringv1.ServiceMonitor{
Spec: monitoringv1.ServiceMonitorSpec{
Endpoints: []monitoringv1.Endpoint{
{Interval: "30s"},
{Interval: "15s"},
{Interval: "20s"},
{},
},
},
},
expServMon: &monitoringv1.ServiceMonitor{
Spec: monitoringv1.ServiceMonitorSpec{
Endpoints: []monitoringv1.Endpoint{
{Interval: "30s"},
{Interval: "15s"},
{Interval: "20s"},
{Interval: "11s"},
},
},
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
s := prometheus.NewServiceMonitorSafer(test.minScrapeInterval)
err := s.EnsureSafety(context.TODO(), test.servMon)
require.NoError(err)
assert.Equal(test.expServMon, test.servMon)
})
}
}