Skip to content

Commit 769a246

Browse files
authored
Merge pull request #233 from pohly/config-defaults
clean up configuration handling
2 parents d305288 + 90f9d07 commit 769a246

File tree

10 files changed

+198
-145
lines changed

10 files changed

+198
-145
lines changed

cmd/csi-sanity/sanity_test.go

Lines changed: 53 additions & 28 deletions
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
)
@@ -30,39 +31,63 @@ const (
3031

3132
var (
3233
VERSION = "(dev)"
33-
version bool
34-
config sanity.Config
34+
config = sanity.NewTestConfig()
3535
)
3636

37-
func init() {
38-
flag.StringVar(&config.Address, prefix+"endpoint", "", "CSI endpoint")
39-
flag.StringVar(&config.ControllerAddress, prefix+"controllerendpoint", "", "CSI controller endpoint")
40-
flag.BoolVar(&version, prefix+"version", false, "Version of this program")
41-
flag.StringVar(&config.TargetPath, prefix+"mountdir", os.TempDir()+"/csi-mount", "Mount point for NodePublish")
42-
flag.StringVar(&config.StagingPath, prefix+"stagingdir", os.TempDir()+"/csi-staging", "Mount point for NodeStage if staging is supported")
43-
flag.StringVar(&config.CreateTargetPathCmd, prefix+"createmountpathcmd", "", "Command to run for target path creation")
44-
flag.StringVar(&config.CreateStagingPathCmd, prefix+"createstagingpathcmd", "", "Command to run for staging path creation")
45-
flag.IntVar(&config.CreatePathCmdTimeout, prefix+"createpathcmdtimeout", 10, "Timeout for the commands to create target and staging paths, in seconds")
46-
flag.StringVar(&config.RemoveTargetPathCmd, prefix+"removemountpathcmd", "", "Command to run for target path removal")
47-
flag.StringVar(&config.RemoveStagingPathCmd, prefix+"removestagingpathcmd", "", "Command to run for staging path removal")
48-
flag.IntVar(&config.RemovePathCmdTimeout, prefix+"removepathcmdtimeout", 10, "Timeout for the commands to remove target and staging paths, in seconds")
49-
flag.StringVar(&config.SecretsFile, prefix+"secrets", "", "CSI secrets file")
50-
flag.Int64Var(&config.TestVolumeSize, prefix+"testvolumesize", sanity.DefTestVolumeSize, "Base volume size used for provisioned volumes")
51-
flag.Int64Var(&config.TestVolumeExpandSize, prefix+"testvolumeexpandsize", 0, "Target size for expanded volumes")
52-
flag.StringVar(&config.TestVolumeParametersFile, prefix+"testvolumeparameters", "", "YAML file of volume parameters for provisioned volumes")
53-
flag.StringVar(&config.TestSnapshotParametersFile, prefix+"testsnapshotparameters", "", "YAML file of snapshot parameters for provisioned snapshots")
54-
flag.BoolVar(&config.TestNodeVolumeAttachLimit, prefix+"testnodevolumeattachlimit", false, "Test node volume attach limit")
55-
flag.StringVar(&config.JUnitFile, prefix+"junitfile", "", "JUnit XML output file where test results will be written")
56-
flag.Parse()
37+
func stringVar(p *string, name string, usage string) {
38+
flag.StringVar(p, prefix+name, *p, usage)
5739
}
5840

59-
func TestSanity(t *testing.T) {
60-
if version {
41+
func boolVar(p *bool, name string, usage string) {
42+
flag.BoolVar(p, prefix+name, *p, usage)
43+
}
44+
45+
func intVar(p *int, name string, usage string) {
46+
flag.IntVar(p, prefix+name, *p, usage)
47+
}
48+
49+
func int64Var(p *int64, name string, usage string) {
50+
flag.Int64Var(p, prefix+name, *p, usage)
51+
}
52+
53+
func durationVar(p *time.Duration, name string, usage string) {
54+
flag.DurationVar(p, prefix+name, *p, usage)
55+
}
56+
57+
func TestMain(m *testing.M) {
58+
version := flag.Bool("version", false, "print version of this program")
59+
60+
// Support overriding the default configuration via flags.
61+
stringVar(&config.Address, "endpoint", "CSI endpoint")
62+
stringVar(&config.ControllerAddress, "controllerendpoint", "CSI controller endpoint")
63+
stringVar(&config.TargetPath, "mountdir", "Mount point for NodePublish")
64+
stringVar(&config.StagingPath, "stagingdir", "Mount point for NodeStage if staging is supported")
65+
stringVar(&config.CreateTargetPathCmd, "createmountpathcmd", "Command to run for target path creation")
66+
stringVar(&config.CreateStagingPathCmd, "createstagingpathcmd", "Command to run for staging path creation")
67+
durationVar(&config.CreatePathCmdTimeout, "createpathcmdtimeout", "Timeout for the commands to create target and staging paths, in seconds")
68+
stringVar(&config.RemoveTargetPathCmd, "removemountpathcmd", "Command to run for target path removal")
69+
stringVar(&config.RemoveStagingPathCmd, "removestagingpathcmd", "Command to run for staging path removal")
70+
durationVar(&config.RemovePathCmdTimeout, "removepathcmdtimeout", "Timeout for the commands to remove target and staging paths, in seconds")
71+
stringVar(&config.SecretsFile, "secrets", "CSI secrets file")
72+
int64Var(&config.TestVolumeSize, "testvolumesize", "Base volume size used for provisioned volumes")
73+
int64Var(&config.TestVolumeExpandSize, "testvolumeexpandsize", "Target size for expanded volumes")
74+
stringVar(&config.TestVolumeParametersFile, "testvolumeparameters", "YAML file of volume parameters for provisioned volumes")
75+
stringVar(&config.TestSnapshotParametersFile, "testsnapshotparameters", "YAML file of snapshot parameters for provisioned snapshots")
76+
boolVar(&config.TestNodeVolumeAttachLimit, "testnodevolumeattachlimit", "Test node volume attach limit")
77+
stringVar(&config.JUnitFile, "junitfile", "JUnit XML output file where test results will be written")
78+
79+
flag.Parse()
80+
if *version {
6181
fmt.Printf("Version = %s\n", VERSION)
62-
return
82+
os.Exit(0)
6383
}
64-
if len(config.Address) == 0 {
65-
t.Fatalf("--%sendpoint must be provided with an CSI endpoint", prefix)
84+
if config.Address == "" {
85+
fmt.Printf("--%sendpoint must be provided with an CSI endpoint\n", prefix)
86+
os.Exit(1)
6687
}
67-
sanity.Test(t, &config)
88+
os.Exit(m.Run())
89+
}
90+
91+
func TestSanity(t *testing.T) {
92+
sanity.Test(t, config)
6893
}

hack/_apitest/api_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package apitest
22

33
import (
4-
"os"
54
"testing"
65

76
"github.com/kubernetes-csi/csi-test/pkg/sanity"
87
)
98

109
func TestMyDriver(t *testing.T) {
11-
config := &sanity.Config{
12-
TargetPath: os.TempDir() + "/csi-target",
13-
StagingPath: os.TempDir() + "/csi-staging",
14-
Address: "/tmp/e2e-csi-sanity.sock",
15-
TestNodeVolumeAttachLimit: true,
16-
}
10+
config := sanity.NewTestConfig()
11+
config.Address = "/tmp/e2e-csi-sanity.sock"
12+
config.TestNodeVolumeAttachLimit = true
1713

1814
sanity.Test(t, config)
1915
}

hack/_apitest2/api_test.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,27 @@ func TestMyDriverWithCustomTargetPaths(t *testing.T) {
2424
// are created. For k8s, it could be /var/lib/kubelet/pods under which the
2525
// mount directories could be created.
2626
tmpPath := path.Join(os.TempDir(), "csi")
27-
config := &sanity.Config{
28-
TargetPath: "foo/target/mount",
29-
StagingPath: "foo/staging/mount",
30-
Address: "/tmp/e2e-csi-sanity.sock",
31-
CreateTargetDir: func(targetPath string) (string, error) {
32-
createTargetDirCalls++
33-
targetPath = path.Join(tmpPath, targetPath)
34-
return targetPath, createTargetDir(targetPath)
35-
},
36-
CreateStagingDir: func(targetPath string) (string, error) {
37-
createStagingDirCalls++
38-
targetPath = path.Join(tmpPath, targetPath)
39-
return targetPath, createTargetDir(targetPath)
40-
},
41-
RemoveTargetPath: func(targetPath string) error {
42-
removeTargetDirCalls++
43-
return os.RemoveAll(targetPath)
44-
},
45-
RemoveStagingPath: func(targetPath string) error {
46-
removeStagingDirCalls++
47-
return os.RemoveAll(targetPath)
48-
},
27+
config := sanity.NewTestConfig()
28+
config.TargetPath = "foo/target/mount"
29+
config.StagingPath = "foo/staging/mount"
30+
config.Address = "/tmp/e2e-csi-sanity.sock"
31+
config.CreateTargetDir = func(targetPath string) (string, error) {
32+
createTargetDirCalls++
33+
targetPath = path.Join(tmpPath, targetPath)
34+
return targetPath, createTargetDir(targetPath)
35+
}
36+
config.CreateStagingDir = func(targetPath string) (string, error) {
37+
createStagingDirCalls++
38+
targetPath = path.Join(tmpPath, targetPath)
39+
return targetPath, createTargetDir(targetPath)
40+
}
41+
config.RemoveTargetPath = func(targetPath string) error {
42+
removeTargetDirCalls++
43+
return os.RemoveAll(targetPath)
44+
}
45+
config.RemoveStagingPath = func(targetPath string) error {
46+
removeStagingDirCalls++
47+
return os.RemoveAll(targetPath)
4948
}
5049

5150
sanity.Test(t, config)

hack/_embedded/embedded_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package embedded
22

33
import (
4-
"os"
54
"testing"
65

76
"github.com/kubernetes-csi/csi-test/pkg/sanity"
87
. "github.com/onsi/ginkgo"
98
. "github.com/onsi/gomega"
109
)
1110

11+
var context *sanity.TestContext
12+
1213
func TestMyDriverGinkgo(t *testing.T) {
1314
RegisterFailHandler(Fail)
1415
RunSpecs(t, "CSI Sanity Test Suite")
@@ -21,24 +22,24 @@ func TestMyDriverGinkgo(t *testing.T) {
2122
// in hack/e2e.sh if a PR adds back such functions in the sanity test
2223
// code.
2324
var _ = BeforeSuite(func() {})
24-
var _ = AfterSuite(func() {})
25+
var _ = AfterSuite(func() {
26+
if context != nil {
27+
context.Finalize()
28+
}
29+
})
2530

2631
var _ = Describe("MyCSIDriver", func() {
2732
Context("Config A", func() {
28-
config := &sanity.Config{
29-
TargetPath: os.TempDir() + "/csi-target",
30-
StagingPath: os.TempDir() + "/csi-staging",
31-
Address: "/tmp/e2e-csi-sanity.sock",
32-
TestNodeVolumeAttachLimit: true,
33-
IDGen: &sanity.DefaultIDGenerator{},
34-
}
33+
config := sanity.NewTestConfig()
34+
config.Address = "/tmp/e2e-csi-sanity.sock"
35+
config.TestNodeVolumeAttachLimit = true
3536

3637
BeforeEach(func() {})
3738

3839
AfterEach(func() {})
3940

4041
Describe("CSI Driver Test Suite", func() {
41-
sanity.GinkgoTest(config)
42+
context = sanity.GinkgoTest(&config)
4243
})
4344
})
4445
})

pkg/sanity/cleanup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type VolumeInfo struct {
3939
// Cleanup keeps track of resources, in particular volumes, which need
4040
// to be freed when testing is done. All methods can be called concurrently.
4141
type Cleanup struct {
42-
Context *SanityContext
42+
Context *TestContext
4343
ControllerClient csi.ControllerClient
4444
NodeClient csi.NodeClient
4545
ControllerPublishSupported bool

pkg/sanity/controller.go

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ import (
3131
)
3232

3333
const (
34-
// DefTestVolumeSize defines the base size of dynamically
35-
// provisioned volumes. 10GB by default, can be overridden by
36-
// setting Config.TestVolumeSize.
37-
DefTestVolumeSize int64 = 10 * 1024 * 1024 * 1024
38-
3934
// DefTestVolumeExpand defines the size increment for volume
4035
// expansion. It can be overriden by setting an
4136
// Config.TestVolumeExpandSize, which will be taken as absolute
@@ -45,14 +40,11 @@ const (
4540
MaxNameLength int = 128
4641
)
4742

48-
func TestVolumeSize(sc *SanityContext) int64 {
49-
if sc.Config.TestVolumeSize > 0 {
50-
return sc.Config.TestVolumeSize
51-
}
52-
return DefTestVolumeSize
43+
func TestVolumeSize(sc *TestContext) int64 {
44+
return sc.Config.TestVolumeSize
5345
}
5446

55-
func TestVolumeExpandSize(sc *SanityContext) int64 {
47+
func TestVolumeExpandSize(sc *TestContext) int64 {
5648
if sc.Config.TestVolumeExpandSize > 0 {
5749
return sc.Config.TestVolumeExpandSize
5850
}
@@ -92,7 +84,7 @@ func isControllerCapabilitySupported(
9284
return false
9385
}
9486

95-
var _ = DescribeSanity("Controller Service [Controller Server]", func(sc *SanityContext) {
87+
var _ = DescribeSanity("Controller Service [Controller Server]", func(sc *TestContext) {
9688
var (
9789
c csi.ControllerClient
9890
n csi.NodeClient
@@ -1617,7 +1609,7 @@ var _ = DescribeSanity("Controller Service [Controller Server]", func(sc *Sanity
16171609
})
16181610
})
16191611

1620-
var _ = DescribeSanity("ListSnapshots [Controller Server]", func(sc *SanityContext) {
1612+
var _ = DescribeSanity("ListSnapshots [Controller Server]", func(sc *TestContext) {
16211613
var (
16221614
c csi.ControllerClient
16231615
)
@@ -1870,7 +1862,7 @@ var _ = DescribeSanity("ListSnapshots [Controller Server]", func(sc *SanityConte
18701862

18711863
})
18721864

1873-
var _ = DescribeSanity("DeleteSnapshot [Controller Server]", func(sc *SanityContext) {
1865+
var _ = DescribeSanity("DeleteSnapshot [Controller Server]", func(sc *TestContext) {
18741866
var (
18751867
c csi.ControllerClient
18761868
)
@@ -1933,7 +1925,7 @@ var _ = DescribeSanity("DeleteSnapshot [Controller Server]", func(sc *SanityCont
19331925
})
19341926
})
19351927

1936-
var _ = DescribeSanity("CreateSnapshot [Controller Server]", func(sc *SanityContext) {
1928+
var _ = DescribeSanity("CreateSnapshot [Controller Server]", func(sc *TestContext) {
19371929
var (
19381930
c csi.ControllerClient
19391931
)
@@ -2082,7 +2074,7 @@ var _ = DescribeSanity("CreateSnapshot [Controller Server]", func(sc *SanityCont
20822074
})
20832075
})
20842076

2085-
var _ = DescribeSanity("ExpandVolume [Controller Server]", func(sc *SanityContext) {
2077+
var _ = DescribeSanity("ExpandVolume [Controller Server]", func(sc *TestContext) {
20862078
var (
20872079
c csi.ControllerClient
20882080
cl *Cleanup
@@ -2188,7 +2180,7 @@ var _ = DescribeSanity("ExpandVolume [Controller Server]", func(sc *SanityContex
21882180
})
21892181
})
21902182

2191-
func MakeCreateVolumeReq(sc *SanityContext, name string) *csi.CreateVolumeRequest {
2183+
func MakeCreateVolumeReq(sc *TestContext, name string) *csi.CreateVolumeRequest {
21922184
size1 := TestVolumeSize(sc)
21932185

21942186
req := &csi.CreateVolumeRequest{
@@ -2217,7 +2209,7 @@ func MakeCreateVolumeReq(sc *SanityContext, name string) *csi.CreateVolumeReques
22172209
return req
22182210
}
22192211

2220-
func MakeCreateSnapshotReq(sc *SanityContext, name, sourceVolumeId string) *csi.CreateSnapshotRequest {
2212+
func MakeCreateSnapshotReq(sc *TestContext, name, sourceVolumeId string) *csi.CreateSnapshotRequest {
22212213
req := &csi.CreateSnapshotRequest{
22222214
Name: name,
22232215
SourceVolumeId: sourceVolumeId,
@@ -2231,7 +2223,7 @@ func MakeCreateSnapshotReq(sc *SanityContext, name, sourceVolumeId string) *csi.
22312223
return req
22322224
}
22332225

2234-
func MakeDeleteSnapshotReq(sc *SanityContext, id string) *csi.DeleteSnapshotRequest {
2226+
func MakeDeleteSnapshotReq(sc *TestContext, id string) *csi.DeleteSnapshotRequest {
22352227
delSnapReq := &csi.DeleteSnapshotRequest{
22362228
SnapshotId: id,
22372229
}
@@ -2243,7 +2235,7 @@ func MakeDeleteSnapshotReq(sc *SanityContext, id string) *csi.DeleteSnapshotRequ
22432235
return delSnapReq
22442236
}
22452237

2246-
func MakeDeleteVolumeReq(sc *SanityContext, id string) *csi.DeleteVolumeRequest {
2238+
func MakeDeleteVolumeReq(sc *TestContext, id string) *csi.DeleteVolumeRequest {
22472239
delVolReq := &csi.DeleteVolumeRequest{
22482240
VolumeId: id,
22492241
}
@@ -2256,7 +2248,7 @@ func MakeDeleteVolumeReq(sc *SanityContext, id string) *csi.DeleteVolumeRequest
22562248
}
22572249

22582250
// MakeControllerPublishVolumeReq creates and returns a ControllerPublishVolumeRequest.
2259-
func MakeControllerPublishVolumeReq(sc *SanityContext, volID, nodeID string) *csi.ControllerPublishVolumeRequest {
2251+
func MakeControllerPublishVolumeReq(sc *TestContext, volID, nodeID string) *csi.ControllerPublishVolumeRequest {
22602252
return &csi.ControllerPublishVolumeRequest{
22612253
VolumeId: volID,
22622254
NodeId: nodeID,
@@ -2274,7 +2266,7 @@ func MakeControllerPublishVolumeReq(sc *SanityContext, volID, nodeID string) *cs
22742266
}
22752267

22762268
// MakeControllerUnpublishVolumeReq creates and returns a ControllerUnpublishVolumeRequest.
2277-
func MakeControllerUnpublishVolumeReq(sc *SanityContext, volID, nodeID string) *csi.ControllerUnpublishVolumeRequest {
2269+
func MakeControllerUnpublishVolumeReq(sc *TestContext, volID, nodeID string) *csi.ControllerUnpublishVolumeRequest {
22782270
return &csi.ControllerUnpublishVolumeRequest{
22792271
VolumeId: volID,
22802272
NodeId: nodeID,
@@ -2283,7 +2275,7 @@ func MakeControllerUnpublishVolumeReq(sc *SanityContext, volID, nodeID string) *
22832275
}
22842276

22852277
// CreateAndControllerPublishVolume creates and controller publishes a volume given a volume name and node ID.
2286-
func CreateAndControllerPublishVolume(sc *SanityContext, c csi.ControllerClient, volName, nodeID string) (volID string, err error) {
2278+
func CreateAndControllerPublishVolume(sc *TestContext, c csi.ControllerClient, volName, nodeID string) (volID string, err error) {
22872279
vol, err := c.CreateVolume(context.Background(), MakeCreateVolumeReq(sc, volName))
22882280
Expect(err).NotTo(HaveOccurred())
22892281
Expect(vol).NotTo(BeNil())
@@ -2298,7 +2290,7 @@ func CreateAndControllerPublishVolume(sc *SanityContext, c csi.ControllerClient,
22982290
}
22992291

23002292
// ControllerUnpublishAndDeleteVolume controller unpublishes and deletes a volume, given volume ID and node ID.
2301-
func ControllerUnpublishAndDeleteVolume(sc *SanityContext, c csi.ControllerClient, volID, nodeID string) error {
2293+
func ControllerUnpublishAndDeleteVolume(sc *TestContext, c csi.ControllerClient, volID, nodeID string) error {
23022294
_, err := c.ControllerUnpublishVolume(
23032295
context.Background(),
23042296
MakeControllerUnpublishVolumeReq(sc, volID, nodeID),

pkg/sanity/identity.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
. "github.com/onsi/gomega"
3131
)
3232

33-
var _ = DescribeSanity("Identity Service", func(sc *SanityContext) {
33+
var _ = DescribeSanity("Identity Service", func(sc *TestContext) {
3434
var (
3535
c csi.IdentityClient
3636
)

pkg/sanity/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func isPluginCapabilitySupported(c csi.IdentityClient,
6666
return false
6767
}
6868

69-
var _ = DescribeSanity("Node Service", func(sc *SanityContext) {
69+
var _ = DescribeSanity("Node Service", func(sc *TestContext) {
7070
var (
7171
cl *Cleanup
7272
c csi.NodeClient

0 commit comments

Comments
 (0)