Skip to content

Commit

Permalink
Added unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmeenkaur committed Jun 19, 2023
1 parent 1fb8534 commit 9e025b4
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/fs/fs.go
Expand Up @@ -1375,6 +1375,18 @@ func (fs *fileSystem) CreateSymlink(

// LOCKS_EXCLUDED(fs.mu)
func (fs *fileSystem) RmDir(
// When rm -r or os.RemoveAll call is made, the following calls are made in order
// 1. RmDir (only in the case of os.RemoveAll)
// 2. Unlink all nested files,
// 3. lookupInode call on implicit directory
// 4. Rmdir on the directory.
//
// When type cache ttl is set, we construct an implicitDir even though one doesn't
// exist on GCS (https://github.com/GoogleCloudPlatform/gcsfuse/blob/master/internal/fs/inode/dir.go#L452),
// and thus, we get rmDir call to GCSFuse.
// Whereas when ttl is zero, lookupInode call itself fails and RmDir is not called
// because object is not present in GCS.

ctx context.Context,
op *fuseops.RmDirOp) (err error) {
// Find the parent.
Expand Down
105 changes: 105 additions & 0 deletions internal/fs/implicit_dirs_with_cache_test.go
@@ -0,0 +1,105 @@
// Copyright 2015 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.

// A collection of tests for a file system where we do not attempt to write to
// the file system at all. Rather we set up contents in a GCS bucket out of
// band, wait for them to be available, and then read them via the file system.

package fs_test

import (
"os"
"os/exec"
"path"
"time"

. "github.com/jacobsa/oglematchers"
. "github.com/jacobsa/ogletest"
)

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

type ImplicitDirsWithCacheTest struct {
fsTest
}

func init() {
RegisterTestSuite(&ImplicitDirsWithCacheTest{})
}

func (t *ImplicitDirsWithCacheTest) SetUpTestSuite() {
t.serverCfg.ImplicitDirectories = true
t.serverCfg.DirTypeCacheTTL = time.Minute * 3
t.fsTest.SetUpTestSuite()
}

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

func (t *ImplicitDirsWithCacheTest) TestRemoveAll() {
// Create an implicit directory.
AssertEq(
nil,
t.createObjects(
map[string]string{
// File
"foo/bar": "",
}))

// Attempt to recursively remove implicit dir.
cmd := exec.Command(
"rm",
"-r",
path.Join(mntDir, "foo/"),
)
output, err := cmd.CombinedOutput()
AssertEq("", string(output))
AssertEq(nil, err)
}

func (t *ImplicitDirsWithCacheTest) TestRenameImplicitDir() {
// Create an implicit directory.
AssertEq(
nil,
t.createObjects(
map[string]string{
// File
"foo/bar1": "",
"foo/bar2": "",
}))

// Attempt to rename implicit dir.
err := os.Rename(
path.Join(mntDir, "foo/"),
path.Join(mntDir, "fooNew/"),
)
AssertEq(nil, err)

// verify the renamed files and directories
fi1, err := os.Stat(path.Join(mntDir, "fooNew"))
AssertEq(nil, err)
AssertTrue(fi1.IsDir())
fi2, err := os.Stat(path.Join(mntDir, "fooNew/bar1"))
AssertEq(nil, err)
AssertEq(fi2.Name(), "bar1")
fi3, err := os.Stat(path.Join(mntDir, "fooNew/bar2"))
AssertEq(nil, err)
AssertEq(fi3.Name(), "bar2")
fi4, err := os.Stat(path.Join(mntDir, "foo"))
ExpectThat(err, Error(HasSubstr("no such file or directory")))
AssertEq(nil, fi4)
}

0 comments on commit 9e025b4

Please sign in to comment.