Skip to content

Commit

Permalink
add subPath e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
songjiaxun committed Sep 11, 2023
1 parent 4d4beb0 commit ad42d24
Show file tree
Hide file tree
Showing 4 changed files with 307 additions and 24 deletions.
1 change: 1 addition & 0 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ var _ = ginkgo.Describe("E2E Test Suite", func() {
testsuites.InitGcsFuseCSIMultiVolumeTestSuite,
testsuites.InitGcsFuseCSIGCSFuseIntegrationTestSuite,
testsuites.InitGcsFuseCSIPerformanceTestSuite,
testsuites.InitGcsFuseCSISubPathTestSuite,
}

testDriver := InitGCSFuseCSITestDriver(c, m, *bucketLocation, *skipGcpSaTest)
Expand Down
57 changes: 57 additions & 0 deletions test/e2e/specs/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package specs
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
"time"

Expand Down Expand Up @@ -188,13 +190,29 @@ func (t *TestPod) WaitForFailedMountError(ctx context.Context, msg string) {
}

func (t *TestPod) SetupVolume(volumeResource *storageframework.VolumeResource, name, mountPath string, readOnly bool, mountOptions ...string) {
t.setupVolume(volumeResource, name, readOnly, mountOptions...)
t.setupVolumeMount(name, mountPath, readOnly, "")
}

func (t *TestPod) SetupVolumeWithSubPath(volumeResource *storageframework.VolumeResource, name, mountPath string, readOnly bool, subPath string, reuseMount bool, mountOptions ...string) {
if !reuseMount {
t.setupVolume(volumeResource, name, readOnly, mountOptions...)
}

t.setupVolumeMount(name, mountPath, readOnly, subPath)
}

func (t *TestPod) setupVolumeMount(name, mountPath string, readOnly bool, subPath string) {
volumeMount := v1.VolumeMount{
Name: name,
MountPath: mountPath,
ReadOnly: readOnly,
SubPath: subPath,
}
t.pod.Spec.Containers[0].VolumeMounts = append(t.pod.Spec.Containers[0].VolumeMounts, volumeMount)
}

func (t *TestPod) setupVolume(volumeResource *storageframework.VolumeResource, name string, readOnly bool, mountOptions ...string) {
volume := v1.Volume{
Name: name,
}
Expand Down Expand Up @@ -689,3 +707,42 @@ func (t *TestJob) Cleanup(ctx context.Context) {
err := t.client.BatchV1().Jobs(t.namespace.Name).Delete(ctx, t.job.Name, metav1.DeleteOptions{PropagationPolicy: &d})
framework.ExpectNoError(err)
}

func CreateImplicitDirInBucket(dirPath, bucketName string) {
// Use bucketName as the name of a temp file since bucketName is unique.
f, err := os.Create(bucketName)
if err != nil {
framework.Failf("Failed to create an empty data file: %v", err)
}
f.Close()
defer func() {
err = os.Remove(bucketName)
if err != nil {
framework.Failf("Failed to delete the empty data file: %v", err)
}
}()

//nolint:gosec
if output, err := exec.Command("gsutil", "cp", bucketName, fmt.Sprintf("gs://%v/%v/", bucketName, dirPath)).CombinedOutput(); err != nil {
framework.Failf("Failed to create a implicit dir in GCS bucket: %v, output: %s", err, output)
}
}

func CreateEmptyFileInBucket(fileName, bucketName string) {
f, err := os.Create(fileName)
if err != nil {
framework.Failf("Failed to create an empty data file: %v", err)
}
f.Close()
defer func() {
err = os.Remove(fileName)
if err != nil {
framework.Failf("Failed to delete the empty data file: %v", err)
}
}()

//nolint:gosec
if output, err := exec.Command("gsutil", "cp", fileName, fmt.Sprintf("gs://%v", bucketName)).CombinedOutput(); err != nil {
framework.Failf("Failed to create an empty file in GCS bucket: %v, output: %s", err, output)
}
}
33 changes: 9 additions & 24 deletions test/e2e/testdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ package main
import (
"context"
"fmt"
"os"
"os/exec"
"strings"

"github.com/google/uuid"
Expand Down Expand Up @@ -176,6 +174,8 @@ func (n *GCSFuseCSITestDriver) CreateVolume(ctx context.Context, config *storage
if len(n.volumeStore) == 0 {
bucketName = n.createBucket(ctx, config.Framework.Namespace.Name)
} else {
config.Prefix = n.volumeStore[0].bucketName

return n.volumeStore[0]
}
}
Expand All @@ -187,11 +187,11 @@ func (n *GCSFuseCSITestDriver) CreateVolume(ctx context.Context, config *storage
case specs.InvalidMountOptionsVolumePrefix:
mountOptions += ",invalid-option"
case specs.ImplicitDirsVolumePrefix:
createImplicitDir(specs.ImplicitDirsPath, bucketName)
specs.CreateImplicitDirInBucket(specs.ImplicitDirsPath, bucketName)
mountOptions += ",implicit-dirs"
case specs.SubfolderInBucketPrefix:
dirPath := uuid.NewString()
createImplicitDir(dirPath, bucketName)
specs.CreateImplicitDirInBucket(dirPath, bucketName)
mountOptions += ",only-dir=" + dirPath
}

Expand All @@ -205,6 +205,11 @@ func (n *GCSFuseCSITestDriver) CreateVolume(ctx context.Context, config *storage
n.volumeStore = append(n.volumeStore, v)
}

if config.Prefix == "" {
// Use config.Prefix to pass the bucket names back to the test suite.
config.Prefix = bucketName
}

return v
case storageframework.DynamicPV:
// Do nothing
Expand Down Expand Up @@ -354,26 +359,6 @@ func (n *GCSFuseCSITestDriver) deleteBucket(ctx context.Context, bucketName stri
}
}

func createImplicitDir(dirPath, bucketName string) {
// Use bucketName as the name of a temp file since bucketName is unique.
f, err := os.Create(bucketName)
if err != nil {
e2eframework.Failf("Failed to create an empty data file: %v", err)
}
f.Close()
defer func() {
err = os.Remove(bucketName)
if err != nil {
e2eframework.Failf("Failed to delete the empty data file: %v", err)
}
}()

//nolint:gosec
if output, err := exec.Command("gsutil", "cp", bucketName, fmt.Sprintf("gs://%v/%v/", bucketName, dirPath)).CombinedOutput(); err != nil {
e2eframework.Failf("Failed to create a implicit dir in GCS bucket: %v, output: %s", err, output)
}
}

func prepareGcpSAName(ns string) string {
if len(ns) > 30 {
return ns[:30]
Expand Down
Loading

0 comments on commit ad42d24

Please sign in to comment.