Skip to content

Commit bcc7c32

Browse files
refactor test suite to inject fake client when necessary
Signed-off-by: Harsh Thakur <harshthakur9030@gmail.com>
1 parent 267080b commit bcc7c32

File tree

8 files changed

+271
-349
lines changed

8 files changed

+271
-349
lines changed

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch Package",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "auto",
12+
"program": "${fileDirname}"
13+
}
14+
]
15+
}

go.mod

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,72 @@
11
module github.com/civo/civo-csi
22

3+
go 1.22
4+
35
require (
46
github.com/BurntSushi/toml v0.3.1
57
github.com/civo/civogo v0.3.13
68
github.com/container-storage-interface/spec v1.6.0
79
github.com/joho/godotenv v1.4.0
810
github.com/kubernetes-csi/csi-test/v4 v4.4.0
9-
github.com/onsi/gomega v1.19.0
11+
github.com/onsi/gomega v1.27.4
1012
github.com/rs/zerolog v1.20.0
11-
github.com/stretchr/testify v1.7.1
12-
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
13-
golang.org/x/sys v0.0.0-20220209214540-3681064d5158
13+
github.com/stretchr/testify v1.8.1
14+
golang.org/x/sync v0.1.0
15+
golang.org/x/sys v0.6.0
1416
google.golang.org/grpc v1.47.0
15-
k8s.io/api v0.24.3
16-
k8s.io/apimachinery v0.24.3
17+
k8s.io/api v0.27.1
18+
k8s.io/apimachinery v0.27.1
1719
k8s.io/client-go v0.24.2
1820
k8s.io/mount-utils v0.24.3
1921
sigs.k8s.io/controller-runtime v0.12.3
2022
)
2123

22-
go 1.15
24+
require (
25+
github.com/davecgh/go-spew v1.1.1 // indirect
26+
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
27+
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
28+
github.com/fsnotify/fsnotify v1.5.1 // indirect
29+
github.com/go-logr/logr v1.2.3 // indirect
30+
github.com/go-openapi/jsonpointer v0.19.6 // indirect
31+
github.com/go-openapi/jsonreference v0.20.1 // indirect
32+
github.com/go-openapi/swag v0.22.3 // indirect
33+
github.com/gogo/protobuf v1.3.2 // indirect
34+
github.com/golang/protobuf v1.5.3 // indirect
35+
github.com/google/gnostic v0.5.7-v3refs // indirect
36+
github.com/google/go-cmp v0.5.9 // indirect
37+
github.com/google/go-querystring v1.1.0 // indirect
38+
github.com/google/gofuzz v1.1.0 // indirect
39+
github.com/google/uuid v1.3.0 // indirect
40+
github.com/imdario/mergo v0.3.12 // indirect
41+
github.com/josharian/intern v1.0.0 // indirect
42+
github.com/json-iterator/go v1.1.12 // indirect
43+
github.com/mailru/easyjson v0.7.7 // indirect
44+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
45+
github.com/modern-go/reflect2 v1.0.2 // indirect
46+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
47+
github.com/nxadm/tail v1.4.8 // indirect
48+
github.com/onsi/ginkgo v1.16.5 // indirect
49+
github.com/pkg/errors v0.9.1 // indirect
50+
github.com/pmezard/go-difflib v1.0.0 // indirect
51+
github.com/spf13/pflag v1.0.5 // indirect
52+
golang.org/x/net v0.8.0 // indirect
53+
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
54+
golang.org/x/term v0.6.0 // indirect
55+
golang.org/x/text v0.8.0 // indirect
56+
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
57+
google.golang.org/appengine v1.6.7 // indirect
58+
google.golang.org/genproto v0.0.0-20210402141018-6c239bbf2bb1 // indirect
59+
google.golang.org/protobuf v1.28.1 // indirect
60+
gopkg.in/inf.v0 v0.9.1 // indirect
61+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
62+
gopkg.in/yaml.v2 v2.4.0 // indirect
63+
gopkg.in/yaml.v3 v3.0.1 // indirect
64+
k8s.io/klog/v2 v2.90.1 // indirect
65+
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a // indirect
66+
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
67+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
68+
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
69+
sigs.k8s.io/yaml v1.3.0 // indirect
70+
)
71+
72+
replace github.com/civo/civogo => ../civogo

go.sum

Lines changed: 129 additions & 309 deletions
Large diffs are not rendered by default.

