Skip to content

Commit

Permalink
RmdirContents test to check that the symlink target is not deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciopoppe committed Jan 26, 2022
1 parent 98bd9a4 commit 38089c9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions integrationtests/filesystem_v2alpha1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package integrationtests

import (
"context"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -186,4 +188,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(outside, insideFile)
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 := ioutil.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)
}
})
}

0 comments on commit 38089c9

Please sign in to comment.