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

Handle flat arrays from jsonpath in contains assert #228

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

yariksheptykin
Copy link
Contributor

I recently tried to write a test that checks if a particular volume name appears in the deployment manifest. Here is a simplified version of my template:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
spec:
  replicas: 1
  selector:
    matchLabels:
      component: test
  template:
    metadata:
      labels:
        component: test
    spec:
      containers:
        - name: test
          image: test
          volumeMounts:
            - name: test-volume
              mountPath: /test-data
      volumes:
        - name: test-volume-1
          persistentVolumeClaim:
            claimName: test-pvc-1
        - name: test-volume-2
          persistentVolumeClaim:
            claimName: test-pvc-2

Here is the test I was trying:

suite: jsonpath test
tests:
  - it: Mounts required PVCs
    asserts:
      - contains:
          path: spec.template.spec.volumes[*].name
          content: test-volume-1

To which I got an error saying:

Error:
        expect 'spec.template.spec.volumes[*].name' to be an array, got:
        test-volume-1

spec.template.spec.volumes[*].name seems like a valid jsonpath to me, so I would expect it to return an array of volume names and contains should be able to apply the assert. But this does not seem to be the case, which is surprising.

With this MR I am trying to add support for my use case if this makes sense?

@yariksheptykin yariksheptykin marked this pull request as ready for review October 27, 2023 12:35
@yariksheptykin yariksheptykin marked this pull request as draft October 27, 2023 12:42
@yariksheptykin
Copy link
Contributor Author

I was able to reproduce my issue in this MR:

https://app.circleci.com/pipelines/circleci/Ty1FBMSY599GLXNCuu7eco/TcvQehEnzgi2Sornz7FLNz/3/workflows/094ac1c5-a6d5-40d8-854d-3d3a7acc09d7/jobs/2/steps

Failed
test_runner_test.go:90: 
        	Error Trace:	/home/circleci/project/pkg/unittest/test_runner_test.go:90
        	Error:      	Should be true
        	Test:       	TestV3RunnerOkWithPassedTests
        	Messages:   	
        	            	### Chart [ basic ] ../../test/data/v3/basic
        	            	
        	            	 PASS  Configmap multiline Test	../../test/data/v3/basic/tests/configmap_test.yaml
        	            	 PASS  Custom Resource Definition Test	../../test/data/v3/basic/tests/crd_test.yaml
        	            	 FAIL  test deployment	../../test/data/v3/basic/tests/deployment_test.yaml
        	            		- should pass all kinds of assertion
        	            	
        	            			- asserts[6] `contains` fail
        	            				Template:	basic/templates/deployment.yaml
        	            				DocumentIndex:	0
        	            				Error:
        	            					expect 'spec.template.spec.containers[*].name' to be an array, got:
        	            					basic
        	            	
        	            	 PASS  test ingress	../../test/data/v3/basic/tests/ingress_test.yaml
        	            	 PASS  test override names and fullNames in Kubernetes resources	../../test/data/v3/basic/tests/namesOverride_test.yaml
        	            	 PASS  test notes	../../test/data/v3/basic/tests/notes_test.yaml
        	            	 PASS  test service	../../test/data/v3/basic/tests/service_test.yaml
        	            	
        	            	Charts:      1 failed, 0 passed, 1 total
        	            	Test Suites: 1 failed, 6 passed, 7 total
        	            	Tests:       1 failed, 16 passed, 17 total
        	            	Snapshot:    4 passed, 4 total
        	            	Time:        41.670427ms

@yariksheptykin
Copy link
Contributor Author

The evaluator for yaml-jsonpath does seem to return an array of elements for a similar jsonpath:

grafik

@yariksheptykin
Copy link
Contributor Author

@quintush I've played a bit with the implementation in my draft and I got a bit stuck deciding what I actually trying to achieve :)
TLDR; should I carry on and add support for my use case or should I better close this?

I started off convinced that path: spec.template.spec.containers[*].name should return an array strings and I should be able to use this array in contains asserts. After looking into the implementation I am not sure that this is actually indented / desired. It looks like the implementation of jsonpaths does support returning arrays but asserts (such as contains) only care for the fist element of this array. So now I am not sure if:

  • this limitation is actually a bug and should be fixed, or just the way it was intended,
  • if it should be fixed / improved, do we want to change the behavior of GetValueOfSetPath or should should we change the assets to handle all returned elements not just the first one

What do you think? I short reply is enough. I am just trying to decide if I should put more effort in this draft poking-around MR or simply drop it.

@yariksheptykin yariksheptykin changed the title Allow array for jsonpath in path Handle flat arrays returned by jsonpath Nov 8, 2023
@yariksheptykin yariksheptykin changed the title Handle flat arrays returned by jsonpath Handle flat arrays returned by jsonpath in contains assert Nov 8, 2023
@yariksheptykin yariksheptykin changed the title Handle flat arrays returned by jsonpath in contains assert Handle flat arrays from jsonpath in contains assert Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant