Skip to content

Commit 6562fac

Browse files
committed
add more unit tests
1 parent f511706 commit 6562fac

File tree

5 files changed

+123
-29
lines changed

5 files changed

+123
-29
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 & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import com.cloud.network.dao.IPAddressDao;
3232
import com.cloud.network.dao.IPAddressVO;
3333
import com.cloud.network.dao.LoadBalancerVMMapDao;
34-
import com.cloud.network.dao.LoadBalancerVMMapVO;
3534
import com.cloud.network.dao.LoadBalancerVO;
3635
import com.cloud.network.dao.NetworkDao;
3736
import com.cloud.network.dao.NetworkVO;
@@ -230,14 +229,14 @@ public void testApplyStaticNatRules() throws ResourceUnavailableException {
230229
2L, 5L, 6L, false, false);
231230

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

237234
when(ipAddressDao.findByIdIncludingRemoved(anyLong())).thenReturn(ipAddress);
238235
when(vmInstanceDao.findByIdIncludingRemoved(anyLong())).thenReturn(vm);
239236
when(networkModel.getNicInNetworkIncludingRemoved(anyLong(), anyLong())).thenReturn(nic);
240237
when(vpcDao.findById(anyLong())).thenReturn(vpc);
238+
when(vpc.getId()).thenReturn(1L);
239+
when(vpc.getName()).thenReturn("vpc1");
241240
when(nsxService.createStaticNatRule(anyLong(), anyLong(), anyLong(), anyLong(), anyString(), anyBoolean(), anyLong(), anyString(), anyString())).thenReturn(true);
242241

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

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

376371
IPAddressVO ipAddress = new IPAddressVO(new Ip("10.1.13.10"), 1L, 1L, 1L,false);
377372
when(vpcDao.findById(anyLong())).thenReturn(vpc);
373+
when(vpc.getDomainId()).thenReturn(2L);
374+
when(vpc.getAccountId()).thenReturn(5L);
378375
when(ipAddressDao.findByIpAndDcId(anyLong(), anyString())).thenReturn(ipAddress);
379376

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

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

Lines changed: 115 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,34 @@
2323
import com.cloud.deploy.DeploymentPlan;
2424
import com.cloud.domain.DomainVO;
2525
import com.cloud.domain.dao.DomainDao;
26+
import com.cloud.exception.InsufficientAddressCapacityException;
27+
import com.cloud.exception.InsufficientVirtualNetworkCapacityException;
28+
import com.cloud.network.IpAddressManager;
2629
import com.cloud.network.Network;
2730
import com.cloud.network.NetworkModel;
2831
import com.cloud.network.Networks;
2932
import com.cloud.network.dao.NetworkDao;
3033
import com.cloud.network.dao.NetworkVO;
3134
import com.cloud.network.dao.PhysicalNetworkDao;
3235
import com.cloud.network.dao.PhysicalNetworkVO;
36+
import com.cloud.network.guru.GuestNetworkGuru;
3337
import com.cloud.network.vpc.VpcVO;
3438
import com.cloud.network.vpc.dao.VpcDao;
3539
import com.cloud.offering.NetworkOffering;
40+
import com.cloud.offerings.dao.NetworkOfferingDao;
3641
import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
3742
import com.cloud.user.Account;
3843
import com.cloud.user.AccountVO;
3944
import com.cloud.user.dao.AccountDao;
45+
import com.cloud.utils.Pair;
46+
import com.cloud.vm.NicProfile;
4047
import com.cloud.vm.ReservationContext;
48+
import com.cloud.vm.VirtualMachine;
49+
import com.cloud.vm.VirtualMachineProfile;
4150
import org.apache.cloudstack.NsxAnswer;
51+
import org.apache.cloudstack.agent.api.CreateNsxDhcpRelayConfigCommand;
4252
import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand;
53+
import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand;
4354
import org.apache.cloudstack.agent.api.NsxCommand;
4455
import org.apache.cloudstack.utils.NsxControllerUtils;
4556
import org.junit.After;
@@ -60,8 +71,7 @@
6071
import static org.junit.Assert.assertSame;
6172
import static org.junit.Assert.assertFalse;
6273
import static org.junit.Assert.assertNotNull;
63-
import static org.mockito.ArgumentMatchers.any;
64-
import static org.mockito.ArgumentMatchers.anyLong;
74+
import static org.mockito.ArgumentMatchers.*;
6575
import static org.mockito.Mockito.when;
6676
import static org.mockito.Mockito.verify;
6777
import static org.mockito.Mockito.mock;
@@ -103,19 +113,27 @@ public class NsxGuestNetworkGuruTest {
103113
DomainDao domainDao;
104114
@Mock
105115
NetworkDao networkDao;
116+
@Mock
117+
IpAddressManager ipAddressManager;
118+
@Mock
119+
NetworkOfferingDao networkOfferingDao;
106120

107121
NsxGuestNetworkGuru guru;
122+
GuestNetworkGuru guestNetworkGuru;
108123
AutoCloseable closeable;
109124

110125
@Before
111-
public void setUp() {
126+
public void setUp() throws IllegalAccessException, NoSuchFieldException {
112127
closeable = MockitoAnnotations.openMocks(this);
113128
guru = new NsxGuestNetworkGuru();
114-
ReflectionTestUtils.setField(guru, "_physicalNetworkDao", physicalNetworkDao);
129+
115130
ReflectionTestUtils.setField(guru, "_dcDao", dcDao);
116131
ReflectionTestUtils.setField(guru, "_networkDao", networkDao);
117132
ReflectionTestUtils.setField(guru, "_networkModel", networkModel);
118133
ReflectionTestUtils.setField(guru, "_vpcDao", vpcDao);
134+
ReflectionTestUtils.setField((GuestNetworkGuru) guru, "_ipAddrMgr", ipAddressManager);
135+
ReflectionTestUtils.setField((GuestNetworkGuru) guru, "_networkModel", networkModel);
136+
ReflectionTestUtils.setField((GuestNetworkGuru) guru, "networkOfferingDao", networkOfferingDao);
119137

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

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)