Skip to content

Commit

Permalink
Add CEL rules test suite
Browse files Browse the repository at this point in the history
This patch adds several YAML test cases that check if the CEL rules
included in the CRDs are correctly working. Both validation rules and
transition rules are checked.

A shell script is provided to execute these tests.
  • Loading branch information
leonardoce committed May 13, 2024
1 parent 0932517 commit 35702d9
Show file tree
Hide file tree
Showing 104 changed files with 977 additions and 0 deletions.
34 changes: 34 additions & 0 deletions client/hack/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Scripts User Guide

This README documents:

* What update-crd.sh and update-generated-code.sh do
* When and how to use them
* The CRD CEL rules test suite

## update-generated-code.sh

Expand Down Expand Up @@ -104,3 +106,35 @@ Update the restoreSize property to use type string only:
```
* Add the VolumeSnapshot namespace to the `additionalPrinterColumns` section in `client/config/crd/snapshot.storage.k8s.io_volumesnapshotcontents.yaml`. Refer https://github.com/kubernetes-csi/external-snapshotter/pull/535 for more details.
## Test suite
The `test-suite` directory contains several test cases that are useful to
validate if the CEL rules that are included in the CRD definitions
are correctly working.
### Prerequisites
- Kubectl access to a cluster with the installed CRDs
- Kubernetes >= 1.29
### How to use it
```
./hack/run-cel-tests.sh
cel-tests/volumegroupsnapshotcontent/vgsc-change-ref-namespace.post.yaml: SUCCESS
cel-tests/volumegroupsnapshotcontent/vgsc-source-volume-to-groupsnapshot.post.yaml: SUCCESS
cel-tests/volumegroupsnapshotcontent/vgsc-source-empty.yaml: SUCCEES (expected failure)
cel-tests/volumegroupsnapshotcontent/vgsc-change-ref-namespace.pre.yaml: SUCCESS
cel-tests/volumegroupsnapshotcontent/vgsc-ref-only-name.yaml: SUCCEES (expected failure)
[...]
cel-tests/volumegroupsnapshotcontent/vgsc-change-ref-namespace.pre.yaml -> cel-tests/volumegroupsnapshotcontent/vgsc-change-ref-namespace.post.yaml: SUCCEES (expected failure)
cel-tests/volumegroupsnapshotcontent/vgsc-source-volume-immutable.pre.yaml -> cel-tests/volumegroupsnapshotcontent/vgsc-source-volume-immutable.post.yaml: SUCCEES (expected failure)
cel-tests/volumegroupsnapshotcontent/vgsc-source-volume-to-groupsnapshot.pre.yaml -> cel-tests/volumegroupsnapshotcontent/vgsc-source-volume-to-groupsnapshot.post.yaml: SUCCEES (expected failure)
cel-tests/volumegroupsnapshotcontent/vgsc-source-groupsnapshot-immutable.pre.yaml -> cel-tests/volumegroupsnapshotcontent/vgsc-source-groupsnapshot-immutable.post.yaml: SUCCEES (expected failure)
[...]
SUCCESS: 90
FAILURES: 0
```
1 change: 1 addition & 0 deletions client/hack/cel-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshot
metadata:
name: new-groupsnapshot-demo
spec:
source:
selector:
matchLabels:
app.kubernetes.io/name: postgresql
volumeGroupSnapshotClassName: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
volumeGroupSnapshotClassName must not be the empty string when set
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshot
metadata:
name: new-groupsnapshot-demo
spec:
source:
volumeGroupSnapshotContentName: this-test-changed
volumeGroupSnapshotClassName: csi-hostpath-groupsnapclass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
volumeGroupSnapshotContentName is immutable
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshot
metadata:
name: new-groupsnapshot-demo
spec:
source:
volumeGroupSnapshotContentName: this-test
volumeGroupSnapshotClassName: csi-hostpath-groupsnapclass
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshot
metadata:
name: new-groupsnapshot-demo
spec:
source:
selector:
matchLabels:
app.kubernetes.io/name: postgresql
volumeGroupSnapshotClassName: csi-hostpath-groupsnapclass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
volumeGroupSnapshotContentName is required once set
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshot
metadata:
name: new-groupsnapshot-demo
spec:
source:
volumeGroupSnapshotContentName: this-test
volumeGroupSnapshotClassName: csi-hostpath-groupsnapclass
10 changes: 10 additions & 0 deletions client/hack/cel-tests/volumegroupsnapshot/vgs-no-class.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshot
metadata:
name: new-groupsnapshot-demo
spec:
source:
selector:
matchLabels:
app.kubernetes.io/name: postgresql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshot
metadata:
name: new-groupsnapshot-demo
spec:
source:
selector:
matchLabels:
app.kubernetes.io/name: mysql
volumeGroupSnapshotClassName: csi-hostpath-groupsnapclass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
selector is immutable
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshot
metadata:
name: new-groupsnapshot-demo
spec:
source:
selector:
matchLabels:
app.kubernetes.io/name: postgresql
volumeGroupSnapshotClassName: csi-hostpath-groupsnapclass
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshot
metadata:
name: new-groupsnapshot-demo
spec:
source:
volumeGroupSnapshotContentName: this-test
volumeGroupSnapshotClassName: csi-hostpath-groupsnapclass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
selector is required once set
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshot
metadata:
name: new-groupsnapshot-demo
spec:
source:
selector:
matchLabels:
app.kubernetes.io/name: postgresql
volumeGroupSnapshotClassName: csi-hostpath-groupsnapclass
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshot
metadata:
name: new-groupsnapshot-demo
spec:
source:
volumeGroupSnapshotContentName: this-test
volumeGroupSnapshotClassName: csi-hostpath-groupsnapclass
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshot
metadata:
name: new-groupsnapshot-demo
spec:
source: {}
volumeGroupSnapshotClassName: csi-hostpath-groupsnapclass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exactly one of selector and volumeGroupSnapshotContentName must be set
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshot
metadata:
name: new-groupsnapshot-demo
spec:
source:
selector:
matchLabels:
app.kubernetes.io/name: postgresql
volumeGroupSnapshotContentName: this-test
volumeGroupSnapshotClassName: csi-hostpath-groupsnapclass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exactly one of selector and volumeGroupSnapshotContentName must be set
11 changes: 11 additions & 0 deletions client/hack/cel-tests/volumegroupsnapshot/vgs-with-selector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshot
metadata:
name: new-groupsnapshot-demo
spec:
source:
selector:
matchLabels:
app.kubernetes.io/name: postgresql
volumeGroupSnapshotClassName: csi-hostpath-groupsnapclass
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshotContent
metadata:
name: new-groupsnapshotcontent-demo
spec:
volumeGroupSnapshotRef:
name: new-groupsnapshot-demo-changed
namespace: default
driver: hostpath.csi.k8s.io
source:
volumeHandles:
- handles
deletionPolicy: Retain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
volumeGroupSnapshotRef is immutable
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshotContent
metadata:
name: new-groupsnapshotcontent-demo
spec:
volumeGroupSnapshotRef:
name: new-groupsnapshot-demo
namespace: default
driver: hostpath.csi.k8s.io
source:
volumeHandles:
- handles
deletionPolicy: Retain
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshotContent
metadata:
name: new-groupsnapshotcontent-demo
spec:
volumeGroupSnapshotRef:
name: new-groupsnapshot-demo
namespace: default-changed
driver: hostpath.csi.k8s.io
source:
volumeHandles:
- handles
deletionPolicy: Retain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
volumeGroupSnapshotRef is immutable
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshotContent
metadata:
name: new-groupsnapshotcontent-demo
spec:
volumeGroupSnapshotRef:
name: new-groupsnapshot-demo
namespace: default
driver: hostpath.csi.k8s.io
source:
volumeHandles:
- handles
deletionPolicy: Retain
14 changes: 14 additions & 0 deletions client/hack/cel-tests/volumegroupsnapshotcontent/vgsc-ok.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshotContent
metadata:
name: new-groupsnapshotcontent-demo
spec:
volumeGroupSnapshotRef:
name: new-groupsnapshot-demo
namespace: default
driver: hostpath.csi.k8s.io
source:
volumeHandles:
- handles
deletionPolicy: Retain
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshotContent
metadata:
name: new-groupsnapshotcontent-demo
spec:
volumeGroupSnapshotRef:
name: new-groupsnapshot-demo
driver: hostpath.csi.k8s.io
source:
volumeHandles:
- handles
deletionPolicy: Retain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
both volumeGroupSnapshotRef.name and volumeGroupSnapshotRef.namespace must be set
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshotContent
metadata:
name: new-groupsnapshotcontent-demo
spec:
volumeGroupSnapshotRef:
namespace: default
driver: hostpath.csi.k8s.io
source:
volumeHandles:
- handles
deletionPolicy: Retain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
both volumeGroupSnapshotRef.name and volumeGroupSnapshotRef.namespace must be set
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshotContent
metadata:
name: new-groupsnapshotcontent-demo
spec:
volumeGroupSnapshotRef:
name: new-groupsnapshot-demo
namespace: default
driver: hostpath.csi.k8s.io
source:
volumeHandles:
- handles
groupSnapshotHandles:
volumeGroupSnapshotHandle: this-handle
volumeSnapshotHandles:
- handle
- another-handle
deletionPolicy: Retain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exactly one of volumeHandles and groupSnapshotHandles must be set
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshotContent
metadata:
name: new-groupsnapshotcontent-demo
spec:
volumeGroupSnapshotRef:
name: new-groupsnapshot-demo
namespace: default
driver: hostpath.csi.k8s.io
source: {}
deletionPolicy: Retain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exactly one of volumeHandles and groupSnapshotHandles must be set
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
apiVersion: groupsnapshot.storage.k8s.io/v1alpha1
kind: VolumeGroupSnapshotContent
metadata:
name: new-groupsnapshotcontent-demo
spec:
volumeGroupSnapshotRef:
name: new-groupsnapshot-demo
namespace: default
driver: hostpath.csi.k8s.io
source:
groupSnapshotHandles:
volumeGroupSnapshotHandle: this-handle
volumeSnapshotHandles:
- handle
- another-handle
- changed-handle
deletionPolicy: Retain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
groupSnapshotHandles is immutable
Loading

0 comments on commit 35702d9

Please sign in to comment.