Skip to content

Commit

Permalink
add subPath e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
boddumanohar committed Apr 7, 2021
1 parent 0e56559 commit 0370392
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 3 deletions.
24 changes: 24 additions & 0 deletions test/e2e/dynamic_provisioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,30 @@ var _ = ginkgo.Describe("Dynamic Provisioning", func() {
}
test.Run(cs, ns)
})

ginkgo.It("should create a pod with volume mount subpath [nfs.csi.k8s.io] [Windows]", func() {
pods := []testsuites.PodDetails{
{
Cmd: convertToPowershellCommandIfNecessary("echo 'hello world' > /mnt/test-1/data && grep 'hello world' /mnt/test-1/data"),
Volumes: []testsuites.VolumeDetails{
{
ClaimSize: "10Gi",
VolumeMount: testsuites.VolumeMountDetails{
NameGenerate: "test-volume-",
MountPathGenerate: "/mnt/test-",
},
},
},
IsWindows: isWindowsCluster,
},
}
test := testsuites.DynamicallyProvisionedVolumeSubpathTester{
CSIDriver: testDriver,
Pods: pods,
StorageClassParameters: defaultStorageClassParameters,
}
test.Run(cs, ns)
})
})

func restClient(group string, version string) (restclientset.Interface, error) {
Expand Down
25 changes: 24 additions & 1 deletion test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ import (
)

const (
kubeconfigEnvVar = "KUBECONFIG"
kubeconfigEnvVar = "KUBECONFIG"
testWindowsEnvVar = "TEST_WINDOWS"
)

var (
nodeID = os.Getenv("NODE_ID")
perm *uint32
nfsDriver *nfs.Driver
isWindowsCluster = os.Getenv(testWindowsEnvVar) != ""
defaultStorageClassParameters = map[string]string{
"server": "nfs-server.default.svc.cluster.local",
"share": "/",
Expand Down Expand Up @@ -168,3 +170,24 @@ func TestE2E(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "E2E Suite")
}

func convertToPowershellCommandIfNecessary(command string) string {
if !isWindowsCluster {
return command
}

switch command {
case "echo 'hello world' > /mnt/test-1/data && grep 'hello world' /mnt/test-1/data":
return "echo 'hello world' | Out-File -FilePath C:\\mnt\\test-1\\data.txt; Get-Content C:\\mnt\\test-1\\data.txt | findstr 'hello world'"
case "touch /mnt/test-1/data":
return "echo $null >> C:\\mnt\\test-1\\data"
case "while true; do echo $(date -u) >> /mnt/test-1/data; sleep 100; done":
return "while (1) { Add-Content -Encoding Unicode C:\\mnt\\test-1\\data.txt $(Get-Date -Format u); sleep 1 }"
case "echo 'hello world' >> /mnt/test-1/data && while true; do sleep 100; done":
return "Add-Content -Encoding Unicode C:\\mnt\\test-1\\data.txt 'hello world'; while (1) { sleep 1 }"
case "echo 'hello world' >> /mnt/test-1/data && while true; do sleep 3600; done":
return "Add-Content -Encoding Unicode C:\\mnt\\test-1\\data.txt 'hello world'; while (1) { sleep 1 }"
}

return command
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package testsuites

import (
"github.com/kubernetes-csi/csi-driver-nfs/test/e2e/driver"

"github.com/onsi/ginkgo"
v1 "k8s.io/api/core/v1"
clientset "k8s.io/client-go/kubernetes"
)

// DynamicallyProvisionedCmdVolumeTest will provision required StorageClass(es), PVC(s) and Pod(s)
// Waiting for the PV provisioner to create a new PV
// Testing if the Pod(s) Cmd is run with a 0 exit code
type DynamicallyProvisionedVolumeSubpathTester struct {
CSIDriver driver.DynamicPVTestDriver
Pods []PodDetails
StorageClassParameters map[string]string
}

func (t *DynamicallyProvisionedVolumeSubpathTester) Run(client clientset.Interface, namespace *v1.Namespace) {
for _, pod := range t.Pods {
tpod, cleanup := pod.SetupWithDynamicVolumesWithSubpath(client, namespace, t.CSIDriver, t.StorageClassParameters)
// defer must be called here for resources not get removed before using them
for i := range cleanup {
defer cleanup[i]()
}

ginkgo.By("deploying the pod")
tpod.Create()
defer tpod.Cleanup()
ginkgo.By("checking that the pods command exits with no error")
tpod.WaitForSuccess()
}
}
16 changes: 14 additions & 2 deletions test/e2e/testsuites/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ var (
)

type PodDetails struct {
Cmd string
Volumes []VolumeDetails
Cmd string
Volumes []VolumeDetails
IsWindows bool
}

type VolumeMode int
Expand Down Expand Up @@ -159,3 +160,14 @@ func (pod *PodDetails) SetupWithDynamicMultipleVolumes(client clientset.Interfac
}
return tpod, cleanupFuncs
}

func (pod *PodDetails) SetupWithDynamicVolumesWithSubpath(client clientset.Interface, namespace *v1.Namespace, csiDriver driver.DynamicPVTestDriver, storageClassParameters map[string]string) (*TestPod, []func()) {
tpod := NewTestPod(client, namespace, pod.Cmd, pod.IsWindows)
cleanupFuncs := make([]func(), 0)
for n, v := range pod.Volumes {
tpvc, funcs := v.SetupDynamicPersistentVolumeClaim(client, namespace, csiDriver, storageClassParameters)
cleanupFuncs = append(cleanupFuncs, funcs...)
tpod.SetupVolumeMountWithSubpath(tpvc.persistentVolumeClaim, fmt.Sprintf("%s%d", v.VolumeMount.NameGenerate, n+1), fmt.Sprintf("%s%d", v.VolumeMount.MountPathGenerate, n+1), "testSubpath", v.VolumeMount.ReadOnly)
}
return tpod, cleanupFuncs
}
22 changes: 22 additions & 0 deletions test/e2e/testsuites/testsuites.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,25 @@ func (t *TestPersistentVolumeClaim) DeleteBackingVolume(cs *nfs.ControllerServer
ginkgo.Fail(fmt.Sprintf("could not delete volume %q: %v", volumeID, err))
}
}

func (t *TestPod) SetupVolumeMountWithSubpath(pvc *v1.PersistentVolumeClaim, name, mountPath string, subpath string, readOnly bool) {
volumeMount := v1.VolumeMount{
Name: name,
MountPath: mountPath,
SubPath: subpath,
ReadOnly: readOnly,
}

t.pod.Spec.Containers[0].VolumeMounts = append(t.pod.Spec.Containers[0].VolumeMounts, volumeMount)

volume := v1.Volume{
Name: name,
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: pvc.Name,
},
},
}

t.pod.Spec.Volumes = append(t.pod.Spec.Volumes, volume)
}

0 comments on commit 0370392

Please sign in to comment.