1
2
3
4
5
6
7
8
9
10
11
12
13
14 package test
15
16 import (
17 "fmt"
18 "testing"
19 "time"
20
21 . "github.com/prometheus/alertmanager/test/with_api_v2"
22 )
23
24 func TestInhibiting(t *testing.T) {
25 t.Parallel()
26
27
28
29
30
31 conf := `
32 route:
33 receiver: "default"
34 group_by: []
35 group_wait: 1s
36 group_interval: 1s
37 repeat_interval: 1s
38
39 receivers:
40 - name: "default"
41 webhook_configs:
42 - url: 'http://%s'
43
44 inhibit_rules:
45 - source_match:
46 alertname: JobDown
47 target_match:
48 alertname: InstanceDown
49 equal:
50 - job
51 - zone
52 `
53
54 at := NewAcceptanceTest(t, &AcceptanceOpts{
55 Tolerance: 150 * time.Millisecond,
56 })
57
58 co := at.Collector("webhook")
59 wh := NewWebhook(t, co)
60
61 amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
62
63 amc.Push(At(1), Alert("alertname", "test1", "job", "testjob", "zone", "aa"))
64 amc.Push(At(1), Alert("alertname", "InstanceDown", "job", "testjob", "zone", "aa"))
65 amc.Push(At(1), Alert("alertname", "InstanceDown", "job", "testjob", "zone", "ab"))
66
67
68
69 amc.Push(At(2.2), Alert("alertname", "JobDown", "job", "testjob", "zone", "aa"))
70
71
72
73 amc.Push(At(3.6), Alert("alertname", "JobDown", "job", "testjob", "zone", "aa").Active(2.2, 3.6))
74
75 co.Want(Between(2, 2.5),
76 Alert("alertname", "test1", "job", "testjob", "zone", "aa").Active(1),
77 Alert("alertname", "InstanceDown", "job", "testjob", "zone", "aa").Active(1),
78 Alert("alertname", "InstanceDown", "job", "testjob", "zone", "ab").Active(1),
79 )
80
81 co.Want(Between(3, 3.5),
82 Alert("alertname", "test1", "job", "testjob", "zone", "aa").Active(1),
83 Alert("alertname", "InstanceDown", "job", "testjob", "zone", "ab").Active(1),
84 Alert("alertname", "JobDown", "job", "testjob", "zone", "aa").Active(2.2),
85 )
86
87 co.Want(Between(4, 4.5),
88 Alert("alertname", "test1", "job", "testjob", "zone", "aa").Active(1),
89 Alert("alertname", "InstanceDown", "job", "testjob", "zone", "aa").Active(1),
90 Alert("alertname", "InstanceDown", "job", "testjob", "zone", "ab").Active(1),
91 Alert("alertname", "JobDown", "job", "testjob", "zone", "aa").Active(2.2, 3.6),
92 )
93
94 at.Run()
95
96 t.Log(co.Check())
97 }
98
99 func TestAlwaysInhibiting(t *testing.T) {
100 t.Parallel()
101
102
103
104
105
106 conf := `
107 route:
108 receiver: "default"
109 group_by: []
110 group_wait: 1s
111 group_interval: 1s
112 repeat_interval: 1s
113
114 receivers:
115 - name: "default"
116 webhook_configs:
117 - url: 'http://%s'
118
119 inhibit_rules:
120 - source_match:
121 alertname: JobDown
122 target_match:
123 alertname: InstanceDown
124 equal:
125 - job
126 - zone
127 `
128
129 at := NewAcceptanceTest(t, &AcceptanceOpts{
130 Tolerance: 150 * time.Millisecond,
131 })
132
133 co := at.Collector("webhook")
134 wh := NewWebhook(t, co)
135
136 amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
137
138 amc.Push(At(1), Alert("alertname", "InstanceDown", "job", "testjob", "zone", "aa"))
139 amc.Push(At(1), Alert("alertname", "JobDown", "job", "testjob", "zone", "aa"))
140
141 amc.Push(At(2.6), Alert("alertname", "JobDown", "job", "testjob", "zone", "aa").Active(1, 2.6))
142 amc.Push(At(2.6), Alert("alertname", "InstanceDown", "job", "testjob", "zone", "aa").Active(1, 2.6))
143
144 co.Want(Between(2, 2.5),
145 Alert("alertname", "JobDown", "job", "testjob", "zone", "aa").Active(1),
146 )
147
148 co.Want(Between(3, 3.5),
149 Alert("alertname", "InstanceDown", "job", "testjob", "zone", "aa").Active(1, 2.6),
150 Alert("alertname", "JobDown", "job", "testjob", "zone", "aa").Active(1, 2.6),
151 )
152
153 at.Run()
154
155 t.Log(co.Check())
156 }
157
View as plain text