Skip to content

Commit 3d2fbff

Browse files
committed
test(kyverno): cover batch workload admission
1 parent acc8033 commit 3d2fbff

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎scripts/tests/tenant-webapp-admission/main.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ func evaluate(f fixture, policies map[string]*policiesv1beta1.ValidatingPolicy)
169169
target = f.OldObject
170170
}
171171
resources := map[string]string{"CiliumNetworkPolicy": "ciliumnetworkpolicies", "Tenant": "tenants", "WebApp": "webapps", "Deployment": "deployments", "Service": "services", "HTTPRoute": "httproutes", "ExternalSecret": "externalsecrets", "SecretStore": "secretstores", "PushSecret": "pushsecrets", "ConfigMap": "configmaps", "Team": "teams", "TeamRepository": "teamrepositories", "TeamMembership": "teammemberships",
172-
"HelmRelease": "helmreleases", "AutoscalingRunnerSet": "autoscalingrunnersets", "AutoscalingListener": "autoscalinglisteners", "Pod": "pods", "Secret": "secrets", "Namespace": "namespaces"}
172+
"HelmRelease": "helmreleases", "AutoscalingRunnerSet": "autoscalingrunnersets", "AutoscalingListener": "autoscalinglisteners", "Pod": "pods", "Secret": "secrets", "Namespace": "namespaces",
173+
"Job": "jobs", "CronJob": "cronjobs"}
173174
gvk := target.GroupVersionKind()
174175
resource, ok := resources[gvk.Kind]
175176
if !ok || gvk.Version == "" {

‎scripts/tests/tenant-webapp-admission/main_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,42 @@ spec:
148148
t.Fatal("an UPDATE without its old object must be refused")
149149
}
150150
}
151+
152+
func TestAdapterMatchesBatchWorkloads(t *testing.T) {
153+
var policy policiesv1beta1.ValidatingPolicy
154+
if err := yaml.UnmarshalStrict([]byte(`apiVersion: policies.kyverno.io/v1
155+
kind: ValidatingPolicy
156+
metadata: {name: batch-control}
157+
spec:
158+
validationActions: [Deny]
159+
matchConstraints:
160+
resourceRules:
161+
- apiGroups: [batch]
162+
apiVersions: [v1]
163+
operations: [CREATE]
164+
resources: [jobs, cronjobs]
165+
validations:
166+
- expression: "object.metadata.name == 'allowed'"
167+
message: only the positive fixture is admitted
168+
`), &policy); err != nil {
169+
t.Fatal(err)
170+
}
171+
policies := map[string]*policiesv1beta1.ValidatingPolicy{"batch-control": &policy}
172+
for _, kind := range []string{"Job", "CronJob"} {
173+
t.Run(kind, func(t *testing.T) {
174+
object := &unstructured.Unstructured{Object: map[string]any{
175+
"apiVersion": "batch/v1", "kind": kind,
176+
"metadata": map[string]any{"name": "allowed", "namespace": "fixture"},
177+
}}
178+
f := fixture{Label: kind, Policy: "batch-control", Actor: "fixture", Object: object, Operation: "CREATE", Expected: "pass"}
179+
if err := evaluate(f, policies); err != nil {
180+
t.Fatal(err)
181+
}
182+
f.Object.SetName("denied")
183+
f.Expected = "fail"
184+
if err := evaluate(f, policies); err != nil {
185+
t.Fatal(err)
186+
}
187+
})
188+
}
189+
}

0 commit comments

Comments
 (0)