-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsnapshot_test.go
320 lines (267 loc) · 14 KB
/
snapshot_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
/*
Copyright © 2019-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"
"time"
"github.com/dell/gounity/mocks"
"github.com/dell/gounity/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
var (
snapVolID = "snapVolID"
snapID = "snapID"
snap2ID = "snap2ID"
snapByFsAccessTypeID = "snapByFsAccessTypeID"
snapCopyID = "snapCopyID"
cloneVolID = "cloneVolID"
now = time.Now()
timeStamp = now.Format("20060102150405")
snapVolName = "Unit-test-snap-vol-" + timeStamp
snapName = "Unit-test-snapshot-" + timeStamp
snap2Name = "Unit-test-snapshot2-" + timeStamp
snapByFsAccessTypeName = "Unit-test-snapshot-by-fsxstype-" + timeStamp
cloneVolName = "Unit-test-clone-vol-" + timeStamp
)
func TestCreateSnapshot(t *testing.T) {
fmt.Println("Begin - Create Snapshot Test")
testConf.volumeAPI.client.api.(*mocks.Client).ExpectedCalls = nil
ctx := context.Background()
testConf.volumeAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
testConf.volumeAPI.client.api.(*mocks.Client).On("DoWithHeaders", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.AnythingOfType("*types.LicenseInfo")).Return(nil).
Run(func(args mock.Arguments) {
resp := args.Get(5).(*types.LicenseInfo)
*resp = types.LicenseInfo{LicenseInfoContent: types.LicenseInfoContent{IsInstalled: true, IsValid: true}}
}).Twice()
testConf.volumeAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err := testConf.volumeAPI.CreateLun(ctx, snapVolName, testConf.poolID, "Description", 5368709120, 0, "", true, false)
assert.Equal(t, nil, err)
testConf.volumeAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err = testConf.volumeAPI.FindVolumeByName(ctx, snapVolName)
assert.Equal(t, nil, err)
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
snap, err := testConf.snapAPI.CreateSnapshot(ctx, snapVolID, snapName, "Snapshot Description", "")
fmt.Println("Create Snapshot:", prettyPrintJSON(snap), err)
assert.Equal(t, nil, err)
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
snap, err = testConf.snapAPI.CreateSnapshot(ctx, snapVolID, snap2Name, "Snapshot Description", "1:23:52:50")
fmt.Println("Create Snapshot2:", prettyPrintJSON(snap), err)
assert.Equal(t, nil, err)
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
snapFsAccess, err := testConf.snapAPI.CreateSnapshotWithFsAccesType(ctx, snapVolID, snapByFsAccessTypeName, "Snapshot Description", "", BlockAccessType)
fmt.Println("Create Snapshot With FsAccessType:", prettyPrintJSON(snapFsAccess), err)
assert.Equal(t, nil, err)
snapByFsAccessTypeID = snapFsAccess.SnapshotContent.ResourceID
// Negative cases
snapVolIDTemp := ""
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err = testConf.snapAPI.CreateSnapshot(ctx, snapVolIDTemp, snap2Name, "Snapshot Description", "")
assert.Equal(t, errors.New("storage Resource ID cannot be empty"), err)
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err = testConf.snapAPI.CreateSnapshotWithFsAccesType(ctx, snapVolIDTemp, snapByFsAccessTypeName, "Snapshot Description", "", BlockAccessType)
assert.Equal(t, errors.New("storage Resource ID cannot be empty"), err)
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
snapNameTemp := "snap-name-max-length-12345678901234567890123456789012345678901234567890"
_, err = testConf.snapAPI.CreateSnapshot(ctx, snapVolID, snapNameTemp, "Snapshot Description", "")
assert.Equal(t, errors.New("invalid snapshot name Error:name too long error"), err)
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err = testConf.snapAPI.CreateSnapshotWithFsAccesType(ctx, snapVolIDTemp, snapNameTemp, "Snapshot Description", "", BlockAccessType)
assert.Equal(t, errors.New("storage Resource ID cannot be empty"), err)
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err = testConf.snapAPI.CreateSnapshot(ctx, snapVolID, snap2Name, "Snapshot Description", "1:23:99:99")
assert.Equal(t, errors.New("hours, minutes and seconds should be in between 0-60"), err)
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err = testConf.snapAPI.CreateSnapshotWithFsAccesType(ctx, snapVolIDTemp, snapNameTemp, "Snapshot Description", "1:23:99:99", BlockAccessType)
assert.Equal(t, errors.New("storage Resource ID cannot be empty"), err)
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err = testConf.snapAPI.CreateSnapshot(ctx, "", snap2Name, "Snapshot Description", "1:23:52:50")
assert.Equal(t, errors.New("storage Resource ID cannot be empty"), err)
fmt.Println("Create Snapshot Test - Successful")
}
func TestFindSnapshotByName(t *testing.T) {
fmt.Println("Begin - Find Snapshot by Name Test")
testConf.volumeAPI.client.api.(*mocks.Client).ExpectedCalls = nil
ctx := context.Background()
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
snap, err := testConf.snapAPI.FindSnapshotByName(ctx, snapName)
fmt.Println("Find snapshot by Name:", prettyPrintJSON(snap), err)
assert.Equal(t, nil, err)
snapID = snap.SnapshotContent.ResourceID
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
snap, err = testConf.snapAPI.FindSnapshotByName(ctx, snap2Name)
fmt.Println("Find snapshot2 by Name:", prettyPrintJSON(snap), err)
assert.Equal(t, nil, err)
snap2ID = snap.SnapshotContent.ResourceID
// Negative test cases
snapNameTemp := ""
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err = testConf.snapAPI.FindSnapshotByName(ctx, snapNameTemp)
assert.Equal(t, errors.New("name empty error"), err)
fmt.Println("Find Snapshot by Name - Successful")
}
func TestFindSnapshotByID(t *testing.T) {
fmt.Println("Begin - Find Snapshot by Id Test")
testConf.volumeAPI.client.api.(*mocks.Client).ExpectedCalls = nil
ctx := context.Background()
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
snapID = "snapID"
snap, err := testConf.snapAPI.FindSnapshotByID(ctx, snapID)
fmt.Println("Find snapshot by ID:", prettyPrintJSON(snap), err)
assert.Equal(t, nil, err)
// Negative test cases
snapIDTemp := ""
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err = testConf.snapAPI.FindSnapshotByID(ctx, snapIDTemp)
assert.Equal(t, errors.New("snapshot ID cannot be empty"), err)
fmt.Println("Find Snapshot by Id - Successful")
}
func TestListSnapshots(t *testing.T) {
fmt.Println("Begin - List Snapshots Test")
testConf.volumeAPI.client.api.(*mocks.Client).ExpectedCalls = nil
ctx := context.Background()
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, _, err := testConf.snapAPI.ListSnapshots(ctx, 0, 10, snapVolID, "")
snaps := []string{"snap1", "snap2"}
fmt.Println("List snapshots:", snaps)
if len(snaps) > 0 {
fmt.Println("List snapshots success:", len(snaps))
} else {
assert.Equal(t, errors.New("List snapshot failed"), err)
}
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, _, err = testConf.snapAPI.ListSnapshots(ctx, 0, 10, snapVolID, snapID)
fmt.Println("List snapshots with snap Id:", len(snaps))
if len(snaps) > 0 {
fmt.Println("List snapshots with snap Id success:", len(snaps))
} else {
assert.Equal(t, errors.New("List snapshot with snap Id failed"), err)
}
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, _, err = testConf.snapAPI.ListSnapshots(ctx, 6, 5, "", "")
fmt.Println("List snapshots pagination:", len(snaps))
if len(snaps) > 0 {
fmt.Println("List snapshots pagination success:", len(snaps))
} else {
assert.Equal(t, errors.New("List snapshot pagination failed"), err)
}
fmt.Println("List Snapshots Test - Successful")
}
func TestModifySnapshotAutoDeleteParameter(t *testing.T) {
fmt.Println("Begin - Modify Snapshot Test")
testConf.volumeAPI.client.api.(*mocks.Client).ExpectedCalls = nil
ctx := context.Background()
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
err := testConf.snapAPI.ModifySnapshotAutoDeleteParameter(ctx, snapID)
assert.Equal(t, nil, err)
snapByFsAccessTypeID = "snapByFsAccessTypeID"
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
err = testConf.snapAPI.ModifySnapshot(ctx, snapByFsAccessTypeID, "Modify Description", "1:22:02:50")
assert.Equal(t, nil, err)
// Negative test cases
snapIDTemp := ""
err = testConf.snapAPI.ModifySnapshotAutoDeleteParameter(ctx, snapIDTemp)
assert.Equal(t, errors.New("snapshot ID cannot be empty"), err)
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
err = testConf.snapAPI.ModifySnapshot(ctx, snapIDTemp, "Modify Description", "1:22:02:50")
assert.Equal(t, errors.New("snapshot ID cannot be empty"), err)
fmt.Println("Modify Snapshot Test - Successful")
}
func TestCreteLunThinClone(t *testing.T) {
fmt.Println("Begin - Create LUN thin clone Test")
testConf.volumeAPI.client.api.(*mocks.Client).ExpectedCalls = nil
ctx := context.Background()
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err := testConf.volumeAPI.CreteLunThinClone(ctx, cloneVolName, snapID, snapVolID)
assert.Equal(t, nil, err)
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
vol, err := testConf.volumeAPI.FindVolumeByName(ctx, cloneVolName)
assert.Equal(t, nil, err)
cloneVolID = vol.VolumeContent.ResourceID
fmt.Println("Create LUN thin clone Test - Successful")
}
func TestCopySnapshot(t *testing.T) {
fmt.Println("Begin - Copy Snapshot Test")
testConf.volumeAPI.client.api.(*mocks.Client).ExpectedCalls = nil
ctx := context.Background()
testConf.volumeAPI.client.api.(*mocks.Client).On("DoWithHeaders", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.AnythingOfType("*types.CopySnapshots")).Return(nil).
Run(func(args mock.Arguments) {
resp := args.Get(5).(*types.CopySnapshots)
*resp = types.CopySnapshots{
CopySnapshotsContent: types.CopySnapshotsContent{
Copies: []types.StorageResource{
{ID: snapCopyID},
},
},
}
}).Once()
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
snapCopy, err := testConf.snapAPI.CopySnapshot(ctx, snapByFsAccessTypeID, snapName+"_copy")
assert.Equal(t, nil, err)
snapCopyID = snapCopy.SnapshotContent.ResourceID
// Negative test cases
snapNameTemp := ""
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err = testConf.snapAPI.CopySnapshot(ctx, snapByFsAccessTypeID, snapNameTemp)
assert.Equal(t, errors.New("Snapshot Name cannot be empty"), err)
snapIDTemp := ""
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
_, err = testConf.snapAPI.CopySnapshot(ctx, snapIDTemp, snapName)
assert.Equal(t, errors.New("Source Snapshot ID cannot be empty"), err)
fmt.Println("Copy Snapshot Test - Successful")
}
func TestDeleteSnapshot(t *testing.T) {
fmt.Println("Begin - Delete Snapshot Test")
testConf.volumeAPI.client.api.(*mocks.Client).ExpectedCalls = nil
ctx := context.Background()
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
err := testConf.snapAPI.DeleteSnapshot(ctx, snapID)
assert.Equal(t, nil, err)
// Negative test cases
snapIDTemp := ""
err = testConf.snapAPI.DeleteSnapshot(ctx, snapIDTemp)
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
assert.Equal(t, errors.New("snapshot ID cannot be empty"), err)
fmt.Println("Delete Snapshot Test - Successful")
}
func TestDeleteFilesystemAsSnapshot(t *testing.T) {
fmt.Println("Begin - Delete Filesystem As Snapshot Test")
testConf.volumeAPI.client.api.(*mocks.Client).ExpectedCalls = nil
ctx := context.Background()
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
sourceFs := &types.Filesystem{
FileContent: types.FileContent{
ID: "test-filesystem-id",
Description: "test description",
},
}
err := testConf.snapAPI.DeleteFilesystemAsSnapshot(ctx, snapID, sourceFs)
assert.Equal(t, nil, err)
// MarkFilesystemForDeletion
sourceFs = &types.Filesystem{
FileContent: types.FileContent{
ID: "test-filesystem-id-delete",
Description: "csi-marked-filesystem-for-deletion(do not remove this from description)",
},
}
// Mock the DeleteSnapshot method
testConf.snapAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Twice()
// Mock the DeleteFilesystem method
testConf.fileAPI.client.api.(*mocks.Client).On("DoWithHeaders", anyArgs...).Return(nil).Once()
err = testConf.snapAPI.DeleteFilesystemAsSnapshot(ctx, snapID, sourceFs)
assert.Equal(t, nil, err)
fmt.Println("Delete Filesystem As Snapshot Test - Successful")
}