Skip to content

Commit

Permalink
Add support for custom target path removal
Browse files Browse the repository at this point in the history
This adds three new flags to support custom target path removal:

- `removemountpathcmd`: Command to remove a custom mount path.
- `removestagingpathcmd`: Command to remove a custom staging path.
- `removepathcmdtimeout`: Timeout for the custom path removal commands.
  • Loading branch information
darkowlzz committed Mar 17, 2019
1 parent 564e750 commit 6edd078
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 10 deletions.
3 changes: 3 additions & 0 deletions cmd/csi-sanity/sanity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func init() {
flag.StringVar(&config.CreateTargetPathCmd, prefix+"createmountpathcmd", "", "Command to run for target path creation")
flag.StringVar(&config.CreateStagingPathCmd, prefix+"createstagingpathcmd", "", "Command to run for staging path creation")
flag.IntVar(&config.CreatePathCmdTimeout, prefix+"createpathcmdtimeout", 10, "Timeout for the commands to create target and staging paths, in seconds")
flag.StringVar(&config.RemoveTargetPathCmd, prefix+"removemountpathcmd", "", "Command to run for target path removal")
flag.StringVar(&config.RemoveStagingPathCmd, prefix+"removestagingpathcmd", "", "Command to run for staging path removal")
flag.IntVar(&config.RemovePathCmdTimeout, prefix+"removepathcmdtimeout", 10, "Timeout for the commands to remove target and staging paths, in seconds")
flag.StringVar(&config.SecretsFile, prefix+"secrets", "", "CSI secrets file")
flag.Int64Var(&config.TestVolumeSize, prefix+"testvolumesize", sanity.DefTestVolumeSize, "Base volume size used for provisioned volumes")
flag.StringVar(&config.TestVolumeParametersFile, prefix+"testvolumeparameters", "", "YAML file of volume parameters for provisioned volumes")
Expand Down
21 changes: 20 additions & 1 deletion hack/_apitest2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import (
// TestMyDriverWithCustomTargetPaths verifies that CreateTargetDir and
// CreateStagingDir are called a specific number of times.
func TestMyDriverWithCustomTargetPaths(t *testing.T) {
var createTargetDirCalls, createStagingDirCalls int
var createTargetDirCalls, createStagingDirCalls,
removeTargetDirCalls, removeStagingDirCalls int

wantCreateTargetCalls := 3
wantCreateStagingCalls := 3
wantRemoveTargetCalls := 3
wantRemoveStagingCalls := 3

// tmpPath could be a CO specific directory under which all the target dirs
// are created. For k8s, it could be /var/lib/kubelet/pods under which the
Expand All @@ -35,6 +38,14 @@ func TestMyDriverWithCustomTargetPaths(t *testing.T) {
targetPath = path.Join(tmpPath, targetPath)
return targetPath, createTargetDir(targetPath)
},
RemoveTargetPath: func(targetPath string) error {
removeTargetDirCalls++
return os.RemoveAll(targetPath)
},
RemoveStagingPath: func(targetPath string) error {
removeStagingDirCalls++
return os.RemoveAll(targetPath)
},
}

sanity.Test(t, config)
Expand All @@ -46,6 +57,14 @@ func TestMyDriverWithCustomTargetPaths(t *testing.T) {
if createStagingDirCalls != wantCreateStagingCalls {
t.Errorf("unexpected number of CreateStagingDir calls:\n(WNT) %d\n(GOT) %d", wantCreateStagingCalls, createStagingDirCalls)
}

if removeTargetDirCalls != wantRemoveTargetCalls {
t.Errorf("unexpected number of RemoveTargetDir calls:\n(WNT) %d\n(GOT) %d", wantRemoveTargetCalls, removeTargetDirCalls)
}

if removeStagingDirCalls != wantRemoveStagingCalls {
t.Errorf("unexpected number of RemoveStagingDir calls:\n(WNT) %d\n(GOT) %d", wantRemoveStagingCalls, removeStagingDirCalls)
}
}

func createTargetDir(targetPath string) error {
Expand Down
21 changes: 15 additions & 6 deletions hack/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,29 @@ runTestWithCustomTargetPaths()
targetpath="/tmp/csi/$@"
mkdir -p $targetpath
echo $targetpath
' > custompath.bash
chmod +x custompath.bash
local scriptpath="$PWD/custompath.bash"
' > custompathcreation.bash

# Create a script for custom target path removal.
echo '#!/bin/bash
rm -rf $@
' > custompathremoval.bash

chmod +x custompathcreation.bash custompathremoval.bash
local creationscriptpath="$PWD/custompathcreation.bash"
local removalscriptpath="$PWD/custompathremoval.bash"

./cmd/csi-sanity/csi-sanity $TESTARGS \
--csi.endpoint=$2 \
--csi.mountdir="foo/target/mount" \
--csi.stagingdir="foo/staging/mount" \
--csi.createmountpathcmd=$scriptpath \
--csi.createstagingpathcmd=$scriptpath; ret=$?
--csi.createmountpathcmd=$creationscriptpath \
--csi.createstagingpathcmd=$creationscriptpath \
--csi.removemountpathcmd=$removalscriptpath \
--csi.removestagingpathcmd=$removalscriptpath; ret=$?
kill -9 $pid

# Delete the script.
rm $scriptpath
rm $creationscriptpath $removalscriptpath

if [ $ret -ne 0 ] ; then
exit $ret
Expand Down
48 changes: 45 additions & 3 deletions pkg/sanity/sanity.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,31 @@ type Config struct {
// Callback functions to customize the creation of target and staging
// directories. Returns the new paths for mount and staging.
// If not defined, directories are created in the default way at TargetPath
// and StagingPath.
// and StagingPath on the host.
CreateTargetDir func(path string) (string, error)
CreateStagingDir func(path string) (string, error)

// Callback functions to customize the removal of the target and staging
// directories.
// If not defined, directories are removed in the default way at TargetPath
// and StagingPath on the host.
RemoveTargetPath func(path string) error
RemoveStagingPath func(path string) error

// Commands to be executed for customized creation of the target and staging
// paths. This command must be available on the host where sanity runs. The
// stdout of the commands are the paths for mount and staging.
CreateTargetPathCmd string
CreateStagingPathCmd string
// Timeout for the executed commands for path creation.
CreatePathCmdTimeout int

// Commands to be executed for customized removal of the target and staging
// paths. Thie command must be available on the host where sanity runs.
RemoveTargetPathCmd string
RemoveStagingPathCmd string
// Timeout for the executed commands for path removal.
RemovePathCmdTimeout int
}

// SanityContext holds the variables that each test can depend on. It
Expand Down Expand Up @@ -173,8 +187,8 @@ func (sc *SanityContext) setup() {

func (sc *SanityContext) teardown() {
// Delete the created paths if any.
os.RemoveAll(sc.targetPath)
os.RemoveAll(sc.stagingPath)
removeMountTargetLocation(sc.targetPath, sc.Config.RemoveTargetPathCmd, sc.Config.RemoveTargetPath, sc.Config.RemovePathCmdTimeout)
removeMountTargetLocation(sc.stagingPath, sc.Config.RemoveStagingPathCmd, sc.Config.RemoveStagingPath, sc.Config.RemovePathCmdTimeout)

// We intentionally do not close the connection to the CSI
// driver here because the large amount of connection attempts
Expand Down Expand Up @@ -242,6 +256,34 @@ func createMountTargetLocation(targetPath string, createPathCmd string, customCr
return newTargetPath, nil
}

// removeMountTargetLocation takes a target path parameter and removes the path
// using a custom command, custom function or falls back to the default removal
// by deleting the path on the host.
func removeMountTargetLocation(targetPath string, removePathCmd string, customRemovePath func(string) error, timeout int) error {
if targetPath == "" {
return nil
}

if removePathCmd != "" {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
defer cancel()

cmd := exec.CommandContext(ctx, removePathCmd, targetPath)
cmd.Stderr = os.Stderr
_, err := cmd.Output()
if err != nil {
return fmt.Errorf("target path removal command %s failed: %v", removePathCmd, err)
}
} else if customRemovePath != nil {
if err := customRemovePath(targetPath); err != nil {
return err
}
} else {
return os.RemoveAll(targetPath)
}
return nil
}

func loadSecrets(path string) (*CSISecrets, error) {
var creds CSISecrets

Expand Down

0 comments on commit 6edd078

Please sign in to comment.