Skip to content

Commit

Permalink
Sanity Test Suite
Browse files Browse the repository at this point in the history
  • Loading branch information
lpabon committed Dec 18, 2017
1 parent b46921b commit 631b186
Show file tree
Hide file tree
Showing 8 changed files with 408 additions and 1 deletion.
7 changes: 7 additions & 0 deletions cmd/csi-sanity/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
all: csi-sanity

csi-sanity: sanity_test.go
go test -c -o csi-sanity

clean:
rm -f csi-sanity
27 changes: 27 additions & 0 deletions cmd/csi-sanity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Sanity Test Command Line Program

Once built using `make`, type:

```
$ csi-sanity --csi.endpoint=<your csi driver endpoint>
```

For verbose type:

```
$ csi-sanity --ginkgo.v --csi.endpoint=<your csi driver endpoint>
```

### Help
The full Ginkgo and golang unit test parameters are available. Type

```
$ csi-sanity -h
```

to get more information

### Download

Please see the [Releases](https://github.com/kubernetes-csi/csi-test/releases) page
to download the latest version of `csi-sanity`
43 changes: 43 additions & 0 deletions cmd/csi-sanity/sanity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2017 Luis Pabón [email protected]
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 (
"flag"
"testing"

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

const (
prefix string = "csi."
)

var (
endpoint string
)

func init() {
flag.StringVar(&endpoint, prefix+"endpoint", "", "CSI endpoint")
flag.Parse()
}

func TestSanity(t *testing.T) {
if len(endpoint) == 0 {
t.Fatalf("--%s.endpoint must be provided with an CSI endpoint", prefix)
}
sanity.Test(t, endpoint)
}
71 changes: 70 additions & 1 deletion glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions pkg/sanity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# CSI Driver Sanity Tester

This library provides a simple way to ensure that a CSI driver conforms to
the CSI specification. There are two ways to leverage this testing framework.
For CSI drivers written in Golang, the framework provides a simple API function
to call to test the driver. Another way to run the test suite is to use the
command line program [csi-sanity](../../cmd/sanity).

## For Golang CSI Drivers
This framework leverages the Ginkgo BDD testing framework to deliver a descriptive
test suite for your driver. To test your driver, simply call the API in one of your
Golang `TestXXX` functions. For example:

```go
func TestMyDriver(t *testing.T) {
// Setup the full driver and its environment
... setup driver ...

// Now call the test suite
sanity.Test(t, driverEndpointAddress)
}
```

## Command line program
Please see [csi-sanity](../../cmd/sanity)
114 changes: 114 additions & 0 deletions pkg/sanity/identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
Copyright 2017 Luis Pabón [email protected]
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 (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/container-storage-interface/spec/lib/go/csi"
context "golang.org/x/net/context"
"google.golang.org/grpc"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var (
csiClientVersion = &csi.Version{
Major: 0,
Minor: 1,
Patch: 0,
}
)

var _ = Describe("GetSupportedVersions [Identity Server]", func() {
var (
c csi.IdentityClient
conn *grpc.ClientConn
)

BeforeEach(func() {
var err error
conn, err = grpc.Dial(driverAddress, grpc.WithInsecure())
Expect(err).ToNot(HaveOccurred())
c = csi.NewIdentityClient(conn)
})

AfterEach(func() {
conn.Close()
})

It("should return an array of supported versions", func() {
res, err := c.GetSupportedVersions(
context.Background(),
&csi.GetSupportedVersionsRequest{})

By("checking response to have supported versions list")
Expect(err).NotTo(HaveOccurred())
Expect(res.GetSupportedVersions()).NotTo(BeNil())
Expect(len(res.GetSupportedVersions()) >= 1).To(BeTrue())

By("checking each version")
for _, version := range res.GetSupportedVersions() {
Expect(version).NotTo(BeNil())
Expect(version.GetMajor()).To(BeNumerically("<", 100))
Expect(version.GetMinor()).To(BeNumerically("<", 100))
Expect(version.GetPatch()).To(BeNumerically("<", 100))
}
})
})

var _ = Describe("GetPluginInfo [Identity Server]", func() {
var (
c csi.IdentityClient
conn *grpc.ClientConn
)

BeforeEach(func() {
var err error
conn, err = grpc.Dial(driverAddress, grpc.WithInsecure())
Expect(err).ToNot(HaveOccurred())
c = csi.NewIdentityClient(conn)
})

AfterEach(func() {
conn.Close()
})

It("should fail when no version is provided", func() {
_, err := c.GetPluginInfo(context.Background(), &csi.GetPluginInfoRequest{})
Expect(err).To(HaveOccurred())

serverError, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(serverError.Code()).To(Equal(codes.InvalidArgument))
})

It("should return appropriate information", func() {
req := &csi.GetPluginInfoRequest{
Version: csiClientVersion,
}
res, err := c.GetPluginInfo(context.Background(), req)
Expect(err).NotTo(HaveOccurred())
Expect(res).NotTo(BeNil())

By("Verifying name size and characters")
Expect(res.GetName()).ToNot(HaveLen(0))
Expect(len(res.GetName())).To(BeNumerically("<=", 63))
})
})
40 changes: 40 additions & 0 deletions pkg/sanity/sanity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2017 Luis Pabón [email protected]
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 (
"sync"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var (
driverAddress string
lock sync.Mutex
)

// Test will test the CSI driver at the specified address
func Test(t *testing.T, address string) {
lock.Lock()
defer lock.Unlock()

driverAddress = address
RegisterFailHandler(Fail)
RunSpecs(t, "CSI Driver Test Suite")
}
Loading

0 comments on commit 631b186

Please sign in to comment.