cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bfede...@apache.org
Subject [38/50] [abbrv] git commit: Unittests for NiciraNvpGuestNetworkGuru
Date Wed, 19 Dec 2012 22:04:28 GMT
Unittests for NiciraNvpGuestNetworkGuru

These unittests do not depend on the componentlocator but instead are
completely dependent on mock objects. This ensures that they can be run
standalone without any requirements on the environment.

Includes some fixes to NiciraNvpGuestNetworkGuru and GuestNetworkGuru


Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/2235776a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/2235776a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/2235776a

Branch: refs/heads/ui-plugins
Commit: 2235776aedf86971179d50b6b8346714d01f7e5f
Parents: 5df88d4
Author: Hugo Trippaers <trippie@gmail.com>
Authored: Fri Dec 14 10:41:15 2012 +0100
Committer: Hugo Trippaers <trippie@gmail.com>
Committed: Fri Dec 14 10:41:36 2012 +0100

----------------------------------------------------------------------
 .../network/guru/NiciraNvpGuestNetworkGuru.java    |    8 +-
 .../guru/NiciraNvpGuestNetworkGuruTest.java        |  327 +++++++++++++++
 .../com/cloud/network/guru/GuestNetworkGuru.java   |    8 +-
 3 files changed, 339 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/2235776a/plugins/network-elements/nicira-nvp/src/com/cloud/network/guru/NiciraNvpGuestNetworkGuru.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/src/com/cloud/network/guru/NiciraNvpGuestNetworkGuru.java
b/plugins/network-elements/nicira-nvp/src/com/cloud/network/guru/NiciraNvpGuestNetworkGuru.java
index a386ae5..1046a5a 100644
--- a/plugins/network-elements/nicira-nvp/src/com/cloud/network/guru/NiciraNvpGuestNetworkGuru.java
+++ b/plugins/network-elements/nicira-nvp/src/com/cloud/network/guru/NiciraNvpGuestNetworkGuru.java
@@ -39,6 +39,7 @@ import com.cloud.host.HostVO;
 import com.cloud.host.dao.HostDao;
 import com.cloud.host.dao.HostDetailsDao;
 import com.cloud.network.Network;
+import com.cloud.network.Network.Service;
 import com.cloud.network.NetworkManager;
 import com.cloud.network.NetworkProfile;
 import com.cloud.network.NetworkVO;
@@ -50,9 +51,11 @@ import com.cloud.network.PhysicalNetwork;
 import com.cloud.network.PhysicalNetwork.IsolationMethod;
 import com.cloud.network.PhysicalNetworkVO;
 import com.cloud.network.dao.NetworkDao;
+import com.cloud.network.dao.NetworkServiceMapDao;
 import com.cloud.network.dao.NiciraNvpDao;
 import com.cloud.network.dao.PhysicalNetworkDao;
 import com.cloud.offering.NetworkOffering;
+import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
 import com.cloud.resource.ResourceManager;
 import com.cloud.user.Account;
 import com.cloud.user.dao.AccountDao;
