-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalert_rules_test.go
More file actions
56 lines (54 loc) · 1.76 KB
/
Copy pathalert_rules_test.go
File metadata and controls
56 lines (54 loc) · 1.76 KB
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
package flashduty
import (
"context"
"encoding/json"
"net/http"
"testing"
)
func TestAlertRuleUpdateV2PreservesInvestigationTargetsPresence(t *testing.T) {
tests := []struct {
name string
targets []InvestigationTarget
want string
}{
{name: "omit keeps existing targets"},
{name: "empty clears targets", targets: []InvestigationTarget{}, want: `[]`},
{
name: "replace targets",
targets: []InvestigationTarget{{
Kind: "dashboard",
Dashboard: DashboardInvestigationTarget{
DashboardID: "01900000-0000-7000-8000-000000000001",
Variables: map[string]string{},
},
TimeRange: InvestigationTimeRange{BeforeSeconds: 1800, AfterSeconds: 600},
}},
want: `[{"dashboard":{"dashboard_id":"01900000-0000-7000-8000-000000000001","variables":{}},"kind":"dashboard","time_range":{"after_seconds":600,"before_seconds":1800}}]`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost || r.URL.Path != "/monit/rule/v2/update" {
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
}
var body map[string]json.RawMessage
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
t.Errorf("decode request: %v", err)
}
if got := string(body["investigation_targets"]); got != tt.want {
t.Errorf("investigation_targets = %q, want %q", got, tt.want)
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{"data": body})
})
_, _, err := client.AlertRules.WriteUpdateV2(context.Background(), &AlertRuleV2{
ID: 123,
InvestigationTargets: tt.targets,
})
if err != nil {
t.Fatal(err)
}
})
}
}