Skip to content

Commit 8cb7e68

Browse files
committed
add unit tests to increase code coverage
1 parent ed47be2 commit 8cb7e68

File tree

6 files changed

+299
-17
lines changed

6 files changed

+299
-17
lines changed

engine/orchestration/src/test/java/org/apache/cloudstack/engine/orchestration/VolumeOrchestratorTest.java

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.cloudstack.engine.orchestration;
1818

1919
import com.cloud.configuration.Resource;
20+
import com.cloud.deploy.DeploymentClusterPlanner;
2021
import com.cloud.exception.InvalidParameterValueException;
2122
import com.cloud.exception.StorageAccessException;
2223
import com.cloud.host.Host;
@@ -46,7 +47,9 @@
4647
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory;
4748
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
4849
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeService;
50+
import org.apache.cloudstack.framework.config.ConfigDepot;
4951
import org.apache.cloudstack.framework.config.ConfigKey;
52+
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
5053
import org.apache.cloudstack.secret.PassphraseVO;
5154
import org.apache.cloudstack.secret.dao.PassphraseDao;
5255
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
@@ -69,7 +72,9 @@
6972
import java.util.Date;
7073
import java.util.List;
7174

75+
import static org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService.VolumeAllocationAlgorithm;
7276
import static org.junit.Assert.assertNotNull;
77+
import static org.junit.Assert.assertTrue;
7378

