Skip to content

Commit 96d7c13

Browse files
Add JUnit test cases
1 parent 6b84876 commit 96d7c13

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3792,7 +3792,7 @@ public void checkIfCanUpgrade(final VirtualMachine vmInstance, final ServiceOffe
37923792
}
37933793
}
37943794

3795-
private void checkIfNewOfferingStorageScopeMatchesStoragePool(VirtualMachine vmInstance, ServiceOffering newServiceOffering) {
3795+
protected void checkIfNewOfferingStorageScopeMatchesStoragePool(VirtualMachine vmInstance, ServiceOffering newServiceOffering) {
37963796
boolean isRootVolumeOnLocalStorage = isRootVolumeOnLocalStorage(vmInstance.getId());
37973797

37983798
if (newServiceOffering.isUseLocalStorage() && !isRootVolumeOnLocalStorage) {

engine/orchestration/src/test/java/com/cloud/vm/VirtualMachineManagerImplTest.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@
3131
import java.util.List;
3232
import java.util.Map;
3333

34+
import com.cloud.exception.InvalidParameterValueException;
35+
import com.cloud.offering.ServiceOffering;
3436
import org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator;
3537
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
3638
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
39+
import org.apache.commons.collections.CollectionUtils;
3740
import org.junit.Assert;
3841
import org.junit.Before;
3942
import org.junit.Test;
@@ -43,7 +46,7 @@
4346
import org.mockito.Mock;
4447
import org.mockito.Mockito;
4548
import org.mockito.Spy;
46-
import org.mockito.runners.MockitoJUnitRunner;
49+
import org.mockito.junit.MockitoJUnitRunner;
4750

4851
import com.cloud.agent.AgentManager;
4952
import com.cloud.agent.api.Command;
@@ -624,4 +627,59 @@ public void matchesOfSorts() {
624627
assertTrue(VirtualMachineManagerImpl.matches(tags,three));
625628
assertTrue(VirtualMachineManagerImpl.matches(others,three));
626629
}
630+
631+
@Test
632+
public void isRootVolumeOnLocalStorageTestOnLocal() {
633+
prepareAndTestIsRootVolumeOnLocalStorage(ScopeType.HOST, true);
634+
}
635+
636+
@Test
637+
public void isRootVolumeOnLocalStorageTestCluster() {
638+
prepareAndTestIsRootVolumeOnLocalStorage(ScopeType.CLUSTER, false);
639+
}
640+
641+
@Test
642+
public void isRootVolumeOnLocalStorageTestZone() {
643+
prepareAndTestIsRootVolumeOnLocalStorage(ScopeType.ZONE, false);
644+
}
645+
646+
private void prepareAndTestIsRootVolumeOnLocalStorage(ScopeType scope, boolean expected) {
647+
StoragePoolVO storagePoolVoMock = Mockito.mock(StoragePoolVO.class);
648+
Mockito.doReturn(storagePoolVoMock).when(storagePoolDaoMock).findById(Mockito.anyLong());
649+
Mockito.doReturn(scope).when(storagePoolVoMock).getScope();
650+
List<VolumeVO> mockedVolumes = new ArrayList<>();
651+
mockedVolumes.add(volumeVoMock);
652+
Mockito.doReturn(mockedVolumes).when(volumeDaoMock).findByInstanceAndType(Mockito.anyLong(), Mockito.any());
653+
654+
boolean result = virtualMachineManagerImpl.isRootVolumeOnLocalStorage(0l);
655+
656+
Assert.assertEquals(expected, result);
657+
}
658+
659+
@Test
660+
public void checkIfNewOfferingStorageScopeMatchesStoragePoolTestLocalLocal() {
661+
prepareAndRunCheckIfNewOfferingStorageScopeMatchesStoragePool(true, true);
662+
}
663+
664+
@Test
665+
public void checkIfNewOfferingStorageScopeMatchesStoragePoolTestSharedShared() {
666+
prepareAndRunCheckIfNewOfferingStorageScopeMatchesStoragePool(false, false);
667+
}
668+
669+
@Test (expected = InvalidParameterValueException.class)
670+
public void checkIfNewOfferingStorageScopeMatchesStoragePoolTestLocalShared() {
671+
prepareAndRunCheckIfNewOfferingStorageScopeMatchesStoragePool(true, false);
672+
}
673+
674+
@Test (expected = InvalidParameterValueException.class)
675+
public void checkIfNewOfferingStorageScopeMatchesStoragePoolTestSharedLocal() {
676+
prepareAndRunCheckIfNewOfferingStorageScopeMatchesStoragePool(false, true);
677+
}
678+
679+
private void prepareAndRunCheckIfNewOfferingStorageScopeMatchesStoragePool(boolean isRootOnLocal, boolean isOfferingUsingLocal) {
680+
Mockito.doReturn(isRootOnLocal).when(virtualMachineManagerImpl).isRootVolumeOnLocalStorage(Mockito.anyLong());
681+
Mockito.doReturn("vmInstanceMockedToString").when(vmInstanceMock).toString();
682+
Mockito.doReturn(isOfferingUsingLocal).when(serviceOfferingMock).isUseLocalStorage();
683+
virtualMachineManagerImpl.checkIfNewOfferingStorageScopeMatchesStoragePool(vmInstanceMock, serviceOfferingMock);
684+
}
627685
}

0 commit comments

Comments
 (0)