From 70b0f99f5ee39bcc65de4cd9b899e982feb470fb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?=
 <charles.edouard@nirmata.com>
Date: Wed, 5 Apr 2023 17:47:01 +0200
Subject: [PATCH] chore: improve a few kuttl tests using shouldFail instead of
 commands (#6791)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
---
 ...pt-check-for-error.yaml => 04-should-fail.yaml} | 13 +++----------
 .../scaffold/03-should-fail.yaml                   |  6 ++++++
 .../scaffold/script-check-for-error.yaml           | 13 -------------
 .../wildcard/block-verifyimage/01-policy.yaml      |  7 +++++++
 .../wildcard/block-verifyimage/01-script.yaml      | 14 --------------
 .../validate/e2e/global-anchor/03-create-bad.yaml  | 13 +++----------
 .../validate/e2e/trusted-images/03-create-bad.yaml | 13 +++----------
 .../validate/e2e/x509-decode/02-bad-configmap.yaml | 13 +++----------
 8 files changed, 25 insertions(+), 67 deletions(-)
 rename test/conformance/kuttl/_aaa_template_resources/{scripts/01-script-check-for-error.yaml => 04-should-fail.yaml} (59%)
 create mode 100644 test/conformance/kuttl/_aaa_template_resources/scaffold/03-should-fail.yaml
 delete mode 100644 test/conformance/kuttl/_aaa_template_resources/scaffold/script-check-for-error.yaml
 create mode 100644 test/conformance/kuttl/validate/clusterpolicy/standard/wildcard/block-verifyimage/01-policy.yaml
 delete mode 100644 test/conformance/kuttl/validate/clusterpolicy/standard/wildcard/block-verifyimage/01-script.yaml

diff --git a/test/conformance/kuttl/_aaa_template_resources/scripts/01-script-check-for-error.yaml b/test/conformance/kuttl/_aaa_template_resources/04-should-fail.yaml
similarity index 59%
rename from test/conformance/kuttl/_aaa_template_resources/scripts/01-script-check-for-error.yaml
rename to test/conformance/kuttl/_aaa_template_resources/04-should-fail.yaml
index 573e2241fb..eeed5d7f34 100644
--- a/test/conformance/kuttl/_aaa_template_resources/scripts/01-script-check-for-error.yaml
+++ b/test/conformance/kuttl/_aaa_template_resources/04-should-fail.yaml
@@ -3,13 +3,6 @@
 ## indicate that creation of a file should fail. See the BEST_PRACTICES.md file for an example. 
 apiVersion: kuttl.dev/v1beta1
 kind: TestStep
