Skip to content

Commit 0b08dd7

Browse files
Merge pull request #956 from chrisThePattyEater/fixZBTestsNonGRPC
use go client to perform test set up in required tests
2 parents 31041c9 + 825b83d commit 0b08dd7

File tree

8 files changed

+114
-47
lines changed

8 files changed

+114
-47
lines changed

pkg/cloud_provider/storage/fake.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ type fakeService struct {
2828
sm fakeServiceManager
2929
}
3030

31+
// DownloadGCSObject implements Service.
32+
func (service *fakeService) DownloadGCSObject(ctx context.Context, bucketName string, objectName string, localPath string) error {
33+
return nil
34+
}
35+
3136
// UploadGCSObject implements Service.
3237
func (service *fakeService) UploadGCSObject(ctx context.Context, localPath string, bucketName string, objectName string) error {
3338
return nil

pkg/cloud_provider/storage/storage.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"errors"
2323
"fmt"
2424
"io"
25+
"os"
26+
"path/filepath"
2527
"strings"
2628
"time"
2729

@@ -56,6 +58,7 @@ type Service interface {
5658
RemoveIAMPolicy(ctx context.Context, obj *ServiceBucket, member, roleName string) error
5759
CheckBucketExists(ctx context.Context, obj *ServiceBucket) (bool, error)
5860
UploadGCSObject(ctx context.Context, localPath, bucketName, objectName string) error
61+
DownloadGCSObject(ctx context.Context, bucketName, objectName, localPath string) error
5962
Close()
6063
}
6164

@@ -344,6 +347,36 @@ func (service *gcsService) UploadGCSObject(ctx context.Context, filePathToUpload
344347
return nil
345348
}
346349

350+
// DownloadGCSObject downloads a GCS object to a specified local file path.
351+
func (service *gcsService) DownloadGCSObject(ctx context.Context, bucketName, objectName, filePathToDownload string) error {
352+
// 1. Create a reader for the GCS object.
353+
obj := service.storageClient.Bucket(bucketName).Object(objectName)
354+
r, err := obj.NewReader(ctx)
355+
if err != nil {
356+
return fmt.Errorf("failed to create reader for gs://%s/%s: %w", bucketName, objectName, err)
357+
}
358+
defer r.Close()
359+
360+
dir := filepath.Dir(filePathToDownload)
361+
if err := os.MkdirAll(dir, 0755); err != nil {
362+
return fmt.Errorf("failed to create directory %q: %w", dir, err)
363+
}
364+
365+
// 2. Create the local destination file.
366+
f, err := os.Create(filePathToDownload)
367+
if err != nil {
368+
return fmt.Errorf("failed to create local file %q: %w", filePathToDownload, err)
369+
}
370+
defer f.Close()
371+
372+
// 3. Copy the contents from the GCS object reader to the local file.
373+
if _, err := io.Copy(f, r); err != nil {
374+
return fmt.Errorf("failed to copy content to local file %q: %w", filePathToDownload, err)
375+
}
376+
377+
return nil
378+
}
379+
347380
func isBucketAZonalBucket(ctx context.Context, client *storage.Client, bucketName string) (bool, error) {
348381
attrs, err := client.Bucket(bucketName).Attrs(ctx)
349382
if err != nil {

test/e2e/specs/specs.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ package specs
2020
import (
2121
"context"
2222
"fmt"
23-
"os"
24-
"os/exec"
2523
"strings"
2624
"time"
2725

@@ -1170,32 +1168,6 @@ func (t *TestJob) Cleanup(ctx context.Context) {
11701168
framework.ExpectNoError(err)
11711169
}
11721170

1173-
func CreateTestFileInBucket(fileName, bucketName string) {
1174-
createTestFileInBucket(fileName, bucketName, []byte(fileName))
1175-
}
1176-
1177-
func CreateTestFileWithSizeInBucket(fileName, bucketName string, fileSize int) {
1178-
createTestFileInBucket(fileName, bucketName, make([]byte, fileSize))
1179-
}
1180-
1181-
func createTestFileInBucket(fileName, bucketName string, fileContent []byte) {
1182-
err := os.WriteFile(fileName, fileContent, 0o600)
1183-
if err != nil {
1184-
framework.Failf("Failed to create a test file: %v", err)
1185-
}
1186-
defer func() {
1187-
err = os.Remove(fileName)
1188-
if err != nil {
1189-
framework.Failf("Failed to delete the empty data file: %v", err)
1190-
}
1191-
}()
1192-
1193-
//nolint:gosec
1194-
if output, err := exec.Command("gsutil", "cp", fileName, fmt.Sprintf("gs://%v", bucketName)).CombinedOutput(); err != nil {
1195-
framework.Failf("Failed to create a test file in GCS bucket: %v, output: %s", err, output)
1196-
}
1197-
}
1198-
11991171
func GetGCSFuseVersion(ctx context.Context, f *framework.Framework) string {
12001172
client := f.ClientSet
12011173
configMaps, err := client.CoreV1().ConfigMaps("").List(ctx, metav1.ListOptions{

test/e2e/specs/testdriver.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func InitGCSFuseCSITestDriver(c clientset.Interface, m metadata.Service, bl stri
9191
bucketLocation: bl,
9292
skipGcpSaTest: skipGcpSaTest,
9393
ClientProtocol: clientProtocol,
94-
EnableHierarchicalNamespace: enableHierarchicalNamespace,
94+
EnableHierarchicalNamespace: enableHierarchicalNamespace || enableZB,
9595
EnableZB: enableZB, // Enable Zonal Buckets
9696
}
9797
}
@@ -511,3 +511,49 @@ func (n *GCSFuseCSITestDriver) CreateImplicitDirInBucket(ctx context.Context, di
511511
e2eframework.Failf("UploadGCSObject failed: %v", err)
512512
}
513513
}
514+
515+
func (n *GCSFuseCSITestDriver) CreateTestFileInBucket(ctx context.Context, fileName, bucketName string) {
516+
n.createTestFileInBucket(ctx, fileName, bucketName, []byte(fileName))
517+
}
518+
519+
func (n *GCSFuseCSITestDriver) CreateTestFileWithSizeInBucket(ctx context.Context, fileName, bucketName string, fileSize int) {
520+
n.createTestFileInBucket(ctx, fileName, bucketName, make([]byte, fileSize))
521+
}
522+
523+
func (n *GCSFuseCSITestDriver) createTestFileInBucket(ctx context.Context, fileName, bucketName string, fileContent []byte) {
524+
storageService, err := n.prepareStorageService(ctx)
525+
if err != nil {
526+
e2eframework.Failf("failed to prepare storage service: %w", err)
527+
return
528+
}
529+
530+
err = os.WriteFile(fileName, fileContent, 0o600)
531+
if err != nil {
532+
e2eframework.Failf("Failed to create a test file: %v", err)
533+
}
534+
defer func() {
535+
err = os.Remove(fileName)
536+
if err != nil {
537+
e2eframework.Failf("Failed to delete the empty data file: %v", err)
538+
}
539+
}()
540+
541+
if err := storageService.UploadGCSObject(ctx, fileName, bucketName, fileName); err != nil {
542+
e2eframework.Failf("Failed to upload test file %q to GCS bucket %q: %v", fileName, bucketName, err)
543+
}
544+
}
545+
546+
func (n *GCSFuseCSITestDriver) DownloadGCSObject(ctx context.Context, bucketName, objectName, localPath string) error {
547+
storageService, err := n.prepareStorageService(ctx)
548+
if err != nil {
549+
fmt.Errorf("failed to prepare storage service: %w", err)
550+
return err
551+
}
552+
553+
if err := storageService.DownloadGCSObject(ctx, bucketName, objectName, localPath); err != nil {
554+
e2eframework.Logf("Failed to download test file %q to local path %q: %v", objectName, localPath, err)
555+
return err
556+
}
557+
558+
return nil
559+
}

test/e2e/testsuites/file_cache.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ func (t *gcsFuseCSIFileCacheTestSuite) SkipUnsupportedTests(_ storageframework.T
5959
}
6060

6161
func (t *gcsFuseCSIFileCacheTestSuite) DefineTests(driver storageframework.TestDriver, pattern storageframework.TestPattern) {
62+
gcsfuseDriver, ok := driver.(*specs.GCSFuseCSITestDriver)
63+
if !ok {
64+
framework.Failf("This test requires a GCSFuseCSITestDriver but received a %T", driver)
65+
}
6266
type local struct {
6367
config *storageframework.PerTestConfig
6468
volumeResource *storageframework.VolumeResource
@@ -94,9 +98,9 @@ func (t *gcsFuseCSIFileCacheTestSuite) DefineTests(driver storageframework.TestD
9498
// The test driver uses config.Prefix to pass the bucket names back to the test suite.
9599
bucketName := l.config.Prefix
96100

97-
// Create files using gsutil
101+
// Create files using go client
98102
fileName := uuid.NewString()
99-
specs.CreateTestFileInBucket(fileName, bucketName)
103+
gcsfuseDriver.CreateTestFileInBucket(ctx, fileName, bucketName)
100104

101105
ginkgo.By("Configuring the pod")
102106
tPod := specs.NewTestPod(f.ClientSet, f.Namespace)
@@ -129,9 +133,9 @@ func (t *gcsFuseCSIFileCacheTestSuite) DefineTests(driver storageframework.TestD
129133
// The test driver uses config.Prefix to pass the bucket names back to the test suite.
130134
bucketName := l.config.Prefix
131135

132-
// Create files using gsutil
136+
// Create files using go client
133137
fileName := uuid.NewString()
134-
specs.CreateTestFileInBucket(fileName, bucketName)
138+
gcsfuseDriver.CreateTestFileInBucket(ctx, fileName, bucketName)
135139

136140
ginkgo.By("Configuring the pod")
137141
tPod := specs.NewTestPod(f.ClientSet, f.Namespace)
@@ -170,9 +174,9 @@ func (t *gcsFuseCSIFileCacheTestSuite) DefineTests(driver storageframework.TestD
170174
// The test driver uses config.Prefix to pass the bucket names back to the test suite.
171175
bucketName := l.config.Prefix
172176

173-
// Create files using gsutil
177+
// Create files using go client
174178
fileName := uuid.NewString()
175-
specs.CreateTestFileInBucket(fileName, bucketName)
179+
gcsfuseDriver.CreateTestFileInBucket(ctx, fileName, bucketName)
176180

177181
ginkgo.By("Configuring the pod")
178182
tPod := specs.NewTestPod(f.ClientSet, f.Namespace)
@@ -212,9 +216,9 @@ func (t *gcsFuseCSIFileCacheTestSuite) DefineTests(driver storageframework.TestD
212216
// The test driver uses config.Prefix to pass the bucket names back to the test suite.
213217
bucketName := l.config.Prefix
214218

215-
// Create files using gsutil
219+
// Create files using go client
216220
fileName := uuid.NewString()
217-
specs.CreateTestFileInBucket(fileName, bucketName)
221+
gcsfuseDriver.CreateTestFileInBucket(ctx, fileName, bucketName)
218222

219223
ginkgo.By("Configuring the pod")
220224
tPod := specs.NewTestPod(f.ClientSet, f.Namespace)
@@ -246,7 +250,7 @@ func (t *gcsFuseCSIFileCacheTestSuite) DefineTests(driver storageframework.TestD
246250
// Create files using gsutil
247251
fileName := uuid.NewString()
248252
// The file size 110 MB is larger than the 100 MB fileCacheCapacity
249-
specs.CreateTestFileWithSizeInBucket(fileName, bucketName, 110*1024*1024)
253+
gcsfuseDriver.CreateTestFileWithSizeInBucket(ctx, fileName, bucketName, 110*1024*1024)
250254

251255
ginkgo.By("Configuring the pod")
252256
tPod := specs.NewTestPod(f.ClientSet, f.Namespace)
@@ -276,7 +280,7 @@ func (t *gcsFuseCSIFileCacheTestSuite) DefineTests(driver storageframework.TestD
276280
// Create files using gsutil
277281
fileName := uuid.NewString()
278282
// The file size 2 GB is larger than the 1 GB PD
279-
specs.CreateTestFileWithSizeInBucket(fileName, bucketName, 2*1024*1024*1024)
283+
gcsfuseDriver.CreateTestFileWithSizeInBucket(ctx, fileName, bucketName, 2*1024*1024*1024)
280284

281285
ginkgo.By("Configuring the pod")
282286
tPod := specs.NewTestPod(f.ClientSet, f.Namespace)

test/e2e/testsuites/gcsfuse_integration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (t *gcsFuseCSIGCSFuseIntegrationTestSuite) DefineTests(driver storageframew
179179
}
180180

181181
// HNS is supported after v2.5.0
182-
if !v.AtLeast(version.MustParseSemantic("v2.5.0-gke.0")) && hnsEnabled(driver) {
182+
if !v.AtLeast(version.MustParseSemantic("v2.5.0-gke.0")) && (hnsEnabled(driver) || zbEnabled(driver)) {
183183
e2eskipper.Skipf("skip gcsfuse integration HNS tests on gcsfuse version %v", v.String())
184184
}
185185

test/e2e/testsuites/metrics.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"context"
2222
"fmt"
2323
"os"
24-
"os/exec"
2524

2625
"local/test/e2e/specs"
2726

@@ -79,6 +78,11 @@ func (t *gcsFuseCSIMetricsTestSuite) SkipUnsupportedTests(_ storageframework.Tes
7978

8079
//nolint:maintidx
8180
func (t *gcsFuseCSIMetricsTestSuite) DefineTests(driver storageframework.TestDriver, pattern storageframework.TestPattern) {
81+
gcsfuseDriver, ok := driver.(*specs.GCSFuseCSITestDriver)
82+
if !ok {
83+
framework.Failf("This test requires a GCSFuseCSITestDriver but received a %T", driver)
84+
}
85+
8286
type local struct {
8387
config *storageframework.PerTestConfig
8488
volumeResourceList []*storageframework.VolumeResource
@@ -132,8 +136,7 @@ func (t *gcsFuseCSIMetricsTestSuite) DefineTests(driver storageframework.TestDri
132136
}
133137

134138
fileName := uuid.NewString()
135-
specs.CreateTestFileInBucket(fileName, bucketName)
136-
139+
gcsfuseDriver.CreateTestFileInBucket(ctx, fileName, bucketName)
137140
// Read file A.
138141
tPod.VerifyExecInPodSucceed(f, specs.TesterContainerName, fmt.Sprintf("cat %v/%v", mountPath, fileName))
139142

@@ -193,8 +196,8 @@ func (t *gcsFuseCSIMetricsTestSuite) DefineTests(driver storageframework.TestDri
193196
promFile := fmt.Sprintf("%v/%v/metrics.prom", l.artifactsDir, f.Namespace.Name)
194197

195198
//nolint:gosec
196-
if output, err := exec.Command("gsutil", "cp", fmt.Sprintf("gs://%v/metrics.prom", bucketName), promFile).CombinedOutput(); err != nil {
197-
framework.Failf("Failed to download the Prometheus metrics data from GCS bucket %q: %v, output: %s", bucketName, err, output)
199+
if err := gcsfuseDriver.DownloadGCSObject(ctx, bucketName, "metrics.prom", promFile); err != nil {
200+
framework.Failf("Failed to download the Prometheus metrics data from GCS bucket %q: %v", bucketName, err)
198201
}
199202

200203
ginkgo.By("Parsing Prometheus metrics")

test/e2e/testsuites/subpath.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,12 @@ func (t *gcsFuseCSISubPathTestSuite) DefineTests(driver storageframework.TestDri
173173
// Create files using gsutil
174174
file1 := uuid.NewString()
175175
file2 := uuid.NewString()
176-
specs.CreateTestFileInBucket(file1, bucketName)
177-
specs.CreateTestFileInBucket(file2, bucketName)
176+
if driver, ok := driver.(*specs.GCSFuseCSITestDriver); ok {
177+
driver.CreateTestFileInBucket(ctx, file1, bucketName)
178+
driver.CreateTestFileInBucket(ctx, file2, bucketName)
179+
} else {
180+
framework.Failf("Driver is not of type GCSFuseCSITestDriver, cannot create implicit directories")
181+
}
178182

179183
ginkgo.By("Configuring the pod")
180184
tPod1 := specs.NewTestPod(f.ClientSet, f.Namespace)

0 commit comments

Comments
 (0)