|
30 | 30 | import com.cloud.exception.InsufficientCapacityException; |
31 | 31 | import com.cloud.exception.InsufficientServerCapacityException; |
32 | 32 | import com.cloud.exception.InvalidParameterValueException; |
| 33 | +import com.cloud.exception.PermissionDeniedException; |
33 | 34 | import com.cloud.exception.ResourceAllocationException; |
34 | 35 | import com.cloud.exception.ResourceUnavailableException; |
35 | 36 | import com.cloud.host.Host; |
|
47 | 48 | import com.cloud.storage.DiskOfferingVO; |
48 | 49 | import com.cloud.storage.GuestOSVO; |
49 | 50 | import com.cloud.storage.ScopeType; |
| 51 | +import com.cloud.storage.Snapshot; |
| 52 | +import com.cloud.storage.SnapshotVO; |
50 | 53 | import com.cloud.storage.Storage; |
51 | 54 | import com.cloud.storage.VMTemplateVO; |
52 | 55 | import com.cloud.storage.Volume; |
53 | 56 | import com.cloud.storage.VolumeApiService; |
54 | 57 | import com.cloud.storage.VolumeVO; |
55 | 58 | import com.cloud.storage.dao.DiskOfferingDao; |
56 | 59 | import com.cloud.storage.dao.GuestOSDao; |
| 60 | +import com.cloud.storage.dao.SnapshotDao; |
57 | 61 | import com.cloud.storage.dao.VMTemplateDao; |
58 | 62 | import com.cloud.storage.dao.VolumeDao; |
59 | 63 | import com.cloud.template.VirtualMachineTemplate; |
|
66 | 70 | import com.cloud.user.UserDataVO; |
67 | 71 | import com.cloud.user.UserVO; |
68 | 72 | import com.cloud.user.dao.AccountDao; |
| 73 | +import com.cloud.user.dao.UserDao; |
69 | 74 | import com.cloud.user.dao.UserDataDao; |
70 | 75 | import com.cloud.uservm.UserVm; |
71 | 76 | import com.cloud.utils.Pair; |
|
74 | 79 | import com.cloud.vm.dao.NicDao; |
75 | 80 | import com.cloud.vm.dao.UserVmDao; |
76 | 81 | import com.cloud.vm.dao.UserVmDetailsDao; |
| 82 | +import com.cloud.vm.snapshot.VMSnapshotVO; |
| 83 | +import com.cloud.vm.snapshot.dao.VMSnapshotDao; |
| 84 | + |
77 | 85 | import org.apache.cloudstack.api.BaseCmd.HTTPMethod; |
78 | 86 | import org.apache.cloudstack.api.command.user.vm.DeployVMCmd; |
79 | 87 | import org.apache.cloudstack.api.command.user.vm.DeployVnfApplianceCmd; |
80 | 88 | import org.apache.cloudstack.api.command.user.vm.ResetVMUserDataCmd; |
| 89 | +import org.apache.cloudstack.api.command.user.vm.RestoreVMCmd; |
81 | 90 | import org.apache.cloudstack.api.command.user.vm.UpdateVMCmd; |
82 | 91 | import org.apache.cloudstack.api.command.user.volume.ResizeVolumeCmd; |
83 | 92 | import org.apache.cloudstack.context.CallContext; |
@@ -191,6 +200,9 @@ public class UserVmManagerImplTest { |
191 | 200 | @Mock |
192 | 201 | private AccountDao accountDao; |
193 | 202 |
|
| 203 | + @Mock |
| 204 | + private UserDao userDao; |
| 205 | + |
194 | 206 | @Mock |
195 | 207 | ResourceLimitService resourceLimitMgr; |
196 | 208 |
|
@@ -218,6 +230,12 @@ public class UserVmManagerImplTest { |
218 | 230 | @Mock |
219 | 231 | private VolumeDao volumeDaoMock; |
220 | 232 |
|
| 233 | + @Mock |
| 234 | + private SnapshotDao snapshotDaoMock; |
| 235 | + |
| 236 | + @Mock |
| 237 | + private VMSnapshotDao vmSnapshotDaoMock; |
| 238 | + |
221 | 239 | @Mock |
222 | 240 | AccountVO account; |
223 | 241 |
|
@@ -1221,4 +1239,160 @@ public void testSetVmRequiredFieldsForImportFromLastHost() { |
1221 | 1239 | Mockito.verify(userVmVoMock).setLastHostId(2L); |
1222 | 1240 | Mockito.verify(userVmVoMock).setState(VirtualMachine.State.Running); |
1223 | 1241 | } |
| 1242 | + |
| 1243 | + @Test(expected = InvalidParameterValueException.class) |
| 1244 | + public void testRestoreVMNoVM() throws ResourceUnavailableException, InsufficientCapacityException { |
| 1245 | + CallContext callContextMock = Mockito.mock(CallContext.class); |
| 1246 | + Mockito.lenient().doReturn(accountMock).when(callContextMock).getCallingAccount(); |
| 1247 | + |
| 1248 | + RestoreVMCmd cmd = Mockito.mock(RestoreVMCmd.class); |
| 1249 | + when(cmd.getVmId()).thenReturn(vmId); |
| 1250 | + when(cmd.getTemplateId()).thenReturn(2L); |
| 1251 | + when(userVmDao.findById(vmId)).thenReturn(null); |
| 1252 | + |
| 1253 | + userVmManagerImpl.restoreVM(cmd); |
| 1254 | + } |
| 1255 | + |
| 1256 | + @Test(expected = CloudRuntimeException.class) |
| 1257 | + public void testRestoreVMWithVolumeSnapshots() throws ResourceUnavailableException, InsufficientCapacityException { |
| 1258 | + CallContext callContextMock = Mockito.mock(CallContext.class); |
| 1259 | + Mockito.lenient().doReturn(accountMock).when(callContextMock).getCallingAccount(); |
| 1260 | + Mockito.lenient().doNothing().when(accountManager).checkAccess(accountMock, null, true, userVmVoMock); |
| 1261 | + |
| 1262 | + RestoreVMCmd cmd = Mockito.mock(RestoreVMCmd.class); |
| 1263 | + when(cmd.getVmId()).thenReturn(vmId); |
| 1264 | + when(cmd.getTemplateId()).thenReturn(2L); |
| 1265 | + when(userVmDao.findById(vmId)).thenReturn(userVmVoMock); |
| 1266 | + |
| 1267 | + List<VolumeVO> volumes = new ArrayList<>(); |
| 1268 | + long rootVolumeId = 1l; |
| 1269 | + VolumeVO rootVolumeOfVm = Mockito.mock(VolumeVO.class); |
| 1270 | + Mockito.when(rootVolumeOfVm.getId()).thenReturn(rootVolumeId); |
| 1271 | + volumes.add(rootVolumeOfVm); |
| 1272 | + when(volumeDaoMock.findByInstanceAndType(vmId, Volume.Type.ROOT)).thenReturn(volumes); |
| 1273 | + |
| 1274 | + List<SnapshotVO> snapshots = new ArrayList<>(); |
| 1275 | + SnapshotVO snapshot = Mockito.mock(SnapshotVO.class); |
| 1276 | + snapshots.add(snapshot); |
| 1277 | + when(snapshotDaoMock.listByStatus(rootVolumeId, Snapshot.State.Creating, Snapshot.State.CreatedOnPrimary, Snapshot.State.BackingUp)).thenReturn(snapshots); |
| 1278 | + |
| 1279 | + userVmManagerImpl.restoreVM(cmd); |
| 1280 | + } |
| 1281 | + |
| 1282 | + @Test(expected = InvalidParameterValueException.class) |
| 1283 | + public void testRestoreVirtualMachineNoOwner() throws ResourceUnavailableException, InsufficientCapacityException { |
| 1284 | + long userId = 1l; |
| 1285 | + long accountId = 2l; |
| 1286 | + long newTemplateId = 2l; |
| 1287 | + when(accountMock.getId()).thenReturn(userId); |
| 1288 | + when(userVmDao.findById(vmId)).thenReturn(userVmVoMock); |
| 1289 | + when(userVmVoMock.getAccountId()).thenReturn(accountId); |
| 1290 | + when(accountDao.findById(accountId)).thenReturn(null); |
| 1291 | + |
| 1292 | + userVmManagerImpl.restoreVirtualMachine(accountMock, vmId, newTemplateId); |
| 1293 | + } |
| 1294 | + |
| 1295 | + @Test(expected = PermissionDeniedException.class) |
| 1296 | + public void testRestoreVirtualMachineOwnerDisabled() throws ResourceUnavailableException, InsufficientCapacityException { |
| 1297 | + long userId = 1l; |
| 1298 | + long accountId = 2l; |
| 1299 | + long newTemplateId = 2l; |
| 1300 | + when(accountMock.getId()).thenReturn(userId); |
| 1301 | + when(userVmDao.findById(vmId)).thenReturn(userVmVoMock); |
| 1302 | + when(userVmVoMock.getAccountId()).thenReturn(accountId); |
| 1303 | + when(accountDao.findById(accountId)).thenReturn(callerAccount); |
| 1304 | + when(callerAccount.getState()).thenReturn(Account.State.DISABLED); |
| 1305 | + |
| 1306 | + userVmManagerImpl.restoreVirtualMachine(accountMock, vmId, newTemplateId); |
| 1307 | + } |
| 1308 | + |
| 1309 | + @Test(expected = CloudRuntimeException.class) |
| 1310 | + public void testRestoreVirtualMachineNotInRightState() throws ResourceUnavailableException, InsufficientCapacityException { |
| 1311 | + long userId = 1l; |
| 1312 | + long accountId = 2l; |
| 1313 | + long newTemplateId = 2l; |
| 1314 | + when(accountMock.getId()).thenReturn(userId); |
| 1315 | + when(userVmDao.findById(vmId)).thenReturn(userVmVoMock); |
| 1316 | + when(userVmVoMock.getAccountId()).thenReturn(accountId); |
| 1317 | + when(userVmVoMock.getUuid()).thenReturn("a967643d-7633-4ab4-ac26-9c0b63f50cc1"); |
| 1318 | + when(accountDao.findById(accountId)).thenReturn(callerAccount); |
| 1319 | + when(userVmVoMock.getState()).thenReturn(VirtualMachine.State.Starting); |
| 1320 | + |
| 1321 | + userVmManagerImpl.restoreVirtualMachine(accountMock, vmId, newTemplateId); |
| 1322 | + } |
| 1323 | + |
| 1324 | + @Test(expected = InvalidParameterValueException.class) |
| 1325 | + public void testRestoreVirtualMachineNoRootVolume() throws ResourceUnavailableException, InsufficientCapacityException { |
| 1326 | + long userId = 1l; |
| 1327 | + long accountId = 2l; |
| 1328 | + long currentTemplateId = 1l; |
| 1329 | + long newTemplateId = 2l; |
| 1330 | + when(accountMock.getId()).thenReturn(userId); |
| 1331 | + when(userVmDao.findById(vmId)).thenReturn(userVmVoMock); |
| 1332 | + when(userVmVoMock.getAccountId()).thenReturn(accountId); |
| 1333 | + when(userVmVoMock.getUuid()).thenReturn("a967643d-7633-4ab4-ac26-9c0b63f50cc1"); |
| 1334 | + when(accountDao.findById(accountId)).thenReturn(callerAccount); |
| 1335 | + when(userVmVoMock.getState()).thenReturn(VirtualMachine.State.Running); |
| 1336 | + when(userVmVoMock.getTemplateId()).thenReturn(currentTemplateId); |
| 1337 | + |
| 1338 | + VMTemplateVO currentTemplate = Mockito.mock(VMTemplateVO.class); |
| 1339 | + when(templateDao.findById(currentTemplateId)).thenReturn(currentTemplate); |
| 1340 | + when(volumeDaoMock.findByInstanceAndType(vmId, Volume.Type.ROOT)).thenReturn(new ArrayList<VolumeVO>()); |
| 1341 | + |
| 1342 | + userVmManagerImpl.restoreVirtualMachine(accountMock, vmId, newTemplateId); |
| 1343 | + } |
| 1344 | + |
| 1345 | + @Test(expected = InvalidParameterValueException.class) |
| 1346 | + public void testRestoreVirtualMachineMoreThanOneRootVolume() throws ResourceUnavailableException, InsufficientCapacityException { |
| 1347 | + long userId = 1l; |
| 1348 | + long accountId = 2l; |
| 1349 | + long currentTemplateId = 1l; |
| 1350 | + long newTemplateId = 2l; |
| 1351 | + when(accountMock.getId()).thenReturn(userId); |
| 1352 | + when(userVmDao.findById(vmId)).thenReturn(userVmVoMock); |
| 1353 | + when(userVmVoMock.getAccountId()).thenReturn(accountId); |
| 1354 | + when(userVmVoMock.getUuid()).thenReturn("a967643d-7633-4ab4-ac26-9c0b63f50cc1"); |
| 1355 | + when(accountDao.findById(accountId)).thenReturn(callerAccount); |
| 1356 | + when(userVmVoMock.getState()).thenReturn(VirtualMachine.State.Running); |
| 1357 | + when(userVmVoMock.getTemplateId()).thenReturn(currentTemplateId); |
| 1358 | + |
| 1359 | + VMTemplateVO currentTemplate = Mockito.mock(VMTemplateVO.class); |
| 1360 | + when(currentTemplate.isDeployAsIs()).thenReturn(false); |
| 1361 | + when(templateDao.findById(currentTemplateId)).thenReturn(currentTemplate); |
| 1362 | + List<VolumeVO> volumes = new ArrayList<>(); |
| 1363 | + VolumeVO rootVolume1 = Mockito.mock(VolumeVO.class); |
| 1364 | + volumes.add(rootVolume1); |
| 1365 | + VolumeVO rootVolume2 = Mockito.mock(VolumeVO.class); |
| 1366 | + volumes.add(rootVolume2); |
| 1367 | + when(volumeDaoMock.findByInstanceAndType(vmId, Volume.Type.ROOT)).thenReturn(volumes); |
| 1368 | + |
| 1369 | + userVmManagerImpl.restoreVirtualMachine(accountMock, vmId, newTemplateId); |
| 1370 | + } |
| 1371 | + |
| 1372 | + @Test(expected = InvalidParameterValueException.class) |
| 1373 | + public void testRestoreVirtualMachineWithVMSnapshots() throws ResourceUnavailableException, InsufficientCapacityException { |
| 1374 | + long userId = 1l; |
| 1375 | + long accountId = 2l; |
| 1376 | + long currentTemplateId = 1l; |
| 1377 | + long newTemplateId = 2l; |
| 1378 | + when(accountMock.getId()).thenReturn(userId); |
| 1379 | + when(userVmDao.findById(vmId)).thenReturn(userVmVoMock); |
| 1380 | + when(userVmVoMock.getAccountId()).thenReturn(accountId); |
| 1381 | + when(accountDao.findById(accountId)).thenReturn(callerAccount); |
| 1382 | + when(userVmVoMock.getState()).thenReturn(VirtualMachine.State.Running); |
| 1383 | + when(userVmVoMock.getTemplateId()).thenReturn(currentTemplateId); |
| 1384 | + |
| 1385 | + VMTemplateVO currentTemplate = Mockito.mock(VMTemplateVO.class); |
| 1386 | + when(templateDao.findById(currentTemplateId)).thenReturn(currentTemplate); |
| 1387 | + List<VolumeVO> volumes = new ArrayList<>(); |
| 1388 | + VolumeVO rootVolumeOfVm = Mockito.mock(VolumeVO.class); |
| 1389 | + volumes.add(rootVolumeOfVm); |
| 1390 | + when(volumeDaoMock.findByInstanceAndType(vmId, Volume.Type.ROOT)).thenReturn(volumes); |
| 1391 | + List<VMSnapshotVO> vmSnapshots = new ArrayList<>(); |
| 1392 | + VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class); |
| 1393 | + vmSnapshots.add(vmSnapshot); |
| 1394 | + when(vmSnapshotDaoMock.findByVm(vmId)).thenReturn(vmSnapshots); |
| 1395 | + |
| 1396 | + userVmManagerImpl.restoreVirtualMachine(accountMock, vmId, newTemplateId); |
| 1397 | + } |
1224 | 1398 | } |
0 commit comments