From 8f2170950edfe03461b864af858e944063e65298 Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Wed, 3 Apr 2024 10:51:38 +0200
Subject: [PATCH] OZG-3328 cleanup helm unittests and add tests to cover each
 line

---
 .../helm/configmap_bindings_type_test.yaml    | 14 +++-
 .../helm/deployment_defaults_labels_test.yaml | 68 +++++++++++++++++++
 .../helm/deployment_defaults_sso_test.yaml    | 19 +-----
 src/test/helm/deployment_resources_test.yaml  |  4 ++
 .../helm/deployment_spring_profile_test.yaml  |  4 --
 src/test/helm/deployment_test.yaml            |  8 +--
 src/test/helm/network_policy_test.yaml        |  2 +-
 src/test/helm/service_test.yaml               | 21 +++---
 8 files changed, 100 insertions(+), 40 deletions(-)
 create mode 100644 src/test/helm/deployment_defaults_labels_test.yaml

diff --git a/src/test/helm/configmap_bindings_type_test.yaml b/src/test/helm/configmap_bindings_type_test.yaml
index c9f2232b..3848f75f 100644
--- a/src/test/helm/configmap_bindings_type_test.yaml
+++ b/src/test/helm/configmap_bindings_type_test.yaml
@@ -22,13 +22,23 @@
 suite: configmap_bindings_type
 release:
   name: administration
+  namespace: sh-helm-test
 templates:
   - templates/configmap_bindings_type.yaml
-set:
-   Release.Namespace: "administration"
+
 tests:
    - it: validate configmap values
      asserts:
+      - isKind:
+          of: ConfigMap
+      - isAPIVersion:
+          of: v1
+      - equal:
+          path: metadata.name
+          value: administration-bindings-type
+      - equal:
+          path: metadata.namespace
+          value: sh-helm-test
       - equal:
          path: data.type
          value: "ca-certificates"
\ No newline at end of file
diff --git a/src/test/helm/deployment_defaults_labels_test.yaml b/src/test/helm/deployment_defaults_labels_test.yaml
new file mode 100644
index 00000000..b01cb297
--- /dev/null
+++ b/src/test/helm/deployment_defaults_labels_test.yaml
@@ -0,0 +1,68 @@
+#
+# Copyright (C) 2024 Das Land Schleswig-Holstein vertreten durch den
+# Ministerpräsidenten des Landes Schleswig-Holstein
+# Staatskanzlei
+# Abteilung Digitalisierung und zentrales IT-Management der Landesregierung
+#
+# Lizenziert unter der EUPL, Version 1.2 oder - sobald
+# diese von der Europäischen Kommission genehmigt wurden -
+# Folgeversionen der EUPL ("Lizenz");
+# Sie dürfen dieses Werk ausschließlich gemäß
+# dieser Lizenz nutzen.
+# Eine Kopie der Lizenz finden Sie hier:
+#
+# https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
+#
+# Sofern nicht durch anwendbare Rechtsvorschriften
+# gefordert oder in schriftlicher Form vereinbart, wird
+# die unter der Lizenz verbreitete Software "so wie sie
+# ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
+# ausdrücklich oder stillschweigend - verbreitet.
+# Die sprachspezifischen Genehmigungen und Beschränkungen
+# unter der Lizenz sind dem Lizenztext zu entnehmen.
+#
+
+suite: test deployment default labels
+release:
+  name: administration
+  namespace: sh-helm-test
+templates:
+  - templates/deployment.yaml
+set:
+  ozgcloud:
+    bundesland: sh
+    bezeichner: helm
+  sso:
+    serverUrl: https://sso.company.local
+
+tests:
+  - it: check metadata.labels
+    asserts:
+      - equal:
+          path: metadata.labels
+          value:        
+            app.kubernetes.io/instance: administration
+            app.kubernetes.io/managed-by: Helm
+            app.kubernetes.io/name: administration
+            app.kubernetes.io/namespace: sh-helm-test
+            helm.sh/chart: administration-0.0.0-MANAGED-BY-JENKINS
+  - it: should set spec.selector.matchLabels
+    asserts:
+      - equal:
+          path: spec.selector.matchLabels
+          value:
+            app.kubernetes.io/name: administration
+            app.kubernetes.io/namespace: sh-helm-test
+
+
+  - it: should have correct deyploment spec.template.metadata.labels
+    asserts:
+      - equal:
+          path: spec.template.metadata.labels
+          value: 
+            app.kubernetes.io/instance: administration
+            app.kubernetes.io/managed-by: Helm
+            app.kubernetes.io/name: administration
+            app.kubernetes.io/namespace: sh-helm-test
+            component: administration
+            helm.sh/chart: administration-0.0.0-MANAGED-BY-JENKINS
\ No newline at end of file
diff --git a/src/test/helm/deployment_defaults_sso_test.yaml b/src/test/helm/deployment_defaults_sso_test.yaml
index 3af4ab33..907a5923 100644
--- a/src/test/helm/deployment_defaults_sso_test.yaml
+++ b/src/test/helm/deployment_defaults_sso_test.yaml
@@ -22,7 +22,7 @@
 # unter der Lizenz sind dem Lizenztext zu entnehmen.
 #
 
