Skip to content

Commit b95cdce

Browse files
author
Daan Hoogland
committed
tests
1 parent f603e1b commit b95cdce

2 files changed

Lines changed: 150 additions & 0 deletions

File tree

engine/schema/src/test/java/com/cloud/host/dao/HostDaoImplTest.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.mockito.Mockito.mock;
2525
import static org.mockito.Mockito.when;
2626

27+
import java.util.ArrayList;
2728
import java.util.Arrays;
2829
import java.util.Collections;
2930
import java.util.List;
@@ -230,6 +231,114 @@ public void testListDistinctHypervisorArchTypes_WithoutZone() {
230231
assertEquals(CPU.CPUArch.amd64, result.get(0).second());
231232
}
232233

234+
@Test
235+
public void testFindHostsWithTagRuleThatMatchComputeOferringTagsScopedNoScopeReturnsAllMatches() {
236+
String offeringTag = "ssd";
237+
HostVO host1 = mock(HostVO.class);
238+
HostVO host2 = mock(HostVO.class);
239+
List<HostVO> unscopedMatches = List.of(host1, host2);
240+
doReturn(unscopedMatches).when(hostDao).findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag);
241+
242+
List<HostVO> result = hostDao.findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag, null, null, null);
243+
244+
assertEquals(unscopedMatches, result);
245+
}
246+
247+
@Test
248+
public void testFindHostsWithTagRuleThatMatchComputeOferringTagsScopedFiltersByDataCenter() {
249+
String offeringTag = "ssd";
250+
long dcId = 1L;
251+
HostVO hostInZone = mock(HostVO.class);
252+
when(hostInZone.getDataCenterId()).thenReturn(dcId);
253+
HostVO hostInOtherZone = mock(HostVO.class);
254+
when(hostInOtherZone.getDataCenterId()).thenReturn(2L);
255+
doReturn(List.of(hostInZone, hostInOtherZone)).when(hostDao).findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag);
256+
257+
List<HostVO> result = hostDao.findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag, null, null, dcId);
258+
259+
assertEquals(1, result.size());
260+
Assert.assertSame(hostInZone, result.get(0));
261+
}
262+
263+
@Test
264+
public void testFindHostsWithTagRuleThatMatchComputeOferringTagsScopedFiltersByPodAndCluster() {
265+
String offeringTag = "ssd";
266+
long dcId = 1L;
267+
Long podId = 2L;
268+
Long clusterId = 3L;
269+
270+
HostVO matchingHost = mock(HostVO.class);
271+
when(matchingHost.getDataCenterId()).thenReturn(dcId);
272+
when(matchingHost.getPodId()).thenReturn(podId);
273+
when(matchingHost.getClusterId()).thenReturn(clusterId);
274+
275+
HostVO wrongPodHost = mock(HostVO.class);
276+
when(wrongPodHost.getDataCenterId()).thenReturn(dcId);
277+
when(wrongPodHost.getPodId()).thenReturn(99L);
278+
279+
HostVO wrongClusterHost = mock(HostVO.class);
280+
when(wrongClusterHost.getDataCenterId()).thenReturn(dcId);
281+
when(wrongClusterHost.getPodId()).thenReturn(podId);
282+
when(wrongClusterHost.getClusterId()).thenReturn(99L);
283+
284+
doReturn(List.of(matchingHost, wrongPodHost, wrongClusterHost)).when(hostDao)
285+
.findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag);
286+
287+
List<HostVO> result = hostDao.findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag, clusterId, podId, dcId);
288+
289+
assertEquals(1, result.size());
290+
Assert.assertSame(matchingHost, result.get(0));
291+
}
292+
293+
@Test
294+
public void testFindHostsWithTagRuleThatMatchComputeOferringTagsScopedExcludesHostWithNullPod() {
295+
String offeringTag = "ssd";
296+
long dcId = 1L;
297+
Long podId = 2L;
298+
Long clusterId = 3L;
299+
300+
HostVO hostWithNoPod = mock(HostVO.class);
301+
when(hostWithNoPod.getDataCenterId()).thenReturn(dcId);
302+
when(hostWithNoPod.getPodId()).thenReturn(null);
303+
304+
doReturn(List.of(hostWithNoPod)).when(hostDao)
305+
.findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag);
306+
307+
List<HostVO> result = hostDao.findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag, clusterId, podId, dcId);
308+
309+
Assert.assertTrue(result.isEmpty());
310+
}
311+
312+
@Test
313+
public void testFindHostsWithTagRuleThatMatchComputeOferringTagsScopedExcludesHostWithNullCluster() {
314+
String offeringTag = "ssd";
315+
long dcId = 1L;
316+
Long podId = 2L;
317+
Long clusterId = 3L;
318+
319+
HostVO hostWithNoCluster = mock(HostVO.class);
320+
when(hostWithNoCluster.getDataCenterId()).thenReturn(dcId);
321+
when(hostWithNoCluster.getPodId()).thenReturn(podId);
322+
when(hostWithNoCluster.getClusterId()).thenReturn(null);
323+
324+
doReturn(List.of(hostWithNoCluster)).when(hostDao)
325+
.findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag);
326+
327+
List<HostVO> result = hostDao.findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag, clusterId, podId, dcId);
328+
329+
Assert.assertTrue(result.isEmpty());
330+
}
331+
332+
@Test
333+
public void testFindHostsWithTagRuleThatMatchComputeOferringTagsScopedNoMatchesReturnsEmpty() {
334+
String offeringTag = "ssd";
335+
doReturn(new ArrayList<>()).when(hostDao).findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag);
336+
337+
List<HostVO> result = hostDao.findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag, 3L, 2L, 1L);
338+
339+
Assert.assertTrue(result.isEmpty());
340+
}
341+
233342
@Test
234343
public void testListDistinctArchTypes() {
235344
Long clusterId = 1L;

server/src/test/java/com/cloud/agent/manager/allocator/impl/FirstFitAllocatorTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@
3737
import com.cloud.storage.VMTemplateVO;
3838
import com.cloud.user.Account;
3939
import com.cloud.vm.VirtualMachineProfile;
40+
import com.cloud.vm.dao.UserVmDetailsDao;
4041

4142
@RunWith(MockitoJUnitRunner.class)
4243
public class FirstFitAllocatorTest {
4344

4445
@Mock
4546
HostDao hostDao;
47+
@Mock
48+
UserVmDetailsDao userVmDetailsDao;
4649
@Spy
4750
@InjectMocks
4851
FirstFitAllocator firstFitAllocator;
@@ -82,4 +85,42 @@ public void testAllocateToWithHostsUsesScopedRuleTagLookup() {
8285
Mockito.verify(hostDao).findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag, clusterId, podId, dcId);
8386
Mockito.verify(hostDao, Mockito.never()).findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag);
8487
}
88+
89+
@Test
90+
public void testAllocateToWithoutHostsUsesScopedRuleTagLookup() {
91+
Host.Type type = Host.Type.Routing;
92+
long dcId = 1L;
93+
Long podId = 2L;
94+
Long clusterId = 3L;
95+
String offeringTag = "compute";
96+
97+
DeploymentPlan plan = Mockito.mock(DeploymentPlan.class);
98+
Mockito.when(plan.getDataCenterId()).thenReturn(dcId);
99+
Mockito.when(plan.getPodId()).thenReturn(podId);
100+
Mockito.when(plan.getClusterId()).thenReturn(clusterId);
101+
102+
VirtualMachineProfile vmProfile = Mockito.mock(VirtualMachineProfile.class);
103+
ServiceOffering offering = Mockito.mock(ServiceOffering.class);
104+
VMTemplateVO template = Mockito.mock(VMTemplateVO.class);
105+
Account account = Mockito.mock(Account.class);
106+
Mockito.when(vmProfile.getServiceOffering()).thenReturn(offering);
107+
Mockito.when(vmProfile.getTemplate()).thenReturn(template);
108+
Mockito.when(vmProfile.getOwner()).thenReturn(account);
109+
Mockito.when(offering.getHostTag()).thenReturn(offeringTag);
110+
Mockito.when(userVmDetailsDao.findDetail(Mockito.anyLong(), Mockito.eq("UEFI"))).thenReturn(null);
111+
112+
HostVO host = Mockito.mock(HostVO.class);
113+
List<Host> selectedHosts = List.of(host);
114+
Mockito.when(hostDao.listByHostTag(type, clusterId, podId, dcId, offeringTag)).thenReturn(new ArrayList<>(List.of(host)));
115+
Mockito.when(hostDao.findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag, clusterId, podId, dcId)).thenReturn(new ArrayList<>());
116+
Mockito.when(hostDao.listAllUpAndEnabledNonHAHosts(type, clusterId, podId, dcId, null)).thenReturn(new ArrayList<>());
117+
Mockito.doReturn(selectedHosts).when(firstFitAllocator).allocateTo(
118+
Mockito.eq(plan), Mockito.eq(offering), Mockito.eq(template), Mockito.any(ExcludeList.class), Mockito.anyList(), Mockito.eq(1), Mockito.eq(true), Mockito.eq(account));
119+
120+
List<Host> result = firstFitAllocator.allocateTo(vmProfile, plan, type, new ExcludeList(), 1, true);
121+
122+
Assert.assertEquals(1, result.size());
123+
Mockito.verify(hostDao).findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag, clusterId, podId, dcId);
124+
Mockito.verify(hostDao, Mockito.never()).findHostsWithTagRuleThatMatchComputeOferringTags(offeringTag);
125+
}
85126
}

0 commit comments

Comments
 (0)