Skip to content

Commit 8337486

Browse files
Pearl1594nvazquez
andauthored
Nsx unit tests (#8090)
* Add tests * add test for NsxGuestNetworkGuru * add unit tests for NsxResource * add unti tests for NsxElement * cleanup * [NSX] Refactor API wrapper operations * update tests * update tests - add nsxProviderServiceImpl test * add unit test - NsxServiceImpl * add license * Big refactor * Address review comment * change network cidr to cidr to prevent NPE * add domain and zone names to the various networks - vpc & tier * fix tests --------- Co-authored-by: nvazquez <[email protected]>
1 parent 3f8f9e2 commit 8337486

File tree

14 files changed

+869
-25
lines changed

14 files changed

+869
-25
lines changed

engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,7 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil
14741474
}
14751475
}
14761476

1477-
private void setVmNetworkDetails(VMInstanceVO vm, VirtualMachineTO vmTO) {
1477+
public void setVmNetworkDetails(VMInstanceVO vm, VirtualMachineTO vmTO) {
14781478
if (VirtualMachine.Type.User.equals(vm.getType())) {
14791479
List<UserVmJoinVO> userVmJoinVOs = userVmJoinDao.searchByIds(vm.getId());
14801480
Map<Long, String> networkToNetworkNameMap = new HashMap<>();

engine/orchestration/src/test/java/com/cloud/vm/VirtualMachineManagerImplTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@
3535
import java.util.Random;
3636
import java.util.stream.Collectors;
3737

38+
import com.cloud.agent.api.to.VirtualMachineTO;
39+
import com.cloud.api.query.vo.UserVmJoinVO;
40+
import com.cloud.dc.DataCenterVO;
41+
import com.cloud.dc.dao.DataCenterDao;
42+
import com.cloud.domain.DomainVO;
43+
import com.cloud.domain.dao.DomainDao;
44+
import com.cloud.network.dao.NetworkDao;
45+
import com.cloud.network.dao.NetworkVO;
46+
import com.cloud.network.vpc.VpcVO;
47+
import com.cloud.network.vpc.dao.VpcDao;
48+
import com.cloud.user.AccountVO;
49+
import com.cloud.user.dao.AccountDao;
3850
import org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator;
3951
import org.apache.cloudstack.framework.config.ConfigKey;
4052
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
@@ -155,6 +167,16 @@ public class VirtualMachineManagerImplTest {
155167
private UserVmDao userVmDaoMock;
156168
@Mock
157169
private UserVmVO userVmMock;
170+
@Mock
171+
private NetworkDao networkDao;
172+
@Mock
173+
private AccountDao accountDao;
174+
@Mock
175+
private DomainDao domainDao;
176+
@Mock
177+
private DataCenterDao dcDao;
178+
@Mock
179+
private VpcDao vpcDao;
158180

159181
@Before
160182
public void setup() {
@@ -895,4 +917,41 @@ public void checkAndAttemptMigrateVmAcrossClusterNonValid() {
895917
map.put(Mockito.mock(Volume.class), pool2);
896918
virtualMachineManagerImpl.checkAndAttemptMigrateVmAcrossCluster(vm, destinationClusterId, map);
897919
}
920+
921+
@Test
922+
public void checkIfVmNetworkDetailsReturnedIsCorrect() {
923+
VMInstanceVO vm = new VMInstanceVO(1L, 1L, "VM1", "i-2-2-VM",
924+
VirtualMachine.Type.User, 1L, HypervisorType.KVM, 1L, 1L, 1L,
925+
1L, false, false);
926+
927+
VirtualMachineTO vmTO = new VirtualMachineTO() {};
928+
UserVmJoinVO userVm = new UserVmJoinVO();
929+
NetworkVO networkVO = new NetworkVO();
930+
AccountVO accountVO = new AccountVO();
931+
DomainVO domainVO = new DomainVO();
932+
domainVO.setName("testDomain");
933+
DataCenterVO dataCenterVO = mock(DataCenterVO.class);
934+
VpcVO vpcVO = new VpcVO();
935+
936+
networkVO.setAccountId(1L);
937+
networkVO.setName("testNet");
938+
networkVO.setVpcId(1L);
939+
940+
accountVO.setAccountName("testAcc");
941+
942+
vpcVO.setName("VPC1");
943+
944+
945+
List<UserVmJoinVO> userVms = List.of(userVm);
946+
Mockito.when(userVmJoinDaoMock.searchByIds(anyLong())).thenReturn(userVms);
947+
Mockito.when(networkDao.findById(anyLong())).thenReturn(networkVO);
948+
Mockito.when(accountDao.findById(anyLong())).thenReturn(accountVO);
949+
Mockito.when(domainDao.findById(anyLong())).thenReturn(domainVO);
950+
Mockito.when(dcDao.findById(anyLong())).thenReturn(dataCenterVO);
951+
Mockito.when(vpcDao.findById(anyLong())).thenReturn(vpcVO);
952+
Mockito.when(dataCenterVO.getName()).thenReturn("testZone");
953+
virtualMachineManagerImpl.setVmNetworkDetails(vm, vmTO);
954+
assertEquals(vmTO.getNetworkIdToNetworkNameMap().size(), 1);
955+
assertEquals(vmTO.getNetworkIdToNetworkNameMap().get(0L), "testDomain-testAcc-testZone-VPC1-testNet");
956+
}
898957
}

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/agent/api/NsxCommand.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public class NsxCommand extends Command {
2525
private String accountName;
2626
private String domainName;
2727

28+
public NsxCommand() {
29+
}
30+
2831
public NsxCommand(String domainName, String accountName, String zoneName) {
2932
this.zoneName = zoneName;
3033
this.accountName = accountName;

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/resource/NsxResource.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public class NsxResource implements ServerResource {
7171
public Host.Type getType() {
7272
return Host.Type.Routing;
7373
}
74-
7574
@Override
7675
public StartupCommand[] initialize() {
7776
StartupNsxCommand sc = new StartupNsxCommand();

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public class NsxElement extends AdapterBase implements DhcpServiceProvider, DnsS
100100
@Inject
101101
NetworkModel networkModel;
102102
@Inject
103-
private DomainDao domainDao;
103+
DomainDao domainDao;
104104

105105
private static final Logger LOGGER = Logger.getLogger(NsxElement.class);
106106

@@ -117,7 +117,6 @@ private static Map<Network.Service, Map<Network.Capability, String>> initCapabil
117117
dnsCapabilities.put(Network.Capability.AllowDnsSuffixModification, "true");
118118
capabilities.put(Network.Service.Dns, dnsCapabilities);
119119

120-
// capabilities.put(Network.Service.Connectivity, null);
121120
capabilities.put(Network.Service.StaticNat, null);
122121

123122
Map<Network.Capability, String> sourceNatCapabilities = new HashMap<>();
@@ -234,7 +233,7 @@ public boolean verifyServicesCombination(Set<Network.Service> services) {
234233
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
235234
agentManager.registerForHostEvents(this, true, true, true);
236235
resourceManager.registerResourceStateAdapter(this.getClass().getSimpleName(), this);
237-
return false;
236+
return true;
238237
}
239238

240239
@Override

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static java.util.Objects.nonNull;
2121

2222
import com.cloud.dc.DataCenter;
23-
import com.cloud.dc.dao.DataCenterDao;
2423
import com.cloud.deploy.DeployDestination;
2524
import com.cloud.deploy.DeploymentPlan;
2625
import com.cloud.domain.DomainVO;
@@ -66,11 +65,9 @@ public class NsxGuestNetworkGuru extends GuestNetworkGuru implements NetworkMigr
6665
@Inject
6766
NsxControllerUtils nsxControllerUtils;
6867
@Inject
69-
DataCenterDao zoneDao;
70-
@Inject
7168
AccountDao accountDao;
7269
@Inject
73-
private DomainDao domainDao;
70+
DomainDao domainDao;
7471

7572
public NsxGuestNetworkGuru() {
7673
super();
@@ -138,7 +135,7 @@ public Network design(NetworkOffering offering, DeploymentPlan plan, Network use
138135
implemented.setBroadcastUri(Networks.BroadcastDomainType.NSX.toUri("nsx"));
139136
try {
140137
long zoneId = network.getDataCenterId();
141-
DataCenter zone = zoneDao.findById(zoneId);
138+
DataCenter zone = _dcDao.findById(zoneId);
142139
if (isNull(zone)) {
143140
throw new CloudRuntimeException(String.format("Failed to find zone with id: %s", zoneId));
144141
}
@@ -179,16 +176,6 @@ public Network implement(Network network, NetworkOffering offering, DeployDestin
179176
implemented.setName(network.getName());
180177
}
181178
implemented.setBroadcastUri(Networks.BroadcastDomainType.NSX.toUri("nsx"));
182-
// try {
183-
// long zoneId = network.getDataCenterId();
184-
// DataCenter zone = zoneDao.findById(zoneId);
185-
// if (isNull(zone)) {
186-
// throw new CloudRuntimeException(String.format("Failed to find zone with id: %s", zoneId));
187-
// }
188-
// createNsxSegment(implemented, zone);
189-
// } catch (Exception ex) {
190-
// throw new CloudRuntimeException("unable to create NSX network " + network.getUuid() + "due to: " + ex.getMessage());
191-
// }
192179
return implemented;
193180
}
194181

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@
5353
import java.util.Objects;
5454
import java.util.UUID;
5555

56-
57-
58-
5956
public class NsxProviderServiceImpl implements NsxProviderService {
6057

6158
@Inject

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030

3131
public class NsxServiceImpl implements NsxService {
3232
@Inject
33-
private NsxControllerUtils nsxControllerUtils;
33+
NsxControllerUtils nsxControllerUtils;
3434
@Inject
35-
private VpcDao vpcDao;
35+
VpcDao vpcDao;
3636

3737
public boolean createVpcNetwork(Long zoneId, String zoneName, String accountName, String domainName, String vpcName) {
3838
CreateNsxTier1GatewayCommand createNsxTier1GatewayCommand =
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.resource;
18+
19+
import com.cloud.network.dao.NetworkVO;
20+
import com.vmware.nsx.model.TransportZone;
21+
import com.vmware.nsx.model.TransportZoneListResult;
22+
import com.vmware.nsx_policy.model.EnforcementPoint;
23+
import com.vmware.nsx_policy.model.EnforcementPointListResult;
24+
import com.vmware.nsx_policy.model.Site;
25+
import com.vmware.nsx_policy.model.SiteListResult;
26+
import junit.framework.Assert;
27+
import org.apache.cloudstack.NsxAnswer;
28+
import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand;
29+
import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand;
30+
import org.apache.cloudstack.agent.api.DeleteNsxSegmentCommand;
31+
import org.apache.cloudstack.agent.api.DeleteNsxTier1GatewayCommand;
32+
import org.apache.cloudstack.agent.api.NsxCommand;
33+
import org.apache.cloudstack.service.NsxApiClient;
34+
import org.junit.After;
35+
import org.junit.Before;
36+
import org.junit.Test;
37+
import org.junit.runner.RunWith;
38+
39+
import org.mockito.Mock;
40+
import org.mockito.MockitoAnnotations;
41+
import org.mockito.junit.MockitoJUnitRunner;
42+
43+
import javax.naming.ConfigurationException;
44+
import java.util.HashMap;
45+
import java.util.List;
46+
import java.util.Map;
47+
48+
import static org.junit.Assert.assertTrue;
49+
import static org.junit.Assert.assertThrows;
50+
import static org.mockito.ArgumentMatchers.anyString;
51+
import static org.mockito.Mockito.mock;
52+
import static org.mockito.Mockito.when;
53+
54+
@RunWith(MockitoJUnitRunner.class)
55+
public class NsxResourceTest {
56+
57+
@Mock
58+
NsxApiClient nsxApi;
59+
60+
NsxResource nsxResource;
61+
AutoCloseable closeable;
62+
@Mock
63+
EnforcementPointListResult enforcementPointListResult;
64+
@Mock
65+
SiteListResult siteListResult;
66+
@Mock
67+
TransportZoneListResult transportZoneListResult;
68+
69+
@Before
70+
public void setup() {
71+
closeable = MockitoAnnotations.openMocks(this);
72+
nsxResource = new NsxResource();
73+
nsxResource.nsxApiClient = nsxApi;
74+
nsxResource.transportZone = "Overlay";
75+
}
76+
77+
@After
78+
public void tearDown() throws Exception {
79+
closeable.close();
80+
}
81+
82+
@Test
83+
public void testConfigure() throws ConfigurationException {
84+
Map<String, Object> params = new HashMap<>();
85+
params.put("name", "nsxController");
86+
params.put("guid", "5944b356-644f-11ee-b8c2-f37bc1b564ff");
87+
params.put("zoneId", "1");
88+
params.put("hostname", "host1");
89+
params.put("username", "admin");
90+
params.put("password", "password");
91+
params.put("tier0Gateway", "Tier0-GW01");
92+
params.put("edgeCluster", "EdgeCluster");
93+
params.put("transportZone", "Overlay");
94+
params.put("port", "443");
95+
96+
Assert.assertTrue(nsxResource.configure("nsx", params));
97+
}
98+
99+
@Test
100+
public void testConfigure_MissingParameter() throws ConfigurationException {
101+
Map<String, Object> params = new HashMap<>();
102+
103+
assertThrows(ConfigurationException.class, () -> nsxResource.configure("nsx", params));
104+
}
105+
106+
@Test
107+
public void testCreateNsxTier1Gateway() {
108+
NsxCommand command = new CreateNsxTier1GatewayCommand("testDomain", "testAcc",
109+
"ZoneNSX", "VPC01");
110+
111+
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(command);
112+
assertTrue(answer.getResult());
113+
}
114+
115+
@Test
116+
public void testDeleteTier1Gateway() {
117+
NsxCommand command = new DeleteNsxTier1GatewayCommand("testDomain", "testAcc",
118+
"ZoneNSX", "VPC01");
119+
120+
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(command);
121+
assertTrue(answer.getResult());
122+
}
123+
124+
@Test
125+
public void testCreateNsxSegment() {
126+
NetworkVO tierNetwork = new NetworkVO();
127+
tierNetwork.setName("tier1");
128+
tierNetwork.setCidr("10.0.0.0/8");
129+
tierNetwork.setGateway("10.0.0.1");
130+
Site site = mock(Site.class);
131+
List<Site> siteList = List.of(site);
132+
EnforcementPoint enforcementPoint = mock(EnforcementPoint.class);
133+
List<EnforcementPoint> enforcementPointList = List.of(enforcementPoint);
134+
List<TransportZone> transportZoneList = List.of(new TransportZone.Builder().setDisplayName("Overlay").build());
135+
136+
NsxCommand command = new CreateNsxSegmentCommand("testDomain", "testAcc",
137+
"ZoneNSX", "VPC01", "Web", "10.10.10.1", "10.10.10.0/24");
138+
139+
when(nsxApi.getSites()).thenReturn(siteListResult);
140+
when(siteListResult.getResults()).thenReturn(siteList);
141+
when(siteList.get(0).getId()).thenReturn("site1");
142+
143+
when(nsxApi.getEnforcementPoints(anyString())).thenReturn(enforcementPointListResult);
144+
when(enforcementPointListResult.getResults()).thenReturn(enforcementPointList);
145+
when(enforcementPointList.get(0).getPath()).thenReturn("enforcementPointPath");
146+
147+
when(nsxApi.getTransportZones()).thenReturn(transportZoneListResult);
148+
when(transportZoneListResult.getResults()).thenReturn(transportZoneList);
149+
150+
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(command);
151+
assertTrue(answer.getResult());
152+
}
153+
154+
@Test
155+
public void testDeleteNsxSegment() {
156+
NetworkVO tierNetwork = new NetworkVO();
157+
tierNetwork.setName("tier1");
158+
DeleteNsxSegmentCommand command = new DeleteNsxSegmentCommand("testDomain", "testAcc", "ZoneA", "VPC01", "Web");
159+
160+
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(command);
161+
assertTrue(answer.getResult());
162+
}
163+
}

0 commit comments

Comments
 (0)