-suite: test deployment
+suite: test deployment sso
 release:
   name: administration
   namespace: sh-helm-test
@@ -57,29 +57,12 @@ tests:
             name: ozgcloud_oauth2_auth-server-url
             value: https://sso.company.local
 
-  - it: check realm with long namespace
-    set:
-      ozgcloud:
-        bezeichner: eins-zwei-drei
-        environment: dev
-        bundesland: sh
-    asserts:
-      - isKind:
-          of: Deployment
-      - contains:
-          path: spec.template.spec.containers[0].env
-          content:
-            name: ozgcloud_oauth2_realm
-            value: sh-eins-zwei-drei-dev
-
   - it: check different client name
     set:
       sso:
         keycloak_client:
           client_name: different-client
     asserts:
-      - isKind:
-          of: Deployment
       - contains:
           path: spec.template.spec.containers[0].env
           content:
diff --git a/src/test/helm/deployment_resources_test.yaml b/src/test/helm/deployment_resources_test.yaml
index d37443f2..888f63ff 100644
--- a/src/test/helm/deployment_resources_test.yaml
+++ b/src/test/helm/deployment_resources_test.yaml
@@ -41,3 +41,7 @@ tests:
       - equal:
           path: spec.template.spec.containers[0].resources
           value: { "test": 1 }
+  - it: should not generate resources when values not set
+    asserts:
+      - isEmpty:
+          path: spec.template.spec.containers[0].resources
\ No newline at end of file
diff --git a/src/test/helm/deployment_spring_profile_test.yaml b/src/test/helm/deployment_spring_profile_test.yaml
index f99d9b98..1a9326cb 100644
--- a/src/test/helm/deployment_spring_profile_test.yaml
+++ b/src/test/helm/deployment_spring_profile_test.yaml
@@ -39,8 +39,6 @@ tests:
     set:
       env.overrideSpringProfiles: oc,dev,ea
     asserts:
-      - isKind:
-          of: Deployment
       - contains:
           path: spec.template.spec.containers[0].env
           content:
@@ -48,8 +46,6 @@ tests:
             value: oc,dev,ea
   - it: should generate the spring profiles
     asserts:
-      - isKind:
-          of: Deployment
       - contains:
           path: spec.template.spec.containers[0].env
           content:
diff --git a/src/test/helm/deployment_test.yaml b/src/test/helm/deployment_test.yaml
index 6bd31db1..5d6ec1cf 100644
--- a/src/test/helm/deployment_test.yaml
+++ b/src/test/helm/deployment_test.yaml
@@ -39,6 +39,8 @@ tests:
     asserts:
       - isKind:
           of: Deployment
+      - isAPIVersion:
+          of: apps/v1
       - equal:
           path: metadata.name
           value: "administration"
@@ -77,12 +79,6 @@ tests:
           value: "RollingUpdate"
   - it: should have correct template configuration
     asserts:
-      - equal:
-          path: spec.template.metadata.labels["app.kubernetes.io/instance"]
-          value: "administration"
-      - equal:
-          path: spec.template.metadata.labels.component
-          value: "administration"
       - equal:
           path: spec.template.spec.topologySpreadConstraints[0].maxSkew
           value: 1
diff --git a/src/test/helm/network_policy_test.yaml b/src/test/helm/network_policy_test.yaml
index 48a5811f..5ce2a529 100644
--- a/src/test/helm/network_policy_test.yaml
+++ b/src/test/helm/network_policy_test.yaml
@@ -126,4 +126,4 @@ tests:
         dnsServerNamespace: test-dns-server-namespace
     asserts:
       - hasDocuments:
-          count: 1
+          count: 1
\ No newline at end of file
diff --git a/src/test/helm/service_test.yaml b/src/test/helm/service_test.yaml
index da7cbbbd..c4c6ef15 100644
--- a/src/test/helm/service_test.yaml
+++ b/src/test/helm/service_test.yaml
@@ -37,14 +37,6 @@ tests:
           apiVersion: v1
           name: administration
           namespace: helm-test
-  - it: should have valid metadata values
-    asserts:
-      - equal:
-          path: metadata.labels["helm.sh/chart"]
-          value: administration-0.0.0-helm-test-version
-      - equal:
-          path: metadata.labels.component
-          value: administration-service
   - it: should be of type ClusterIP
     asserts:
       - equal:
@@ -79,4 +71,15 @@ tests:
     asserts:
       - equal:
           path: spec.selector.component
-          value: administration
\ No newline at end of file
+          value: administration
+  - it: should contain default lables and component lables
+    asserts:
+      - equal:
+          path: metadata.labels
+          value:
+            app.kubernetes.io/instance: administration
+            app.kubernetes.io/managed-by: Helm
+            app.kubernetes.io/name: administration
+            app.kubernetes.io/namespace: helm-test
+            component: administration-service
+            helm.sh/chart: administration-0.0.0-helm-test-version
\ No newline at end of file
-- 
GitLab