From 978918154f3913affcd88d5caae5bb015dcfb534 Mon Sep 17 00:00:00 2001 From: Sunny Date: Sat, 4 Aug 2018 15:29:02 +0530 Subject: [PATCH 1/2] pkg/sanity: Use a single grpc connection This change makes the grpc client connection to be stored as a global variable and used across all the test cases. This helps avoid creating multiple connections at every setup and teardown cycle. The whole test suite runs faster and more reliable with a single shared connection. --- cmd/csi-sanity/Makefile | 2 +- pkg/sanity/controller.go | 13 +++++++------ pkg/sanity/identity.go | 2 +- pkg/sanity/node.go | 6 +++--- pkg/sanity/sanity.go | 26 +++++++++++++++++--------- 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/cmd/csi-sanity/Makefile b/cmd/csi-sanity/Makefile index 520c2153..83baeaca 100644 --- a/cmd/csi-sanity/Makefile +++ b/cmd/csi-sanity/Makefile @@ -1,5 +1,5 @@ APP_NAME := csi-sanity -VER :=$(shell git describe) +VER :=$(shell git describe --tag --dirty) RELEASEVER := $(shell git describe --abbrev=0) BRANCH := $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD)) SHA := $(shell git rev-parse --short HEAD) diff --git a/pkg/sanity/controller.go b/pkg/sanity/controller.go index 294a1e0d..c92c85bb 100644 --- a/pkg/sanity/controller.go +++ b/pkg/sanity/controller.go @@ -25,9 +25,10 @@ import ( "github.com/container-storage-interface/spec/lib/go/csi/v0" + "strconv" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "strconv" ) const ( @@ -86,8 +87,8 @@ var _ = DescribeSanity("Controller Service", func(sc *SanityContext) { ) BeforeEach(func() { - c = csi.NewControllerClient(sc.Conn) - n = csi.NewNodeClient(sc.Conn) + c = csi.NewControllerClient(conn) + n = csi.NewNodeClient(conn) cl = &Cleanup{ NodeClient: n, @@ -1098,7 +1099,7 @@ var _ = DescribeSanity("ListSnapshots [Controller Server]", func(sc *SanityConte ) BeforeEach(func() { - c = csi.NewControllerClient(sc.Conn) + c = csi.NewControllerClient(conn) if !isControllerCapabilitySupported(c, csi.ControllerServiceCapability_RPC_LIST_SNAPSHOTS) { Skip("ListSnapshots not supported") @@ -1392,7 +1393,7 @@ var _ = DescribeSanity("DeleteSnapshot [Controller Server]", func(sc *SanityCont ) BeforeEach(func() { - c = csi.NewControllerClient(sc.Conn) + c = csi.NewControllerClient(conn) if !isControllerCapabilitySupported(c, csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT) { Skip("DeleteSnapshot not supported") @@ -1455,7 +1456,7 @@ var _ = DescribeSanity("CreateSnapshot [Controller Server]", func(sc *SanityCont ) BeforeEach(func() { - c = csi.NewControllerClient(sc.Conn) + c = csi.NewControllerClient(conn) if !isControllerCapabilitySupported(c, csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT) { Skip("CreateSnapshot not supported") diff --git a/pkg/sanity/identity.go b/pkg/sanity/identity.go index e60439b3..9595d3f2 100644 --- a/pkg/sanity/identity.go +++ b/pkg/sanity/identity.go @@ -36,7 +36,7 @@ var _ = DescribeSanity("Identity Service", func(sc *SanityContext) { ) BeforeEach(func() { - c = csi.NewIdentityClient(sc.Conn) + c = csi.NewIdentityClient(conn) }) Describe("GetPluginCapabilities", func() { diff --git a/pkg/sanity/node.go b/pkg/sanity/node.go index a98f5151..8c4d2f2c 100644 --- a/pkg/sanity/node.go +++ b/pkg/sanity/node.go @@ -79,8 +79,8 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) { ) BeforeEach(func() { - c = csi.NewNodeClient(sc.Conn) - s = csi.NewControllerClient(sc.Conn) + c = csi.NewNodeClient(conn) + s = csi.NewControllerClient(conn) controllerPublishSupported = isControllerCapabilitySupported( s, @@ -145,7 +145,7 @@ var _ = DescribeSanity("Node Service", func(sc *SanityContext) { ) BeforeEach(func() { - i = csi.NewIdentityClient(sc.Conn) + i = csi.NewIdentityClient(conn) accessibilityConstraintSupported = isPluginCapabilitySupported(i, csi.PluginCapability_Service_ACCESSIBILITY_CONSTRAINTS) }) diff --git a/pkg/sanity/sanity.go b/pkg/sanity/sanity.go index 9a4de8be..4dde314f 100644 --- a/pkg/sanity/sanity.go +++ b/pkg/sanity/sanity.go @@ -58,10 +58,14 @@ type Config struct { // gets initialized before each test block runs. type SanityContext struct { Config *Config - Conn *grpc.ClientConn Secrets *CSISecrets } +var ( + conn *grpc.ClientConn + address string +) + // 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) { @@ -69,11 +73,23 @@ func Test(t *testing.T, reqConfig *Config) { Config: reqConfig, } + address = sc.Config.Address registerTestsInGinkgo(sc) RegisterFailHandler(Fail) RunSpecs(t, "CSI Driver Test Suite") } +var _ = BeforeSuite(func() { + var err error + By("connecting to CSI driver") + conn, err = utils.Connect(address) + Expect(err).NotTo(HaveOccurred()) +}) + +var _ = AfterSuite(func() { + conn.Close() +}) + func GinkgoTest(reqConfig *Config) { sc := &SanityContext{ Config: reqConfig, @@ -92,10 +108,6 @@ func (sc *SanityContext) setup() { sc.Secrets = &CSISecrets{} } - By("connecting to CSI driver") - sc.Conn, err = utils.Connect(sc.Config.Address) - Expect(err).NotTo(HaveOccurred()) - By("creating mount and staging directories") err = createMountTargetLocation(sc.Config.TargetPath) Expect(err).NotTo(HaveOccurred()) @@ -106,10 +118,6 @@ func (sc *SanityContext) setup() { } func (sc *SanityContext) teardown() { - if sc.Conn != nil { - sc.Conn.Close() - sc.Conn = nil - } } func createMountTargetLocation(targetPath string) error { From 241488ec5b9038223cfa122478cfafa10f325070 Mon Sep 17 00:00:00 2001 From: Sunny Date: Sun, 5 Aug 2018 15:40:07 +0530 Subject: [PATCH 2/2] Add e2e tests for go api and embedded use --- hack/_apitest/api_test.go | 18 ++++++++++++++++++ hack/_embedded/embedded_test.go | 33 +++++++++++++++++++++++++++++++++ hack/e2e.sh | 22 ++++++++++++++++++++++ pkg/sanity/sanity.go | 1 + 4 files changed, 74 insertions(+) create mode 100644 hack/_apitest/api_test.go create mode 100644 hack/_embedded/embedded_test.go diff --git a/hack/_apitest/api_test.go b/hack/_apitest/api_test.go new file mode 100644 index 00000000..10ea5f35 --- /dev/null +++ b/hack/_apitest/api_test.go @@ -0,0 +1,18 @@ +package apitest + +import ( + "os" + "testing" + + "github.com/kubernetes-csi/csi-test/pkg/sanity" +) + +func TestMyDriver(t *testing.T) { + config := &sanity.Config{ + TargetPath: os.TempDir() + "/csi", + StagingPath: os.TempDir() + "/csi", + Address: "/tmp/e2e-csi-sanity.sock", + } + + sanity.Test(t, config) +} diff --git a/hack/_embedded/embedded_test.go b/hack/_embedded/embedded_test.go new file mode 100644 index 00000000..142add5d --- /dev/null +++ b/hack/_embedded/embedded_test.go @@ -0,0 +1,33 @@ +package embedded + +import ( + "os" + "testing" + + "github.com/kubernetes-csi/csi-test/pkg/sanity" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestMyDriverGinkgo(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "CSI Sanity Test Suite") +} + +var _ = Describe("MyCSIDriver", func() { + Context("Config A", func() { + config := &sanity.Config{ + TargetPath: os.TempDir() + "/csi", + StagingPath: os.TempDir() + "/csi", + Address: "/tmp/e2e-csi-sanity.sock", + } + + BeforeEach(func() {}) + + AfterEach(func() {}) + + Describe("CSI Driver Test Suite", func() { + sanity.GinkgoTest(config) + }) + }) +}) diff --git a/hack/e2e.sh b/hack/e2e.sh index 777250eb..38fcd151 100755 --- a/hack/e2e.sh +++ b/hack/e2e.sh @@ -35,6 +35,25 @@ runTestWithCreds() fi } +runTestAPI() +{ + CSI_ENDPOINT=$1 ./bin/mock & + local pid=$! + + GOCACHE=off go test -v ./hack/_apitest/api_test.go; ret=$? + + if [ $ret -ne 0 ] ; then + exit $ret + fi + + GOCACHE=off go test -v ./hack/_embedded/embedded_test.go; ret=$? + kill -9 $pid + + if [ $ret -ne 0 ] ; then + exit $ret + fi +} + go build -o bin/mock ./mock || exit 1 cd cmd/csi-sanity @@ -47,4 +66,7 @@ rm -f $UDS runTestWithCreds "${UDS}" "${UDS}" rm -f $UDS +runTestAPI "${UDS}" +rm -f $UDS + exit 0 diff --git a/pkg/sanity/sanity.go b/pkg/sanity/sanity.go index 4dde314f..f7a13776 100644 --- a/pkg/sanity/sanity.go +++ b/pkg/sanity/sanity.go @@ -95,6 +95,7 @@ func GinkgoTest(reqConfig *Config) { Config: reqConfig, } + address = sc.Config.Address registerTestsInGinkgo(sc) }