Skip to content

Commit

Permalink
Merge pull request #2938 from GoogleCloudPlatform/streaming-writes-it
Browse files Browse the repository at this point in the history
Add Symlink/ReadFile tests for streaming writes scenario and run CommonLocalFileTestSuite from Streaming writes package.
  • Loading branch information
meet2mky authored Jan 30, 2025
2 parents 3c616bf + b29f539 commit 5db9e11
Show file tree
Hide file tree
Showing 18 changed files with 246 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,16 @@
// limitations under the License.

// Provides integration tests for create local file.
package local_file_test
package local_file

import (
"path"

. "github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/client"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/operations"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/setup"
"github.com/stretchr/testify/suite"
)

// //////////////////////////////////////////////////////////////////////
// Boilerplate
// //////////////////////////////////////////////////////////////////////

type CommonLocalFileTestSuite struct {
suite.Suite
}

////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////
Expand Down
80 changes: 80 additions & 0 deletions tools/integration_tests/local_file/local_file_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2025 Google LLC
//
// 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 local_file

import (
"context"
"os"
"testing"

"cloud.google.com/go/storage"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/client"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/operations"
)

const (
onlyDirMounted = "OnlyDirMountLocalFiles"
testDirLocalFileTest = "LocalFileTest"
)

var (
testDirName string
testDirPath string
storageClient *storage.Client
ctx context.Context
)

////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////

func WritingToLocalFileShouldNotWriteToGCS(ctx context.Context, storageClient *storage.Client,
fh *os.File, testDirName, fileName string, t *testing.T) {
operations.WriteWithoutClose(fh, client.FileContents, t)
client.ValidateObjectNotFoundErrOnGCS(ctx, storageClient, testDirName, fileName, t)
}

func NewFileShouldGetSyncedToGCSAtClose(ctx context.Context, storageClient *storage.Client,
testDirPath, fileName string, t *testing.T) {
// Create a local file.
_, fh := client.CreateLocalFileInTestDir(ctx, storageClient, testDirPath, fileName, t)

// Writing contents to local file shouldn't create file on GCS.
testDirName := client.GetDirName(testDirPath)
WritingToLocalFileShouldNotWriteToGCS(ctx, storageClient, fh, testDirName, fileName, t)

// Close the file and validate if the file is created on GCS.
client.CloseFileAndValidateContentFromGCS(ctx, storageClient, fh, testDirName, fileName, client.FileContents, t)
}

// Following setters are required to setup ctx, storageClient, testDirName for the local file tests
// getting executed from different package.
func SetCtx(otherCtx context.Context) {
if ctx == nil {
ctx = otherCtx
}
}

func SetStorageClient(otherStorageClient *storage.Client) {
if storageClient == nil {
storageClient = otherStorageClient
}
}

