mirror of https://github.com/apache/cloudstack.git
Initial set of tests, will add more in subsequent commits
This commit is contained in:
parent
50bfcc1f75
commit
4f305c2beb
|
|
@ -73,7 +73,11 @@ public class CiscoVnmcResource implements ServerResource {
|
|||
private String _guid;
|
||||
private Integer _numRetries;
|
||||
|
||||
CiscoVnmcConnectionImpl _connection;
|
||||
private CiscoVnmcConnectionImpl _connection;
|
||||
|
||||
public void setConnection(CiscoVnmcConnectionImpl connection) {
|
||||
this._connection = connection;
|
||||
}
|
||||
|
||||
private final Logger s_logger = Logger.getLogger(CiscoVnmcResource.class);
|
||||
|
||||
|
|
@ -135,7 +139,7 @@ public class CiscoVnmcResource implements ServerResource {
|
|||
_password = (String) params.get("password");
|
||||
if (_password == null) {
|
||||
throw new ConfigurationException("Unable to find password");
|
||||
}
|
||||
}
|
||||
|
||||
_guid = (String)params.get("guid");
|
||||
if (_guid == null) {
|
||||
|
|
@ -148,9 +152,9 @@ public class CiscoVnmcResource implements ServerResource {
|
|||
|
||||
// Open a socket and login
|
||||
_connection = new CiscoVnmcConnectionImpl(_ip, _username, _password);
|
||||
if (!refreshVnmcConnection()) {
|
||||
throw new ConfigurationException("Unable to open a connection to the VNMC.");
|
||||
}
|
||||
//if (!refreshVnmcConnection()) {
|
||||
// throw new ConfigurationException("Unable to open a connection to the VNMC.");
|
||||
//}
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
|
|
@ -192,6 +196,9 @@ public class CiscoVnmcResource implements ServerResource {
|
|||
|
||||
@Override
|
||||
public PingCommand getCurrentStatus(final long id) {
|
||||
if (!refreshVnmcConnection()) {
|
||||
return null;
|
||||
}
|
||||
return new PingCommand(Host.Type.ExternalFirewall, id);
|
||||
}
|
||||
|
||||
|
|
@ -222,7 +229,7 @@ public class CiscoVnmcResource implements ServerResource {
|
|||
/*
|
||||
* Login
|
||||
*/
|
||||
private boolean refreshVnmcConnection() {
|
||||
public boolean refreshVnmcConnection() {
|
||||
boolean ret = false;
|
||||
try {
|
||||
ret = _connection.login();
|
||||
|
|
|
|||
|
|
@ -111,7 +111,7 @@ public class CiscoVnmcConnectionTest {
|
|||
public void testCreateTenantVDCEdgeDeviceRoute() {
|
||||
try {
|
||||
boolean response = connection.createTenantVDCEdgeStaticRoute(tenantName,
|
||||
"10.223.136.1", "Edge_Outside", "0.0.0.0", "0.0.0.0");
|
||||
"10.223.136.1", "0.0.0.0", "0.0.0.0");
|
||||
assertTrue(response);
|
||||
} catch (ExecutionException e) {
|
||||
// TODO Auto-generated catch block
|
||||
|
|
|
|||
|
|
@ -0,0 +1,121 @@
|
|||
// 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 com.cloud.network.element;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.internal.matchers.Any;
|
||||
|
||||
import com.cloud.api.response.CiscoVnmcResourceResponse;
|
||||
import com.cloud.configuration.ConfigurationManager;
|
||||
import com.cloud.dc.DataCenter;
|
||||
import com.cloud.deploy.DeployDestination;
|
||||
import com.cloud.domain.Domain;
|
||||
import com.cloud.exception.ConcurrentOperationException;
|
||||
import com.cloud.exception.InsufficientCapacityException;
|
||||
import com.cloud.exception.ResourceUnavailableException;
|
||||
import com.cloud.network.Network;
|
||||
import com.cloud.network.Network.GuestType;
|
||||
import com.cloud.network.Network.Provider;
|
||||
import com.cloud.network.Network.Service;
|
||||
import com.cloud.network.NetworkManager;
|
||||
import com.cloud.network.NetworkModel;
|
||||
import com.cloud.network.Networks.BroadcastDomainType;
|
||||
import com.cloud.network.Networks.TrafficType;
|
||||
import com.cloud.network.cisco.CiscoVnmcControllerVO;
|
||||
import com.cloud.network.dao.CiscoVnmcDao;
|
||||
import com.cloud.network.dao.CiscoVnmcDaoImpl;
|
||||
import com.cloud.network.dao.NetworkServiceMapDao;
|
||||
import com.cloud.offering.NetworkOffering;
|
||||
import com.cloud.resource.ResourceManager;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.vm.ReservationContext;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class CiscoVnmcElementTest {
|
||||
|
||||
CiscoVnmcElement _element = new CiscoVnmcElement();
|
||||
NetworkManager _networkMgr = mock(NetworkManager.class);
|
||||
NetworkModel _networkModel = mock(NetworkModel.class);
|
||||
NetworkServiceMapDao _ntwkSrvcDao = mock(NetworkServiceMapDao.class);
|
||||
ConfigurationManager _configMgr = mock(ConfigurationManager.class);
|
||||
CiscoVnmcDao _ciscoVnmcDao = mock(CiscoVnmcDao.class);
|
||||
|
||||
@Before
|
||||
public void setUp() throws ConfigurationException {
|
||||
_element._resourceMgr = mock(ResourceManager.class);
|
||||
_element._networkMgr = _networkMgr;
|
||||
_element._configMgr = _configMgr;
|
||||
_element._ciscoVnmcDao = _ciscoVnmcDao;
|
||||
|
||||
// Standard responses
|
||||
when(_networkModel.isProviderForNetwork(Provider.CiscoVnmc, 1L)).thenReturn(true);
|
||||
|
||||
_element.configure("CiscoVnmcTestElement", Collections.<String, Object> emptyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canHandleTest() {
|
||||
Network network = mock(Network.class);
|
||||
when(network.getId()).thenReturn(1L);
|
||||
when(network.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Vlan);
|
||||
assertTrue(_element.canHandle(network));
|
||||
|
||||
when(network.getBroadcastDomainType()).thenReturn(BroadcastDomainType.UnDecided);
|
||||
assertFalse(_element.canHandle(network));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void implementTest() throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
|
||||
Network network = mock(Network.class);
|
||||
when(network.getId()).thenReturn(1L);
|
||||
when(network.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Vlan);
|
||||
when(network.getDataCenterId()).thenReturn(1L);
|
||||
|
||||
NetworkOffering offering = mock(NetworkOffering.class);
|
||||
when(offering.getId()).thenReturn(1L);
|
||||
when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
|
||||
when(offering.getGuestType()).thenReturn(GuestType.Isolated);
|
||||
|
||||
DeployDestination dest = mock(DeployDestination.class);
|
||||
|
||||
Domain dom = mock(Domain.class);
|
||||
when(dom.getName()).thenReturn("domain");
|
||||
Account acc = mock(Account.class);
|
||||
when(acc.getAccountName()).thenReturn("accountname");
|
||||
ReservationContext context = mock(ReservationContext.class);
|
||||
when(context.getDomain()).thenReturn(dom);
|
||||
when(context.getAccount()).thenReturn(acc);
|
||||
|
||||
DataCenter dc = mock(DataCenter.class);
|
||||
when(_configMgr.getZone(network.getDataCenterId())).thenReturn(dc);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<CiscoVnmcControllerVO> devices = mock(List.class);
|
||||
when(devices.isEmpty()).thenReturn(false);
|
||||
when(_ciscoVnmcDao.listByPhysicalNetwork(network.getId())).thenReturn(devices);
|
||||
//assertTrue(_element.implement(network, offering, dest, context));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,132 @@
|
|||
// 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 com.cloud.network.resource;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.cloud.agent.api.Answer;
|
||||
import com.cloud.agent.api.PingCommand;
|
||||
import com.cloud.agent.api.StartupCommand;
|
||||
import com.cloud.agent.api.routing.NetworkElementCommand;
|
||||
import com.cloud.agent.api.routing.SetSourceNatCommand;
|
||||
import com.cloud.agent.api.to.IpAddressTO;
|
||||
import com.cloud.agent.api.to.PortForwardingRuleTO;
|
||||
import com.cloud.agent.api.to.StaticNatRuleTO;
|
||||
import com.cloud.host.Host;
|
||||
import com.cloud.network.cisco.CiscoVnmcConnectionImpl;
|
||||
import com.cloud.utils.exception.ExecutionException;
|
||||
|
||||
public class CiscoVnmcResourceTest {
|
||||
CiscoVnmcConnectionImpl _connection = mock(CiscoVnmcConnectionImpl.class);
|
||||
CiscoVnmcResource _resource;
|
||||
Map<String,Object> _parameters;
|
||||
|
||||
@Before
|
||||
public void setUp() throws ConfigurationException {
|
||||
_resource = new CiscoVnmcResource();
|
||||
|
||||
_parameters = new HashMap<String, Object>();
|
||||
_parameters.put("name", "CiscoVnmc");
|
||||
_parameters.put("zoneId", "1");
|
||||
_parameters.put("physicalNetworkId", "100");
|
||||
_parameters.put("ip", "1.2.3.4");
|
||||
_parameters.put("username", "admin");
|
||||
_parameters.put("password", "pass");
|
||||
_parameters.put("guid", "e8e13097-0a08-4e82-b0af-1101589ec3b8");
|
||||
_parameters.put("numretries", "3");
|
||||
_parameters.put("timeout", "300");
|
||||
}
|
||||
|
||||
@Test(expected=ConfigurationException.class)
|
||||
public void resourceConfigureFailure() throws ConfigurationException, ExecutionException {
|
||||
_resource.configure("CiscoVnmcResource", Collections.<String,Object>emptyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resourceConfigure() throws ConfigurationException {
|
||||
_resource.configure("CiscoVnmcResource", _parameters);
|
||||
assertTrue("CiscoVnmc".equals(_resource.getName()));
|
||||
assertTrue(_resource.getType() == Host.Type.ExternalFirewall);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitialization() throws ConfigurationException {
|
||||
_resource.configure("CiscoVnmcResource", _parameters);
|
||||
StartupCommand[] sc = _resource.initialize();
|
||||
assertTrue(sc.length ==1);
|
||||
assertTrue("e8e13097-0a08-4e82-b0af-1101589ec3b8".equals(sc[0].getGuid()));
|
||||
assertTrue("CiscoVnmc".equals(sc[0].getName()));
|
||||
assertTrue("1".equals(sc[0].getDataCenter()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPingCommandStatusOk() throws ConfigurationException, ExecutionException {
|
||||
_resource.configure("CiscoVnmcResource", _parameters);
|
||||
_resource.setConnection(_connection);
|
||||
when(_connection.login()).thenReturn(true);
|
||||
PingCommand ping = _resource.getCurrentStatus(1);
|
||||
assertTrue(ping != null);
|
||||
assertTrue(ping.getHostId() == 1);
|
||||
assertTrue(ping.getHostType() == Host.Type.ExternalFirewall);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPingCommandStatusFail() throws ConfigurationException, ExecutionException {
|
||||
_resource.configure("CiscoVnmcResource", _parameters);
|
||||
_resource.setConnection(_connection);
|
||||
when(_connection.login()).thenReturn(false);
|
||||
PingCommand ping = _resource.getCurrentStatus(1);
|
||||
assertTrue(ping == null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSourceNat() throws ConfigurationException, ExecutionException, Exception {
|
||||
long vlanId = 123;
|
||||
IpAddressTO ip = new IpAddressTO(1, "1.2.3.4", true, false,
|
||||
false, null, "1.2.3.1", "255.255.255.0", null, null, false);
|
||||
SetSourceNatCommand cmd = new SetSourceNatCommand(ip, true);
|
||||
cmd.setContextParam(NetworkElementCommand.GUEST_VLAN_TAG, Long.toString(vlanId));
|
||||
cmd.setContextParam(NetworkElementCommand.GUEST_NETWORK_CIDR, "1.2.3.4/32");
|
||||
|
||||
_resource.configure("CiscoVnmcResource", _parameters);
|
||||
_resource.setConnection(_connection);
|
||||
when(_connection.login()).thenReturn(true);
|
||||
when(_connection.createTenantVDCNatPolicySet((String)any())).thenReturn(true);
|
||||
when(_connection.createTenantVDCSourceNatPolicy((String)any(), (String)any())).thenReturn(true);
|
||||
when(_connection.createTenantVDCSourceNatPolicyRef((String)any(), (String)any())).thenReturn(true);
|
||||
when(_connection.createTenantVDCSourceNatIpPool((String)any(), (String)any(), (String)any())).thenReturn(true);
|
||||
when(_connection.createTenantVDCSourceNatRule((String)any(), (String)any(), (String)any(), (String)any())).thenReturn(true);
|
||||
when(_connection.associateNatPolicySet((String)any())).thenReturn(true);
|
||||
|
||||
Answer answer = _resource.executeRequest(cmd);
|
||||
System.out.println(answer.getDetails());
|
||||
assertTrue(answer.getResult());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue