From 84ffc81af944fa7fa1243e2cefc430e5ed308024 Mon Sep 17 00:00:00 2001 From: Mauricio Poppe Date: Wed, 26 Jan 2022 23:40:22 +0000 Subject: [PATCH] RmdirContents test to check that the symlink target is not deleted --- Makefile | 2 +- integrationtests/filesystem_v2alpha1_test.go | 59 ++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 87ea0947..ce9c0d38 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ all: build test # include release tools for building binary and testing targets include release-tools/build.make -BUILD_PLATFORMS=windows amd64 .exe +BUILD_PLATFORMS=windows amd64 amd64 .exe GOPATH ?= $(shell go env GOPATH) REPO_ROOT = $(CURDIR) BUILD_DIR = bin diff --git a/integrationtests/filesystem_v2alpha1_test.go b/integrationtests/filesystem_v2alpha1_test.go index cb0fa1ac..15b5a898 100644 --- a/integrationtests/filesystem_v2alpha1_test.go +++ b/integrationtests/filesystem_v2alpha1_test.go @@ -2,6 +2,7 @@ package integrationtests import ( "context" + "errors" "fmt" "math/rand" "os" @@ -186,4 +187,62 @@ func v2alpha1FilesystemTests(t *testing.T) { } }) + t.Run("RmdirContentsNoFollowSymlink", func(t *testing.T) { + // RmdirContents should not delete the target of a symlink, only the symlink + client, err := v2alpha1client.NewClient() + require.Nil(t, err) + defer client.Close() + + r1 := rand.New(rand.NewSource(time.Now().UnixNano())) + rand1 := r1.Intn(100) + + rootPath := getKubeletPathForTest(fmt.Sprintf("testplugin-%d.csi.io", rand1), t) + // this line should delete the rootPath because only its content were deleted + defer os.RemoveAll(rootPath) + + insidePath := filepath.Join(rootPath, "inside/") + outsidePath := filepath.Join(rootPath, "outside/") + paths := []string{ + filepath.Join(insidePath, "foo/goo/"), + filepath.Join(insidePath, "foo/bar/baz/"), + filepath.Join(insidePath, "foo/beta/gamma/"), + outsidePath, + } + for _, path := range paths { + err = os.MkdirAll(path, os.ModeDir) + require.Nil(t, err) + } + + // create a temp file on the outside and make a symlink from the inside to the outside + outsideFile := filepath.Join(outsidePath, "target") + insideFile := filepath.Join(insidePath, "source") + + file, err := os.Create(outsideFile) + require.Nil(t, err) + defer file.Close() + err = os.Symlink(insideFile, outsideFile) + require.Nil(t, err) + + rmdirContentsRequest := &v2alpha1.RmdirContentsRequest{ + Path: insidePath, + Force: true, + } + _, err = client.RmdirContents(context.Background(), rmdirContentsRequest) + require.Nil(t, err) + + // the inside path should exist + exists, err := pathExists(insidePath) + require.Nil(t, err) + assert.True(t, exists, "The path shouldn't exist") + // it should have no children + children, err := os.ReadDir(insidePath) + require.Nil(t, err) + assert.True(t, len(children) == 0, "The RmdirContents path to delete shouldn't have children") + // the symlink target should exist + _, err = os.Open(outsideFile) + if errors.Is(err, os.ErrNotExist) { + // the file should exist but it was deleted! + t.Fatalf("File outsideFile=%s doesn't exist", outsideFile) + } + }) }