Skip to content

Commit 642a44a

Browse files
committed
Add testdata for e2e testing
Signed-off-by: Chuan-Yen Chiang <cychiang0823@gmail.com>
1 parent 505dabb commit 642a44a

4 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: apiextensions.k8s.io/v1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: immutables.cmd.example.org
5+
spec:
6+
group: cmd.example.org
7+
names:
8+
kind: Immutable
9+
listKind: ImmutableList
10+
plural: immutables
11+
singular: immutable
12+
scope: Cluster
13+
versions:
14+
- name: v1alpha1
15+
served: true
16+
storage: true
17+
schema:
18+
openAPIV3Schema:
19+
type: object
20+
properties:
21+
spec:
22+
type: object
23+
required:
24+
- param
25+
properties:
26+
param:
27+
type: string
28+
x-kubernetes-validations:
29+
- rule: "self.param == oldSelf.param"
30+
message: "param is immutable"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: cmd.example.org/v1alpha1
2+
kind: Immutable
3+
metadata:
4+
name: immutable-instance
5+
spec:
6+
param: changed-value
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: cmd.example.org/v1alpha1
2+
kind: Immutable
3+
metadata:
4+
name: immutable-instance
5+
spec:
6+
param: original-value

cmd/crossplane/validate/validate_integration_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,40 @@ func TestRun(t *testing.T) {
232232
}
233233
},
234234
},
235+
"OldResourcesTransitionViolationExitsNonZero": {
236+
reason: "With --old-resources supplying the previous state, a CEL transition rule (immutable field changed) fires: the resource is Invalid with a CEL error and the command exits non-zero.",
237+
extensions: "testdata/cmd/crd_transition.yaml",
238+
resources: "testdata/cmd/resources_transition_new.yaml",
239+
extraArgs: []string{"--output=json", "--old-resources=testdata/cmd/resources_transition_old.yaml"},
240+
wantErr: true,
241+
assertJSON: func(t *testing.T, r *pkgvalidate.ValidationResult) {
242+
t.Helper()
243+
if r.Summary.Invalid != 1 {
244+
t.Errorf("Summary.Invalid = %d; want 1", r.Summary.Invalid)
245+
}
246+
if len(r.Resources) != 1 || r.Resources[0].Status != pkgvalidate.ValidationStatusInvalid {
247+
t.Errorf("Resources = %+v; want one Invalid entry", r.Resources)
248+
}
249+
if len(r.Resources[0].Errors) == 0 || r.Resources[0].Errors[0].Type != pkgvalidate.FieldErrorTypeCEL {
250+
t.Errorf("Resources[0].Errors = %+v; want a CEL error", r.Resources[0].Errors)
251+
}
252+
},
253+
},
254+
"OldResourcesTransitionSkippedWithoutFlag": {
255+
reason: "Without --old-resources the same resource is Valid: the transition rule references oldSelf and is skipped, exactly as on a create.",
256+
extensions: "testdata/cmd/crd_transition.yaml",
257+
resources: "testdata/cmd/resources_transition_new.yaml",
258+
extraArgs: []string{"--output=json"},
259+
assertJSON: func(t *testing.T, r *pkgvalidate.ValidationResult) {
260+
t.Helper()
261+
if r.Summary.Total != 1 || r.Summary.Valid != 1 {
262+
t.Errorf("Summary = %+v; want Total=1 Valid=1", r.Summary)
263+
}
264+
if len(r.Resources) != 1 || r.Resources[0].Status != pkgvalidate.ValidationStatusValid {
265+
t.Errorf("Resources = %+v; want one Valid entry", r.Resources)
266+
}
267+
},
268+
},
235269
}
236270

237271
for name, tc := range cases {

0 commit comments

Comments
 (0)