suite: pv metadata test
templates:
  - common.yaml
chart:
  appVersion: &appVer v9.9.9
tests:
  - it: should pass with pv created with labels and annotations (nfs-pv-pvc)
    set:
      label1: label1
      label2: global_label2
      annotation1: annotation1
      annotation2: global_annotation2
      global:
        labels:
          g_label1: global_label1
          g_label2: "{{ .Values.label2 }}"
        annotations:
          g_annotation1: global_annotation1
          g_annotation2: "{{ .Values.annotation2 }}"
      persistence:
        my-volume1:
          enabled: true
          type: nfs-pv-pvc
          server: my-server
          share: /my-path
          labels:
            label1: "{{ .Values.label1 }}"
            label2: label2
          annotations:
            annotation1: "{{ .Values.annotation1 }}"
            annotation2: annotation2
    asserts:
      - documentIndex: &pvDoc 0
        isKind:
          of: PersistentVolume
      - documentIndex: *pvDoc
        isAPIVersion:
          of: v1
      - documentIndex: *pvDoc
        equal:
          path: metadata.annotations
          value:
            annotation1: annotation1
            annotation2: annotation2
            g_annotation1: global_annotation1
            g_annotation2: global_annotation2
            pv.kubernetes.io/provisioned-by: nfs.csi.k8s.io
      - documentIndex: *pvDoc
        equal:
          path: metadata.labels
          value:
            app: common-test-1.0.0
            release: RELEASE-NAME
            helm-revision: "0"
            helm.sh/chart: common-test-1.0.0
            app.kubernetes.io/instance: RELEASE-NAME
            app.kubernetes.io/managed-by: Helm
            app.kubernetes.io/version: *appVer
            app.kubernetes.io/name: common-test
            g_label1: global_label1
            g_label2: global_label2
            label1: label1
            label2: label2

  - it: should pass with pvc created with retain set to true (nfs-pv-pvc)
    set:
      persistence:
        my-volume1:
          enabled: true
          type: nfs-pv-pvc
          server: my-server
          share: /my-path
          retain: true
    asserts:
      - documentIndex: *pvDoc
        isKind:
          of: PersistentVolume
      - documentIndex: *pvDoc
        equal:
          path: metadata.annotations
          value:
            "helm.sh/resource-policy": keep
            pv.kubernetes.io/provisioned-by: nfs.csi.k8s.io
      - documentIndex: *pvDoc
        equal:
          path: metadata.labels
          value:
            app: common-test-1.0.0
            release: RELEASE-NAME
            helm-revision: "0"
            helm.sh/chart: common-test-1.0.0
            app.kubernetes.io/instance: RELEASE-NAME
            app.kubernetes.io/managed-by: Helm
            app.kubernetes.io/version: *appVer
            app.kubernetes.io/name: common-test

  - it: should pass with pv created with labels and annotations (smb-pv-pvc)
    set:
      label1: label1
      label2: global_label2
      annotation1: annotation1
      annotation2: global_annotation2
      global:
        labels:
          g_label1: global_label1
          g_label2: "{{ .Values.label2 }}"
        annotations:
          g_annotation1: global_annotation1
          g_annotation2: "{{ .Values.annotation2 }}"
      persistence:
        my-volume1:
          enabled: true
          type: smb-pv-pvc
          server: my-server
          share: my-path
          username: my-user
          password: my-password
          labels:
            label1: "{{ .Values.label1 }}"
            label2: label2
          annotations:
            annotation1: "{{ .Values.annotation1 }}"
            annotation2: annotation2
    asserts:
      - documentIndex: &secretDoc 0
        isKind:
          of: Secret
      - documentIndex: *secretDoc
        isAPIVersion:
          of: v1
      - documentIndex: *secretDoc
        equal:
          path: metadata.annotations
          value:
            annotation1: annotation1
            annotation2: annotation2
            g_annotation1: global_annotation1
            g_annotation2: global_annotation2
      - documentIndex: *secretDoc
        equal:
          path: metadata.labels
          value:
            app: common-test-1.0.0
            release: RELEASE-NAME
            helm-revision: "0"
            helm.sh/chart: common-test-1.0.0
            app.kubernetes.io/instance: RELEASE-NAME
            app.kubernetes.io/managed-by: Helm
            app.kubernetes.io/version: *appVer
            app.kubernetes.io/name: common-test
            g_label1: global_label1
            g_label2: global_label2
            label1: label1
            label2: label2
      - documentIndex: &pvDoc 1
        isKind:
          of: PersistentVolume
      - documentIndex: *pvDoc
        isAPIVersion:
          of: v1
      - documentIndex: *pvDoc
        equal:
          path: metadata.annotations
          value:
            annotation1: annotation1
            annotation2: annotation2
            g_annotation1: global_annotation1
            g_annotation2: global_annotation2
            pv.kubernetes.io/provisioned-by: smb.csi.k8s.io
      - documentIndex: *pvDoc
        equal:
          path: metadata.labels
          value:
            app: common-test-1.0.0
            release: RELEASE-NAME
            helm-revision: "0"
            helm.sh/chart: common-test-1.0.0
            app.kubernetes.io/instance: RELEASE-NAME
            app.kubernetes.io/managed-by: Helm
            app.kubernetes.io/version: *appVer
            app.kubernetes.io/name: common-test
            g_label1: global_label1
            g_label2: global_label2
            label1: label1
            label2: label2

  - it: should pass with pvc created with retain set to true (smb-pv-pvc)
    set:
      persistence:
        my-volume1:
          enabled: true
          type: smb-pv-pvc
          server: my-server
          share: my-path
          username: my-user
          password: my-password
          retain: true
    asserts:
      - documentIndex: *secretDoc
        isKind:
          of: Secret
      - documentIndex: *secretDoc
        isAPIVersion:
          of: v1
      - documentIndex: *secretDoc
        isNull:
          path: metadata.annotations
      - documentIndex: *pvDoc
        isKind:
          of: PersistentVolume
      - documentIndex: *pvDoc
        equal:
          path: metadata.annotations
          value:
            "helm.sh/resource-policy": keep
            pv.kubernetes.io/provisioned-by: smb.csi.k8s.io
      - documentIndex: *pvDoc
        equal:
          path: metadata.labels
          value:
            app: common-test-1.0.0
            release: RELEASE-NAME
            helm-revision: "0"
            helm.sh/chart: common-test-1.0.0
            app.kubernetes.io/instance: RELEASE-NAME
            app.kubernetes.io/managed-by: Helm
            app.kubernetes.io/version: *appVer
            app.kubernetes.io/name: common-test
