Skip to content

Commit

Permalink
sanity: specify timeouts as durations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pohly committed Nov 6, 2019
1 parent 00e10f7 commit 90f9d07
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
9 changes: 7 additions & 2 deletions cmd/csi-sanity/sanity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os"
"testing"
"time"

"github.com/kubernetes-csi/csi-test/v3/pkg/sanity"
)
Expand Down Expand Up @@ -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")

Expand All @@ -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")
Expand Down
16 changes: 8 additions & 8 deletions pkg/sanity/sanity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{},
}
Expand Down Expand Up @@ -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 == "" {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 90f9d07

Please sign in to comment.