|
| 1 | +package org.apache.cloudstack.service; |
| 2 | + |
| 3 | +import com.cloud.dc.DataCenter; |
| 4 | +import com.cloud.dc.DataCenterVO; |
| 5 | +import com.cloud.dc.dao.DataCenterDao; |
| 6 | +import com.cloud.deploy.DeployDestination; |
| 7 | +import com.cloud.deploy.DeploymentPlan; |
| 8 | +import com.cloud.network.Network; |
| 9 | +import com.cloud.network.NetworkModel; |
| 10 | +import com.cloud.network.Networks; |
| 11 | +import com.cloud.network.dao.PhysicalNetworkDao; |
| 12 | +import com.cloud.network.dao.PhysicalNetworkVO; |
| 13 | +import com.cloud.network.vpc.VpcVO; |
| 14 | +import com.cloud.network.vpc.dao.VpcDao; |
| 15 | +import com.cloud.offering.NetworkOffering; |
| 16 | +import com.cloud.offerings.dao.NetworkOfferingServiceMapDao; |
| 17 | +import com.cloud.user.Account; |
| 18 | +import com.cloud.user.AccountVO; |
| 19 | +import com.cloud.user.dao.AccountDao; |
| 20 | +import com.cloud.vm.ReservationContext; |
| 21 | +import org.apache.cloudstack.NsxAnswer; |
| 22 | +import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand; |
| 23 | +import org.apache.cloudstack.agent.api.NsxCommand; |
| 24 | +import org.junit.After; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | +import org.mockito.ArgumentMatchers; |
| 29 | +import org.mockito.Mock; |
| 30 | +import org.mockito.Mockito; |
| 31 | +import org.mockito.MockitoAnnotations; |
| 32 | +import org.mockito.junit.MockitoJUnitRunner; |
| 33 | +import org.springframework.test.util.ReflectionTestUtils; |
| 34 | + |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +import static org.junit.Assert.*; |
| 38 | +import static org.mockito.ArgumentMatchers.any; |
| 39 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 40 | +import static org.mockito.Mockito.*; |
| 41 | + |
| 42 | +@RunWith(MockitoJUnitRunner.class) |
| 43 | +public class NsxGuestNetworkGuruTest { |
| 44 | + |
| 45 | + @Mock |
| 46 | + PhysicalNetworkDao physicalNetworkDao; |
| 47 | + @Mock |
| 48 | + DataCenterDao dcDao; |
| 49 | + @Mock |
| 50 | + VpcDao vpcDao; |
| 51 | + @Mock |
| 52 | + NetworkOfferingServiceMapDao networkOfferingServiceMapDao; |
| 53 | + @Mock |
| 54 | + NsxControllerUtils nsxControllerUtils; |
| 55 | + @Mock |
| 56 | + DataCenterDao zoneDao; |
| 57 | + @Mock |
| 58 | + AccountDao accountDao; |
| 59 | + @Mock |
| 60 | + PhysicalNetworkVO physicalNetwork; |
| 61 | + @Mock |
| 62 | + DataCenterVO dataCenterVO; |
| 63 | + @Mock |
| 64 | + NetworkOffering offering; |
| 65 | + @Mock |
| 66 | + DeploymentPlan plan; |
| 67 | + @Mock |
| 68 | + Network network; |
| 69 | + @Mock |
| 70 | + Account account; |
| 71 | + @Mock |
| 72 | + VpcVO vpcVO; |
| 73 | + @Mock |
| 74 | + NetworkModel networkModel; |
| 75 | + |
| 76 | + NsxGuestNetworkGuru guru; |
| 77 | + AutoCloseable closeable; |
| 78 | + |
| 79 | + @Before |
| 80 | + public void setUp() { |
| 81 | + closeable = MockitoAnnotations.openMocks(this); |
| 82 | + guru = new NsxGuestNetworkGuru(); |
| 83 | + ReflectionTestUtils.setField(guru, "_physicalNetworkDao", physicalNetworkDao); |
| 84 | + ReflectionTestUtils.setField(guru, "_dcDao", dcDao); |
| 85 | + ReflectionTestUtils.setField(guru, "_networkModel", networkModel); |
| 86 | + ReflectionTestUtils.setField(guru, "_vpcDao", vpcDao); |
| 87 | + |
| 88 | + guru.networkOfferingServiceMapDao = networkOfferingServiceMapDao; |
| 89 | + guru.nsxControllerUtils = nsxControllerUtils; |
| 90 | + guru.accountDao = accountDao; |
| 91 | + |
| 92 | + Mockito.when(dataCenterVO.getNetworkType()).thenReturn(DataCenter.NetworkType.Advanced); |
| 93 | + |
| 94 | + when(physicalNetwork.getIsolationMethods()).thenReturn(List.of("NSX")); |
| 95 | + |
| 96 | + when(offering.getTrafficType()).thenReturn(Networks.TrafficType.Guest); |
| 97 | + when(offering.getGuestType()).thenReturn(Network.GuestType.Isolated); |
| 98 | + when(offering.getId()).thenReturn(1L); |
| 99 | + |
| 100 | + when(plan.getDataCenterId()).thenReturn(1L); |
| 101 | + when(plan.getPhysicalNetworkId()).thenReturn(1L); |
| 102 | + |
| 103 | + when(vpcDao.findById(anyLong())).thenReturn(vpcVO); |
| 104 | + |
| 105 | + when(vpcVO.getName()).thenReturn("VPC01"); |
| 106 | + |
| 107 | + when(account.getAccountId()).thenReturn(1L); |
| 108 | + when(accountDao.findById(anyLong())).thenReturn(mock(AccountVO.class)); |
| 109 | + |
| 110 | + Mockito.when(networkOfferingServiceMapDao.isProviderForNetworkOffering(offering.getId(), Network.Provider.Nsx)).thenReturn( |
| 111 | + true); |
| 112 | + } |
| 113 | + |
| 114 | + @After |
| 115 | + public void tearDown() throws Exception { |
| 116 | + closeable.close(); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testIsMyIsolationMethod() { |
| 121 | + assertTrue(guru.isMyIsolationMethod(physicalNetwork)); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testCanHandles() { |
| 126 | + assertTrue(guru.canHandle(offering, dataCenterVO.getNetworkType(), physicalNetwork)); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testNsxNetworkDesign() { |
| 131 | + when(physicalNetworkDao.findById(ArgumentMatchers.anyLong())).thenReturn(physicalNetwork); |
| 132 | + when(dcDao.findById(ArgumentMatchers.anyLong())).thenReturn(dataCenterVO); |
| 133 | + when(nsxControllerUtils.sendNsxCommand(any(CreateNsxSegmentCommand.class), anyLong())).thenReturn( |
| 134 | + new NsxAnswer(new NsxCommand(nullable(String.class), nullable(Long.class), nullable(String.class), nullable(Long.class)), true, "")); |
| 135 | + |
| 136 | + Network designedNetwork = guru.design(offering, plan, network, "", 1L, account); |
| 137 | + verify(nsxControllerUtils, times(1)).sendNsxCommand(any(CreateNsxSegmentCommand.class), anyLong()); |
| 138 | + assertNotNull(designedNetwork); |
| 139 | + assertSame(Networks.BroadcastDomainType.NSX, designedNetwork.getBroadcastDomainType()); |
| 140 | + assertSame(Network.State.Allocated, designedNetwork.getState()); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testNsxNetworkImplementation() { |
| 145 | + final DeployDestination deployDestination = mock(DeployDestination.class); |
| 146 | + final ReservationContext reservationContext = mock(ReservationContext.class); |
| 147 | + |
| 148 | + when(network.getTrafficType()).thenReturn(Networks.TrafficType.Guest); |
| 149 | + when(network.getMode()).thenReturn(Networks.Mode.Dhcp); |
| 150 | + when(network.getGateway()).thenReturn("192.168.1.1"); |
| 151 | + when(network.getCidr()).thenReturn("192.168.1.0/24"); |
| 152 | + when(network.getBroadcastDomainType()).thenReturn(Networks.BroadcastDomainType.NSX); |
| 153 | + when(network.getNetworkOfferingId()).thenReturn(1L); |
| 154 | + when(network.getState()).thenReturn(Network.State.Implementing); |
| 155 | + when(network.getDataCenterId()).thenReturn(2L); |
| 156 | + when(network.getPhysicalNetworkId()).thenReturn(3L); |
| 157 | + when(network.getVpcId()).thenReturn(4L); |
| 158 | + when(offering.isRedundantRouter()).thenReturn(false); |
| 159 | + when(offering.getGuestType()).thenReturn(Network.GuestType.Isolated); |
| 160 | + |
| 161 | + |
| 162 | + final Network implemented = guru.implement(network, offering, deployDestination, reservationContext); |
| 163 | + assertEquals(Networks.BroadcastDomainType.NSX.toUri("nsx"), implemented.getBroadcastUri()); |
| 164 | + assertEquals("192.168.1.1", implemented.getGateway()); |
| 165 | + assertEquals("192.168.1.0/24", implemented.getCidr()); |
| 166 | + assertEquals(Networks.Mode.Dhcp, implemented.getMode()); |
| 167 | + assertEquals(Networks.BroadcastDomainType.NSX, implemented.getBroadcastDomainType()); |
| 168 | + assertEquals(1L, implemented.getNetworkOfferingId()); |
| 169 | + assertEquals(Network.State.Implemented, implemented.getState()); |
| 170 | + assertEquals(2L, implemented.getDataCenterId()); |
| 171 | + assertEquals(3L, implemented.getPhysicalNetworkId().longValue()); |
| 172 | + assertEquals(4L, implemented.getVpcId().longValue()); |
| 173 | + assertFalse(implemented.isRedundant()); |
| 174 | + } |
| 175 | +} |
0 commit comments