@@ -18,6 +18,7 @@ package connection
18
18
19
19
import (
20
20
"context"
21
+ "fmt"
21
22
"io/ioutil"
22
23
"net"
23
24
"os"
@@ -33,6 +34,8 @@ import (
33
34
34
35
"github.com/stretchr/testify/assert"
35
36
"github.com/stretchr/testify/require"
37
+
38
+ "github.com/container-storage-interface/spec/lib/go/csi"
36
39
)
37
40
38
41
func tmpDir (t * testing.T ) string {
@@ -48,11 +51,14 @@ const (
48
51
// startServer creates a gRPC server without any registered services.
49
52
// The returned address can be used to connect to it. The cleanup
50
53
// function stops it. It can be called multiple times.
51
- func startServer (t * testing.T , tmp string ) (string , func ()) {
54
+ func startServer (t * testing.T , tmp string , identity csi. IdentityServer ) (string , func ()) {
52
55
addr := path .Join (tmp , serverSock )
53
56
listener , err := net .Listen ("unix" , addr )
54
57
require .NoError (t , err , "listening on %s" , addr )
55
58
server := grpc .NewServer ()
59
+ if identity != nil {
60
+ csi .RegisterIdentityServer (server , identity )
61
+ }
56
62
var wg sync.WaitGroup
57
63
wg .Add (1 )
58
64
go func () {
@@ -73,7 +79,7 @@ func startServer(t *testing.T, tmp string) (string, func()) {
73
79
func TestConnect (t * testing.T ) {
74
80
tmp := tmpDir (t )
75
81
defer os .RemoveAll (tmp )
76
- addr , stopServer := startServer (t , tmp )
82
+ addr , stopServer := startServer (t , tmp , nil )
77
83
defer stopServer ()
78
84
79
85
conn , err := Connect (addr )
@@ -88,7 +94,7 @@ func TestConnect(t *testing.T) {
88
94
func TestConnectUnix (t * testing.T ) {
89
95
tmp := tmpDir (t )
90
96
defer os .RemoveAll (tmp )
91
- addr , stopServer := startServer (t , tmp )
97
+ addr , stopServer := startServer (t , tmp , nil )
92
98
defer stopServer ()
93
99
94
100
conn , err := Connect ("unix:///" + addr )
@@ -129,7 +135,7 @@ func TestWaitForServer(t *testing.T) {
129
135
t .Logf ("sleeping %s before starting server" , delay )
130
136
time .Sleep (delay )
131
137
startTimeServer = time .Now ()
132
- _ , stopServer = startServer (t , tmp )
138
+ _ , stopServer = startServer (t , tmp , nil )
133
139
}()
134
140
conn , err := Connect (path .Join (tmp , serverSock ))
135
141
if assert .NoError (t , err , "connect via absolute path" ) {
@@ -163,7 +169,7 @@ func TestTimout(t *testing.T) {
163
169
func TestReconnect (t * testing.T ) {
164
170
tmp := tmpDir (t )
165
171
defer os .RemoveAll (tmp )
166
- addr , stopServer := startServer (t , tmp )
172
+ addr , stopServer := startServer (t , tmp , nil )
167
173
defer func () {
168
174
stopServer ()
169
175
}()
@@ -190,7 +196,7 @@ func TestReconnect(t *testing.T) {
190
196
}
191
197
192
198
// No reconnection either when the server comes back.
193
- _ , stopServer = startServer (t , tmp )
199
+ _ , stopServer = startServer (t , tmp , nil )
194
200
// We need to give gRPC some time. It does not attempt to reconnect
195
201
// immediately. If we send the method call too soon, the test passes
196
202
// even though a later method call will go through again.
@@ -208,7 +214,7 @@ func TestReconnect(t *testing.T) {
208
214
func TestDisconnect (t * testing.T ) {
209
215
tmp := tmpDir (t )
210
216
defer os .RemoveAll (tmp )
211
- addr , stopServer := startServer (t , tmp )
217
+ addr , stopServer := startServer (t , tmp , nil )
212
218
defer func () {
213
219
stopServer ()
214
220
}()
@@ -239,7 +245,7 @@ func TestDisconnect(t *testing.T) {
239
245
}
240
246
241
247
// No reconnection either when the server comes back.
242
- _ , stopServer = startServer (t , tmp )
248
+ _ , stopServer = startServer (t , tmp , nil )
243
249
// We need to give gRPC some time. It does not attempt to reconnect
244
250
// immediately. If we send the method call too soon, the test passes
245
251
// even though a later method call will go through again.
@@ -259,7 +265,7 @@ func TestDisconnect(t *testing.T) {
259
265
func TestExplicitReconnect (t * testing.T ) {
260
266
tmp := tmpDir (t )
261
267
defer os .RemoveAll (tmp )
262
- addr , stopServer := startServer (t , tmp )
268
+ addr , stopServer := startServer (t , tmp , nil )
263
269
defer func () {
264
270
stopServer ()
265
271
}()
@@ -290,7 +296,7 @@ func TestExplicitReconnect(t *testing.T) {
290
296
}
291
297
292
298
// No reconnection either when the server comes back.
293
- _ , stopServer = startServer (t , tmp )
299
+ _ , stopServer = startServer (t , tmp , nil )
294
300
// We need to give gRPC some time. It does not attempt to reconnect
295
301
// immediately. If we send the method call too soon, the test passes
296
302
// even though a later method call will go through again.
@@ -306,3 +312,87 @@ func TestExplicitReconnect(t *testing.T) {
306
312
assert .Equal (t , 1 , reconnectCount , "connection loss callback should be called once" )
307
313
}
308
314
}
315
+
316
+ func TestGetDriverName (t * testing.T ) {
317
+ tests := []struct {
318
+ name string
319
+ output * csi.GetPluginInfoResponse
320
+ injectError bool
321
+ expectError bool
322
+ }{
323
+ {
324
+ name : "success" ,
325
+ output : & csi.GetPluginInfoResponse {
326
+ Name : "csi/example" ,
327
+ VendorVersion : "0.2.0" ,
328
+ Manifest : map [string ]string {
329
+ "hello" : "world" ,
330
+ },
331
+ },
332
+ expectError : false ,
333
+ },
334
+ {
335
+ name : "gRPC error" ,
336
+ output : nil ,
337
+ injectError : true ,
338
+ expectError : true ,
339
+ },
340
+ {
341
+ name : "empty name" ,
342
+ output : & csi.GetPluginInfoResponse {
343
+ Name : "" ,
344
+ },
345
+ expectError : true ,
346
+ },
347
+ }
348
+
349
+ for _ , test := range tests {
350
+ t .Run (test .name , func (t * testing.T ) {
351
+ out := test .output
352
+ var injectedErr error
353
+ if test .injectError {
354
+ injectedErr = fmt .Errorf ("mock error" )
355
+ }
356
+
357
+ tmp := tmpDir (t )
358
+ defer os .RemoveAll (tmp )
359
+ identity := & identityServer {out , injectedErr }
360
+ addr , stopServer := startServer (t , tmp , identity )
361
+ defer func () {
362
+ stopServer ()
363
+ }()
364
+
365
+ conn , err := Connect (addr )
366
+
367
+ name , err := GetDriverName (context .Background (), conn )
368
+ if test .expectError && err == nil {
369
+ t .Errorf ("test %q: Expected error, got none" , test .name )
370
+ }
371
+ if ! test .expectError && err != nil {
372
+ t .Errorf ("test %q: got error: %v" , test .name , err )
373
+ }
374
+ if err == nil && name != "csi/example" {
375
+ t .Errorf ("got unexpected name: %q" , name )
376
+ }
377
+ })
378
+ }
379
+ }
380
+
381
+ type identityServer struct {
382
+ response * csi.GetPluginInfoResponse
383
+ err error
384
+ }
385
+
386
+ var _ csi.IdentityServer = & identityServer {}
387
+
388
+ func (i * identityServer ) GetPluginCapabilities (context.Context , * csi.GetPluginCapabilitiesRequest ) (* csi.GetPluginCapabilitiesResponse , error ) {
389
+ return nil , fmt .Errorf ("Not implemented" )
390
+ }
391
+
392
+ func (i * identityServer ) GetPluginInfo (context.Context , * csi.GetPluginInfoRequest ) (* csi.GetPluginInfoResponse , error ) {
393
+ return i .response , i .err
394
+ }
395
+
396
+ func (i * identityServer ) Probe (context.Context , * csi.ProbeRequest ) (* csi.ProbeResponse , error ) {
397
+ return nil , fmt .Errorf ("Not implemented" )
398
+ }
0 commit comments