7479
@RunWith(MockitoJUnitRunner.class)
7580
public class VolumeOrchestratorTest {
@@ -96,20 +101,22 @@ public class VolumeOrchestratorTest {
96101
protected DiskOfferingDao diskOfferingDao;
97102
@Mock
98103
protected CallContext mockCallContext;
104+
@Mock
105+
ConfigDepot configDepot;
106+
@Mock
107+
ConfigurationDao configurationDao;
108+
99109

100110
@Spy
101111
@InjectMocks
102112
private VolumeOrchestrator volumeOrchestrator = new VolumeOrchestrator();
103113

114+
104115
private static final Long DEFAULT_ACCOUNT_PS_RESOURCE_COUNT = 100L;
105116
private Long accountPSResourceCount;
106-
private static final long MOCK_VOLUME_ID = 101L;
107117
private static final long MOCK_VM_ID = 202L;
108118
private static final long MOCK_POOL_ID = 303L;
109-
private static final long MOCK_DISK_OFFERING_ID = 404L;
110-
private static final long MOCK_ACCOUNT_ID = 505L;
111119
private static final String MOCK_VM_NAME = "Test-VM";
112-
private static final String MOCK_VOLUME_UUID = "vol-uuid-123";
113120

114121
@Before
115122
public void setUp() throws Exception {
@@ -375,7 +382,7 @@ public void testVolumeOnSharedStoragePoolTrue() {
375382
Mockito.when(pool.getScope()).thenReturn(ScopeType.CLUSTER); // Shared scope
376383
Mockito.when(storagePoolDao.findById(MOCK_POOL_ID)).thenReturn(pool);
377384

378-
Assert.assertTrue(volumeOrchestrator.volumeOnSharedStoragePool(volume));
385+
assertTrue(volumeOrchestrator.volumeOnSharedStoragePool(volume));
379386
}
380387

381388
@Test
@@ -414,7 +421,7 @@ public void testVolumeOnSharedStoragePoolFalsePoolNotFound() {
414421
public void testVolumeInactiveNoVmId() {
415422
VolumeVO volume = Mockito.mock(VolumeVO.class);
416423
Mockito.when(volume.getInstanceId()).thenReturn(null);
417-
Assert.assertTrue(volumeOrchestrator.volumeInactive(volume));
424+
assertTrue(volumeOrchestrator.volumeInactive(volume));
418425
Mockito.verify(entityMgr, Mockito.never()).findById(Mockito.eq(UserVm.class), Mockito.anyLong());
419426
}
420427

@@ -423,7 +430,7 @@ public void testVolumeInactiveVmNotFound() {
423430
VolumeVO volume = Mockito.mock(VolumeVO.class);
424431
Mockito.when(volume.getInstanceId()).thenReturn(MOCK_VM_ID);
425432
Mockito.when(entityMgr.findById(UserVm.class, MOCK_VM_ID)).thenReturn(null);
426-
Assert.assertTrue(volumeOrchestrator.volumeInactive(volume));
433+
assertTrue(volumeOrchestrator.volumeInactive(volume));
427434
}
428435

429436
@Test
@@ -433,7 +440,7 @@ public void testVolumeInactiveVmStopped() {
433440
UserVm vm = Mockito.mock(UserVm.class);
434441
Mockito.when(vm.getState()).thenReturn(VirtualMachine.State.Stopped);
435442
Mockito.when(entityMgr.findById(UserVm.class, MOCK_VM_ID)).thenReturn(vm);
436-
Assert.assertTrue(volumeOrchestrator.volumeInactive(volume));
443+
assertTrue(volumeOrchestrator.volumeInactive(volume));
437444
}
438445

439446
@Test
@@ -443,7 +450,7 @@ public void testVolumeInactiveVmDestroyed() {
443450
UserVm vm = Mockito.mock(UserVm.class);
444451
Mockito.when(vm.getState()).thenReturn(VirtualMachine.State.Destroyed);
445452
Mockito.when(entityMgr.findById(UserVm.class, MOCK_VM_ID)).thenReturn(vm);
446-
Assert.assertTrue(volumeOrchestrator.volumeInactive(volume));
453+
assertTrue(volumeOrchestrator.volumeInactive(volume));
447454
}
448455

449456
@Test
@@ -485,8 +492,8 @@ public void testGetVmNameOnVolumeSuccess() {
485492
@Test
486493
public void testValidateVolumeSizeRangeValid() throws Exception {
487494
overrideDefaultConfigValue(VolumeOrchestrator.MaxVolumeSize, "2000");
488-
Assert.assertTrue(volumeOrchestrator.validateVolumeSizeRange(1024 * 1024 * 1024)); // 1 GiB
489-
Assert.assertTrue(volumeOrchestrator.validateVolumeSizeRange(2000 * 1024 * 1024 * 1024)); // 2 TiB
495+
assertTrue(volumeOrchestrator.validateVolumeSizeRange(1024 * 1024 * 1024)); // 1 GiB
496+
assertTrue(volumeOrchestrator.validateVolumeSizeRange(2000 * 1024 * 1024 * 1024)); // 2 TiB
490497
}
491498

492499
@Test(expected = InvalidParameterValueException.class)
@@ -525,7 +532,7 @@ public void testCanVmRestartOnAnotherServerAllShared() {
525532
Mockito.when(storagePoolDao.findById(20L)).thenReturn(pool2);
526533

527534

528-
Assert.assertTrue(volumeOrchestrator.canVmRestartOnAnotherServer(MOCK_VM_ID));
535+
assertTrue(volumeOrchestrator.canVmRestartOnAnotherServer(MOCK_VM_ID));
529536
}
530537

531538
@Test
@@ -567,7 +574,7 @@ public void testCanVmRestartOnAnotherServerOneLocalRecreatable() {
567574
Mockito.when(storagePoolDao.findById(10L)).thenReturn(pool1);
568575
Mockito.when(storagePoolDao.findById(30L)).thenReturn(pool2);
569576

570-
Assert.assertTrue("VM restart should be true if local disk is recreatable",
577+
assertTrue("VM restart should be true if local disk is recreatable",
571578
volumeOrchestrator.canVmRestartOnAnotherServer(MOCK_VM_ID));
572579
}
573580

@@ -576,4 +583,17 @@ private void overrideDefaultConfigValue(final ConfigKey configKey, final String
576583
f.setAccessible(true);
577584
f.set(configKey, value);
578585
}
586+
587+
@Test
588+
public void testStart() throws Exception {
589+
Mockito.when(configDepot.isNewConfig(VolumeAllocationAlgorithm)).thenReturn(true);
590+
overrideDefaultConfigValue(DeploymentClusterPlanner.VmAllocationAlgorithm, "firstfit");
591+
Mockito.when(configurationDao.update(Mockito.anyString(), Mockito.anyString())).thenReturn(true);
592+
volumeOrchestrator.start();
593+
}
594+
595+
@Test
596+
public void testConfigKeys() {
597+
assertTrue(volumeOrchestrator.getConfigKeys().length > 0);
598+
}
579599
}

engine/storage/src/main/java/org/apache/cloudstack/storage/allocator/AbstractStoragePoolAllocator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public abstract class AbstractStoragePoolAllocator extends AdapterBase implement
7777
@Inject protected PrimaryDataStoreDao storagePoolDao;
7878
@Inject protected VolumeDao volumeDao;
7979
@Inject protected ConfigurationDao configDao;
80-
@Inject private CapacityDao capacityDao;
80+
@Inject protected CapacityDao capacityDao;
8181
@Inject private ClusterDao clusterDao;
8282
@Inject private StorageManager storageMgr;
8383
@Inject private StorageUtil storageUtil;

engine/storage/src/main/java/org/apache/cloudstack/storage/allocator/ZoneWideStoragePoolAllocator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class ZoneWideStoragePoolAllocator extends AbstractStoragePoolAllocator {
4949
@Inject
5050
private DataStoreManager dataStoreMgr;
5151
@Inject
52-
private CapacityDao capacityDao;
52+
protected CapacityDao capacityDao;
5353

5454
@Override
5555
protected List<StoragePool> select(DiskProfile dskCh, VirtualMachineProfile vmProfile, DeploymentPlan plan, ExcludeList avoid, int returnUpTo, boolean bypassStorageTypeCheck, String keyword) {

engine/storage/src/test/java/org/apache/cloudstack/storage/allocator/AbstractStoragePoolAllocatorTest.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@
1717
package org.apache.cloudstack.storage.allocator;
1818

1919

20+
import com.cloud.capacity.Capacity;
21+
import com.cloud.capacity.dao.CapacityDao;
2022
import com.cloud.deploy.DeploymentPlan;
2123
import com.cloud.deploy.DeploymentPlanner;
2224
import com.cloud.storage.Storage;
2325
import com.cloud.storage.StoragePool;
2426
import com.cloud.storage.dao.VolumeDao;
2527
import com.cloud.user.Account;
28+
import com.cloud.utils.Pair;
2629
import com.cloud.vm.DiskProfile;
2730
import com.cloud.vm.VirtualMachineProfile;
28-
2931
import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService;
3032
import org.apache.cloudstack.framework.config.ConfigKey;
3133
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
32-
3334
import org.junit.After;
3435
import org.junit.Assert;
3536
import org.junit.Before;
@@ -41,10 +42,15 @@
4142

4243
import java.lang.reflect.Field;
4344
import java.util.ArrayList;
45+
import java.util.Arrays;
46+
import java.util.HashMap;
4447
import java.util.HashSet;
4548
import java.util.List;
49+
import java.util.Map;
4650
import java.util.Set;
4751

52+
import static org.junit.Assert.assertEquals;
53+
import static org.mockito.Mockito.mock;
4854
import static org.mockito.Mockito.when;
4955

5056
@RunWith(MockitoJUnitRunner.class)
@@ -57,6 +63,10 @@ public class AbstractStoragePoolAllocatorTest {
5763

5864
@Mock
5965
Account account;
66+
67+
@Mock
68+
CapacityDao capacityDao;
69+
6070
private List<StoragePool> pools;
6171

6272
@Mock
@@ -137,6 +147,28 @@ public void reorderRandomPools() {
137147
Assert.assertTrue(firstchoice.size() > 2);
138148
}
139149

150+
@Test
151+
public void reorderStoragePoolsBasedOnAlgorithmFirstFitLeastConsumed() throws Exception {
152+
overrideDefaultConfigValue(VolumeOrchestrationService.VolumeAllocationAlgorithm, "firstfitleastconsumed");
153+
when(plan.getDataCenterId()).thenReturn(1L);
154+
when(plan.getClusterId()).thenReturn(1L);
155+
StoragePool pool1 = mock(StoragePool.class);
156+
StoragePool pool2 = mock(StoragePool.class);
157+
when(pool1.getId()).thenReturn(1L);
158+
when(pool2.getId()).thenReturn(2L);
159+
List<StoragePool> pools = Arrays.asList(pool1, pool2);
160+
List<Long> poolIds = Arrays.asList(2L, 1L);
161+
Map<Long, Double> hostCapacityMap = new HashMap<>();
162+
hostCapacityMap.put(1L, 8.0);
163+
hostCapacityMap.put(2L, 8.5);
164+
Pair<List<Long>, Map<Long, Double>> poolsOrderedByCapacity = new Pair<>(poolIds, hostCapacityMap);
165+
166+
allocator.capacityDao = capacityDao;
167+
Mockito.when(capacityDao.orderHostsByFreeCapacity(1L, 1L, Capacity.CAPACITY_TYPE_LOCAL_STORAGE)).thenReturn(poolsOrderedByCapacity);
168+
List<StoragePool> result = allocator.reorderPoolsByCapacity(plan, pools);
169+
assertEquals(Arrays.asList(pool2, pool1), result);
170+
}
171+
140172
private void overrideDefaultConfigValue(final ConfigKey configKey, final String value) throws IllegalAccessException, NoSuchFieldException {
141173
final Field f = ConfigKey.class.getDeclaredField("_defaultValue");
142174
f.setAccessible(true);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.cloudstack.storage.allocator;
20+
21+
import com.cloud.capacity.Capacity;
22+
import com.cloud.capacity.dao.CapacityDao;
23+
import com.cloud.deploy.DeploymentPlan;
24+
import com.cloud.storage.Storage;
25+
import com.cloud.storage.StoragePool;
26+
import com.cloud.utils.Pair;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
import org.mockito.Mockito;
30+
31+
import java.util.Arrays;
32+
import java.util.HashMap;
33+
import java.util.List;
34+
import java.util.Map;
35+
36+
import static org.junit.Assert.assertEquals;
37+
import static org.mockito.Mockito.mock;
38+
import static org.mockito.Mockito.when;
39+
40+
public class ZoneWideStoragePoolAllocatorTest {
41+
private ZoneWideStoragePoolAllocator allocator;
42+
private DeploymentPlan plan;
43+
44+
@Before
45+
public void setUp() {
46+
allocator = new ZoneWideStoragePoolAllocator();
47+
plan = mock(DeploymentPlan.class);
48+
}
49+
50+
@Test
51+
public void testReorderPoolsByCapacity() {
52+
when(plan.getDataCenterId()).thenReturn(1L);
53+
when(plan.getClusterId()).thenReturn(null);
54+
StoragePool pool1 = mock(StoragePool.class);
55+
StoragePool pool2 = mock(StoragePool.class);
56+
when(pool1.getPoolType()).thenReturn(Storage.StoragePoolType.Filesystem);
57+
when(pool1.getId()).thenReturn(1L);
58+
when(pool2.getId()).thenReturn(2L);
59+
List<StoragePool> pools = Arrays.asList(pool1, pool2);
60+
List<Long> poolIds = Arrays.asList(2L, 1L);
61+
Map<Long, Double> hostCapacityMap = new HashMap<>();
62+
hostCapacityMap.put(1L, 8.0);
63+
hostCapacityMap.put(2L, 8.5);
64+
Pair<List<Long>, Map<Long, Double>> poolsOrderedByCapacity = new Pair<>(poolIds, hostCapacityMap);
65+
CapacityDao capacityDao = mock(CapacityDao.class);
66+
Mockito.when(capacityDao.orderHostsByFreeCapacity(1L, null, Capacity.CAPACITY_TYPE_LOCAL_STORAGE)).thenReturn(poolsOrderedByCapacity);
67+
allocator.capacityDao = capacityDao;
68+
List<StoragePool> result = allocator.reorderPoolsByCapacity(plan, pools);
69+
assertEquals(Arrays.asList(pool2, pool1), result);
70+
}
71+
}

0 commit comments

Comments
 (0)