Skip to content

Commit

Permalink
sanity: fix nil pointer error for IDGen when using Ginkgo suite
Browse files Browse the repository at this point in the history
When adding IDGen, only one of the two code paths that trigger testing
was updated such that it sets a default ID generator when the config
doesn't already have one. GinkgoTest was not updated.

As a result, existing E2E suites which use GinkgoTest and don't set the
new field crash at runtime with a nil pointer error when updated to
csi-test 2.2.0.
  • Loading branch information
pohly committed Sep 25, 2019
1 parent 82b0519 commit 8ac7c59
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions pkg/sanity/sanity.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,11 @@ type Config struct {
// Timeout for the executed commands for path removal.
RemovePathCmdTimeout int

// IDGen is an optional interface for callers to provide a generator for
// valid Volume and Node IDs. Defaults to DefaultIDGenerator which generates
// generic string IDs
// IDGen is an optional interface for callers to provide a
// generator for valid Volume and Node IDs. A custom value has
// to be set before calling Test or GinkgoTest. If unset,
// those functions will set it to a DefaultIDGenerator
// instance.
IDGen IDGenerator
}

Expand All @@ -143,6 +145,21 @@ type SanityContext struct {
StagingPath string
}

// newContext sets up sanity testing with a config supplied by the
// user of the sanity package. Ownership of that config is shared
// between the sanity package and the caller.
func newContext(reqConfig *Config) *SanityContext {
// To avoid runtime if checks when using IDGen, a default
// is set here.
if reqConfig.IDGen == nil {
reqConfig.IDGen = &DefaultIDGenerator{}
}

return &SanityContext{
Config: reqConfig,
}
}

// Test will test the CSI driver at the specified address by
// setting up a Ginkgo suite and running it.
func Test(t *testing.T, reqConfig *Config) {
Expand All @@ -158,14 +175,7 @@ func Test(t *testing.T, reqConfig *Config) {
}
}

if reqConfig.IDGen == nil {
reqConfig.IDGen = &DefaultIDGenerator{}
}

sc := &SanityContext{
Config: reqConfig,
}

sc := newContext(reqConfig)
registerTestsInGinkgo(sc)
RegisterFailHandler(Fail)

Expand All @@ -180,11 +190,11 @@ func Test(t *testing.T, reqConfig *Config) {
}
}

// GinkoTest is another entry point for sanity testing: instead of directly
// running tests like Test does, it merely registers the tests. This can
// be used to embed sanity testing in a custom Ginkgo test suite.
func GinkgoTest(reqConfig *Config) {
sc := &SanityContext{
Config: reqConfig,
}

sc := newContext(reqConfig)
registerTestsInGinkgo(sc)
}

Expand Down

0 comments on commit 8ac7c59

Please sign in to comment.