func SetTestDirName(otherTestDirName string) {
if testDirName == "" {
testDirName = otherTestDirName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Provides integration tests for create local file.

package local_file_test
package local_file

import (
"testing"

"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/test_suite"
"github.com/stretchr/testify/suite"
)

Expand All @@ -28,12 +25,9 @@ import (

type localFileTestSuite struct {
CommonLocalFileTestSuite
suite.Suite
}

////////////////////////////////////////////////////////////////////////
// Tests
////////////////////////////////////////////////////////////////////////

func TestLocalFileTestSuite(t *testing.T) {
suite.Run(t, new(localFileTestSuite))
type CommonLocalFileTestSuite struct {
test_suite.TestifySuite
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// Provides integration tests for readDir call containing local files.
package local_file_test
package local_file

import (
"io/fs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// Provides integration tests for read operation on local files.
package local_file_test
package local_file

import (
. "github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/client"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//
// Provides integration tests for removeDir operation on directories containing local files.
package local_file_test
package local_file

import (
"path"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// Provides integration tests for rename operation on local files.
package local_file_test
package local_file

import (
"os"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// Provides integration tests for file and directory operations.

package local_file_test
package local_file

import (
"context"
Expand All @@ -23,49 +23,14 @@ import (
"path"
"testing"

"cloud.google.com/go/storage"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/client"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/mounting/dynamic_mounting"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/mounting/only_dir_mounting"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/mounting/static_mounting"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/operations"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/setup"
"github.com/stretchr/testify/suite"
)

const (
testDirName = "LocalFileTest"
onlyDirMounted = "OnlyDirMountLocalFiles"
)

var (
testDirPath string
storageClient *storage.Client
ctx context.Context
)

////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////

func WritingToLocalFileShouldNotWriteToGCS(ctx context.Context, storageClient *storage.Client,
fh *os.File, testDirName, fileName string, t *testing.T) {
operations.WriteWithoutClose(fh, client.FileContents, t)
client.ValidateObjectNotFoundErrOnGCS(ctx, storageClient, testDirName, fileName, t)
}

func NewFileShouldGetSyncedToGCSAtClose(ctx context.Context, storageClient *storage.Client,
testDirPath, fileName string, t *testing.T) {
// Create a local file.
_, fh := client.CreateLocalFileInTestDir(ctx, storageClient, testDirPath, fileName, t)

// Writing contents to local file shouldn't create file on GCS.
testDirName := client.GetDirName(testDirPath)
WritingToLocalFileShouldNotWriteToGCS(ctx, storageClient, fh, testDirName, fileName, t)

// Close the file and validate if the file is created on GCS.
client.CloseFileAndValidateContentFromGCS(ctx, storageClient, fh, testDirName, fileName, client.FileContents, t)
}

////////////////////////////////////////////////////////////////////////
// TestMain
////////////////////////////////////////////////////////////////////////
Expand All @@ -75,6 +40,9 @@ func TestMain(m *testing.M) {

setup.ExitWithFailureIfBothTestBucketAndMountedDirectoryFlagsAreNotSet()

// set the test dir to local file test
testDirName = testDirLocalFileTest

// Create storage client before running tests.
ctx = context.Background()
closeStorageClient := client.CreateStorageClientWithCancel(&ctx, &storageClient)
Expand Down Expand Up @@ -125,3 +93,9 @@ func TestMain(m *testing.M) {
setup.CleanupDirectoryOnGCS(ctx, storageClient, path.Join(setup.TestBucket(), testDirName))
os.Exit(successCode)
}

func TestLocalFileTestSuite(t *testing.T) {
s := new(localFileTestSuite)
s.CommonLocalFileTestSuite.TestifySuite = &s.Suite
suite.Run(t, s)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// Provides integration tests for stat operation on local files.
package local_file_test
package local_file

import (
"os"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//
// Provides integration tests for symlink operation on local files.
package local_file_test
package local_file

import (
"os"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// Provides integration tests for operation on unlinked local files.
package local_file_test
package local_file

import (
"path"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// Provides integration tests for write on local files.
package local_file_test
package local_file

import (
. "github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/client"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

type defaultMountEmptyGCSFile struct {
defaultMountCommonTest
suite.Suite
}

func (t *defaultMountEmptyGCSFile) SetupTest() {
Expand All @@ -47,5 +48,7 @@ func (t *defaultMountEmptyGCSFile) createEmptyGCSFile() {

// Executes all tests that run with single streamingWrites configuration for empty GCS Files.
func TestDefaultMountEmptyGCSFileTest(t *testing.T) {
suite.Run(t, new(defaultMountEmptyGCSFile))
s := new(defaultMountEmptyGCSFile)
s.defaultMountCommonTest.TestifySuite = &s.Suite
suite.Run(t, s)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@
package streaming_writes

import (
"path"
"testing"

. "github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/local_file"
. "github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/client"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/setup"
"github.com/stretchr/testify/suite"
)

type defaultMountLocalFile struct {
defaultMountCommonTest
CommonLocalFileTestSuite
suite.Suite
}

func (t *defaultMountLocalFile) SetupTest() {
Expand All @@ -38,11 +40,13 @@ func (t *defaultMountLocalFile) SetupSubTest() {
func (t *defaultMountLocalFile) createLocalFile() {
t.fileName = FileName1 + setup.GenerateRandomString(5)
// Create a local file.
_, t.f1 = CreateLocalFileInTestDir(ctx, storageClient, testDirPath, t.fileName, t.T())
t.filePath = path.Join(testDirPath, t.fileName)
t.filePath, t.f1 = CreateLocalFileInTestDir(ctx, storageClient, testDirPath, t.fileName, t.T())
}

// Executes all tests that run with single streamingWrites configuration for localFiles.
func TestDefaultMountLocalFileTest(t *testing.T) {
suite.Run(t, new(defaultMountLocalFile))
s := new(defaultMountLocalFile)
s.CommonLocalFileTestSuite.TestifySuite = &s.Suite
s.defaultMountCommonTest.TestifySuite = &s.Suite
suite.Run(t, s)
}
12 changes: 9 additions & 3 deletions tools/integration_tests/streaming_writes/default_mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@ package streaming_writes
import (
"os"

. "github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/local_file"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/setup"
"github.com/stretchr/testify/suite"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/test_suite"
)

type defaultMountCommonTest struct {
f1 *os.File
fileName string
// filePath of the above file in the mounted directory.
filePath string
suite.Suite
test_suite.TestifySuite
}

func (t *defaultMountCommonTest) SetupSuite() {
flags := []string{"--enable-streaming-writes=true", "--write-block-size-mb=1", "--write-max-blocks-per-file=2"}
// TODO(mohitkyadav): Make these part of test suite after refactoring.
SetCtx(ctx)
SetStorageClient(storageClient)
SetTestDirName(testDirName)

flags := []string{"--rename-dir-limit=3", "--enable-streaming-writes=true", "--write-block-size-mb=1", "--write-max-blocks-per-file=2"}
setup.MountGCSFuseWithGivenMountFunc(flags, mountFunc)
testDirPath = setup.SetupTestDirectory(testDirName)
}
Expand Down
Loading

0 comments on commit 5db9e11

Please sign in to comment.