Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.cloud.agent.api.VMSnapshotTO;
import com.cloud.event.EventTypes;
import com.cloud.event.UsageEventUtils;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.hypervisor.kvm.storage.StorPoolStorageAdaptor;
import com.cloud.storage.DiskOfferingVO;
import com.cloud.storage.VolumeDetailVO;
Expand Down Expand Up @@ -185,16 +186,36 @@ public VMSnapshot takeVMSnapshot(VMSnapshot vmSnapshot) {

@Override
public StrategyPriority canHandle(VMSnapshot vmSnapshot) {
if (VMSnapshot.State.Allocated.equals(vmSnapshot.getState()) && VMSnapshot.Type.DiskAndMemory.equals(vmSnapshot.getType())) {
return StrategyPriority.CANT_HANDLE;
}
return areAllVolumesOnStorPool(vmSnapshot.getVmId());
}

public StrategyPriority canHandle(Long vmId, Long rootPoolId, boolean snapshotMemory) {
if (snapshotMemory) {
if (isAnyVolumeOnStorPool(vmId)) {
Comment thread
sureshanaparti marked this conversation as resolved.
Resolved
throw new InvalidParameterValueException("Instance Snapshots with memory are not supported for volumes on StorPool primary storage");
}
return StrategyPriority.CANT_HANDLE;
}
return areAllVolumesOnStorPool(vmId);
}

private boolean isAnyVolumeOnStorPool(Long vmId) {
List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(vmId);
if (volumeTOs == null) {
return false;
}
for (VolumeObjectTO volumeTO : volumeTOs) {
StoragePoolVO pool = storagePool.findById(volumeTO.getPoolId());
if (pool != null && StorPoolUtil.SP_PROVIDER_NAME.equals(pool.getStorageProviderName())) {
return true;
}
}
return false;
}

private StrategyPriority areAllVolumesOnStorPool(Long vmId) {
List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(vmId);
if (volumeTOs == null || volumeTOs.isEmpty()) {
Expand Down Expand Up @@ -279,6 +300,11 @@ public boolean revertVMSnapshot(VMSnapshot vmSnapshot) {
VMSnapshotVO vmSnapshotVO = (VMSnapshotVO) vmSnapshot;
UserVmVO userVm = userVmDao.findById(vmSnapshot.getVmId());

if (vmSnapshotVO.getType() == VMSnapshot.Type.DiskAndMemory) {
throw new CloudRuntimeException(String.format("Reverting Instance Snapshot [%s] is not allowed because it is marked as a Snapshot with memory, " +
"but StorPool keeps only the disks and reverting them under the running Instance would corrupt its data", vmSnapshot.getUuid()));
}

if (userVm.getState() == VirtualMachine.State.Running && vmSnapshotVO.getType() == VMSnapshot.Type.Disk) {
throw new CloudRuntimeException("The Instance should be in stopped state for revert operation");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* 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 org.apache.cloudstack.storage.snapshot;

import com.cloud.exception.InvalidParameterValueException;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.UserVmVO;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.dao.UserVmDao;
import com.cloud.vm.snapshot.VMSnapshot;
import com.cloud.vm.snapshot.VMSnapshotVO;
import org.apache.cloudstack.engine.subsystem.api.storage.StrategyPriority;
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.apache.cloudstack.storage.datastore.util.StorPoolUtil;
import org.apache.cloudstack.storage.to.VolumeObjectTO;
import org.apache.cloudstack.storage.vmsnapshot.VMSnapshotHelper;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.List;

@RunWith(MockitoJUnitRunner.class)
public class StorPoolVMSnapshotStrategyTest {

private static final long VM_ID = 1L;
private static final long STORPOOL_POOL_ID = 10L;
private static final long OTHER_POOL_ID = 20L;

@Mock
private VMSnapshotHelper vmSnapshotHelper;
@Mock
private PrimaryDataStoreDao storagePool;
@Mock
private UserVmDao userVmDao;

@InjectMocks
private StorPoolVMSnapshotStrategy strategy;

private VolumeObjectTO mockVolumeOnPool(long poolId, String providerName) {
VolumeObjectTO volumeTO = Mockito.mock(VolumeObjectTO.class);
Mockito.when(volumeTO.getPoolId()).thenReturn(poolId);
StoragePoolVO pool = Mockito.mock(StoragePoolVO.class);
Mockito.when(pool.getStorageProviderName()).thenReturn(providerName);
Mockito.when(storagePool.findById(poolId)).thenReturn(pool);
return volumeTO;
}

private VMSnapshot mockVmSnapshot(VMSnapshot.State state, VMSnapshot.Type type) {
VMSnapshot vmSnapshot = Mockito.mock(VMSnapshot.class);
Mockito.when(vmSnapshot.getVmId()).thenReturn(VM_ID);
Mockito.lenient().when(vmSnapshot.getState()).thenReturn(state);
Mockito.lenient().when(vmSnapshot.getType()).thenReturn(type);
return vmSnapshot;
}

@Test(expected = InvalidParameterValueException.class)
public void canHandleAllocationWithMemoryThrowsWhenAllVolumesOnStorPool() {
VolumeObjectTO volumeTO = mockVolumeOnPool(STORPOOL_POOL_ID, StorPoolUtil.SP_PROVIDER_NAME);
Mockito.when(vmSnapshotHelper.getVolumeTOList(VM_ID)).thenReturn(List.of(volumeTO));

strategy.canHandle(VM_ID, STORPOOL_POOL_ID, true);
}

@Test(expected = InvalidParameterValueException.class)
public void canHandleAllocationWithMemoryThrowsWhenOneVolumeOnStorPool() {
VolumeObjectTO otherVolumeTO = mockVolumeOnPool(OTHER_POOL_ID, "DefaultPrimary");
VolumeObjectTO storPoolVolumeTO = mockVolumeOnPool(STORPOOL_POOL_ID, StorPoolUtil.SP_PROVIDER_NAME);
Mockito.when(vmSnapshotHelper.getVolumeTOList(VM_ID)).thenReturn(List.of(otherVolumeTO, storPoolVolumeTO));

strategy.canHandle(VM_ID, OTHER_POOL_ID, true);
}

@Test
public void canHandleAllocationWithMemoryReturnsCantHandleWhenNoVolumeOnStorPool() {
VolumeObjectTO volumeTO = mockVolumeOnPool(OTHER_POOL_ID, "DefaultPrimary");
Mockito.when(vmSnapshotHelper.getVolumeTOList(VM_ID)).thenReturn(List.of(volumeTO));

Assert.assertEquals(StrategyPriority.CANT_HANDLE, strategy.canHandle(VM_ID, OTHER_POOL_ID, true));
}

@Test
public void canHandleAllocationWithoutMemoryReturnsHighestWhenAllVolumesOnStorPool() {
VolumeObjectTO volumeTO = mockVolumeOnPool(STORPOOL_POOL_ID, StorPoolUtil.SP_PROVIDER_NAME);
Mockito.when(vmSnapshotHelper.getVolumeTOList(VM_ID)).thenReturn(List.of(volumeTO));

Assert.assertEquals(StrategyPriority.HIGHEST, strategy.canHandle(VM_ID, STORPOOL_POOL_ID, false));
}

@Test
public void canHandleAllocatedDiskAndMemorySnapshotReturnsCantHandle() {
VMSnapshot vmSnapshot = Mockito.mock(VMSnapshot.class);
Mockito.when(vmSnapshot.getState()).thenReturn(VMSnapshot.State.Allocated);
Mockito.when(vmSnapshot.getType()).thenReturn(VMSnapshot.Type.DiskAndMemory);

Assert.assertEquals(StrategyPriority.CANT_HANDLE, strategy.canHandle(vmSnapshot));
Mockito.verifyNoInteractions(vmSnapshotHelper);
}

@Test
public void canHandleAllocatedDiskSnapshotReturnsHighestWhenAllVolumesOnStorPool() {
VolumeObjectTO volumeTO = mockVolumeOnPool(STORPOOL_POOL_ID, StorPoolUtil.SP_PROVIDER_NAME);
Mockito.when(vmSnapshotHelper.getVolumeTOList(VM_ID)).thenReturn(List.of(volumeTO));

Assert.assertEquals(StrategyPriority.HIGHEST, strategy.canHandle(mockVmSnapshot(VMSnapshot.State.Allocated, VMSnapshot.Type.Disk)));
}

@Test
public void canHandleExistingDiskAndMemorySnapshotReturnsHighestWhenAllVolumesOnStorPool() {
VolumeObjectTO volumeTO = mockVolumeOnPool(STORPOOL_POOL_ID, StorPoolUtil.SP_PROVIDER_NAME);
Mockito.when(vmSnapshotHelper.getVolumeTOList(VM_ID)).thenReturn(List.of(volumeTO));

Assert.assertEquals(StrategyPriority.HIGHEST, strategy.canHandle(mockVmSnapshot(VMSnapshot.State.Ready, VMSnapshot.Type.DiskAndMemory)));
}

private void assertRevertOfDiskAndMemorySnapshotIsBlocked(VirtualMachine.State vmState) {
UserVmVO userVm = Mockito.mock(UserVmVO.class);
Mockito.lenient().when(userVm.getState()).thenReturn(vmState);
Mockito.when(userVmDao.findById(VM_ID)).thenReturn(userVm);
VMSnapshotVO vmSnapshot = Mockito.mock(VMSnapshotVO.class);
Mockito.when(vmSnapshot.getVmId()).thenReturn(VM_ID);
Mockito.when(vmSnapshot.getType()).thenReturn(VMSnapshot.Type.DiskAndMemory);

Assert.assertThrows(CloudRuntimeException.class, () -> strategy.revertVMSnapshot(vmSnapshot));
Mockito.verifyNoInteractions(vmSnapshotHelper);
}

@Test
public void revertDiskAndMemorySnapshotIsBlockedWhenVmIsRunning() {
assertRevertOfDiskAndMemorySnapshotIsBlocked(VirtualMachine.State.Running);
}

@Test
public void revertDiskAndMemorySnapshotIsBlockedWhenVmIsStopped() {
assertRevertOfDiskAndMemorySnapshotIsBlocked(VirtualMachine.State.Stopped);
}
}
36 changes: 35 additions & 1 deletion test/integration/plugins/storpool/TestVmSnapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.

# Import Local Modules
import math
import random
import time

Expand Down Expand Up @@ -104,6 +105,7 @@ def setUpCloudStack(cls):
with description %s" % cls.services["ostype"]

cls.template = template
cls.root_disk_size = int(math.ceil(template.size / (1024 ** 3)))

cls.account = cls.helper.create_account(
cls.apiclient,
Expand Down Expand Up @@ -169,7 +171,7 @@ def setUpCloudStack(cls):
domainid=cls.account.domainid,
serviceofferingid=cls.service_offering_only.id,
hypervisor=cls.hypervisor,
rootdisksize=10
rootdisksize=cls.root_disk_size
)

cls.random_data_0 = random_gen(size=100)
Expand Down Expand Up @@ -373,3 +375,35 @@ def test_03_delete_vm_snapshots(self):
self.debug('list_snapshot_response -------------------- %s' % list_snapshot_response)

self.assertIsNone(list_snapshot_response, "snapshot is already deleted")

@attr(tags=["advanced", "advancedns", "smoke"], required_hardware="true")
def test_04_create_vm_snapshot_with_memory_fails(self):
"""Test that creating a VM snapshot with memory is rejected for VMs with volumes on StorPool
"""
vm = list_virtual_machines(
self.apiclient,
id=self.virtual_machine.id,
listall=True)[0]
self.assertEqual(vm.state, RUNNING, "Check the VM is running")

MemorySnapshot = True
with self.assertRaises(Exception) as context:
VmSnapshot.create(
self.apiclient,
self.virtual_machine.id,
MemorySnapshot,
"TestMemorySnapshot",
"Display Text"
)
self.debug("Creating a VM snapshot with memory failed with: %s" % context.exception)
self.assertIn(
"not supported for volumes on StorPool",
str(context.exception),
"Check the VM snapshot with memory is rejected by StorPool"
)

list_snapshot_response = VmSnapshot.list(
self.apiclient,
virtualmachineid=self.virtual_machine.id,
listall=True)
self.assertIsNone(list_snapshot_response, "Check no VM snapshot was created")
Loading