-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfilesystem_test.go
351 lines (280 loc) · 15.2 KB
/
filesystem_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
/*
Copyright © 2020-2025 Dell Inc. or its subsidiaries. All Rights Reserved.
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 gounity
import (
"context"
"errors"
"fmt"
"testing"
"github.com/dell/gounity/mocks"
"github.com/stretchr/testify/assert"
)
var (
fsName string
fsID string
nfsShareName string
nfsShareID string
storageResourceID string
snapshotID string
nfsShareIDBySnap string
)
const (
NFSShareLocalPath = "/"
NFSShareNamePrefix = "csishare-"
)
func TestFindNasServer(t *testing.T) {
fmt.Println("Begin - Find Nas Server Test")
ctx := context.Background()
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err := testConf.fileAPI.FindNASServerByID(ctx, testConf.nasServer)
if err != nil {
t.Fatalf("Find filesystem by name failed: %v", err)
}
// Test case : GET using invalid ID
nasServer := "nas_dummy_1"
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(fmt.Errorf("not found")).Once()
_, err = testConf.fileAPI.FindNASServerByID(ctx, nasServer)
if err == nil {
t.Fatal("Find Nas Server - Negative case failed")
}
// Test case : GET using empty ID
nasServer = ""
_, err = testConf.fileAPI.FindNASServerByID(ctx, nasServer)
assert.Equal(t, errors.New("NAS Server Id shouldn't be empty"), err)
fmt.Println("Find Nas Server Test Successful")
}
func TestCreateFilesystem(t *testing.T) {
fmt.Println("Begin - Create Filesystem Test")
ctx := context.Background()
fsName = ""
_, err := testConf.fileAPI.CreateFilesystem(ctx, fsName, testConf.poolID, "Unit test resource", testConf.nasServer, 5368709120, 0, 8192, 0, true, false)
assert.Equal(t, errors.New("filesystem name should not be empty"), err)
// Negative cases
fsNameTemp := "dummy-fs-1234567890123456789012345678901234567890123456789012345678"
_, err = testConf.fileAPI.CreateFilesystem(ctx, fsNameTemp, testConf.poolID, "Unit test resource", testConf.nasServer, 5368709120, 0, 8192, 0, true, false)
assert.Equal(t, errors.New("filesystem name dummy-fs-1234567890123456789012345678901234567890123456789012345678 should not exceed 63 characters"), err)
poolIDTemp := "dummy_pool_1"
fsName = "xfs"
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
_, err = testConf.fileAPI.CreateFilesystem(ctx, fsName, poolIDTemp, "Unit test resource", testConf.nasServer, 5368709120, 0, 8192, 0, true, false)
assert.Equal(t, errors.New("thin provisioning is not supported on array and hence cannot create Filesystem"), err)
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
_, err = testConf.fileAPI.CreateFilesystem(ctx, fsName, poolIDTemp, "Unit test resource", testConf.nasServer, 5368709120, 0, 8192, 0, false, true)
assert.Equal(t, errors.New("data reduction is not supported on array and hence cannot create Filesystem"), err)
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
_, err = testConf.fileAPI.CreateFilesystem(ctx, fsName, poolIDTemp, "Unit test resource", testConf.nasServer, 5368709120, 0, 8192, 0, false, false)
assert.Equal(t, nil, err)
fmt.Println("Create Filesystem test successful")
}
func TestFindFilesystem(t *testing.T) {
fmt.Println("Begin - Find Filesystem Test")
ctx := context.Background()
fsName = ""
_, err := testConf.fileAPI.FindFilesystemByName(ctx, fsName)
assert.Equal(t, errors.New("Filesystem Name shouldn't be empty"), err)
_, err = testConf.fileAPI.FindFilesystemByID(ctx, "")
assert.Equal(t, errors.New("Filesystem Id shouldn't be empty"), err)
_, err = testConf.fileAPI.GetFilesystemIDFromResID(ctx, "")
assert.Equal(t, errors.New("Filesystem Resource Id shouldn't be empty"), err)
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err = testConf.fileAPI.GetFilesystemIDFromResID(ctx, "ID")
assert.Equal(t, nil, err)
// Test case : GET using invalid fsName/ID
fsNameTemp := "dummy-fs-1"
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err = testConf.fileAPI.FindFilesystemByName(ctx, fsNameTemp)
assert.Equal(t, nil, err)
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err = testConf.fileAPI.FindFilesystemByID(ctx, fsNameTemp)
assert.Equal(t, nil, err)
fmt.Println("Find Filesystem test successful")
}
func TestCreateNfsShare(t *testing.T) {
fmt.Println("Begin - Create NFS Share Test")
ctx := context.Background()
_, err := testConf.fileAPI.CreateNFSShare(ctx, nfsShareName, NFSShareLocalPath, fsID, NoneDefaultAccess)
assert.Equal(t, errors.New("Filesystem Id cannot be empty"), err)
// Test case : Create NFS share using snapshot
_, err = testConf.snapAPI.CreateSnapshot(ctx, storageResourceID, "snapName", "Snapshot Description", "")
assert.Equal(t, errors.New("storage Resource ID cannot be empty"), err)
snapshotID = ""
_, err = testConf.fileAPI.CreateNFSShareFromSnapshot(ctx, nfsShareName+"_by_snap", NFSShareLocalPath, snapshotID, NoneDefaultAccess)
assert.Equal(t, errors.New("Snapshot Id cannot be empty"), err)
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
_, err = testConf.fileAPI.CreateNFSShare(ctx, nfsShareName, NFSShareLocalPath, "fsID", NoneDefaultAccess)
if err != nil {
t.Fatalf("Create NFS Share Negative scenario failed: %v", err)
}
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err = testConf.fileAPI.CreateNFSShareFromSnapshot(ctx, nfsShareName+"_by_snap", NFSShareLocalPath, "snapshotID", NoneDefaultAccess)
if err != nil {
t.Fatalf("Create NFS Share from snapshot negative case failed: %v", err)
}
fmt.Println("Create NFS Share Test Successful")
}
func TestFindNfsShare(t *testing.T) {
fmt.Println("Begin - Find NFS Share Test")
ctx := context.Background()
_, err := testConf.fileAPI.FindNFSShareByName(ctx, nfsShareName)
assert.Equal(t, errors.New("NFS Share Name shouldn't be empty"), err)
_, err = testConf.fileAPI.FindNFSShareByID(ctx, nfsShareID)
assert.Equal(t, errors.New("NFS Share Id shouldn't be empty"), err)
// Test case : GET using invalid shareName/ID
nfsShareNameTemp := "dummy-fs-1"
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
_, err = testConf.fileAPI.FindNFSShareByName(ctx, nfsShareNameTemp)
assert.Equal(t, nil, err)
fmt.Println("Find NFS Share Test Successful")
}
func TestModifyNfsShare(t *testing.T) {
fmt.Println("Begin - Modify NFS Share Test")
ctx := context.Background()
testConf.hostAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err := testConf.hostAPI.FindHostByName(ctx, testConf.nodeHostName)
if err != nil {
t.Fatalf("Find host failed: %v", err)
}
var hostIDList []string
hostIDList = append(hostIDList, "host.HostContent.ID")
err = testConf.fileAPI.ModifyNFSShareHostAccess(ctx, fsID, nfsShareID, hostIDList, ReadOnlyAccessType)
assert.Equal(t, errors.New("Filesystem Id cannot be empty"), err)
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
err = testConf.fileAPI.ModifyNFSShareCreatedFromSnapshotHostAccess(ctx, "nfsShareIDBySnap", []string{"host1", "host2"}, ReadOnlyAccessType)
assert.Equal(t, nil, err)
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
err = testConf.fileAPI.ModifyNFSShareCreatedFromSnapshotHostAccess(ctx, "nfsShareIDBySnap", []string{"host1", "host2"}, ReadWriteRootAccessType)
assert.Equal(t, nil, err)
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
err = testConf.fileAPI.ModifyNFSShareCreatedFromSnapshotHostAccess(ctx, "nfsShareIDBySnap", []string{"host1", "host2"}, ReadOnlyRootAccessType)
assert.Equal(t, nil, err)
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
err = testConf.fileAPI.ModifyNFSShareCreatedFromSnapshotHostAccess(ctx, "nfsShareIDBySnap", []string{"host1", "host2"}, ReadWriteAccessType)
assert.Equal(t, nil, err)
fsIDTemp := "dummy-fs-1"
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
err = testConf.fileAPI.ModifyNFSShareHostAccess(ctx, fsIDTemp, nfsShareID, hostIDList, ReadWriteRootAccessType)
assert.Equal(t, nil, err)
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
err = testConf.fileAPI.ModifyNFSShareHostAccess(ctx, fsIDTemp, nfsShareID, hostIDList, ReadOnlyRootAccessType)
assert.Equal(t, nil, err)
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
err = testConf.fileAPI.ModifyNFSShareHostAccess(ctx, fsIDTemp, nfsShareID, hostIDList, ReadWriteAccessType)
assert.Equal(t, nil, err)
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
err = testConf.fileAPI.ModifyNFSShareHostAccess(ctx, fsIDTemp, nfsShareID, hostIDList, ReadOnlyAccessType)
assert.Equal(t, nil, err)
fsIDTemp = ""
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
err = testConf.fileAPI.ModifyNFSShareHostAccess(ctx, fsIDTemp, nfsShareID, hostIDList, ReadWriteRootAccessType)
assert.Equal(t, errors.New("Filesystem Id cannot be empty"), err)
nfsShareIDBySnapTemp := "dummy-nsf-share-1"
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
err = testConf.fileAPI.ModifyNFSShareCreatedFromSnapshotHostAccess(ctx, nfsShareIDBySnapTemp, hostIDList, ReadOnlyAccessType)
assert.Equal(t, nil, err)
fmt.Println("Modify NFS Share Test Successful")
}
func TestDescription(t *testing.T) {
fmt.Println("Begin - Update Description of Filesystem Test")
ctx := context.Background()
// Positive scenario is covered under DeleteFilesystemTest()
// Negative test case
filesystemIDTemp := ""
err := testConf.fileAPI.updateDescription(ctx, filesystemIDTemp, "Description of filesystem")
assert.Equal(t, errors.New("Filesystem Id cannot be empty"), err)
filesystemIDTemp = "dummy_fs_1"
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
err = testConf.fileAPI.updateDescription(ctx, filesystemIDTemp, "Description of filesystem")
assert.Equal(t, nil, err)
}
func TestDeleteNfsShare(t *testing.T) {
fmt.Println("Begin - Delete NFS Share Test")
ctx := context.Background()
// Test case : Delete using invalid shareID and fsID
nfsShareIDTemp := "dummy-fs-1"
fsIDTemp := "dummy-fs-1"
err := testConf.fileAPI.DeleteNFSShare(ctx, fsID, nfsShareIDTemp)
assert.Equal(t, errors.New("Filesystem Id cannot be empty"), err)
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
err = testConf.fileAPI.DeleteNFSShare(ctx, fsIDTemp, nfsShareIDTemp)
assert.Equal(t, nil, err)
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
err = testConf.fileAPI.DeleteNFSShareCreatedFromSnapshot(ctx, nfsShareIDTemp)
assert.Equal(t, nil, err)
// Test case : Delete using empty shareID and fsID
nfsShareIDTemp = ""
err = testConf.fileAPI.DeleteNFSShare(ctx, fsID, nfsShareIDTemp)
if err == nil {
t.Fatalf("Delete NFS Share with empty nfs share ID failed")
}
fsIDTemp = ""
err = testConf.fileAPI.DeleteNFSShare(ctx, fsIDTemp, nfsShareIDTemp)
if err == nil {
t.Fatalf("Delete NFS Share with empty fsID failed")
}
err = testConf.fileAPI.DeleteNFSShareCreatedFromSnapshot(ctx, nfsShareIDTemp)
if err == nil {
t.Fatalf("Delete NFS Share created by snapshot with empty nfs share ID failed")
}
//@TODO: Check and Add negative test cases
fmt.Println("Delete NFS Share Test Successful")
}
func TestExpandFilesystem(t *testing.T) {
fmt.Println("Begin - Expand Filesystem Test")
ctx := context.Background()
err := testConf.fileAPI.ExpandFilesystem(ctx, fsID, 7516192768)
assert.Equal(t, errors.New("unable to find filesystem Id . Error: Filesystem Id shouldn't be empty"), err)
// Negative cases
fsIDTemp := "dummy_fs_sv_1"
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
err = testConf.fileAPI.ExpandFilesystem(ctx, fsIDTemp, 7368709120)
assert.Equal(t, nil, err)
err = testConf.fileAPI.ExpandFilesystem(ctx, fsID, 4368709120)
if err == nil {
t.Fatalf("Expand filesystem with smaller size case failed: %v", err)
}
fmt.Println("Expand Filesystem Test Successful")
}
func TestDeleteFilesystem(t *testing.T) {
fmt.Println("Begin - Delete Filesystem Test")
ctx := context.Background()
// Clear existing expectations
testConf.volumeAPI.client.api.(*mocks.Client).ExpectedCalls = nil
// Test case: Delete using empty fsName/ID
fsIDTemp := ""
err := testConf.fileAPI.DeleteFilesystem(ctx, fsIDTemp)
assert.Equal(t, errors.New("Filesystem Id cannot be empty"), err)
fsIDTemp = "dummy-fs-1"
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
err = testConf.fileAPI.DeleteFilesystem(ctx, fsIDTemp)
assert.Equal(t, nil, err)
fsIDTemp = "fsID"
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(errors.New("error")).Once()
err = testConf.fileAPI.DeleteFilesystem(ctx, "fsID")
assert.ErrorContainsf(t, err, "Error", "delete Filesystem %s Failed.", fsIDTemp)
fsIDTemp = "fsID"
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(errors.New(AttachedSnapshotsErrorCode)).Once()
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(errors.New(AttachedSnapshotsErrorCode)).Once()
err = testConf.fileAPI.DeleteFilesystem(ctx, "fsID")
assert.ErrorContainsf(t, err, "Error", "mark filesystem %s for deletion failed.", fsIDTemp)
fmt.Println("Delete Filesystem Test Successful")
}