Skip to content

Commit f272f52

Browse files
authored
Merge pull request #93 from pohly/test-suite
test suite reorganization
2 parents 2be3063 + 275a086 commit f272f52

File tree

12 files changed

+1515
-1540
lines changed

12 files changed

+1515
-1540
lines changed

.travis.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ matrix:
33
include:
44
- go: 1.10.3
55
script:
6-
- go fmt $(go list ./... | grep -v vendor) | wc -l | grep 0
7-
- go vet $(go list ./... | grep -v vendor)
8-
- go test $(go list ./... | grep -v vendor | grep -v "cmd/csi-sanity")
9-
- ./hack/e2e.sh
6+
- make test
107
after_success:
118
- if [ "${TRAVIS_BRANCH}" == "master" ] && [ "${TRAVIS_PULL_REQUEST}" == "false" ]; then
129
docker login -u "${DOCKER_USERNAME}" -p "${DOCKER_PASSWORD}" quay.io;

Makefile

+12-2
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,15 @@ container: $(APP)
3838
push: container
3939
docker push $(IMAGE_NAME):$(IMAGE_VERSION)
4040

41-
.PHONY: all clean container push
42-
41+
test:
42+
files=$$(find ./ -name '*.go' | grep -v '^./vendor' ); \
43+
if [ $$(gofmt -d $$files | wc -l) -ne 0 ]; then \
44+
echo "formatting errors:"; \
45+
gofmt -d $$files; \
46+
false; \
47+
fi
48+
go vet $$(go list ./... | grep -v vendor)
49+
go test $$(go list ./... | grep -v vendor | grep -v "cmd/csi-sanity")
50+
./hack/e2e.sh
51+
52+
.PHONY: all clean container push test

driver/driver.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ limitations under the License.
1919
package driver
2020

2121
import (
22-
context "context"
22+
"context"
2323
"errors"
2424
"net"
2525
"sync"
2626

2727
"google.golang.org/grpc/codes"
2828
"google.golang.org/grpc/status"
2929

30-
csi "github.com/container-storage-interface/spec/lib/go/csi/v0"
30+
"github.com/container-storage-interface/spec/lib/go/csi/v0"
3131
"google.golang.org/grpc"
3232
"google.golang.org/grpc/reflection"
3333
)

