From 02720d32dd2cf9e4de323b6d8dc59844a52e782c Mon Sep 17 00:00:00 2001 From: Vlad Klokun Date: Wed, 11 Jan 2023 18:49:16 +0200 Subject: [PATCH] tests: extend test cases for autofix inserts This change re-organizes the test cases for inserts performed by the autofixing feature. --- core/pkg/fixhandler/fixhandler_test.go | 29 ++++++++++++++----- ...c-01-00-input-mapping-insert-mapping.yaml} | 0 ...put-mapping-insert-mapping-with-list.yaml} | 0 ...=> tc-03-00-input-list-append-scalar.yaml} | 0 ...l => tc-04-00-input-multiple-inserts.yaml} | 0 ...put-comment-blank-line-single-insert.yaml} | 0 ...-00-input-list-append-scalar-oneline.yaml} | 0 ...=> tc-07-00-input-multiple-documents.yaml} | 0 ...input-mapping-insert-mapping-indented.yaml | 11 +++++++ .../testdata/inserts/tc-08-01-expected.yaml | 15 ++++++++++ ...nput-list-insert-new-mapping-indented.yaml | 11 +++++++ .../testdata/inserts/tc-09-01-expected.yaml | 13 +++++++++ ...c-10-00-input-list-insert-new-mapping.yaml | 11 +++++++ .../testdata/inserts/tc-10-01-expected.yaml | 13 +++++++++ 14 files changed, 96 insertions(+), 7 deletions(-) rename core/pkg/fixhandler/testdata/inserts/{tc-01-00-input.yaml => tc-01-00-input-mapping-insert-mapping.yaml} (100%) rename core/pkg/fixhandler/testdata/inserts/{tc-02-00-input.yaml => tc-02-00-input-mapping-insert-mapping-with-list.yaml} (100%) rename core/pkg/fixhandler/testdata/inserts/{tc-03-00-input.yaml => tc-03-00-input-list-append-scalar.yaml} (100%) rename core/pkg/fixhandler/testdata/inserts/{tc-04-00-input.yaml => tc-04-00-input-multiple-inserts.yaml} (100%) rename core/pkg/fixhandler/testdata/inserts/{tc-05-00-input.yaml => tc-05-00-input-comment-blank-line-single-insert.yaml} (100%) rename core/pkg/fixhandler/testdata/inserts/{tc-06-00-input.yaml => tc-06-00-input-list-append-scalar-oneline.yaml} (100%) rename core/pkg/fixhandler/testdata/inserts/{tc-07-00-input.yaml => tc-07-00-input-multiple-documents.yaml} (100%) create mode 100644 core/pkg/fixhandler/testdata/inserts/tc-08-00-input-mapping-insert-mapping-indented.yaml create mode 100644 core/pkg/fixhandler/testdata/inserts/tc-08-01-expected.yaml create mode 100644 core/pkg/fixhandler/testdata/inserts/tc-09-00-input-list-insert-new-mapping-indented.yaml create mode 100644 core/pkg/fixhandler/testdata/inserts/tc-09-01-expected.yaml create mode 100644 core/pkg/fixhandler/testdata/inserts/tc-10-00-input-list-insert-new-mapping.yaml create mode 100644 core/pkg/fixhandler/testdata/inserts/tc-10-01-expected.yaml diff --git a/core/pkg/fixhandler/fixhandler_test.go b/core/pkg/fixhandler/fixhandler_test.go index 4b263ef1..44a26b23 100644 --- a/core/pkg/fixhandler/fixhandler_test.go +++ b/core/pkg/fixhandler/fixhandler_test.go @@ -40,22 +40,22 @@ func getTestCases() []indentationTestCase { indentationTestCases := []indentationTestCase{ // Insertion Scenarios { - "inserts/tc-01-00-input.yaml", + "inserts/tc-01-00-input-mapping-insert-mapping.yaml", "select(di==0).spec.containers[0].securityContext.allowPrivilegeEscalation |= false", "inserts/tc-01-01-expected.yaml", }, { - "inserts/tc-02-00-input.yaml", + "inserts/tc-02-00-input-mapping-insert-mapping-with-list.yaml", "select(di==0).spec.containers[0].securityContext.capabilities.drop += [\"NET_RAW\"]", "inserts/tc-02-01-expected.yaml", }, { - "inserts/tc-03-00-input.yaml", + "inserts/tc-03-00-input-list-append-scalar.yaml", "select(di==0).spec.containers[0].securityContext.capabilities.drop += [\"SYS_ADM\"]", "inserts/tc-03-01-expected.yaml", }, { - "inserts/tc-04-00-input.yaml", + "inserts/tc-04-00-input-multiple-inserts.yaml", `select(di==0).spec.template.spec.securityContext.allowPrivilegeEscalation |= false | select(di==0).spec.template.spec.containers[0].securityContext.capabilities.drop += ["NET_RAW"] | @@ -66,23 +66,38 @@ func getTestCases() []indentationTestCase { "inserts/tc-04-01-expected.yaml", }, { - "inserts/tc-05-00-input.yaml", + "inserts/tc-05-00-input-comment-blank-line-single-insert.yaml", "select(di==0).spec.containers[0].securityContext.allowPrivilegeEscalation |= false", "inserts/tc-05-01-expected.yaml", }, { - "inserts/tc-06-00-input.yaml", + "inserts/tc-06-00-input-list-append-scalar-oneline.yaml", "select(di==0).spec.containers[0].securityContext.capabilities.drop += [\"SYS_ADM\"]", "inserts/tc-06-01-expected.yaml", }, { - "inserts/tc-07-00-input.yaml", + "inserts/tc-07-00-input-multiple-documents.yaml", `select(di==0).spec.containers[0].securityContext.allowPrivilegeEscalation |= false | select(di==1).spec.containers[0].securityContext.allowPrivilegeEscalation |= false`, "inserts/tc-07-01-expected.yaml", }, + { + "inserts/tc-08-00-input-mapping-insert-mapping-indented.yaml", + "select(di==0).spec.containers[0].securityContext.capabilities.drop += [\"NET_RAW\"]", + "inserts/tc-08-01-expected.yaml", + }, + { + "inserts/tc-09-00-input-list-insert-new-mapping-indented.yaml", + `select(di==0).spec.containers += {"name": "redis", "image": "redis"}`, + "inserts/tc-09-01-expected.yaml", + }, + { + "inserts/tc-10-00-input-list-insert-new-mapping.yaml", + `select(di==0).spec.containers += {"name": "redis", "image": "redis"}`, + "inserts/tc-10-01-expected.yaml", + }, // Removal Scenarios { diff --git a/core/pkg/fixhandler/testdata/inserts/tc-01-00-input.yaml b/core/pkg/fixhandler/testdata/inserts/tc-01-00-input-mapping-insert-mapping.yaml similarity index 100% rename from core/pkg/fixhandler/testdata/inserts/tc-01-00-input.yaml rename to core/pkg/fixhandler/testdata/inserts/tc-01-00-input-mapping-insert-mapping.yaml diff --git a/core/pkg/fixhandler/testdata/inserts/tc-02-00-input.yaml b/core/pkg/fixhandler/testdata/inserts/tc-02-00-input-mapping-insert-mapping-with-list.yaml similarity index 100% rename from core/pkg/fixhandler/testdata/inserts/tc-02-00-input.yaml rename to core/pkg/fixhandler/testdata/inserts/tc-02-00-input-mapping-insert-mapping-with-list.yaml diff --git a/core/pkg/fixhandler/testdata/inserts/tc-03-00-input.yaml b/core/pkg/fixhandler/testdata/inserts/tc-03-00-input-list-append-scalar.yaml similarity index 100% rename from core/pkg/fixhandler/testdata/inserts/tc-03-00-input.yaml rename to core/pkg/fixhandler/testdata/inserts/tc-03-00-input-list-append-scalar.yaml diff --git a/core/pkg/fixhandler/testdata/inserts/tc-04-00-input.yaml b/core/pkg/fixhandler/testdata/inserts/tc-04-00-input-multiple-inserts.yaml similarity index 100% rename from core/pkg/fixhandler/testdata/inserts/tc-04-00-input.yaml rename to core/pkg/fixhandler/testdata/inserts/tc-04-00-input-multiple-inserts.yaml diff --git a/core/pkg/fixhandler/testdata/inserts/tc-05-00-input.yaml b/core/pkg/fixhandler/testdata/inserts/tc-05-00-input-comment-blank-line-single-insert.yaml similarity index 100% rename from core/pkg/fixhandler/testdata/inserts/tc-05-00-input.yaml rename to core/pkg/fixhandler/testdata/inserts/tc-05-00-input-comment-blank-line-single-insert.yaml diff --git a/core/pkg/fixhandler/testdata/inserts/tc-06-00-input.yaml b/core/pkg/fixhandler/testdata/inserts/tc-06-00-input-list-append-scalar-oneline.yaml similarity index 100% rename from core/pkg/fixhandler/testdata/inserts/tc-06-00-input.yaml rename to core/pkg/fixhandler/testdata/inserts/tc-06-00-input-list-append-scalar-oneline.yaml diff --git a/core/pkg/fixhandler/testdata/inserts/tc-07-00-input.yaml b/core/pkg/fixhandler/testdata/inserts/tc-07-00-input-multiple-documents.yaml similarity index 100% rename from core/pkg/fixhandler/testdata/inserts/tc-07-00-input.yaml rename to core/pkg/fixhandler/testdata/inserts/tc-07-00-input-multiple-documents.yaml diff --git a/core/pkg/fixhandler/testdata/inserts/tc-08-00-input-mapping-insert-mapping-indented.yaml b/core/pkg/fixhandler/testdata/inserts/tc-08-00-input-mapping-insert-mapping-indented.yaml new file mode 100644 index 00000000..ac27db64 --- /dev/null +++ b/core/pkg/fixhandler/testdata/inserts/tc-08-00-input-mapping-insert-mapping-indented.yaml @@ -0,0 +1,11 @@ +# Fix to Apply: +# select(di==0).spec.containers[0].securityContext.capabilities.drop += ["NET_RAW"] + +apiVersion: v1 +kind: Pod +metadata: + name: indented-parent-list-insert-list-value +spec: + containers: + - name: nginx_container + image: nginx diff --git a/core/pkg/fixhandler/testdata/inserts/tc-08-01-expected.yaml b/core/pkg/fixhandler/testdata/inserts/tc-08-01-expected.yaml new file mode 100644 index 00000000..46a3acbe --- /dev/null +++ b/core/pkg/fixhandler/testdata/inserts/tc-08-01-expected.yaml @@ -0,0 +1,15 @@ +# Fix to Apply: +# select(di==0).spec.containers[0].securityContext.capabilities.drop += ["NET_RAW"] + +apiVersion: v1 +kind: Pod +metadata: + name: indented-parent-list-insert-list-value +spec: + containers: + - name: nginx_container + image: nginx + securityContext: + capabilities: + drop: + - NET_RAW diff --git a/core/pkg/fixhandler/testdata/inserts/tc-09-00-input-list-insert-new-mapping-indented.yaml b/core/pkg/fixhandler/testdata/inserts/tc-09-00-input-list-insert-new-mapping-indented.yaml new file mode 100644 index 00000000..416c0c1a --- /dev/null +++ b/core/pkg/fixhandler/testdata/inserts/tc-09-00-input-list-insert-new-mapping-indented.yaml @@ -0,0 +1,11 @@ +# Fix to Apply: +# select(di==0).spec.containers += {"name": "redis", "image": "redis"} + +apiVersion: v1 +kind: Pod +metadata: + name: indented-parent-list-insert-list-value +spec: + containers: + - name: nginx_container + image: nginx diff --git a/core/pkg/fixhandler/testdata/inserts/tc-09-01-expected.yaml b/core/pkg/fixhandler/testdata/inserts/tc-09-01-expected.yaml new file mode 100644 index 00000000..61ce65f7 --- /dev/null +++ b/core/pkg/fixhandler/testdata/inserts/tc-09-01-expected.yaml @@ -0,0 +1,13 @@ +# Fix to Apply: +# select(di==0).spec.containers += {"name": "redis", "image": "redis"} + +apiVersion: v1 +kind: Pod +metadata: + name: indented-parent-list-insert-list-value +spec: + containers: + - name: nginx_container + image: nginx + - name: redis + image: redis diff --git a/core/pkg/fixhandler/testdata/inserts/tc-10-00-input-list-insert-new-mapping.yaml b/core/pkg/fixhandler/testdata/inserts/tc-10-00-input-list-insert-new-mapping.yaml new file mode 100644 index 00000000..827cad1d --- /dev/null +++ b/core/pkg/fixhandler/testdata/inserts/tc-10-00-input-list-insert-new-mapping.yaml @@ -0,0 +1,11 @@ +# Fix to Apply: +# select(di==0).spec.containers += {"name": "redis", "image": "redis"} + +apiVersion: v1 +kind: Pod +metadata: + name: indented-list-insert-new-object +spec: + containers: + - name: nginx_container + image: nginx diff --git a/core/pkg/fixhandler/testdata/inserts/tc-10-01-expected.yaml b/core/pkg/fixhandler/testdata/inserts/tc-10-01-expected.yaml new file mode 100644 index 00000000..a46f6b75 --- /dev/null +++ b/core/pkg/fixhandler/testdata/inserts/tc-10-01-expected.yaml @@ -0,0 +1,13 @@ +# Fix to Apply: +# select(di==0).spec.containers += {"name": "redis", "image": "redis"} + +apiVersion: v1 +kind: Pod +metadata: + name: indented-list-insert-new-object +spec: + containers: + - name: nginx_container + image: nginx + - name: redis + image: redis