Skip to content

Commit 9721484

Browse files
committed
add unit tests to increase code coverage
1 parent 979386d commit 9721484

7 files changed

Lines changed: 915 additions & 31 deletions

File tree

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

Lines changed: 399 additions & 11 deletions
Large diffs are not rendered by default.

engine/schema/src/test/java/com/cloud/capacity/dao/CapacityDaoImplTest.java

Lines changed: 250 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,59 @@
1616
// under the License.
1717
package com.cloud.capacity.dao;
1818

19+
import com.cloud.capacity.CapacityVO;
20+
import com.cloud.host.Host;
21+
import com.cloud.utils.Pair;
22+
import com.cloud.utils.Ternary;
23+
import com.cloud.utils.db.SearchBuilder;
24+
import com.cloud.utils.db.SearchCriteria;
25+
import com.cloud.utils.db.TransactionLegacy;
26+
import org.junit.After;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.mockito.InjectMocks;
31+
import org.mockito.Mock;
32+
import org.mockito.MockedStatic;
33+
import org.mockito.Mockito;
34+
import org.mockito.Spy;
35+
import org.mockito.junit.MockitoJUnitRunner;
36+
37+
import java.sql.PreparedStatement;
38+
import java.sql.ResultSet;
39+
import java.util.Arrays;
40+
import java.util.Collections;
41+
import java.util.List;
42+
import java.util.Map;
43+
1944
import static org.junit.Assert.assertEquals;
45+
import static org.junit.Assert.assertNotNull;
2046
import static org.junit.Assert.assertSame;
2147
import static org.junit.Assert.assertTrue;
48+
import static org.mockito.ArgumentMatchers.anyString;
2249
import static org.mockito.Mockito.any;
50+
import static org.mockito.Mockito.doNothing;
2351
import static org.mockito.Mockito.doReturn;
2452
import static org.mockito.Mockito.eq;
2553
import static org.mockito.Mockito.mock;
54+
import static org.mockito.Mockito.times;
2655
import static org.mockito.Mockito.verify;
2756
import static org.mockito.Mockito.when;
2857

