forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsilence_test.go
142 lines (134 loc) · 3.78 KB
/
silence_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
package models
import (
"testing"
"github.com/prometheus/alertmanager/pkg/labels"
"github.com/stretchr/testify/assert"
"github.com/grafana/alerting/models"
"github.com/grafana/grafana/pkg/util"
)
func TestSilenceGetRuleUID(t *testing.T) {
testCases := []struct {
name string
silence Silence
expectedRuleUID *string
}{
{
name: "silence with no rule UID",
silence: SilenceGen()(),
expectedRuleUID: nil,
},
{
name: "silence with rule UID",
silence: SilenceGen(SilenceMuts.WithMatcher(models.RuleUIDLabel, "someuid", labels.MatchEqual))(),
expectedRuleUID: util.Pointer("someuid"),
},
{
name: "silence with rule UID Matcher but MatchNotEqual",
silence: SilenceGen(SilenceMuts.WithMatcher(models.RuleUIDLabel, "someuid", labels.MatchNotEqual))(),
expectedRuleUID: nil,
},
{
name: "silence with rule UID Matcher but MatchRegexp",
silence: SilenceGen(SilenceMuts.WithMatcher(models.RuleUIDLabel, "someuid", labels.MatchRegexp))(),
expectedRuleUID: nil,
},
{
name: "silence with rule UID Matcher but MatchNotRegexp",
silence: SilenceGen(SilenceMuts.WithMatcher(models.RuleUIDLabel, "someuid", labels.MatchNotRegexp))(),
expectedRuleUID: nil,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expectedRuleUID, tt.silence.GetRuleUID(), "unexpected rule UID")
})
}
}
func TestSilencePermissionSet(t *testing.T) {
t.Run("Clone", func(t *testing.T) {
perms := SilencePermissionSet{
SilencePermissionRead: true,
SilencePermissionWrite: false,
}
clone := perms.Clone()
assert.Equal(t, perms, clone)
clone[SilencePermissionRead] = false
assert.NotEqual(t, perms, clone)
})
t.Run("AllSet + SilencePermissions", func(t *testing.T) {
readPerms := SilencePermissionSet{
SilencePermissionRead: true,
}
assert.False(t, readPerms.AllSet())
allPerms := SilencePermissionSet{}
for _, perm := range SilencePermissions() {
allPerms[perm] = true
}
assert.True(t, allPerms.AllSet())
})
t.Run("Has", func(t *testing.T) {
testCases := []struct {
name string
permissionSet SilencePermissionSet
expectedHas map[SilencePermission]bool
}{
{
name: "all false",
permissionSet: SilencePermissionSet{
SilencePermissionRead: false,
SilencePermissionWrite: false,
SilencePermissionCreate: false,
},
expectedHas: map[SilencePermission]bool{
SilencePermissionRead: false,
SilencePermissionWrite: false,
SilencePermissionCreate: false,
},
},
{
name: "all true",
permissionSet: SilencePermissionSet{
SilencePermissionRead: true,
SilencePermissionWrite: true,
SilencePermissionCreate: true,
},
expectedHas: map[SilencePermission]bool{
SilencePermissionRead: true,
SilencePermissionWrite: true,
SilencePermissionCreate: true,
},
},
{
name: "mixed",
permissionSet: SilencePermissionSet{
SilencePermissionRead: true,
SilencePermissionWrite: false,
SilencePermissionCreate: true,
},
expectedHas: map[SilencePermission]bool{
SilencePermissionRead: true,
SilencePermissionWrite: false,
SilencePermissionCreate: true,
},
},
{
name: "not set = false",
permissionSet: SilencePermissionSet{
SilencePermissionRead: true,
},
expectedHas: map[SilencePermission]bool{
SilencePermissionRead: true,
SilencePermissionWrite: false,
SilencePermissionCreate: false,
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for perm, expected := range tc.expectedHas {
assert.Equal(t, expected, tc.permissionSet.Has(perm))
}
})
}
})
}