Skip to content

Commit 867653e

Browse files
committed
add more unit tests
1 parent f511706 commit 867653e

File tree

5 files changed

+130
-28
lines changed

5 files changed

+130
-28
lines changed

engine/schema/src/main/java/com/cloud/network/vpc/VpcVO.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ public long getId() {
139139
return id;
140140
}
141141

142-
public void setId(long id) {
143-
this.id = id;
144-
}
145-
146142
@Override
147143
public String getUuid() {
148144
return uuid;
@@ -168,19 +164,11 @@ public long getDomainId() {
168164
return domainId;
169165
}
170166

171-
public void setDomainId(long domainId) {
172-
this.domainId = domainId;
173-
}
174-
175167
@Override
176168
public long getAccountId() {
177169
return accountId;
178170
}
179171

180-
public void setAccountId(long accountId) {
181-
this.accountId = accountId;
182-
}
183-
184172
@Override
185173
public State getState() {
186174
return state;

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxGuestNetworkGuru.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ public class NsxGuestNetworkGuru extends GuestNetworkGuru implements NetworkMigr
7676
DomainDao domainDao;
7777
@Inject
7878
NetworkModel networkModel;
79-
@Inject
80-
NetworkOfferingDao networkOfferingDao;
8179

8280
public NsxGuestNetworkGuru() {
8381
super();
@@ -297,7 +295,7 @@ public void commitMigration(NicProfile nic, Network network, VirtualMachineProfi
297295
// Do nothing
298296
}
299297

300-
private void createNsxSegment(NetworkVO networkVO, DataCenter zone) {
298+
public void createNsxSegment(NetworkVO networkVO, DataCenter zone) {
301299
Account account = accountDao.findById(networkVO.getAccountId());
302300
if (isNull(account)) {
303301
throw new CloudRuntimeException(String.format("Unable to find account with id: %s", networkVO.getAccountId()));

plugins/network-elements/nsx/src/test/java/org/apache/cloudstack/service/NsxElementTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,14 @@ public void testApplyStaticNatRules() throws ResourceUnavailableException {
230230
2L, 5L, 6L, false, false);
231231

232232
NicVO nic = Mockito.mock(NicVO.class);
233-
VpcVO vpc = new VpcVO();
234-
vpc.setId(1L);
235-
vpc.setName("vpc1");
233+
VpcVO vpc = Mockito.mock(VpcVO.class);
236234

237235
when(ipAddressDao.findByIdIncludingRemoved(anyLong())).thenReturn(ipAddress);
238236
when(vmInstanceDao.findByIdIncludingRemoved(anyLong())).thenReturn(vm);
239237
when(networkModel.getNicInNetworkIncludingRemoved(anyLong(), anyLong())).thenReturn(nic);
240238
when(vpcDao.findById(anyLong())).thenReturn(vpc);
239+
when(vpc.getId()).thenReturn(1L);
240+
when(vpc.getName()).thenReturn("vpc1");
241241
when(nsxService.createStaticNatRule(anyLong(), anyLong(), anyLong(), anyLong(), anyString(), anyBoolean(), anyLong(), anyString(), anyString())).thenReturn(true);
242242

243243
assertTrue(nsxElement.applyStaticNats(networkVO, List.of(rule)));
@@ -367,14 +367,12 @@ public void testApplyLBRules() throws ResourceUnavailableException {
367367
LoadBalancingRule.LbDestination destination = new LoadBalancingRule.LbDestination(6443, 6443, "172.30.110.11", false);
368368
LoadBalancingRule rule = new LoadBalancingRule(lb, List.of(destination), null, null, new Ip("10.1.13.10"));
369369

370-
LoadBalancerVMMapVO lbVmMap = new LoadBalancerVMMapVO(1L, 21L);
371-
372-
VpcVO vpc = new VpcVO();
373-
vpc.setDomainId(2L);
374-
vpc.setAccountId(5L);
370+
VpcVO vpc = Mockito.mock(VpcVO.class);
375371

376372
IPAddressVO ipAddress = new IPAddressVO(new Ip("10.1.13.10"), 1L, 1L, 1L,false);
377373
when(vpcDao.findById(anyLong())).thenReturn(vpc);
374+
when(vpc.getDomainId()).thenReturn(2L);
375+
when(vpc.getAccountId()).thenReturn(5L);
378376
when(ipAddressDao.findByIpAndDcId(anyLong(), anyString())).thenReturn(ipAddress);
379377

380378
assertTrue(nsxElement.applyLBRules(networkVO, List.of(rule)));

plugins/network-elements/nsx/src/test/java/org/apache/cloudstack/service/NsxGuestNetworkGuruTest.java

Lines changed: 122 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,45 @@
1616
// under the License.
1717
package org.apache.cloudstack.service;
1818

19+
import com.cloud.api.ApiDBUtils;
1920
import com.cloud.dc.DataCenter;
2021
import com.cloud.dc.DataCenterVO;
2122
import com.cloud.dc.dao.DataCenterDao;
2223
import com.cloud.deploy.DeployDestination;
2324
import com.cloud.deploy.DeploymentPlan;
2425
import com.cloud.domain.DomainVO;
2526
import com.cloud.domain.dao.DomainDao;
27+
import com.cloud.exception.InsufficientAddressCapacityException;
28+
import com.cloud.exception.InsufficientVirtualNetworkCapacityException;
29+
import com.cloud.network.IpAddressManager;
2630
import com.cloud.network.Network;
2731
import com.cloud.network.NetworkModel;
2832
import com.cloud.network.Networks;
33+
import com.cloud.network.PhysicalNetwork;
2934
import com.cloud.network.dao.NetworkDao;
3035
import com.cloud.network.dao.NetworkVO;
3136
import com.cloud.network.dao.PhysicalNetworkDao;
3237
import com.cloud.network.dao.PhysicalNetworkVO;
38+
import com.cloud.network.guru.GuestNetworkGuru;
39+
import com.cloud.network.vpc.NetworkACLItem;
3340
import com.cloud.network.vpc.VpcVO;
3441
import com.cloud.network.vpc.dao.VpcDao;
3542
import com.cloud.offering.NetworkOffering;
43+
import com.cloud.offerings.dao.NetworkOfferingDao;
3644
import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
3745
import com.cloud.user.Account;
3846
import com.cloud.user.AccountVO;
3947
import com.cloud.user.dao.AccountDao;
48+
import com.cloud.utils.Pair;
49+
import com.cloud.vm.Nic;
50+
import com.cloud.vm.NicProfile;
4051
import com.cloud.vm.ReservationContext;
52+
import com.cloud.vm.VirtualMachine;
53+
import com.cloud.vm.VirtualMachineProfile;
4154
import org.apache.cloudstack.NsxAnswer;
55+
import org.apache.cloudstack.agent.api.CreateNsxDhcpRelayConfigCommand;
4256
import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand;
57+
import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand;
4358
import org.apache.cloudstack.agent.api.NsxCommand;
4459
import org.apache.cloudstack.utils.NsxControllerUtils;
4560
import org.junit.After;
@@ -53,15 +68,17 @@
5368
import org.mockito.junit.MockitoJUnitRunner;
5469
import org.springframework.test.util.ReflectionTestUtils;
5570

71+
import java.lang.reflect.Field;
72+
import java.lang.reflect.InvocationTargetException;
73+
import java.lang.reflect.Method;
5674
import java.util.List;
5775

5876
import static org.junit.Assert.assertTrue;
5977
import static org.junit.Assert.assertEquals;
6078
import static org.junit.Assert.assertSame;
6179
import static org.junit.Assert.assertFalse;
6280
import static org.junit.Assert.assertNotNull;
63-
import static org.mockito.ArgumentMatchers.any;
64-
import static org.mockito.ArgumentMatchers.anyLong;
81+
import static org.mockito.ArgumentMatchers.*;
6582
import static org.mockito.Mockito.when;
6683
import static org.mockito.Mockito.verify;
6784
import static org.mockito.Mockito.mock;
@@ -103,19 +120,27 @@ public class NsxGuestNetworkGuruTest {
103120
DomainDao domainDao;
104121
@Mock
105122
NetworkDao networkDao;
123+
@Mock
124+
IpAddressManager ipAddressManager;
125+
@Mock
126+
NetworkOfferingDao networkOfferingDao;
106127

107128
NsxGuestNetworkGuru guru;
129+
GuestNetworkGuru guestNetworkGuru;
108130
AutoCloseable closeable;
109131

110132
@Before
111-
public void setUp() {
133+
public void setUp() throws IllegalAccessException, NoSuchFieldException {
112134
closeable = MockitoAnnotations.openMocks(this);
113135
guru = new NsxGuestNetworkGuru();
114-
ReflectionTestUtils.setField(guru, "_physicalNetworkDao", physicalNetworkDao);
136+
115137
ReflectionTestUtils.setField(guru, "_dcDao", dcDao);
116138
ReflectionTestUtils.setField(guru, "_networkDao", networkDao);
117139
ReflectionTestUtils.setField(guru, "_networkModel", networkModel);
118140
ReflectionTestUtils.setField(guru, "_vpcDao", vpcDao);
141+
ReflectionTestUtils.setField((GuestNetworkGuru) guru, "_ipAddrMgr", ipAddressManager);
142+
ReflectionTestUtils.setField((GuestNetworkGuru) guru, "_networkModel", networkModel);
143+
ReflectionTestUtils.setField((GuestNetworkGuru) guru, "networkOfferingDao", networkOfferingDao);
119144

120145
guru.networkOfferingServiceMapDao = networkOfferingServiceMapDao;
121146
guru.nsxControllerUtils = nsxControllerUtils;
@@ -215,4 +240,97 @@ public void testNsxNetworkImplementation() {
215240
assertEquals(4L, implemented.getVpcId().longValue());
216241
assertFalse(implemented.isRedundant());
217242
}
243+
244+
@Test
245+
public void testAllocateForUserVM() throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
246+
Network network = Mockito.mock(Network.class);
247+
NicProfile nicProfile = Mockito.mock(NicProfile.class);
248+
VirtualMachineProfile vmProfile = Mockito.mock(VirtualMachineProfile.class);
249+
VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class);
250+
Pair<String, String> dns = new Pair<>("10.1.5.1", "8.8.8.8");
251+
String macAddress = "00:00:00:11:1D:1E:CD";
252+
253+
when(network.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
254+
when(vmProfile.getVirtualMachine()).thenReturn(virtualMachine);
255+
when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.User);
256+
when(network.getId()).thenReturn(2L);
257+
when(offering.getId()).thenReturn(11L);
258+
when(networkModel.getNetworkIp4Dns(any(Network.class), nullable(DataCenter.class))).thenReturn(dns);
259+
when(networkModel.getNextAvailableMacAddressInNetwork(anyLong())).thenReturn(macAddress);
260+
when(nicProfile.getMacAddress()).thenReturn(macAddress);
261+
when(networkOfferingDao.isIpv6Supported(anyLong())).thenReturn(false);
262+
263+
NicProfile profile = guru.allocate(network, nicProfile, vmProfile);
264+
assertNotNull(profile);
265+
}
266+
267+
@Test
268+
public void testAllocateForDomainRouter() throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
269+
Network network = Mockito.mock(Network.class);
270+
NicProfile nicProfile = Mockito.mock(NicProfile.class);
271+
VirtualMachineProfile vmProfile = Mockito.mock(VirtualMachineProfile.class);
272+
VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class);
273+
Pair<String, String> dns = new Pair<>("10.1.5.1", "8.8.8.8");
274+
String macAddress = "00:00:00:11:1D:1E:CD";
275+
276+
when(network.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
277+
when(vmProfile.getType()).thenReturn(VirtualMachine.Type.DomainRouter);
278+
when(vmProfile.getVirtualMachine()).thenReturn(virtualMachine);
279+
when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.DomainRouter);
280+
when(network.getId()).thenReturn(2L);
281+
when(offering.getId()).thenReturn(11L);
282+
when(networkModel.getNetworkIp4Dns(any(Network.class), nullable(DataCenter.class))).thenReturn(dns);
283+
when(networkModel.getNextAvailableMacAddressInNetwork(anyLong())).thenReturn(macAddress);
284+
when(nicProfile.getMacAddress()).thenReturn(macAddress);
285+
when(networkOfferingDao.isIpv6Supported(anyLong())).thenReturn(false);
286+
when(network.getDataCenterId()).thenReturn(1L);
287+
when(network.getAccountId()).thenReturn(5L);
288+
when(network.getVpcId()).thenReturn(51L);
289+
when(account.getDomainId()).thenReturn(2L);
290+
when(dcDao.findById(anyLong())).thenReturn(Mockito.mock(DataCenterVO.class));
291+
when(accountDao.findById(anyLong())).thenReturn(Mockito.mock(AccountVO.class));
292+
when(vpcDao.findById(anyLong())).thenReturn(Mockito.mock(VpcVO.class));
293+
when(domainDao.findById(anyLong())).thenReturn(Mockito.mock(DomainVO.class));
294+
when(nicProfile.getIPv4Address()).thenReturn("10.1.13.10");
295+
when(nsxControllerUtils.sendNsxCommand(any(CreateNsxDhcpRelayConfigCommand.class),
296+
anyLong())).thenReturn(new NsxAnswer(new NsxCommand(), true, ""));
297+
298+
NicProfile profile = guru.allocate(network, nicProfile, vmProfile);
299+
300+
assertNotNull(profile);
301+
verify(nsxControllerUtils, times(1)).sendNsxCommand(any(CreateNsxDhcpRelayConfigCommand.class),
302+
anyLong());
303+
}
304+
305+
@Test
306+
public void testCreateNsxSegmentForVpc() {
307+
NetworkVO networkVO = Mockito.mock(NetworkVO.class);
308+
DataCenter dataCenter = Mockito.mock(DataCenter.class);
309+
310+
when(networkVO.getAccountId()).thenReturn(1L);
311+
when(nsxControllerUtils.sendNsxCommand(any(CreateNsxSegmentCommand.class),
312+
anyLong())).thenReturn(new NsxAnswer(new NsxCommand(), true, ""));
313+
guru.createNsxSegment(networkVO, dataCenter);
314+
verify(nsxControllerUtils, times(1)).sendNsxCommand(any(CreateNsxSegmentCommand.class),
315+
anyLong());
316+
}
317+
318+
319+
@Test
320+
public void testCreateNsxSegmentForIsolatedNetwork() {
321+
NetworkVO networkVO = Mockito.mock(NetworkVO.class);
322+
DataCenter dataCenter = Mockito.mock(DataCenter.class);
323+
324+
when(networkVO.getAccountId()).thenReturn(1L);
325+
when(networkVO.getVpcId()).thenReturn(null);
326+
when(nsxControllerUtils.sendNsxCommand(any(CreateNsxTier1GatewayCommand.class),
327+
anyLong())).thenReturn(new NsxAnswer(new NsxCommand(), true, ""));
328+
when(nsxControllerUtils.sendNsxCommand(any(CreateNsxSegmentCommand.class),
329+
anyLong())).thenReturn(new NsxAnswer(new NsxCommand(), true, ""));
330+
guru.createNsxSegment(networkVO, dataCenter);
331+
verify(nsxControllerUtils, times(1)).sendNsxCommand(any(CreateNsxTier1GatewayCommand.class),
332+
anyLong());
333+
verify(nsxControllerUtils, times(1)).sendNsxCommand(any(CreateNsxSegmentCommand.class),
334+
anyLong());
335+
}
218336
}

server/src/main/java/com/cloud/network/guru/GuestNetworkGuru.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public abstract class GuestNetworkGuru extends AdapterBase implements NetworkGur
118118
@Inject
119119
protected IpAddressManager _ipAddrMgr;
120120
@Inject
121-
NetworkOfferingDao networkOfferingDao;
121+
protected NetworkOfferingDao networkOfferingDao;
122122
@Inject
123123
Ipv6AddressManager ipv6AddressManager;
124124
@Inject

0 commit comments

Comments
 (0)