Skip to content

Commit

Permalink
Making operations test package parallell (#1858)
Browse files Browse the repository at this point in the history
* making operations tests directiry independent

* fix dir path

* fix dir path

* fix dir path

* fix dir path

* rebasing

* lint fix

* small fix

* review comments

* fix tests by writing file

* lint fix

* removing unnecessry changes

* nit
  • Loading branch information
Tulsishah committed Apr 30, 2024
1 parent 70d49e2 commit 5625d97
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 198 deletions.
65 changes: 28 additions & 37 deletions tools/integration_tests/operations/copy_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,20 @@ import (
// srcCopyDir -- Dir
// srcCopyDir/copy.txt -- File
// srcCopyDir/subSrcCopyDir -- Dir
func createSrcDirectoryWithObjects(dirPath string, t *testing.T) {
// Clean the mountedDirectory before running test.
setup.CleanMntDir()

func createSrcDirectoryWithObjects(dirPath string, t *testing.T) string {
// testBucket/srcCopyDir
err := os.Mkdir(dirPath, setup.FilePermission_0600)
if err != nil {
t.Errorf("Mkdir at %q: %v", dirPath, err)
return
return ""
}

// testBucket/subSrcCopyDir
subDirPath := path.Join(dirPath, SubSrcCopyDirectory)
err = os.Mkdir(subDirPath, setup.FilePermission_0600)
if err != nil {
t.Errorf("Mkdir at %q: %v", subDirPath, err)
return
return ""
}

// testBucket/srcCopyDir/copy.txt
Expand All @@ -63,6 +60,8 @@ func createSrcDirectoryWithObjects(dirPath string, t *testing.T) {

// Closing file at the end
defer operations.CloseFile(file)

return dirPath
}

func checkIfCopiedDirectoryHasCorrectData(destDir string, t *testing.T) {
Expand Down Expand Up @@ -109,11 +108,9 @@ func checkIfCopiedDirectoryHasCorrectData(destDir string, t *testing.T) {
// destCopyDir/copy.txt -- File
// destCopyDir/subSrcCopyDir -- Dir
func TestCopyDirectoryInNonExistingDirectory(t *testing.T) {
srcDir := path.Join(setup.MntDir(), SrcCopyDirectory)

createSrcDirectoryWithObjects(srcDir, t)

destDir := path.Join(setup.MntDir(), DestCopyDirectoryNotExist)
testDir := setup.SetupTestDirectory(DirForOperationTests)
srcDir := createSrcDirectoryWithObjects(path.Join(testDir, SrcCopyDirectory), t)
destDir := path.Join(testDir, DestCopyDirectoryNotExist)

err := operations.CopyDir(srcDir, destDir)
if err != nil {
Expand All @@ -133,13 +130,12 @@ func TestCopyDirectoryInNonExistingDirectory(t *testing.T) {
// destCopyDir/srcCopyDir/copy.txt -- File
// destCopyDir/srcCopyDir/subSrcCopyDir -- Dir
func TestCopyDirectoryInEmptyDirectory(t *testing.T) {
srcDir := path.Join(setup.MntDir(), SrcCopyDirectory)

createSrcDirectoryWithObjects(srcDir, t)
testDir := setup.SetupTestDirectory(DirForOperationTests)
srcDir := createSrcDirectoryWithObjects(path.Join(testDir, SrcCopyDirectory), t)

// Create below directory
// destCopyDir -- Dir
destDir := path.Join(setup.MntDir(), DestCopyDirectory)
destDir := path.Join(testDir, DestCopyDirectory)
err := os.Mkdir(destDir, setup.FilePermission_0600)
if err != nil {
t.Errorf("Error in creating directory: %v", err)
Expand Down Expand Up @@ -167,23 +163,22 @@ func TestCopyDirectoryInEmptyDirectory(t *testing.T) {
checkIfCopiedDirectoryHasCorrectData(destSrc, t)
}

func createDestNonEmptyDirectory(t *testing.T) {
destDir := path.Join(setup.MntDir(), DestNonEmptyCopyDirectory)
operations.CreateDirectoryWithNFiles(0, destDir, "", t)
func createDestNonEmptyDirectory(dirPath string, t *testing.T) string {
operations.CreateDirectoryWithNFiles(0, dirPath, "", t)

destSubDir := path.Join(destDir, SubDirInNonEmptyDestCopyDirectory)
destSubDir := path.Join(dirPath, SubDirInNonEmptyDestCopyDirectory)
operations.CreateDirectoryWithNFiles(0, destSubDir, "", t)

return dirPath
}

func TestCopyDirectoryInNonEmptyDirectory(t *testing.T) {
srcDir := path.Join(setup.MntDir(), SrcCopyDirectory)

createSrcDirectoryWithObjects(srcDir, t)
testDir := setup.SetupTestDirectory(DirForOperationTests)
srcDir := createSrcDirectoryWithObjects(path.Join(testDir, SrcCopyDirectory), t)

// Create below directory
// destCopyDir -- Dir
destDir := path.Join(setup.MntDir(), DestNonEmptyCopyDirectory)
createDestNonEmptyDirectory(t)
destDir := createDestNonEmptyDirectory(path.Join(testDir, DestNonEmptyCopyDirectory), t)

err := operations.CopyDir(srcDir, destDir)
if err != nil {
Expand Down Expand Up @@ -242,17 +237,15 @@ func checkIfCopiedEmptyDirectoryHasNoData(destSrc string, t *testing.T) {
// destNonEmptyCopyDirectory/subDirInNonEmptyDestCopyDirectory
// destNonEmptyCopyDirectory/emptySrcDirectoryCopyTest
func TestCopyEmptyDirectoryInNonEmptyDirectory(t *testing.T) {
// Clean the mountedDirectory before running test.
setup.CleanMntDir()
testDir := setup.SetupTestDirectory(DirForOperationTests)

srcDir := path.Join(setup.MntDir(), EmptySrcDirectoryCopyTest)
srcDir := path.Join(testDir, EmptySrcDirectoryCopyTest)
operations.CreateDirectoryWithNFiles(0, srcDir, "", t)

// Create below directory
// destNonEmptyCopyDirectory -- Dir
// destNonEmptyCopyDirectory/subDirInNonEmptyDestCopyDirectory -- Dir
destDir := path.Join(setup.MntDir(), DestNonEmptyCopyDirectory)
createDestNonEmptyDirectory(t)
destDir := createDestNonEmptyDirectory(path.Join(testDir, DestNonEmptyCopyDirectory), t)

err := operations.CopyDir(srcDir, destDir)
if err != nil {
Expand Down Expand Up @@ -297,15 +290,14 @@ func TestCopyEmptyDirectoryInNonEmptyDirectory(t *testing.T) {
// Output
// destEmptyCopyDirectory/emptySrcDirectoryCopyTest
func TestCopyEmptyDirectoryInEmptyDirectory(t *testing.T) {
// Clean the mountedDirectory before running test.
setup.CleanMntDir()
testDir := setup.SetupTestDirectory(DirForOperationTests)

srcDir := path.Join(setup.MntDir(), EmptySrcDirectoryCopyTest)
srcDir := path.Join(testDir, EmptySrcDirectoryCopyTest)
operations.CreateDirectoryWithNFiles(0, srcDir, "", t)

// Create below directory
// destCopyDir -- Dir
destDir := path.Join(setup.MntDir(), DestEmptyCopyDirectory)
destDir := path.Join(testDir, DestEmptyCopyDirectory)
operations.CreateDirectoryWithNFiles(0, destDir, "", t)

err := operations.CopyDir(srcDir, destDir)
Expand Down Expand Up @@ -342,14 +334,13 @@ func TestCopyEmptyDirectoryInEmptyDirectory(t *testing.T) {
// Output
// destCopyDirectoryNotExist
func TestCopyEmptyDirectoryInNonExistingDirectory(t *testing.T) {
// Clean the mountedDirectory before running test.
setup.CleanMntDir()
testDir := setup.SetupTestDirectory(DirForOperationTests)

srcDir := path.Join(setup.MntDir(), EmptySrcDirectoryCopyTest)
srcDir := path.Join(testDir, EmptySrcDirectoryCopyTest)
operations.CreateDirectoryWithNFiles(0, srcDir, "", t)

// destCopyDirectoryNotExist -- Dir
destDir := path.Join(setup.MntDir(), DestCopyDirectoryNotExist)
destDir := path.Join(testDir, DestCopyDirectoryNotExist)

_, err := os.Stat(destDir)
if err == nil {
Expand Down
9 changes: 5 additions & 4 deletions tools/integration_tests/operations/copy_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@
// limitations under the License.

// Provides integration tests for copy file.
package operations
package operations_test

import (
"os"
"path"
"testing"

"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/operations"
"github.com/googlecloudplatform/gcsfuse/v2/tools/integration_tests/util/setup"
)

func TestCopyFile(t *testing.T) {
// Clean the mountedDirectory before running test.
setup.CleanMntDir()
testDir := setup.SetupTestDirectory(DirForOperationTests)
fileName := path.Join(testDir, tempFileName)

fileName := setup.CreateTempFile()
operations.CreateFileWithContent(fileName, setup.FilePermission_0600, Content, t)

content, err := operations.ReadFile(fileName)
if err != nil {
Expand Down
25 changes: 12 additions & 13 deletions tools/integration_tests/operations/create_three_level_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ import (

func TestCreateThreeLevelDirectories(t *testing.T) {
// Directory structure
// testBucket/dirOneInCreateThreeLevelDirTest -- Dir
// testBucket/dirOneInCreateThreeLevelDirTest/dirTwoInCreateThreeLevelDirTest -- Dir
// testBucket/dirOneInCreateThreeLevelDirTest/dirTwoInCreateThreeLevelDirTest/dirThreeInCreateThreeLevelDirTest -- Dir
// testBucket/dirOneInCreateThreeLevelDirTest/dirTwoInCreateThreeLevelDirTest/dirThreeInCreateThreeLevelDirTest/fileInDirThreeInCreateThreeLevelDirTest -- File
// testBucket/dirForOperationTests/dirOneInCreateThreeLevelDirTest -- Dir
// testBucket/dirForOperationTests/dirOneInCreateThreeLevelDirTest/dirTwoInCreateThreeLevelDirTest -- Dir
// testBucket/dirForOperationTests/dirOneInCreateThreeLevelDirTest/dirTwoInCreateThreeLevelDirTest/dirThreeInCreateThreeLevelDirTest -- Dir
// testBucket/dirForOperationTests/dirOneInCreateThreeLevelDirTest/dirTwoInCreateThreeLevelDirTest/dirThreeInCreateThreeLevelDirTest/fileInDirThreeInCreateThreeLevelDirTest -- File

// Clean the mountedDirectory before running test.
setup.CleanMntDir()
testDir := setup.SetupTestDirectory(DirForOperationTests)

dirPath := path.Join(setup.MntDir(), DirOneInCreateThreeLevelDirTest)
dirPath := path.Join(testDir, DirOneInCreateThreeLevelDirTest)

operations.CreateDirectoryWithNFiles(0, dirPath, "", t)

Expand All @@ -56,7 +55,7 @@ func TestCreateThreeLevelDirectories(t *testing.T) {
}

// Recursively walk into directory and test.
err = filepath.WalkDir(setup.MntDir(), func(dirPath string, dir fs.DirEntry, err error) error {
err = filepath.WalkDir(testDir, func(dirPath string, dir fs.DirEntry, err error) error {
if err != nil {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", dirPath, err)
return err
Expand All @@ -73,13 +72,13 @@ func TestCreateThreeLevelDirectories(t *testing.T) {
}

// Check if mntDir has correct objects.
if dirPath == setup.MntDir() {
if dirPath == testDir {
// numberOfObjects - 1
if len(objs) != NumberOfObjectsInBucketDirectoryCreateTest {
t.Errorf("Incorrect number of objects in the bucket.")
}

// testBucket/dirOneInCreateThreeLevelDirTest -- Dir
// testBucket/dirForOperationTests/dirOneInCreateThreeLevelDirTest -- Dir
if objs[0].Name() != DirOneInCreateThreeLevelDirTest || objs[0].IsDir() != true {
t.Errorf("Directory is not created.")
}
Expand All @@ -92,7 +91,7 @@ func TestCreateThreeLevelDirectories(t *testing.T) {
t.Errorf("Incorrect number of objects in the dirOneInCreateThreeLevelDirTest.")
}

// testBucket/dirOneInCreateThreeLevelDirTest/dirTwoInCreateThreeLevelDirTest -- Dir
// testBucket/dirForOperationTests/dirOneInCreateThreeLevelDirTest/dirTwoInCreateThreeLevelDirTest -- Dir
if objs[0].Name() != DirTwoInCreateThreeLevelDirTest || objs[0].IsDir() != true {
t.Errorf("Directory is not created.")
}
Expand All @@ -106,7 +105,7 @@ func TestCreateThreeLevelDirectories(t *testing.T) {
t.Errorf("Incorrect number of objects in the dirTwoInCreateThreeLevelDirTest.")
}

// testBucket/dirOneInCreateThreeLevelDirTest/dirTwoInCreateThreeLevelDirTest/dirThreeInCreateThreeLevelDirTest -- Dir
// testBucket/dirForOperationTests/dirOneInCreateThreeLevelDirTest/dirTwoInCreateThreeLevelDirTest/dirThreeInCreateThreeLevelDirTest -- Dir
if objs[0].Name() != DirThreeInCreateThreeLevelDirTest || objs[0].IsDir() != true {
t.Errorf("Directory is not created.")
}
Expand All @@ -120,7 +119,7 @@ func TestCreateThreeLevelDirectories(t *testing.T) {
t.Errorf("Incorrect number of objects in the dirThreeInCreateThreeLevelDirTest.")
}

// testBucket/dirOneInCreateThreeLevelDirTest/dirTwoInCreateThreeLevelDirTest/dirThreeInCreateThreeLevelDirTest/fileInDirThreeInCreateThreeLevelDirTest -- File
// testBucket/dirForOperationTests/dirOneInCreateThreeLevelDirTest/dirTwoInCreateThreeLevelDirTest/dirThreeInCreateThreeLevelDirTest/fileInDirThreeInCreateThreeLevelDirTest -- File
if objs[0].Name() != FileInDirThreeInCreateThreeLevelDirTest || objs[0].IsDir() != false {
t.Errorf("Incorrect object exist in the dirThreeInCreateThreeLevelDirTest directory.")
}
Expand Down
10 changes: 4 additions & 6 deletions tools/integration_tests/operations/delete_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ import (
)

func TestDeleteEmptyExplicitDir(t *testing.T) {
// Clean the mountedDirectory before running test.
setup.CleanMntDir()
testDir := setup.SetupTestDirectory(DirForOperationTests)

dirPath := path.Join(setup.MntDir(), EmptyExplicitDirectoryForDeleteTest)
dirPath := path.Join(testDir, EmptyExplicitDirectoryForDeleteTest)
operations.CreateDirectoryWithNFiles(0, dirPath, "", t)

err := os.RemoveAll(dirPath)
Expand All @@ -43,10 +42,9 @@ func TestDeleteEmptyExplicitDir(t *testing.T) {
}

func TestDeleteNonEmptyExplicitDir(t *testing.T) {
// Clean the mountedDirectory before running tests.
setup.CleanMntDir()
testDir := setup.SetupTestDirectory(DirForOperationTests)

dirPath := path.Join(setup.MntDir(), NonEmptyExplicitDirectoryForDeleteTest)
dirPath := path.Join(testDir, NonEmptyExplicitDirectoryForDeleteTest)
operations.CreateDirectoryWithNFiles(NumberOfFilesInNonEmptyExplicitDirectoryForDeleteTest, dirPath, PrefixFilesInNonEmptyExplicitDirectoryForDeleteTest, t)

subDirPath := path.Join(dirPath, NonEmptyExplicitSubDirectoryForDeleteTest)
Expand Down
12 changes: 5 additions & 7 deletions tools/integration_tests/operations/delete_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// Provides integration tests for delete files.
package operations
package operations_test

import (
"os"
Expand Down Expand Up @@ -53,10 +53,9 @@ func createFile(filePath string, t *testing.T) {

// Remove testBucket/A.txt
func TestDeleteFileFromBucket(t *testing.T) {
// Clean the mountedDirectory before running test.
setup.CleanMntDir()
testDir := setup.SetupTestDirectory(DirForOperationTests)

filePath := path.Join(setup.MntDir(), FileNameInTestBucket)
filePath := path.Join(testDir, FileNameInTestBucket)

createFile(filePath, t)

Expand All @@ -65,10 +64,9 @@ func TestDeleteFileFromBucket(t *testing.T) {

// Remove testBucket/A/a.txt
func TestDeleteFileFromBucketDirectory(t *testing.T) {
// Clean the mountedDirectory before running test.
setup.CleanMntDir()
testDir := setup.SetupTestDirectory(DirForOperationTests)

dirPath := path.Join(setup.MntDir(), DirNameInTestBucket)
dirPath := path.Join(testDir, DirNameInTestBucket)
err := os.Mkdir(dirPath, setup.FilePermission_0600)
if err != nil {
t.Errorf("Error in creating directory: %v", err)
Expand Down
20 changes: 9 additions & 11 deletions tools/integration_tests/operations/file_and_dir_attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func checkIfObjectAttrIsCorrect(objName string, preCreateTime time.Time, postCre
if err != nil {
t.Errorf("os.Stat error: %s, %v", objName, err)
}
statObjName := path.Join(setup.MntDir(), oStat.Name())
statObjName := path.Join(setup.MntDir(), DirForOperationTests, oStat.Name())
if objName != statObjName {
t.Errorf("File name not matched in os.Stat, found: %s, expected: %s", statObjName, objName)
}
Expand All @@ -51,13 +51,13 @@ func checkIfObjectAttrIsCorrect(objName string, preCreateTime time.Time, postCre
}

func TestFileAttributes(t *testing.T) {
// Clean the mountedDirectory before running test.
setup.CleanMntDir()
testDir := setup.SetupTestDirectory(DirForOperationTests)

// kernel time can be slightly out of sync of time.Now(), so rounding off
// times to seconds. Ref: https://github.com/golang/go/issues/33510
preCreateTime := time.Now().Round(time.Second)
fileName := setup.CreateTempFile()
fileName := path.Join(testDir, tempFileName)
operations.CreateFileWithContent(fileName, setup.FilePermission_0600, Content, t)
postCreateTime := time.Now().Round(time.Second)

// The file size in createTempFile() is BytesWrittenInFile bytes
Expand All @@ -66,27 +66,25 @@ func TestFileAttributes(t *testing.T) {
}

func TestEmptyDirAttributes(t *testing.T) {
// Clean the mountedDirectory before running test.
setup.CleanMntDir()
testDir := setup.SetupTestDirectory(DirForOperationTests)

// kernel time can be slightly out of sync of time.Now(), so rounding off
// times to seconds. Ref: https://github.com/golang/go/issues/33510
preCreateTime := time.Now().Round(time.Second)
dirName := path.Join(setup.MntDir(), DirAttrTest)
dirName := path.Join(testDir, DirAttrTest)
operations.CreateDirectoryWithNFiles(0, dirName, "", t)
postCreateTime := time.Now().Round(time.Second)

checkIfObjectAttrIsCorrect(dirName, preCreateTime, postCreateTime, 0, t)
checkIfObjectAttrIsCorrect(path.Join(testDir, DirAttrTest), preCreateTime, postCreateTime, 0, t)
}

func TestNonEmptyDirAttributes(t *testing.T) {
// Clean the mountedDirectory before running test.
setup.CleanMntDir()
testDir := setup.SetupTestDirectory(DirForOperationTests)

// kernel time can be slightly out of sync of time.Now(), so rounding off
// times to seconds. Ref: https://github.com/golang/go/issues/33510
preCreateTime := time.Now().Round(time.Second)
dirName := path.Join(setup.MntDir(), DirAttrTest)
dirName := path.Join(testDir, DirAttrTest)
operations.CreateDirectoryWithNFiles(NumberOfFilesInDirAttrTest, dirName, PrefixFileInDirAttrTest, t)
postCreateTime := time.Now().Round(time.Second)

Expand Down

0 comments on commit 5625d97

Please sign in to comment.