suite: cnpg credentials test
templates:
  - common.yaml
release:
  name: test-release-name
  namespace: test-release-namespace
tests:
  - it: should generate correct secret
    set:
      cnpg:
        my-pg:
          enabled: true
          user: test-user
          database: test-db
          password: test-password
    asserts:
      - documentIndex: &secretDoc 2
        isKind:
          of: Secret
      - documentIndex: *secretDoc
        isAPIVersion:
          of: v1
      - documentIndex: *secretDoc
        equal:
          path: type
          value: kubernetes.io/basic-auth
      - documentIndex: *secretDoc
        equal:
          path: metadata.name
          value: test-release-name-common-test-cnpg-my-pg-user
      - documentIndex: *secretDoc
        equal:
          path: stringData.username
          value: test-user
      - documentIndex: *secretDoc
        equal:
          path: stringData.password
          value: test-password

      - documentIndex: &secretDoc 1
        isKind:
          of: Secret
      - documentIndex: *secretDoc
        isAPIVersion:
          of: v1
      - documentIndex: *secretDoc
        equal:
          path: metadata.name
          value: test-release-name-common-test-cnpg-my-pg-urls
      - documentIndex: *secretDoc
        isSubset:
          path: stringData
          content:
            porthost: test-release-name-common-test-cnpg-my-pg-rw:5432
            host: test-release-name-common-test-cnpg-my-pg-rw
            jdbc: jdbc:postgresql://test-release-name-common-test-cnpg-my-pg-rw:5432/test-db
            std: postgresql://test-user:test-password@test-release-name-common-test-cnpg-my-pg-rw:5432/test-db
            nossl: postgresql://test-user:test-password@test-release-name-common-test-cnpg-my-pg-rw:5432/test-db?sslmode=disable

  - it: should generate correct secret with pooler, only rw
    set:
      cnpg:
        my-pg:
          enabled: true
          user: test-user
          database: test-db
          password: test-password
          pooler:
            enabled: true
            createRO: false
    asserts:
      - documentIndex: &secretDoc 2
        isKind:
          of: Secret
      - documentIndex: *secretDoc
        isAPIVersion:
          of: v1
      - documentIndex: *secretDoc
        equal:
          path: metadata.name
          value: test-release-name-common-test-cnpg-my-pg-urls
      - documentIndex: *secretDoc
        isSubset:
          path: stringData
          content:
            porthost: test-release-name-common-test-cnpg-my-pg-pooler-rw:5432
            host: test-release-name-common-test-cnpg-my-pg-pooler-rw
            jdbc: jdbc:postgresql://test-release-name-common-test-cnpg-my-pg-pooler-rw:5432/test-db
            std: postgresql://test-user:test-password@test-release-name-common-test-cnpg-my-pg-pooler-rw:5432/test-db
            nossl: postgresql://test-user:test-password@test-release-name-common-test-cnpg-my-pg-pooler-rw:5432/test-db?sslmode=disable

  - it: should generate correct secret with pooler rw and ro
    set:
      cnpg:
        my-pg:
          enabled: true
          user: test-user
          database: test-db
          password: test-password
          pooler:
            enabled: true
            createRO: true
    asserts:
      - documentIndex: &secretDoc 3
        isKind:
          of: Secret
      - documentIndex: *secretDoc
        isAPIVersion:
          of: v1
      - documentIndex: *secretDoc
        equal:
          path: metadata.name
          value: test-release-name-common-test-cnpg-my-pg-urls
      - documentIndex: *secretDoc
        isSubset:
          path: stringData
          content:
            porthost: test-release-name-common-test-cnpg-my-pg-pooler-rw:5432
            host: test-release-name-common-test-cnpg-my-pg-pooler-rw
            jdbc: jdbc:postgresql://test-release-name-common-test-cnpg-my-pg-pooler-rw:5432/test-db
            hostRO: test-release-name-common-test-cnpg-my-pg-pooler-ro
            jdbcRO: jdbc:postgresql://test-release-name-common-test-cnpg-my-pg-pooler-ro:5432/test-db
            porthostRO: test-release-name-common-test-cnpg-my-pg-pooler-ro:5432
            std: postgresql://test-user:test-password@test-release-name-common-test-cnpg-my-pg-pooler-rw:5432/test-db
            nossl: postgresql://test-user:test-password@test-release-name-common-test-cnpg-my-pg-pooler-rw:5432/test-db?sslmode=disable
            stdRO: postgresql://test-user:test-password@test-release-name-common-test-cnpg-my-pg-pooler-ro:5432/test-db
            nosslRO: postgresql://test-user:test-password@test-release-name-common-test-cnpg-my-pg-pooler-ro:5432/test-db?sslmode=disable

  - it: should generate correct secret with password set
    set:
      cnpg:
        my-pg:
          enabled: true
          user: test-user
          database: test-db
          password: test-password
    asserts:
      - documentIndex: &secretDoc 2
        isKind:
          of: Secret
      - documentIndex: *secretDoc
        isAPIVersion:
          of: v1
      - documentIndex: *secretDoc
        equal:
          path: type
          value: kubernetes.io/basic-auth
      - documentIndex: *secretDoc
        equal:
          path: metadata.name
          value: test-release-name-common-test-cnpg-my-pg-user
      - documentIndex: *secretDoc
        equal:
          path: stringData.username
          value: test-user
      - documentIndex: *secretDoc
        equal:
          path: stringData.password
          value: test-password
  # Failures
  - it: should fail with no credentials on recovery
    set:
      cnpg:
        my-pg:
          enabled: true
          user: test-user
          database: test-db
          password: test-password
          mode: recovery
          recovery:
            method: object_store
    asserts:
      - failedTemplate:
          errorMessage: CNPG Recovery Provider Secret Spawner - Expected [recovery.credentials] to be defined on [recovery] mode
  - it: should fail with no credentials on backup
    set:
      cnpg:
        my-pg:
          enabled: true
          user: test-user
          database: test-db
          password: test-password
          mode: backup
          backups:
            enabled: true
    asserts:
      - failedTemplate:
          errorMessage: CNPG Recovery Provider Secret Spawner - Expected [backups.credentials] to be defined on [backup] mode