@@ -88,6 +91,8 @@ public class NiciraNvpGuestNetworkGuru extends GuestNetworkGuru {
     AgentManager _agentMgr;
     @Inject
     HostDetailsDao _hostDetailsDao;
+    @Inject
+    NetworkOfferingServiceMapDao _ntwkOfferingSrvcDao;
     
     public NiciraNvpGuestNetworkGuru() {
         super();
@@ -100,7 +105,8 @@ public class NiciraNvpGuestNetworkGuru extends GuestNetworkGuru {
         if (networkType == NetworkType.Advanced 
                 && isMyTrafficType(offering.getTrafficType()) 
                 && offering.getGuestType() == Network.GuestType.Isolated
-                && isMyIsolationMethod(physicalNetwork)) {
+                && isMyIsolationMethod(physicalNetwork)
+                && _ntwkOfferingSrvcDao.areServicesSupportedByNetworkOffering(offering.getId(),
Service.Connectivity)) {
             return true;
         } else {
             s_logger.trace("We only take care of Guest networks of type   " + GuestType.Isolated
+ " in zone of type " + NetworkType.Advanced);

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/2235776a/plugins/network-elements/nicira-nvp/test/com/cloud/network/guru/NiciraNvpGuestNetworkGuruTest.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nicira-nvp/test/com/cloud/network/guru/NiciraNvpGuestNetworkGuruTest.java
b/plugins/network-elements/nicira-nvp/test/com/cloud/network/guru/NiciraNvpGuestNetworkGuruTest.java
new file mode 100644
index 0000000..7467439
--- /dev/null
+++ b/plugins/network-elements/nicira-nvp/test/com/cloud/network/guru/NiciraNvpGuestNetworkGuruTest.java
@@ -0,0 +1,327 @@
+package com.cloud.network.guru;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Collections;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+import com.cloud.agent.AgentManager;
+import com.cloud.agent.api.Command;
+import com.cloud.agent.api.CreateLogicalSwitchAnswer;
+import com.cloud.agent.api.DeleteLogicalSwitchAnswer;
+import com.cloud.dc.DataCenter;
+import com.cloud.dc.DataCenterVO;
+import com.cloud.dc.DataCenter.NetworkType;
+import com.cloud.dc.dao.DataCenterDao;
+import com.cloud.deploy.DeployDestination;
+import com.cloud.deploy.DeploymentPlan;
+import com.cloud.domain.Domain;
+import com.cloud.exception.InsufficientVirtualNetworkCapcityException;
+import com.cloud.host.HostVO;
+import com.cloud.host.dao.HostDao;
+import com.cloud.network.Network;
+import com.cloud.network.Network.GuestType;
+import com.cloud.network.Network.Service;
+import com.cloud.network.Network.State;
+import com.cloud.network.NetworkManager;
+import com.cloud.network.NetworkProfile;
+import com.cloud.network.NetworkVO;
+import com.cloud.network.Networks.BroadcastDomainType;
+import com.cloud.network.Networks.TrafficType;
+import com.cloud.network.NiciraNvpDeviceVO;
+import com.cloud.network.PhysicalNetworkVO;
+import com.cloud.network.dao.NetworkDao;
+import com.cloud.network.dao.NiciraNvpDao;
+import com.cloud.network.dao.PhysicalNetworkDao;
+import com.cloud.offering.NetworkOffering;
+import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
+import com.cloud.user.Account;
+import com.cloud.vm.ReservationContext;
+
+import edu.emory.mathcs.backport.java.util.Arrays;
+
+public class NiciraNvpGuestNetworkGuruTest {
+	PhysicalNetworkDao physnetdao = mock (PhysicalNetworkDao.class);
+	NiciraNvpDao nvpdao = mock(NiciraNvpDao.class);
+	DataCenterDao dcdao = mock(DataCenterDao.class);
+	NetworkOfferingServiceMapDao nosd = mock(NetworkOfferingServiceMapDao.class);
+	AgentManager agentmgr = mock (AgentManager.class);
+	NetworkManager netmgr = mock (NetworkManager.class);
+	HostDao hostdao = mock (HostDao.class);
+	NetworkDao netdao = mock(NetworkDao.class);
+	NiciraNvpGuestNetworkGuru guru;
+	
+	
+	@Before
+	public void setUp() {
+		guru = new NiciraNvpGuestNetworkGuru();
+		((GuestNetworkGuru) guru)._physicalNetworkDao = physnetdao;
+		guru._physicalNetworkDao = physnetdao;
+		guru._niciraNvpDao = nvpdao;
+		guru._dcDao = dcdao;
+		guru._ntwkOfferingSrvcDao = nosd;
+		guru._networkMgr = netmgr;
+		guru._hostDao = hostdao;
+		guru._agentMgr = agentmgr;
+		guru._networkDao = netdao;
+		
+		DataCenterVO dc = mock(DataCenterVO.class);
+		when(dc.getNetworkType()).thenReturn(NetworkType.Advanced);
+		when(dc.getGuestNetworkCidr()).thenReturn("10.1.1.1/24");
+		
+		when(dcdao.findById((Long) any())).thenReturn((DataCenterVO) dc);
+	}
+	
+	@Test
+	public void testCanHandle() {
+		NetworkOffering offering = mock(NetworkOffering.class);
+		when(offering.getId()).thenReturn(42L);
+		when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
+		when(offering.getGuestType()).thenReturn(GuestType.Isolated);
+	
+		PhysicalNetworkVO physnet = mock(PhysicalNetworkVO.class);
+		when(physnet.getIsolationMethods()).thenReturn(Arrays.asList(new String[] { "STT" }));
+		when(physnet.getId()).thenReturn(42L);
+		
+		when(nosd.areServicesSupportedByNetworkOffering(42L, Service.Connectivity)).thenReturn(true);
+		
+		assertTrue(guru.canHandle(offering, NetworkType.Advanced, physnet) == true);
+		
+		// Not supported TrafficType != Guest
+		when(offering.getTrafficType()).thenReturn(TrafficType.Management);
+		assertFalse(guru.canHandle(offering, NetworkType.Advanced, physnet) == true);
+		
+		// Not supported: GuestType Shared
+		when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
+		when(offering.getGuestType()).thenReturn(GuestType.Shared);
+		assertFalse(guru.canHandle(offering, NetworkType.Advanced, physnet) == true);
+		
+		// Not supported: Basic networking
+		when(offering.getGuestType()).thenReturn(GuestType.Isolated);
+		assertFalse(guru.canHandle(offering, NetworkType.Basic, physnet) == true);
+		
+		// Not supported: IsolationMethod != STT
+		when(physnet.getIsolationMethods()).thenReturn(Arrays.asList(new String[] { "VLAN" }));
+		assertFalse(guru.canHandle(offering, NetworkType.Advanced, physnet) == true);
+		
+	}
+	
+	
+	@Test
+	public void testDesign() {
+		PhysicalNetworkVO physnet = mock(PhysicalNetworkVO.class);
+		when(physnetdao.findById((Long) any())).thenReturn(physnet);
+		when(physnet.getIsolationMethods()).thenReturn(Arrays.asList(new String[] { "STT" }));
+		when(physnet.getId()).thenReturn(42L);
+		
+		NiciraNvpDeviceVO device = mock(NiciraNvpDeviceVO.class);
+		when(nvpdao.listByPhysicalNetwork(42L)).thenReturn(Arrays.asList(new NiciraNvpDeviceVO[]
{ device }));
+		when(device.getId()).thenReturn(1L);
+		
+		NetworkOffering offering = mock(NetworkOffering.class);
+		when(offering.getId()).thenReturn(42L);
+		when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
+		when(offering.getGuestType()).thenReturn(GuestType.Isolated);
+		
+		when(nosd.areServicesSupportedByNetworkOffering(42L, Service.Connectivity)).thenReturn(true);
+		
+		DeploymentPlan plan = mock(DeploymentPlan.class);
+		Network network = mock(Network.class);
+		Account account = mock(Account.class);
+		
+		Network designednetwork = guru.design(offering, plan, network, account);
+		assertTrue(designednetwork != null);
+		assertTrue(designednetwork.getBroadcastDomainType() == BroadcastDomainType.Lswitch);		
+	}
+	
+	@Test
+	public void testDesignNoElementOnPhysicalNetwork() {
+		PhysicalNetworkVO physnet = mock(PhysicalNetworkVO.class);
+		when(physnetdao.findById((Long) any())).thenReturn(physnet);
+		when(physnet.getIsolationMethods()).thenReturn(Arrays.asList(new String[] { "STT" }));
+		when(physnet.getId()).thenReturn(42L);
+		
+		NiciraNvpDeviceVO device = mock(NiciraNvpDeviceVO.class);
+		when(nvpdao.listByPhysicalNetwork(42L)).thenReturn(Collections.<NiciraNvpDeviceVO>
emptyList());
+		
+		NetworkOffering offering = mock(NetworkOffering.class);
+		when(offering.getId()).thenReturn(42L);
+		when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
+		when(offering.getGuestType()).thenReturn(GuestType.Isolated);
+		
+		DeploymentPlan plan = mock(DeploymentPlan.class);
+		Network network = mock(Network.class);
+		Account account = mock(Account.class);
+		
+		Network designednetwork = guru.design(offering, plan, network, account);
+		assertTrue(designednetwork == null);	
+	}
+	
+	@Test
+	public void testDesignNoIsolationMethodSTT() {
+		PhysicalNetworkVO physnet = mock(PhysicalNetworkVO.class);
+		when(physnetdao.findById((Long) any())).thenReturn(physnet);
+		when(physnet.getIsolationMethods()).thenReturn(Arrays.asList(new String[] { "VLAN" }));
+		when(physnet.getId()).thenReturn(42L);
+		
+		NiciraNvpDeviceVO device = mock(NiciraNvpDeviceVO.class);
+		when(nvpdao.listByPhysicalNetwork(42L)).thenReturn(Collections.<NiciraNvpDeviceVO>
emptyList());
+		
+		NetworkOffering offering = mock(NetworkOffering.class);
+		when(offering.getId()).thenReturn(42L);
+		when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
+		when(offering.getGuestType()).thenReturn(GuestType.Isolated);
+		
+		DeploymentPlan plan = mock(DeploymentPlan.class);
+		Network network = mock(Network.class);
+		Account account = mock(Account.class);
+		
+		Network designednetwork = guru.design(offering, plan, network, account);
+		assertTrue(designednetwork == null);			
+	}
+	
+	@Test
+	public void testDesignNoConnectivityInOffering() {
+		PhysicalNetworkVO physnet = mock(PhysicalNetworkVO.class);
+		when(physnetdao.findById((Long) any())).thenReturn(physnet);
+		when(physnet.getIsolationMethods()).thenReturn(Arrays.asList(new String[] { "STT" }));
+		when(physnet.getId()).thenReturn(42L);
+		
+		NiciraNvpDeviceVO device = mock(NiciraNvpDeviceVO.class);
+		when(nvpdao.listByPhysicalNetwork(42L)).thenReturn(Arrays.asList(new NiciraNvpDeviceVO[]
{ device }));
+		when(device.getId()).thenReturn(1L);
+		
+		NetworkOffering offering = mock(NetworkOffering.class);
+		when(offering.getId()).thenReturn(42L);
+		when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
+		when(offering.getGuestType()).thenReturn(GuestType.Isolated);
+		
+		when(nosd.areServicesSupportedByNetworkOffering(42L, Service.Connectivity)).thenReturn(false);
+		
+		DeploymentPlan plan = mock(DeploymentPlan.class);
+		Network network = mock(Network.class);
+		Account account = mock(Account.class);
+		
+		Network designednetwork = guru.design(offering, plan, network, account);
+		assertTrue(designednetwork == null);		
+	}
+	
+	@Test
+	public void testImplement() throws InsufficientVirtualNetworkCapcityException {
+		PhysicalNetworkVO physnet = mock(PhysicalNetworkVO.class);
+		when(physnetdao.findById((Long) any())).thenReturn(physnet);
+		when(physnet.getIsolationMethods()).thenReturn(Arrays.asList(new String[] { "STT" }));
+		when(physnet.getId()).thenReturn(42L);
+		
+		NiciraNvpDeviceVO device = mock(NiciraNvpDeviceVO.class);
+		when(nvpdao.listByPhysicalNetwork(42L)).thenReturn(Arrays.asList(new NiciraNvpDeviceVO[]
{ device }));
+		when(device.getId()).thenReturn(1L);
+		
+		NetworkOffering offering = mock(NetworkOffering.class);
+		when(offering.getId()).thenReturn(42L);
+		when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
+		when(offering.getGuestType()).thenReturn(GuestType.Isolated);
+		
+		when(nosd.areServicesSupportedByNetworkOffering(42L, Service.Connectivity)).thenReturn(false);
+		
+		DeploymentPlan plan = mock(DeploymentPlan.class);
+		
+		NetworkVO network = mock(NetworkVO.class);
+		when(network.getName()).thenReturn("testnetwork");
+		when(network.getState()).thenReturn(State.Implementing);
+		
+		DeployDestination dest = mock(DeployDestination.class);
+		
+		DataCenter dc = mock(DataCenter.class);
+		when(dest.getDataCenter()).thenReturn(dc);
+		
+		HostVO niciraHost = mock(HostVO.class);
+		when(hostdao.findById(anyLong())).thenReturn(niciraHost);
+		when(niciraHost.getDetail("transportzoneuuid")).thenReturn("aaaa");
+		when(niciraHost.getDetail("transportzoneisotype")).thenReturn("stt");
+		when(niciraHost.getId()).thenReturn(42L);
+		
+		when(netmgr.findPhysicalNetworkId(anyLong(), (String) any(), (TrafficType) any())).thenReturn(42L);
+		Domain dom = mock(Domain.class);
+		when(dom.getName()).thenReturn("domain");
+		Account acc = mock(Account.class);
+		when(acc.getAccountName()).thenReturn("accountname");
+		ReservationContext res = mock(ReservationContext.class);
+		when(res.getDomain()).thenReturn(dom);
+		when(res.getAccount()).thenReturn(acc);
+		
+		CreateLogicalSwitchAnswer answer = mock(CreateLogicalSwitchAnswer.class);
+		when(answer.getResult()).thenReturn(true);
+ 		when(agentmgr.easySend(eq(42L), (Command)any())).thenReturn(answer);		
+
+ 		Network implementednetwork = guru.implement(network, offering, dest, res);
+		assertTrue(implementednetwork != null);		
+		verify(agentmgr, times(1)).easySend(eq(42L), (Command)any());
+	}
+	
+	@Test
+	public void testShutdown() throws InsufficientVirtualNetworkCapcityException, URISyntaxException
{
+		PhysicalNetworkVO physnet = mock(PhysicalNetworkVO.class);
+		when(physnetdao.findById((Long) any())).thenReturn(physnet);
+		when(physnet.getIsolationMethods()).thenReturn(Arrays.asList(new String[] { "STT" }));
+		when(physnet.getId()).thenReturn(42L);
+		
+		NiciraNvpDeviceVO device = mock(NiciraNvpDeviceVO.class);
+		when(nvpdao.listByPhysicalNetwork(42L)).thenReturn(Arrays.asList(new NiciraNvpDeviceVO[]
{ device }));
+		when(device.getId()).thenReturn(1L);
+		
+		NetworkOffering offering = mock(NetworkOffering.class);
+		when(offering.getId()).thenReturn(42L);
+		when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
+		when(offering.getGuestType()).thenReturn(GuestType.Isolated);
+		
+		when(nosd.areServicesSupportedByNetworkOffering(42L, Service.Connectivity)).thenReturn(false);
+		
+		DeploymentPlan plan = mock(DeploymentPlan.class);
+		
+		NetworkVO network = mock(NetworkVO.class);
+		when(network.getName()).thenReturn("testnetwork");
+		when(network.getState()).thenReturn(State.Implementing);
+		when(network.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Lswitch);
+		when(network.getBroadcastUri()).thenReturn(new URI("lswitch:aaaaa"));
+		when(network.getPhysicalNetworkId()).thenReturn(42L);
+		when(netdao.findById(42L)).thenReturn(network);
+		
+		DeployDestination dest = mock(DeployDestination.class);
+		
+		DataCenter dc = mock(DataCenter.class);
+		when(dest.getDataCenter()).thenReturn(dc);
+		
+		HostVO niciraHost = mock(HostVO.class);
+		when(hostdao.findById(anyLong())).thenReturn(niciraHost);
+		when(niciraHost.getDetail("transportzoneuuid")).thenReturn("aaaa");
+		when(niciraHost.getDetail("transportzoneisotype")).thenReturn("stt");
+		when(niciraHost.getId()).thenReturn(42L);
+		
+		when(netmgr.findPhysicalNetworkId(anyLong(), (String) any(), (TrafficType) any())).thenReturn(42L);
+		Domain dom = mock(Domain.class);
+		when(dom.getName()).thenReturn("domain");
+		Account acc = mock(Account.class);
+		when(acc.getAccountName()).thenReturn("accountname");
+		ReservationContext res = mock(ReservationContext.class);
+		when(res.getDomain()).thenReturn(dom);
+		when(res.getAccount()).thenReturn(acc);
+		
+		DeleteLogicalSwitchAnswer answer = mock(DeleteLogicalSwitchAnswer.class);
+		when(answer.getResult()).thenReturn(true);
+ 		when(agentmgr.easySend(eq(42L), (Command)any())).thenReturn(answer);		
+
+ 		NetworkProfile implementednetwork = mock(NetworkProfile.class);
+ 		when(implementednetwork.getId()).thenReturn(42L);
+ 		when(implementednetwork.getBroadcastUri()).thenReturn(new URI("lswitch:aaaa"));
+ 		when(offering.getSpecifyVlan()).thenReturn(false);
+ 		
+ 		guru.shutdown(implementednetwork, offering);
+		verify(agentmgr, times(1)).easySend(eq(42L), (Command)any());
+		verify(implementednetwork, times(1)).setBroadcastUri(null);
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/2235776a/server/src/com/cloud/network/guru/GuestNetworkGuru.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/guru/GuestNetworkGuru.java b/server/src/com/cloud/network/guru/GuestNetworkGuru.java
index cebfb08..2a8a092 100755
--- a/server/src/com/cloud/network/guru/GuestNetworkGuru.java
+++ b/server/src/com/cloud/network/guru/GuestNetworkGuru.java
@@ -442,15 +442,17 @@ public abstract class GuestNetworkGuru extends AdapterBase implements
NetworkGur
 
     @Override
     public void shutdown(NetworkProfile profile, NetworkOffering offering) {
-        s_logger.debug("Releasing vnet for the network id=" + profile.getId());
-        if (profile.getBroadcastUri() != null && !offering.getSpecifyVlan()) {
+        
+        if (profile.getBroadcastDomainType() == BroadcastDomainType.Vlan && 
+        		profile.getBroadcastUri() != null && !offering.getSpecifyVlan()) {
+        	s_logger.debug("Releasing vnet for the network id=" + profile.getId());
             _dcDao.releaseVnet(profile.getBroadcastUri().getHost(), profile.getDataCenterId(),

                     profile.getPhysicalNetworkId(), profile.getAccountId(), profile.getReservationId());
             EventUtils.saveEvent(UserContext.current().getCallerUserId(), profile.getAccountId(),

                     EventVO.LEVEL_INFO, EventTypes.EVENT_ZONE_VLAN_RELEASE, "Released Zone
Vlan: "
                     +profile.getBroadcastUri().getHost()+" for Network: "+profile.getId(),
0);
-            profile.setBroadcastUri(null);
         }
+        profile.setBroadcastUri(null);
     }
 
     @Override


Mime
View raw message