From a6f3a953751fb337a7a433a0576714635e076569 Mon Sep 17 00:00:00 2001 From: Yury Tsarev Date: Sat, 31 Aug 2024 08:52:20 +0200 Subject: [PATCH 1/3] Fix namespaced resources testing * Before this change any namespaced resource testing lead to generation of malformed apply test case that led to uptest failure ``` 2024/08/31 02:05:36 Error: spec.steps[1].try[1].script.content: Invalid value: v1alpha1.Script{Timeout:(*v1.Duration)(nil), Bindings:[]v1alpha1.Binding(nil), Outputs:[]v1alpha1.Output(nil), Env:[]v1alpha1.Binding(nil), Cluster:"", Content:"", SkipLogOutput:false, Check:(*v1alpha1.Any)(nil)}: content must be specified ``` * Reason of the failure is empty `content: |` in case of namespace resources in generated `00-apply.yaml` ``` - script: content: | - name: Assert Status Conditions ``` * This change fixes the generation loop to enable `script` only when we have at least 1 cluster-scoped resource Signed-off-by: Yury Tsarev (cherry picked from commit 5049702551cba1de83599cad7294e7c585c89341) --- internal/templates/00-apply.yaml.tmpl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/internal/templates/00-apply.yaml.tmpl b/internal/templates/00-apply.yaml.tmpl index e5eb05b..063f520 100644 --- a/internal/templates/00-apply.yaml.tmpl +++ b/internal/templates/00-apply.yaml.tmpl @@ -20,13 +20,15 @@ spec: try: - apply: file: {{ .TestCase.TestDirectory }} - - script: - content: | - {{- range $resource := .Resources }} + {{- range $i, $resource := .Resources }} {{- if eq $resource.KindGroup "secret." -}} {{continue}} {{- end -}} {{- if not $resource.Namespace }} + {{- if eq $i 0}} + - script: + content: | + {{- end }} ${KUBECTL} annotate {{ $resource.KindGroup }}/{{ $resource.Name }} upjet.upbound.io/test=true --overwrite {{- end }} {{- end }} @@ -63,4 +65,4 @@ spec: entrypoint: {{ $resource.PostAssertScriptPath }} {{- end }} {{- end }} -{{ end }} \ No newline at end of file +{{ end }} From d9b5c87e0fb00d8bec62a289d97a1fa5995e2b64 Mon Sep 17 00:00:00 2001 From: Yury Tsarev Date: Sat, 31 Aug 2024 09:10:57 +0200 Subject: [PATCH 2/3] Remove new line end of file that broke the unit tests Signed-off-by: Yury Tsarev (cherry picked from commit 312bb2aff22038a862b005121b282175830a0cb2) --- internal/templates/00-apply.yaml.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/templates/00-apply.yaml.tmpl b/internal/templates/00-apply.yaml.tmpl index 063f520..0363754 100644 --- a/internal/templates/00-apply.yaml.tmpl +++ b/internal/templates/00-apply.yaml.tmpl @@ -65,4 +65,4 @@ spec: entrypoint: {{ $resource.PostAssertScriptPath }} {{- end }} {{- end }} -{{ end }} +{{ end }} \ No newline at end of file From 9f095910ff648a26b1ecdfdfc3200baf4bba454f Mon Sep 17 00:00:00 2001 From: Yury Tsarev Date: Sun, 1 Sep 2024 08:56:22 +0200 Subject: [PATCH 3/3] Add unit test case for single Claim Signed-off-by: Yury Tsarev (cherry picked from commit 484d14e02bfaba83cb3e22e10252a15c05d9ceb6) --- internal/templates/renderer_test.go | 71 +++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/internal/templates/renderer_test.go b/internal/templates/renderer_test.go index 64a1354..b98a65b 100644 --- a/internal/templates/renderer_test.go +++ b/internal/templates/renderer_test.go @@ -961,6 +961,77 @@ spec: metadata: name: example-bucket ("status.atProvider.id" == "metadata.annotations.uptest-old-id"): true +`, + }, + }, + }, + "SuccessClaim": { + args: args{ + tc: &config.TestCase{ + Timeout: 10 * time.Minute, + SetupScriptPath: "/tmp/setup.sh", + TeardownScriptPath: "/tmp/teardown.sh", + TestDirectory: "/tmp/test-input.yaml", + SkipUpdate: true, + SkipImport: true, + }, + resources: []config.Resource{ + { + YAML: claimManifest, + APIVersion: "cluster.gcp.platformref.upbound.io/v1alpha1", + Kind: "Cluster", + Name: "test-cluster-claim", + KindGroup: "cluster.gcp.platformref.upbound.io", + Namespace: "upbound-system", + PostAssertScriptPath: "/tmp/claim/post-assert.sh", + PreDeleteScriptPath: "/tmp/claim/pre-delete.sh", + Conditions: []string{"Ready", "Synced"}, + }, + }, + }, + want: want{ + out: map[string]string{ + "00-apply.yaml": `# This file belongs to the resource apply step. +apiVersion: chainsaw.kyverno.io/v1alpha1 +kind: Test +metadata: + name: apply +spec: + timeouts: + apply: 10m0s + assert: 10m0s + exec: 10m0s + steps: + - name: Run Setup Script + description: Setup the test environment by running the setup script. + try: + - command: + entrypoint: /tmp/setup.sh + - name: Apply Resources + description: Apply resources to the cluster. + try: + - apply: + file: /tmp/test-input.yaml + - name: Assert Status Conditions + description: | + Assert applied resources. First, run the pre-assert script if exists. + Then, check the status conditions. Finally run the post-assert script if it + exists. + try: + - assert: + resource: + apiVersion: cluster.gcp.platformref.upbound.io/v1alpha1 + kind: Cluster + metadata: + name: test-cluster-claim + namespace: upbound-system + status: + ((conditions[?type == 'Ready'])[0]): + status: "True" + ((conditions[?type == 'Synced'])[0]): + status: "True" + - command: + entrypoint: /tmp/claim/post-assert.sh `, }, },