quintush / helm-unittest

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

Library testing #34

Open tomassatka opened 4 years ago

tomassatka commented 4 years ago

Hi Is it possible to write unittests for library chart? I know that user cant apply helm template @chart on library chart therefore not sure if it can be unit tested. Motivation behind is that libraries play crucial role as they contain common _helpers.tpl and therefore we would like to focus on unit testing it to assure the quality and compatibility with charts that depend on it.

Any advice?

Example of tpl from library templates:

{{- define "common.service.tpl" -}}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "common.name" .Root }}
  labels:
    {{- include "common.labels" .Root | nindent 4 }}
spec:
  selector:
    {{- include "common.selectorLabels" .Root | nindent 4 }}
  type: {{ .Values.type }}
  ports:
    {{- range .Values.ports }}
    - port: {{ .port }}
      protocol: {{ .protocol | default "TCP" }}
      {{- if .targetPort }}
      targetPort: {{ .targetPort }} 
      {{- end }}
      {{- if .name }}
      name: {{ .name | toString | quote }} 
      {{- end }}
    {{- end }}
{{- end -}}

and test it with:

suite: test service
templates:
  - _service.tpl
tests:
  - it: should work
    asserts:
      - isKind:
          of: Service
   ... more asserts ...
quintush commented 4 years ago

Library templates don't generate any input, so it is nog possible to unittest.

I have looked into it by parsing the script itself, bit this resulted in writing almost a complete copy of the actusl template parser.

Greeting @quintush

elopezvalecky commented 3 years ago

Maybe is better to call the plugin integrationtest instead of unittest... then less confusion and more precises .

mederel commented 2 years ago

could write another chart on the side that uses the library chart and put the tests there

eshepelyuk commented 2 years ago

The library chart can only be tested on a fake chart that uses the library. But this project could simplify a developers' lives by hiding this, i.e. detecting that current chart is a library and creating a fake chart behind the scene, with the library chart as dependency. Wdyt ?

obsidian33 commented 1 year ago

The library chart can only be tested on a fake chart that uses the library. But this project could simplify a developers' lives by hiding this, i.e. detecting that current chart is a library and creating a fake chart behind the scene, with the library chart as dependency. Wdyt ?

I like this idea. Is this something we can get in a future release?