More tests for VnmcResource class

This commit is contained in:
Koushik Das 2013-03-21 10:50:29 +05:30
parent 9e9c179212
commit e81ab3a2f4
2 changed files with 161 additions and 14 deletions

View File

@ -229,7 +229,7 @@ public class CiscoVnmcResource implements ServerResource {
/*
* Login
*/
public boolean refreshVnmcConnection() {
private boolean refreshVnmcConnection() {
boolean ret = false;
try {
ret = _connection.login();
@ -360,7 +360,7 @@ public class CiscoVnmcResource implements ServerResource {
throw new Exception("Failed to delete ACL ingress rule in VNMC for guest network with vlan " + vlanId);
}
} else {
if (rule.getTrafficType() == TrafficType.Ingress) {
if (rule.getTrafficType() == TrafficType.Ingress) {
String[] externalIpRange = getIpRangeFromCidr(rule.getSourceCidrList().get(0));
if (!_connection.createTenantVDCIngressAclRule(tenant,
Long.toString(rule.getId()), policyIdentifier,
@ -368,9 +368,9 @@ public class CiscoVnmcResource implements ServerResource {
Integer.toString(rule.getSrcPortRange()[0]), Integer.toString(rule.getSrcPortRange()[1]), publicIp)) {
throw new Exception("Failed to create ACL ingress rule in VNMC for guest network with vlan " + vlanId);
}
} else {
} else {
// TODO for egress
}
}
}
}
}
@ -384,7 +384,7 @@ public class CiscoVnmcResource implements ServerResource {
return new Answer(cmd, false, msg);
}
return new Answer(cmd);
return new Answer(cmd, true, "Success");
}
/*

View File

@ -20,8 +20,10 @@ import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.naming.ConfigurationException;
@ -30,15 +32,28 @@ import org.junit.Before;
import org.junit.Test;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.CreateLogicalEdgeFirewallCommand;
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.SetFirewallRulesCommand;
import com.cloud.agent.api.routing.SetPortForwardingRulesCommand;
import com.cloud.agent.api.routing.SetSourceNatCommand;
import com.cloud.agent.api.routing.SetStaticNatRulesCommand;
import com.cloud.agent.api.to.FirewallRuleTO;
import com.cloud.agent.api.to.IpAddressTO;
import com.cloud.agent.api.to.PortForwardingRuleTO;
import com.cloud.agent.api.to.StaticNatRuleTO;
import com.cloud.dc.Vlan;
import com.cloud.host.Host;
import com.cloud.network.IpAddress;
import com.cloud.network.cisco.CiscoVnmcConnectionImpl;
import com.cloud.network.rules.FirewallRule;
import com.cloud.network.rules.PortForwardingRule;
import com.cloud.network.rules.StaticNat;
import com.cloud.network.rules.FirewallRule.Purpose;
import com.cloud.network.rules.FirewallRule.TrafficType;
import com.cloud.network.rules.FirewallRuleVO;
import com.cloud.utils.exception.ExecutionException;
public class CiscoVnmcResourceTest {
@ -63,7 +78,7 @@ public class CiscoVnmcResourceTest {
}
@Test(expected=ConfigurationException.class)
public void resourceConfigureFailure() throws ConfigurationException, ExecutionException {
public void resourceConfigureFailure() throws ConfigurationException {
_resource.configure("CiscoVnmcResource", Collections.<String,Object>emptyMap());
}
@ -105,7 +120,7 @@ public class CiscoVnmcResourceTest {
}
@Test
public void testSourceNat() throws ConfigurationException, ExecutionException, Exception {
public void testSourceNat() throws ConfigurationException, 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);
@ -116,17 +131,149 @@ public class CiscoVnmcResourceTest {
_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);
when(_connection.createTenantVDCNatPolicySet(anyString())).thenReturn(true);
when(_connection.createTenantVDCSourceNatPolicy(anyString(), anyString())).thenReturn(true);
when(_connection.createTenantVDCSourceNatPolicyRef(anyString(), anyString())).thenReturn(true);
when(_connection.createTenantVDCSourceNatIpPool(anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.createTenantVDCSourceNatRule(anyString(), anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.associateNatPolicySet(anyString())).thenReturn(true);
Answer answer = _resource.executeRequest(cmd);
System.out.println(answer.getDetails());
assertTrue(answer.getResult());
}
}
@Test
public void testFirewall() throws ConfigurationException, Exception {
long vlanId = 123;
List<FirewallRuleTO> rules = new ArrayList<FirewallRuleTO>();
FirewallRuleTO active = new FirewallRuleTO(1,
null, "1.2.3.4", "tcp", 22, 22, false, false,
FirewallRule.Purpose.Firewall, null, null, null);
rules.add(active);
FirewallRuleTO revoked = new FirewallRuleTO(1,
null, "1.2.3.4", "tcp", 22, 22, true, false,
FirewallRule.Purpose.Firewall, null, null, null);
rules.add(revoked);
SetFirewallRulesCommand cmd = new SetFirewallRulesCommand(rules);
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.createTenantVDCAclPolicySet(anyString(), anyBoolean())).thenReturn(true);
when(_connection.createTenantVDCAclPolicy(anyString(), anyString(), anyBoolean())).thenReturn(true);
when(_connection.createTenantVDCAclPolicyRef(anyString(), anyString(), anyBoolean())).thenReturn(true);
when(_connection.deleteTenantVDCAclRule(anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.createTenantVDCIngressAclRule(
anyString(), anyString(), anyString(),
anyString(), anyString(), anyString(),
anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.associateAclPolicySet(anyString())).thenReturn(true);
Answer answer = _resource.executeRequest(cmd);
System.out.println(answer.getDetails());
assertTrue(answer.getResult());
}
@Test
public void testStaticNat() throws ConfigurationException, Exception {
long vlanId = 123;
List<StaticNatRuleTO> rules = new ArrayList<StaticNatRuleTO>();
StaticNatRuleTO active = new StaticNatRuleTO(0, "1.2.3.4", null,
null, "5.6.7.8", null, null, null, false, false);
rules.add(active);
StaticNatRuleTO revoked = new StaticNatRuleTO(0, "1.2.3.4", null,
null, "5.6.7.8", null, null, null, true, false);
rules.add(revoked);
SetStaticNatRulesCommand cmd = new SetStaticNatRulesCommand(rules, null);
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.createTenantVDCNatPolicySet(anyString())).thenReturn(true);
when(_connection.createTenantVDCAclPolicySet(anyString(), anyBoolean())).thenReturn(true);
when(_connection.createTenantVDCDNatPolicy(anyString(), anyString())).thenReturn(true);
when(_connection.createTenantVDCDNatPolicyRef(anyString(), anyString())).thenReturn(true);
when(_connection.createTenantVDCAclPolicy(anyString(), anyString(), anyBoolean())).thenReturn(true);
when(_connection.createTenantVDCAclPolicyRef(anyString(), anyString(), anyBoolean())).thenReturn(true);
when(_connection.deleteTenantVDCDNatRule(anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.deleteTenantVDCAclRule(anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.createTenantVDCDNatIpPool(anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.createTenantVDCDNatRule(anyString(),
anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.createTenantVDCIngressAclRuleForDNat(anyString(),
anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.associateNatPolicySet(anyString())).thenReturn(true);
Answer answer = _resource.executeRequest(cmd);
System.out.println(answer.getDetails());
assertTrue(answer.getResult());
}
@Test
public void testPortForwarding() throws ConfigurationException, Exception {
long vlanId = 123;
List<PortForwardingRuleTO> rules = new ArrayList<PortForwardingRuleTO>();
PortForwardingRuleTO active = new PortForwardingRuleTO(1, "1.2.3.4", 22, 22,
"5.6.7.8", 22, 22, "tcp", false, false);
rules.add(active);
PortForwardingRuleTO revoked = new PortForwardingRuleTO(1, "1.2.3.4", 22, 22,
"5.6.7.8", 22, 22, "tcp", false, false);
rules.add(revoked);
SetPortForwardingRulesCommand cmd = new SetPortForwardingRulesCommand(rules);
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.createTenantVDCNatPolicySet(anyString())).thenReturn(true);
when(_connection.createTenantVDCAclPolicySet(anyString(), anyBoolean())).thenReturn(true);
when(_connection.createTenantVDCPFPolicy(anyString(), anyString())).thenReturn(true);
when(_connection.createTenantVDCPFPolicyRef(anyString(), anyString())).thenReturn(true);
when(_connection.createTenantVDCAclPolicy(anyString(), anyString(), anyBoolean())).thenReturn(true);
when(_connection.createTenantVDCAclPolicyRef(anyString(), anyString(), anyBoolean())).thenReturn(true);
when(_connection.deleteTenantVDCPFRule(anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.deleteTenantVDCAclRule(anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.createTenantVDCPFIpPool(anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.createTenantVDCPFPortPool(anyString(), anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.createTenantVDCPFRule(anyString(),
anyString(), anyString(), anyString(),
anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.createTenantVDCIngressAclRuleForPF(anyString(),
anyString(), anyString(), anyString(),
anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.associateNatPolicySet(anyString())).thenReturn(true);
Answer answer = _resource.executeRequest(cmd);
System.out.println(answer.getDetails());
assertTrue(answer.getResult());
}
@Test
public void testCreateEdgeFirewall() throws ConfigurationException, Exception {
long vlanId = 123;
CreateLogicalEdgeFirewallCommand cmd = new CreateLogicalEdgeFirewallCommand(vlanId, "1.2.3.4", "5.6.7.8", "255.255.255.0", "255.255.255.0");
cmd.getPublicGateways().add("1.1.1.1");
cmd.getPublicGateways().add("2.2.2.2");
_resource.configure("CiscoVnmcResource", _parameters);
_resource.setConnection(_connection);
when(_connection.createTenant(anyString())).thenReturn(true);
when(_connection.createTenantVDC(anyString())).thenReturn(true);
when(_connection.createTenantVDCEdgeSecurityProfile(anyString())).thenReturn(true);
when(_connection.createTenantVDCEdgeDeviceProfile(anyString())).thenReturn(true);
when(_connection.createTenantVDCEdgeStaticRoutePolicy(anyString())).thenReturn(true);
when(_connection.createTenantVDCEdgeStaticRoute(anyString(), anyString(), anyString(), anyString())).thenReturn(true);
when(_connection.associateTenantVDCEdgeStaticRoutePolicy(anyString())).thenReturn(true);
when(_connection.createEdgeFirewall(anyString(), anyString(), anyString(), anyString(), anyString())).thenReturn(true);
Answer answer = _resource.executeRequest(cmd);
System.out.println(answer.getDetails());
assertTrue(answer.getResult());
}
}