Skip to content

Commit

Permalink
Dynamic mounting integration tests (#1242)
Browse files Browse the repository at this point in the history
* testing

* testing

* adding dynamic mounting testing

* testing

* testing

* separating unmounting

* small fix

* unblocking remaning tests

* small fix(updating echo message)

* fixing lint tests and adding comments

* updating file path

* small fix

* testing

* testing

* testing

* unblocking code for testing

* testing

* increasing time as taking more than 10m to run

* adding random suffix at the end of bucket name as with fixed bucket it can fail when multiple instance run inetgration tests at a time

* assigning it to variable

* small fix

* small fix

* updating const var name

* testing

* testing

* unblocking code

* updating comment
  • Loading branch information
Tulsishah authored Jul 26, 2023
1 parent 15448be commit 2d7fc6e
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 10 deletions.
2 changes: 1 addition & 1 deletion perfmetrics/scripts/continuous_test/gcp_ubuntu/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ commitId=$(git log --before='yesterday 23:59:59' --max-count=1 --pretty=%H)
git checkout $commitId

echo "Executing integration tests"
GODEBUG=asyncpreemptoff=1 CGO_ENABLED=0 go test ./tools/integration_tests/... -p 1 --integrationTest -v --testbucket=gcsfuse-integration-test
GODEBUG=asyncpreemptoff=1 CGO_ENABLED=0 go test ./tools/integration_tests/... -p 1 --integrationTest -v --testbucket=gcsfuse-integration-test -timeout 15m

# Checkout back to master branch to use latest CI test scripts in master.
git checkout master
Expand Down
2 changes: 1 addition & 1 deletion perfmetrics/scripts/presubmit_test/pr_perf_test/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ echo checkout PR branch
git checkout pr/$KOKORO_GITHUB_PULL_REQUEST_NUMBER

# Executing integration tests
GODEBUG=asyncpreemptoff=1 CGO_ENABLED=0 go test ./tools/integration_tests/... -p 1 --integrationTest -v --testbucket=gcsfuse-integration-test
GODEBUG=asyncpreemptoff=1 CGO_ENABLED=0 go test ./tools/integration_tests/... -p 1 --integrationTest -v --testbucket=gcsfuse-integration-test -timeout 15m

# Executing perf tests
echo Mounting gcs bucket from pr branch
Expand Down
5 changes: 5 additions & 0 deletions tools/integration_tests/operations/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/creds_tests"
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/mounting/dynamic_mounting"
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/mounting/only_dir_mounting"
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/mounting/persistent_mounting"
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/mounting/static_mounting"
Expand Down Expand Up @@ -113,6 +114,10 @@ func TestMain(m *testing.M) {
successCode = persistent_mounting.RunTests(flags, m)
}

if successCode == 0 {
successCode = dynamic_mounting.RunTests(flags, m)
}

if successCode == 0 {
// Test for admin permission on test bucket.
successCode = creds_tests.RunTestsForKeyFileAndGoogleApplicationCredentialsEnvVarSet(flags, "objectAdmin", m)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//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.

package dynamic_mounting

import (
"fmt"
"log"
"math/rand"
"path"
"testing"
"time"

"cloud.google.com/go/compute/metadata"
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/mounting"
"github.com/googlecloudplatform/gcsfuse/tools/integration_tests/util/setup"
)

const PrefixBucketForDynamicMountingTest = "gcsfuse-dynamic-mounting-test-"
const Charset = "abcdefghijklmnopqrstuvwxyz0123456789"

var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
var testBucketForDynamicMounting = PrefixBucketForDynamicMountingTest + generateRandomString(5)

func mountGcsfuseWithDynamicMounting(flags []string) (err error) {
defaultArg := []string{"--debug_gcs",
"--debug_fs",
"--debug_fuse",
"--log-file=" + setup.LogFile(),
"--log-format=text",
setup.MntDir()}

for i := 0; i < len(defaultArg); i++ {
flags = append(flags, defaultArg[i])
}

err = mounting.MountGcsfuse(setup.BinFile(), flags)

return err
}

func runTestsOnGivenMountedTestBucket(bucketName string, flags [][]string, rootMntDir string, m *testing.M) (successCode int) {
for i := 0; i < len(flags); i++ {
if err := mountGcsfuseWithDynamicMounting(flags[i]); err != nil {
setup.LogAndExit(fmt.Sprintf("mountGcsfuse: %v\n", err))
}

// Changing mntDir to path of bucket mounted in mntDir for testing.
mntDirOfTestBucket := path.Join(setup.MntDir(), bucketName)

setup.SetMntDir(mntDirOfTestBucket)

// Running tests on flags.
successCode = setup.ExecuteTest(m)

// Currently mntDir is mntDir/bucketName.
// Unmounting can happen on rootMntDir. Changing mntDir to rootMntDir for unmounting.
setup.SetMntDir(rootMntDir)
setup.UnMountAndThrowErrorInFailure(flags[i], successCode)
}
return
}

func executeTestsForDynamicMounting(flags [][]string, m *testing.M) (successCode int) {
rootMntDir := setup.MntDir()

// In dynamic mounting all the buckets mounted in mntDir which user has permission.
// mntDir - bucket1, bucket2, bucket3, ...
// We will test on passed testBucket and one created bucket.

// Test on testBucket
successCode = runTestsOnGivenMountedTestBucket(setup.TestBucket(), flags, rootMntDir, m)

// Test on created bucket.
if successCode == 0 {
successCode = runTestsOnGivenMountedTestBucket(testBucketForDynamicMounting, flags, rootMntDir, m)
}

// Setting back the original mntDir after testing.
setup.SetMntDir(rootMntDir)
return
}

func generateRandomString(length int) string {
b := make([]byte, length)
for i := range b {
b[i] = Charset[seededRand.Intn(len(Charset))]
}
return string(b)
}

func RunTests(flags [][]string, m *testing.M) (successCode int) {
project_id, err := metadata.ProjectID()
if err != nil {
log.Printf("Error in fetching project id: %v", err)
}

// Create bucket with name gcsfuse-dynamic-mounting-test-xxxxx
setup.RunScriptForTestData("../util/mounting/dynamic_mounting/testdata/create_bucket.sh", testBucketForDynamicMounting, project_id)

successCode = executeTestsForDynamicMounting(flags, m)

log.Printf("Test log: %s\n", setup.LogFile())

// Deleting bucket after testing.
setup.RunScriptForTestData("../util/mounting/dynamic_mounting/testdata/delete_bucket.sh", testBucketForDynamicMounting)

return successCode
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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.
# Create bucket for testing.

BUCKET_NAME=$1
PROJECT_ID=$2
gcloud storage buckets create gs://$BUCKET_NAME --project=$PROJECT_ID --location=us-west1 --uniform-bucket-level-access 2> ~/output.txt
if [ $? -eq 1 ]; then
if grep "HTTPError 409" ~/output.txt; then
echo "Bucket already exist."
rm ~/output.txt
else
rm ~/output.txt
exit 1
fi
fi
rm ~/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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.
# Delete bucket after testing.

BUCKET_NAME=$1

gcloud storage rm --recursive gs://$BUCKET_NAME/
19 changes: 11 additions & 8 deletions tools/integration_tests/util/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,14 @@ func UnMount() error {
return nil
}

func executeTest(m *testing.M) (successCode int) {
func ExecuteTest(m *testing.M) (successCode int) {
successCode = m.Run()

return successCode
}

func ExecuteTestForFlagsSet(flags []string, m *testing.M) (successCode int) {
var err error

successCode = executeTest(m)

err = UnMount()
func UnMountAndThrowErrorInFailure(flags []string, successCode int) {
err := UnMount()
if err != nil {
LogAndExit(fmt.Sprintf("Error in unmounting bucket: %v", err))
}
Expand All @@ -217,6 +213,13 @@ func ExecuteTestForFlagsSet(flags []string, m *testing.M) (successCode int) {
log.Print("Test Fails on " + f)
return
}
}

func ExecuteTestForFlagsSet(flags []string, m *testing.M) (successCode int) {
successCode = ExecuteTest(m)

UnMountAndThrowErrorInFailure(flags, successCode)

return
}

Expand All @@ -242,7 +245,7 @@ func RunTestsForMountedDirectoryFlag(m *testing.M) {
// Execute tests for the mounted directory.
if *mountedDirectory != "" {
mntDir = *mountedDirectory
successCode := executeTest(m)
successCode := ExecuteTest(m)
os.Exit(successCode)
}
}
Expand Down

0 comments on commit 2d7fc6e

Please sign in to comment.