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

How to use contains for a list within a list #319

Closed
Kellen275 opened this issue Apr 6, 2024 · 2 comments
Closed

How to use contains for a list within a list #319

Kellen275 opened this issue Apr 6, 2024 · 2 comments
Labels

Comments

@Kellen275
Copy link

Kellen275 commented Apr 6, 2024

In my particular case, I'm attempting to check if a Deployment contains a container that mounts a particular volume. However, I have multiple volumeMounts that I'm not concerned with

I.e., I want to verify the following exists

# deployment.yaml
spec:
  template:
    spec:
      containers:
        - name: RELEASE-NAME
          volumeMounts:
            - mountPath: /my-volume
              name: my-volume

I'm attempting to use the following example_test.yaml

suite: example test
templates:
  - deployment.yaml
tests:
  - it: Should Mount Volume
    asserts:
      - contains:
          any: true
          path: spec.template.spec.containers
          content:
            name: RELEASE-NAME
            volumeMounts:
              - mountPath: /my-volume
                name: my-volume

However, it appears that the any: true doesn't propagate

$ helm unittest .

### Chart [ example ] .

 FAIL  example test tests/example_test.yaml
    - Should Mount Volume

        - asserts[0] `contains` fail
            Template:   example/templates/deployment.yaml
            DocumentIndex:  0
            Path:   spec.template.spec.containers
            Expected to contain:
                - name: RELEASE-NAME
                  volumeMounts:
                    - mountPath: /my-volume
                      name: my-volume
            Actual:
                - name: RELEASE-NAME
                  volumeMounts:
                    - mountPath: /my-volume
                      name: my-volume
                    - mountPath: /tmp
                      name: tmp
@quintush
Copy link
Contributor

Hello @Kellen275,

The contains assertion can only validate 1 level deep.
So in your example it can only validate the object structure of the container object and not the container/volumeMount object.

As the focus of youre is pointing to the volumeMounts, the best way to use contain is by setting a more strict path that identifies the volumeMounts

suite: example test
templates:
  - deployment.yaml
tests:
  - it: Should Mount Volume
    asserts:
      - contains:
          any: true
          path: spec.template.spec.containers[0].volumeMounts
          content:
            mountPath: /my-volume
            name: my-volume

or (when using jsonPath selector in the path)

suite: example test
templates:
  - deployment.yaml
tests:
  - it: Should Mount Volume
    asserts:
      - contains:
          any: true
          path: spec.template.spec.containers[?(@.name == "RELEASE-NAME")].volumeMounts
          content:
            mountPath: /my-volume
            name: my-volume

Greetings,
@quintush

@Kellen275
Copy link
Author

Hello @quintush thanks for the clarification.

I was hoping to avoid needing to "arbitrarily" index into containers with containers[0]. However, your second example achieves exactly what I was hoping to do! Thank you!

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

2 participants