This repository was archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrules_test.go
219 lines (203 loc) · 5.1 KB
/
rules_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"io/ioutil"
"os"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)
func createValidLabels() map[string]interface{} {
labels := make(map[string]interface{})
labels["phone-number"] = "555-555-5555"
labels["number"] = "0"
return labels
}
func createInvalidLabels() map[string]interface{} {
labels := make(map[string]interface{})
labels["phone-number"] = "555-555-5555"
labels["num"] = "0"
return labels
}
func createInvalidYamlFile(t *testing.T, path string) {
// Create invalid yaml file
invalidYaml := `
rules:
- name:`
invalidYamlFile := []byte(invalidYaml)
// Break test if error writing file
fileErr := ioutil.WriteFile(path, invalidYamlFile, 0600)
if fileErr != nil {
t.Error("Error writing test yaml file")
}
}
func createInvalidRulesFile(t *testing.T, path string) {
// Create invalid yaml file
invalidRules := &rules{
Rules: []rule{
{
Name: "require-phone-number",
Key: "phone-number",
Value: value{
Regex: "[",
},
},
},
}
data, _ := yaml.Marshal(invalidRules)
// Break test if error writing file
fileErr := ioutil.WriteFile(path, data, 0600)
if fileErr != nil {
t.Error("Error writing test yaml file")
}
}
func TestLoadValid(t *testing.T) {
t.Parallel()
// Load valid yaml
// New struct
r := &rules{}
// Init map
r.compiledRegexs = make(map[string]*regexp.Regexp)
// Load valid yaml
err := r.load("rules.yaml")
assert.NoError(t, err)
}
func TestLoadInvalid(t *testing.T) {
t.Parallel()
// Load invalid yaml
// New struct
r := &rules{}
// Init map
r.compiledRegexs = make(map[string]*regexp.Regexp)
// Create invalid yaml file
invalidYamlFile := "test.yaml"
createInvalidYamlFile(t, invalidYamlFile)
// Clean file after test
defer os.Remove(invalidYamlFile)
// Ensure error due to invalid yaml
assert.Error(t, r.load(invalidYamlFile))
}
func TestCompileRegexValid(t *testing.T) {
t.Parallel()
// New struct
r := &rules{}
// Init map
r.compiledRegexs = make(map[string]*regexp.Regexp)
// Load valid yaml
err := r.load("rules.yaml")
if err != nil {
t.Error("Error loading yaml")
}
// Ensure no rule errors
assert.Equal(t, 0, len(r.compileRegex(false)), "Compiling regex should not return any errors")
}
func TestCompileRegexValidStore(t *testing.T) {
t.Parallel()
// New struct
r := &rules{}
// Init map
r.compiledRegexs = make(map[string]*regexp.Regexp)
// Load valid yaml
err := r.load("rules.yaml")
if err != nil {
t.Error("Error loading yaml")
}
// Store in map
r.compileRegex(true)
_, phoneKeyPresent := r.compiledRegexs["require-phone-number"]
_, numberKeyPresent := r.compiledRegexs["require-number"]
assert.True(t, phoneKeyPresent)
assert.True(t, numberKeyPresent)
}
func TestCompileRegexInvalid(t *testing.T) {
t.Parallel()
invalidRulesFile := "test.yaml"
// Create invalid yaml file
createInvalidRulesFile(t, invalidRulesFile)
// Clean file after test
defer os.Remove(invalidRulesFile)
// New struct
r := &rules{}
// Init map
r.compiledRegexs = make(map[string]*regexp.Regexp)
err := r.load(invalidRulesFile)
if err != nil {
t.Error("Error loading yaml")
}
// Ensure 1 rule error
assert.Equal(t, 1, len(r.compileRegex(false)), "Compiling regex should return 1 error")
}
func TestValidateAllRulesRegex(t *testing.T) {
t.Parallel()
// New struct
r := &rules{}
// Init map
r.compiledRegexs = make(map[string]*regexp.Regexp)
// Load valid yaml
err := r.load("rules.yaml")
if err != nil {
t.Error("Error loading yaml")
}
// Ensure no rule errors
assert.Equal(t, 0, len(r.validateAllRulesRegex()), "Compiling regex should not return any errors")
}
func TestEnsureLabelsContainRulesValid(t *testing.T) {
t.Parallel()
// New struct
r := &rules{}
// Init map
r.compiledRegexs = make(map[string]*regexp.Regexp)
// Load valid yaml
err := r.load("rules.yaml")
if err != nil {
t.Error("Error loading yaml")
}
// Simulate labels from k8s request
labels := createValidLabels()
assert.NoError(t, r.ensureLabelsMatchRules(labels))
}
func TestEnsureLabelsContainRulesInvalid(t *testing.T) {
t.Parallel()
// New struct
r := &rules{}
// Init map
r.compiledRegexs = make(map[string]*regexp.Regexp)
// Load valid yaml
err := r.load("rules.yaml")
if err != nil {
t.Error("Error loading yaml")
}
// Simulate labels from k8s request
labels := createInvalidLabels()
assert.Error(t, r.ensureLabelsMatchRules(labels))
}
func TestEnsureLabelsMatchRulesValid(t *testing.T) {
t.Parallel()
// New struct
r := &rules{}
// Init map
r.compiledRegexs = make(map[string]*regexp.Regexp)
// Load valid yaml
err := r.load("rules.yaml")
if err != nil {
t.Error("Error loading yaml")
}
// Simulate labels from k8s request
labels := createValidLabels()
assert.NoError(t, r.ensureLabelsMatchRules(labels))
}
func TestEnsureLabelsMatchRulesInvalid(t *testing.T) {
t.Parallel()
// New struct
r := &rules{}
// Init map
r.compiledRegexs = make(map[string]*regexp.Regexp)
// Load valid yaml
err := r.load("rules.yaml")
if err != nil {
t.Error("Error loading yaml")
}
// Simulate labels from k8s request
labels := createInvalidLabels()
assert.Error(t, r.ensureLabelsMatchRules(labels))
}