kyverno / chainsaw

Declarative K8s e2e testing
https://kyverno.github.io/chainsaw/
Apache License 2.0
272 stars 41 forks source link

[Question] Can you use conditionals within templating? #1874

Open jtucci opened 3 weeks ago

jtucci commented 3 weeks ago

Describe your question

Hey All, Is there a way to conditionally assert fields within resources based on templated input?

Here is an example of what I am trying to do. I have two values files to represent two different test scenarios for asserting a crossplane resource that creates a cluster.

#values-basic.yaml
clusterName: dev
tenant: test
---
#values-hibernation-enabled

clusterName: dev
tenant: test
hibernation:
  enabled: true 

The resource I apply in my test step

apiVersion: example.org/v1alpha1
kind: Shoot
metadata:
  name: shoot
  namespace: ($values.tenant)
spec:
  parameters:
    name: ($values.clusterName)

    # I want to only set this if it was set in values file 
    # IF values.hibernation.enabled
    hibernation: ($values.hibernation.enabled)
# Assertion File 
kind: Shoot
apiVersion: core.gardener.cloud/v1beta1
metadata:
  name: ($values.clusterName)
  namespace: (join('-', ['garden', $values.clusterName]))
  annotations:
    confirmation.gardener.cloud/deletion: 'true'
spec: 

 # I only want to assert this if it was set in values file 
 # IF values.hibernation.enabled
  hibernation:
    enabled: true 

I then have a script which loops over all the values files to run chainsaw test --values ./compositions/shoot/$values_file.

Is something like this possible? Are there any examples I can look through?

chainsaw version Version

v0.2.8

Additional context

No response

eddycharly commented 4 days ago

Shouldn't this be done in bash (the caller) ?

eddycharly commented 1 day ago

@jtucci does the answer above makes sense ?