forked from open-policy-agent/frameworks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_client_test.go
More file actions
280 lines (262 loc) · 7.6 KB
/
template_client_test.go
File metadata and controls
280 lines (262 loc) · 7.6 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package client
import (
"testing"
"github.com/open-policy-agent/frameworks/constraint/pkg/core/templates"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
)
func TestTemplateClient_MatchesOperation(t *testing.T) {
tests := []struct {
name string
targets []templates.Target
operation string
expected bool
description string
}{
{
name: "no targets - returns true for backward compatibility",
targets: []templates.Target{},
operation: "CREATE",
expected: true,
description: "when no targets are defined, should return true for backward compatibility",
},
{
name: "multiple targets - returns true for backward compatibility",
targets: []templates.Target{
{Target: "target1"},
{Target: "target2"},
},
operation: "CREATE",
expected: true,
description: "when multiple targets are defined, should return true for backward compatibility",
},
{
name: "single target with no operations - allows all operations",
targets: []templates.Target{
{
Target: "admission.k8s.gatekeeper.sh",
Operations: []admissionregistrationv1.OperationType{},
},
},
operation: "CREATE",
expected: true,
description: "when no operations are specified, should allow all operations",
},
{
name: "single target with CREATE operation - matches CREATE",
targets: []templates.Target{
{
Target: "admission.k8s.gatekeeper.sh",
Operations: []admissionregistrationv1.OperationType{admissionregistrationv1.Create},
},
},
operation: "CREATE",
expected: true,
description: "should match when operation is explicitly listed",
},
{
name: "single target with CREATE operation - does not match UPDATE",
targets: []templates.Target{
{
Target: "admission.k8s.gatekeeper.sh",
Operations: []admissionregistrationv1.OperationType{admissionregistrationv1.Create},
},
},
operation: "UPDATE",
expected: false,
description: "should not match when operation is not listed",
},
{
name: "single target with multiple operations - matches CREATE",
targets: []templates.Target{
{
Target: "admission.k8s.gatekeeper.sh",
Operations: []admissionregistrationv1.OperationType{admissionregistrationv1.Create, admissionregistrationv1.Update},
},
},
operation: "CREATE",
expected: true,
description: "should match when operation is one of multiple listed operations",
},
{
name: "single target with multiple operations - does not match DELETE",
targets: []templates.Target{
{
Target: "admission.k8s.gatekeeper.sh",
Operations: []admissionregistrationv1.OperationType{admissionregistrationv1.Create, admissionregistrationv1.Update},
},
},
operation: "DELETE",
expected: false,
description: "should not match when operation is not in the list",
},
{
name: "single target with wildcard operation - matches DELETE",
targets: []templates.Target{
{
Target: "admission.k8s.gatekeeper.sh",
Operations: []admissionregistrationv1.OperationType{admissionregistrationv1.OperationAll},
},
},
operation: "DELETE",
expected: true,
description: "should match any operation when wildcard (*) is specified",
},
{
name: "single target with mixed operations including wildcard",
targets: []templates.Target{
{
Target: "admission.k8s.gatekeeper.sh",
Operations: []admissionregistrationv1.OperationType{admissionregistrationv1.Create, admissionregistrationv1.OperationAll},
},
},
operation: "DELETE",
expected: true,
description: "should match when wildcard is present even with other specific operations",
},
{
name: "empty operation string",
targets: []templates.Target{
{
Target: "admission.k8s.gatekeeper.sh",
Operations: []admissionregistrationv1.OperationType{admissionregistrationv1.Create},
},
},
operation: "",
expected: false,
description: "should not match empty operation string",
},
{
name: "all standard operations",
targets: []templates.Target{
{
Target: "admission.k8s.gatekeeper.sh",
Operations: []admissionregistrationv1.OperationType{
admissionregistrationv1.Create,
admissionregistrationv1.Update,
admissionregistrationv1.Delete,
admissionregistrationv1.Connect,
},
},
},
operation: "UPDATE",
expected: true,
description: "should match when operation is in comprehensive list",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tc := &templateClient{
template: &templates.ConstraintTemplate{
Spec: templates.ConstraintTemplateSpec{
Targets: tt.targets,
},
},
}
result := tc.MatchesOperation(tt.operation)
if result != tt.expected {
t.Errorf("MatchesOperation(%q) = %v, expected %v\nDescription: %s",
tt.operation, result, tt.expected, tt.description)
}
})
}
}
func TestTemplateClient_MatchesOperation_EdgeCases(t *testing.T) {
t.Run("nil template", func(t *testing.T) {
tc := &templateClient{
template: nil,
}
// This should panic or handle gracefully
defer func() {
if r := recover(); r != nil {
t.Logf("Function panicked as expected when template is nil: %v", r)
}
}()
// If it doesn't panic, it should return false or handle gracefully
result := tc.MatchesOperation("CREATE")
t.Logf("Result with nil template: %v", result)
})
t.Run("nil template spec", func(t *testing.T) {
tc := &templateClient{
template: &templates.ConstraintTemplate{},
}
// Should handle gracefully when spec is not initialized
result := tc.MatchesOperation("CREATE")
expected := true // Should return true for backward compatibility when no targets
if result != expected {
t.Errorf("MatchesOperation with nil spec = %v, expected %v", result, expected)
}
})
}
func TestTemplateClient_MatchesOperation_BackwardCompatibility(t *testing.T) {
tests := []struct {
name string
targetCount int
operation string
expected bool
}{
{
name: "zero targets",
targetCount: 0,
operation: "CREATE",
expected: true,
},
{
name: "two targets",
targetCount: 2,
operation: "UPDATE",
expected: true,
},
{
name: "three targets",
targetCount: 3,
operation: "DELETE",
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
targets := make([]templates.Target, tt.targetCount)
for i := 0; i < tt.targetCount; i++ {
targets[i] = templates.Target{
Target: "target" + string(rune('1'+i)),
}
}
tc := &templateClient{
template: &templates.ConstraintTemplate{
Spec: templates.ConstraintTemplateSpec{
Targets: targets,
},
},
}
result := tc.MatchesOperation(tt.operation)
if result != tt.expected {
t.Errorf("MatchesOperation with %d targets = %v, expected %v (backward compatibility)",
tt.targetCount, result, tt.expected)
}
})
}
}
// Benchmark test to ensure the function is performant.
func BenchmarkTemplateClient_MatchesOperation(b *testing.B) {
tc := &templateClient{
template: &templates.ConstraintTemplate{
Spec: templates.ConstraintTemplateSpec{
Targets: []templates.Target{
{
Target: "admission.k8s.gatekeeper.sh",
Operations: []admissionregistrationv1.OperationType{
admissionregistrationv1.Create,
admissionregistrationv1.Update,
admissionregistrationv1.Delete,
admissionregistrationv1.Connect,
},
},
},
},
},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
tc.MatchesOperation("UPDATE")
}
}