Skip to content

Commit 0029da6

Browse files
committed
Add complete interpreter test for Policy
Signed-off-by: Abhay349 <[email protected]>
1 parent b5a60ff commit 0029da6

File tree

3 files changed

+339
-0
lines changed

3 files changed

+339
-0
lines changed

pkg/resourceinterpreter/default/thirdparty/resourcecustomizations/kyverno.io/v1/Policy/testdata/aggregatestatus-test.yaml

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# test case for aggregating status of Policy
22
# case1. Policy with two status items
3+
# case2. Policy with different Ready reasons
4+
# case3. Policy with nil statusItems
35

46
name: "Policy with two status items"
57
description: "Test aggregating status of Policy with two status items"
@@ -145,3 +147,139 @@ statusItems:
145147
verifyimages: 0
146148
operation: AggregateStatus
147149
output:
150+
aggregatedStatus:
151+
apiVersion: kyverno.io/v1
152+
kind: Policy
153+
metadata:
154+
name: sample
155+
namespace: test-policy
156+
spec:
157+
validationFailureAction: Enforce
158+
rules:
159+
- name: require-pod-purpose-label
160+
match:
161+
any:
162+
- resources:
163+
kinds:
164+
- Pod
165+
validate:
166+
message: "You must have label `purpose` with value `production` set on all new Pod in test-policy Namespace."
167+
pattern:
168+
metadata:
169+
labels:
170+
purpose: production
171+
status:
172+
ready: true
173+
autogen:
174+
rules:
175+
- match:
176+
any:
177+
- resources:
178+
kinds:
179+
- DaemonSet
180+
- Deployment
181+
- Job
182+
- StatefulSet
183+
- ReplicaSet
184+
- ReplicationController
185+
name: autogen-require-pod-purpose-label
186+
validate:
187+
message: You must have label `purpose` with value `production` set on all new Pod in test-policy Namespace.
188+
pattern:
189+
spec:
190+
template:
191+
metadata:
192+
labels:
193+
purpose: production
194+
- match:
195+
any:
196+
- resources:
197+
kinds:
198+
- CronJob
199+
name: autogen-cronjob-require-pod-purpose-label
200+
validate:
201+
message: You must have label `purpose` with value `production` set on all new Pod in test-policy Namespace.
202+
pattern:
203+
spec:
204+
jobTemplate:
205+
spec:
206+
template:
207+
metadata:
208+
labels:
209+
purpose: production
210+
rulecount:
211+
generate: 0
212+
mutate: 0
213+
validate: 2
214+
verifyimages: 0
215+
conditions:
216+
- type: Ready
217+
status: "True"
218+
reason: Succeeded
219+
lastTransitionTime: "2023-05-07T09:19:06Z"
220+
message: "member2=, member3="
221+
---
222+
name: "Policy with different Ready reasons"
223+
description: "Conditions with different reasons should not be merged"
224+
desiredObj:
225+
apiVersion: kyverno.io/v1
226+
kind: Policy
227+
metadata:
228+
name: mixed-reasons
229+
namespace: test-policy
230+
statusItems:
231+
- clusterName: member1
232+
status:
233+
conditions:
234+
- type: Ready
235+
status: "True"
236+
reason: Succeeded
237+
message: "ok"
238+
- clusterName: member2
239+
status:
240+
conditions:
241+
- type: Ready
242+
status: "True"
243+
reason: Failed
244+
message: "error"
245+
operation: AggregateStatus
246+
output:
247+
aggregatedStatus:
248+
apiVersion: kyverno.io/v1
249+
kind: Policy
250+
metadata:
251+
name: mixed-reasons
252+
namespace: test-policy
253+
status:
254+
conditions:
255+
- type: Ready
256+
status: "True"
257+
reason: Succeeded
258+
message: "member1=ok"
259+
- type: Ready
260+
status: "True"
261+
reason: Failed
262+
message: "member2=error"
263+
rulecount:
264+
generate: 0
265+
mutate: 0
266+
validate: 0
267+
verifyimages: 0
268+
---
269+
name: "Policy with nil statusItems"
270+
description: "AggregateStatus should return desiredObj unchanged when statusItems is nil"
271+
desiredObj:
272+
apiVersion: kyverno.io/v1
273+
kind: Policy
274+
metadata:
275+
name: no-status
276+
namespace: test-policy
277+
statusItems: null
278+
operation: AggregateStatus
279+
output:
280+
aggregatedStatus:
281+
apiVersion: kyverno.io/v1
282+
kind: Policy
283+
metadata:
284+
name: no-status
285+
namespace: test-policy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# testcases for interprethealth of Policy
2+
# case1. Policy with status.ready = true
3+
# case2. Policy with status.ready false
4+
# case3. Policy with Ready=True and Succeeded condition
5+
# case4. Policy with Ready=True but non-succeeded reason
6+
# case5. Policy with empty status
7+
# case6. Policy without status
8+
9+
name: "Policy with status.ready true"
10+
description: "InterpretHealth should return true when status.ready is true"
11+
observedObj:
12+
apiVersion: kyverno.io/v1
13+
kind: Policy
14+
metadata:
15+
name: sample
16+
namespace: test-policy
17+
status:
18+
ready: true
19+
operation: InterpretHealth
20+
output:
21+
healthy: true
22+
---
23+
name: "Policy with status.ready false"
24+
description: "InterpretHealth should return false when status.ready is false"
25+
observedObj:
26+
apiVersion: kyverno.io/v1
27+
kind: Policy
28+
metadata:
29+
name: sample
30+
namespace: test-policy
31+
status:
32+
ready: false
33+
operation: InterpretHealth
34+
output:
35+
healthy: false
36+
---
37+
name: "Policy with Ready condition succeeded"
38+
description: "InterpretHealth should return true when Ready condition is True and Succeeded"
39+
observedObj:
40+
apiVersion: kyverno.io/v1
41+
kind: Policy
42+
metadata:
43+
name: sample
44+
namespace: test-policy
45+
status:
46+
conditions:
47+
- type: Ready
48+
status: "True"
49+
reason: Succeeded
50+
operation: InterpretHealth
51+
output:
52+
healthy: true
53+
---
54+
name: "Policy with Ready condition but failed reason"
55+
description: "InterpretHealth should return false if Ready condition reason is not Succeeded"
56+
observedObj:
57+
apiVersion: kyverno.io/v1
58+
kind: Policy
59+
metadata:
60+
name: sample
61+
namespace: test-policy
62+
status:
63+
conditions:
64+
- type: Ready
65+
status: "True"
66+
reason: Failed
67+
operation: InterpretHealth
68+
output:
69+
healthy: false
70+
---
71+
name: "Policy with empty status"
72+
description: "InterpretHealth should return false when status has no ready or conditions"
73+
observedObj:
74+
apiVersion: kyverno.io/v1
75+
kind: Policy
76+
metadata:
77+
name: sample
78+
namespace: test-policy
79+
status: {}
80+
operation: InterpretHealth
81+
output:
82+
healthy: false
83+
---
84+
name: "Policy without status"
85+
description: "InterpretHealth should return false when status is nil"
86+
observedObj:
87+
apiVersion: kyverno.io/v1
88+
kind: Policy
89+
metadata:
90+
name: sample
91+
namespace: test-policy
92+
operation: InterpretHealth
93+
output:
94+
healthy: false

