Code Monkey home page Code Monkey logo

helm3-unittest's Introduction

Unit Test plugin for Helm 3

Release Status Latest Release

This is a fork of https://github.com/lrills/helm-unittest for Helm 3.

Documentation

If you are ready for writing tests, check the DOCUMENT for the test API in YAML.

Install

$ helm plugin install https://github.com/vbehar/helm3-unittest

It will install the latest version of binary into helm plugin directory.

Get Started

Add tests in .helmignore of your chart, and create the following test file at $YOUR_CHART/tests/deployment_test.yaml:

suite: test deployment
templates:
  - deployment.yaml
tests:
  - it: should work
    set:
      image.tag: latest
    asserts:
      - isKind:
          of: Deployment
      - matchRegex:
          path: metadata.name
          pattern: -my-chart$
      - equal:
          path: spec.template.spec.containers[0].image
          value: nginx:latest

and run:

$ helm unittest $YOUR_CHART

Now there is your first test! ;)

Test Suite File

The test suite file is written in pure YAML, and default placed under the tests/ directory of the chart with suffix _test.yaml. You can also have your own suite files arrangement with -f, --file option of cli set as the glob patterns of test suite files related to chart directory, like:

$ helm unittest -f 'my-tests/*.yaml' -f 'more-tests/*.yaml' my-chart

Check DOCUMENT for more details about writing tests.

Usage

$ helm unittest [flags] CHART [...]

This renders your charts locally (without tiller) and runs tests defined in test suite files.

Flags

