|
64 | 64 | import org.mockito.junit.MockitoJUnitRunner; |
65 | 65 |
|
66 | 66 | import java.lang.reflect.Field; |
| 67 | +import java.lang.reflect.Method; |
67 | 68 | import java.util.ArrayList; |
68 | 69 | import java.util.Arrays; |
69 | 70 | import java.util.Collections; |
| 71 | +import java.util.Date; |
70 | 72 | import java.util.List; |
71 | 73 | import java.util.UUID; |
72 | 74 |
|
@@ -683,4 +685,49 @@ public void testDestroyLocalStoragePoolVolumesNoDisks() { |
683 | 685 | resourceManager.destroyLocalStoragePoolVolumes(poolId); |
684 | 686 | verify(volumeDao, never()).updateAndRemoveVolume(any(VolumeVO.class)); |
685 | 687 | } |
| 688 | + |
| 689 | + private HostVO invokeGetNewHost(StartupCommand[] cmds) throws Exception { |
| 690 | + Method method = ResourceManagerImpl.class.getDeclaredMethod("getNewHost", StartupCommand[].class); |
| 691 | + method.setAccessible(true); |
| 692 | + return (HostVO) method.invoke(resourceManager, (Object) cmds); |
| 693 | + } |
| 694 | + |
| 695 | + @Test |
| 696 | + public void testGetNewHostRevivesRemovedHostOnGuidCollision() throws Exception { |
| 697 | + String guid = "s-14-VM-PremiumSecondaryStorageResource"; |
| 698 | + String guidPrefix = "s-14-VM"; |
| 699 | + StartupCommand cmd = Mockito.mock(StartupCommand.class); |
| 700 | + when(cmd.getGuid()).thenReturn(guid); |
| 701 | + when(cmd.getGuidWithoutResource()).thenReturn(guidPrefix); |
| 702 | + |
| 703 | + Mockito.doReturn(null).when(resourceManager).findHostByGuid(guid); |
| 704 | + Mockito.doReturn(null).when(resourceManager).findHostByGuidPrefix(guidPrefix); |
| 705 | + |
| 706 | + HostVO removedHost = Mockito.mock(HostVO.class); |
| 707 | + when(removedHost.getId()).thenReturn(99L); |
| 708 | + when(removedHost.getRemoved()).thenReturn(new Date()); |
| 709 | + when(hostDao.findByGuidIncludingRemoved(guid)).thenReturn(removedHost); |
| 710 | + |
| 711 | + HostVO result = invokeGetNewHost(new StartupCommand[]{cmd}); |
| 712 | + |
| 713 | + Assert.assertSame(removedHost, result); |
| 714 | + Mockito.verify(removedHost).setRemoved(null); |
| 715 | + } |
| 716 | + |
| 717 | + @Test |
| 718 | + public void testGetNewHostReturnsNullWhenNoHostAndNoRemovedMatch() throws Exception { |
| 719 | + String guid = "unknown-guid"; |
| 720 | + String guidPrefix = "unknown"; |
| 721 | + StartupCommand cmd = Mockito.mock(StartupCommand.class); |
| 722 | + when(cmd.getGuid()).thenReturn(guid); |
| 723 | + when(cmd.getGuidWithoutResource()).thenReturn(guidPrefix); |
| 724 | + |
| 725 | + Mockito.doReturn(null).when(resourceManager).findHostByGuid(guid); |
| 726 | + Mockito.doReturn(null).when(resourceManager).findHostByGuidPrefix(guidPrefix); |
| 727 | + when(hostDao.findByGuidIncludingRemoved(guid)).thenReturn(null); |
| 728 | + |
| 729 | + HostVO result = invokeGetNewHost(new StartupCommand[]{cmd}); |
| 730 | + |
| 731 | + Assert.assertNull(result); |
| 732 | + } |
686 | 733 | } |
0 commit comments