Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude some keys during snapshot testing #197

Open
denisbrainyyack opened this issue Aug 19, 2023 · 4 comments
Open

Exclude some keys during snapshot testing #197

denisbrainyyack opened this issue Aug 19, 2023 · 4 comments
Labels

Comments

@denisbrainyyack
Copy link

denisbrainyyack commented Aug 19, 2023

During the snapshot testing, we want to ignore some document keys that are important during the deployment of the chart, but are not important during development. For example, the key helm.sh/chart , which is updated every time we update the version field in Chart.yaml.

It would be great if it were possible to specify such a configuration:

templates:
  - templates/serviceaccount.yaml
tests:
  - it: Manifests should match snapshot
    asserts:
      - matchSnapshot:
          ignoreKeys:
            - "helm.sh/chart"

Expecting that in this case the snapshot would not contain keys with that name.

Manifests should match snapshot:
  1: |
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      labels:
        app.kubernetes.io/instance: RELEASE-NAME
        app.kubernetes.io/managed-by: Helm
        app.kubernetes.io/name: helm-unittest-snapshot-testing
        app.kubernetes.io/version: 1.16.0
        # Commented-out lines should not be generated when using the `ignoreKeys` settings.
        # helm.sh/chart: helm-unittest-snapshot-testing-0.1.0
      name: RELEASE-NAME-helm-unittest-snapshot-testing
@quintush
Copy link
Contributor

Hello @denisbrainyyack,

I will look into this.
With the current set it is possible to set a path to generate the snapshot.
With the path it is not required to validate the whole output.

Greetings,
@quintush

@denisbrainyyack
Copy link
Author

Hello @quintush,

I'll add some context about our case.

We have a library chart and several application charts that we build based on the library chart. When developing a library chart, we do not use snapshot testing, but for application charts we use snapshot testing to control changes in default values, dependency versions, etc. Some application charts may consist of several sub-charts and that in total gives more than 50 documents.

It looks like using the path key is not suitable in this case.

I would like to take this opportunity to thank you for your great work!

@michael-todorovic
Copy link

Until we can have this feat, I did a small workaround in _helpers.tpl

{{- define "mychart.version" -}}
{{- printf "%s-%s" .Chart.Name (.Values.unittestChartVersion | default .Chart.Version) | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

and then I use it in my annotation:

helm.sh/chart: {{ include "mychart.version" . }}

Finally, in the values I give to unittest, I have

# this is only to make snapshot work
unittestChartVersion: 0.0.1-unittest

@begemotik
Copy link

Alternatively, you might simply implement some "switch variable", for instance .Values.unitTest == false (by default). If set to true it should prevent rendering dynamic entities like app.kubernetes.io/version, checksum/config, checksum/secret, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants