suite: cnpg scheduled backup metadata test
templates:
  - common.yaml
chart:
  appVersion: &appVer v9.9.9
release:
  name: test-release-name
  namespace: test-release-namespace
tests:
  - it: should pass with cnpg scheduledBackup backup created with labels and annotations
    set:
      credentials:
        test: &test
          type: s3
          url: http://some-url
          bucket: some-bucket
          encrKey: some-encr-key
          accessKey: some-access-key
          secretKey: some-secret-key
      label1: label1
      label2: global_label2
      label3: label3
      annotation1: annotation1
      annotation2: global_annotation2
      annotation3: annotation3
      global:
        labels:
          g_label1: global_label1
          g_label2: "{{ .Values.label2 }}"
        annotations:
          g_annotation1: global_annotation1
          g_annotation2: "{{ .Values.annotation2 }}"
      cnpg:
        my-pg:
          enabled: true
          user: test-user
          database: test-db
          password: test-password
          labels:
            label1: "{{ .Values.label1 }}"
            label2: label2
          annotations:
            annotation1: "{{ .Values.annotation1 }}"
            annotation2: annotation2
          annotationsList:
            - name: some-annotation
              value: some-value
          labelsList:
            - name: some-label
              value: some-value
          backups:
            enabled: true
            target: primary
            retentionPolicy: 10d
            credentials: test
            destinationPath: some-path
            labels:
              label5: label5
            annotations:
              annotation5: annotation5
            scheduledBackups:
              - name: daily
                schedule: "0 0 * * *"
                backupOwnerReference: self
                labels:
                  label3: "{{ .Values.label3 }}"
                  label4: label4
                annotations:
                  annotation3: "{{ .Values.annotation3 }}"
                  annotation4: annotation4
    asserts:
      - documentIndex: &backupDoc 0
        isKind:
          of: ScheduledBackup
      - documentIndex: *backupDoc
        equal:
          path: metadata.annotations
          value:
            annotation1: annotation1
            annotation2: annotation2
            annotation3: annotation3
            annotation4: annotation4
            annotation5: annotation5
            g_annotation1: global_annotation1
            g_annotation2: global_annotation2
            some-annotation: some-value
      - documentIndex: *backupDoc
        equal:
          path: metadata.labels
          value:
            app: common-test-1.0.0
            release: test-release-name
            helm-revision: "0"
            helm.sh/chart: common-test-1.0.0
            app.kubernetes.io/name: common-test
            app.kubernetes.io/instance: test-release-name
            app.kubernetes.io/managed-by: Helm
            app.kubernetes.io/version: *appVer
            cnpg.io/cluster: test-release-name-common-test-cnpg-my-pg
            g_label1: global_label1
            g_label2: global_label2
            label1: label1
            label2: label2
            label3: label3
            label4: label4
            label5: label5
            some-label: some-value
      - documentIndex: *backupDoc
        equal:
          path: metadata.namespace
          value: test-release-namespace

  - it: should pass with cnpg scheduledBackup backup created with namespace
    set:
      credentials:
        test: *test
      cnpg:
        my-pg:
          enabled: true
          user: test-user
          database: test-db
          password: test-password
          namespace: some-namespace
          backups:
            enabled: true
            target: primary
            retentionPolicy: 10d
            credentials: test
            destinationPath: some-path
            scheduledBackups:
              - name: daily
                schedule: "0 0 * * *"
                backupOwnerReference: self
    asserts:
      - documentIndex: *backupDoc
        equal:
          path: metadata.namespace
          value: some-namespace

  - it: should pass with cnpg scheduledBackup backup created with object namespace from tpl
    set:
      credentials:
        test: *test
      key: some-namespace
      cnpg:
        my-pg:
          enabled: true
          user: test-user
          database: test-db
          password: test-password
          namespace: "{{ .Values.key }}"
          backups:
            enabled: true
            target: primary
            retentionPolicy: 10d
            credentials: test
            destinationPath: some-path
            scheduledBackups:
              - name: daily
                schedule: "0 0 * * *"
                backupOwnerReference: self
    asserts:
      - documentIndex: *backupDoc
        equal:
          path: metadata.namespace
          value: some-namespace

  - it: should pass with cnpg scheduledBackup backup created with namespace from global with tpl
    set:
      credentials:
        test: *test
      key: global-namespace
      global:
        namespace: "{{ .Values.key }}"
      cnpg:
        my-pg:
          enabled: true
          user: test-user
          database: test-db
          password: test-password
          backups:
            enabled: true
            target: primary
            retentionPolicy: 10d
            credentials: test
            destinationPath: some-path
            scheduledBackups:
              - name: daily
                schedule: "0 0 * * *"
                backupOwnerReference: self
    asserts:
      - documentIndex: *backupDoc
        equal:
          path: metadata.namespace
          value: global-namespace

  - it: should pass with cnpg scheduledBackup backup created with namespace from root with tpl
    set:
      credentials:
        test: *test
      key: local-namespace
      namespace: "{{ .Values.key }}"
      global:
        namespace: global-namespace
      cnpg:
        my-pg:
          enabled: true
          user: test-user
          database: test-db
          password: test-password
          backups:
            enabled: true
            target: primary
            retentionPolicy: 10d
            credentials: test
            destinationPath: some-path
            scheduledBackups:
              - name: daily
                schedule: "0 0 * * *"
                backupOwnerReference: self
    asserts:
      - documentIndex: *backupDoc
        equal:
          path: metadata.namespace
          value: local-namespace