pkg/resourceinterpreter/default/thirdparty/resourcecustomizations/kyverno.io/v1/Policy/testdata/interpretstatus-test.yaml

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# test case for interpreting status of Policy
22
# case1. Policy: interpret status test
3+
# case2. Policy with nil status
4+
# case3. Policy with only ready field
5+
# case4. Policy without conditions
36

47
name: "Policy: interpret status test"
58
description: "Test interpreting status for Policy"
@@ -92,3 +95,107 @@ observedObj:
9295
verifyimages: 0
9396
operation: InterpretStatus
9497
output:
98+
status:
99+
autogen:
100+
rules:
101+
- exclude: {}
102+
generate: {}
103+
match:
104+
any:
105+
- resources:
106+
kinds:
107+
- DaemonSet
108+
- Deployment
109+
- Job
110+
- StatefulSet
111+
- ReplicaSet
112+
- ReplicationController
113+
name: autogen-require-pod-purpose-label
114+
validate:
115+
message: You must have label `purpose` with value `production` set on all new Pod in test-policy Namespace.
116+
pattern:
117+
spec:
118+
template:
119+
metadata:
120+
labels:
121+
purpose: production
122+
- exclude: {}
123+
generate: {}
124+
match:
125+
any:
126+
- resources:
127+
kinds:
128+
- CronJob
129+
name: autogen-cronjob-require-pod-purpose-label
130+
validate:
131+
message: You must have label `purpose` with value `production` set on all new Pod in test-policy Namespace.
132+
pattern:
133+
spec:
134+
jobTemplate:
135+
spec:
136+
template:
137+
metadata:
138+
labels:
139+
purpose: production
140+
conditions:
141+
- lastTransitionTime: "2023-05-07T09:19:06Z"
142+
message: ""
143+
reason: Succeeded
144+
status: "True"
145+
type: Ready
146+
ready: true
147+
rulecount:
148+
generate: 0
149+
mutate: 0
150+
validate: 1
151+
verifyimages: 0
152+
---
153+
name: "Policy with nil status"
154+
description: "InterprettStatus should return empty status when status is nil"
155+
observedObj:
156+
apiVersion: kyverno.io/v1
157+
kind: Policy
158+
metadata:
159+
name: sample
160+
namespace: test-policy
161+
operation: InterpretStatus
162+
output:
163+
status: {}
164+
---
165+
name: "Policy with only ready"
166+
description: "InterpretStatus should copy ready when other fields are missing"
167+
observedObj:
168+
apiVersion: kyverno.io/v1
169+
kind: Policy
170+
metadata:
171+
name: sample
172+
namespace: test-policy
173+
status:
174+
ready: false
175+
operation: InterpretStatus
176+
output:
177+
status:
178+
ready: false
179+
---
180+
name: "Policy without conditions"
181+
description: "InterpretStatus should work when conditions are absent"
182+
observedObj:
183+
apiVersion: kyverno.io/v1
184+
kind: Policy
185+
metadata:
186+
name: sample
187+
namespace: test-policy
188+
status:
189+
rulecount:
190+
generate: 0
191+
mutate: 1
192+
validate: 0
193+
verifyimages: 0
194+
operation: InterpretStatus
195+
output:
196+
status:
197+
rulecount:
198+
generate: 0
199+
mutate: 1
200+
validate: 0
201+
verifyimages: 0

0 commit comments

Comments
 (0)