quintush / helm-unittest

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

metadata.annotations is expected to be array when it should be a map #99

Closed danielhoherd closed 3 years ago

danielhoherd commented 3 years ago

Description

metadata.annotations is expected to be array when it should be a map.

Test

suite: Test nginx-service
templates:
  - nginx-service.yaml
tests:
  - it: works with ingressAnnotations
    set:
      ingressAnnotations:
        foo1: foo
        foo2: foo
        foo3: foo
    asserts:
      - contains:
          path: metadata.annotations
          content:
            foo1: foo
            foo2: foo
            foo3: foo

Result

 FAIL  Test nginx-service   charts/nginx/tests/nginx-service_test.yaml
    - works with ingressAnnotations

        - asserts[0] `contains` fail

            Template:   astronomer/charts/nginx/templates/nginx-service.yaml
            DocumentIndex:  0
            Error:
                expect 'metadata.annotations' to be an array, got:
                foo1: foo
                foo2: foo
                foo3: foo
                prometheus.io/port: "10254"
                prometheus.io/scrape: "true"
                service.beta.kubernetes.io/aws-load-balancer-type: nlb

Discussion

Using the contains example as a reference...

contains:
  path: spec.ports
  content:
    name: web
    port: 80
    targetPort: 80
    protocle:TCP

…I expected my test would work. Looking through the code, I don't see a reference to 'annotations' anywhere so I suspect the behavior is imported from an upstream library, however the bug is manifesting itself here so I filed the bug here.

I also tried restructuring the test to compare against metadata and assert content of annotations: but that didn't work either. However, I was able to get it to work with a different comparison:

    asserts:
      - equal:
          path: metadata.annotations.foo1
          value: foo

It's possible I've got an invalid test. Looking around in Github I don't see any other tests that use contains with metadata. It's also possible this is a documentation bug.

danielhoherd commented 3 years ago

I think I discovered my issue. It look like the contains comparison expects the 'content' to be the contents of an item within an array of the comparator, not a direct dict.

quintush commented 3 years ago

Hello @danielhoherd,

You already answered yourself. 😁

You can try to use the following as a test:

- equal:
          ​path​: ​metadata.annotations​
          ​value:
            ​foo1​: ​foo​
            ​foo2​: ​foo​
            ​foo3​: ​foo

The equal should validate the complete map.

Greetings, @quintush

danielhoherd commented 3 years ago

@quintush thanks for the quick reply! Also, I really appreciate the work you've put into this project.