From 90f9d0799ac00fc4deab8a77fb459de6c3a71b69 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Tue, 5 Nov 2019 10:18:13 +0100 Subject: [PATCH] sanity: specify timeouts as durations This is cleaner. This also changes the values of the command line timeout parameters from plain int (10) to durations (10s), but this is okay as we are preparing a major new release. --- cmd/csi-sanity/sanity_test.go | 9 +++++++-- pkg/sanity/sanity.go | 16 ++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/cmd/csi-sanity/sanity_test.go b/cmd/csi-sanity/sanity_test.go index cb759a50..e4c35dc6 100644 --- a/cmd/csi-sanity/sanity_test.go +++ b/cmd/csi-sanity/sanity_test.go @@ -20,6 +20,7 @@ import ( "fmt" "os" "testing" + "time" "github.com/kubernetes-csi/csi-test/v3/pkg/sanity" ) @@ -49,6 +50,10 @@ func int64Var(p *int64, name string, usage string) { flag.Int64Var(p, prefix+name, *p, usage) } +func durationVar(p *time.Duration, name string, usage string) { + flag.DurationVar(p, prefix+name, *p, usage) +} + func TestMain(m *testing.M) { version := flag.Bool("version", false, "print version of this program") @@ -59,10 +64,10 @@ func TestMain(m *testing.M) { stringVar(&config.StagingPath, "stagingdir", "Mount point for NodeStage if staging is supported") stringVar(&config.CreateTargetPathCmd, "createmountpathcmd", "Command to run for target path creation") stringVar(&config.CreateStagingPathCmd, "createstagingpathcmd", "Command to run for staging path creation") - intVar(&config.CreatePathCmdTimeout, "createpathcmdtimeout", "Timeout for the commands to create target and staging paths, in seconds") + durationVar(&config.CreatePathCmdTimeout, "createpathcmdtimeout", "Timeout for the commands to create target and staging paths, in seconds") stringVar(&config.RemoveTargetPathCmd, "removemountpathcmd", "Command to run for target path removal") stringVar(&config.RemoveStagingPathCmd, "removestagingpathcmd", "Command to run for staging path removal") - intVar(&config.RemovePathCmdTimeout, "removepathcmdtimeout", "Timeout for the commands to remove target and staging paths, in seconds") + durationVar(&config.RemovePathCmdTimeout, "removepathcmdtimeout", "Timeout for the commands to remove target and staging paths, in seconds") stringVar(&config.SecretsFile, "secrets", "CSI secrets file") int64Var(&config.TestVolumeSize, "testvolumesize", "Base volume size used for provisioned volumes") int64Var(&config.TestVolumeExpandSize, "testvolumeexpandsize", "Target size for expanded volumes") diff --git a/pkg/sanity/sanity.go b/pkg/sanity/sanity.go index 08bc18ec..86be7caa 100644 --- a/pkg/sanity/sanity.go +++ b/pkg/sanity/sanity.go @@ -134,14 +134,14 @@ type TestConfig struct { CreateTargetPathCmd string CreateStagingPathCmd string // Timeout for the executed commands for path creation. - CreatePathCmdTimeout int + CreatePathCmdTimeout time.Duration // 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 + RemovePathCmdTimeout time.Duration // IDGen is an interface for callers to provide a // generator for valid Volume and Node IDs. Defaults to @@ -171,8 +171,8 @@ func NewTestConfig() TestConfig { return TestConfig{ TargetPath: os.TempDir() + "/csi-mount", StagingPath: os.TempDir() + "/csi-staging", - CreatePathCmdTimeout: 10, - RemovePathCmdTimeout: 10, + CreatePathCmdTimeout: 10 * time.Second, + RemovePathCmdTimeout: 10 * time.Second, TestVolumeSize: 10 * 1024 * 1024 * 1024, // 10 GiB IDGen: &DefaultIDGenerator{}, } @@ -307,7 +307,7 @@ func (sc *TestContext) Finalize() { // createMountTargetLocation takes a target path parameter and creates the // target path using a custom command, custom function or falls back to the // default using mkdir and returns the new target path. -func createMountTargetLocation(targetPath string, createPathCmd string, customCreateDir func(string) (string, error), timeout int) (string, error) { +func createMountTargetLocation(targetPath string, createPathCmd string, customCreateDir func(string) (string, error), timeout time.Duration) (string, error) { // Return the target path if empty. if targetPath == "" { @@ -318,7 +318,7 @@ func createMountTargetLocation(targetPath string, createPathCmd string, customCr if createPathCmd != "" { // Create the target path using the create path command. - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() cmd := exec.CommandContext(ctx, createPathCmd, targetPath) @@ -352,13 +352,13 @@ func createMountTargetLocation(targetPath string, createPathCmd string, customCr // 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 { +func removeMountTargetLocation(targetPath string, removePathCmd string, customRemovePath func(string) error, timeout time.Duration) error { if targetPath == "" { return nil } if removePathCmd != "" { - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() cmd := exec.CommandContext(ctx, removePathCmd, targetPath)