|
23 | 23 | import static org.mockito.ArgumentMatchers.eq; |
24 | 24 | import static org.mockito.ArgumentMatchers.matches; |
25 | 25 | import static org.mockito.ArgumentMatchers.nullable; |
| 26 | +import static org.mockito.Mockito.doReturn; |
26 | 27 | import static org.mockito.Mockito.never; |
27 | 28 | import static org.mockito.Mockito.times; |
28 | 29 | import static org.mockito.Mockito.when; |
|
135 | 136 | import com.cloud.service.ServiceOfferingVO; |
136 | 137 | import com.cloud.service.dao.ServiceOfferingDao; |
137 | 138 | import com.cloud.storage.DiskOfferingVO; |
| 139 | +import com.cloud.storage.GuestOS; |
| 140 | +import com.cloud.storage.GuestOSVO; |
138 | 141 | import com.cloud.storage.VMTemplateVO; |
139 | 142 | import com.cloud.storage.dao.DiskOfferingDao; |
| 143 | +import com.cloud.storage.dao.GuestOSDao; |
140 | 144 | import com.cloud.template.VirtualMachineTemplate; |
141 | 145 | import com.cloud.user.Account; |
142 | 146 | import com.cloud.user.AccountManager; |
@@ -259,9 +263,10 @@ public class AutoScaleManagerImplTest { |
259 | 263 | LoadBalancingRulesService loadBalancingRulesService; |
260 | 264 | @Mock |
261 | 265 | VMInstanceDao vmInstanceDao; |
262 | | - |
263 | 266 | @Mock |
264 | 267 | VirtualMachineManager virtualMachineManager; |
| 268 | + @Mock |
| 269 | + GuestOSDao guestOSDao; |
265 | 270 |
|
266 | 271 | AccountVO account; |
267 | 272 | UserVO user; |
@@ -420,6 +425,11 @@ public void setUp() { |
420 | 425 | userDataDetails.put("0", new HashMap<>() {{ put("key1", "value1"); put("key2", "value2"); }}); |
421 | 426 | Mockito.doReturn(userDataFinal).when(userVmMgr).finalizeUserData(any(), any(), any()); |
422 | 427 | Mockito.doReturn(userDataFinal).when(userDataMgr).validateUserData(eq(userDataFinal), nullable(BaseCmd.HTTPMethod.class)); |
| 428 | + |
| 429 | + when(templateMock.getGuestOSId()).thenReturn(100L); |
| 430 | + GuestOSVO guestOSMock = Mockito.mock(GuestOSVO.class); |
| 431 | + when(guestOSDao.findById(anyLong())).thenReturn(guestOSMock); |
| 432 | + when(guestOSMock.getName()).thenReturn("linux"); |
423 | 433 | } |
424 | 434 |
|
425 | 435 | @After |
@@ -2495,4 +2505,53 @@ public void destroyVm() { |
2495 | 2505 |
|
2496 | 2506 | Mockito.verify(userVmMgr).expunge(eq(userVmMock)); |
2497 | 2507 | } |
| 2508 | + |
| 2509 | + @Test |
| 2510 | + public void getNextVmHostAndDisplayNameGeneratesCorrectHostAndDisplayNameForLinuxTemplate() { |
| 2511 | + when(asVmGroupMock.getName()).thenReturn(vmGroupName); |
| 2512 | + when(asVmGroupMock.getNextVmSeq()).thenReturn(1L); |
| 2513 | + Pair<String, String> result = autoScaleManagerImplSpy.getNextVmHostAndDisplayName(asVmGroupMock, templateMock); |
| 2514 | + String vmHostNamePattern = AutoScaleManagerImpl.VM_HOSTNAME_PREFIX + vmGroupName + |
| 2515 | + "-" + asVmGroupMock.getNextVmSeq() + "-[a-z]{6}"; |
| 2516 | + Assert.assertTrue(result.first().matches(vmHostNamePattern)); |
| 2517 | + Assert.assertEquals(result.first(), result.second()); |
| 2518 | + } |
| 2519 | + |
| 2520 | + @Test |
| 2521 | + public void getNextVmHostAndDisplayNameGeneratesCorrectHostAndDisplayNameForWindowsTemplate() { |
| 2522 | + GuestOSVO guestOS = Mockito.mock(GuestOSVO.class); |
| 2523 | + when(asVmGroupMock.getName()).thenReturn(vmGroupName); |
| 2524 | + when(asVmGroupMock.getNextVmSeq()).thenReturn(1L); |
| 2525 | + when(templateMock.getGuestOSId()).thenReturn(1L); |
| 2526 | + when(guestOS.getName()).thenReturn("Windows Server"); |
| 2527 | + when(guestOSDao.findById(1L)).thenReturn(guestOS); |
| 2528 | + Pair<String, String> result = autoScaleManagerImplSpy.getNextVmHostAndDisplayName(asVmGroupMock, templateMock); |
| 2529 | + String vmHostNamePattern = AutoScaleManagerImpl.VM_HOSTNAME_PREFIX + vmGroupName + |
| 2530 | + "-" + asVmGroupMock.getNextVmSeq() + "-[a-z]{6}"; |
| 2531 | + Assert.assertTrue(result.second().matches(vmHostNamePattern)); |
| 2532 | + Assert.assertEquals(15, result.first().length()); |
| 2533 | + Assert.assertTrue(result.second().endsWith(result.first())); |
| 2534 | + } |
| 2535 | + |
| 2536 | + @Test |
| 2537 | + public void getNextVmHostAndDisplayNameTruncatesGroupNameWhenExceedingMaxLength() { |
| 2538 | + when(asVmGroupMock.getName()).thenReturn(vmGroupNameWithMaxLength); |
| 2539 | + when(asVmGroupMock.getNextVmSeq()).thenReturn(1L); |
| 2540 | + Pair<String, String> result = autoScaleManagerImplSpy.getNextVmHostAndDisplayName(asVmGroupMock, templateMock); |
| 2541 | + Assert.assertTrue(result.first().length() <= 63); |
| 2542 | + Assert.assertTrue(result.second().length() <= 63); |
| 2543 | + } |
| 2544 | + |
| 2545 | + @Test |
| 2546 | + public void getNextVmHostAndDisplayNameHandlesNullGuestOS() { |
| 2547 | + when(asVmGroupMock.getName()).thenReturn(vmGroupName); |
| 2548 | + when(asVmGroupMock.getNextVmSeq()).thenReturn(1L); |
| 2549 | + when(templateMock.getGuestOSId()).thenReturn(1L); |
| 2550 | + when(guestOSDao.findById(1L)).thenReturn(null); |
| 2551 | + Pair<String, String> result = autoScaleManagerImplSpy.getNextVmHostAndDisplayName(asVmGroupMock, templateMock); |
| 2552 | + String vmHostNamePattern = AutoScaleManagerImpl.VM_HOSTNAME_PREFIX + vmGroupName + |
| 2553 | + "-" + asVmGroupMock.getNextVmSeq() + "-[a-z]{6}"; |
| 2554 | + Assert.assertTrue(result.first().matches(vmHostNamePattern)); |
| 2555 | + Assert.assertEquals(result.first(), result.second()); |
| 2556 | + } |
2498 | 2557 | } |
0 commit comments