Skip to content

Commit

Permalink
Copy directory integration tests (#1131)
Browse files Browse the repository at this point in the history
* copy directory tests

* copy directory tests

* adding comment

* adding comment

* adding new test case

* updating comment

* updating comment
  • Loading branch information
Tulsishah committed May 19, 2023
1 parent d3a8bdc commit bce784a
Show file tree
Hide file tree
Showing 2 changed files with 235 additions and 0 deletions.
225 changes: 225 additions & 0 deletions tools/integration_tests/operations/copy_dir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
// Copyright 2023 Google Inc. All Rights Reserved.
//
// 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.

// Provides integration tests for copy directory.
package operations_test

import (
"log"
"os"
"path"
"testing"

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

// Create below directory structure.
// srcCopyDir -- Dir
// srcCopyDir/copy.txt -- File
// srcCopyDir/subSrcCopyDir -- Dir
func createSrcDirectoryWithObjects(dirPath string, t *testing.T) {
// testBucket/srcCopyDir
err := os.Mkdir(dirPath, setup.FilePermission_0600)
if err != nil {
t.Errorf("Mkdir at %q: %v", dirPath, err)
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
}

// testBucket/srcCopyDir/copy.txt
filePath := path.Join(dirPath, SrcCopyFile)

file, err := os.Create(filePath)
if err != nil {
t.Errorf("Error in creating file %v:", err)
}

err = operations.WriteFile(file.Name(), SrcCopyFileContent)
if err != nil {
t.Errorf("File at %v", err)
}
}

func checkIfCopiedDirectoryHasCorrectData(destDir string, t *testing.T) {
obj, err := os.ReadDir(destDir)
if err != nil {
log.Fatal(err)
}

// Comparing number of objects in the testBucket - 2
if len(obj) != NumberOfObjectsInSrcCopyDirectory {
t.Errorf("The number of objects in the current directory doesn't match.")
return
}

// Comparing first object name and type
// Name - testBucket/destCopyDir/copy.txt, Type - file
if obj[0].Name() != SrcCopyFile || obj[0].IsDir() == true {
t.Errorf("Object Listed for bucket directory is incorrect.")
}

// Comparing second object name and type
// Name - testBucket/destCopyDir/srcCopyDir, Type - dir
if obj[1].Name() != SubSrcCopyDirectory || obj[1].IsDir() != true {
t.Errorf("Object Listed for bucket directory is incorrect.")
}

destFile := path.Join(destDir, SrcCopyFile)

content, err := operations.ReadFile(destFile)
if err != nil {
t.Errorf("ReadAll: %v", err)
}
if got, want := string(content), SrcCopyFileContent; got != want {
t.Errorf("File content %q not match %q", got, want)
}
}

// Copy SrcDirectory objects in DestDirectory
// srcCopyDir -- Dir
// srcCopyDir/copy.txt -- File
// srcCopyDir/subSrcCopyDir -- Dir

// destCopyDir -- Dir
// 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)

err := operations.CopyDir(srcDir, destDir)
if err != nil {
t.Errorf("Error in copying directory: %v", err)
}

checkIfCopiedDirectoryHasCorrectData(destDir, t)

os.RemoveAll(srcDir)
os.RemoveAll(destDir)
}

// Copy SrcDirectory in DestDirectory
// srcCopyDir -- Dir
// srcCopyDir/copy.txt -- File
// srcCopyDir/subSrcCopyDir -- Dir

// destCopyDir -- Dir
// destCopyDir/srcCopyDir -- Dir
// destCopyDir/srcCopyDir/copy.txt -- File
// destCopyDir/srcCopyDir/subSrcCopyDir -- Dir
func TestCopyDirectoryInEmptyDirectory(t *testing.T) {
srcDir := path.Join(setup.MntDir(), SrcCopyDirectory)

createSrcDirectoryWithObjects(srcDir, t)

// Create below directory
// destCopyDir -- Dir
destDir := path.Join(setup.MntDir(), DestCopyDirectory)
err := os.Mkdir(destDir, setup.FilePermission_0600)
if err != nil {
t.Errorf("Error in creating directory: %v", err)
}

err = operations.CopyDir(srcDir, destDir)
if err != nil {
t.Errorf("Error in copying directory: %v", err)
}

obj, err := os.ReadDir(destDir)
if err != nil {
log.Fatal(err)
}

// Check if destCopyDirectory has the correct directory copied.
// destCopyDirectory
// destCopyDirectory/srcCopyDirectory
if len(obj) != 1 || obj[0].Name() != SrcCopyDirectory || obj[0].IsDir() != true {
t.Errorf("Error in copying directory.")
return
}

destSrc := path.Join(destDir, SrcCopyDirectory)
checkIfCopiedDirectoryHasCorrectData(destSrc, t)

os.RemoveAll(srcDir)
os.RemoveAll(destDir)
}

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

createSrcDirectoryWithObjects(srcDir, t)

// Create below directory
// destCopyDir -- Dir
destDir := path.Join(setup.MntDir(), DestNonEmptyCopyDirectory)
err := os.Mkdir(destDir, setup.FilePermission_0600)
if err != nil {
t.Errorf("Error in creating directory: %v", err)
}

destSubDir := path.Join(destDir, SubDirInNonEmptyDestCopyDirectory)
err = os.Mkdir(destSubDir, setup.FilePermission_0600)
if err != nil {
t.Errorf("Error in creating directory: %v", err)
}

err = operations.CopyDir(srcDir, destDir)
if err != nil {
t.Errorf("Error in copying directory: %v", err)
}

obj, err := os.ReadDir(destDir)
if err != nil {
log.Fatal(err)
}

// Check if destCopyDirectory has the correct directory copied.
// destCopyDirectory
// destCopyDirectory/srcCopyDirectory
// destCopyDirectory/subDestCopyDirectory
if len(obj) != NumberOfObjectsInDestCopyDirectory {
t.Errorf("The number of objects in the current directory doesn't match.")
return
}

// destCopyDirectory/srcCopyDirectory - Dir
if obj[0].Name() != SrcCopyDirectory || obj[0].IsDir() != true {
t.Errorf("Error in copying directory.")
return
}

// destCopyDirectory/subDirInNonEmptyDestCopyDirectory - Dir
if obj[1].Name() != SubDirInNonEmptyDestCopyDirectory || obj[1].IsDir() != true {
t.Errorf("Existing object affected.")
return
}

destSrc := path.Join(destDir, SrcCopyDirectory)
checkIfCopiedDirectoryHasCorrectData(destSrc, t)

os.RemoveAll(srcDir)
os.RemoveAll(destDir)
}
10 changes: 10 additions & 0 deletions tools/integration_tests/operations/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ import (

const MoveFile = "move.txt"
const MoveFileContent = "This is from move file in Test directory.\n"
const SrcCopyDirectory = "srcCopyDir"
const SubSrcCopyDirectory = "subSrcCopyDir"
const SrcCopyFile = "copy.txt"
const SrcCopyFileContent = "This is from copy file in srcCopy directory.\n"
const DestCopyDirectory = "destCopyDir"
const DestNonEmptyCopyDirectory = "destNonEmptyCopyDirectory"
const SubDirInNonEmptyDestCopyDirectory = "subDestCopyDir"
const DestCopyDirectoryNotExist = "notExist"
const NumberOfObjectsInSrcCopyDirectory = 2
const NumberOfObjectsInDestCopyDirectory = 2

func TestMain(m *testing.M) {
setup.ParseSetUpFlags()
Expand Down

0 comments on commit bce784a

Please sign in to comment.