mirror of https://github.com/apache/cloudstack.git
NSX: Add unit tests to increase coverage (#8355)
* NSX: Add unit tests * cleanup unused imports * add more unit tests * add tests for publicnsxnetworkguru * add license * fix build failures * address sonar comment
This commit is contained in:
parent
4457c62ad3
commit
7288ac458f
|
|
@ -29,7 +29,21 @@
|
|||
<version>4.19.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<version>${cs.jacoco-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/org/apache/cloudstack/agent/api/**</exclude>
|
||||
<exclude>**/org/apache/cloudstack/api/response/**</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.vmware</groupId>
|
||||
|
|
|
|||
|
|
@ -39,7 +39,6 @@ import com.cloud.network.dao.PhysicalNetworkVO;
|
|||
import com.cloud.network.guru.GuestNetworkGuru;
|
||||
import com.cloud.network.vpc.VpcVO;
|
||||
import com.cloud.offering.NetworkOffering;
|
||||
import com.cloud.offerings.dao.NetworkOfferingDao;
|
||||
import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.user.dao.AccountDao;
|
||||
|
|
@ -76,8 +75,6 @@ public class NsxGuestNetworkGuru extends GuestNetworkGuru implements NetworkMigr
|
|||
DomainDao domainDao;
|
||||
@Inject
|
||||
NetworkModel networkModel;
|
||||
@Inject
|
||||
NetworkOfferingDao networkOfferingDao;
|
||||
|
||||
public NsxGuestNetworkGuru() {
|
||||
super();
|
||||
|
|
@ -298,7 +295,7 @@ public class NsxGuestNetworkGuru extends GuestNetworkGuru implements NetworkMigr
|
|||
// Do nothing
|
||||
}
|
||||
|
||||
private void createNsxSegment(NetworkVO networkVO, DataCenter zone) {
|
||||
public void createNsxSegment(NetworkVO networkVO, DataCenter zone) {
|
||||
Account account = accountDao.findById(networkVO.getAccountId());
|
||||
if (isNull(account)) {
|
||||
throw new CloudRuntimeException(String.format("Unable to find account with id: %s", networkVO.getAccountId()));
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ import com.cloud.resource.ResourceManager;
|
|||
import com.cloud.utils.db.Transaction;
|
||||
import com.cloud.utils.db.TransactionCallback;
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import org.apache.cloudstack.api.command.DeleteNsxControllerCmd;
|
||||
import org.apache.cloudstack.api.command.ListNsxControllersCmd;
|
||||
import org.apache.cloudstack.api.BaseResponse;
|
||||
|
|
@ -197,7 +198,8 @@ public class NsxProviderServiceImpl implements NsxProviderService {
|
|||
return cmdList;
|
||||
}
|
||||
|
||||
private void validateNetworkState(List<NetworkVO> networkList) {
|
||||
@VisibleForTesting
|
||||
void validateNetworkState(List<NetworkVO> networkList) {
|
||||
for (NetworkVO network : networkList) {
|
||||
if (network.getBroadcastDomainType() == Networks.BroadcastDomainType.NSX) {
|
||||
if ((network.getState() != Network.State.Shutdown) && (network.getState() != Network.State.Destroy)) {
|
||||
|
|
|
|||
|
|
@ -25,8 +25,14 @@ import com.vmware.nsx_policy.model.Site;
|
|||
import com.vmware.nsx_policy.model.SiteListResult;
|
||||
import junit.framework.Assert;
|
||||
import org.apache.cloudstack.NsxAnswer;
|
||||
import org.apache.cloudstack.agent.api.CreateNsxDistributedFirewallRulesCommand;
|
||||
import org.apache.cloudstack.agent.api.CreateNsxLoadBalancerRuleCommand;
|
||||
import org.apache.cloudstack.agent.api.CreateNsxPortForwardRuleCommand;
|
||||
import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand;
|
||||
import org.apache.cloudstack.agent.api.CreateNsxStaticNatCommand;
|
||||
import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand;
|
||||
import org.apache.cloudstack.agent.api.DeleteNsxDistributedFirewallRulesCommand;
|
||||
import org.apache.cloudstack.agent.api.DeleteNsxNatRuleCommand;
|
||||
import org.apache.cloudstack.agent.api.DeleteNsxSegmentCommand;
|
||||
import org.apache.cloudstack.agent.api.DeleteNsxTier1GatewayCommand;
|
||||
import org.apache.cloudstack.agent.api.NsxCommand;
|
||||
|
|
@ -160,4 +166,57 @@ public class NsxResourceTest {
|
|||
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(command);
|
||||
assertTrue(answer.getResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateStaticNat() {
|
||||
CreateNsxStaticNatCommand cmd = new CreateNsxStaticNatCommand(1L, 1L, 1L, 3L, "VPC01", true, 2L, "10.1.12.10", "172.30.20.12");
|
||||
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(cmd);
|
||||
assertTrue(answer.getResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreatePortForwardRule() {
|
||||
CreateNsxPortForwardRuleCommand cmd = new CreateNsxPortForwardRuleCommand(1L, 1L, 1L, 3L, "VPC01", true, 2L, 5L, "10.1.12.10", "172.30.20.12", "2222", "22", "tcp");
|
||||
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(cmd);
|
||||
assertTrue(answer.getResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteNsxNatRule() {
|
||||
DeleteNsxNatRuleCommand cmd = new DeleteNsxNatRuleCommand(1L, 1L, 1L, 3L, "VPC01", true, 2L, 5L, "22", "tcp");
|
||||
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(cmd);
|
||||
assertTrue(answer.getResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNsxLoadBalancerRule() {
|
||||
List<NsxLoadBalancerMember> loadBalancerMembers = List.of(new NsxLoadBalancerMember(
|
||||
1L, "172.30.20.12", 6443
|
||||
));
|
||||
CreateNsxLoadBalancerRuleCommand cmd = new CreateNsxLoadBalancerRuleCommand(1L, 1L, 1L,
|
||||
3L, "VPC01", true, loadBalancerMembers, 1L, "6443", "6443", "RoundRobin", "TCP");
|
||||
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(cmd);
|
||||
assertTrue(answer.getResult());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCreateNsxDistributedFirewallRule() {
|
||||
List<NsxNetworkRule> networkRules = List.of(new NsxNetworkRule());
|
||||
CreateNsxDistributedFirewallRulesCommand cmd = new CreateNsxDistributedFirewallRulesCommand(1L, 1L, 1L,
|
||||
3L, 1L, networkRules);
|
||||
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(cmd);
|
||||
assertTrue(answer.getResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteNsxDistributedFirewallRule() {
|
||||
List<NsxNetworkRule> networkRules = List.of(new NsxNetworkRule());
|
||||
DeleteNsxDistributedFirewallRulesCommand cmd = new DeleteNsxDistributedFirewallRulesCommand(1L, 1L, 1L,
|
||||
3L, 1L, networkRules);
|
||||
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(cmd);
|
||||
assertTrue(answer.getResult());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
// under the License.
|
||||
package org.apache.cloudstack.service;
|
||||
|
||||
import com.cloud.api.ApiDBUtils;
|
||||
import com.cloud.dc.DataCenterVO;
|
||||
import com.cloud.dc.dao.DataCenterDao;
|
||||
import com.cloud.deploy.DeployDestination;
|
||||
|
|
@ -23,31 +24,64 @@ import com.cloud.domain.DomainVO;
|
|||
import com.cloud.domain.dao.DomainDao;
|
||||
import com.cloud.exception.InsufficientCapacityException;
|
||||
import com.cloud.exception.ResourceUnavailableException;
|
||||
import com.cloud.hypervisor.Hypervisor;
|
||||
import com.cloud.network.Network;
|
||||
import com.cloud.network.NetworkModel;
|
||||
import com.cloud.network.Networks;
|
||||
import com.cloud.network.dao.IPAddressDao;
|
||||
import com.cloud.network.dao.IPAddressVO;
|
||||
import com.cloud.network.dao.LoadBalancerVMMapDao;
|
||||
import com.cloud.network.dao.LoadBalancerVO;
|
||||
import com.cloud.network.dao.NetworkDao;
|
||||
import com.cloud.network.dao.NetworkVO;
|
||||
import com.cloud.network.dao.PhysicalNetworkDao;
|
||||
import com.cloud.network.dao.PhysicalNetworkVO;
|
||||
import com.cloud.network.lb.LoadBalancingRule;
|
||||
import com.cloud.network.rules.FirewallRule;
|
||||
import com.cloud.network.rules.FirewallRuleVO;
|
||||
import com.cloud.network.rules.PortForwardingRule;
|
||||
import com.cloud.network.rules.PortForwardingRuleVO;
|
||||
import com.cloud.network.rules.StaticNatImpl;
|
||||
import com.cloud.network.vpc.NetworkACLItem;
|
||||
import com.cloud.network.vpc.NetworkACLItemVO;
|
||||
import com.cloud.network.vpc.Vpc;
|
||||
import com.cloud.network.vpc.VpcVO;
|
||||
import com.cloud.network.vpc.dao.VpcDao;
|
||||
import com.cloud.network.vpc.dao.VpcOfferingServiceMapDao;
|
||||
import com.cloud.resource.ResourceManager;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.user.AccountManager;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.net.Ip;
|
||||
import com.cloud.vm.NicVO;
|
||||
import com.cloud.vm.ReservationContext;
|
||||
import com.cloud.vm.VMInstanceVO;
|
||||
import com.cloud.vm.VirtualMachine;
|
||||
import com.cloud.vm.dao.UserVmDao;
|
||||
import com.cloud.vm.dao.VMInstanceDao;
|
||||
import org.apache.cloudstack.acl.ControlledEntity;
|
||||
import org.apache.cloudstack.resource.NsxNetworkRule;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
|
@ -79,7 +113,17 @@ public class NsxElementTest {
|
|||
@Mock
|
||||
DomainVO domain;
|
||||
@Mock
|
||||
IPAddressDao ipAddressDao;
|
||||
@Mock
|
||||
VMInstanceDao vmInstanceDao;
|
||||
@Mock
|
||||
VpcDao vpcDao;
|
||||
@Mock
|
||||
UserVmDao userVmDao;
|
||||
@Mock
|
||||
private VpcOfferingServiceMapDao vpcOfferingServiceMapDao;
|
||||
@Mock
|
||||
LoadBalancerVMMapDao lbVmMapDao;
|
||||
|
||||
NsxElement nsxElement;
|
||||
ReservationContext reservationContext;
|
||||
|
|
@ -88,7 +132,7 @@ public class NsxElementTest {
|
|||
DomainDao domainDao;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
public void setup() throws NoSuchFieldException, IllegalAccessException {
|
||||
nsxElement = new NsxElement();
|
||||
|
||||
nsxElement.dataCenterDao = dataCenterDao;
|
||||
|
|
@ -100,6 +144,18 @@ public class NsxElementTest {
|
|||
nsxElement.domainDao = domainDao;
|
||||
nsxElement.networkModel = networkModel;
|
||||
nsxElement.vpcOfferingServiceMapDao = vpcOfferingServiceMapDao;
|
||||
nsxElement.ipAddressDao = ipAddressDao;
|
||||
nsxElement.vmInstanceDao = vmInstanceDao;
|
||||
nsxElement.vpcDao = vpcDao;
|
||||
nsxElement.lbVmMapDao = lbVmMapDao;
|
||||
|
||||
Field field = ApiDBUtils.class.getDeclaredField("s_ipAddressDao");
|
||||
field.setAccessible(true);
|
||||
field.set(null, ipAddressDao);
|
||||
|
||||
field = ApiDBUtils.class.getDeclaredField("s_userVmDao");
|
||||
field.setAccessible(true);
|
||||
field.set(null, userVmDao);
|
||||
reservationContext = mock(ReservationContext.class);
|
||||
deployDestination = mock(DeployDestination.class);
|
||||
|
||||
|
|
@ -152,4 +208,242 @@ public class NsxElementTest {
|
|||
Assert.assertEquals("ANY", values.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCanHandleService() {
|
||||
when(networkModel.isProviderForNetwork(any(Network.Provider.class), anyLong())).thenReturn(true);
|
||||
|
||||
Network.Service service = new Network.Service("service1", new Network.Capability("capability"));
|
||||
NetworkVO network = new NetworkVO();
|
||||
network.setName("network1");
|
||||
assertTrue(nsxElement.canHandle(network, service));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplyStaticNatRules() throws ResourceUnavailableException {
|
||||
StaticNatImpl rule = new StaticNatImpl(1L , 1L, 3L, 7L, "172.30.10.15", false);
|
||||
NetworkVO networkVO = new NetworkVO(1L, Networks.TrafficType.Public, Networks.Mode.Static,
|
||||
Networks.BroadcastDomainType.NSX, 12L, 2L, 5L, 1L, "network1",
|
||||
"network1", null, Network.GuestType.Isolated, 2L, 2L,
|
||||
ControlledEntity.ACLType.Domain, false, 1L, false );
|
||||
|
||||
Ip ip = new Ip("10.1.13.15");
|
||||
IPAddressVO ipAddress = new IPAddressVO(ip, 2L, 0xaabbccddeeffL, 3L, false);
|
||||
ipAddress.setAssociatedWithVmId(10L);
|
||||
|
||||
VMInstanceVO vm = new VMInstanceVO(10L, 9L, "vm1", "i-5-10-VM" , VirtualMachine.Type.User,
|
||||
18L, Hypervisor.HypervisorType.VMware, 26L,
|
||||
2L, 5L, 6L, false, false);
|
||||
|
||||
NicVO nic = Mockito.mock(NicVO.class);
|
||||
VpcVO vpc = Mockito.mock(VpcVO.class);
|
||||
|
||||
when(ipAddressDao.findByIdIncludingRemoved(anyLong())).thenReturn(ipAddress);
|
||||
when(vmInstanceDao.findByIdIncludingRemoved(anyLong())).thenReturn(vm);
|
||||
when(networkModel.getNicInNetworkIncludingRemoved(anyLong(), anyLong())).thenReturn(nic);
|
||||
when(vpcDao.findById(anyLong())).thenReturn(vpc);
|
||||
when(vpc.getId()).thenReturn(1L);
|
||||
when(vpc.getName()).thenReturn("vpc1");
|
||||
when(nsxService.createStaticNatRule(anyLong(), anyLong(), anyLong(), anyLong(), anyString(), anyBoolean(), anyLong(), anyString(), anyString())).thenReturn(true);
|
||||
|
||||
assertTrue(nsxElement.applyStaticNats(networkVO, List.of(rule)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplyPFRules() throws ResourceUnavailableException {
|
||||
NetworkVO networkVO = new NetworkVO(1L, Networks.TrafficType.Public, Networks.Mode.Static,
|
||||
Networks.BroadcastDomainType.NSX, 12L, 2L, 5L, 1L, "network1",
|
||||
"network1", null, Network.GuestType.Isolated, 2L, 2L,
|
||||
ControlledEntity.ACLType.Domain, false, 1L, false );
|
||||
PortForwardingRule rule = new PortForwardingRuleVO("1", 11L, 80, 90, new Ip("172.30.10.11"), 8080, 8090, "tcp", 12L,
|
||||
5L, 2L, 15L);
|
||||
Network.Service service = new Network.Service("service1", new Network.Capability("capability"));
|
||||
|
||||
when(nsxElement.canHandle(networkVO, service)).thenReturn(true);
|
||||
assertTrue(nsxElement.applyPFRules(networkVO, List.of(rule)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetVpcOrNetworkReturnsVpcIfVpcIdPresent() {
|
||||
VpcVO vpc = new VpcVO();
|
||||
when(vpcDao.findById(anyLong())).thenReturn(vpc);
|
||||
|
||||
Pair<VpcVO, NetworkVO> vpcNetworkPair = nsxElement.getVpcOrNetwork(1L, 1L);
|
||||
assertNotNull(vpcNetworkPair.first());
|
||||
assertNull(vpcNetworkPair.second());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetVpcOrNetworkReturnsNetworkIfVpcIdNotPresent() {
|
||||
NetworkVO network = new NetworkVO();
|
||||
when(networkDao.findById(anyLong())).thenReturn(network);
|
||||
|
||||
Pair<VpcVO, NetworkVO> vpcNetworkPair = nsxElement.getVpcOrNetwork(null, 1L);
|
||||
assertNull(vpcNetworkPair.first());
|
||||
assertNotNull(vpcNetworkPair.second());
|
||||
}
|
||||
|
||||
private Method getPublicPortRangeMethod() throws NoSuchMethodException {
|
||||
Method method = NsxElement.class.getDeclaredMethod("getPublicPortRange", PortForwardingRule.class);
|
||||
method.setAccessible(true);
|
||||
return method;
|
||||
}
|
||||
|
||||
private Method getPrivatePFPortRangeMethod() throws NoSuchMethodException {
|
||||
Method method = NsxElement.class.getDeclaredMethod("getPrivatePFPortRange", PortForwardingRule.class);
|
||||
method.setAccessible(true);
|
||||
return method;
|
||||
}
|
||||
|
||||
private Method getPrivatePortRangeMethod() throws NoSuchMethodException {
|
||||
Method method = NsxElement.class.getDeclaredMethod("getPrivatePortRange", FirewallRule.class);
|
||||
method.setAccessible(true);
|
||||
return method;
|
||||
}
|
||||
|
||||
private Method getPrivatePortRangeForACLRuleMethod() throws NoSuchMethodException {
|
||||
Method method = NsxElement.class.getDeclaredMethod("getPrivatePortRangeForACLRule", NetworkACLItem.class);
|
||||
method.setAccessible(true);
|
||||
return method;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPublicPortRangeWhenStartAndEndPortNumbersAreDifferent() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||
PortForwardingRule rule = new PortForwardingRuleVO("1", 11L, 80, 90, new Ip("172.30.10.11"), 8080, 8090, "tcp", 12L,
|
||||
5L, 2L, 15L);
|
||||
assertEquals("80-90", getPublicPortRangeMethod().invoke(null, rule));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPublicPortRangeWhenStartAndEndPortNumbersAreSame() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||
PortForwardingRule rule = new PortForwardingRuleVO("1", 11L, 80, 80, new Ip("172.30.10.11"), 8080, 8080, "tcp", 12L,
|
||||
5L, 2L, 15L);
|
||||
assertEquals("80", getPublicPortRangeMethod().invoke(null, rule));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPrivatePFPortRangeWhenStartAndEndPortNumbersAreDifferent() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||
PortForwardingRule rule = new PortForwardingRuleVO("1", 11L, 80, 90, new Ip("172.30.10.11"), 8080, 8090, "tcp", 12L,
|
||||
5L, 2L, 15L);
|
||||
assertEquals("8080-8090", getPrivatePFPortRangeMethod().invoke(null, rule));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPrivatePFPortRangeWhenStartAndEndPortNumbersAreSame() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||
PortForwardingRule rule = new PortForwardingRuleVO("1", 11L, 80, 80, new Ip("172.30.10.11"), 8080, 8080, "tcp", 12L,
|
||||
5L, 2L, 15L);
|
||||
assertEquals("8080", getPrivatePFPortRangeMethod().invoke(null, rule));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPrivatePortRangeWhenStartAndEndPortNumbersAreSame() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||
FirewallRuleVO rule = new FirewallRuleVO("1", 11L, 80, 80, "tcp", 23L, 5L, 2L,
|
||||
FirewallRule.Purpose.Firewall, List.of("172.30.10.0/24"), null, null, null, null, FirewallRule.TrafficType.Egress, FirewallRule.FirewallRuleType.User);
|
||||
assertEquals("80", getPrivatePortRangeMethod().invoke(null, rule));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPrivatePortRangeWhenStartAndEndPortNumbersAreDifferent() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||
FirewallRuleVO rule = new FirewallRuleVO("1", 11L, 80, 90, "tcp", 23L, 5L, 2L,
|
||||
FirewallRule.Purpose.Firewall, List.of("172.30.10.0/24"), null, null, null, null, FirewallRule.TrafficType.Egress, FirewallRule.FirewallRuleType.User);
|
||||
assertEquals("80-90", getPrivatePortRangeMethod().invoke(null, rule));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPrivatePortRangeForACLWhenStartAndEndPortNumbersAreSame() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||
NetworkACLItem rule = new NetworkACLItemVO(80, 80, "udp", 10L, List.of("172.30.10.0/24"), null, null, NetworkACLItem.TrafficType.Ingress, NetworkACLItem.Action.Allow,
|
||||
2, null);
|
||||
assertEquals("80", getPrivatePortRangeForACLRuleMethod().invoke(null, rule));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPrivatePortRangeForACLWhenStartAndEndPortNumbersAreDifferent() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||
NetworkACLItem rule = new NetworkACLItemVO(80, 90, "udp", 10L, List.of("172.30.10.0/24"), null, null, NetworkACLItem.TrafficType.Ingress, NetworkACLItem.Action.Allow,
|
||||
2, null);
|
||||
assertEquals("80-90", getPrivatePortRangeForACLRuleMethod().invoke(null, rule));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplyLBRules() throws ResourceUnavailableException {
|
||||
NetworkVO networkVO = new NetworkVO(1L, Networks.TrafficType.Public, Networks.Mode.Static,
|
||||
Networks.BroadcastDomainType.NSX, 12L, 2L, 5L, 1L, "network1",
|
||||
"network1", null, Network.GuestType.Isolated, 2L, 2L,
|
||||
ControlledEntity.ACLType.Domain, false, 1L, false );
|
||||
LoadBalancerVO lb = new LoadBalancerVO(null, null, null, 0L, 8080, 8081, null, 0L, 0L, 1L, null, null);
|
||||
LoadBalancingRule.LbDestination destination = new LoadBalancingRule.LbDestination(6443, 6443, "172.30.110.11", false);
|
||||
LoadBalancingRule rule = new LoadBalancingRule(lb, List.of(destination), null, null, new Ip("10.1.13.10"));
|
||||
|
||||
VpcVO vpc = Mockito.mock(VpcVO.class);
|
||||
|
||||
IPAddressVO ipAddress = new IPAddressVO(new Ip("10.1.13.10"), 1L, 1L, 1L,false);
|
||||
when(vpcDao.findById(anyLong())).thenReturn(vpc);
|
||||
when(vpc.getDomainId()).thenReturn(2L);
|
||||
when(vpc.getAccountId()).thenReturn(5L);
|
||||
when(ipAddressDao.findByIpAndDcId(anyLong(), anyString())).thenReturn(ipAddress);
|
||||
|
||||
assertTrue(nsxElement.applyLBRules(networkVO, List.of(rule)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplyNetworkAclRules() throws ResourceUnavailableException {
|
||||
NetworkVO networkVO = new NetworkVO(1L, Networks.TrafficType.Public, Networks.Mode.Static,
|
||||
Networks.BroadcastDomainType.NSX, 12L, 2L, 5L, 1L, "network1",
|
||||
"network1", null, Network.GuestType.Isolated, 2L, 2L,
|
||||
ControlledEntity.ACLType.Domain, false, 1L, false );
|
||||
NetworkACLItem rule = new NetworkACLItemVO(80, 80, "udp", 10L, List.of("172.30.10.0/24"), null, null, NetworkACLItem.TrafficType.Ingress, NetworkACLItem.Action.Allow,
|
||||
2, null);
|
||||
Network.Service service = new Network.Service("service1", new Network.Capability("capability"));
|
||||
|
||||
when(nsxElement.canHandle(networkVO, service)).thenReturn(true);
|
||||
when(nsxService.addFirewallRules(any(Network.class), any(List.class))).thenReturn(true);
|
||||
assertTrue(nsxElement.applyNetworkACLs(networkVO, List.of(rule)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteNetworkAclRules() throws ResourceUnavailableException {
|
||||
NetworkVO networkVO = new NetworkVO(1L, Networks.TrafficType.Public, Networks.Mode.Static,
|
||||
Networks.BroadcastDomainType.NSX, 12L, 2L, 5L, 1L, "network1",
|
||||
"network1", null, Network.GuestType.Isolated, 2L, 2L,
|
||||
ControlledEntity.ACLType.Domain, false, 1L, false );
|
||||
NetworkACLItemVO rule = new NetworkACLItemVO(80, 80, "udp", 10L, List.of("172.30.10.0/24"), null, null, NetworkACLItem.TrafficType.Ingress, NetworkACLItem.Action.Allow,
|
||||
2, null);
|
||||
rule.setState(NetworkACLItem.State.Revoke);
|
||||
Network.Service service = new Network.Service("service1", new Network.Capability("capability"));
|
||||
|
||||
when(nsxElement.canHandle(networkVO, service)).thenReturn(true);
|
||||
when(nsxService.deleteFirewallRules(any(Network.class), any(List.class))).thenReturn(true);
|
||||
when(nsxService.addFirewallRules(any(Network.class), any(List.class))).thenReturn(true);
|
||||
assertTrue(nsxElement.applyNetworkACLs(networkVO, List.of(rule)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplyFirewallRules() throws ResourceUnavailableException {
|
||||
NetworkVO networkVO = new NetworkVO(1L, Networks.TrafficType.Public, Networks.Mode.Static,
|
||||
Networks.BroadcastDomainType.NSX, 12L, 2L, 5L, 1L, "network1",
|
||||
"network1", null, Network.GuestType.Isolated, 2L, 2L,
|
||||
ControlledEntity.ACLType.Domain, false, 1L, false );
|
||||
FirewallRuleVO rule = new FirewallRuleVO("1", 11L, 80, 80, "tcp", 23L, 5L, 2L,
|
||||
FirewallRule.Purpose.Firewall, List.of("172.30.10.0/24"), null, null, null, null, FirewallRule.TrafficType.Egress, FirewallRule.FirewallRuleType.User);
|
||||
Network.Service service = new Network.Service("service1", new Network.Capability("capability"));
|
||||
|
||||
when(nsxElement.canHandle(networkVO, service)).thenReturn(true);
|
||||
when(nsxService.addFirewallRules(any(Network.class), any(List.class))).thenReturn(true);
|
||||
assertTrue(nsxElement.applyFWRules(networkVO, List.of(rule)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRevokeFirewallRules() throws ResourceUnavailableException {
|
||||
NetworkVO networkVO = new NetworkVO(1L, Networks.TrafficType.Public, Networks.Mode.Static,
|
||||
Networks.BroadcastDomainType.NSX, 12L, 2L, 5L, 1L, "network1",
|
||||
"network1", null, Network.GuestType.Isolated, 2L, 2L,
|
||||
ControlledEntity.ACLType.Domain, false, 1L, false );
|
||||
FirewallRuleVO rule = new FirewallRuleVO("1", 11L, 80, 80, "tcp", 23L, 5L, 2L,
|
||||
FirewallRule.Purpose.Firewall, List.of("172.30.10.0/24"), null, null, null, null, FirewallRule.TrafficType.Egress, FirewallRule.FirewallRuleType.User);
|
||||
rule.setState(FirewallRule.State.Revoke);
|
||||
Network.Service service = new Network.Service("service1", new Network.Capability("capability"));
|
||||
|
||||
when(nsxElement.canHandle(networkVO, service)).thenReturn(true);
|
||||
when(nsxService.deleteFirewallRules(any(Network.class), any(List.class))).thenReturn(true);
|
||||
when(nsxService.addFirewallRules(any(Network.class), any(List.class))).thenReturn(true);
|
||||
assertTrue(nsxElement.applyFWRules(networkVO, List.of(rule)));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,9 @@ import com.cloud.deploy.DeployDestination;
|
|||
import com.cloud.deploy.DeploymentPlan;
|
||||
import com.cloud.domain.DomainVO;
|
||||
import com.cloud.domain.dao.DomainDao;
|
||||
import com.cloud.exception.InsufficientAddressCapacityException;
|
||||
import com.cloud.exception.InsufficientVirtualNetworkCapacityException;
|
||||
import com.cloud.network.IpAddressManager;
|
||||
import com.cloud.network.Network;
|
||||
import com.cloud.network.NetworkModel;
|
||||
import com.cloud.network.Networks;
|
||||
|
|
@ -30,16 +33,24 @@ import com.cloud.network.dao.NetworkDao;
|
|||
import com.cloud.network.dao.NetworkVO;
|
||||
import com.cloud.network.dao.PhysicalNetworkDao;
|
||||
import com.cloud.network.dao.PhysicalNetworkVO;
|
||||
import com.cloud.network.guru.GuestNetworkGuru;
|
||||
import com.cloud.network.vpc.VpcVO;
|
||||
import com.cloud.network.vpc.dao.VpcDao;
|
||||
import com.cloud.offering.NetworkOffering;
|
||||
import com.cloud.offerings.dao.NetworkOfferingDao;
|
||||
import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.user.dao.AccountDao;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.vm.NicProfile;
|
||||
import com.cloud.vm.ReservationContext;
|
||||
import com.cloud.vm.VirtualMachine;
|
||||
import com.cloud.vm.VirtualMachineProfile;
|
||||
import org.apache.cloudstack.NsxAnswer;
|
||||
import org.apache.cloudstack.agent.api.CreateNsxDhcpRelayConfigCommand;
|
||||
import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand;
|
||||
import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand;
|
||||
import org.apache.cloudstack.agent.api.NsxCommand;
|
||||
import org.apache.cloudstack.utils.NsxControllerUtils;
|
||||
import org.junit.After;
|
||||
|
|
@ -62,6 +73,7 @@ import static org.junit.Assert.assertFalse;
|
|||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.nullable;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
|
@ -103,19 +115,28 @@ public class NsxGuestNetworkGuruTest {
|
|||
DomainDao domainDao;
|
||||
@Mock
|
||||
NetworkDao networkDao;
|
||||
@Mock
|
||||
IpAddressManager ipAddressManager;
|
||||
@Mock
|
||||
NetworkOfferingDao networkOfferingDao;
|
||||
|
||||
NsxGuestNetworkGuru guru;
|
||||
GuestNetworkGuru guestNetworkGuru;
|
||||
AutoCloseable closeable;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
public void setUp() throws IllegalAccessException, NoSuchFieldException {
|
||||
closeable = MockitoAnnotations.openMocks(this);
|
||||
guru = new NsxGuestNetworkGuru();
|
||||
ReflectionTestUtils.setField(guru, "_physicalNetworkDao", physicalNetworkDao);
|
||||
|
||||
ReflectionTestUtils.setField(guru, "_dcDao", dcDao);
|
||||
ReflectionTestUtils.setField(guru, "_networkDao", networkDao);
|
||||
ReflectionTestUtils.setField(guru, "_networkModel", networkModel);
|
||||
ReflectionTestUtils.setField(guru, "_vpcDao", vpcDao);
|
||||
ReflectionTestUtils.setField((GuestNetworkGuru) guru, "_ipAddrMgr", ipAddressManager);
|
||||
ReflectionTestUtils.setField((GuestNetworkGuru) guru, "_networkModel", networkModel);
|
||||
ReflectionTestUtils.setField((GuestNetworkGuru) guru, "networkOfferingDao", networkOfferingDao);
|
||||
ReflectionTestUtils.setField((GuestNetworkGuru) guru, "_physicalNetworkDao", physicalNetworkDao);
|
||||
|
||||
guru.networkOfferingServiceMapDao = networkOfferingServiceMapDao;
|
||||
guru.nsxControllerUtils = nsxControllerUtils;
|
||||
|
|
@ -215,4 +236,97 @@ public class NsxGuestNetworkGuruTest {
|
|||
assertEquals(4L, implemented.getVpcId().longValue());
|
||||
assertFalse(implemented.isRedundant());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllocateForUserVM() throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
|
||||
Network network = Mockito.mock(Network.class);
|
||||
NicProfile nicProfile = Mockito.mock(NicProfile.class);
|
||||
VirtualMachineProfile vmProfile = Mockito.mock(VirtualMachineProfile.class);
|
||||
VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class);
|
||||
Pair<String, String> dns = new Pair<>("10.1.5.1", "8.8.8.8");
|
||||
String macAddress = "00:00:00:11:1D:1E:CD";
|
||||
|
||||
when(network.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
|
||||
when(vmProfile.getVirtualMachine()).thenReturn(virtualMachine);
|
||||
when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.User);
|
||||
// when(network.getId()).thenReturn(2L);
|
||||
// when(offering.getId()).thenReturn(11L);
|
||||
when(networkModel.getNetworkIp4Dns(any(Network.class), nullable(DataCenter.class))).thenReturn(dns);
|
||||
// when(networkModel.getNextAvailableMacAddressInNetwork(anyLong())).thenReturn(macAddress);
|
||||
when(nicProfile.getMacAddress()).thenReturn(macAddress);
|
||||
when(networkOfferingDao.isIpv6Supported(anyLong())).thenReturn(false);
|
||||
|
||||
NicProfile profile = guru.allocate(network, nicProfile, vmProfile);
|
||||
assertNotNull(profile);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllocateForDomainRouter() throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
|
||||
Network network = Mockito.mock(Network.class);
|
||||
NicProfile nicProfile = Mockito.mock(NicProfile.class);
|
||||
VirtualMachineProfile vmProfile = Mockito.mock(VirtualMachineProfile.class);
|
||||
VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class);
|
||||
Pair<String, String> dns = new Pair<>("10.1.5.1", "8.8.8.8");
|
||||
String macAddress = "00:00:00:11:1D:1E:CD";
|
||||
|
||||
when(network.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
|
||||
when(vmProfile.getType()).thenReturn(VirtualMachine.Type.DomainRouter);
|
||||
when(vmProfile.getVirtualMachine()).thenReturn(virtualMachine);
|
||||
when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.DomainRouter);
|
||||
when(network.getId()).thenReturn(2L);
|
||||
// when(offering.getId()).thenReturn(11L);
|
||||
// when(networkModel.getNetworkIp4Dns(any(Network.class), nullable(DataCenter.class))).thenReturn(dns);
|
||||
// when(networkModel.getNextAvailableMacAddressInNetwork(anyLong())).thenReturn(macAddress);
|
||||
when(nicProfile.getMacAddress()).thenReturn(macAddress);
|
||||
when(networkOfferingDao.isIpv6Supported(anyLong())).thenReturn(false);
|
||||
when(network.getDataCenterId()).thenReturn(1L);
|
||||
when(network.getAccountId()).thenReturn(5L);
|
||||
when(network.getVpcId()).thenReturn(51L);
|
||||
// when(account.getDomainId()).thenReturn(2L);
|
||||
when(dcDao.findById(anyLong())).thenReturn(Mockito.mock(DataCenterVO.class));
|
||||
when(accountDao.findById(anyLong())).thenReturn(Mockito.mock(AccountVO.class));
|
||||
when(vpcDao.findById(anyLong())).thenReturn(Mockito.mock(VpcVO.class));
|
||||
when(domainDao.findById(anyLong())).thenReturn(Mockito.mock(DomainVO.class));
|
||||
when(nicProfile.getIPv4Address()).thenReturn("10.1.13.10");
|
||||
when(nsxControllerUtils.sendNsxCommand(any(CreateNsxDhcpRelayConfigCommand.class),
|
||||
anyLong())).thenReturn(new NsxAnswer(new NsxCommand(), true, ""));
|
||||
|
||||
NicProfile profile = guru.allocate(network, nicProfile, vmProfile);
|
||||
|
||||
assertNotNull(profile);
|
||||
verify(nsxControllerUtils, times(1)).sendNsxCommand(any(CreateNsxDhcpRelayConfigCommand.class),
|
||||
anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNsxSegmentForVpc() {
|
||||
NetworkVO networkVO = Mockito.mock(NetworkVO.class);
|
||||
DataCenter dataCenter = Mockito.mock(DataCenter.class);
|
||||
|
||||
when(networkVO.getAccountId()).thenReturn(1L);
|
||||
when(nsxControllerUtils.sendNsxCommand(any(CreateNsxSegmentCommand.class),
|
||||
anyLong())).thenReturn(new NsxAnswer(new NsxCommand(), true, ""));
|
||||
guru.createNsxSegment(networkVO, dataCenter);
|
||||
verify(nsxControllerUtils, times(1)).sendNsxCommand(any(CreateNsxSegmentCommand.class),
|
||||
anyLong());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCreateNsxSegmentForIsolatedNetwork() {
|
||||
NetworkVO networkVO = Mockito.mock(NetworkVO.class);
|
||||
DataCenter dataCenter = Mockito.mock(DataCenter.class);
|
||||
|
||||
when(networkVO.getAccountId()).thenReturn(1L);
|
||||
when(networkVO.getVpcId()).thenReturn(null);
|
||||
when(nsxControllerUtils.sendNsxCommand(any(CreateNsxTier1GatewayCommand.class),
|
||||
anyLong())).thenReturn(new NsxAnswer(new NsxCommand(), true, ""));
|
||||
when(nsxControllerUtils.sendNsxCommand(any(CreateNsxSegmentCommand.class),
|
||||
anyLong())).thenReturn(new NsxAnswer(new NsxCommand(), true, ""));
|
||||
guru.createNsxSegment(networkVO, dataCenter);
|
||||
verify(nsxControllerUtils, times(1)).sendNsxCommand(any(CreateNsxTier1GatewayCommand.class),
|
||||
anyLong());
|
||||
verify(nsxControllerUtils, times(1)).sendNsxCommand(any(CreateNsxSegmentCommand.class),
|
||||
anyLong());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@ import com.cloud.dc.DataCenterVO;
|
|||
import com.cloud.dc.dao.DataCenterDao;
|
||||
import com.cloud.host.Host;
|
||||
import com.cloud.host.dao.HostDetailsDao;
|
||||
import com.cloud.network.Network;
|
||||
import com.cloud.network.Networks;
|
||||
import com.cloud.network.nsx.NsxProvider;
|
||||
import com.cloud.network.dao.NetworkDao;
|
||||
import com.cloud.network.dao.NetworkVO;
|
||||
|
|
@ -41,12 +43,14 @@ import org.mockito.Mock;
|
|||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyMap;
|
||||
|
|
@ -155,4 +159,16 @@ public class NsxProviderServiceImplTest {
|
|||
|
||||
assertTrue(nsxProviderService.deleteNsxController(1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNetworkStateValidation() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||
NetworkVO networkVO = Mockito.mock(NetworkVO.class);
|
||||
List<NetworkVO> networkVOList = List.of(networkVO);
|
||||
when(networkVO.getBroadcastDomainType()).thenReturn(Networks.BroadcastDomainType.NSX);
|
||||
when(networkVO.getState()).thenReturn(Network.State.Allocated);
|
||||
|
||||
NsxProviderServiceImpl nsxProviderService = new NsxProviderServiceImpl();
|
||||
|
||||
assertThrows(CloudRuntimeException.class, () -> nsxProviderService.validateNetworkState(networkVOList));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,177 @@
|
|||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
package org.apache.cloudstack.service;
|
||||
|
||||
import com.cloud.dc.VlanDetailsVO;
|
||||
import com.cloud.dc.dao.VlanDetailsDao;
|
||||
import com.cloud.deploy.DeploymentPlan;
|
||||
import com.cloud.exception.InsufficientAddressCapacityException;
|
||||
import com.cloud.exception.InsufficientVirtualNetworkCapacityException;
|
||||
import com.cloud.network.Network;
|
||||
import com.cloud.network.Networks;
|
||||
import com.cloud.network.dao.IPAddressDao;
|
||||
import com.cloud.network.dao.IPAddressVO;
|
||||
import com.cloud.network.guru.PublicNetworkGuru;
|
||||
import com.cloud.network.vpc.VpcOfferingVO;
|
||||
import com.cloud.network.vpc.VpcVO;
|
||||
import com.cloud.network.vpc.dao.VpcDao;
|
||||
import com.cloud.network.vpc.dao.VpcOfferingDao;
|
||||
import com.cloud.network.vpc.dao.VpcOfferingServiceMapDao;
|
||||
import com.cloud.offering.NetworkOffering;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.utils.net.Ip;
|
||||
import com.cloud.vm.NicProfile;
|
||||
import com.cloud.vm.VirtualMachineProfile;
|
||||
import org.apache.cloudstack.NsxAnswer;
|
||||
import org.apache.cloudstack.agent.api.CreateOrUpdateNsxTier1NatRuleCommand;
|
||||
import org.apache.cloudstack.agent.api.NsxCommand;
|
||||
import org.apache.cloudstack.api.ApiConstants;
|
||||
import org.apache.cloudstack.utils.NsxControllerUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.times;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class NsxPublicNetworkGuruTest {
|
||||
|
||||
NetworkOffering offering;
|
||||
|
||||
NsxPublicNetworkGuru guru;
|
||||
@Mock
|
||||
NsxServiceImpl nsxService;
|
||||
@Mock
|
||||
IPAddressDao ipAddressDao;
|
||||
@Mock
|
||||
VpcDao vpcDao;
|
||||
@Mock
|
||||
VlanDetailsDao vlanDetailsDao;
|
||||
@Mock
|
||||
VpcOfferingServiceMapDao vpcOfferingServiceMapDao;
|
||||
@Mock
|
||||
VpcOfferingDao vpcOfferingDao;
|
||||
@Mock
|
||||
NsxControllerUtils nsxControllerUtils;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
guru = new NsxPublicNetworkGuru();
|
||||
|
||||
ReflectionTestUtils.setField((PublicNetworkGuru) guru, "_ipAddressDao", ipAddressDao);
|
||||
ReflectionTestUtils.setField(guru, "vpcDao", vpcDao);
|
||||
ReflectionTestUtils.setField(guru, "vlanDetailsDao", vlanDetailsDao);
|
||||
ReflectionTestUtils.setField(guru, "vpcOfferingServiceMapDao", vpcOfferingServiceMapDao);
|
||||
ReflectionTestUtils.setField(guru, "nsxService", nsxService);
|
||||
ReflectionTestUtils.setField(guru, "vpcOfferingDao", vpcOfferingDao);
|
||||
ReflectionTestUtils.setField(guru, "nsxControllerUtils", nsxControllerUtils);
|
||||
|
||||
offering = Mockito.mock(NetworkOffering.class);
|
||||
when(offering.getTrafficType()).thenReturn(Networks.TrafficType.Public);
|
||||
when(offering.isForNsx()).thenReturn(true);
|
||||
when(offering.isSystemOnly()).thenReturn(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCanHandle() {
|
||||
Assert.assertTrue(guru.canHandle(offering));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCannotHandle() {
|
||||
NetworkOffering offering = Mockito.mock(NetworkOffering.class);
|
||||
|
||||
when(offering.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
|
||||
|
||||
Assert.assertFalse(guru.canHandle(offering));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDesign() {
|
||||
DeploymentPlan plan = Mockito.mock(DeploymentPlan.class);
|
||||
Network network = Mockito.mock(Network.class);
|
||||
Account account = Mockito.mock(Account.class);
|
||||
|
||||
// when(network.getTrafficType()).thenReturn(Networks.TrafficType.Public);
|
||||
|
||||
Network designedNetwork = guru.design(offering, plan, network, "net1", 1L, account);
|
||||
Assert.assertEquals(Networks.TrafficType.Public, designedNetwork.getTrafficType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDesign_whenOfferingIsForGuestTraffic() {
|
||||
DeploymentPlan plan = Mockito.mock(DeploymentPlan.class);
|
||||
Network network = Mockito.mock(Network.class);
|
||||
Account account = Mockito.mock(Account.class);
|
||||
|
||||
when(offering.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
|
||||
|
||||
Network designedNetwork = guru.design(offering, plan, network, "net1", 1L, account);
|
||||
Assert.assertNull(designedNetwork);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllocate() throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
|
||||
String publicIpVR = "10.1.12.10";
|
||||
String publicIpNSX = "10.1.13.10";
|
||||
Network network = Mockito.mock(Network.class);
|
||||
NicProfile profile = Mockito.mock(NicProfile.class);
|
||||
VirtualMachineProfile vmProfile = Mockito.mock(VirtualMachineProfile.class);
|
||||
IPAddressVO srcNatIpOnVR = new IPAddressVO(new Ip(publicIpVR), 2L , 0xaabbccddeeffL, 2L, true);
|
||||
srcNatIpOnVR.setVpcId(12L);
|
||||
IPAddressVO srcNatIpOnNSX = new IPAddressVO(new Ip(publicIpNSX), 2L , 0xaabbccddeeffL, 3L, true);
|
||||
srcNatIpOnNSX.setVpcId(12L);
|
||||
VpcVO vpcVO = Mockito.mock(VpcVO.class);
|
||||
List<IPAddressVO> sourceNatList = List.of(srcNatIpOnNSX);
|
||||
VlanDetailsVO vlanDetailVO = new VlanDetailsVO(3L,ApiConstants.NSX_DETAIL_KEY, "true", false);
|
||||
VpcOfferingVO vpcOffering = Mockito.mock(VpcOfferingVO.class);
|
||||
|
||||
|
||||
when(profile.getIPv4Address()).thenReturn(publicIpVR);
|
||||
when(ipAddressDao.findByIp(anyString())).thenReturn(srcNatIpOnVR);
|
||||
when(vpcDao.findById(anyLong())).thenReturn(vpcVO);
|
||||
when(ipAddressDao.listByAssociatedVpc(12L, true)).thenReturn(sourceNatList);
|
||||
when(vlanDetailsDao.findDetail(anyLong(), anyString())).thenReturn(vlanDetailVO);
|
||||
when(vpcVO.getVpcOfferingId()).thenReturn(12L);
|
||||
when(vpcVO.getName()).thenReturn("nsxVPCNet");
|
||||
when(vpcOfferingServiceMapDao.areServicesSupportedByVpcOffering(anyLong(), any())).thenReturn(true);
|
||||
when(nsxService.createVpcNetwork(anyLong(), anyLong(), anyLong(), anyLong(), anyString(), anyBoolean())).thenReturn(true);
|
||||
when(vpcOfferingDao.findById(anyLong())).thenReturn(vpcOffering);
|
||||
when(vpcOffering.getNsxMode()).thenReturn(NetworkOffering.NsxMode.NATTED.name());
|
||||
when(nsxControllerUtils.sendNsxCommand(any(CreateOrUpdateNsxTier1NatRuleCommand.class),
|
||||
anyLong())).thenReturn(new NsxAnswer(new NsxCommand(), true, ""));
|
||||
|
||||
guru.allocate(network, profile, vmProfile);
|
||||
|
||||
verify(nsxControllerUtils, times(1)).sendNsxCommand(any(CreateOrUpdateNsxTier1NatRuleCommand.class),
|
||||
anyLong());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -118,7 +118,7 @@ public abstract class GuestNetworkGuru extends AdapterBase implements NetworkGur
|
|||
@Inject
|
||||
protected IpAddressManager _ipAddrMgr;
|
||||
@Inject
|
||||
NetworkOfferingDao networkOfferingDao;
|
||||
protected NetworkOfferingDao networkOfferingDao;
|
||||
@Inject
|
||||
Ipv6AddressManager ipv6AddressManager;
|
||||
@Inject
|
||||
|
|
|
|||
Loading…
Reference in New Issue