# yaml-language-server: $schema=https://raw.githubusercontent.com/kyverno/chainsaw/main/.schemas/json/test-chainsaw-v1alpha1.json
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
  name: cert-recreates
spec:
  steps:
    - name: step-00
      description: "verify certificate is mounted in restarted operators"
      try:
        - delete:
            ref:
              apiVersion: v1
              kind: Secret
              namespace: keptn-system
              name: keptn-certs
        - script:
            content: |
              set -e
              kubectl rollout restart deployment -n keptn-system -l control-plane=lifecycle-operator
        - assert:
            file: 00-assert.yaml
    - name: step-01
      description: "verify certificate is working with new secret"
      try:
        - delete:
            ref:
              apiVersion: v1
              kind: Secret
              namespace: keptn-system
              name: keptn-certs
        - assert:
            file: 01-assert.yaml
    - name: step-02
      description: "force an invalid/expired certificate to check recreation"
      try:
        - apply:
            file: 02-install.yaml
        - script:
            content: |
              str1=$(kubectl get secret keptn-certs -n keptn-system -o=go-template='{{index .data "tls.crt"}}')
              str2="LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUNkekNDQWg2Z0F3SUJBZ0lRZUpBWkJMcmxCY2VqNzh3cm4wV1ZIekFLQmdncWhrak9QUVFEQWpDQmlURUwKa0ZGY1FpY29hdE8yRFJnPQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0t"
              if [ "$str1" == "$str2" ]; then
              echo "Strings are equal" $str1
              exit 1
              else
              echo "Strings are not equal" $str1 "!=" $str2
              fi
        - assert:
            file: 02-assert.yaml
