|
24 | 24 | import static org.mockito.Mockito.mock; |
25 | 25 | import static org.mockito.Mockito.when; |
26 | 26 |
|
| 27 | +import java.util.ArrayList; |
27 | 28 | import java.util.Arrays; |
28 | 29 | import java.util.Collections; |
29 | 30 | import java.util.List; |
@@ -230,6 +231,114 @@ public void testListDistinctHypervisorArchTypes_WithoutZone() { |
230 | 231 | assertEquals(CPU.CPUArch.amd64, result.get(0).second()); |
231 | 232 | } |
232 | 233 |
|
| 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 | + |
233 | 342 | @Test |
234 | 343 | public void testListDistinctArchTypes() { |
235 | 344 | Long clusterId = 1L; |
|
0 commit comments