Skip to content

Commit

Permalink
Readonly Integration Tests - Create, Delete, List (#1067)
Browse files Browse the repository at this point in the history
* adding list, delete and create object tests

* adding more test cases

* small fix

* updating variable name

* updating file name

* adding more tests for create

* fixing lint test

* small fix

* local test

* revert local changes

* adding clear bucket

* adding extra line

* fixing bucket argument

* fixing comments

* updating path with path.join

* changing function name

* making variable for bucket objects

* small fix

* adding comment for directory structure

* replace with variable

* lint test

* lint test
  • Loading branch information
Tulsishah authored Apr 21, 2023
1 parent da0799f commit 9cec8d4
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 1 deletion.
98 changes: 98 additions & 0 deletions tools/integration_tests/readonly/create_and_delete_object_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// 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 file operations with --o=ro flag set.
package readonly_test

import (
"io/fs"
"os"
"path"
"testing"

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

func ensureFileSystemLockedForFileCreation(filePath string, t *testing.T) {
file, err := os.OpenFile(filePath, os.O_CREATE, setup.FilePermission_0600)

// It will throw an error read-only file system or permission denied.
if err == nil {
t.Errorf("File is created in read-only file system.")
}

defer file.Close()
}

func TestCreateFile(t *testing.T) {
filePath := path.Join(setup.MntDir(), "testFile.txt")
ensureFileSystemLockedForFileCreation(filePath, t)
}

func TestCreateFileInSubDirectory(t *testing.T) {
filePath := path.Join(setup.MntDir(), DirectoryNameInTestBucket, "testFile.txt")
ensureFileSystemLockedForFileCreation(filePath, t)
}

func ensureFileSystemLockedForDirCreation(dirPath string, t *testing.T) {
err := os.Mkdir(dirPath, fs.ModeDir)

// It will throw an error read-only file system or permission denied.
if err == nil {
t.Errorf("Directory is created in read-only file system.")
}
}

func TestCreateDir(t *testing.T) {
dirPath := path.Join(setup.MntDir(), "test")
ensureFileSystemLockedForDirCreation(dirPath, t)
}

func TestCreateDirInSubDirectory(t *testing.T) {
dirPath := path.Join(setup.MntDir(), DirectoryNameInTestBucket, "test")
ensureFileSystemLockedForDirCreation(dirPath, t)
}

func ensureFileSystemLockedForDeletion(objPath string, t *testing.T) {
err := os.RemoveAll(objPath)

// It will throw an error read-only file system or permission denied.
if err == nil {
t.Errorf("Objects are deleted in read-only file system.")
}
}

func TestDeleteDir(t *testing.T) {
objPath := path.Join(setup.MntDir(), DirectoryNameInTestBucket)
ensureFileSystemLockedForDeletion(objPath, t)
}

func TestDeleteFile(t *testing.T) {
objPath := path.Join(setup.MntDir(), FileNameInTestBucket)
ensureFileSystemLockedForDeletion(objPath, t)
}

func TestDeleteSubDirectory(t *testing.T) {
objPath := path.Join(setup.MntDir(), DirectoryNameInTestBucket, SubDirectoryNameInTestBucket)
ensureFileSystemLockedForDeletion(objPath, t)
}

func TestDeleteFileInSubDirectory(t *testing.T) {
objPath := path.Join(setup.MntDir(), DirectoryNameInTestBucket, FileInSubDirectoryNameInTestBucket)
ensureFileSystemLockedForDeletion(objPath, t)
}

func TestDeleteAllObjectsInBucket(t *testing.T) {
ensureFileSystemLockedForDeletion(setup.MntDir(), t)
}
74 changes: 74 additions & 0 deletions tools/integration_tests/readonly/list_objects_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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 file operations with --o=ro flag set.
package readonly_test

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

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

func TestListObjectsInBucket(t *testing.T) {
// ** Directory structure **
// testBucket
// testBucket/Test -- Dir
// testBucket/Test1.txt -- File

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

if len(obj) != NumberOfObjectsInTestBucket {
t.Errorf("The number of objects in the current directory doesn't match.")
}

if obj[0].Name() != DirectoryNameInTestBucket && obj[0].IsDir() != true {
t.Errorf("Listed object is incorrect.")
}

if obj[1].Name() != FileNameInTestBucket && obj[1].IsDir() != false {
t.Errorf("Listed object is incorrect.")
}
}

func TestListObjectsInBucketSubDirectory(t *testing.T) {
// ** SubDirectory structure **
// testBucket/Test
// testBucket/Test/a.txt -- File
// testBucket/Test/b/ -- Dir

Dir := path.Join(setup.MntDir(), DirectoryNameInTestBucket)
obj, err := os.ReadDir(Dir)
if err != nil {
log.Fatal(err)
}

if len(obj) != NumberOfObjectsInTestBucketSubDirectory {
t.Errorf("The number of objects in the current directory doesn't match.")
}

if obj[0].Name() != FileInSubDirectoryNameInTestBucket && obj[0].IsDir() != false {
t.Errorf("Listed object is incorrect.")
}

if obj[1].Name() != SubDirectoryNameInTestBucket && obj[1].IsDir() != true {
t.Errorf("Listed object is incorrect.")
}
}
12 changes: 11 additions & 1 deletion tools/integration_tests/readonly/readonly_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Google Inc. All Rights Reserved.
// 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.
Expand All @@ -23,6 +23,13 @@ import (
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/setup"
)

const DirectoryNameInTestBucket = "Test" // testBucket/Test
const FileNameInTestBucket = "Test1.txt" // testBucket/Test1.txt
const SubDirectoryNameInTestBucket = "b" // testBucket/Test/b
const FileInSubDirectoryNameInTestBucket = "a.txt" // testBucket/Test/a.txt
const NumberOfObjectsInTestBucket = 2
const NumberOfObjectsInTestBucketSubDirectory = 2

// Run shell script
func runScriptForTestData(script string, testBucket string) {
cmd := exec.Command("/bin/bash", script, testBucket)
Expand All @@ -37,6 +44,9 @@ func TestMain(m *testing.M) {

flags := [][]string{{"--o=ro", "--implicit-dirs=true"}, {"--file-mode=544", "--dir-mode=544", "--implicit-dirs=true"}}

// Clean the bucket for readonly testing.
runScriptForTestData("testdata/delete_objects.sh", setup.TestBucket())

// Create objects in bucket for testing.
runScriptForTestData("testdata/create_objects.sh", setup.TestBucket())

Expand Down
6 changes: 6 additions & 0 deletions tools/integration_tests/readonly/testdata/delete_objects.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# Here $1 refers to the testBucket argument
gsutil rm -a gs://$1/**

# If bucket is empty it will throw an CommandException.
if [ $? -eq 1 ]; then
echo "Bucket is already empty."
exit 0
fi

0 comments on commit 9cec8d4

Please sign in to comment.