diff --git a/helm/README.md b/helm/README.md index 22814095..e1defeb6 100644 --- a/helm/README.md +++ b/helm/README.md @@ -46,4 +46,18 @@ helm install bankapp-dev bankapp/ --set namespace=dev-namespace --set bankapp_sv Happy Helming! +## Testing + +Helm unit tests are located in `bankapp/tests/` and use the [helm-unittest](https://github.com/helm-unittest/helm-unittest) plugin. + +### Install the helm-unittest plugin +```bash +helm plugin install https://github.com/helm-unittest/helm-unittest +``` + +### Run tests +```bash +helm unittest helm/bankapp/ +``` + diff --git a/helm/bankapp/Chart.yaml b/helm/bankapp/Chart.yaml index d8c225e2..c200988f 100644 --- a/helm/bankapp/Chart.yaml +++ b/helm/bankapp/Chart.yaml @@ -1,24 +1,6 @@ apiVersion: v2 name: bankapp -description: A Helm chart for Kubernetes - -# A chart can be either an 'application' or a 'library' chart. -# -# Application charts are a collection of templates that can be packaged into versioned archives -# to be deployed. -# -# Library charts provide useful utilities or functions for the chart developer. They're included as -# a dependency of application charts to inject those utilities and functions into the rendering -# pipeline. Library charts do not define any templates and therefore cannot be deployed. -type: application - -# This is the chart version. This version number should be incremented each time you make changes -# to the chart and its templates, including the app version. -# Versions are expected to follow Semantic Versioning (https://semver.org/) +description: A Helm chart for the Spring Boot BankApp with MySQL version: 0.1.0 - -# This is the version number of the application being deployed. This version number should be -# incremented each time you make changes to the application. Versions are not expected to -# follow Semantic Versioning. They should reflect the version the application is using. -# It is recommended to use it with quotes. -appVersion: "1.16.0" \ No newline at end of file +appVersion: "1.0.0" +type: application diff --git a/helm/bankapp/tests/deployment_test.yaml b/helm/bankapp/tests/deployment_test.yaml new file mode 100644 index 00000000..a559689f --- /dev/null +++ b/helm/bankapp/tests/deployment_test.yaml @@ -0,0 +1,208 @@ +suite: Deployment tests +templates: + - templates/deployment.yml +tests: + - it: should be a Deployment + asserts: + - isKind: + of: Deployment + + - it: should set the default name to bankapp + asserts: + - equal: + path: metadata.name + value: bankapp + + - it: should set the default namespace to bankapp-namespace + asserts: + - equal: + path: metadata.namespace + value: bankapp-namespace + + - it: should override namespace when set in values + set: + namespace: custom-namespace + asserts: + - equal: + path: metadata.namespace + value: custom-namespace + + - it: should have app label on metadata + asserts: + - equal: + path: metadata.labels.app + value: bankapp + + - it: should have app label on selector matchLabels + asserts: + - equal: + path: spec.selector.matchLabels.app + value: bankapp + + - it: should have app label on pod template + asserts: + - equal: + path: spec.template.metadata.labels.app + value: bankapp + + - it: should set replicas to 1 + asserts: + - equal: + path: spec.replicas + value: 1 + + - it: should have init container named wait-for-mysql + asserts: + - equal: + path: spec.template.spec.initContainers[0].name + value: wait-for-mysql + + - it: should use busybox 1.28 for init container + asserts: + - equal: + path: spec.template.spec.initContainers[0].image + value: "busybox:1.28" + + - it: should check mysql-0.mysql-headless:3306 in init container command + asserts: + - matchRegex: + path: spec.template.spec.initContainers[0].command[2] + pattern: "mysql-0\\.mysql-headless 3306" + + - it: should use default app image + asserts: + - equal: + path: spec.template.spec.containers[0].image + value: "trainwithshubham/springboot-bankapp:latest" + + - it: should expose container port 8080 + asserts: + - equal: + path: spec.template.spec.containers[0].ports[0].containerPort + value: 8080 + + - it: should get SPRING_DATASOURCE_URL from configMapKeyRef bankapp-config + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: SPRING_DATASOURCE_URL + valueFrom: + configMapKeyRef: + name: bankapp-config + key: SPRING_DATASOURCE_URL + + - it: should get SPRING_DATASOURCE_USERNAME from configMapKeyRef bankapp-config + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: SPRING_DATASOURCE_USERNAME + valueFrom: + configMapKeyRef: + name: bankapp-config + key: SPRING_DATASOURCE_USERNAME + + - it: should get SPRING_DATASOURCE_PASSWORD from secretKeyRef mysql-secret + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: SPRING_DATASOURCE_PASSWORD + valueFrom: + secretKeyRef: + name: mysql-secret + key: SPRING_DATASOURCE_PASSWORD + + - it: should configure liveness probe on /actuator/health port 8080 + asserts: + - equal: + path: spec.template.spec.containers[0].livenessProbe.httpGet.path + value: /actuator/health + - equal: + path: spec.template.spec.containers[0].livenessProbe.httpGet.port + value: 8080 + - equal: + path: spec.template.spec.containers[0].livenessProbe.initialDelaySeconds + value: 30 + - equal: + path: spec.template.spec.containers[0].livenessProbe.periodSeconds + value: 10 + + - it: should configure readiness probe on /actuator/health port 8080 + asserts: + - equal: + path: spec.template.spec.containers[0].readinessProbe.httpGet.path + value: /actuator/health + - equal: + path: spec.template.spec.containers[0].readinessProbe.httpGet.port + value: 8080 + - equal: + path: spec.template.spec.containers[0].readinessProbe.initialDelaySeconds + value: 10 + - equal: + path: spec.template.spec.containers[0].readinessProbe.periodSeconds + value: 5 + + - it: should set default resource requests + asserts: + - equal: + path: spec.template.spec.containers[0].resources.requests.cpu + value: 80m + - equal: + path: spec.template.spec.containers[0].resources.requests.memory + value: 150Mi + + - it: should set default resource limits + asserts: + - equal: + path: spec.template.spec.containers[0].resources.limits.cpu + value: 800m + - equal: + path: spec.template.spec.containers[0].resources.limits.memory + value: 700Mi + + - it: should use custom image when overridden + set: + image.app: myregistry/myapp:v2.0 + asserts: + - equal: + path: spec.template.spec.containers[0].image + value: "myregistry/myapp:v2.0" + + - it: should use custom resource limits when overridden + set: + app_deployment.cpu_req: 100m + app_deployment.cpu_limit: 1000m + app_deployment.mem_req: 256Mi + app_deployment.mem_limit: 1Gi + asserts: + - equal: + path: spec.template.spec.containers[0].resources.requests.cpu + value: 100m + - equal: + path: spec.template.spec.containers[0].resources.requests.memory + value: 256Mi + - equal: + path: spec.template.spec.containers[0].resources.limits.cpu + value: 1000m + - equal: + path: spec.template.spec.containers[0].resources.limits.memory + value: 1Gi + + - it: should use custom deployment name when overridden + set: + app_deployment.name: my-bankapp + asserts: + - equal: + path: metadata.name + value: my-bankapp + - equal: + path: metadata.labels.app + value: my-bankapp + - equal: + path: spec.selector.matchLabels.app + value: my-bankapp + - equal: + path: spec.template.metadata.labels.app + value: my-bankapp diff --git a/helm/bankapp/tests/mysql_statefulset_test.yaml b/helm/bankapp/tests/mysql_statefulset_test.yaml new file mode 100644 index 00000000..615db6d1 --- /dev/null +++ b/helm/bankapp/tests/mysql_statefulset_test.yaml @@ -0,0 +1,164 @@ +suite: MySQL StatefulSet tests +templates: + - templates/mysqlStatefulSet.yml +tests: + - it: should be a StatefulSet + asserts: + - isKind: + of: StatefulSet + + - it: should set the name to mysql + asserts: + - equal: + path: metadata.name + value: mysql + + - it: should set the default namespace to bankapp-namespace + asserts: + - equal: + path: metadata.namespace + value: bankapp-namespace + + - it: should set serviceName to mysql-headless + asserts: + - equal: + path: spec.serviceName + value: mysql-headless + + - it: should set replicas to 1 + asserts: + - equal: + path: spec.replicas + value: 1 + + - it: should have app label on metadata + asserts: + - equal: + path: metadata.labels.app + value: mysql + + - it: should have app label on selector matchLabels + asserts: + - equal: + path: spec.selector.matchLabels.app + value: mysql + + - it: should have app label on pod template + asserts: + - equal: + path: spec.template.metadata.labels.app + value: mysql + + - it: should use default db image mysql:latest + asserts: + - equal: + path: spec.template.spec.containers[0].image + value: "mysql:latest" + + - it: should expose container port 3306 + asserts: + - equal: + path: spec.template.spec.containers[0].ports[0].containerPort + value: 3306 + + - it: should get MYSQL_ROOT_PASSWORD from secretKeyRef mysql-secret + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: MYSQL_ROOT_PASSWORD + valueFrom: + secretKeyRef: + name: mysql-secret + key: MYSQL_ROOT_PASSWORD + + - it: should get MYSQL_DATABASE from configMapKeyRef bankapp-config + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: MYSQL_DATABASE + valueFrom: + configMapKeyRef: + name: bankapp-config + key: MYSQL_DATABASE + + - it: should mount mysql-data volume at /var/lib/mysql + asserts: + - contains: + path: spec.template.spec.containers[0].volumeMounts + content: + name: mysql-data + mountPath: /var/lib/mysql + + - it: should configure liveness probe with mysqladmin ping + asserts: + - equal: + path: spec.template.spec.containers[0].livenessProbe.exec.command + value: + - mysqladmin + - ping + - -h + - localhost + - equal: + path: spec.template.spec.containers[0].livenessProbe.initialDelaySeconds + value: 30 + - equal: + path: spec.template.spec.containers[0].livenessProbe.periodSeconds + value: 10 + + - it: should configure readiness probe with mysqladmin ping + asserts: + - equal: + path: spec.template.spec.containers[0].readinessProbe.exec.command + value: + - mysqladmin + - ping + - -h + - localhost + - equal: + path: spec.template.spec.containers[0].readinessProbe.initialDelaySeconds + value: 10 + - equal: + path: spec.template.spec.containers[0].readinessProbe.periodSeconds + value: 5 + + - it: should have volumeClaimTemplate named mysql-data with ReadWriteOnce and 5Gi + asserts: + - equal: + path: spec.volumeClaimTemplates[0].metadata.name + value: mysql-data + - contains: + path: spec.volumeClaimTemplates[0].spec.accessModes + content: ReadWriteOnce + - equal: + path: spec.volumeClaimTemplates[0].spec.resources.requests.storage + value: 5Gi + + - it: should use custom db_statefulset name when overridden + set: + db_statefulset.name: custom-mysql + asserts: + - equal: + path: metadata.name + value: custom-mysql + - equal: + path: metadata.labels.app + value: custom-mysql + - equal: + path: spec.serviceName + value: custom-mysql-headless + - equal: + path: spec.selector.matchLabels.app + value: custom-mysql + - equal: + path: spec.template.metadata.labels.app + value: custom-mysql + + - it: should use custom storage when overridden + set: + db_statefulset.storage: 10Gi + asserts: + - equal: + path: spec.volumeClaimTemplates[0].spec.resources.requests.storage + value: 10Gi