-commands:
-- script: |
-    if kubectl apply -f manifests.yaml
-    then 
-      echo "Tested failed. Policy was created when it shouldn't have been."
-      exit 1 
-    else 
-      echo "Test succeeded. Policy was not created as intended."
-      exit 0
-    fi
\ No newline at end of file
+apply:
+- file: manifests.yaml
+  shouldFail: true
diff --git a/test/conformance/kuttl/_aaa_template_resources/scaffold/03-should-fail.yaml b/test/conformance/kuttl/_aaa_template_resources/scaffold/03-should-fail.yaml
new file mode 100644
index 0000000000..5e25f6d97c
--- /dev/null
+++ b/test/conformance/kuttl/_aaa_template_resources/scaffold/03-should-fail.yaml
@@ -0,0 +1,6 @@
+## Checks that the manifests.yaml file CANNOT be successfully created. If it can, fail the test as this is incorrect.
+apiVersion: kuttl.dev/v1beta1
+kind: TestStep
+apply:
+- file: manifests.yaml
+  shouldFail: true
diff --git a/test/conformance/kuttl/_aaa_template_resources/scaffold/script-check-for-error.yaml b/test/conformance/kuttl/_aaa_template_resources/scaffold/script-check-for-error.yaml
deleted file mode 100644
index fc29fa83d3..0000000000
--- a/test/conformance/kuttl/_aaa_template_resources/scaffold/script-check-for-error.yaml
+++ /dev/null
@@ -1,13 +0,0 @@
-## Checks that the manifests.yaml file CANNOT be successfully created. If it can, fail the test as this is incorrect.
-apiVersion: kuttl.dev/v1beta1
-kind: TestStep
-commands:
-- script: |
-    if kubectl apply -f manifests.yaml
-    then 
-      echo "Tested failed. Policy was created when it shouldn't have been."
-      exit 1 
-    else 
-      echo "Test succeeded. Policy was not created as intended."
-      exit 0
-    fi
\ No newline at end of file
diff --git a/test/conformance/kuttl/validate/clusterpolicy/standard/wildcard/block-verifyimage/01-policy.yaml b/test/conformance/kuttl/validate/clusterpolicy/standard/wildcard/block-verifyimage/01-policy.yaml
new file mode 100644
index 0000000000..32f9bb87ba
--- /dev/null
+++ b/test/conformance/kuttl/validate/clusterpolicy/standard/wildcard/block-verifyimage/01-policy.yaml
@@ -0,0 +1,7 @@
+# Checks that the manifests.yaml file CANNOT be successfully created. If it can, fail the test as this is incorrect.
+
+apiVersion: kuttl.dev/v1beta1
+kind: TestStep
+apply:
+- file: policy.yaml
+  shouldFail: true
diff --git a/test/conformance/kuttl/validate/clusterpolicy/standard/wildcard/block-verifyimage/01-script.yaml b/test/conformance/kuttl/validate/clusterpolicy/standard/wildcard/block-verifyimage/01-script.yaml
deleted file mode 100644
index b1fedb4b9d..0000000000
--- a/test/conformance/kuttl/validate/clusterpolicy/standard/wildcard/block-verifyimage/01-script.yaml
+++ /dev/null
@@ -1,14 +0,0 @@
-# Checks that the manifests.yaml file CANNOT be successfully created. If it can, fail the test as this is incorrect.
-
-apiVersion: kuttl.dev/v1beta1
-kind: TestStep
-commands:
-- script: |
-    if kubectl apply -f policy.yaml
-    then 
-      echo "Tested failed. policy was allowed."
-      exit 1 
-    else 
-      echo "Test succeeded. policy was blocked."
-      exit 0
-    fi
\ No newline at end of file
diff --git a/test/conformance/kuttl/validate/e2e/global-anchor/03-create-bad.yaml b/test/conformance/kuttl/validate/e2e/global-anchor/03-create-bad.yaml
index 49ab28f50f..67dbaec049 100644
--- a/test/conformance/kuttl/validate/e2e/global-anchor/03-create-bad.yaml
+++ b/test/conformance/kuttl/validate/e2e/global-anchor/03-create-bad.yaml
@@ -1,13 +1,6 @@
 ## Checks that the manifests.yaml file CANNOT be successfully created. If it can, fail the test as this is incorrect.
 apiVersion: kuttl.dev/v1beta1
 kind: TestStep
-commands:
-- script: |
-    if kubectl apply -f bad.yaml
-    then 
-      echo "Tested failed. Pod was created when it shouldn't have been."
-      exit 1 
-    else 
-      echo "Test succeeded. Pod was not created as intended."
-      exit 0
-    fi
\ No newline at end of file
+apply:
+- file: bad.yaml
+  shouldFail: true
diff --git a/test/conformance/kuttl/validate/e2e/trusted-images/03-create-bad.yaml b/test/conformance/kuttl/validate/e2e/trusted-images/03-create-bad.yaml
index 49ab28f50f..67dbaec049 100644
--- a/test/conformance/kuttl/validate/e2e/trusted-images/03-create-bad.yaml
+++ b/test/conformance/kuttl/validate/e2e/trusted-images/03-create-bad.yaml
@@ -1,13 +1,6 @@
 ## Checks that the manifests.yaml file CANNOT be successfully created. If it can, fail the test as this is incorrect.
 apiVersion: kuttl.dev/v1beta1
 kind: TestStep
-commands:
-- script: |
-    if kubectl apply -f bad.yaml
-    then 
-      echo "Tested failed. Pod was created when it shouldn't have been."
-      exit 1 
-    else 
-      echo "Test succeeded. Pod was not created as intended."
-      exit 0
-    fi
\ No newline at end of file
+apply:
+- file: bad.yaml
+  shouldFail: true
diff --git a/test/conformance/kuttl/validate/e2e/x509-decode/02-bad-configmap.yaml b/test/conformance/kuttl/validate/e2e/x509-decode/02-bad-configmap.yaml
index 76c3073a88..67dbaec049 100644
--- a/test/conformance/kuttl/validate/e2e/x509-decode/02-bad-configmap.yaml
+++ b/test/conformance/kuttl/validate/e2e/x509-decode/02-bad-configmap.yaml
@@ -1,13 +1,6 @@
 ## Checks that the manifests.yaml file CANNOT be successfully created. If it can, fail the test as this is incorrect.
 apiVersion: kuttl.dev/v1beta1
 kind: TestStep
-commands:
-- script: |
-    if kubectl apply -f bad.yaml
-    then 
-      echo "Tested failed. ConfigMap was created when it shouldn't have been."
-      exit 1 
-    else 
-      echo "Test succeeded. ConfigMap was not created as intended."
-      exit 0
-    fi
\ No newline at end of file
+apply:
+- file: bad.yaml
+  shouldFail: true