quintush / helm-unittest

BDD styled unit test framework for Kubernetes Helm charts as a Helm plugin.
MIT License
345 stars 69 forks source link

'hasDocuments' assertion returning 1 while `helm template` returns 2 #168

Closed obsidian33 closed 2 years ago

obsidian33 commented 2 years ago

I just started utilizing this helm plugin and everything was going great until I tried to test logic that creates multiple templates, in my test it is 2. Below are shortened versions of my files. When I run the helm template command I do have 2 deployment documents outputted.

deployment_test.yaml

suite: test deployment
templates:
  - deployment.yaml
release:
  name: microservice
  namespace: app
tests:
  - it: should render 2 depoyments
    set:
      deployments:
      - name: first-deployment
      - name: second-deployment
    asserts:
      - isKind:
          of: Deployment
      - hasDocuments:
          count: 2

deployment.yaml

{{- range $deployment := .Values.deployments }}
  {{- $name := (tpl $deployment.name $) -}}
  {{- $data := dict "name" $name "deployment" $deployment "Release" $.Release "Values" $.Values -}}
  {{ include "libchart.deployment" $data | indent 0 }}
---
{{- end }}

_deployment.yaml

{{/*
Template for Microservices Deployment
*/}}
{{- define "libchart.deployment" -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .name }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "libchart.labels" . | nindent 4 }}
  annotations:
    {{- include "libchart.annotations" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "libchart.selector" . | nindent 6 }}
  revisionHistoryLimit: 2
  template:
    metadata:
      labels:
        {{- include "libchart.selector" . | nindent 8 }}
        aadpodidbinding: {{ .Values.podbinding }}
      annotations:
        rollme: {{ randAlphaNum 5 | quote }}
    spec:
      containers:
      - name: {{ .Release.Name }}
        image: "{{ .Values.image }}:{{ .Values.image.buildNumber }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        ports:
        - containerPort: {{ .Values.image.containerPort }}
{{- end -}}

When running the test I get:

 FAIL  test deployment  Microservices\tests\deployment_test.yaml
        - should render 2 deployments

                - asserts[0] `hasDocuments` fail
                        Template:       Microservices/templates/deployment.yaml
                        Expected documents count to be:
                                2
                        Actual:
                                1
obsidian33 commented 2 years ago

It appears that my issue was due to the dashes used to strip whitespace. I found this Helm lint error issue that prompted me to test various scenarios.

It's strange that the helm lint and template commands did not throw any errors at all but the tests started passing once I adjusted my deployment.yaml:

{{- range $deployment := .Values.deployments }}
  {{- $name := (tpl $deployment.name $) }}
  {{- $data := dict "name" $name "deployment" $deployment "Release" $.Release "Values" $.Values }}
  {{ include "libchart.deployment" $data | nindent 0 }}
---
{{- end }}