kudobuilder / kuttl

KUbernetes Test TooL (kuttl)
https://kuttl.dev
Apache License 2.0
681 stars 86 forks source link

Proposal: Generate expected yaml from a command #552

Open gmolau opened 2 months ago

gmolau commented 2 months ago

Hi everyone, what do you think of making it possible to generate the expected yaml from a command? Currently there are two ways of writing the expected state:

  1. Directly in yaml
  2. Together with an assertion in a script

My use case is a controller that installs Helm charts, where I would like to make assertions against the helm status output. Since that output does not live on a cluster I currently have to write a script like this:


apiVersion: kuttl.dev/v1beta1
kind: TestAssert
commands:
  - script: |
      expectedNamespace=test-chart
      expectedRelease=test-chart
      expectedStatus=deployed

      helmStatus=$(helm status -n $expectedNamespace $expectedRelease)
      if [ $? -ne 0 ]; then
        echo "Failed to get Helm release status for $expectedRelease in namespace $expectedNamespace"
        exit 1
      fi

      status=$(echo "$helmStatus" | grep STATUS | awk '{print $2}')

      if [ "$status" != "$expectedStatus" ]; then
        echo "Expected Helm release $expectedRelease in namespace $expectedNamespace to have status '$expectedStatus', but got '$status'"
        exit 1
      fi

It would be much nicer if I could give a command that produces a yaml and an expected yaml, then rely on Kuttls internal machinery to compare the two. E.g. something like:

expected:
  status: deployed
actual: helm status -o yaml

Do you think it would be possible to add something like this? And if so, what would be the best way to implement it?

porridge commented 2 months ago

I think it would be doable, but not exactly easy. Here is the function that does the fetching and comparison.

I'd suggest that you write a short proposal how this would look from the user's PoV. The expected yaml would likely go into an assert.yaml file, but what about the command which produces the actual yaml?