quintush / helm-unittest

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

Assert equal on whole manifest #164

Closed johanvandeweerd closed 1 year ago

johanvandeweerd commented 2 years ago

Currently it's not possible to assert a whole manifest/document.

When you have this manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: test
  name: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
      - image: nginx
        name: nginx

and this unit test with an equal assert with an empty path:

suite: Equal on full document
templates:
  - deployment.yaml
tests:
  - it: should pass when asserting on full document
    asserts:
      - equal:
          path:
          value:
            apiVersion: apps/v1
            kind: Deployment
            metadata:
              labels:
                app: test
              name: test
            spec:
              replicas: 1
              selector:
                matchLabels:
                  app: test
              template:
                metadata:
                  labels:
                    app: test
                spec:
                  containers:
                  - image: nginx
                    name: nginx

I would expect the test to pass but it fails with following output:

### Chart [ unittest ] .

 FAIL  Equal on full document   tests/deployment_test.yaml
        - should pass when asserting on full document

                - asserts[0] `equal` fail

                        Template:       unittest/templates/deployment.yaml
                        DocumentIndex:  0
                        Path:
                        Expected:
                                apiVersion: apps/v1
                                kind: Deployment
                                metadata:
                                  labels:
                                    app: test
                                  name: test
                                spec:
                                  replicas: 1
                                  selector:
                                    matchLabels:
                                      app: test
                                  template:
                                    metadata:
                                      labels:
                                        app: test
                                    spec:
                                      containers:
                                      - image: nginx
                                        name: nginx
                        Actual:
                                apiVersion: apps/v1
                                kind: Deployment
                                metadata:
                                  labels:
                                    app: test
                                  name: test
                                spec:
                                  replicas: 1
                                  selector:
                                    matchLabels:
                                      app: test
                                  template:
                                    metadata:
                                      labels:
                                        app: test
                                    spec:
                                      containers:
                                      - image: nginx
                                        name: nginx
                        Diff:

Charts:      1 failed, 0 passed, 1 total
Test Suites: 1 failed, 0 passed, 1 total
Tests:       1 failed, 0 passed, 1 total
Snapshot:    0 passed, 0 total
Time:        24.591392ms

Error: plugin "unittest" exited with error

When browsing the code I see when path is empty, the whole manifest is returned and then this is passed to reflect.DeepEqual. reflect.DeepEqual checks if both types are the same (which they aren't) and fails the test. See https://github.com/quintush/helm-unittest/blob/master/pkg/unittest/validators/equal_validator.go#L63 for more info.

quintush commented 1 year ago

Hello @johanvandeweerd,

It has been a while before the confirmation. Indeed it is not possible, however using the snapshot functionality, you will get similar behavior and less coding.

Greetings, @quintush

johanvandeweerd commented 1 year ago

Thanks for the feedback.