forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVMSnapshotManagerTest.java
More file actions
426 lines (381 loc) · 19.2 KB
/
VMSnapshotManagerTest.java
File metadata and controls
426 lines (381 loc) · 19.2 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 com.cloud.vm.snapshot;
import com.cloud.agent.AgentManager;
import com.cloud.exception.AgentUnavailableException;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.ManagementServerException;
import com.cloud.exception.OperationTimedoutException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.exception.VirtualMachineMigrationException;
import com.cloud.host.dao.HostDao;
import com.cloud.hypervisor.Hypervisor;
import com.cloud.hypervisor.HypervisorGuruManager;
import com.cloud.hypervisor.dao.HypervisorCapabilitiesDao;
import com.cloud.service.ServiceOfferingVO;
import com.cloud.service.dao.ServiceOfferingDao;
import com.cloud.service.dao.ServiceOfferingDetailsDao;
import com.cloud.storage.GuestOSVO;
import com.cloud.storage.Snapshot;
import com.cloud.storage.SnapshotVO;
import com.cloud.storage.VolumeVO;
import com.cloud.storage.dao.GuestOSDao;
import com.cloud.storage.dao.SnapshotDao;
import com.cloud.storage.dao.VolumeDao;
import com.cloud.user.Account;
import com.cloud.user.AccountManager;
import com.cloud.user.AccountVO;
import com.cloud.user.dao.AccountDao;
import com.cloud.user.dao.UserDao;
import com.cloud.uservm.UserVm;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.fsm.NoTransitionException;
import com.cloud.vm.VMInstanceDetailVO;
import com.cloud.vm.UserVmManager;
import com.cloud.vm.UserVmVO;
import com.cloud.vm.VirtualMachine.State;
import com.cloud.vm.VirtualMachineManager;
import com.cloud.vm.dao.UserVmDao;
import com.cloud.vm.dao.VMInstanceDetailsDao;
import com.cloud.vm.dao.VMInstanceDao;
import com.cloud.vm.snapshot.dao.VMSnapshotDao;
import com.cloud.vm.snapshot.dao.VMSnapshotDetailsDao;
import org.apache.cloudstack.acl.ControlledEntity;
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
import org.apache.cloudstack.api.ResourceDetail;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class VMSnapshotManagerTest {
@Spy
VMSnapshotManagerImpl _vmSnapshotMgr = new VMSnapshotManagerImpl();
@Mock
Account admin;
@Mock
VMSnapshotDao _vmSnapshotDao;
@Mock
VolumeDao _volumeDao;
@Mock
AccountDao _accountDao;
@Mock
VMInstanceDao _vmInstanceDao;
@Mock
UserVmDao _userVMDao;
@Mock
HostDao _hostDao;
@Mock
UserDao _userDao;
@Mock
AgentManager _agentMgr;
@Mock
HypervisorGuruManager _hvGuruMgr;
@Mock
AccountManager _accountMgr;
@Mock
GuestOSDao _guestOSDao;
@Mock
PrimaryDataStoreDao _storagePoolDao;
@Mock
SnapshotDao _snapshotDao;
@Mock
VirtualMachineManager _itMgr;
@Mock
ConfigurationDao _configDao;
@Mock
HypervisorCapabilitiesDao _hypervisorCapabilitiesDao;
@Mock
ServiceOfferingDetailsDao _serviceOfferingDetailsDao;
@Mock
ServiceOfferingDao _serviceOfferingDao;
@Mock
VMInstanceDetailsDao _vmInstanceDetailsDao;
@Mock
VMSnapshotDetailsDao _vmSnapshotDetailsDao;
@Mock
UserVmManager _userVmManager;
@Mock
private AccountVO accountVOMock;
private static final long TEST_VM_ID = 3L;
private static final long SERVICE_OFFERING_ID = 1L;
private static final long SERVICE_OFFERING_DIFFERENT_ID = 2L;
private static VMSnapshot.Type vmSnapshotType;
private static List<VMInstanceDetailVO> userVmDetails;
private static List<VMSnapshotDetailsVO> vmSnapshotDetails;
private static final long VM_SNAPSHOT_ID = 1L;
private static final String VM_SNAPSHOT_NAME = "Vm-Snapshot-Name";
private static final String VM_SNAPSHOT_DESCRIPTION = "Vm-Snapshot-Desc";
private static final String VM_SNAPSHOT_DISPLAY_NAME = "Vm-Snapshot-Display-Name";
@Mock
UserVmVO vmMock;
@Mock
VolumeVO volumeMock;
@Mock
VMSnapshotVO vmSnapshotVO;
@Mock
ServiceOfferingVO serviceOffering;
@Mock
VMInstanceDetailVO userVmDetailCpuNumber;
@Mock
VMInstanceDetailVO userVmDetailMemory;
@Mock
VMSnapshotDetailsVO vmSnapshotDetailCpuNumber;
@Mock
VMSnapshotDetailsVO vmSnapshotDetailMemory;
@Mock
UserVm userVm;
@Captor
ArgumentCaptor<List<VMSnapshotDetailsVO>> listVmSnapshotDetailsCaptor;
@Captor
ArgumentCaptor<Map<String,String>> mapDetailsCaptor;
@Captor
ArgumentCaptor<List<VMInstanceDetailVO>> listUserVmDetailsCaptor;
private AutoCloseable closeable;
@Before
public void setup() {
closeable = MockitoAnnotations.openMocks(this);
doReturn(admin).when(_vmSnapshotMgr).getCaller();
_vmSnapshotMgr._accountDao = _accountDao;
_vmSnapshotMgr._userVMDao = _userVMDao;
_vmSnapshotMgr._vmSnapshotDao = _vmSnapshotDao;
_vmSnapshotMgr._volumeDao = _volumeDao;
_vmSnapshotMgr._storagePoolDao = _storagePoolDao;
_vmSnapshotMgr._accountMgr = _accountMgr;
_vmSnapshotMgr._snapshotDao = _snapshotDao;
_vmSnapshotMgr._guestOSDao = _guestOSDao;
_vmSnapshotMgr._hypervisorCapabilitiesDao = _hypervisorCapabilitiesDao;
_vmSnapshotMgr._serviceOfferingDetailsDao = _serviceOfferingDetailsDao;
doNothing().when(_accountMgr).checkAccess(any(Account.class), any(AccessType.class), any(Boolean.class), any(ControlledEntity.class));
_vmSnapshotMgr._serviceOfferingDao = _serviceOfferingDao;
_vmSnapshotMgr._vmInstanceDetailsDao = _vmInstanceDetailsDao;
_vmSnapshotMgr._vmSnapshotDetailsDao = _vmSnapshotDetailsDao;
_vmSnapshotMgr._userVmManager = _userVmManager;
when(_userVMDao.findById(anyLong())).thenReturn(vmMock);
when(_vmSnapshotDao.findByName(anyLong(), anyString())).thenReturn(null);
when(_vmSnapshotDao.findByVm(anyLong())).thenReturn(new ArrayList<VMSnapshotVO>());
when(_hypervisorCapabilitiesDao.isVmSnapshotEnabled(Hypervisor.HypervisorType.XenServer, "default")).thenReturn(true);
when(_serviceOfferingDetailsDao.findDetail(anyLong(), anyString())).thenReturn(null);
List<VolumeVO> mockVolumeList = new ArrayList<VolumeVO>();
mockVolumeList.add(volumeMock);
when(volumeMock.getInstanceId()).thenReturn(TEST_VM_ID);
when(_volumeDao.findByInstance(anyLong())).thenReturn(mockVolumeList);
when(_volumeDao.findReadyRootVolumesByInstance(anyLong())).thenReturn(mockVolumeList);
when(_storagePoolDao.findById(anyLong())).thenReturn(mock(StoragePoolVO.class));
when(vmMock.getId()).thenReturn(TEST_VM_ID);
when(vmMock.getServiceOfferingId()).thenReturn(SERVICE_OFFERING_ID);
when(vmMock.getAccountId()).thenReturn(1L);
when(vmMock.getDomainId()).thenReturn(1L);
when(vmMock.getInstanceName()).thenReturn("i-3-VM-TEST");
when(vmMock.getState()).thenReturn(State.Running);
when(vmMock.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.XenServer);
when(_guestOSDao.findById(anyLong())).thenReturn(mock(GuestOSVO.class));
when(vmSnapshotVO.getId()).thenReturn(VM_SNAPSHOT_ID);
when(serviceOffering.isDynamic()).thenReturn(false);
when(_serviceOfferingDao.findById(SERVICE_OFFERING_ID)).thenReturn(serviceOffering);
for (ResourceDetail detail : Arrays.asList(userVmDetailCpuNumber, vmSnapshotDetailCpuNumber)) {
when(detail.getName()).thenReturn("cpuNumber");
when(detail.getValue()).thenReturn("2");
when(detail.isDisplay()).thenReturn(true);
}
for (ResourceDetail detail : Arrays.asList(userVmDetailMemory, vmSnapshotDetailMemory)) {
when(detail.getName()).thenReturn("memory");
when(detail.getValue()).thenReturn("2048");
when(detail.isDisplay()).thenReturn(true);
}
userVmDetails = Arrays.asList(userVmDetailCpuNumber, userVmDetailMemory);
vmSnapshotDetails = Arrays.asList(vmSnapshotDetailCpuNumber, vmSnapshotDetailMemory);
when(_vmInstanceDetailsDao.listDetails(TEST_VM_ID)).thenReturn(userVmDetails);
when(_vmSnapshotDetailsDao.listDetails(VM_SNAPSHOT_ID)).thenReturn(vmSnapshotDetails);
when(userVm.getId()).thenReturn(TEST_VM_ID);
when(userVm.getServiceOfferingId()).thenReturn(SERVICE_OFFERING_ID);
when(vmSnapshotVO.getServiceOfferingId()).thenReturn(SERVICE_OFFERING_ID);
}
@After
public void tearDown() throws Exception {
closeable.close();
}
// vmId null case
@Test(expected = InvalidParameterValueException.class)
public void testAllocVMSnapshotF1() throws ResourceAllocationException {
when(_userVMDao.findById(TEST_VM_ID)).thenReturn(null);
_vmSnapshotMgr.allocVMSnapshot(TEST_VM_ID, "", "", true);
}
// hypervisorCapabilities not expected case
@Test(expected = InvalidParameterValueException.class)
public void testAllocVMSnapshotF6() throws ResourceAllocationException {
when(vmMock.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.Ovm);
when(_hypervisorCapabilitiesDao.isVmSnapshotEnabled(Hypervisor.HypervisorType.Ovm, "default")).thenReturn(false);
_vmSnapshotMgr.allocVMSnapshot(TEST_VM_ID, "", "", true);
}
// vm state not in [running, stopped] case
@Test(expected = InvalidParameterValueException.class)
public void testAllocVMSnapshotF2() throws ResourceAllocationException {
when(vmMock.getState()).thenReturn(State.Starting);
_vmSnapshotMgr.allocVMSnapshot(TEST_VM_ID, "", "", true);
}
// VM in stopped state & snapshotmemory case
@Test(expected = InvalidParameterValueException.class)
public void testCreateVMSnapshotF3() throws AgentUnavailableException, OperationTimedoutException, ResourceAllocationException {
when(vmMock.getState()).thenReturn(State.Stopped);
_vmSnapshotMgr.allocVMSnapshot(TEST_VM_ID, "", "", true);
}
// max snapshot limit case
@SuppressWarnings("unchecked")
@Test(expected = CloudRuntimeException.class)
public void testAllocVMSnapshotF4() throws ResourceAllocationException {
long accountId = 1L;
List<VMSnapshotVO> mockList = mock(List.class);
when(mockList.size()).thenReturn(10);
when(_userVMDao.findById(TEST_VM_ID)).thenReturn(vmMock);
when(userVm.getAccountId()).thenReturn(accountId);
when(_accountMgr.getAccount(accountId)).thenReturn(accountVOMock);
when(_vmSnapshotDao.findByVm(TEST_VM_ID)).thenReturn(mockList);
_vmSnapshotMgr.allocVMSnapshot(TEST_VM_ID, "", "", true);
}
// active volume snapshots case
@SuppressWarnings("unchecked")
@Test(expected = CloudRuntimeException.class)
public void testAllocVMSnapshotF5() throws ResourceAllocationException {
long accountId = 1L;
List<SnapshotVO> mockList = mock(List.class);
when(mockList.size()).thenReturn(1);
when(_userVMDao.findById(TEST_VM_ID)).thenReturn(vmMock);
when(userVm.getAccountId()).thenReturn(accountId);
when(_accountMgr.getAccount(accountId)).thenReturn(accountVOMock);
when(_snapshotDao.listByInstanceId(TEST_VM_ID, Snapshot.State.Creating, Snapshot.State.CreatedOnPrimary, Snapshot.State.BackingUp)).thenReturn(mockList);
_vmSnapshotMgr.allocVMSnapshot(TEST_VM_ID, "", "", true);
}
// successful creation case
@Test
public void testCreateVMSnapshot() throws AgentUnavailableException, OperationTimedoutException, ResourceAllocationException, NoTransitionException {
long accountId = 1L;
when(_userVMDao.findById(TEST_VM_ID)).thenReturn(vmMock);
when(userVm.getAccountId()).thenReturn(accountId);
when(_accountMgr.getAccount(accountId)).thenReturn(accountVOMock);
when(vmMock.getState()).thenReturn(State.Running);
_vmSnapshotMgr.allocVMSnapshot(TEST_VM_ID, "", "", true);
}
@Test
public void testCreateAndPersistVMSnapshot() {
when(_vmSnapshotDao.persist(any(VMSnapshotVO.class))).thenReturn(vmSnapshotVO);
_vmSnapshotMgr.createAndPersistVMSnapshot(vmMock, VM_SNAPSHOT_DESCRIPTION,
VM_SNAPSHOT_NAME, VM_SNAPSHOT_DISPLAY_NAME, vmSnapshotType);
verify(_vmSnapshotMgr).addSupportForCustomServiceOffering(TEST_VM_ID, SERVICE_OFFERING_ID, VM_SNAPSHOT_ID);
}
@Test(expected=CloudRuntimeException.class)
public void testCreateAndPersistVMSnapshotNullVMSnapshot() {
when(_vmSnapshotDao.persist(any(VMSnapshotVO.class))).thenReturn(null);
_vmSnapshotMgr.createAndPersistVMSnapshot(vmMock, VM_SNAPSHOT_DESCRIPTION,
VM_SNAPSHOT_NAME, VM_SNAPSHOT_DISPLAY_NAME, vmSnapshotType);
}
@Test
public void testAddSupportForCustomServiceOfferingNotDynamicServiceOffering() {
_vmSnapshotMgr.addSupportForCustomServiceOffering(TEST_VM_ID, SERVICE_OFFERING_ID, VM_SNAPSHOT_ID);
verify(_vmInstanceDetailsDao, never()).listDetails(TEST_VM_ID);
}
@Test
public void testAddSupportForCustomServiceOfferingDynamicServiceOffering() {
when(serviceOffering.isDynamic()).thenReturn(true);
_vmSnapshotMgr.addSupportForCustomServiceOffering(TEST_VM_ID, SERVICE_OFFERING_ID, VM_SNAPSHOT_ID);
verify(_vmInstanceDetailsDao).listDetails(TEST_VM_ID);
verify(_vmSnapshotDetailsDao).saveDetails(listVmSnapshotDetailsCaptor.capture());
}
@Test
public void testUpdateUserVmServiceOfferingSameServiceOffering() {
_vmSnapshotMgr.updateUserVmServiceOffering(userVm, vmSnapshotVO);
verify(_vmSnapshotMgr, never()).changeUserVmServiceOffering(userVm, vmSnapshotVO);
}
@Test
public void testUpdateUserVmServiceOfferingDifferentServiceOffering() throws ConcurrentOperationException, ResourceUnavailableException, ManagementServerException, VirtualMachineMigrationException {
when(userVm.getServiceOfferingId()).thenReturn(SERVICE_OFFERING_DIFFERENT_ID);
when(_userVmManager.upgradeVirtualMachine(ArgumentMatchers.eq(TEST_VM_ID), ArgumentMatchers.eq(SERVICE_OFFERING_ID), mapDetailsCaptor.capture())).thenReturn(true);
_vmSnapshotMgr.updateUserVmServiceOffering(userVm, vmSnapshotVO);
verify(_vmSnapshotMgr).changeUserVmServiceOffering(userVm, vmSnapshotVO);
verify(_vmSnapshotMgr).getVmMapDetails(userVm);
verify(_vmSnapshotMgr).upgradeUserVmServiceOffering(ArgumentMatchers.eq(userVm), ArgumentMatchers.eq(SERVICE_OFFERING_ID), mapDetailsCaptor.capture());
}
@Test
public void testGetVmMapDetails() {
Map<String, String> result = _vmSnapshotMgr.getVmMapDetails(userVm);
assert(result.containsKey(userVmDetailCpuNumber.getName()));
assert(result.containsKey(userVmDetailMemory.getName()));
assertEquals(userVmDetails.size(), result.size());
assertEquals(userVmDetailCpuNumber.getValue(), result.get(userVmDetailCpuNumber.getName()));
assertEquals(userVmDetailMemory.getValue(), result.get(userVmDetailMemory.getName()));
}
@Test
public void testChangeUserVmServiceOffering() throws ConcurrentOperationException, ResourceUnavailableException, ManagementServerException, VirtualMachineMigrationException {
when(_userVmManager.upgradeVirtualMachine(ArgumentMatchers.eq(TEST_VM_ID), ArgumentMatchers.eq(SERVICE_OFFERING_ID), mapDetailsCaptor.capture())).thenReturn(true);
_vmSnapshotMgr.changeUserVmServiceOffering(userVm, vmSnapshotVO);
verify(_vmSnapshotMgr).getVmMapDetails(userVm);
verify(_vmSnapshotMgr).upgradeUserVmServiceOffering(ArgumentMatchers.eq(userVm), ArgumentMatchers.eq(SERVICE_OFFERING_ID), mapDetailsCaptor.capture());
}
@Test(expected=CloudRuntimeException.class)
public void testChangeUserVmServiceOfferingFailOnUpgradeVMServiceOffering() throws ConcurrentOperationException, ResourceUnavailableException, ManagementServerException, VirtualMachineMigrationException {
when(_userVmManager.upgradeVirtualMachine(ArgumentMatchers.eq(TEST_VM_ID), ArgumentMatchers.eq(SERVICE_OFFERING_ID), mapDetailsCaptor.capture())).thenReturn(false);
_vmSnapshotMgr.changeUserVmServiceOffering(userVm, vmSnapshotVO);
verify(_vmSnapshotMgr).getVmMapDetails(userVm);
verify(_vmSnapshotMgr).upgradeUserVmServiceOffering(ArgumentMatchers.eq(userVm), ArgumentMatchers.eq(SERVICE_OFFERING_ID), mapDetailsCaptor.capture());
}
@Test
public void testUpgradeUserVmServiceOffering() throws ConcurrentOperationException, ResourceUnavailableException, ManagementServerException, VirtualMachineMigrationException {
Map<String, String> details = new HashMap<String, String>() {{
put(userVmDetailCpuNumber.getName(), userVmDetailCpuNumber.getValue());
put(userVmDetailMemory.getName(), userVmDetailMemory.getValue());
}};
when(_userVmManager.upgradeVirtualMachine(TEST_VM_ID, SERVICE_OFFERING_ID, details)).thenReturn(true);
_vmSnapshotMgr.upgradeUserVmServiceOffering(userVm, SERVICE_OFFERING_ID, details);
verify(_userVmManager).upgradeVirtualMachine(TEST_VM_ID, SERVICE_OFFERING_ID, details);
}
@Test
public void testRevertUserVmDetailsFromVmSnapshotNotDynamicServiceOffering() {
_vmSnapshotMgr.revertUserVmDetailsFromVmSnapshot(vmMock, vmSnapshotVO);
verify(_vmSnapshotDetailsDao, never()).listDetails(anyLong());
}
@Test
public void testRevertUserVmDetailsFromVmSnapshotDynamicServiceOffering() {
when(serviceOffering.isDynamic()).thenReturn(true);
_vmSnapshotMgr.revertUserVmDetailsFromVmSnapshot(vmMock, vmSnapshotVO);
verify(_vmSnapshotDetailsDao).listDetails(VM_SNAPSHOT_ID);
verify(_vmInstanceDetailsDao).saveDetails(listUserVmDetailsCaptor.capture());
}
}