--color              enforce printing colored output even stdout is not a tty. Set to false to disable color
-f, --file stringArray   glob paths of test files location, default to tests/*_test.yaml (default [tests/*_test.yaml])
-h, --help               help for unittest
-u, --update-snapshot    update the snapshot cached if needed, make sure you review the change before update

Example

Check __fixtures__/basic/ for some basic use cases of a simple chart.

Snapshot Testing

Sometimes you may just want to keep the rendered manifest not changed between changes without every details asserted. That's the reason for snapshot testing! Check the tests below:

templates:
  - deployment.yaml
tests:
  - it: pod spec should match snapshot
    asserts:
      - matchSnapshot:
          path: spec.template.spec
  # or you can snapshot the whole manifest
  - it: manifest should match snapshot
    asserts:
      - matchSnapshot: {}

The matchSnapshot assertion validate the content rendered the same as cached last time. It fails if the content changed, and you should check and update the cache with -u, --update-snapshot option of cli.

$ helm unittest -u my-chart

The cache files is stored as __snapshot__/*_test.yaml.snap at the directory your test file placed, you should add them in version control with your chart.

Tests within subchart

If you have customized subchart (not installed via helm dependency) existed in charts directory, tests inside would also be executed by default. You can disable this behavior by setting --with-subchart=false flag in cli, thus only the tests in root chart will be executed. Notice that the values defined in subchart tests will be automatically scoped, you don't have to add dependency scope yourself:

# parent-chart/charts/child-chart/tests/xxx_test.yaml
templates:
  - xxx.yaml
tests:
  - it:
    set:
      # no need to prefix with "child-chart."
      somevalue: should_be_scoped
    asserts:
      - ...

Check __fixtures__/with-subchart/ as an example.

Related Projects / Commands

This plugin is inspired by helm-template, and the idea of snapshot testing and some printing format comes from jest.

And there are some other helm commands you might want to use:

  • helm template: render the chart and print the output.

  • helm lint: examines a chart for possible issues, useful to validate chart dependencies.

  • helm test: test a release with testing pod defined in chart. Note this does create resources on your cluster to verify if your release is correct. Check the doc.

License

MIT

Contributing

Issues and PRs are welcome!
Before start developing this plugin, you must have go and dep installed, and run:

git clone [email protected]:lrills/helm-unittest.git
cd helm-unittest
dep ensure

And please make CI passed when request a PR which would check following things:

  • dep status passed. Make sure you run dep ensure if new dependencies added.
  • gofmt no changes needed. Please run gofmt -w -s before you commit.
  • go test ./unittest/... passed.

In some cases you might need to manually fix the tests in *_test.go. If the snapshot tests (of the plugin's test code) failed you need to run:

UPDATE_SNAPSHOTS=true go test ./unittest/...

This update the snapshot cache file and please add them before you commit.

helm3-unittest's People

Contributors

anthony-pastor avatar floretan avatar kenji-fukasawa avatar lrills avatar matiasanaya avatar matthope avatar r0fls avatar spremel avatar tony-clarke-amdocs avatar vbehar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

helm3-unittest's Issues

Ability to check correctness of values against Chart JSON schema

Hello,

Is there a way to check that the JSON Schema is applied correctly by Helm and the charts ?

The idea is to check valid and invalid values, and check they are accepted or rejected by the Helm chart. For now, I understand it is only possible to check for correctness, as incorrectness would fail upfront. Would that be a good idea to add helm-unittest the possibility to test failure ?
For now, we're running a python script that checks for assertions, but we'd prefer to have only one source of UT (helm-unittest).

Thanks,

(note: same question asked to https://github.com/quintush/helm-unittest , not sure which is best :) )

feature: edit release information on test, and reference maps

Hi,

Currently I see it's possible to set values, but not alter other Helm metadata before tempting for a test.
Would it be possible to adjust the temlpating to include the ability to adjust values such as .Release.Namespace?

While here, is it possible to make assertions based on the value of a map where the key contains .'s?
For example it would be awesome to be able to reference something like this for an Ingress

suite: test ingress
templates:
  - ingress.yaml
tests:
  - it: should include a default nginx snippet when not set by user
    asserts:
      - equal:
          path: metadata.annotations['nginx.ingress.kubernetes.io/configuration-snippet']
          value: "example stuff and things"

Apologies if it's already possible to do this, any help appreciated :-)

Assert failure

Hi,

Would it be possible to add an assertiontype where error would be accepted.

The use case for this is that in our charts we often use the required and or fail command to indicate that something is not setup correctly. We would like to write test cases for this to check that it's always covered.

Could be something like this

- it: Test failure
    set:
      image.tag: latest
    asserts:
      - hasFailed:
          pattern: '[0-9]+ tag is not valid'

Which would check against the error message. This could be with regex support or even just fixed string.

It's possible to use multiple files?

Hi!

I'm trying to use more than one file test and everything that I try test outside of deployment_test.yaml gives me:

Error:
documentIndex 0 out of range

Am I doing something wrong?

Deployment Test

suite: test deployment
values:
  - values.yaml
templates:
  - deployment.yaml
tests:
  - it: should pass all kinds of assertion
    asserts:
      - isNotEmpty:
          path: spec.template.spec.containers[0].image
      - isNotEmpty:
          path: metadata.name
      - isNotEmpty:
          path: spec.template.spec.containers[0].livenessProbe
      - isNotEmpty:
          path: spec.template.spec.containers[0].readinessProbe
      - isNotEmpty:
          path: spec.template.spec.containers[0].resources
      - isNotEmpty:
          path: spec.template.spec.serviceAccountName   
      - notMatchRegex:
          path: metadata.name
          pattern: ^.*-foobar$
      - isNotNull:
          path: spec.template
      - isKind:
          of: Deployment

Service Test

suite: test service
values:
  - values.yaml
templates:
  - service.yaml
tests:
  - it: should pass
    asserts:
      - isNotEmpty:
            path: spec.ports
      - isNotEmpty:
            path: spec.ports.port
      - isNotEm
<img width="530" alt="deployment_test yaml โ€” helm-charts 2022-05-06 16-47-50" src="https://user-images.githubusercontent.com/51094510/167207854-e701ca75-21dc-4c96-ba81-36dcc3e53983.png">
pty:
            path: spec.ports.targetPort
      - equal:
            path: spec.type
            value: ClusterIP

Sorry for the novice question!

Thanks!

It's not suport apiVersion v2?

The version of plugin unittest is unittest 0.2.8. And the version of helm is 3.8.2. I run these commands blow then got errors:

helm create helm3-unittest-demo
created some tests
helm unittest .

### Error:  apiVersion 'v2' is not valid. The value must be "v1"


Charts:      1 failed, 1 errored, 0 passed, 1 total

Installation failure

Trying to install this plugin, it shows the following error:

Error: Unable to update repository: exit status 128

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.