forked from libopenstorage/openstorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsi_test.go
494 lines (430 loc) · 12.7 KB
/
csi_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
CSI Interface for OSD
Copyright 2017 Portworx
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 csi
import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"os"
"testing"
"time"
csi "github.com/container-storage-interface/spec/lib/go/csi"
jwt "github.com/dgrijalva/jwt-go"
"github.com/golang/mock/gomock"
"github.com/kubernetes-csi/csi-test/utils"
"github.com/libopenstorage/openstorage/api"
"github.com/libopenstorage/openstorage/api/server/sdk"
"github.com/libopenstorage/openstorage/cluster"
clustermanager "github.com/libopenstorage/openstorage/cluster/manager"
mockcluster "github.com/libopenstorage/openstorage/cluster/mock"
"github.com/libopenstorage/openstorage/config"
"github.com/libopenstorage/openstorage/pkg/auth"
"github.com/libopenstorage/openstorage/pkg/grpcserver"
"github.com/libopenstorage/openstorage/pkg/loadbalancer"
"github.com/libopenstorage/openstorage/pkg/options"
"github.com/libopenstorage/openstorage/pkg/role"
"github.com/libopenstorage/openstorage/pkg/storagepolicy"
"github.com/libopenstorage/openstorage/volume"
volumedrivers "github.com/libopenstorage/openstorage/volume/drivers"
mockdriver "github.com/libopenstorage/openstorage/volume/drivers/mock"
"github.com/portworx/kvdb"
"github.com/portworx/kvdb/mem"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
const (
mockDriverName = "mock"
testSharedSecret = "mysecret"
fakeWithSched = "fake-sched"
testSocketLocation = "/tmp/csi-ut.sock"
)
var (
cm cluster.Cluster
systemUserToken string
)
func init() {
setupFakeDriver()
}
// testServer is a simple struct used abstract
// the creation and setup of the gRPC CSI service
type testServer struct {
conn *grpc.ClientConn
server grpcserver.Server
m *mockdriver.MockVolumeDriver
c *mockcluster.MockCluster
mc *gomock.Controller
sdk *sdk.Server
port string
gwport string
uds string
}
func setupFakeDriver() {
kv, err := kvdb.New(mem.Name, "fake_test", []string{}, nil, kvdb.LogFatalErrorCB)
if err != nil {
clogger.Panicf("Failed to initialize KVDB")
}
if err := kvdb.SetInstance(kv); err != nil {
clogger.Panicf("Failed to set KVDB instance")
}
// Need to setup a fake cluster. No need to start it.
clustermanager.Init(config.ClusterConfig{
ClusterId: "fakecluster",
NodeId: "fakeNode",
})
cm, err = clustermanager.Inst()
if err != nil {
clogger.Panicf("Unable to initialize cluster manager: %v", err)
}
// Requires a non-nil cluster
if err := volumedrivers.Register("fake", map[string]string{}); err != nil {
clogger.Panicf("Unable to start volume driver fake: %v", err)
}
}
func createToken(t *testing.T, name, role string) string {
claims := &auth.Claims{
Issuer: "openstorage.io",
Name: name,
Email: name + "@openstorage.io",
Roles: []string{role},
}
signature := &auth.Signature{
Key: []byte(testSharedSecret),
Type: jwt.SigningMethodHS256,
}
options := &auth.Options{
Expiration: time.Now().Add(1 * time.Hour).Unix(),
}
token, err := auth.Token(claims, signature, options)
assert.NoError(t, err)
return token
}
func setupMockDriver(tester *testServer, t *testing.T) {
volumedrivers.Add(mockDriverName, func(map[string]string) (volume.VolumeDriver, error) {
return tester.m, nil
})
var err error
// Register mock driver
err = volumedrivers.Register(mockDriverName, nil)
assert.Nil(t, err)
}
func newTestServer(t *testing.T) *testServer {
return newTestServerWithConfig(t, &OsdCsiServerConfig{
DriverName: mockDriverName,
})
}
func newUDSTestServer(t *testing.T) *testServer {
os.Remove(testSocketLocation)
return newTestServerWithConfig(t, &OsdCsiServerConfig{
DriverName: mockDriverName,
Address: testSocketLocation,
Net: "unix",
})
}
func newTestServerWithConfig(t *testing.T, config *OsdCsiServerConfig) *testServer {
tester := &testServer{}
tester.setPorts()
// Add driver to registry
tester.mc = gomock.NewController(&utils.SafeGoroutineTester{})
tester.m = mockdriver.NewMockVolumeDriver(tester.mc)
tester.c = mockcluster.NewMockCluster(tester.mc)
if config.Cluster == nil {
config.Cluster = tester.c
}
config.RoundRobinBalancer = loadbalancer.NewNullBalancer()
setupMockDriver(tester, t)
// Initialise storage policy manager
kv, err := kvdb.New(mem.Name, "policy", []string{}, nil, kvdb.LogFatalErrorCB)
assert.NoError(t, err)
rm, err := role.NewSdkRoleManager(kv)
assert.NoError(t, err)
// Setup storage policy
kv, err = kvdb.New(mem.Name, "test", []string{}, nil, kvdb.LogFatalErrorCB)
assert.NoError(t, err)
kvdb.SetInstance(kv)
stp, err := storagepolicy.Init()
if err != nil {
stp, _ = storagepolicy.Inst()
}
assert.NotNil(t, stp)
selfsignedJwt, err := auth.NewJwtAuth(&auth.JwtAuthConfig{
SharedSecret: []byte(testSharedSecret),
UsernameClaim: auth.UsernameClaimTypeName,
})
// setup sdk server
tester.sdk, err = sdk.New(&sdk.ServerConfig{
DriverName: "fake",
Net: "tcp",
Address: ":" + tester.port,
RestPort: tester.gwport,
Cluster: tester.c,
Socket: tester.uds,
StoragePolicy: stp,
AccessOutput: ioutil.Discard,
AuditOutput: ioutil.Discard,
Security: &sdk.SecurityConfig{
Role: rm,
Authenticators: map[string]auth.Authenticator{
"openstorage.io": selfsignedJwt,
},
},
})
assert.Nil(t, err)
err = tester.sdk.Start()
assert.Nil(t, err)
tester.sdk.UseVolumeDrivers(map[string]volume.VolumeDriver{
"mock": tester.m,
"default": tester.m,
})
// Setup CSI simple driver
// Allow for net and address to be overwritten
if config.Net == "" {
config.Net = "tcp"
}
if config.Address == "" {
config.Address = "127.0.0.1:0"
}
config.SdkUds = tester.uds
config.SdkPort = tester.port
tester.server, err = NewOsdCsiServer(config)
assert.Nil(t, err)
err = tester.server.Start()
assert.Nil(t, err)
// Setup a connection to the driver
tester.conn, err = grpc.Dial(tester.server.Address(), grpc.WithInsecure())
assert.Nil(t, err)
systemUserToken = createToken(t, "user1", "system.user")
// Setup fake-sched driver for REST UTs
// Point it to the fake driver head
/*fakeDriver, err := volumedrivers.Get(fake.Name)
assert.NoError(t, err)
volumedrivers.Add(fakeWithSched,
func(params map[string]string) (volume.VolumeDriver, error) {
return fakeDriver, nil
},
)
volumedrivers.Register(fakeWithSched, nil)
*/
return tester
}
func (s *testServer) setPorts() {
source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
port := r.Intn(20000) + 10000
s.port = fmt.Sprintf("%d", port)
s.gwport = fmt.Sprintf("%d", port+1)
s.uds = fmt.Sprintf("/tmp/osd-csi-ut-%d.sock", port)
}
func (s *testServer) mockClusterEnumerateNode(_ *testing.T, nodeName string) {
s.MockCluster().EXPECT().
Enumerate().
Return(api.Cluster{
NodeId: nodeName,
Nodes: []*api.Node{{
Id: "1",
MgmtIp: "[::]",
}},
}, nil).
AnyTimes()
}
func (s *testServer) MockDriver() *mockdriver.MockVolumeDriver {
return s.m
}
func (s *testServer) MockCluster() *mockcluster.MockCluster {
return s.c
}
func (s *testServer) Stop() {
// Remove from registry
volumedrivers.Remove("mock")
// Shutdown servers
s.conn.Close()
s.m.EXPECT().StopVolumeWatcher().Return().AnyTimes()
s.server.Stop()
s.sdk.Stop()
// Check mocks
s.mc.Finish()
}
func (s *testServer) Conn() *grpc.ClientConn {
return s.conn
}
func (s *testServer) Server() grpcserver.Server {
return s.server
}
func TestCSIServerStart(t *testing.T) {
s := newTestServer(t)
assert.True(t, s.Server().IsRunning())
defer s.Stop()
// Check if we can still talk to the server
// after starting multiple times.
err := s.Server().Start()
assert.True(t, s.Server().IsRunning())
assert.NotNil(t, err)
err = s.Server().Start()
assert.True(t, s.Server().IsRunning())
assert.NotNil(t, err)
err = s.Server().Start()
assert.True(t, s.Server().IsRunning())
assert.NotNil(t, err)
// Make a call
s.MockDriver().EXPECT().Name().Return("mock").Times(2)
c := csi.NewIdentityClient(s.Conn())
r, err := c.GetPluginInfo(context.Background(), &csi.GetPluginInfoRequest{})
assert.Nil(t, err)
// Verify
name := r.GetName()
version := r.GetVendorVersion()
assert.Equal(t, name, "mock.openstorage.org")
assert.Equal(t, version, csiDriverVersion)
}
func TestCSIServerStop(t *testing.T) {
s := newTestServer(t)
assert.True(t, s.Server().IsRunning())
s.Stop()
assert.False(t, s.Server().IsRunning())
}
func TestNewCSIServerBadParameters(t *testing.T) {
tester := &testServer{}
tester.setPorts()
setupMockDriver(tester, t)
s, err := NewOsdCsiServer(nil)
assert.Nil(t, s)
assert.NotNil(t, err)
s, err = NewOsdCsiServer(&OsdCsiServerConfig{})
assert.Nil(t, s)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "must be provided")
s, err = NewOsdCsiServer(&OsdCsiServerConfig{
Net: "test",
SdkUds: tester.uds,
})
assert.Nil(t, s)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "must be provided")
s, err = NewOsdCsiServer(&OsdCsiServerConfig{
Net: "test",
Address: "blah",
SdkUds: tester.uds,
})
assert.Nil(t, s)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "must be provided")
s, err = NewOsdCsiServer(&OsdCsiServerConfig{
Net: "test",
Address: "blah",
DriverName: "name",
SdkUds: tester.uds,
})
assert.Nil(t, s)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Unable to get driver")
s, err = NewOsdCsiServer(&OsdCsiServerConfig{
Net: "test",
Address: "blah",
DriverName: "name",
})
assert.Nil(t, s)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "SdkUds must be provided")
// Add driver to registry
mc := gomock.NewController(t)
defer mc.Finish()
m := mockdriver.NewMockVolumeDriver(mc)
volumedrivers.Add("mock", func(map[string]string) (volume.VolumeDriver, error) {
return m, nil
})
defer volumedrivers.Remove("mock")
s, err = NewOsdCsiServer(&OsdCsiServerConfig{
Net: "test",
Address: "blah",
DriverName: "mock",
SdkUds: tester.uds,
})
assert.Nil(t, s)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Unable to setup server")
os.Remove(tester.uds)
}
func TestAddEncryptionInfoToLabels(t *testing.T) {
s := OsdCsiServer{}
secrets := map[string]string{
options.OptionsSecret: "secret",
options.OptionsSecretContext: "context",
options.OptionsSecretKey: "key",
}
labels := map[string]string{
"test": "val",
}
labels = s.addEncryptionInfoToLabels(labels, secrets)
assert.Equal(t, labels[options.OptionsSecret], "secret")
assert.Equal(t, labels[options.OptionsSecretContext], "context")
assert.Equal(t, labels[options.OptionsSecretKey], "key")
}
func TestCSIServerStartContextInterceptor(t *testing.T) {
s := newTestServer(t)
assert.True(t, s.Server().IsRunning())
defer s.Stop()
gomock.InOrder(
s.MockDriver().
EXPECT().
SnapEnumerate(nil, nil).
Return([]*api.Volume{}, nil).
Times(1),
s.MockDriver().
EXPECT().
Enumerate(nil, nil).
Return([]*api.Volume{}, nil).
Times(1),
)
var buf bytes.Buffer
clogger.SetOutput(&buf)
// Make a call
c := csi.NewControllerClient(s.Conn())
resp, err := c.ListSnapshots(context.Background(), &csi.ListSnapshotsRequest{})
assert.NoError(t, err)
assert.NotNil(t, resp)
// Should have correlation ID in logs.
logStr := buf.String()
expectedInfoLog := "correlation-id"
assert.Contains(t, logStr, expectedInfoLog)
expectedInfoLog = "csi-driver"
assert.Contains(t, logStr, expectedInfoLog)
}
func TestCSISocketAutoRecover(t *testing.T) {
csiSocketCheckInterval = 1 * time.Second
// Start server and wait for socket to be up and running
s := newUDSTestServer(t)
assert.True(t, s.Server().IsRunning())
defer func() {
s.Stop()
}()
assert.Eventually(t, s.server.IsRunning, 30*time.Second, time.Second)
assert.Eventually(t, func() bool {
_, err := os.Stat(testSocketLocation)
return err == nil
}, 30*time.Second, time.Second)
_, err := os.Stat(testSocketLocation)
assert.NoError(t, err, "UDS should exist after startup")
// Delete socket and wait for it to be gone
err = os.Remove(testSocketLocation)
assert.NoError(t, err)
// Wait for auto-recover
assert.Eventually(t, func() bool {
_, err := os.Stat(testSocketLocation)
return err == nil
}, 30*time.Second, time.Second)
assert.True(t, s.server.IsRunning(), "Server should be running after autorecover")
_, err = os.Stat(testSocketLocation)
assert.NoError(t, err, "UDS should exist after autorecover")
}