From ab80ed5bd6e20816d24e68ce98eca5573c14b36b Mon Sep 17 00:00:00 2001
From: Derek Su <derek.su@suse.com>
Date: Mon, 27 May 2024 07:45:19 +0800
Subject: [PATCH] Add e2e test for custom path patterns

Signed-off-by: Derek Su <derek.su@suse.com>
---
 test/pod_test.go                              | 11 +++++++++--
 .../custom-path-pattern/kustomization.yaml    | 14 ++++++++++++++
 .../local-path-config.yaml                    | 19 +++++++++++++++++++
 test/testdata/custom-path-pattern/pod.yaml    | 18 ++++++++++++++++++
 test/testdata/custom-path-pattern/pvc.yaml    | 13 +++++++++++++
 .../custom-path-pattern/storage-class.yaml    | 10 ++++++++++
 6 files changed, 83 insertions(+), 2 deletions(-)
 create mode 100644 test/testdata/custom-path-pattern/kustomization.yaml
 create mode 100644 test/testdata/custom-path-pattern/local-path-config.yaml
 create mode 100644 test/testdata/custom-path-pattern/pod.yaml
 create mode 100644 test/testdata/custom-path-pattern/pvc.yaml
 create mode 100644 test/testdata/custom-path-pattern/storage-class.yaml

diff --git a/test/pod_test.go b/test/pod_test.go
index 3d129ebb..5a69d7e7 100644
--- a/test/pod_test.go
+++ b/test/pod_test.go
@@ -5,11 +5,12 @@ package test
 
 import (
 	"fmt"
-	"github.com/kelseyhightower/envconfig"
-	"github.com/stretchr/testify/suite"
 	"strings"
 	"testing"
 	"time"
+
+	"github.com/kelseyhightower/envconfig"
+	"github.com/stretchr/testify/suite"
 )
 
 const (
@@ -150,6 +151,12 @@ func (p *PodTestSuite) TestPodWithMultipleStorageClasses() {
 	runTest(p, []string{p.config.IMAGE}, "ready", hostPathVolumeType)
 }
 
+func (p *PodTestSuite) TestPodWithCustomPathPatternStorageClasses() {
+	p.kustomizeDir = "custom-path-pattern"
+
+	runTest(p, []string{p.config.IMAGE}, "ready", hostPathVolumeType)
+}
+
 func runTest(p *PodTestSuite, images []string, waitCondition, volumeType string) {
 	kustomizeDir := testdataFile(p.kustomizeDir)
 
diff --git a/test/testdata/custom-path-pattern/kustomization.yaml b/test/testdata/custom-path-pattern/kustomization.yaml
new file mode 100644
index 00000000..df458be4
--- /dev/null
+++ b/test/testdata/custom-path-pattern/kustomization.yaml
@@ -0,0 +1,14 @@
+apiVersion: kustomize.config.k8s.io/v1beta1
+kind: Kustomization
+resources:
+- ../../../deploy
+- storage-class.yaml
+- pod.yaml
+- pvc.yaml
+patchesStrategicMerge:
+- local-path-config.yaml
+commonLabels:
+  app: local-path-provisioner
+images:
+- name: rancher/local-path-provisioner
+  newTag: dev
\ No newline at end of file
diff --git a/test/testdata/custom-path-pattern/local-path-config.yaml b/test/testdata/custom-path-pattern/local-path-config.yaml
new file mode 100644
index 00000000..ee2087c0
--- /dev/null
+++ b/test/testdata/custom-path-pattern/local-path-config.yaml
@@ -0,0 +1,19 @@
+kind: ConfigMap
+apiVersion: v1
+metadata:
+  name: local-path-config
+  namespace: local-path-storage
+data:
+  config.json: |-
+    {
+            "storageClassConfigs":{
+                   "local-path-custom-path-pattern": {
+                          "nodePathMap": [
+                          {
+                                  "node":"DEFAULT_PATH_FOR_NON_LISTED_NODES",
+                                  "paths":["/opt/local-path-provisioner"]
+                          }
+                          ]
+                   }
+            }
+    }
\ No newline at end of file
diff --git a/test/testdata/custom-path-pattern/pod.yaml b/test/testdata/custom-path-pattern/pod.yaml
new file mode 100644
index 00000000..08725116
--- /dev/null
+++ b/test/testdata/custom-path-pattern/pod.yaml
@@ -0,0 +1,18 @@
+apiVersion: v1
+kind: Pod
+metadata:
+  name: volume-test
+spec:
+  containers:
+  - name: volume-test
+    image: nginx:stable-alpine
+    imagePullPolicy: IfNotPresent
+    volumeMounts:
+    - name: volv
+      mountPath: /data
+    ports:
+    - containerPort: 80
+  volumes:
+  - name: volv
+    persistentVolumeClaim:
+      claimName: local-path-pvc
\ No newline at end of file
diff --git a/test/testdata/custom-path-pattern/pvc.yaml b/test/testdata/custom-path-pattern/pvc.yaml
new file mode 100644
index 00000000..67f46a2c
--- /dev/null
+++ b/test/testdata/custom-path-pattern/pvc.yaml
@@ -0,0 +1,13 @@
+apiVersion: v1
+kind: PersistentVolumeClaim
+metadata:
+  labels:
+    app: test
+  name: local-path-pvc
+spec:
+  storageClassName: local-path-custom-path-pattern
+  accessModes:
+    - ReadWriteOnce
+  resources:
+    requests:
+      storage: 100Mi
\ No newline at end of file
diff --git a/test/testdata/custom-path-pattern/storage-class.yaml b/test/testdata/custom-path-pattern/storage-class.yaml
new file mode 100644
index 00000000..7656afc3
--- /dev/null
+++ b/test/testdata/custom-path-pattern/storage-class.yaml
@@ -0,0 +1,10 @@
+apiVersion: storage.k8s.io/v1
+kind: StorageClass
+metadata:
+  name: local-path-custom-path-pattern
+provisioner: rancher.io/local-path
+parameters:
+  nodePath: /opt/local-path-provisioner
+  pathPattern: "{{ .PVC.Namespace }}/{{ .PVC.Name }}"
+volumeBindingMode: WaitForFirstConsumer
+reclaimPolicy: Retain
\ No newline at end of file
-- 
GitLab