Skip to content

Commit d63a8c4

Browse files
authored
Add e2e tests using chainsaw (#590)
* Add e2e tests using chainsaw We attempted to roll our own E2E framework in #290 The []steps{input,outputs} model the DIY framework match is a direct match to the []steps{apply, assert} model of chainsaw. So we can port the tests from the other PR to this as is. The go-test code in the DIY framework would be translate to `chainsaw-test.yaml` for each test scenario (folder) Ported over these tests: * TestAutoscaledDeploymentRGD -> multiple-resource * SimpleDeployment -> simple-deployment * Addressing review comments
1 parent 3b37c6b commit d63a8c4

39 files changed

+1446
-1
lines changed

.github/workflows/e2e-tests.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: E2E Tests
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
e2e-kind:
11+
name: E2E Tests (Using KiND)
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Set up Go
18+
uses: actions/setup-go@v5
19+
with:
20+
go-version-file: 'go.mod'
21+
cache: true
22+
23+
- name: Install kind
24+
run: go install sigs.k8s.io/kind@latest
25+
26+
- name: Build and deploy to kind cluster
27+
run: make deploy-kind
28+
29+
- name: Run e2e tests
30+
run: make test-e2e

Makefile

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,23 @@ KUSTOMIZE ?= $(LOCALBIN)/kustomize
173173
KO ?= $(LOCALBIN)/ko
174174
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
175175
ENVTEST ?= $(LOCALBIN)/setup-envtest
176+
CHAINSAW ?= $(LOCALBIN)/chainsaw
176177

177178
## Tool Versions
178179
KO_VERSION ?= v0.17.1
179180
KUSTOMIZE_VERSION ?= v5.2.1
180181
CONTROLLER_TOOLS_VERSION ?= v0.16.2
182+
CHAINSAW_VERSION ?= v0.2.12
183+
184+
.PHONY: chainsaw
185+
chainsaw: $(CHAINSAW) ## Download chainsaw locally if necessary. If wrong version is installed, it will be removed before downloading.
186+
$(CHAINSAW): $(LOCALBIN)
187+
@if test -x $(LOCALBIN)/chainsaw && ! $(LOCALBIN)/chainsaw version | grep -q $(CHAINSAW_VERSION); then \
188+
echo "$(LOCALBIN)/chainsaw version is not expected $(CHAINSAW_VERSION). Removing it before installing."; \
189+
rm -rf $(LOCALBIN)/chainsaw; \
190+
fi
191+
test -s $(LOCALBIN)/chainsaw || GOBIN=$(LOCALBIN) GO111MODULE=on go install github.com/kyverno/chainsaw@$(CHAINSAW_VERSION)
192+
181193

182194
.PHONY: ko
183195
ko: $(KO) ## Download ko locally if necessary. If wrong version is installed, it will be removed before downloading.
@@ -268,4 +280,14 @@ ko-apply: ko
268280
cli:
269281
go build -o bin/kro cmd/kro/main.go
270282
sudo mv bin/kro /usr/local/bin
271-
@echo "CLI built successfully"
283+
@echo "CLI built successfully"
284+
285+
##@ E2E Tests
286+
287+
.PHONY: test-e2e
288+
test-e2e: chainsaw ## Run e2e tests
289+
$(CHAINSAW) test ./test/e2e/chainsaw
290+
291+
.PHONY: test-e2e-kind
292+
test-e2e-kind: deploy-kind
293+
make test-e2e
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
apiVersion: chainsaw.kyverno.io/v1alpha1
2+
kind: Test
3+
metadata:
4+
name: check-instance-creation
5+
spec:
6+
description: |
7+
Tests if a creating an instance creates a deployment
8+
steps:
9+
- name: install-rgd
10+
try:
11+
#description: Install the RGD that creates an Instance CRD
12+
- apply:
13+
file: rgd.yaml
14+
description: Apply RGD
15+
- assert:
16+
file: rgd-assert.yaml
17+
description: Verify RGD state
18+
- assert:
19+
file: instancecrd-assert.yaml
20+
description: Verify Instance CRD state
21+
catch:
22+
- description: kro controller pod logs collector
23+
podLogs:
24+
selector: app.kubernetes.io/instance=kro
25+
namespace: kro-system
26+
finally:
27+
- description: sleep operation
28+
sleep:
29+
duration: 5s
30+
- name: create-instance
31+
try:
32+
#description: Create instance
33+
- apply:
34+
file: instance.yaml
35+
description: Create SimpleDeployment Instance
36+
- assert:
37+
file: instance-create-assert.yaml
38+
description: Verify Instance state
39+
- assert:
40+
file: deployment-assert.yaml
41+
description: Verify Deployment state
42+
catch:
43+
- description: kro controller pod logs collector
44+
podLogs:
45+
selector: app.kubernetes.io/instance=kro
46+
namespace: kro-system
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: test-instance
5+
spec:
6+
replicas: 2
7+
selector:
8+
matchLabels:
9+
app: test-instance
10+
template:
11+
metadata:
12+
labels:
13+
app: test-instance
14+
spec:
15+
containers:
16+
- image: nginx:1.25
17+
name: main
18+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: kro.run/v1alpha1
2+
kind: CheckInstanceCreationSimpleDeployment
3+
metadata:
4+
finalizers:
5+
- kro.run/finalizer
6+
generation: 1
7+
labels:
8+
kro.run/owned: "true"
9+
name: test-instance
10+
spec:
11+
image: nginx:1.25
12+
replicas: 2
13+
status:
14+
conditions:
15+
- message: Instance reconciled successfully
16+
observedGeneration: 1
17+
reason: ReconciliationSucceeded
18+
status: "True"
19+
type: InstanceSynced
20+
state: ACTIVE
21+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: kro.run/v1alpha1
2+
kind: CheckInstanceCreationSimpleDeployment
3+
metadata:
4+
name: test-instance
5+
spec:
6+
replicas: 2
7+
image: nginx:1.25
8+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
apiVersion: apiextensions.k8s.io/v1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
generation: 1
5+
name: checkinstancecreationsimpledeployments.kro.run
6+
spec:
7+
conversion:
8+
strategy: None
9+
group: kro.run
10+
names:
11+
kind: CheckInstanceCreationSimpleDeployment
12+
listKind: CheckInstanceCreationSimpleDeploymentList
13+
plural: checkinstancecreationsimpledeployments
14+
singular: checkinstancecreationsimpledeployment
15+
scope: Namespaced
16+
versions:
17+
- additionalPrinterColumns:
18+
- description: The state of a ResourceGraphDefinition instance
19+
jsonPath: .status.state
20+
name: State
21+
type: string
22+
- description: Whether a ResourceGraphDefinition instance have all it's subresources
23+
ready
24+
jsonPath: .status.conditions[?(@.type=="InstanceSynced")].status
25+
name: Synced
26+
type: string
27+
- description: Age of the resource
28+
jsonPath: .metadata.creationTimestamp
29+
name: Age
30+
type: date
31+
name: v1alpha1
32+
schema:
33+
openAPIV3Schema:
34+
properties:
35+
apiVersion:
36+
type: string
37+
kind:
38+
type: string
39+
metadata:
40+
type: object
41+
spec:
42+
properties:
43+
image:
44+
type: string
45+
replicas:
46+
type: integer
47+
type: object
48+
status:
49+
properties:
50+
conditions:
51+
items:
52+
properties:
53+
lastTransitionTime:
54+
type: string
55+
message:
56+
type: string
57+
observedGeneration:
58+
type: integer
59+
reason:
60+
type: string
61+
status:
62+
type: string
63+
type:
64+
type: string
65+
type: object
66+
type: array
67+
state:
68+
type: string
69+
type: object
70+
type: object
71+
served: true
72+
storage: true
73+
subresources:
74+
status: {}
75+
status:
76+
acceptedNames:
77+
kind: CheckInstanceCreationSimpleDeployment
78+
listKind: CheckInstanceCreationSimpleDeploymentList
79+
plural: checkinstancecreationsimpledeployments
80+
singular: checkinstancecreationsimpledeployment
81+
conditions:
82+
- message: no conflicts found
83+
reason: NoConflicts
84+
status: "True"
85+
type: NamesAccepted
86+
- message: the initial names have been accepted
87+
reason: InitialNamesAccepted
88+
status: "True"
89+
type: Established
90+
storedVersions:
91+
- v1alpha1
92+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apiVersion: kro.run/v1alpha1
2+
kind: ResourceGraphDefinition
3+
metadata:
4+
name: check-instance-creation
5+
finalizers:
6+
- kro.run/finalizer
7+
generation: 1
8+
status:
9+
conditions:
10+
- message: micro controller is ready
11+
reason: ""
12+
status: "True"
13+
type: ReconcilerReady
14+
- message: Directed Acyclic Graph is synced
15+
reason: ""
16+
status: "True"
17+
type: GraphVerified
18+
- message: Custom Resource Definition is synced
19+
reason: ""
20+
status: "True"
21+
type: CustomResourceDefinitionSynced
22+
state: Active
23+
topologicalOrder:
24+
- deployment
25+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
apiVersion: kro.run/v1alpha1
2+
kind: ResourceGraphDefinition
3+
metadata:
4+
name: check-instance-creation
5+
spec:
6+
schema:
7+
apiVersion: v1alpha1
8+
kind: CheckInstanceCreationSimpleDeployment
9+
spec:
10+
replicas: integer | default=1
11+
image: string | default="nginx:latest"
12+
resources:
13+
- id: deployment
14+
template:
15+
apiVersion: apps/v1
16+
kind: Deployment
17+
metadata:
18+
name: ${schema.metadata.name}
19+
spec:
20+
replicas: ${schema.spec.replicas}
21+
selector:
22+
matchLabels:
23+
app: ${schema.metadata.name}
24+
template:
25+
metadata:
26+
labels:
27+
app: ${schema.metadata.name}
28+
spec:
29+
containers:
30+
- name: main
31+
image: ${schema.spec.image}
32+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
apiVersion: chainsaw.kyverno.io/v1alpha1
2+
kind: Test
3+
metadata:
4+
name: check-instance-update
5+
spec:
6+
description: |
7+
Tests if a updating an instance updates the deployment
8+
steps:
9+
- name: install-rgd
10+
try:
11+
#description: Install the RGD that creates an Instance CRD
12+
- apply:
13+
file: rgd.yaml
14+
description: Apply RGD
15+
- assert:
16+
file: rgd-assert.yaml
17+
description: Verify RGD state
18+
- assert:
19+
file: instancecrd-assert.yaml
20+
description: Verify Instance CRD state
21+
catch:
22+
- description: kro controller pod logs collector
23+
podLogs:
24+
selector: app.kubernetes.io/instance=kro
25+
namespace: kro-system
26+
finally:
27+
- description: sleep operation
28+
sleep:
29+
duration: 5s
30+
- name: create-instance
31+
try:
32+
#description: Create instance
33+
- apply:
34+
file: instance.yaml
35+
description: Create SimpleDeployment Instance
36+
- assert:
37+
file: instance-create-assert.yaml
38+
description: Verify Instance state
39+
- assert:
40+
file: deployment-assert.yaml
41+
description: Verify Deployment state
42+
catch:
43+
- description: kro controller pod logs collector
44+
podLogs:
45+
selector: app.kubernetes.io/instance=kro
46+
namespace: kro-system
47+
finally:
48+
- description: sleep operation
49+
sleep:
50+
duration: 5s
51+
- name: update-instance
52+
try:
53+
#description: Update instance SimpleDeployment with replicas=3
54+
- apply:
55+
file: instance-update.yaml
56+
description: Update Instance
57+
- assert:
58+
file: deployment-update-assert.yaml
59+
description: Verify Deployment state
60+
catch:
61+
- description: kro controller pod logs collector
62+
podLogs:
63+
selector: app.kubernetes.io/instance=kro
64+
namespace: kro-system
65+
66+

0 commit comments

Comments
 (0)