helm-unittest icon indicating copy to clipboard operation
helm-unittest copied to clipboard

Library testing

Open tomassatka opened this issue 5 years ago • 5 comments

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 ...

tomassatka avatar Jun 22 '20 08:06 tomassatka

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

quintush avatar Jul 06 '20 21:07 quintush

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

elopezvalecky avatar Jun 23 '21 08:06 elopezvalecky

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

mederel avatar Sep 20 '21 17:09 mederel

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 ?

eshepelyuk avatar Nov 09 '21 06:11 eshepelyuk

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?

obsidian33 avatar Nov 15 '22 21:11 obsidian33