29-
import java.util.Arrays;
30-
import java.util.Collections;
31-
import java.util.List;
32-
33-
import org.junit.Before;
34-
import org.junit.Test;
35-
import org.junit.runner.RunWith;
36-
import org.mockito.InjectMocks;
37-
import org.mockito.Mockito;
38-
import org.mockito.Spy;
39-
import org.mockito.junit.MockitoJUnitRunner;
40-
41-
import com.cloud.capacity.CapacityVO;
42-
import com.cloud.utils.db.SearchBuilder;
43-
import com.cloud.utils.db.SearchCriteria;
44-
4558
@RunWith(MockitoJUnitRunner.class)
4659
public class CapacityDaoImplTest {
4760
@Spy
4861
@InjectMocks
4962
CapacityDaoImpl capacityDao = new CapacityDaoImpl();
5063

64+
@Mock
65+
private TransactionLegacy txn;
66+
@Mock
67+
private PreparedStatement pstmt;
68+
@Mock
69+
private ResultSet resultSet;
70+
private MockedStatic<TransactionLegacy> mockedTransactionLegacy;
71+
5172
private SearchBuilder<CapacityVO> searchBuilder;
5273
private SearchCriteria<CapacityVO> searchCriteria;
5374

@@ -59,6 +80,16 @@ public void setUp() {
5980
searchCriteria = mock(SearchCriteria.class);
6081
doReturn(searchBuilder).when(capacityDao).createSearchBuilder();
6182
when(searchBuilder.create()).thenReturn(searchCriteria);
83+
84+
mockedTransactionLegacy = Mockito.mockStatic(TransactionLegacy.class);
85+
mockedTransactionLegacy.when(TransactionLegacy::currentTxn).thenReturn(txn);
86+
}
87+
88+
@After
89+
public void tearDown() {
90+
if (mockedTransactionLegacy != null) {
91+
mockedTransactionLegacy.close();
92+
}
6293
}
6394

6495
@Test
@@ -96,4 +127,207 @@ public void testListByHostIdTypesEmptyResult() {
96127
verify(capacityDao).listBy(searchCriteria);
97128
assertTrue(result.isEmpty());
98129
}
130+
131+
@Test
132+
public void testListClustersCrossingThresholdEmptyResult() throws Exception {
133+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
134+
when(pstmt.executeQuery()).thenReturn(resultSet);
135+
when(resultSet.next()).thenReturn(false);
136+
List<Long> result = capacityDao.listClustersCrossingThreshold((short)1, 1L, "cpu.threshold", 5000L);
137+
assertNotNull(result);
138+
assertTrue(result.isEmpty());
139+
}
140+
141+
@Test
142+
public void testFindCapacityByZoneAndHostTagNoResults() throws Exception {
143+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
144+
when(pstmt.executeQuery()).thenReturn(resultSet);
145+
when(resultSet.next()).thenReturn(false);
146+
147+
Ternary<Long, Long, Long> result = capacityDao.findCapacityByZoneAndHostTag(1L, "host-tag");
148+
assertNotNull(result);
149+
assertEquals(Long.valueOf(0L), result.first());
150+
assertEquals(Long.valueOf(0L), result.second());
151+
assertEquals(Long.valueOf(0L), result.third());
152+
}
153+
@Test
154+
public void testFindByHostIdType() {
155+
CapacityVO capacity = new CapacityVO();
156+
capacity.setHostId(1L);
157+
capacity.setCapacityType((short) 1);
158+
159+
doReturn(capacity).when(capacityDao).findOneBy(any());
160+
161+
CapacityVO found = capacityDao.findByHostIdType(1L, (short) 1);
162+
assertNotNull(found);
163+
assertEquals(Long.valueOf(1L), found.getHostOrPoolId());
164+
}
165+
166+
@Test
167+
public void testUpdateAllocatedAddition() throws Exception {
168+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
169+
doNothing().when(txn).start();
170+
when(txn.commit()).thenReturn(true);
171+
172+
capacityDao.updateAllocated(1L, 1000L, (short)1, true);
173+
174+
verify(txn, times(1)).start();
175+
verify(txn, times(1)).commit();
176+
verify(pstmt, times(1)).executeUpdate();
177+
}
178+
179+
@Test
180+
public void testUpdateAllocatedSubtraction() throws Exception {
181+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
182+
doNothing().when(txn).start();
183+
when(txn.commit()).thenReturn(true);
184+
185+
capacityDao.updateAllocated(1L, 500L, (short)1, false);
186+
187+
verify(txn, times(1)).start();
188+
verify(txn, times(1)).commit();
189+
verify(pstmt, times(1)).executeUpdate();
190+
}
191+
192+
@Test
193+
public void testFindFilteredCapacityByEmptyResult() throws Exception {
194+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
195+
when(pstmt.executeQuery()).thenReturn(resultSet);
196+
when(resultSet.next()).thenReturn(false);
197+
List<CapacityDaoImpl.SummedCapacity> result = capacityDao.findFilteredCapacityBy(null, null, null, null, Collections.emptyList(), Collections.emptyList());
198+
assertNotNull(result);
199+
assertTrue(result.isEmpty());
200+
}
201+
202+
@Test
203+
public void testListClustersInZoneOrPodByHostCapacitiesEmpty() throws Exception {
204+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
205+
when(pstmt.executeQuery()).thenReturn(resultSet);
206+
when(resultSet.next()).thenReturn(false);
207+
208+
List<Long> resultZone = capacityDao.listClustersInZoneOrPodByHostCapacities(1L, 123L, 2, 2048L, (short)0, true);
209+
assertNotNull(resultZone);
210+
assertTrue(resultZone.isEmpty());
211+
212+
List<Long> resultPod = capacityDao.listClustersInZoneOrPodByHostCapacities(1L, 123L, 2, 2048L, (short)0, false);
213+
assertNotNull(resultPod);
214+
assertTrue(resultPod.isEmpty());
215+
}
216+
217+
218+
@Test
219+
public void testListHostsWithEnoughCapacityEmptyResult() throws Exception {
220+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
221+
when(pstmt.executeQuery()).thenReturn(resultSet);
222+
when(resultSet.next()).thenReturn(false);
223+
224+
List<Long> result = capacityDao.listHostsWithEnoughCapacity(1, 100L, 200L, Host.Type.Routing.toString());
225+
assertNotNull(result);
226+
assertTrue(result.isEmpty());
227+
}
228+
229+
230+
@Test
231+
public void testOrderClustersByAggregateCapacityEmptyResult() throws Exception {
232+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
233+
when(pstmt.executeQuery()).thenReturn(resultSet);
234+
when(resultSet.next()).thenReturn(false);
235+
236+
Pair<List<Long>, Map<Long, Double>> result = capacityDao.orderClustersByAggregateCapacity(1L, 1L, (short) 1, true);
237+
assertNotNull(result);
238+
assertTrue(result.first().isEmpty());
239+
assertTrue(result.second().isEmpty());
240+
}
241+
242+
243+
@Test
244+
public void testOrderPodsByAggregateCapacityEmptyResult() throws Exception {
245+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
246+
when(pstmt.executeQuery()).thenReturn(resultSet);
247+
when(resultSet.next()).thenReturn(false);
248+
249+
Pair<List<Long>, Map<Long, Double>> result = capacityDao.orderPodsByAggregateCapacity(1L, (short) 1);
250+
assertNotNull(result);
251+
assertTrue(result.first().isEmpty());
252+
assertTrue(result.second().isEmpty());
253+
}
254+
255+
256+
@Test
257+
public void testUpdateCapacityState() throws Exception {
258+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
259+
when(pstmt.executeUpdate()).thenReturn(1);
260+
261+
capacityDao.updateCapacityState(1L, 1L, 1L, 1L, "Enabled", new short[]{1});
262+
263+
verify(pstmt, times(1)).executeUpdate();
264+
}
265+
266+
267+
@Test
268+
public void testFindClusterConsumption() throws Exception {
269+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
270+
when(pstmt.executeQuery()).thenReturn(resultSet);
271+
when(resultSet.next()).thenReturn(true);
272+
when(resultSet.getFloat(1)).thenReturn(0.5f);
273+
274+
float result = capacityDao.findClusterConsumption(1L, (short) 1, 1000L);
275+
assertEquals(0.5f, result, 0.0f);
276+
}
277+
278+
@Test
279+
public void testListPodsByHostCapacitiesEmptyResult() throws Exception {
280+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
281+
when(pstmt.executeQuery()).thenReturn(resultSet);
282+
when(resultSet.next()).thenReturn(false);
283+
284+
List<Long> result = capacityDao.listPodsByHostCapacities(1L, 2, 1024L, (short)0);
285+
assertNotNull(result);
286+
assertTrue(result.isEmpty());
287+
}
288+
289+
@Test
290+
public void testOrderHostsByFreeCapacityEmptyResult() throws Exception {
291+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
292+
when(pstmt.executeQuery()).thenReturn(resultSet);
293+
when(resultSet.next()).thenReturn(false);
294+
295+
Pair<List<Long>, Map<Long, Double>> result = capacityDao.orderHostsByFreeCapacity(1L, 1L, (short) 0);
296+
assertNotNull(result);
297+
assertTrue(result.first().isEmpty());
298+
}
299+
300+
@Test
301+
public void testFindByClusterPodZoneEmptyResult() throws Exception {
302+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
303+
when(pstmt.executeQuery()).thenReturn(resultSet);
304+
when(resultSet.next()).thenReturn(false);
305+
306+
List<CapacityDaoImpl.SummedCapacity> result = capacityDao.findByClusterPodZone(1L, 1L, 1L);
307+
assertNotNull(result);
308+
assertTrue(result.isEmpty());
309+
}
310+
311+
@Test
312+
public void testListCapacitiesGroupedByLevelAndTypeEmptyResult() throws Exception {
313+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
314+
when(pstmt.executeQuery()).thenReturn(resultSet);
315+
when(resultSet.next()).thenReturn(false);
316+
317+
List<CapacityDaoImpl.SummedCapacity> result = capacityDao.listCapacitiesGroupedByLevelAndType(0, 1L,
318+
1L, 1L, 0, Collections.emptyList(), Collections.emptyList(), 1L);
319+
assertNotNull(result);
320+
assertTrue(result.isEmpty());
321+
}
322+
323+
@Test
324+
public void testFindCapacityByEmptyResult() throws Exception {
325+
when(txn.prepareAutoCloseStatement(anyString())).thenReturn(pstmt);
326+
when(pstmt.executeQuery()).thenReturn(resultSet);
327+
when(resultSet.next()).thenReturn(false);
328+
329+
List<CapacityDaoImpl.SummedCapacity> result = capacityDao.findCapacityBy(1, 1L, 1L, 1L);
330+
assertNotNull(result);
331+
assertTrue(result.isEmpty());
332+
}
99333
}

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);

0 commit comments

Comments
 (0)