main_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ func TestCivoCSI(t *testing.T) {
1818
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
1919
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
2020

21-
d, _ := driver.NewTestDriver()
21+
fc, _ := civogo.NewFakeClient()
22+
d, _ := driver.NewTestDriver(fc)
2223

2324
ctx, cancel := context.WithCancel(context.Background())
2425

pkg/driver/controller_server_test.go

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
func TestCreateVolume(t *testing.T) {
1414
t.Run("Create a default size volume", func(t *testing.T) {
15-
d, _ := driver.NewTestDriver()
15+
d, _ := driver.NewTestDriver(nil)
1616

1717
resp, err := d.CreateVolume(context.Background(), &csi.CreateVolumeRequest{
1818
Name: "foo",
@@ -34,7 +34,7 @@ func TestCreateVolume(t *testing.T) {
3434
})
3535

3636
t.Run("Disallow block volumes", func(t *testing.T) {
37-
d, _ := driver.NewTestDriver()
37+
d, _ := driver.NewTestDriver(nil)
3838

3939
_, err := d.CreateVolume(context.Background(), &csi.CreateVolumeRequest{
4040
Name: "foo",
@@ -51,7 +51,7 @@ func TestCreateVolume(t *testing.T) {
5151
})
5252

5353
t.Run("Create a specified size volume", func(t *testing.T) {
54-
d, _ := driver.NewTestDriver()
54+
d, _ := driver.NewTestDriver(nil)
5555

5656
_, err := d.CreateVolume(context.Background(), &csi.CreateVolumeRequest{
5757
Name: "foo",
@@ -73,10 +73,11 @@ func TestCreateVolume(t *testing.T) {
7373
})
7474

7575
t.Run("Don't create if the volume already exists and just return it", func(t *testing.T) {
76-
d, _ := driver.NewTestDriver()
76+
d, _ := driver.NewTestDriver(nil)
7777

7878
volume, err := d.CivoClient.NewVolume(&civogo.VolumeConfig{
79-
Name: "foo",
79+
Name: "foo",
80+
SizeGigabytes: 10,
8081
})
8182
assert.Nil(t, err)
8283

@@ -101,7 +102,7 @@ func TestCreateVolume(t *testing.T) {
101102

102103
func TestDeleteVolume(t *testing.T) {
103104
t.Run("Delete a volume", func(t *testing.T) {
104-
d, _ := driver.NewTestDriver()
105+
d, _ := driver.NewTestDriver(nil)
105106

106107
volume, err := d.CivoClient.NewVolume(&civogo.VolumeConfig{
107108
Name: "foo",
@@ -120,12 +121,20 @@ func TestDeleteVolume(t *testing.T) {
120121

121122
func TestControllerPublishVolume(t *testing.T) {
122123
t.Run("Publish a volume", func(t *testing.T) {
123-
d, _ := driver.NewTestDriver()
124-
125-
instance, err := d.CivoClient.CreateInstance(&civogo.InstanceConfig{
124+
fc, _ := civogo.NewFakeClient()
125+
instanceID := "i-12345678"
126+
fc.Clusters = []civogo.KubernetesCluster{{
127+
ID: "12345678",
128+
Instances: []civogo.KubernetesInstance{{
129+
ID: instanceID,
130+
Hostname: "instance-1",
131+
}},
132+
}}
133+
fc.Instances = []civogo.Instance{{
134+
ID: instanceID,
126135
Hostname: "instance-1",
127-
})
128-
assert.Nil(t, err)
136+
}}
137+
d, _ := driver.NewTestDriver(fc)
129138

130139
volume, err := d.CivoClient.NewVolume(&civogo.VolumeConfig{
131140
Name: "foo",
@@ -134,19 +143,20 @@ func TestControllerPublishVolume(t *testing.T) {
134143

135144
_, err = d.ControllerPublishVolume(context.Background(), &csi.ControllerPublishVolumeRequest{
136145
VolumeId: volume.ID,
137-
NodeId: instance.ID,
146+
NodeId: instanceID,
138147
VolumeCapability: &csi.VolumeCapability{},
139148
})
140149
assert.Nil(t, err)
141150

142151
volumes, _ := d.CivoClient.ListVolumes()
143-
assert.Equal(t, instance.ID, volumes[0].InstanceID)
152+
assert.Equal(t, instanceID, volumes[0].InstanceID)
144153
})
145154
}
146155

147156
func TestControllerUnpublishVolume(t *testing.T) {
148157
t.Run("Unpublish a volume if attached to the correct node", func(t *testing.T) {
149-
d, _ := driver.NewTestDriver()
158+
fc, _ := civogo.NewFakeClient()
159+
d, _ := driver.NewTestDriver(fc)
150160

151161
volume, err := d.CivoClient.NewVolume(&civogo.VolumeConfig{
152162
Name: "foo",
@@ -167,7 +177,8 @@ func TestControllerUnpublishVolume(t *testing.T) {
167177
})
168178

169179
t.Run("Doesn't unpublish a volume if attached to a different node", func(t *testing.T) {
170-
d, _ := driver.NewTestDriver()
180+
fc, _ := civogo.NewFakeClient()
181+
d, _ := driver.NewTestDriver(fc)
171182

172183
volume, err := d.CivoClient.NewVolume(&civogo.VolumeConfig{
173184
Name: "foo",
@@ -190,7 +201,8 @@ func TestControllerUnpublishVolume(t *testing.T) {
190201

191202
func TestListVolumes(t *testing.T) {
192203
t.Run("Lists available existing volumes", func(t *testing.T) {
193-
d, _ := driver.NewTestDriver()
204+
fc, _ := civogo.NewFakeClient()
205+
d, _ := driver.NewTestDriver(fc)
194206

195207
volume, err := d.CivoClient.NewVolume(&civogo.VolumeConfig{
196208
Name: "foo",
@@ -209,7 +221,8 @@ func TestListVolumes(t *testing.T) {
209221

210222
func TestGetCapacity(t *testing.T) {
211223
t.Run("Has available capacity from usage and limit", func(t *testing.T) {
212-
d, _ := driver.NewTestDriver()
224+
fc, _ := civogo.NewFakeClient()
225+
d, _ := driver.NewTestDriver(fc)
213226

214227
civoClient, _ := civogo.NewFakeClient()
215228
d.CivoClient = civoClient
@@ -228,7 +241,8 @@ func TestGetCapacity(t *testing.T) {
228241
})
229242

230243
t.Run("Has no capacity from usage and limit", func(t *testing.T) {
231-
d, _ := driver.NewTestDriver()
244+
fc, _ := civogo.NewFakeClient()
245+
d, _ := driver.NewTestDriver(fc)
232246

233247
civoClient, _ := civogo.NewFakeClient()
234248
d.CivoClient = civoClient
@@ -247,7 +261,8 @@ func TestGetCapacity(t *testing.T) {
247261
})
248262

249263
t.Run("Has no capacity from volume count limit", func(t *testing.T) {
250-
d, _ := driver.NewTestDriver()
264+
fc, _ := civogo.NewFakeClient()
265+
d, _ := driver.NewTestDriver(fc)
251266

252267
civoClient, _ := civogo.NewFakeClient()
253268
d.CivoClient = civoClient

pkg/driver/driver.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,15 @@ func NewDriver(apiURL, apiKey, region, namespace, clusterID string) (*Driver, er
8686
}
8787

8888
// NewTestDriver returns a new Civo CSI driver specifically setup to call a fake Civo API
89-
func NewTestDriver() (*Driver, error) {
89+
func NewTestDriver(fc *civogo.FakeClient) (*Driver, error) {
9090
d, err := NewDriver("https://civo-api.example.com", "NO_API_KEY_NEEDED", "TEST1", "default", "12345678")
9191
d.SocketFilename = "unix:///tmp/civo-csi.sock"
92-
d.CivoClient, _ = civogo.NewFakeClient()
92+
if fc != nil {
93+
d.CivoClient = fc
94+
} else {
95+
d.CivoClient, _ = civogo.NewFakeClient()
96+
}
97+
9398
d.DiskHotPlugger = &FakeDiskHotPlugger{}
9499
d.TestMode = true // Just stops so much logging out of failures, as they are often expected during the tests
95100

pkg/driver/identity_server_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package driver_test
22

33
import (
44
"context"
5+
"fmt"
56
"testing"
67

78
"github.com/civo/civo-csi/pkg/driver"
@@ -23,6 +24,7 @@ func TestProbe(t *testing.T) {
2324

2425
func TestProbeUnhealthy(t *testing.T) {
2526
fc, _ := civogo.NewFakeClient()
27+
fc.PingErr = fmt.Errorf("something went wrong")
2628
d, _ := driver.NewTestDriver(fc)
2729

2830
_, err := d.Probe(context.Background(), &csi.ProbeRequest{})

0 commit comments

Comments
 (0)