Skip to content

Commit 90f9d07

Browse files
committed
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.
1 parent 00e10f7 commit 90f9d07

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

cmd/csi-sanity/sanity_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"os"
2222
"testing"
23+
"time"
2324

2425
"github.com/kubernetes-csi/csi-test/v3/pkg/sanity"
2526
)
@@ -49,6 +50,10 @@ func int64Var(p *int64, name string, usage string) {
4950
flag.Int64Var(p, prefix+name, *p, usage)
5051
}
5152

53+
func durationVar(p *time.Duration, name string, usage string) {
54+
flag.DurationVar(p, prefix+name, *p, usage)
55+
}
56+
5257
func TestMain(m *testing.M) {
5358
version := flag.Bool("version", false, "print version of this program")
5459

@@ -59,10 +64,10 @@ func TestMain(m *testing.M) {
5964
stringVar(&config.StagingPath, "stagingdir", "Mount point for NodeStage if staging is supported")
6065
stringVar(&config.CreateTargetPathCmd, "createmountpathcmd", "Command to run for target path creation")
6166
stringVar(&config.CreateStagingPathCmd, "createstagingpathcmd", "Command to run for staging path creation")
62-
intVar(&config.CreatePathCmdTimeout, "createpathcmdtimeout", "Timeout for the commands to create target and staging paths, in seconds")
67+
durationVar(&config.CreatePathCmdTimeout, "createpathcmdtimeout", "Timeout for the commands to create target and staging paths, in seconds")
6368
stringVar(&config.RemoveTargetPathCmd, "removemountpathcmd", "Command to run for target path removal")
6469
stringVar(&config.RemoveStagingPathCmd, "removestagingpathcmd", "Command to run for staging path removal")
65-
intVar(&config.RemovePathCmdTimeout, "removepathcmdtimeout", "Timeout for the commands to remove target and staging paths, in seconds")
70+
durationVar(&config.RemovePathCmdTimeout, "removepathcmdtimeout", "Timeout for the commands to remove target and staging paths, in seconds")
6671
stringVar(&config.SecretsFile, "secrets", "CSI secrets file")
6772
int64Var(&config.TestVolumeSize, "testvolumesize", "Base volume size used for provisioned volumes")
6873
int64Var(&config.TestVolumeExpandSize, "testvolumeexpandsize", "Target size for expanded volumes")

pkg/sanity/sanity.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ type TestConfig struct {
134134
CreateTargetPathCmd string
135135
CreateStagingPathCmd string
136136
// Timeout for the executed commands for path creation.
137-
CreatePathCmdTimeout int
137+
CreatePathCmdTimeout time.Duration
138138

139139
// Commands to be executed for customized removal of the target and staging
140140
// paths. Thie command must be available on the host where sanity runs.
141141
RemoveTargetPathCmd string
142142
RemoveStagingPathCmd string
143143
// Timeout for the executed commands for path removal.
144-
RemovePathCmdTimeout int
144+
RemovePathCmdTimeout time.Duration
145145

146146
// IDGen is an interface for callers to provide a
147147
// generator for valid Volume and Node IDs. Defaults to
@@ -171,8 +171,8 @@ func NewTestConfig() TestConfig {
171171
return TestConfig{
172172
TargetPath: os.TempDir() + "/csi-mount",
173173
StagingPath: os.TempDir() + "/csi-staging",
174-
CreatePathCmdTimeout: 10,
175-
RemovePathCmdTimeout: 10,
174+
CreatePathCmdTimeout: 10 * time.Second,
175+
RemovePathCmdTimeout: 10 * time.Second,
176176
TestVolumeSize: 10 * 1024 * 1024 * 1024, // 10 GiB
177177
IDGen: &DefaultIDGenerator{},
178178
}
@@ -307,7 +307,7 @@ func (sc *TestContext) Finalize() {
307307
// createMountTargetLocation takes a target path parameter and creates the
308308
// target path using a custom command, custom function or falls back to the
309309
// default using mkdir and returns the new target path.
310-
func createMountTargetLocation(targetPath string, createPathCmd string, customCreateDir func(string) (string, error), timeout int) (string, error) {
310+
func createMountTargetLocation(targetPath string, createPathCmd string, customCreateDir func(string) (string, error), timeout time.Duration) (string, error) {
311311

312312
// Return the target path if empty.
313313
if targetPath == "" {
@@ -318,7 +318,7 @@ func createMountTargetLocation(targetPath string, createPathCmd string, customCr
318318

319319
if createPathCmd != "" {
320320
// Create the target path using the create path command.
321-
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
321+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
322322
defer cancel()
323323

324324
cmd := exec.CommandContext(ctx, createPathCmd, targetPath)
@@ -352,13 +352,13 @@ func createMountTargetLocation(targetPath string, createPathCmd string, customCr
352352
// removeMountTargetLocation takes a target path parameter and removes the path
353353
// using a custom command, custom function or falls back to the default removal
354354
// by deleting the path on the host.
355-
func removeMountTargetLocation(targetPath string, removePathCmd string, customRemovePath func(string) error, timeout int) error {
355+
func removeMountTargetLocation(targetPath string, removePathCmd string, customRemovePath func(string) error, timeout time.Duration) error {
356356
if targetPath == "" {
357357
return nil
358358
}
359359

360360
if removePathCmd != "" {
361-
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
361+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
362362
defer cancel()
363363

364364
cmd := exec.CommandContext(ctx, removePathCmd, targetPath)

0 commit comments

Comments
 (0)