From 44a5850955843e5a8293e0f7a2556a7bdca8d5a6 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Thu, 5 Jul 2018 17:22:02 +0200 Subject: [PATCH] pkg/sanity: support sanity testing in another Ginkgo suite This was not possible before: - Ginkgo only supports one set of Before/AfterSuite calls, which might be in use already in the existing suite (for example, the Kubernetes e2e framework). - When nesting the sanity test inside a Ginkgo spec there is no testing.T pointer that could be passed to the Test method. Now all global variables are replaced with a test context that gets passed into the specs explicitly. For this to work, specs must be set up with SanityDescribe instead of the previous Describe call. The Test and Config API are unchanged, but there is one slight change of behavior: previously it was possible to define two tests with different configs in the same test binary and then running each test would execute the sanity tests for that config. That no longer works because running one test changes the global Ginkgo suite and running the second test would then also run the specs from the first one. This usage could be supported by having one test function which takes multiple configs, then runs one suite that contains tests for all of them. Probably it is easier to just use a Ginkgo suite, because there multiple different configs can be handled already normally via different Describe specs: calling the new GinkgoTest API will add all sanity test cases for one configuration to the spec in which the API is called. --- pkg/sanity/README.md | 45 ++++++++- pkg/sanity/controller.go | 204 +++++++++++++++++++-------------------- pkg/sanity/identity.go | 12 +-- pkg/sanity/node.go | 122 +++++++++++------------ pkg/sanity/sanity.go | 62 +++++++----- pkg/sanity/tests.go | 57 +++++++++++ 6 files changed, 305 insertions(+), 197 deletions(-) create mode 100644 pkg/sanity/tests.go diff --git a/pkg/sanity/README.md b/pkg/sanity/README.md index 747744ea..de4ae501 100644 --- a/pkg/sanity/README.md +++ b/pkg/sanity/README.md @@ -13,13 +13,50 @@ Golang `TestXXX` functions. For example: ```go func TestMyDriver(t *testing.T) { - // Setup the full driver and its environment - ... setup driver ... + // Setup the full driver and its environment + ... setup driver ... + config := &sanity.Config{ + TargetPath: ... + StagingPath: ... + Address: endpoint, + } - // Now call the test suite - sanity.Test(t, driverEndpointAddress, "/mnt") + + // Now call the test suite + sanity.Test(t, config) } ``` +Only one such test function is supported because under the hood a +Ginkgo test suite gets constructed and executed by the call. + +Alternatively, the tests can also be embedded inside a Ginkgo test +suite. In that case it is possible to define multiple tests with +different configurations: + +```go +var _ = Describe("MyCSIDriver", func () { + Context("Config A", func () { + var config &sanity.Config + + BeforeEach() { + ... setup driver and config... + } + + AfterEach() { + ...tear down driver... + } + + Describe("CSI sanity", func() { + sanity.GinkgoTest(config) + }) + }) + + Context("Config B", func () { + ... + }) +}) +``` + ## Command line program Please see [csi-sanity](https://github.com/kubernetes-csi/csi-test/tree/master/cmd/csi-sanity) diff --git a/pkg/sanity/controller.go b/pkg/sanity/controller.go index 79bdc23e..73de0703 100644 --- a/pkg/sanity/controller.go +++ b/pkg/sanity/controller.go @@ -36,9 +36,9 @@ const ( DefTestVolumeSize int64 = 10 * 1024 * 1024 * 1024 ) -func TestVolumeSize() int64 { - if config.TestVolumeSize > 0 { - return config.TestVolumeSize +func TestVolumeSize(sc *SanityContext) int64 { + if sc.Config.TestVolumeSize > 0 { + return sc.Config.TestVolumeSize } return DefTestVolumeSize } @@ -69,13 +69,13 @@ func isControllerCapabilitySupported( return false } -var _ = Describe("ControllerGetCapabilities [Controller Server]", func() { +var _ = DescribeSanity("ControllerGetCapabilities [Controller Server]", func(sc *SanityContext) { var ( c csi.ControllerClient ) BeforeEach(func() { - c = csi.NewControllerClient(conn) + c = csi.NewControllerClient(sc.Conn) }) It("should return appropriate capabilities", func() { @@ -105,13 +105,13 @@ var _ = Describe("ControllerGetCapabilities [Controller Server]", func() { }) }) -var _ = Describe("GetCapacity [Controller Server]", func() { +var _ = DescribeSanity("GetCapacity [Controller Server]", func(sc *SanityContext) { var ( c csi.ControllerClient ) BeforeEach(func() { - c = csi.NewControllerClient(conn) + c = csi.NewControllerClient(sc.Conn) if !isControllerCapabilitySupported(c, csi.ControllerServiceCapability_RPC_GET_CAPACITY) { Skip("GetCapacity not supported") @@ -129,13 +129,13 @@ var _ = Describe("GetCapacity [Controller Server]", func() { }) }) -var _ = Describe("ListVolumes [Controller Server]", func() { +var _ = DescribeSanity("ListVolumes [Controller Server]", func(sc *SanityContext) { var ( c csi.ControllerClient ) BeforeEach(func() { - c = csi.NewControllerClient(conn) + c = csi.NewControllerClient(sc.Conn) if !isControllerCapabilitySupported(c, csi.ControllerServiceCapability_RPC_LIST_VOLUMES) { Skip("ListVolumes not supported") @@ -160,13 +160,13 @@ var _ = Describe("ListVolumes [Controller Server]", func() { // and not there when deleted. }) -var _ = Describe("CreateVolume [Controller Server]", func() { +var _ = DescribeSanity("CreateVolume [Controller Server]", func(sc *SanityContext) { var ( c csi.ControllerClient ) BeforeEach(func() { - c = csi.NewControllerClient(conn) + c = csi.NewControllerClient(sc.Conn) if !isControllerCapabilitySupported(c, csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME) { Skip("CreateVolume not supported") @@ -177,8 +177,8 @@ var _ = Describe("CreateVolume [Controller Server]", func() { req := &csi.CreateVolumeRequest{} - if secrets != nil { - req.ControllerCreateSecrets = secrets.CreateVolumeSecret + if sc.Secrets != nil { + req.ControllerCreateSecrets = sc.Secrets.CreateVolumeSecret } _, err := c.CreateVolume(context.Background(), req) @@ -195,8 +195,8 @@ var _ = Describe("CreateVolume [Controller Server]", func() { Name: "name", } - if secrets != nil { - req.ControllerCreateSecrets = secrets.CreateVolumeSecret + if sc.Secrets != nil { + req.ControllerCreateSecrets = sc.Secrets.CreateVolumeSecret } _, err := c.CreateVolume(context.Background(), req) @@ -226,8 +226,8 @@ var _ = Describe("CreateVolume [Controller Server]", func() { }, } - if secrets != nil { - req.ControllerCreateSecrets = secrets.CreateVolumeSecret + if sc.Secrets != nil { + req.ControllerCreateSecrets = sc.Secrets.CreateVolumeSecret } vol, err := c.CreateVolume(context.Background(), req) @@ -242,8 +242,8 @@ var _ = Describe("CreateVolume [Controller Server]", func() { VolumeId: vol.GetVolume().GetId(), } - if secrets != nil { - delReq.ControllerDeleteSecrets = secrets.DeleteVolumeSecret + if sc.Secrets != nil { + delReq.ControllerDeleteSecrets = sc.Secrets.DeleteVolumeSecret } _, err = c.DeleteVolume(context.Background(), delReq) @@ -268,12 +268,12 @@ var _ = Describe("CreateVolume [Controller Server]", func() { }, }, CapacityRange: &csi.CapacityRange{ - RequiredBytes: TestVolumeSize(), + RequiredBytes: TestVolumeSize(sc), }, } - if secrets != nil { - req.ControllerCreateSecrets = secrets.CreateVolumeSecret + if sc.Secrets != nil { + req.ControllerCreateSecrets = sc.Secrets.CreateVolumeSecret } vol, err := c.CreateVolume(context.Background(), req) @@ -289,7 +289,7 @@ var _ = Describe("CreateVolume [Controller Server]", func() { Expect(vol).NotTo(BeNil()) Expect(vol.GetVolume()).NotTo(BeNil()) Expect(vol.GetVolume().GetId()).NotTo(BeEmpty()) - Expect(vol.GetVolume().GetCapacityBytes()).To(BeNumerically(">=", TestVolumeSize())) + Expect(vol.GetVolume().GetCapacityBytes()).To(BeNumerically(">=", TestVolumeSize(sc))) } By("cleaning up deleting the volume") @@ -297,8 +297,8 @@ var _ = Describe("CreateVolume [Controller Server]", func() { VolumeId: vol.GetVolume().GetId(), } - if secrets != nil { - delReq.ControllerDeleteSecrets = secrets.DeleteVolumeSecret + if sc.Secrets != nil { + delReq.ControllerDeleteSecrets = sc.Secrets.DeleteVolumeSecret } _, err = c.DeleteVolume(context.Background(), delReq) @@ -308,7 +308,7 @@ var _ = Describe("CreateVolume [Controller Server]", func() { By("creating a volume") name := "sanity" - size := TestVolumeSize() + size := TestVolumeSize(sc) req := &csi.CreateVolumeRequest{ Name: name, @@ -327,8 +327,8 @@ var _ = Describe("CreateVolume [Controller Server]", func() { }, } - if secrets != nil { - req.ControllerCreateSecrets = secrets.CreateVolumeSecret + if sc.Secrets != nil { + req.ControllerCreateSecrets = sc.Secrets.CreateVolumeSecret } vol1, err := c.CreateVolume(context.Background(), req) @@ -355,8 +355,8 @@ var _ = Describe("CreateVolume [Controller Server]", func() { }, } - if secrets != nil { - req2.ControllerCreateSecrets = secrets.CreateVolumeSecret + if sc.Secrets != nil { + req2.ControllerCreateSecrets = sc.Secrets.CreateVolumeSecret } vol2, err := c.CreateVolume(context.Background(), req2) @@ -373,8 +373,8 @@ var _ = Describe("CreateVolume [Controller Server]", func() { VolumeId: vol1.GetVolume().GetId(), } - if secrets != nil { - delReq.ControllerDeleteSecrets = secrets.DeleteVolumeSecret + if sc.Secrets != nil { + delReq.ControllerDeleteSecrets = sc.Secrets.DeleteVolumeSecret } _, err = c.DeleteVolume(context.Background(), delReq) @@ -384,7 +384,7 @@ var _ = Describe("CreateVolume [Controller Server]", func() { By("creating a volume") name := "sanity" - size1 := TestVolumeSize() + size1 := TestVolumeSize(sc) req := &csi.CreateVolumeRequest{ Name: name, @@ -404,8 +404,8 @@ var _ = Describe("CreateVolume [Controller Server]", func() { }, } - if secrets != nil { - req.ControllerCreateSecrets = secrets.CreateVolumeSecret + if sc.Secrets != nil { + req.ControllerCreateSecrets = sc.Secrets.CreateVolumeSecret } vol1, err := c.CreateVolume(context.Background(), req) @@ -413,7 +413,7 @@ var _ = Describe("CreateVolume [Controller Server]", func() { Expect(vol1).NotTo(BeNil()) Expect(vol1.GetVolume()).NotTo(BeNil()) Expect(vol1.GetVolume().GetId()).NotTo(BeEmpty()) - size2 := 2 * TestVolumeSize() + size2 := 2 * TestVolumeSize(sc) req2 := &csi.CreateVolumeRequest{ Name: name, @@ -433,8 +433,8 @@ var _ = Describe("CreateVolume [Controller Server]", func() { }, } - if secrets != nil { - req2.ControllerCreateSecrets = secrets.CreateVolumeSecret + if sc.Secrets != nil { + req2.ControllerCreateSecrets = sc.Secrets.CreateVolumeSecret } _, err = c.CreateVolume(context.Background(), req2) @@ -449,8 +449,8 @@ var _ = Describe("CreateVolume [Controller Server]", func() { VolumeId: vol1.GetVolume().GetId(), } - if secrets != nil { - delReq.ControllerDeleteSecrets = secrets.DeleteVolumeSecret + if sc.Secrets != nil { + delReq.ControllerDeleteSecrets = sc.Secrets.DeleteVolumeSecret } _, err = c.DeleteVolume(context.Background(), delReq) @@ -458,13 +458,13 @@ var _ = Describe("CreateVolume [Controller Server]", func() { }) }) -var _ = Describe("DeleteVolume [Controller Server]", func() { +var _ = DescribeSanity("DeleteVolume [Controller Server]", func(sc *SanityContext) { var ( c csi.ControllerClient ) BeforeEach(func() { - c = csi.NewControllerClient(conn) + c = csi.NewControllerClient(sc.Conn) if !isControllerCapabilitySupported(c, csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME) { Skip("DeleteVolume not supported") @@ -475,8 +475,8 @@ var _ = Describe("DeleteVolume [Controller Server]", func() { req := &csi.DeleteVolumeRequest{} - if secrets != nil { - req.ControllerDeleteSecrets = secrets.DeleteVolumeSecret + if sc.Secrets != nil { + req.ControllerDeleteSecrets = sc.Secrets.DeleteVolumeSecret } _, err := c.DeleteVolume(context.Background(), req) @@ -493,8 +493,8 @@ var _ = Describe("DeleteVolume [Controller Server]", func() { VolumeId: "reallyfakevolumeid", } - if secrets != nil { - req.ControllerDeleteSecrets = secrets.DeleteVolumeSecret + if sc.Secrets != nil { + req.ControllerDeleteSecrets = sc.Secrets.DeleteVolumeSecret } _, err := c.DeleteVolume(context.Background(), req) @@ -521,8 +521,8 @@ var _ = Describe("DeleteVolume [Controller Server]", func() { }, } - if secrets != nil { - createReq.ControllerCreateSecrets = secrets.CreateVolumeSecret + if sc.Secrets != nil { + createReq.ControllerCreateSecrets = sc.Secrets.CreateVolumeSecret } vol, err := c.CreateVolume(context.Background(), createReq) @@ -539,8 +539,8 @@ var _ = Describe("DeleteVolume [Controller Server]", func() { VolumeId: vol.GetVolume().GetId(), } - if secrets != nil { - req.ControllerDeleteSecrets = secrets.DeleteVolumeSecret + if sc.Secrets != nil { + req.ControllerDeleteSecrets = sc.Secrets.DeleteVolumeSecret } _, err = c.DeleteVolume(context.Background(), req) @@ -548,13 +548,13 @@ var _ = Describe("DeleteVolume [Controller Server]", func() { }) }) -var _ = Describe("ValidateVolumeCapabilities [Controller Server]", func() { +var _ = DescribeSanity("ValidateVolumeCapabilities [Controller Server]", func(sc *SanityContext) { var ( c csi.ControllerClient ) BeforeEach(func() { - c = csi.NewControllerClient(conn) + c = csi.NewControllerClient(sc.Conn) }) It("should fail when no volume id is provided", func() { @@ -603,8 +603,8 @@ var _ = Describe("ValidateVolumeCapabilities [Controller Server]", func() { }, } - if secrets != nil { - req.ControllerCreateSecrets = secrets.CreateVolumeSecret + if sc.Secrets != nil { + req.ControllerCreateSecrets = sc.Secrets.CreateVolumeSecret } vol, err := c.CreateVolume(context.Background(), req) @@ -640,8 +640,8 @@ var _ = Describe("ValidateVolumeCapabilities [Controller Server]", func() { VolumeId: vol.GetVolume().GetId(), } - if secrets != nil { - delReq.ControllerDeleteSecrets = secrets.DeleteVolumeSecret + if sc.Secrets != nil { + delReq.ControllerDeleteSecrets = sc.Secrets.DeleteVolumeSecret } _, err = c.DeleteVolume(context.Background(), delReq) @@ -674,15 +674,15 @@ var _ = Describe("ValidateVolumeCapabilities [Controller Server]", func() { }) }) -var _ = Describe("ControllerPublishVolume [Controller Server]", func() { +var _ = DescribeSanity("ControllerPublishVolume [Controller Server]", func(sc *SanityContext) { var ( c csi.ControllerClient n csi.NodeClient ) BeforeEach(func() { - c = csi.NewControllerClient(conn) - n = csi.NewNodeClient(conn) + c = csi.NewControllerClient(sc.Conn) + n = csi.NewNodeClient(sc.Conn) if !isControllerCapabilitySupported(c, csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME) { Skip("ControllerPublishVolume not supported") @@ -693,8 +693,8 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { req := &csi.ControllerPublishVolumeRequest{} - if secrets != nil { - req.ControllerPublishSecrets = secrets.ControllerPublishVolumeSecret + if sc.Secrets != nil { + req.ControllerPublishSecrets = sc.Secrets.ControllerPublishVolumeSecret } _, err := c.ControllerPublishVolume(context.Background(), req) @@ -711,8 +711,8 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { VolumeId: "id", } - if secrets != nil { - req.ControllerPublishSecrets = secrets.ControllerPublishVolumeSecret + if sc.Secrets != nil { + req.ControllerPublishSecrets = sc.Secrets.ControllerPublishVolumeSecret } _, err := c.ControllerPublishVolume(context.Background(), req) @@ -730,8 +730,8 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { NodeId: "fakenode", } - if secrets != nil { - req.ControllerPublishSecrets = secrets.ControllerPublishVolumeSecret + if sc.Secrets != nil { + req.ControllerPublishSecrets = sc.Secrets.ControllerPublishVolumeSecret } _, err := c.ControllerPublishVolume(context.Background(), req) @@ -761,8 +761,8 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { }, } - if secrets != nil { - req.ControllerCreateSecrets = secrets.CreateVolumeSecret + if sc.Secrets != nil { + req.ControllerCreateSecrets = sc.Secrets.CreateVolumeSecret } vol, err := c.CreateVolume(context.Background(), req) @@ -796,8 +796,8 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { Readonly: false, } - if secrets != nil { - pubReq.ControllerPublishSecrets = secrets.ControllerPublishVolumeSecret + if sc.Secrets != nil { + pubReq.ControllerPublishSecrets = sc.Secrets.ControllerPublishVolumeSecret } conpubvol, err := c.ControllerPublishVolume(context.Background(), pubReq) @@ -812,8 +812,8 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { NodeId: nid.GetNodeId(), } - if secrets != nil { - unpubReq.ControllerUnpublishSecrets = secrets.ControllerUnpublishVolumeSecret + if sc.Secrets != nil { + unpubReq.ControllerUnpublishSecrets = sc.Secrets.ControllerUnpublishVolumeSecret } conunpubvol, err := c.ControllerUnpublishVolume(context.Background(), unpubReq) @@ -826,8 +826,8 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { VolumeId: vol.GetVolume().GetId(), } - if secrets != nil { - delReq.ControllerDeleteSecrets = secrets.DeleteVolumeSecret + if sc.Secrets != nil { + delReq.ControllerDeleteSecrets = sc.Secrets.DeleteVolumeSecret } _, err = c.DeleteVolume(context.Background(), delReq) @@ -852,8 +852,8 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { Readonly: false, } - if secrets != nil { - pubReq.ControllerPublishSecrets = secrets.ControllerPublishVolumeSecret + if sc.Secrets != nil { + pubReq.ControllerPublishSecrets = sc.Secrets.ControllerPublishVolumeSecret } conpubvol, err := c.ControllerPublishVolume(context.Background(), pubReq) @@ -884,8 +884,8 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { }, } - if secrets != nil { - req.ControllerCreateSecrets = secrets.CreateVolumeSecret + if sc.Secrets != nil { + req.ControllerCreateSecrets = sc.Secrets.CreateVolumeSecret } vol, err := c.CreateVolume(context.Background(), req) @@ -911,8 +911,8 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { Readonly: false, } - if secrets != nil { - pubReq.ControllerPublishSecrets = secrets.ControllerPublishVolumeSecret + if sc.Secrets != nil { + pubReq.ControllerPublishSecrets = sc.Secrets.ControllerPublishVolumeSecret } conpubvol, err := c.ControllerPublishVolume(context.Background(), pubReq) @@ -929,8 +929,8 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { VolumeId: vol.GetVolume().GetId(), } - if secrets != nil { - delReq.ControllerDeleteSecrets = secrets.DeleteVolumeSecret + if sc.Secrets != nil { + delReq.ControllerDeleteSecrets = sc.Secrets.DeleteVolumeSecret } _, err = c.DeleteVolume(context.Background(), delReq) @@ -956,8 +956,8 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { }, } - if secrets != nil { - req.ControllerCreateSecrets = secrets.CreateVolumeSecret + if sc.Secrets != nil { + req.ControllerCreateSecrets = sc.Secrets.CreateVolumeSecret } vol, err := c.CreateVolume(context.Background(), req) @@ -991,8 +991,8 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { Readonly: false, } - if secrets != nil { - pubReq.ControllerPublishSecrets = secrets.ControllerPublishVolumeSecret + if sc.Secrets != nil { + pubReq.ControllerPublishSecrets = sc.Secrets.ControllerPublishVolumeSecret } conpubvol, err := c.ControllerPublishVolume(context.Background(), pubReq) @@ -1018,8 +1018,8 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { NodeId: nid.GetNodeId(), } - if secrets != nil { - unpubReq.ControllerUnpublishSecrets = secrets.ControllerUnpublishVolumeSecret + if sc.Secrets != nil { + unpubReq.ControllerUnpublishSecrets = sc.Secrets.ControllerUnpublishVolumeSecret } conunpubvol, err := c.ControllerUnpublishVolume(context.Background(), unpubReq) @@ -1032,8 +1032,8 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { VolumeId: vol.GetVolume().GetId(), } - if secrets != nil { - delReq.ControllerDeleteSecrets = secrets.DeleteVolumeSecret + if sc.Secrets != nil { + delReq.ControllerDeleteSecrets = sc.Secrets.DeleteVolumeSecret } _, err = c.DeleteVolume(context.Background(), delReq) @@ -1041,15 +1041,15 @@ var _ = Describe("ControllerPublishVolume [Controller Server]", func() { }) }) -var _ = Describe("ControllerUnpublishVolume [Controller Server]", func() { +var _ = DescribeSanity("ControllerUnpublishVolume [Controller Server]", func(sc *SanityContext) { var ( c csi.ControllerClient n csi.NodeClient ) BeforeEach(func() { - c = csi.NewControllerClient(conn) - n = csi.NewNodeClient(conn) + c = csi.NewControllerClient(sc.Conn) + n = csi.NewNodeClient(sc.Conn) if !isControllerCapabilitySupported(c, csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME) { Skip("ControllerUnpublishVolume not supported") @@ -1060,8 +1060,8 @@ var _ = Describe("ControllerUnpublishVolume [Controller Server]", func() { req := &csi.ControllerUnpublishVolumeRequest{} - if secrets != nil { - req.ControllerUnpublishSecrets = secrets.ControllerUnpublishVolumeSecret + if sc.Secrets != nil { + req.ControllerUnpublishSecrets = sc.Secrets.ControllerUnpublishVolumeSecret } _, err := c.ControllerUnpublishVolume(context.Background(), req) @@ -1092,8 +1092,8 @@ var _ = Describe("ControllerUnpublishVolume [Controller Server]", func() { }, } - if secrets != nil { - req.ControllerCreateSecrets = secrets.CreateVolumeSecret + if sc.Secrets != nil { + req.ControllerCreateSecrets = sc.Secrets.CreateVolumeSecret } vol, err := c.CreateVolume(context.Background(), req) @@ -1127,8 +1127,8 @@ var _ = Describe("ControllerUnpublishVolume [Controller Server]", func() { Readonly: false, } - if secrets != nil { - pubReq.ControllerPublishSecrets = secrets.ControllerPublishVolumeSecret + if sc.Secrets != nil { + pubReq.ControllerPublishSecrets = sc.Secrets.ControllerPublishVolumeSecret } conpubvol, err := c.ControllerPublishVolume(context.Background(), pubReq) @@ -1144,8 +1144,8 @@ var _ = Describe("ControllerUnpublishVolume [Controller Server]", func() { NodeId: nid.GetNodeId(), } - if secrets != nil { - unpubReq.ControllerUnpublishSecrets = secrets.ControllerUnpublishVolumeSecret + if sc.Secrets != nil { + unpubReq.ControllerUnpublishSecrets = sc.Secrets.ControllerUnpublishVolumeSecret } conunpubvol, err := c.ControllerUnpublishVolume(context.Background(), unpubReq) @@ -1158,8 +1158,8 @@ var _ = Describe("ControllerUnpublishVolume [Controller Server]", func() { VolumeId: vol.GetVolume().GetId(), } - if secrets != nil { - delReq.ControllerDeleteSecrets = secrets.DeleteVolumeSecret + if sc.Secrets != nil { + delReq.ControllerDeleteSecrets = sc.Secrets.DeleteVolumeSecret } _, err = c.DeleteVolume(context.Background(), delReq) diff --git a/pkg/sanity/identity.go b/pkg/sanity/identity.go index c03eab23..2d3e1e3e 100644 --- a/pkg/sanity/identity.go +++ b/pkg/sanity/identity.go @@ -30,13 +30,13 @@ import ( . "github.com/onsi/gomega" ) -var _ = Describe("GetPluginCapabilities [Identity Service]", func() { +var _ = DescribeSanity("GetPluginCapabilities [Identity Service]", func(sc *SanityContext) { var ( c csi.IdentityClient ) BeforeEach(func() { - c = csi.NewIdentityClient(conn) + c = csi.NewIdentityClient(sc.Conn) }) It("should return appropriate capabilities", func() { @@ -60,13 +60,13 @@ var _ = Describe("GetPluginCapabilities [Identity Service]", func() { }) -var _ = Describe("Probe [Identity Service]", func() { +var _ = DescribeSanity("Probe [Identity Service]", func(sc *SanityContext) { var ( c csi.IdentityClient ) BeforeEach(func() { - c = csi.NewIdentityClient(conn) + c = csi.NewIdentityClient(sc.Conn) }) It("should return appropriate information", func() { @@ -88,13 +88,13 @@ var _ = Describe("Probe [Identity Service]", func() { }) }) -var _ = Describe("GetPluginInfo [Identity Server]", func() { +var _ = DescribeSanity("GetPluginInfo [Identity Server]", func(sc *SanityContext) { var ( c csi.IdentityClient ) BeforeEach(func() { - c = csi.NewIdentityClient(conn) + c = csi.NewIdentityClient(sc.Conn) }) It("should return appropriate information", func() { diff --git a/pkg/sanity/node.go b/pkg/sanity/node.go index 5085bc50..59d59635 100644 --- a/pkg/sanity/node.go +++ b/pkg/sanity/node.go @@ -69,13 +69,13 @@ func isPluginCapabilitySupported(c csi.IdentityClient, return false } -var _ = Describe("NodeGetCapabilities [Node Server]", func() { +var _ = DescribeSanity("NodeGetCapabilities [Node Server]", func(sc *SanityContext) { var ( c csi.NodeClient ) BeforeEach(func() { - c = csi.NewNodeClient(conn) + c = csi.NewNodeClient(sc.Conn) }) It("should return appropriate capabilities", func() { @@ -101,13 +101,13 @@ var _ = Describe("NodeGetCapabilities [Node Server]", func() { }) }) -var _ = Describe("NodeGetId [Node Server]", func() { +var _ = DescribeSanity("NodeGetId [Node Server]", func(sc *SanityContext) { var ( c csi.NodeClient ) BeforeEach(func() { - c = csi.NewNodeClient(conn) + c = csi.NewNodeClient(sc.Conn) }) It("should return appropriate values", func() { @@ -121,7 +121,7 @@ var _ = Describe("NodeGetId [Node Server]", func() { }) }) -var _ = Describe("NodeGetInfo [Node Server]", func() { +var _ = DescribeSanity("NodeGetInfo [Node Server]", func(sc *SanityContext) { var ( c csi.NodeClient i csi.IdentityClient @@ -129,8 +129,8 @@ var _ = Describe("NodeGetInfo [Node Server]", func() { ) BeforeEach(func() { - c = csi.NewNodeClient(conn) - i = csi.NewIdentityClient(conn) + c = csi.NewNodeClient(sc.Conn) + i = csi.NewIdentityClient(sc.Conn) accessibilityConstraintSupported = isPluginCapabilitySupported(i, csi.PluginCapability_Service_ACCESSIBILITY_CONSTRAINTS) }) @@ -150,7 +150,7 @@ var _ = Describe("NodeGetInfo [Node Server]", func() { }) }) -var _ = Describe("NodePublishVolume [Node Server]", func() { +var _ = DescribeSanity("NodePublishVolume [Node Server]", func(sc *SanityContext) { var ( s csi.ControllerClient c csi.NodeClient @@ -159,14 +159,14 @@ var _ = Describe("NodePublishVolume [Node Server]", func() { ) BeforeEach(func() { - s = csi.NewControllerClient(conn) - c = csi.NewNodeClient(conn) + s = csi.NewControllerClient(sc.Conn) + c = csi.NewNodeClient(sc.Conn) controllerPublishSupported = isControllerCapabilitySupported( s, csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME) nodeStageSupported = isNodeCapabilitySupported(c, csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME) if nodeStageSupported { - err := createMountTargetLocation(config.StagingPath) + err := createMountTargetLocation(sc.Config.StagingPath) Expect(err).NotTo(HaveOccurred()) } }) @@ -175,8 +175,8 @@ var _ = Describe("NodePublishVolume [Node Server]", func() { req := &csi.NodePublishVolumeRequest{} - if secrets != nil { - req.NodePublishSecrets = secrets.NodePublishVolumeSecret + if sc.Secrets != nil { + req.NodePublishSecrets = sc.Secrets.NodePublishVolumeSecret } _, err := c.NodePublishVolume(context.Background(), req) @@ -193,8 +193,8 @@ var _ = Describe("NodePublishVolume [Node Server]", func() { VolumeId: "id", } - if secrets != nil { - req.NodePublishSecrets = secrets.NodePublishVolumeSecret + if sc.Secrets != nil { + req.NodePublishSecrets = sc.Secrets.NodePublishVolumeSecret } _, err := c.NodePublishVolume(context.Background(), req) @@ -209,11 +209,11 @@ var _ = Describe("NodePublishVolume [Node Server]", func() { req := &csi.NodePublishVolumeRequest{ VolumeId: "id", - TargetPath: config.TargetPath, + TargetPath: sc.Config.TargetPath, } - if secrets != nil { - req.NodePublishSecrets = secrets.NodePublishVolumeSecret + if sc.Secrets != nil { + req.NodePublishSecrets = sc.Secrets.NodePublishVolumeSecret } _, err := c.NodePublishVolume(context.Background(), req) @@ -225,11 +225,11 @@ var _ = Describe("NodePublishVolume [Node Server]", func() { }) It("should return appropriate values (no optional values added)", func() { - testFullWorkflowSuccess(s, c, controllerPublishSupported, nodeStageSupported) + testFullWorkflowSuccess(sc, s, c, controllerPublishSupported, nodeStageSupported) }) }) -var _ = Describe("NodeUnpublishVolume [Node Server]", func() { +var _ = DescribeSanity("NodeUnpublishVolume [Node Server]", func(sc *SanityContext) { var ( s csi.ControllerClient c csi.NodeClient @@ -238,14 +238,14 @@ var _ = Describe("NodeUnpublishVolume [Node Server]", func() { ) BeforeEach(func() { - s = csi.NewControllerClient(conn) - c = csi.NewNodeClient(conn) + s = csi.NewControllerClient(sc.Conn) + c = csi.NewNodeClient(sc.Conn) controllerPublishSupported = isControllerCapabilitySupported( s, csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME) nodeStageSupported = isNodeCapabilitySupported(c, csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME) if nodeStageSupported { - err := createMountTargetLocation(config.StagingPath) + err := createMountTargetLocation(sc.Config.StagingPath) Expect(err).NotTo(HaveOccurred()) } }) @@ -277,12 +277,12 @@ var _ = Describe("NodeUnpublishVolume [Node Server]", func() { }) It("should return appropriate values (no optional values added)", func() { - testFullWorkflowSuccess(s, c, controllerPublishSupported, nodeStageSupported) + testFullWorkflowSuccess(sc, s, c, controllerPublishSupported, nodeStageSupported) }) }) // TODO: Tests for NodeStageVolume/NodeUnstageVolume -func testFullWorkflowSuccess(s csi.ControllerClient, c csi.NodeClient, controllerPublishSupported, nodeStageSupported bool) { +func testFullWorkflowSuccess(sc *SanityContext, s csi.ControllerClient, c csi.NodeClient, controllerPublishSupported, nodeStageSupported bool) { // Create Volume First By("creating a single node writer volume") name := "sanity" @@ -300,8 +300,8 @@ func testFullWorkflowSuccess(s csi.ControllerClient, c csi.NodeClient, controlle }, } - if secrets != nil { - req.ControllerCreateSecrets = secrets.CreateVolumeSecret + if sc.Secrets != nil { + req.ControllerCreateSecrets = sc.Secrets.CreateVolumeSecret } vol, err := s.CreateVolume(context.Background(), req) @@ -335,8 +335,8 @@ func testFullWorkflowSuccess(s csi.ControllerClient, c csi.NodeClient, controlle Readonly: false, } - if secrets != nil { - pubReq.ControllerPublishSecrets = secrets.ControllerPublishVolumeSecret + if sc.Secrets != nil { + pubReq.ControllerPublishSecrets = sc.Secrets.ControllerPublishVolumeSecret } conpubvol, err = s.ControllerPublishVolume(context.Background(), pubReq) @@ -356,13 +356,13 @@ func testFullWorkflowSuccess(s csi.ControllerClient, c csi.NodeClient, controlle Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER, }, }, - StagingTargetPath: config.StagingPath, + StagingTargetPath: sc.Config.StagingPath, } if controllerPublishSupported { nodeStageVolReq.PublishInfo = conpubvol.GetPublishInfo() } - if secrets != nil { - nodeStageVolReq.NodeStageSecrets = secrets.NodeStageVolumeSecret + if sc.Secrets != nil { + nodeStageVolReq.NodeStageSecrets = sc.Secrets.NodeStageVolumeSecret } nodestagevol, err := c.NodeStageVolume( context.Background(), nodeStageVolReq) @@ -373,7 +373,7 @@ func testFullWorkflowSuccess(s csi.ControllerClient, c csi.NodeClient, controlle By("publishing the volume on a node") nodepubvolRequest := &csi.NodePublishVolumeRequest{ VolumeId: vol.GetVolume().GetId(), - TargetPath: config.TargetPath, + TargetPath: sc.Config.TargetPath, VolumeCapability: &csi.VolumeCapability{ AccessType: &csi.VolumeCapability_Mount{ Mount: &csi.VolumeCapability_MountVolume{}, @@ -384,13 +384,13 @@ func testFullWorkflowSuccess(s csi.ControllerClient, c csi.NodeClient, controlle }, } if nodeStageSupported { - nodepubvolRequest.StagingTargetPath = config.StagingPath + nodepubvolRequest.StagingTargetPath = sc.Config.StagingPath } if controllerPublishSupported { nodepubvolRequest.PublishInfo = conpubvol.GetPublishInfo() } - if secrets != nil { - nodepubvolRequest.NodePublishSecrets = secrets.NodePublishVolumeSecret + if sc.Secrets != nil { + nodepubvolRequest.NodePublishSecrets = sc.Secrets.NodePublishVolumeSecret } nodepubvol, err := c.NodePublishVolume(context.Background(), nodepubvolRequest) Expect(err).NotTo(HaveOccurred()) @@ -402,7 +402,7 @@ func testFullWorkflowSuccess(s csi.ControllerClient, c csi.NodeClient, controlle context.Background(), &csi.NodeUnpublishVolumeRequest{ VolumeId: vol.GetVolume().GetId(), - TargetPath: config.TargetPath, + TargetPath: sc.Config.TargetPath, }) Expect(err).NotTo(HaveOccurred()) Expect(nodeunpubvol).NotTo(BeNil()) @@ -413,7 +413,7 @@ func testFullWorkflowSuccess(s csi.ControllerClient, c csi.NodeClient, controlle context.Background(), &csi.NodeUnstageVolumeRequest{ VolumeId: vol.GetVolume().GetId(), - StagingTargetPath: config.StagingPath, + StagingTargetPath: sc.Config.StagingPath, }, ) Expect(err).NotTo(HaveOccurred()) @@ -428,8 +428,8 @@ func testFullWorkflowSuccess(s csi.ControllerClient, c csi.NodeClient, controlle NodeId: nid.GetNodeId(), } - if secrets != nil { - unpubReq.ControllerUnpublishSecrets = secrets.ControllerUnpublishVolumeSecret + if sc.Secrets != nil { + unpubReq.ControllerUnpublishSecrets = sc.Secrets.ControllerUnpublishVolumeSecret } controllerunpubvol, err := s.ControllerUnpublishVolume(context.Background(), unpubReq) @@ -443,15 +443,15 @@ func testFullWorkflowSuccess(s csi.ControllerClient, c csi.NodeClient, controlle VolumeId: vol.GetVolume().GetId(), } - if secrets != nil { - delReq.ControllerDeleteSecrets = secrets.DeleteVolumeSecret + if sc.Secrets != nil { + delReq.ControllerDeleteSecrets = sc.Secrets.DeleteVolumeSecret } _, err = s.DeleteVolume(context.Background(), delReq) Expect(err).NotTo(HaveOccurred()) } -var _ = Describe("NodeStageVolume [Node Server]", func() { +var _ = DescribeSanity("NodeStageVolume [Node Server]", func(sc *SanityContext) { var ( s csi.ControllerClient c csi.NodeClient @@ -461,15 +461,15 @@ var _ = Describe("NodeStageVolume [Node Server]", func() { ) BeforeEach(func() { - s = csi.NewControllerClient(conn) - c = csi.NewNodeClient(conn) + s = csi.NewControllerClient(sc.Conn) + c = csi.NewNodeClient(sc.Conn) device = "/dev/mock" controllerPublishSupported = isControllerCapabilitySupported( s, csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME) nodeStageSupported = isNodeCapabilitySupported(c, csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME) if nodeStageSupported { - err := createMountTargetLocation(config.StagingPath) + err := createMountTargetLocation(sc.Config.StagingPath) Expect(err).NotTo(HaveOccurred()) } else { Skip("NodeStageVolume not supported") @@ -479,7 +479,7 @@ var _ = Describe("NodeStageVolume [Node Server]", func() { It("should fail when no volume id is provided", func() { req := &csi.NodeStageVolumeRequest{ - StagingTargetPath: config.StagingPath, + StagingTargetPath: sc.Config.StagingPath, VolumeCapability: &csi.VolumeCapability{ AccessType: &csi.VolumeCapability_Mount{ Mount: &csi.VolumeCapability_MountVolume{}, @@ -493,8 +493,8 @@ var _ = Describe("NodeStageVolume [Node Server]", func() { }, } - if secrets != nil { - req.NodeStageSecrets = secrets.NodeStageVolumeSecret + if sc.Secrets != nil { + req.NodeStageSecrets = sc.Secrets.NodeStageVolumeSecret } _, err := c.NodeStageVolume(context.Background(), req) @@ -522,8 +522,8 @@ var _ = Describe("NodeStageVolume [Node Server]", func() { }, } - if secrets != nil { - req.NodeStageSecrets = secrets.NodeStageVolumeSecret + if sc.Secrets != nil { + req.NodeStageSecrets = sc.Secrets.NodeStageVolumeSecret } _, err := c.NodeStageVolume(context.Background(), req) @@ -538,14 +538,14 @@ var _ = Describe("NodeStageVolume [Node Server]", func() { req := &csi.NodeStageVolumeRequest{ VolumeId: "id", - StagingTargetPath: config.StagingPath, + StagingTargetPath: sc.Config.StagingPath, PublishInfo: map[string]string{ "device": device, }, } - if secrets != nil { - req.NodeStageSecrets = secrets.NodeStageVolumeSecret + if sc.Secrets != nil { + req.NodeStageSecrets = sc.Secrets.NodeStageVolumeSecret } _, err := c.NodeStageVolume(context.Background(), req) @@ -557,11 +557,11 @@ var _ = Describe("NodeStageVolume [Node Server]", func() { }) It("should return appropriate values (no optional values added)", func() { - testFullWorkflowSuccess(s, c, controllerPublishSupported, nodeStageSupported) + testFullWorkflowSuccess(sc, s, c, controllerPublishSupported, nodeStageSupported) }) }) -var _ = Describe("NodeUnstageVolume [Node Server]", func() { +var _ = DescribeSanity("NodeUnstageVolume [Node Server]", func(sc *SanityContext) { var ( s csi.ControllerClient c csi.NodeClient @@ -570,14 +570,14 @@ var _ = Describe("NodeUnstageVolume [Node Server]", func() { ) BeforeEach(func() { - s = csi.NewControllerClient(conn) - c = csi.NewNodeClient(conn) + s = csi.NewControllerClient(sc.Conn) + c = csi.NewNodeClient(sc.Conn) controllerPublishSupported = isControllerCapabilitySupported( s, csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME) nodeStageSupported = isNodeCapabilitySupported(c, csi.NodeServiceCapability_RPC_STAGE_UNSTAGE_VOLUME) if nodeStageSupported { - err := createMountTargetLocation(config.StagingPath) + err := createMountTargetLocation(sc.Config.StagingPath) Expect(err).NotTo(HaveOccurred()) } else { Skip("NodeUnstageVolume not supported") @@ -589,7 +589,7 @@ var _ = Describe("NodeUnstageVolume [Node Server]", func() { _, err := c.NodeUnstageVolume( context.Background(), &csi.NodeUnstageVolumeRequest{ - StagingTargetPath: config.StagingPath, + StagingTargetPath: sc.Config.StagingPath, }) Expect(err).To(HaveOccurred()) @@ -613,6 +613,6 @@ var _ = Describe("NodeUnstageVolume [Node Server]", func() { }) It("should return appropriate values (no optional values added)", func() { - testFullWorkflowSuccess(s, c, controllerPublishSupported, nodeStageSupported) + testFullWorkflowSuccess(sc, s, c, controllerPublishSupported, nodeStageSupported) }) }) diff --git a/pkg/sanity/sanity.go b/pkg/sanity/sanity.go index 58d63b70..049851ed 100644 --- a/pkg/sanity/sanity.go +++ b/pkg/sanity/sanity.go @@ -20,7 +20,6 @@ import ( "fmt" "io/ioutil" "os" - "sync" "testing" "github.com/kubernetes-csi/csi-test/utils" @@ -42,14 +41,8 @@ type CSISecrets struct { NodePublishVolumeSecret map[string]string `yaml:"NodePublishVolumeSecret"` } -var ( - config *Config - conn *grpc.ClientConn - lock sync.Mutex - secrets *CSISecrets -) - -// Config provides the configuration for the sanity tests +// Config provides the configuration for the sanity tests. It +// needs to be initialized by the user of the sanity package. type Config struct { TargetPath string StagingPath string @@ -58,40 +51,61 @@ type Config struct { TestVolumeSize int64 } -// Test will test the CSI driver at the specified address +// SanityContext holds the variables that each test can depend on. It +// gets initialized before each test block runs. +type SanityContext struct { + Config *Config + Conn *grpc.ClientConn + Secrets *CSISecrets +} + +// 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) { - lock.Lock() - defer lock.Unlock() + sc := &SanityContext{ + Config: reqConfig, + } - config = reqConfig + registerTestsInGinkgo(sc) RegisterFailHandler(Fail) RunSpecs(t, "CSI Driver Test Suite") } -var _ = BeforeSuite(func() { +func GinkgoTest(reqConfig *Config) { + sc := &SanityContext{ + Config: reqConfig, + } + + registerTestsInGinkgo(sc) +} + +func (sc *SanityContext) setup() { var err error - if len(config.SecretsFile) > 0 { - secrets, err = loadSecrets(config.SecretsFile) + if len(sc.Config.SecretsFile) > 0 { + sc.Secrets, err = loadSecrets(sc.Config.SecretsFile) Expect(err).NotTo(HaveOccurred()) } By("connecting to CSI driver") - conn, err = utils.Connect(config.Address) + sc.Conn, err = utils.Connect(sc.Config.Address) Expect(err).NotTo(HaveOccurred()) By("creating mount and staging directories") - err = createMountTargetLocation(config.TargetPath) + err = createMountTargetLocation(sc.Config.TargetPath) Expect(err).NotTo(HaveOccurred()) - if len(config.StagingPath) > 0 { - err = createMountTargetLocation(config.StagingPath) + if len(sc.Config.StagingPath) > 0 { + err = createMountTargetLocation(sc.Config.StagingPath) Expect(err).NotTo(HaveOccurred()) } -}) +} -var _ = AfterSuite(func() { - conn.Close() -}) +func (sc *SanityContext) teardown() { + if sc.Conn != nil { + sc.Conn.Close() + sc.Conn = nil + } +} func createMountTargetLocation(targetPath string) error { fileInfo, err := os.Stat(targetPath) diff --git a/pkg/sanity/tests.go b/pkg/sanity/tests.go new file mode 100644 index 00000000..e11f0d76 --- /dev/null +++ b/pkg/sanity/tests.go @@ -0,0 +1,57 @@ +/* +Copyright 2018 Intel Corporation + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package sanity + +import ( + . "github.com/onsi/ginkgo" +) + +type test struct { + text string + body func(*SanityContext) +} + +var tests []test + +// DescribeSanity must be used instead of the usual Ginkgo Describe to +// register a test block. The difference is that the body function +// will be called multiple times with the right context (when +// setting up a Ginkgo suite or a testing.T test, with the right +// configuration). +func DescribeSanity(text string, body func(*SanityContext)) bool { + tests = append(tests, test{text, body}) + return true +} + +// registerTestsInGinkgo invokes the actual Gingko Describe +// for the tests registered earlier with DescribeSanity. +func registerTestsInGinkgo(sc *SanityContext) { + for _, test := range tests { + Describe(test.text, func() { + + BeforeEach(func() { + sc.setup() + }) + + AfterEach(func() { + sc.teardown() + }) + + test.body(sc) + }) + } +}