CLOUDSTACK-7119. [VMware] Don't allow VM reset when VM has snapshots.

This commit is contained in:
Likitha Shetty 2014-07-17 13:04:58 +05:30
parent 34bed5fa56
commit d3e7b43ee9
2 changed files with 21 additions and 2 deletions

View File

@ -4627,6 +4627,12 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
templateId = vm.getIsoId();
}
// If target VM has associated VM snapshots then don't allow restore of VM
List<VMSnapshotVO> vmSnapshots = _vmSnapshotDao.findByVm(vmId);
if (vmSnapshots.size() > 0 && vm.getHypervisorType() == HypervisorType.VMware) {
throw new InvalidParameterValueException("Unable to restore VM, please specify a VM that does not have VM snapshots");
}
VMTemplateVO template = null;
//newTemplateId can be either template or ISO id. In the following snippet based on the vm deployment (from template or ISO) it is handled accordingly
if (newTemplateId != null) {

View File

@ -27,6 +27,7 @@ import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ -86,6 +87,8 @@ import com.cloud.utils.db.EntityManager;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.dao.UserVmDao;
import com.cloud.vm.dao.VMInstanceDao;
import com.cloud.vm.snapshot.VMSnapshotVO;
import com.cloud.vm.snapshot.dao.VMSnapshotDao;
public class UserVmManagerTest {
@ -149,6 +152,8 @@ public class UserVmManagerTest {
PrimaryDataStoreDao _storagePoolDao;
@Mock
UsageEventDao _usageEventDao;
@Mock
VMSnapshotDao _vmSnapshotDao;
@Before
public void setup() {
@ -172,6 +177,7 @@ public class UserVmManagerTest {
_userVmMgr._scaleRetry = 2;
_userVmMgr._entityMgr = _entityMgr;
_userVmMgr._storagePoolDao = _storagePoolDao;
_userVmMgr._vmSnapshotDao = _vmSnapshotDao;
doReturn(3L).when(_account).getId();
doReturn(8L).when(_vmMock).getAccountId();
@ -181,6 +187,9 @@ public class UserVmManagerTest {
when(_vmMock.getId()).thenReturn(314L);
when(_vmInstance.getId()).thenReturn(1L);
when(_vmInstance.getServiceOfferingId()).thenReturn(2L);
List<VMSnapshotVO> mockList = mock(List.class);
when(_vmSnapshotDao.findByVm(anyLong())).thenReturn(mockList);
when(mockList.size()).thenReturn(0);
}
@ -298,7 +307,9 @@ public class UserVmManagerTest {
doNothing().when(_volsDao).attachVolume(anyLong(), anyLong(), anyLong());
when(_volumeMock.getId()).thenReturn(3L);
doNothing().when(_volsDao).detachVolume(anyLong());
List<VMSnapshotVO> mockList = mock(List.class);
when(_vmSnapshotDao.findByVm(anyLong())).thenReturn(mockList);
when(mockList.size()).thenReturn(0);
when(_templateMock.getUuid()).thenReturn("b1a3626e-72e0-4697-8c7c-a110940cc55d");
Account account = new AccountVO("testaccount", 1L, "networkdomain", (short)0, "uuid");
@ -343,7 +354,9 @@ public class UserVmManagerTest {
doNothing().when(_volsDao).attachVolume(anyLong(), anyLong(), anyLong());
when(_volumeMock.getId()).thenReturn(3L);
doNothing().when(_volsDao).detachVolume(anyLong());
List<VMSnapshotVO> mockList = mock(List.class);
when(_vmSnapshotDao.findByVm(anyLong())).thenReturn(mockList);
when(mockList.size()).thenReturn(0);
when(_templateMock.getUuid()).thenReturn("b1a3626e-72e0-4697-8c7c-a110940cc55d");
Account account = new AccountVO("testaccount", 1L, "networkdomain", (short)0, "uuid");