suite: test MySQL password envs for deployments
release:
  name: oncall
templates:
  - engine/deployment.yaml
  - engine/job-migrate.yaml
  - celery/deployment.yaml
  - telegram-polling/deployment.yaml
  - secrets.yaml
tests:
  - it: secrets -> should fail if externalMysql.password not set
    set:
      mariadb.enabled: false
    asserts:
      - failedTemplate:
          errorMessage: externalMysql.password is required if not mariadb.enabled and not externalMysql.existingSecret
        template: secrets.yaml

  - it: externalMySQL.password -> should create a Secret -mariadb-external
    templates:
      - engine/deployment.yaml
      - engine/job-migrate.yaml
      - celery/deployment.yaml
      - telegram-polling/deployment.yaml
    set:
      telegramPolling.enabled: true
      mariadb.enabled: false
      externalMysql:
        user: user123
        password: abcd123
    asserts:
      - contains:
          path: spec.template.spec.containers[0].env
          content:
            name: MYSQL_USER
            value: user123
      - contains:
          path: spec.template.spec.containers[0].env
          content:
            name: MYSQL_PASSWORD
            valueFrom:
              secretKeyRef:
                name: oncall-mysql-external
                key: mariadb-root-password
      - containsDocument:
          kind: Secret
          apiVersion: v1
          metadata.name: oncall-mysql-external
        template: secrets.yaml
      - equal:
          path: data.mariadb-root-password
          value: abcd123
          decodeBase64: true
        documentIndex: 1
        template: secrets.yaml

  - it: externalMysql.existingSecret -> should use existing secret
    templates:
      - engine/deployment.yaml
      - engine/job-migrate.yaml
      - celery/deployment.yaml
    set:
      mariadb.enabled: false
      externalMysql:
        existingSecret: some-mysql-secret
    asserts:
      - contains:
          path: spec.template.spec.containers[0].env
          content:
            name: MYSQL_USER
            value: root
      - contains:
          path: spec.template.spec.containers[0].env
          content:
            name: MYSQL_PASSWORD
            valueFrom:
              secretKeyRef:
                name: some-mysql-secret
                key: mariadb-root-password

  - it: externalMysql.usernameKey and externalMysql.passwordKey -> should be used for existing secret
    templates:
      - engine/deployment.yaml
      - engine/job-migrate.yaml
      - celery/deployment.yaml
    set:
      mariadb.enabled: false
      externalMysql:
        existingSecret: some-mysql-secret
        usernameKey: mysql.user
        passwordKey: mysql.key
    asserts:
      - contains:
          path: spec.template.spec.containers[0].env
          content:
            name: MYSQL_USER
            valueFrom:
              secretKeyRef:
                name: some-mysql-secret
                key: mysql.user
      - contains:
          path: spec.template.spec.containers[0].env
          content:
            name: MYSQL_PASSWORD
            valueFrom:
              secretKeyRef:
                name: some-mysql-secret
                key: mysql.key

  - it: |
      mariadb.enabled=false and migrate.useHook=>true -> should add "helm.sh/hook" annotations
      to secret with MySQL credentials
    templates:
      - secrets.yaml
    set:
      migrate.useHook: true
      mariadb.enabled: false
      externalMysql:
        user: user123
        password: abcd123
    asserts:
      - containsDocument:
          kind: Secret
          apiVersion: v1
          name: oncall-mysql-external
        documentIndex: 1
      - isSubset:
          path: metadata.annotations
          content:
            "helm.sh/hook": pre-install,pre-upgrade
            "helm.sh/hook-weight": "-5"
        documentIndex: 1
