suite: volsync replication dest spec test
templates:
  - common.yaml
chart:
  appVersion: &appVer v9.9.9
release:
  name: test-release-name
  namespace: test-release-namespace
tests:
  - it: should generate correct spec
    set:
      persistence:
        destbackup:
          enabled: true
          type: pvc
          mountPath: /backedup
          volsync:
            - name: mybackup1
              type: restic
              credentials: mys3
              dest:
                enabled: true
              src:
                enabled: false
      credentials: &credentials
        mys3:
          type: s3
          url: https://s3.some-url
          bucket: some-bucket
          encrKey: some-key
          accessKey: some-access-key
          secretKey: some-secret-key
    asserts:
      - documentIndex: &secretDoc 0
        isKind:
          of: Secret
      - documentIndex: *secretDoc
        isAPIVersion:
          of: v1
      - documentIndex: *secretDoc
        equal:
          path: metadata.name
          value: test-release-name-common-test-destbackup-volsync-mybackup1
      - documentIndex: *secretDoc
        equal:
          path: stringData
          value:
            AWS_ACCESS_KEY_ID: some-access-key
            AWS_SECRET_ACCESS_KEY: some-secret-key
            RESTIC_PASSWORD: some-key
            RESTIC_REPOSITORY: s3:s3.some-url/some-bucket/test-release-name/volsync/destbackup-volsync-mybackup1
      - documentIndex: &replicationDestDoc 1
        isKind:
          of: ReplicationDestination
      - documentIndex: *replicationDestDoc
        isAPIVersion:
          of: volsync.backube/v1alpha1
      - documentIndex: *replicationDestDoc
        equal:
          path: spec
          value:
            trigger:
              manual: restore-once
            restic:
              repository: test-release-name-common-test-destbackup-volsync-mybackup1
              copyMethod: Snapshot
              cleanupTempPVC: false
              cleanupCachePVC: false
              cacheCapacity: 10Gi
              capacity: 100Gi
              accessModes:
                - ReadWriteOnce
              moverSecurityContext:
                fsGroup: 568
                runAsUser: 568
                runAsGroup: 568

  - it: should generate correct spec with overridden region
    set:
      persistence:
        destbackup:
          enabled: true
          type: pvc
          mountPath: /backedup
          volsync:
            - name: mybackup1
              type: restic
              credentials: mys3
              dest:
                enabled: true
              src:
                enabled: false
      credentials:
        mys3:
          type: s3
          url: https://s3.some-url
          bucket: some-bucket
          encrKey: some-key
          accessKey: some-access-key
          secretKey: some-secret-key
          region: some-region
    asserts:
      - documentIndex: *secretDoc
        isKind:
          of: Secret
      - documentIndex: *secretDoc
        isAPIVersion:
          of: v1
      - documentIndex: *secretDoc
        equal:
          path: metadata.name
          value: test-release-name-common-test-destbackup-volsync-mybackup1
      - documentIndex: *secretDoc
        equal:
          path: stringData
          value:
            AWS_ACCESS_KEY_ID: some-access-key
            AWS_SECRET_ACCESS_KEY: some-secret-key
            RESTIC_PASSWORD: some-key
            RESTIC_REPOSITORY: s3:s3.some-url/some-bucket/test-release-name/volsync/destbackup-volsync-mybackup1
            AWS_DEFAULT_REGION: some-region
      - documentIndex: *replicationDestDoc
        isKind:
          of: ReplicationDestination
      - documentIndex: *replicationDestDoc
        isAPIVersion:
          of: volsync.backube/v1alpha1
      - documentIndex: *replicationDestDoc
        equal:
          path: spec
          value:
            trigger:
              manual: restore-once
            restic:
              repository: test-release-name-common-test-destbackup-volsync-mybackup1
              copyMethod: Snapshot
              cleanupTempPVC: false
              cleanupCachePVC: false
              cacheCapacity: 10Gi
              capacity: 100Gi
              accessModes:
                - ReadWriteOnce
              moverSecurityContext:
                fsGroup: 568
                runAsUser: 568
                runAsGroup: 568

  - it: should generate correct spec with changed defaults
    set:
      persistence:
        destbackup:
          enabled: true
          type: pvc
          mountPath: /backedup
          volsync:
            - name: mybackup1
              type: restic
              credentials: mys3
              copyMethod: Clone
              dest:
                enabled: true
                capacity: 200Gi
              src:
                enabled: false
      credentials: *credentials
    asserts:
      - documentIndex: *replicationDestDoc
        isKind:
          of: ReplicationDestination
      - documentIndex: *replicationDestDoc
        isAPIVersion:
          of: volsync.backube/v1alpha1
      - documentIndex: *replicationDestDoc
        equal:
          path: spec
          value:
            trigger:
              manual: restore-once
            restic:
              repository: test-release-name-common-test-destbackup-volsync-mybackup1
              copyMethod: Clone
              cleanupTempPVC: false
              cleanupCachePVC: false
              cacheCapacity: 10Gi
              capacity: 200Gi
              accessModes:
                - ReadWriteOnce
              moverSecurityContext:
                fsGroup: 568
                runAsUser: 568
                runAsGroup: 568

  - it: should generate correct spec with storageClassName
    set:
      persistence:
        destbackup:
          enabled: true
          type: pvc
          mountPath: /backedup
          volsync:
            - name: mybackup1
              type: restic
              credentials: mys3
              copyMethod: Clone
              dest:
                enabled: true
                storageClassName: somestorageclass
              src:
                enabled: false
      credentials: *credentials
    asserts:
      - documentIndex: *replicationDestDoc
        isKind:
          of: ReplicationDestination
      - documentIndex: *replicationDestDoc
        isAPIVersion:
          of: volsync.backube/v1alpha1
      - documentIndex: *replicationDestDoc
        equal:
          path: spec
          value:
            trigger:
              manual: restore-once
            restic:
              repository: test-release-name-common-test-destbackup-volsync-mybackup1
              copyMethod: Clone
              cleanupTempPVC: false
              cleanupCachePVC: false
              cacheCapacity: 10Gi
              capacity: 100Gi
              accessModes:
                - ReadWriteOnce
              storageClassName: somestorageclass
              moverSecurityContext:
                fsGroup: 568
                runAsUser: 568
                runAsGroup: 568

  - it: should generate correct spec with volumeSnapshotClass
    set:
      persistence:
        destbackup:
          enabled: true
          type: pvc
          mountPath: /backedup
          volsync:
            - name: mybackup1
              type: restic
              credentials: mys3
              copyMethod: Clone
              dest:
                enabled: true
                volumeSnapshotClassName: somevsc
              src:
                enabled: false
      credentials: *credentials
    asserts:
      - documentIndex: *replicationDestDoc
        isKind:
          of: ReplicationDestination
      - documentIndex: *replicationDestDoc
        isAPIVersion:
          of: volsync.backube/v1alpha1
      - documentIndex: *replicationDestDoc
        equal:
          path: spec
          value:
            trigger:
              manual: restore-once
            restic:
              repository: test-release-name-common-test-destbackup-volsync-mybackup1
              copyMethod: Clone
              cleanupTempPVC: false
              cleanupCachePVC: false
              cacheCapacity: 10Gi
              capacity: 100Gi
              accessModes:
                - ReadWriteOnce
              volumeSnapshotClassName: somevsc
              moverSecurityContext:
                fsGroup: 568
                runAsUser: 568
                runAsGroup: 568

  - it: should generate correct spec with volumeSnapshotClass and StorageClass
    set:
      persistence:
        destbackup:
          enabled: true
          type: pvc
          mountPath: /backedup
          volsync:
            - name: mybackup1
              type: restic
              credentials: mys3
              copyMethod: Clone
              dest:
                enabled: true
                storageClassName: somestorageclass
                volumeSnapshotClassName: somevsc
              src:
                enabled: false
      credentials: *credentials
    asserts:
      - documentIndex: *replicationDestDoc
        isKind:
          of: ReplicationDestination
      - documentIndex: *replicationDestDoc
        isAPIVersion:
          of: volsync.backube/v1alpha1
      - documentIndex: *replicationDestDoc
        equal:
          path: spec
          value:
            trigger:
              manual: restore-once
            restic:
              repository: test-release-name-common-test-destbackup-volsync-mybackup1
              copyMethod: Clone
              cleanupTempPVC: false
              cleanupCachePVC: false
              cacheCapacity: 10Gi
              capacity: 100Gi
              accessModes:
                - ReadWriteOnce
              storageClassName: somestorageclass
              volumeSnapshotClassName: somevsc
              moverSecurityContext:
                fsGroup: 568
                runAsUser: 568
                runAsGroup: 568

  - it: should generate correct spec with cleanup flags
    set:
      persistence:
        destbackup:
          enabled: true
          type: pvc
          mountPath: /backedup
          volsync:
            - name: mybackup1
              type: restic
              credentials: mys3
              cleanupTempPVC: true
              cleanupCachePVC: true
              dest:
                enabled: true
              src:
                enabled: false
      credentials: *credentials
    asserts:
      - documentIndex: *replicationDestDoc
        isKind:
          of: ReplicationDestination
      - documentIndex: *replicationDestDoc
        isAPIVersion:
          of: volsync.backube/v1alpha1
      - documentIndex: *replicationDestDoc
        equal:
          path: spec
          value:
            trigger:
              manual: restore-once
            restic:
              repository: test-release-name-common-test-destbackup-volsync-mybackup1
              copyMethod: Snapshot
              cleanupTempPVC: true
              cleanupCachePVC: true
              cacheCapacity: 10Gi
              capacity: 100Gi
              accessModes:
                - ReadWriteOnce
              moverSecurityContext:
                fsGroup: 568
                runAsUser: 568
                runAsGroup: 568

  - it: should generate correct spec with destinationPVC
    set:
      persistence:
        destbackup:
          enabled: true
          type: pvc
          mountPath: /backedup
          volsync:
            - name: mybackup1
              type: restic
              credentials: mys3
              copyMethod: Direct
              dest:
                enabled: true
                capacity: 200Gi
              src:
                enabled: false
      credentials: *credentials
    asserts:
      - documentIndex: *replicationDestDoc
        isKind:
          of: ReplicationDestination
      - documentIndex: *replicationDestDoc
        isAPIVersion:
          of: volsync.backube/v1alpha1
      - documentIndex: *replicationDestDoc
        equal:
          path: spec
          value:
            trigger:
              manual: restore-once
            restic:
              repository: test-release-name-common-test-destbackup-volsync-mybackup1
              copyMethod: Direct
              cleanupTempPVC: false
              cleanupCachePVC: false
              destinationPVC: test-release-name-common-test-destbackup
              cacheCapacity: 10Gi
              capacity: 200Gi
              accessModes:
                - ReadWriteOnce
              moverSecurityContext:
                fsGroup: 568
                runAsUser: 568
                runAsGroup: 568

  - it: should generate correct spec with customCA
    set:
      persistence:
        destbackup:
          enabled: true
          type: pvc
          mountPath: /backedup
          volsync:
            - name: mybackup1
              type: restic
              credentials: mys3
              dest:
                enabled: true
              src:
                enabled: false
      credentials:
        mys3:
          type: s3
          url: https://s3.some-url
          bucket: some-bucket
          encrKey: some-key
          accessKey: some-access-key
          secretKey: some-secret-key
          customCA: |-
            -----BEGIN CERTIFICATE-----
            ...
            -----END CERTIFICATE-----
    asserts:
      - documentIndex: &secretCaDoc 1
        isKind:
          of: Secret
      - documentIndex: *secretCaDoc
        isAPIVersion:
          of: v1
      - documentIndex: *secretCaDoc
        equal:
          path: metadata.name
          value: test-release-name-common-test-volsync-ca-mys3
      - documentIndex: *secretCaDoc
        equal:
          path: stringData
          value:
            ca.crt: |-
              -----BEGIN CERTIFICATE-----
              ...
              -----END CERTIFICATE-----
      - documentIndex: &replicationCaDestDoc 2
        isKind:
          of: ReplicationDestination
      - documentIndex: *replicationCaDestDoc
        isAPIVersion:
          of: volsync.backube/v1alpha1
      - documentIndex: *replicationCaDestDoc
        equal:
          path: spec
          value:
            trigger:
              manual: restore-once
            restic:
              repository: test-release-name-common-test-destbackup-volsync-mybackup1
              customCA:
                secretName: test-release-name-common-test-volsync-ca-mys3
                key: ca.crt
              copyMethod: Snapshot
              cleanupTempPVC: false
              cleanupCachePVC: false
              cacheCapacity: 10Gi
              capacity: 100Gi
              accessModes:
                - ReadWriteOnce
              moverSecurityContext:
                fsGroup: 568
                runAsUser: 568
                runAsGroup: 568

  - it: should generate correct spec with customCA and uppercase credentials name
    set:
      persistence:
        destbackup:
          enabled: true
          type: pvc
          mountPath: /backedup
          volsync:
            - name: mybackup1
              type: restic
              credentials: MyS3
              dest:
                enabled: true
              src:
                enabled: false
      credentials:
        MyS3:
          type: s3
          url: https://s3.some-url
          bucket: some-bucket
          encrKey: some-key
          accessKey: some-access-key
          secretKey: some-secret-key
          customCA: |-
            -----BEGIN CERTIFICATE-----
            ...
            -----END CERTIFICATE-----
    asserts:
      - documentIndex: &secretCaDoc 1
        isKind:
          of: Secret
      - documentIndex: *secretCaDoc
        isAPIVersion:
          of: v1
      - documentIndex: *secretCaDoc
        equal:
          path: metadata.name
          value: test-release-name-common-test-volsync-ca-mys3
      - documentIndex: *secretCaDoc
        equal:
          path: stringData
          value:
            ca.crt: |-
              -----BEGIN CERTIFICATE-----
              ...
              -----END CERTIFICATE-----
      - documentIndex: &replicationCaDestDoc 2
        isKind:
          of: ReplicationDestination
      - documentIndex: *replicationCaDestDoc
        isAPIVersion:
          of: volsync.backube/v1alpha1
      - documentIndex: *replicationCaDestDoc
        equal:
          path: spec
          value:
            trigger:
              manual: restore-once
            restic:
              repository: test-release-name-common-test-destbackup-volsync-mybackup1
              customCA:
                secretName: test-release-name-common-test-volsync-ca-mys3
                key: ca.crt
              copyMethod: Snapshot
              cleanupTempPVC: false
              cleanupCachePVC: false
              cacheCapacity: 10Gi
              capacity: 100Gi
              accessModes:
                - ReadWriteOnce
              moverSecurityContext:
                fsGroup: 568
                runAsUser: 568
                runAsGroup: 568

  - it: should generate correct spec with customCASecretRef
    set:
      secret:
        mys3-ca-secret:
          enabled: true
          data:
            ca.crt: |-
              -----BEGIN CERTIFICATE-----
              ...
              -----END CERTIFICATE-----
      persistence:
        destbackup:
          enabled: true
          type: pvc
          mountPath: /backedup
          volsync:
            - name: mybackup1
              type: restic
              credentials: mys3
              dest:
                enabled: true
              src:
                enabled: false
      credentials:
        mys3:
          type: s3
          url: https://s3.some-url
          bucket: some-bucket
          encrKey: some-key
          accessKey: some-access-key
          secretKey: some-secret-key
          customCASecretRef:
            name: mys3-ca-secret
            key: ca.crt
    asserts:
      - documentIndex: *secretDoc
        isKind:
          of: Secret
      - documentIndex: *secretDoc
        isAPIVersion:
          of: v1
      - documentIndex: *secretDoc
        equal:
          path: metadata.name
          value: test-release-name-common-test-mys3-ca-secret
      - documentIndex: *secretDoc
        equal:
          path: stringData
          value:
            ca.crt: |-
              -----BEGIN CERTIFICATE-----
              ...
              -----END CERTIFICATE-----
      - documentIndex: *replicationCaDestDoc
        isKind:
          of: ReplicationDestination
      - documentIndex: *replicationCaDestDoc
        isAPIVersion:
          of: volsync.backube/v1alpha1
      - documentIndex: *replicationCaDestDoc
        equal:
          path: spec
          value:
            trigger:
              manual: restore-once
            restic:
              repository: test-release-name-common-test-destbackup-volsync-mybackup1
              customCA:
                secretName: test-release-name-common-test-mys3-ca-secret
                key: ca.crt
              copyMethod: Snapshot
              cleanupTempPVC: false
              cleanupCachePVC: false
              cacheCapacity: 10Gi
              capacity: 100Gi
              accessModes:
                - ReadWriteOnce
              moverSecurityContext:
                fsGroup: 568
                runAsUser: 568
                runAsGroup: 568