hack/e2e.sh

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ CSI_MOCK_VERSION="master"
1111
# See https://github.com/grpc/grpc/blob/master/doc/naming.md
1212
runTest()
1313
{
14-
CSI_ENDPOINT=$1 mock &
14+
CSI_ENDPOINT=$1 ./bin/mock &
1515
local pid=$!
1616

17-
csi-sanity $TESTARGS --csi.endpoint=$2; ret=$?
17+
./cmd/csi-sanity/csi-sanity $TESTARGS --csi.endpoint=$2; ret=$?
1818
kill -9 $pid
1919

2020
if [ $ret -ne 0 ] ; then
@@ -24,18 +24,18 @@ runTest()
2424

2525
runTestWithCreds()
2626
{
27-
CSI_ENDPOINT=$1 CSI_ENABLE_CREDS=true mock &
27+
CSI_ENDPOINT=$1 CSI_ENABLE_CREDS=true ./bin/mock &
2828
local pid=$!
2929

30-
csi-sanity $TESTARGS --csi.endpoint=$2 --csi.secrets=mock/mocksecret.yaml; ret=$?
30+
./cmd/csi-sanity/csi-sanity $TESTARGS --csi.endpoint=$2 --csi.secrets=mock/mocksecret.yaml; ret=$?
3131
kill -9 $pid
3232

3333
if [ $ret -ne 0 ] ; then
3434
exit $ret
3535
fi
3636
}
3737

38-
go install ./mock || exit 1
38+
go build -o bin/mock ./mock || exit 1
3939

4040
cd cmd/csi-sanity
4141
make clean install || exit 1

pkg/sanity/cleanup.go

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
Copyright 2018 Intel Corporation
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package sanity
18+
19+
import (
20+
"context"
21+
"log"
22+
23+
"github.com/container-storage-interface/spec/lib/go/csi/v0"
24+
25+
. "github.com/onsi/ginkgo"
26+
)
27+
28+
// VolumeInfo keeps track of the information needed to delete a volume.
29+
type VolumeInfo struct {
30+
// Node on which the volume was published, empty if none
31+
// or publishing is not supported.
32+
NodeID string
33+
34+
// Volume ID assigned by CreateVolume.
35+
VolumeID string
36+
}
37+
38+
// Cleanup keeps track of resources, in particular volumes, which need
39+
// to be freed when testing is done.
40+
type Cleanup struct {
41+
Context *SanityContext
42+
ControllerClient csi.ControllerClient
43+
NodeClient csi.NodeClient
44+
ControllerPublishSupported bool
45+
NodeStageSupported bool
46+
47+
// Maps from volume name to the node ID for which the volume
48+
// is published and the volume ID.
49+
volumes map[string]VolumeInfo
50+
}
51+
52+
// RegisterVolume adds or updates an entry for the volume with the
53+
// given name.
54+
func (cl *Cleanup) RegisterVolume(name string, info VolumeInfo) {
55+
if cl.volumes == nil {
56+
cl.volumes = make(map[string]VolumeInfo)
57+
}
58+
cl.volumes[name] = info
59+
}
60+
61+
// MaybeRegisterVolume adds or updates an entry for the volume with
62+
// the given name if CreateVolume was successful.
63+
func (cl *Cleanup) MaybeRegisterVolume(name string, vol *csi.CreateVolumeResponse, err error) {
64+
if err == nil && vol.GetVolume().GetId() != "" {
65+
cl.RegisterVolume(name, VolumeInfo{VolumeID: vol.GetVolume().GetId()})
66+
}
67+
}
68+
69+
// UnregisterVolume removes the entry for the volume with the
70+
// given name, thus preventing all cleanup operations for it.
71+
func (cl *Cleanup) UnregisterVolume(name string) {
72+
if cl.volumes != nil {
73+
delete(cl.volumes, name)
74+
}
75+
}
76+
77+
// DeleteVolumes stops using the registered volumes and tries to delete all of them.
78+
func (cl *Cleanup) DeleteVolumes() {
79+
if cl.volumes == nil {
80+
return
81+
}
82+
logger := log.New(GinkgoWriter, "cleanup: ", 0)
83+
ctx := context.Background()
84+
85+
for name, info := range cl.volumes {
86+
logger.Printf("deleting %s = %s", name, info.VolumeID)
87+
if _, err := cl.NodeClient.NodeUnpublishVolume(
88+
ctx,
89+
&csi.NodeUnpublishVolumeRequest{
90+
VolumeId: info.VolumeID,
91+
TargetPath: cl.Context.Config.TargetPath,
92+
},
93+
); err != nil {
94+
logger.Printf("warning: NodeUnpublishVolume: %s", err)
95+
}
96+
97+
if cl.NodeStageSupported {
98+
if _, err := cl.NodeClient.NodeUnstageVolume(
99+
ctx,
100+
&csi.NodeUnstageVolumeRequest{
101+
VolumeId: info.VolumeID,
102+
StagingTargetPath: cl.Context.Config.StagingPath,
103+
},
104+
); err != nil {
105+
logger.Printf("warning: NodeUnstageVolume: %s", err)
106+
}
107+
}
108+
109+
if cl.ControllerPublishSupported && info.NodeID != "" {
110+
if _, err := cl.ControllerClient.ControllerUnpublishVolume(
111+
ctx,
112+
&csi.ControllerUnpublishVolumeRequest{
113+
VolumeId: info.VolumeID,
114+
NodeId: info.NodeID,
115+
ControllerUnpublishSecrets: cl.Context.Secrets.ControllerUnpublishVolumeSecret,
116+
},
117+
); err != nil {
118+
logger.Printf("warning: ControllerUnpublishVolume: %s", err)
119+
}
120+
}
121+
122+
if _, err := cl.ControllerClient.DeleteVolume(
123+
ctx,
124+
&csi.DeleteVolumeRequest{
125+
VolumeId: info.VolumeID,
126+
ControllerDeleteSecrets: cl.Context.Secrets.DeleteVolumeSecret,
127+
},
128+
); err != nil {
129+
logger.Printf("error: DeleteVolume: %s", err)
130+
}
131+
132+
cl.UnregisterVolume(name)
133+
}
134+
}

0 commit comments

Comments
 (0)