http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpRequestWrapperTest.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpRequestWrapperTest.java b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpRequestWrapperTest.java new file mode 100644 index 0000000..7fe6287 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpRequestWrapperTest.java @@ -0,0 +1,255 @@ +// +// 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.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; + +import com.cloud.agent.api.Answer; +import com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterCommand; +import com.cloud.agent.api.CreateLogicalSwitchCommand; +import com.cloud.agent.api.DeleteLogicalRouterCommand; +import com.cloud.agent.api.DeleteLogicalSwitchCommand; +import com.cloud.agent.api.DeleteLogicalSwitchPortCommand; +import com.cloud.agent.api.MaintainCommand; +import com.cloud.agent.api.ReadyCommand; +import com.cloud.agent.api.UpdateLogicalSwitchPortCommand; +import com.cloud.network.nicira.LogicalRouterPort; +import com.cloud.network.nicira.LogicalSwitch; +import com.cloud.network.nicira.NiciraNvpApi; +import com.cloud.network.nicira.NiciraNvpApiException; +import com.cloud.network.nicira.NiciraNvpList; +import com.cloud.network.nicira.VifAttachment; + +public class NiciraNvpRequestWrapperTest { + + @Mock + private final NiciraNvpResource niciraNvpResource = Mockito.mock(NiciraNvpResource.class); + + @Test + public void testReadyCommandWrapper() { + final ReadyCommand command = new ReadyCommand(); + + final NiciraNvpRequestWrapper wrapper = NiciraNvpRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, niciraNvpResource); + + assertTrue(answer.getResult()); + } + + @Test + public void testMaintainCommandWrapper() { + final MaintainCommand command = new MaintainCommand(); + + final NiciraNvpRequestWrapper wrapper = NiciraNvpRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, niciraNvpResource); + + assertTrue(answer.getResult()); + } + + @Test + public void testCreateLogicalSwitchCommandWrapper() { + final NiciraNvpApi niciraNvpApi = Mockito.mock(NiciraNvpApi.class); + final NiciraNvpUtilities niciraNvpUtilities = Mockito.mock(NiciraNvpUtilities.class); + final LogicalSwitch logicalSwitch = Mockito.mock(LogicalSwitch.class); + + final String transportUuid = "d2e05a9e-7120-4487-a5fc-414ab36d9345"; + final String transportType = "stt"; + final String name = "logicalswitch"; + final String ownerName = "owner"; + + final CreateLogicalSwitchCommand command = new CreateLogicalSwitchCommand(transportUuid, transportType, name, ownerName); + + final String truncated = "lswitch-" + command.getName(); + + when(niciraNvpResource.getNiciraNvpUtilities()).thenReturn(niciraNvpUtilities); + when(niciraNvpUtilities.createLogicalSwitch()).thenReturn(logicalSwitch); + when(niciraNvpResource.truncate("lswitch-" + command.getName(), NiciraNvpResource.NAME_MAX_LEN)).thenReturn(truncated); + when(niciraNvpResource.getNiciraNvpApi()).thenReturn(niciraNvpApi); + + try { + when(niciraNvpApi.createLogicalSwitch(logicalSwitch)).thenReturn(logicalSwitch); + when(logicalSwitch.getUuid()).thenReturn(transportUuid); + } catch (final NiciraNvpApiException e) { + fail(e.getMessage()); + } + + final NiciraNvpRequestWrapper wrapper = NiciraNvpRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, niciraNvpResource); + + assertTrue(answer.getResult()); + } + + @Test + public void testDeleteLogicalSwitchCommandWrapper() { + final NiciraNvpApi niciraNvpApi = Mockito.mock(NiciraNvpApi.class); + + final String logicalSwitchUuid = "d2e05a9e-7120-4487-a5fc-414ab36d9345"; + + final DeleteLogicalSwitchCommand command = new DeleteLogicalSwitchCommand(logicalSwitchUuid); + + when(niciraNvpResource.getNiciraNvpApi()).thenReturn(niciraNvpApi); + + try { + doNothing().when(niciraNvpApi).deleteLogicalSwitch(command.getLogicalSwitchUuid()); + } catch (final NiciraNvpApiException e) { + fail(e.getMessage()); + } + + final NiciraNvpRequestWrapper wrapper = NiciraNvpRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, niciraNvpResource); + + assertTrue(answer.getResult()); + } + + @Test + public void testConfigurePublicIpsOnLogicalRouterCommand() { + final NiciraNvpApi niciraNvpApi = Mockito.mock(NiciraNvpApi.class); + final LogicalRouterPort port1 = Mockito.mock(LogicalRouterPort.class); + + final List<LogicalRouterPort> listPorts = new ArrayList<LogicalRouterPort>(); + listPorts.add(port1); + + final NiciraNvpList<LogicalRouterPort> ports = new NiciraNvpList<LogicalRouterPort>(); + ports.setResults(listPorts); + ports.setResultCount(1); + + final String logicalRouterUuid = "d2e05a9e-7120-4487-a5fc-414ab36d9345"; + final String l3GatewayServiceUuid = "d2e05a9e-7120-4487-a5fc-414ab36d9345"; + final List<String> publicCidrs = new ArrayList<String>(); + publicCidrs.add("10.1.1.0/24"); + + final ConfigurePublicIpsOnLogicalRouterCommand command = new ConfigurePublicIpsOnLogicalRouterCommand(logicalRouterUuid, l3GatewayServiceUuid, publicCidrs); + + when(niciraNvpResource.getNiciraNvpApi()).thenReturn(niciraNvpApi); + + try { + when(niciraNvpApi.findLogicalRouterPortByGatewayServiceUuid(command.getLogicalRouterUuid(), command.getL3GatewayServiceUuid())).thenReturn(ports); + doNothing().when(niciraNvpApi).updateLogicalRouterPort(command.getLogicalRouterUuid(), port1); + } catch (final NiciraNvpApiException e) { + fail(e.getMessage()); + } + + final NiciraNvpRequestWrapper wrapper = NiciraNvpRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, niciraNvpResource); + + assertTrue(answer.getResult()); + } + + @Test + public void testDeleteLogicalSwitchPortCommand() { + final NiciraNvpApi niciraNvpApi = Mockito.mock(NiciraNvpApi.class); + + final String logicalSwitchUuid = "d2e05a9e-7120-4487-a5fc-414ab36d9345"; + final String logicalSwitchPortUuid = "d2e05a9e-7120-4487-a5fc-414ab36d9345"; + + final DeleteLogicalSwitchPortCommand command = new DeleteLogicalSwitchPortCommand(logicalSwitchUuid, logicalSwitchPortUuid); + + when(niciraNvpResource.getNiciraNvpApi()).thenReturn(niciraNvpApi); + + try { + doNothing().when(niciraNvpApi).deleteLogicalSwitchPort(command.getLogicalSwitchUuid(), command.getLogicalSwitchPortUuid()); + } catch (final NiciraNvpApiException e) { + fail(e.getMessage()); + } + + final NiciraNvpRequestWrapper wrapper = NiciraNvpRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, niciraNvpResource); + + assertTrue(answer.getResult()); + } + + @Test + public void testDeleteLogicalRouterCommand() { + final NiciraNvpApi niciraNvpApi = Mockito.mock(NiciraNvpApi.class); + + final String logicalRouterUuid = "d2e05a9e-7120-4487-a5fc-414ab36d9345"; + + final DeleteLogicalRouterCommand command = new DeleteLogicalRouterCommand(logicalRouterUuid); + + when(niciraNvpResource.getNiciraNvpApi()).thenReturn(niciraNvpApi); + + try { + doNothing().when(niciraNvpApi).deleteLogicalRouter(command.getLogicalRouterUuid()); + } catch (final NiciraNvpApiException e) { + fail(e.getMessage()); + } + + final NiciraNvpRequestWrapper wrapper = NiciraNvpRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, niciraNvpResource); + + assertTrue(answer.getResult()); + } + + @Test + public void testUpdateLogicalSwitchPortCommand() { + final NiciraNvpApi niciraNvpApi = Mockito.mock(NiciraNvpApi.class); + final NiciraNvpUtilities niciraNvpUtilities = Mockito.mock(NiciraNvpUtilities.class); + final VifAttachment vifAttachment = Mockito.mock(VifAttachment.class); + + final String logicalSwitchPortUuid = "d2e05a9e-7120-4487-a5fc-414ab36d9345"; + final String logicalSwitchUuid = "d2e05a9e-7120-4487-a5fc-414ab36d9345"; + final String attachmentUuid = "d2e05a9e-7120-4487-a5fc-414ab36d9345"; + final String ownerName = "admin"; + final String nicName = "eth0"; + + final UpdateLogicalSwitchPortCommand command = new UpdateLogicalSwitchPortCommand(logicalSwitchPortUuid, logicalSwitchUuid, attachmentUuid, ownerName, nicName); + + when(niciraNvpResource.getNiciraNvpUtilities()).thenReturn(niciraNvpUtilities); + when(niciraNvpResource.getNiciraNvpApi()).thenReturn(niciraNvpApi); + + try { + when(niciraNvpUtilities.createVifAttachment(attachmentUuid)).thenReturn(vifAttachment); + doNothing().when(niciraNvpApi).updateLogicalSwitchPortAttachment(logicalSwitchUuid, logicalSwitchPortUuid, vifAttachment); + } catch (final NiciraNvpApiException e) { + fail(e.getMessage()); + } + + final NiciraNvpRequestWrapper wrapper = NiciraNvpRequestWrapper.getInstance(); + assertNotNull(wrapper); + + final Answer answer = wrapper.execute(command, niciraNvpResource); + + assertTrue(answer.getResult()); + } +} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpResourceTest.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpResourceTest.java b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpResourceTest.java new file mode 100644 index 0000000..ddf2993 --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/test/java/com/cloud/network/resource/NiciraNvpResourceTest.java @@ -0,0 +1,882 @@ +// +// 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.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import javax.naming.ConfigurationException; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentMatcher; + +import com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterAnswer; +import com.cloud.agent.api.ConfigurePortForwardingRulesOnLogicalRouterCommand; +import com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterAnswer; +import com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterCommand; +import com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterAnswer; +import com.cloud.agent.api.ConfigureStaticNatRulesOnLogicalRouterCommand; +import com.cloud.agent.api.CreateLogicalRouterAnswer; +import com.cloud.agent.api.CreateLogicalRouterCommand; +import com.cloud.agent.api.CreateLogicalSwitchAnswer; +import com.cloud.agent.api.CreateLogicalSwitchCommand; +import com.cloud.agent.api.CreateLogicalSwitchPortAnswer; +import com.cloud.agent.api.CreateLogicalSwitchPortCommand; +import com.cloud.agent.api.DeleteLogicalRouterAnswer; +import com.cloud.agent.api.DeleteLogicalRouterCommand; +import com.cloud.agent.api.DeleteLogicalSwitchAnswer; +import com.cloud.agent.api.DeleteLogicalSwitchCommand; +import com.cloud.agent.api.DeleteLogicalSwitchPortAnswer; +import com.cloud.agent.api.DeleteLogicalSwitchPortCommand; +import com.cloud.agent.api.FindLogicalSwitchPortAnswer; +import com.cloud.agent.api.FindLogicalSwitchPortCommand; +import com.cloud.agent.api.PingCommand; +import com.cloud.agent.api.StartupCommand; +import com.cloud.agent.api.UpdateLogicalSwitchPortAnswer; +import com.cloud.agent.api.UpdateLogicalSwitchPortCommand; +import com.cloud.agent.api.to.PortForwardingRuleTO; +import com.cloud.agent.api.to.StaticNatRuleTO; +import com.cloud.host.Host; +import com.cloud.network.nicira.Attachment; +import com.cloud.network.nicira.ControlClusterStatus; +import com.cloud.network.nicira.DestinationNatRule; +import com.cloud.network.nicira.LogicalRouter; +import com.cloud.network.nicira.LogicalRouterPort; +import com.cloud.network.nicira.LogicalSwitch; +import com.cloud.network.nicira.LogicalSwitchPort; +import com.cloud.network.nicira.NatRule; +import com.cloud.network.nicira.NiciraNvpApi; +import com.cloud.network.nicira.NiciraNvpApiException; +import com.cloud.network.nicira.NiciraNvpList; +import com.cloud.network.nicira.SourceNatRule; +import com.cloud.network.utils.CommandRetryUtility; + +public class NiciraNvpResourceTest { + NiciraNvpApi nvpApi = mock(NiciraNvpApi.class); + NiciraNvpResource resource; + Map<String, Object> parameters; + + private CommandRetryUtility retryUtility; + + @Before + public void setUp() throws ConfigurationException { + resource = new NiciraNvpResource() { + @Override + protected NiciraNvpApi createNiciraNvpApi() { + return nvpApi; + } + }; + + parameters = new HashMap<String, Object>(); + parameters.put("name", "nvptestdevice"); + parameters.put("ip", "127.0.0.1"); + parameters.put("adminuser", "adminuser"); + parameters.put("guid", "aaaaa-bbbbb-ccccc"); + parameters.put("zoneId", "blublub"); + parameters.put("adminpass", "adminpass"); + + retryUtility = CommandRetryUtility.getInstance(); + retryUtility.setServerResource(resource); + } + + @Test(expected = ConfigurationException.class) + public void resourceConfigureFailure() throws ConfigurationException { + resource.configure("NiciraNvpResource", Collections.<String, Object> emptyMap()); + } + + @Test + public void resourceConfigure() throws ConfigurationException { + resource.configure("NiciraNvpResource", parameters); + + verify(nvpApi).setAdminCredentials("adminuser", "adminpass"); + verify(nvpApi).setControllerAddress("127.0.0.1"); + assertTrue("Incorrect resource name", "nvptestdevice".equals(resource.getName())); + + /* Pretty lame test, but here to assure this plugin fails + * if the type name ever changes from L2Networking + */ + assertTrue("Incorrect resource type", resource.getType() == Host.Type.L2Networking); + } + + @Test + public void testInitialization() throws ConfigurationException { + resource.configure("NiciraNvpResource", parameters); + + final StartupCommand[] sc = resource.initialize(); + assertTrue(sc.length == 1); + assertTrue("Incorrect startup command GUID", "aaaaa-bbbbb-ccccc".equals(sc[0].getGuid())); + assertTrue("Incorrect NVP device name", "nvptestdevice".equals(sc[0].getName())); + assertTrue("Incorrect Data Center", "blublub".equals(sc[0].getDataCenter())); + } + + @Test + public void testPingCommandStatusOk() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + final ControlClusterStatus ccs = mock(ControlClusterStatus.class); + when(ccs.getClusterStatus()).thenReturn("stable"); + when(nvpApi.getControlClusterStatus()).thenReturn(ccs); + + final PingCommand ping = resource.getCurrentStatus(42); + assertTrue(ping != null); + assertTrue(ping.getHostId() == 42); + assertTrue(ping.getHostType() == Host.Type.L2Networking); + } + + @Test + public void testPingCommandStatusFail() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + final ControlClusterStatus ccs = mock(ControlClusterStatus.class); + when(ccs.getClusterStatus()).thenReturn("unstable"); + when(nvpApi.getControlClusterStatus()).thenReturn(ccs); + + final PingCommand ping = resource.getCurrentStatus(42); + assertTrue(ping == null); + } + + @Test + public void testPingCommandStatusApiException() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + final ControlClusterStatus ccs = mock(ControlClusterStatus.class); + when(ccs.getClusterStatus()).thenReturn("unstable"); + when(nvpApi.getControlClusterStatus()).thenThrow(new NiciraNvpApiException()); + + final PingCommand ping = resource.getCurrentStatus(42); + assertTrue(ping == null); + } + + @Test + public void testRetries() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + final LogicalSwitch ls = mock(LogicalSwitch.class); + when(ls.getUuid()).thenReturn("cccc").thenReturn("cccc"); + when(nvpApi.createLogicalSwitch((LogicalSwitch)any())).thenThrow(new NiciraNvpApiException()).thenThrow(new NiciraNvpApiException()).thenReturn(ls); + + final CreateLogicalSwitchCommand clsc = new CreateLogicalSwitchCommand((String)parameters.get("guid"), "stt", "loigicalswitch", "owner"); + final CreateLogicalSwitchAnswer clsa = (CreateLogicalSwitchAnswer)resource.executeRequest(clsc); + assertTrue(clsa.getResult()); + } + + @Test + public void testCreateLogicalSwitch() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + final LogicalSwitch ls = mock(LogicalSwitch.class); + when(ls.getUuid()).thenReturn("cccc").thenReturn("cccc"); + when(nvpApi.createLogicalSwitch((LogicalSwitch)any())).thenReturn(ls); + + final CreateLogicalSwitchCommand clsc = new CreateLogicalSwitchCommand((String)parameters.get("guid"), "stt", "loigicalswitch", "owner"); + final CreateLogicalSwitchAnswer clsa = (CreateLogicalSwitchAnswer)resource.executeRequest(clsc); + assertTrue(clsa.getResult()); + assertTrue("cccc".equals(clsa.getLogicalSwitchUuid())); + } + + @Test + public void testCreateLogicalSwitchApiException() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + final LogicalSwitch ls = mock(LogicalSwitch.class); + when(ls.getUuid()).thenReturn("cccc").thenReturn("cccc"); + when(nvpApi.createLogicalSwitch((LogicalSwitch)any())).thenThrow(new NiciraNvpApiException()); + + final CreateLogicalSwitchCommand clsc = new CreateLogicalSwitchCommand((String)parameters.get("guid"), "stt", "loigicalswitch", "owner"); + final CreateLogicalSwitchAnswer clsa = (CreateLogicalSwitchAnswer)resource.executeRequest(clsc); + assertFalse(clsa.getResult()); + } + + @Test + public void testDeleteLogicalSwitch() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + final DeleteLogicalSwitchCommand dlsc = new DeleteLogicalSwitchCommand("cccc"); + final DeleteLogicalSwitchAnswer dlsa = (DeleteLogicalSwitchAnswer)resource.executeRequest(dlsc); + assertTrue(dlsa.getResult()); + } + + @Test + public void testDeleteLogicalSwitchApiException() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + doThrow(new NiciraNvpApiException()).when(nvpApi).deleteLogicalSwitch((String)any()); + + final DeleteLogicalSwitchCommand dlsc = new DeleteLogicalSwitchCommand("cccc"); + final DeleteLogicalSwitchAnswer dlsa = (DeleteLogicalSwitchAnswer)resource.executeRequest(dlsc); + assertFalse(dlsa.getResult()); + } + + @Test + public void testCreateLogicalSwitchPort() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + final LogicalSwitchPort lsp = mock(LogicalSwitchPort.class); + when(lsp.getUuid()).thenReturn("eeee"); + when(nvpApi.createLogicalSwitchPort(eq("cccc"), (LogicalSwitchPort)any())).thenReturn(lsp); + + final CreateLogicalSwitchPortCommand clspc = new CreateLogicalSwitchPortCommand("cccc", "dddd", "owner", "nicname"); + final CreateLogicalSwitchPortAnswer clspa = (CreateLogicalSwitchPortAnswer)resource.executeRequest(clspc); + assertTrue(clspa.getResult()); + assertTrue("eeee".equals(clspa.getLogicalSwitchPortUuid())); + + } + + @Test + public void testCreateLogicalSwitchPortApiExceptionInCreate() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + final LogicalSwitchPort lsp = mock(LogicalSwitchPort.class); + when(lsp.getUuid()).thenReturn("eeee"); + when(nvpApi.createLogicalSwitchPort(eq("cccc"), (LogicalSwitchPort)any())).thenThrow(new NiciraNvpApiException()); + + final CreateLogicalSwitchPortCommand clspc = new CreateLogicalSwitchPortCommand("cccc", "dddd", "owner", "nicname"); + final CreateLogicalSwitchPortAnswer clspa = (CreateLogicalSwitchPortAnswer)resource.executeRequest(clspc); + assertFalse(clspa.getResult()); + } + + @Test + public void testCreateLogicalSwitchPortApiExceptionInModify() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + final LogicalSwitchPort lsp = mock(LogicalSwitchPort.class); + when(lsp.getUuid()).thenReturn("eeee"); + when(nvpApi.createLogicalSwitchPort(eq("cccc"), (LogicalSwitchPort)any())).thenReturn(lsp); + doThrow(new NiciraNvpApiException()).when(nvpApi).updateLogicalSwitchPortAttachment((String)any(), (String)any(), (Attachment)any()); + + final CreateLogicalSwitchPortCommand clspc = new CreateLogicalSwitchPortCommand("cccc", "dddd", "owner", "nicname"); + final CreateLogicalSwitchPortAnswer clspa = (CreateLogicalSwitchPortAnswer)resource.executeRequest(clspc); + assertFalse(clspa.getResult()); + verify(nvpApi, atLeastOnce()).deleteLogicalSwitchPort((String)any(), (String)any()); + } + + @Test + public void testDeleteLogicalSwitchPortException() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + doThrow(new NiciraNvpApiException()).when(nvpApi).deleteLogicalSwitchPort((String)any(), (String)any()); + final DeleteLogicalSwitchPortAnswer dlspa = (DeleteLogicalSwitchPortAnswer)resource.executeRequest(new DeleteLogicalSwitchPortCommand("aaaa", "bbbb")); + assertFalse(dlspa.getResult()); + } + + @Test + public void testUpdateLogicalSwitchPortException() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + doThrow(new NiciraNvpApiException()).when(nvpApi).updateLogicalSwitchPortAttachment((String)any(), (String)any(), (Attachment)any()); + final UpdateLogicalSwitchPortAnswer dlspa = + (UpdateLogicalSwitchPortAnswer)resource.executeRequest(new UpdateLogicalSwitchPortCommand("aaaa", "bbbb", "cccc", "owner", "nicname")); + assertFalse(dlspa.getResult()); + } + + @Test + public void testFindLogicalSwitchPort() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + @SuppressWarnings("unchecked") + final + NiciraNvpList<LogicalSwitchPort> lspl = mock(NiciraNvpList.class); + when(lspl.getResultCount()).thenReturn(1); + when(nvpApi.findLogicalSwitchPortsByUuid("aaaa", "bbbb")).thenReturn(lspl); + + final FindLogicalSwitchPortAnswer flspa = (FindLogicalSwitchPortAnswer)resource.executeRequest(new FindLogicalSwitchPortCommand("aaaa", "bbbb")); + assertTrue(flspa.getResult()); + } + + @Test + public void testFindLogicalSwitchPortNotFound() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + @SuppressWarnings("unchecked") + final + NiciraNvpList<LogicalSwitchPort> lspl = mock(NiciraNvpList.class); + when(lspl.getResultCount()).thenReturn(0); + when(nvpApi.findLogicalSwitchPortsByUuid("aaaa", "bbbb")).thenReturn(lspl); + + final FindLogicalSwitchPortAnswer flspa = (FindLogicalSwitchPortAnswer)resource.executeRequest(new FindLogicalSwitchPortCommand("aaaa", "bbbb")); + assertFalse(flspa.getResult()); + } + + @Test + public void testFindLogicalSwitchPortApiException() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + when(nvpApi.findLogicalSwitchPortsByUuid("aaaa", "bbbb")).thenThrow(new NiciraNvpApiException()); + + final FindLogicalSwitchPortAnswer flspa = (FindLogicalSwitchPortAnswer)resource.executeRequest(new FindLogicalSwitchPortCommand("aaaa", "bbbb")); + assertFalse(flspa.getResult()); + } + + @Test + public void testCreateLogicalRouter() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + final LogicalRouter lrc = mock(LogicalRouter.class); + final LogicalRouterPort lrp = mock(LogicalRouterPort.class); + final LogicalSwitchPort lsp = mock(LogicalSwitchPort.class); + when(lrc.getUuid()).thenReturn("ccccc"); + when(lrp.getUuid()).thenReturn("ddddd").thenReturn("eeeee"); + when(lsp.getUuid()).thenReturn("fffff"); + when(nvpApi.createLogicalRouter((LogicalRouter)any())).thenReturn(lrc); + when(nvpApi.createLogicalRouterPort(eq("ccccc"), (LogicalRouterPort)any())).thenReturn(lrp); + when(nvpApi.createLogicalSwitchPort(eq("bbbbb"), (LogicalSwitchPort)any())).thenReturn(lsp); + final CreateLogicalRouterCommand clrc = new CreateLogicalRouterCommand("aaaaa", 50, "bbbbb", "lrouter", "publiccidr", "nexthop", "internalcidr", "owner"); + final CreateLogicalRouterAnswer clra = (CreateLogicalRouterAnswer)resource.executeRequest(clrc); + + assertTrue(clra.getResult()); + assertTrue("ccccc".equals(clra.getLogicalRouterUuid())); + verify(nvpApi, atLeast(1)).createLogicalRouterNatRule((String)any(), (NatRule)any()); + } + + @Test + public void testCreateLogicalRouterApiException() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + when(nvpApi.createLogicalRouter((LogicalRouter)any())).thenThrow(new NiciraNvpApiException()); + final CreateLogicalRouterCommand clrc = new CreateLogicalRouterCommand("aaaaa", 50, "bbbbb", "lrouter", "publiccidr", "nexthop", "internalcidr", "owner"); + final CreateLogicalRouterAnswer clra = (CreateLogicalRouterAnswer)resource.executeRequest(clrc); + + assertFalse(clra.getResult()); + } + + @Test + public void testCreateLogicalRouterApiExceptionRollbackRouter() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + final LogicalRouter lrc = mock(LogicalRouter.class); + when(lrc.getUuid()).thenReturn("ccccc"); + when(nvpApi.createLogicalRouter((LogicalRouter)any())).thenReturn(lrc); + when(nvpApi.createLogicalRouterPort(eq("ccccc"), (LogicalRouterPort)any())).thenThrow(new NiciraNvpApiException()); + final CreateLogicalRouterCommand clrc = new CreateLogicalRouterCommand("aaaaa", 50, "bbbbb", "lrouter", "publiccidr", "nexthop", "internalcidr", "owner"); + final CreateLogicalRouterAnswer clra = (CreateLogicalRouterAnswer)resource.executeRequest(clrc); + + assertFalse(clra.getResult()); + verify(nvpApi, atLeast(1)).deleteLogicalRouter(eq("ccccc")); + } + + @Test + public void testCreateLogicalRouterApiExceptionRollbackRouterAndSwitchPort() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + final LogicalRouter lrc = mock(LogicalRouter.class); + final LogicalRouterPort lrp = mock(LogicalRouterPort.class); + final LogicalSwitchPort lsp = mock(LogicalSwitchPort.class); + when(lrc.getUuid()).thenReturn("ccccc"); + when(lrp.getUuid()).thenReturn("ddddd").thenReturn("eeeee"); + when(lsp.getUuid()).thenReturn("fffff"); + when(nvpApi.createLogicalRouter((LogicalRouter)any())).thenReturn(lrc); + when(nvpApi.createLogicalRouterPort(eq("ccccc"), (LogicalRouterPort)any())).thenReturn(lrp); + when(nvpApi.createLogicalSwitchPort(eq("bbbbb"), (LogicalSwitchPort)any())).thenReturn(lsp); + when(nvpApi.createLogicalRouterNatRule((String)any(), (NatRule)any())).thenThrow(new NiciraNvpApiException()); + final CreateLogicalRouterCommand clrc = new CreateLogicalRouterCommand("aaaaa", 50, "bbbbb", "lrouter", "publiccidr", "nexthop", "internalcidr", "owner"); + final CreateLogicalRouterAnswer clra = (CreateLogicalRouterAnswer)resource.executeRequest(clrc); + + assertFalse(clra.getResult()); + verify(nvpApi, atLeast(1)).deleteLogicalRouter(eq("ccccc")); + verify(nvpApi, atLeast(1)).deleteLogicalSwitchPort(eq("bbbbb"), eq("fffff")); + } + + @Test + public void testDeleteLogicalRouterApiException() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + doThrow(new NiciraNvpApiException()).when(nvpApi).deleteLogicalRouter(eq("aaaaa")); + final DeleteLogicalRouterAnswer dlspa = (DeleteLogicalRouterAnswer)resource.executeRequest(new DeleteLogicalRouterCommand("aaaaa")); + assertFalse(dlspa.getResult()); + } + + @Test + public void testConfigurePublicIpsOnLogicalRouterApiException() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + final ConfigurePublicIpsOnLogicalRouterCommand cmd = mock(ConfigurePublicIpsOnLogicalRouterCommand.class); + @SuppressWarnings("unchecked") + final + NiciraNvpList<LogicalRouterPort> list = mock(NiciraNvpList.class); + + when(cmd.getLogicalRouterUuid()).thenReturn("aaaaa"); + when(cmd.getL3GatewayServiceUuid()).thenReturn("bbbbb"); + when(nvpApi.findLogicalRouterPortByGatewayServiceUuid("aaaaa", "bbbbb")).thenReturn(list); + doThrow(new NiciraNvpApiException()).when(nvpApi).updateLogicalRouterPort((String)any(), (LogicalRouterPort)any()); + + final ConfigurePublicIpsOnLogicalRouterAnswer answer = (ConfigurePublicIpsOnLogicalRouterAnswer)resource.executeRequest(cmd); + assertFalse(answer.getResult()); + + } + + @Test + public void testConfigurePublicIpsOnLogicalRouterRetry() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + + final ConfigurePublicIpsOnLogicalRouterCommand cmd = mock(ConfigurePublicIpsOnLogicalRouterCommand.class); + + when(cmd.getLogicalRouterUuid()).thenReturn("aaaaa"); + when(cmd.getL3GatewayServiceUuid()).thenReturn("bbbbb"); + when(nvpApi.findLogicalRouterPortByGatewayServiceUuid("aaaaa", "bbbbb")).thenThrow(new NiciraNvpApiException("retry 1")).thenThrow(new NiciraNvpApiException("retry 2")); + + final ConfigurePublicIpsOnLogicalRouterAnswer answer = (ConfigurePublicIpsOnLogicalRouterAnswer)resource.executeRequest(cmd); + assertFalse(answer.getResult()); + + } + + @Test + public void testConfigureStaticNatRulesOnLogicalRouter() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + /* StaticNat + * Outside IP: 11.11.11.11 + * Inside IP: 10.10.10.10 + */ + + // Mock the command + final ConfigureStaticNatRulesOnLogicalRouterCommand cmd = mock(ConfigureStaticNatRulesOnLogicalRouterCommand.class); + final StaticNatRuleTO rule = new StaticNatRuleTO(1, "11.11.11.11", null, null, "10.10.10.10", null, null, null, false, false); + final List<StaticNatRuleTO> rules = new ArrayList<StaticNatRuleTO>(); + rules.add(rule); + when(cmd.getRules()).thenReturn(rules); + when(cmd.getLogicalRouterUuid()).thenReturn("aaaaa"); + + // Mock the api find call + @SuppressWarnings("unchecked") + final + NiciraNvpList<NatRule> storedRules = mock(NiciraNvpList.class); + when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); + + // Mock the api create calls + final NatRule[] rulepair = resource.generateStaticNatRulePair("10.10.10.10", "11.11.11.11"); + rulepair[0].setUuid(UUID.randomUUID()); + rulepair[1].setUuid(UUID.randomUUID()); + when(nvpApi.createLogicalRouterNatRule(eq("aaaaa"), (NatRule)any())).thenReturn(rulepair[0]).thenReturn(rulepair[1]); + + final ConfigureStaticNatRulesOnLogicalRouterAnswer a = (ConfigureStaticNatRulesOnLogicalRouterAnswer)resource.executeRequest(cmd); + + assertTrue(a.getResult()); + verify(nvpApi, atLeast(2)).createLogicalRouterNatRule(eq("aaaaa"), argThat(new ArgumentMatcher<NatRule>() { + @Override + public boolean matches(final Object argument) { + final NatRule rule = (NatRule)argument; + if (rule.getType().equals("DestinationNatRule") && ((DestinationNatRule)rule).getToDestinationIpAddress().equals("10.10.10.10")) { + return true; + } + if (rule.getType().equals("SourceNatRule") && ((SourceNatRule)rule).getToSourceIpAddressMin().equals("11.11.11.11")) { + return true; + } + return false; + } + })); + } + + @Test + public void testConfigureStaticNatRulesOnLogicalRouterExistingRules() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + /* StaticNat + * Outside IP: 11.11.11.11 + * Inside IP: 10.10.10.10 + */ + + // Mock the command + final ConfigureStaticNatRulesOnLogicalRouterCommand cmd = mock(ConfigureStaticNatRulesOnLogicalRouterCommand.class); + final StaticNatRuleTO rule = new StaticNatRuleTO(1, "11.11.11.11", null, null, "10.10.10.10", null, null, null, false, false); + final List<StaticNatRuleTO> rules = new ArrayList<StaticNatRuleTO>(); + rules.add(rule); + when(cmd.getRules()).thenReturn(rules); + when(cmd.getLogicalRouterUuid()).thenReturn("aaaaa"); + + // Mock the api create calls + final NatRule[] rulepair = resource.generateStaticNatRulePair("10.10.10.10", "11.11.11.11"); + rulepair[0].setUuid(UUID.randomUUID()); + rulepair[1].setUuid(UUID.randomUUID()); + when(nvpApi.createLogicalRouterNatRule(eq("aaaaa"), (NatRule)any())).thenReturn(rulepair[0]).thenReturn(rulepair[1]); + + // Mock the api find call + @SuppressWarnings("unchecked") + final + NiciraNvpList<NatRule> storedRules = mock(NiciraNvpList.class); + when(storedRules.getResultCount()).thenReturn(2); + when(storedRules.getResults()).thenReturn(Arrays.asList(rulepair)); + when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); + + final ConfigureStaticNatRulesOnLogicalRouterAnswer a = (ConfigureStaticNatRulesOnLogicalRouterAnswer)resource.executeRequest(cmd); + + assertTrue(a.getResult()); + verify(nvpApi, never()).createLogicalRouterNatRule(eq("aaaaa"), argThat(new ArgumentMatcher<NatRule>() { + @Override + public boolean matches(final Object argument) { + final NatRule rule = (NatRule)argument; + if (rule.getType().equals("DestinationNatRule") && ((DestinationNatRule)rule).getToDestinationIpAddress().equals("10.10.10.10")) { + return true; + } + if (rule.getType().equals("SourceNatRule") && ((SourceNatRule)rule).getToSourceIpAddressMin().equals("11.11.11.11")) { + return true; + } + return false; + } + })); + } + + @Test + public void testConfigureStaticNatRulesOnLogicalRouterRemoveRules() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + /* StaticNat + * Outside IP: 11.11.11.11 + * Inside IP: 10.10.10.10 + */ + + // Mock the command + final ConfigureStaticNatRulesOnLogicalRouterCommand cmd = mock(ConfigureStaticNatRulesOnLogicalRouterCommand.class); + final StaticNatRuleTO rule = new StaticNatRuleTO(1, "11.11.11.11", null, null, "10.10.10.10", null, null, null, true, false); + final List<StaticNatRuleTO> rules = new ArrayList<StaticNatRuleTO>(); + rules.add(rule); + when(cmd.getRules()).thenReturn(rules); + when(cmd.getLogicalRouterUuid()).thenReturn("aaaaa"); + + // Mock the api create calls + final NatRule[] rulepair = resource.generateStaticNatRulePair("10.10.10.10", "11.11.11.11"); + final UUID rule0Uuid = UUID.randomUUID(); + final UUID rule1Uuid = UUID.randomUUID(); + rulepair[0].setUuid(rule0Uuid); + rulepair[1].setUuid(rule1Uuid); + when(nvpApi.createLogicalRouterNatRule(eq("aaaaa"), (NatRule)any())).thenReturn(rulepair[0]).thenReturn(rulepair[1]); + + // Mock the api find call + @SuppressWarnings("unchecked") + final + NiciraNvpList<NatRule> storedRules = mock(NiciraNvpList.class); + when(storedRules.getResultCount()).thenReturn(2); + when(storedRules.getResults()).thenReturn(Arrays.asList(rulepair)); + when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); + + final ConfigureStaticNatRulesOnLogicalRouterAnswer a = (ConfigureStaticNatRulesOnLogicalRouterAnswer)resource.executeRequest(cmd); + + assertTrue(a.getResult()); + verify(nvpApi, atLeast(2)).deleteLogicalRouterNatRule(eq("aaaaa"), argThat(new ArgumentMatcher<UUID>() { + @Override + public boolean matches(final Object argument) { + final UUID uuid = (UUID)argument; + if (rule0Uuid.equals(uuid) || rule1Uuid.equals(uuid)) { + return true; + } + return false; + } + })); + } + + @Test + public void testConfigureStaticNatRulesOnLogicalRouterRollback() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + /* StaticNat + * Outside IP: 11.11.11.11 + * Inside IP: 10.10.10.10 + */ + + // Mock the command + final ConfigureStaticNatRulesOnLogicalRouterCommand cmd = mock(ConfigureStaticNatRulesOnLogicalRouterCommand.class); + final StaticNatRuleTO rule = new StaticNatRuleTO(1, "11.11.11.11", null, null, "10.10.10.10", null, null, null, false, false); + final List<StaticNatRuleTO> rules = new ArrayList<StaticNatRuleTO>(); + rules.add(rule); + when(cmd.getRules()).thenReturn(rules); + when(cmd.getLogicalRouterUuid()).thenReturn("aaaaa"); + + // Mock the api create calls + final NatRule[] rulepair = resource.generateStaticNatRulePair("10.10.10.10", "11.11.11.11"); + rulepair[0].setUuid(UUID.randomUUID()); + rulepair[1].setUuid(UUID.randomUUID()); + when(nvpApi.createLogicalRouterNatRule(eq("aaaaa"), (NatRule)any())).thenReturn(rulepair[0]).thenThrow(new NiciraNvpApiException()); + + // Mock the api find call + @SuppressWarnings("unchecked") + final + NiciraNvpList<NatRule> storedRules = mock(NiciraNvpList.class); + when(storedRules.getResultCount()).thenReturn(0); + when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); + + final ConfigureStaticNatRulesOnLogicalRouterAnswer a = (ConfigureStaticNatRulesOnLogicalRouterAnswer)resource.executeRequest(cmd); + + assertFalse(a.getResult()); + verify(nvpApi, atLeastOnce()).deleteLogicalRouterNatRule(eq("aaaaa"), eq(rulepair[0].getUuid())); + } + + @Test + public void testConfigurePortForwardingRulesOnLogicalRouter() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + /* StaticNat + * Outside IP: 11.11.11.11 + * Inside IP: 10.10.10.10 + */ + + // Mock the command + final ConfigurePortForwardingRulesOnLogicalRouterCommand cmd = mock(ConfigurePortForwardingRulesOnLogicalRouterCommand.class); + final PortForwardingRuleTO rule = new PortForwardingRuleTO(1, "11.11.11.11", 80, 80, "10.10.10.10", 8080, 8080, "tcp", false, false); + final List<PortForwardingRuleTO> rules = new ArrayList<PortForwardingRuleTO>(); + rules.add(rule); + when(cmd.getRules()).thenReturn(rules); + when(cmd.getLogicalRouterUuid()).thenReturn("aaaaa"); + + // Mock the api find call + @SuppressWarnings("unchecked") + final + NiciraNvpList<NatRule> storedRules = mock(NiciraNvpList.class); + when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); + + // Mock the api create calls + final NatRule[] rulepair = resource.generatePortForwardingRulePair("10.10.10.10", new int[] {8080, 8080}, "11.11.11.11", new int[] {80, 80}, "tcp"); + rulepair[0].setUuid(UUID.randomUUID()); + rulepair[1].setUuid(UUID.randomUUID()); + when(nvpApi.createLogicalRouterNatRule(eq("aaaaa"), (NatRule)any())).thenReturn(rulepair[0]).thenReturn(rulepair[1]); + + final ConfigurePortForwardingRulesOnLogicalRouterAnswer a = (ConfigurePortForwardingRulesOnLogicalRouterAnswer)resource.executeRequest(cmd); + + assertTrue(a.getResult()); + verify(nvpApi, atLeast(2)).createLogicalRouterNatRule(eq("aaaaa"), argThat(new ArgumentMatcher<NatRule>() { + @Override + public boolean matches(final Object argument) { + final NatRule rule = (NatRule)argument; + if (rule.getType().equals("DestinationNatRule") && ((DestinationNatRule)rule).getToDestinationIpAddress().equals("10.10.10.10")) { + return true; + } + if (rule.getType().equals("SourceNatRule") && ((SourceNatRule)rule).getToSourceIpAddressMin().equals("11.11.11.11")) { + return true; + } + return false; + } + })); + } + + @Test + public void testConfigurePortForwardingRulesOnLogicalRouterExistingRules() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + /* StaticNat + * Outside IP: 11.11.11.11 + * Inside IP: 10.10.10.10 + */ + + // Mock the command + final ConfigurePortForwardingRulesOnLogicalRouterCommand cmd = mock(ConfigurePortForwardingRulesOnLogicalRouterCommand.class); + final PortForwardingRuleTO rule = new PortForwardingRuleTO(1, "11.11.11.11", 80, 80, "10.10.10.10", 8080, 8080, "tcp", false, true); + final List<PortForwardingRuleTO> rules = new ArrayList<PortForwardingRuleTO>(); + rules.add(rule); + when(cmd.getRules()).thenReturn(rules); + when(cmd.getLogicalRouterUuid()).thenReturn("aaaaa"); + + // Mock the api create calls + final NatRule[] rulepair = resource.generatePortForwardingRulePair("10.10.10.10", new int[] {8080, 8080}, "11.11.11.11", new int[] {80, 80}, "tcp"); + rulepair[0].setUuid(UUID.randomUUID()); + rulepair[1].setUuid(UUID.randomUUID()); + when(nvpApi.createLogicalRouterNatRule(eq("aaaaa"), (NatRule)any())).thenReturn(rulepair[0]).thenReturn(rulepair[1]); + + // Mock the api find call + @SuppressWarnings("unchecked") + final + NiciraNvpList<NatRule> storedRules = mock(NiciraNvpList.class); + when(storedRules.getResultCount()).thenReturn(2); + when(storedRules.getResults()).thenReturn(Arrays.asList(rulepair)); + when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); + + final ConfigurePortForwardingRulesOnLogicalRouterAnswer a = (ConfigurePortForwardingRulesOnLogicalRouterAnswer)resource.executeRequest(cmd); + + assertTrue(a.getResult()); + verify(nvpApi, never()).createLogicalRouterNatRule(eq("aaaaa"), argThat(new ArgumentMatcher<NatRule>() { + @Override + public boolean matches(final Object argument) { + final NatRule rule = (NatRule)argument; + if (rule.getType().equals("DestinationNatRule") && ((DestinationNatRule)rule).getToDestinationIpAddress().equals("10.10.10.10")) { + return true; + } + if (rule.getType().equals("SourceNatRule") && ((SourceNatRule)rule).getToSourceIpAddressMin().equals("11.11.11.11")) { + return true; + } + return false; + } + })); + } + + @Test + public void testConfigurePortForwardingRulesOnLogicalRouterRemoveRules() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + /* StaticNat + * Outside IP: 11.11.11.11 + * Inside IP: 10.10.10.10 + */ + + // Mock the command + final ConfigurePortForwardingRulesOnLogicalRouterCommand cmd = mock(ConfigurePortForwardingRulesOnLogicalRouterCommand.class); + final PortForwardingRuleTO rule = new PortForwardingRuleTO(1, "11.11.11.11", 80, 80, "10.10.10.10", 8080, 8080, "tcp", true, true); + final List<PortForwardingRuleTO> rules = new ArrayList<PortForwardingRuleTO>(); + rules.add(rule); + when(cmd.getRules()).thenReturn(rules); + when(cmd.getLogicalRouterUuid()).thenReturn("aaaaa"); + + // Mock the api create calls + final NatRule[] rulepair = resource.generatePortForwardingRulePair("10.10.10.10", new int[] {8080, 8080}, "11.11.11.11", new int[] {80, 80}, "tcp"); + final UUID rule0Uuid = UUID.randomUUID(); + final UUID rule1Uuid = UUID.randomUUID(); + rulepair[0].setUuid(rule0Uuid); + rulepair[1].setUuid(rule1Uuid); + when(nvpApi.createLogicalRouterNatRule(eq("aaaaa"), (NatRule)any())).thenReturn(rulepair[0]).thenReturn(rulepair[1]); + + // Mock the api find call + @SuppressWarnings("unchecked") + final + NiciraNvpList<NatRule> storedRules = mock(NiciraNvpList.class); + when(storedRules.getResultCount()).thenReturn(2); + when(storedRules.getResults()).thenReturn(Arrays.asList(rulepair)); + when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); + + final ConfigurePortForwardingRulesOnLogicalRouterAnswer a = (ConfigurePortForwardingRulesOnLogicalRouterAnswer)resource.executeRequest(cmd); + + assertTrue(a.getResult()); + verify(nvpApi, atLeast(2)).deleteLogicalRouterNatRule(eq("aaaaa"), argThat(new ArgumentMatcher<UUID>() { + @Override + public boolean matches(final Object argument) { + final UUID uuid = (UUID)argument; + if (rule0Uuid.equals(uuid) || rule1Uuid.equals(uuid)) { + return true; + } + return false; + } + })); + } + + @Test + public void testConfigurePortForwardingRulesOnLogicalRouterRollback() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + /* StaticNat + * Outside IP: 11.11.11.11 + * Inside IP: 10.10.10.10 + */ + + // Mock the command + final ConfigurePortForwardingRulesOnLogicalRouterCommand cmd = mock(ConfigurePortForwardingRulesOnLogicalRouterCommand.class); + final PortForwardingRuleTO rule = new PortForwardingRuleTO(1, "11.11.11.11", 80, 80, "10.10.10.10", 8080, 8080, "tcp", false, false); + final List<PortForwardingRuleTO> rules = new ArrayList<PortForwardingRuleTO>(); + rules.add(rule); + when(cmd.getRules()).thenReturn(rules); + when(cmd.getLogicalRouterUuid()).thenReturn("aaaaa"); + + // Mock the api create calls + final NatRule[] rulepair = resource.generatePortForwardingRulePair("10.10.10.10", new int[] {8080, 8080}, "11.11.11.11", new int[] {80, 80}, "tcp"); + rulepair[0].setUuid(UUID.randomUUID()); + rulepair[1].setUuid(UUID.randomUUID()); + when(nvpApi.createLogicalRouterNatRule(eq("aaaaa"), (NatRule)any())).thenReturn(rulepair[0]).thenThrow(new NiciraNvpApiException()); + + // Mock the api find call + @SuppressWarnings("unchecked") + final + NiciraNvpList<NatRule> storedRules = mock(NiciraNvpList.class); + when(storedRules.getResultCount()).thenReturn(0); + when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); + + final ConfigurePortForwardingRulesOnLogicalRouterAnswer a = (ConfigurePortForwardingRulesOnLogicalRouterAnswer)resource.executeRequest(cmd); + + assertFalse(a.getResult()); + verify(nvpApi, atLeastOnce()).deleteLogicalRouterNatRule(eq("aaaaa"), eq(rulepair[0].getUuid())); + } + + @Test + public void testConfigurePortForwardingRulesOnLogicalRouterPortRange() throws ConfigurationException, NiciraNvpApiException { + resource.configure("NiciraNvpResource", parameters); + /* StaticNat + * Outside IP: 11.11.11.11 + * Inside IP: 10.10.10.10 + */ + + // Mock the command + final ConfigurePortForwardingRulesOnLogicalRouterCommand cmd = mock(ConfigurePortForwardingRulesOnLogicalRouterCommand.class); + final PortForwardingRuleTO rule = new PortForwardingRuleTO(1, "11.11.11.11", 80, 85, "10.10.10.10", 80, 85, "tcp", false, false); + final List<PortForwardingRuleTO> rules = new ArrayList<PortForwardingRuleTO>(); + rules.add(rule); + when(cmd.getRules()).thenReturn(rules); + when(cmd.getLogicalRouterUuid()).thenReturn("aaaaa"); + + // Mock the api find call + @SuppressWarnings("unchecked") + final + NiciraNvpList<NatRule> storedRules = mock(NiciraNvpList.class); + when(nvpApi.findNatRulesByLogicalRouterUuid("aaaaa")).thenReturn(storedRules); + + // Mock the api create calls + final NatRule[] rulepair = resource.generatePortForwardingRulePair("10.10.10.10", new int[] {80, 85}, "11.11.11.11", new int[] {80, 85}, "tcp"); + rulepair[0].setUuid(UUID.randomUUID()); + rulepair[1].setUuid(UUID.randomUUID()); + when(nvpApi.createLogicalRouterNatRule(eq("aaaaa"), (NatRule)any())).thenReturn(rulepair[0]).thenReturn(rulepair[1]); + + final ConfigurePortForwardingRulesOnLogicalRouterAnswer a = (ConfigurePortForwardingRulesOnLogicalRouterAnswer)resource.executeRequest(cmd); + + // The expected result is false, Nicira does not support port ranges in DNAT + assertFalse(a.getResult()); + + } + + @Test + public void testGenerateStaticNatRulePair() { + final NatRule[] rules = resource.generateStaticNatRulePair("10.10.10.10", "11.11.11.11"); + assertTrue("DestinationNatRule".equals(rules[0].getType())); + assertTrue("SourceNatRule".equals(rules[1].getType())); + + final DestinationNatRule dnr = (DestinationNatRule)rules[0]; + assertTrue(dnr.getToDestinationIpAddress().equals("10.10.10.10")); + assertTrue(dnr.getToDestinationPort() == null); + assertTrue(dnr.getMatch().getDestinationIpAddresses().equals("11.11.11.11")); + + final SourceNatRule snr = (SourceNatRule)rules[1]; + assertTrue(snr.getToSourceIpAddressMin().equals("11.11.11.11") && snr.getToSourceIpAddressMax().equals("11.11.11.11")); + assertTrue(snr.getToSourcePort() == null); + assertTrue(snr.getMatch().getSourceIpAddresses().equals("10.10.10.10")); + } + + @Test + public void testGeneratePortForwardingRulePair() { + final NatRule[] rules = resource.generatePortForwardingRulePair("10.10.10.10", new int[] {8080, 8080}, "11.11.11.11", new int[] {80, 80}, "tcp"); + assertTrue("DestinationNatRule".equals(rules[0].getType())); + assertTrue("SourceNatRule".equals(rules[1].getType())); + + final DestinationNatRule dnr = (DestinationNatRule)rules[0]; + assertTrue(dnr.getToDestinationIpAddress().equals("10.10.10.10")); + assertTrue(dnr.getToDestinationPort() == 8080); + assertTrue(dnr.getMatch().getDestinationIpAddresses().equals("11.11.11.11")); + assertTrue(dnr.getMatch().getDestinationPort() == 80); + assertTrue(dnr.getMatch().getEthertype().equals("IPv4") && dnr.getMatch().getProtocol() == 6); + + final SourceNatRule snr = (SourceNatRule)rules[1]; + assertTrue(snr.getToSourceIpAddressMin().equals("11.11.11.11") && snr.getToSourceIpAddressMax().equals("11.11.11.11")); + assertTrue(snr.getToSourcePort() == 80); + assertTrue(snr.getMatch().getSourceIpAddresses().equals("10.10.10.10")); + assertTrue(snr.getMatch().getSourcePort() == 8080); + assertTrue(snr.getMatch().getEthertype().equals("IPv4") && rules[1].getMatch().getProtocol() == 6); + } +} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/src/test/resources/config.properties ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/src/test/resources/config.properties b/plugins/network-elements/nicira-nvp/src/test/resources/config.properties new file mode 100644 index 0000000..4006e38d --- /dev/null +++ b/plugins/network-elements/nicira-nvp/src/test/resources/config.properties @@ -0,0 +1,23 @@ +# +# 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. +# + + +nvp.host=${nvp-host} +nvp.admin.user=${nvp-admin-user} +nvp.admin.pwd=${nvp-admin-pwd} http://git-wip-us.apache.org/repos/asf/cloudstack/blob/93b0989d/plugins/network-elements/nicira-nvp/test/com/cloud/network/element/NiciraNvpElementTest.java ---------------------------------------------------------------------- diff --git a/plugins/network-elements/nicira-nvp/test/com/cloud/network/element/NiciraNvpElementTest.java b/plugins/network-elements/nicira-nvp/test/com/cloud/network/element/NiciraNvpElementTest.java deleted file mode 100644 index bf8ce18..0000000 --- a/plugins/network-elements/nicira-nvp/test/com/cloud/network/element/NiciraNvpElementTest.java +++ /dev/null @@ -1,217 +0,0 @@ -// -// 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 static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.argThat; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.atLeast; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import javax.naming.ConfigurationException; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentMatcher; - -import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; - -import com.cloud.agent.AgentManager; -import com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterAnswer; -import com.cloud.agent.api.ConfigurePublicIpsOnLogicalRouterCommand; -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.host.HostVO; -import com.cloud.host.dao.HostDao; -import com.cloud.network.IpAddress; -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.NetworkModel; -import com.cloud.network.Networks.BroadcastDomainType; -import com.cloud.network.Networks.TrafficType; -import com.cloud.network.NiciraNvpDeviceVO; -import com.cloud.network.NiciraNvpRouterMappingVO; -import com.cloud.network.PublicIpAddress; -import com.cloud.network.dao.NetworkServiceMapDao; -import com.cloud.network.dao.NiciraNvpDao; -import com.cloud.network.dao.NiciraNvpRouterMappingDao; -import com.cloud.offering.NetworkOffering; -import com.cloud.resource.ResourceManager; -import com.cloud.user.Account; -import com.cloud.utils.net.Ip; -import com.cloud.vm.ReservationContext; - -public class NiciraNvpElementTest { - - private static final long NETWORK_ID = 42L; - NiciraNvpElement element = new NiciraNvpElement(); - NetworkOrchestrationService networkManager = mock(NetworkOrchestrationService.class); - NetworkModel networkModel = mock(NetworkModel.class); - NetworkServiceMapDao ntwkSrvcDao = mock(NetworkServiceMapDao.class); - AgentManager agentManager = mock(AgentManager.class); - HostDao hostDao = mock(HostDao.class); - NiciraNvpDao niciraNvpDao = mock(NiciraNvpDao.class); - NiciraNvpRouterMappingDao niciraNvpRouterMappingDao = mock(NiciraNvpRouterMappingDao.class); - - @Before - public void setUp() throws ConfigurationException { - element.resourceMgr = mock(ResourceManager.class); - element.networkManager = networkManager; - element.ntwkSrvcDao = ntwkSrvcDao; - element.networkModel = networkModel; - element.agentMgr = agentManager; - element.hostDao = hostDao; - element.niciraNvpDao = niciraNvpDao; - element.niciraNvpRouterMappingDao = niciraNvpRouterMappingDao; - - // Standard responses - when(networkModel.isProviderForNetwork(Provider.NiciraNvp, NETWORK_ID)).thenReturn(true); - - element.configure("NiciraNvpTestElement", Collections.<String, Object> emptyMap()); - } - - @Test - public void canHandleTest() { - final Network net = mock(Network.class); - when(net.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Lswitch); - when(net.getId()).thenReturn(NETWORK_ID); - - when(ntwkSrvcDao.canProviderSupportServiceInNetwork(NETWORK_ID, Service.Connectivity, Provider.NiciraNvp)).thenReturn(true); - // Golden path - assertTrue(element.canHandle(net, Service.Connectivity)); - - when(net.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Vlan); - // Only broadcastdomaintype lswitch is supported - assertFalse(element.canHandle(net, Service.Connectivity)); - - when(net.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Lswitch); - when(ntwkSrvcDao.canProviderSupportServiceInNetwork(NETWORK_ID, Service.Connectivity, Provider.NiciraNvp)).thenReturn(false); - // No nvp provider in the network - assertFalse(element.canHandle(net, Service.Connectivity)); - - when(networkModel.isProviderForNetwork(Provider.NiciraNvp, NETWORK_ID)).thenReturn(false); - when(ntwkSrvcDao.canProviderSupportServiceInNetwork(NETWORK_ID, Service.Connectivity, Provider.NiciraNvp)).thenReturn(true); - // NVP provider does not provide Connectivity for this network - assertFalse(element.canHandle(net, Service.Connectivity)); - - when(networkModel.isProviderForNetwork(Provider.NiciraNvp, NETWORK_ID)).thenReturn(true); - // Only service Connectivity is supported - assertFalse(element.canHandle(net, Service.Dhcp)); - - } - - @Test - public void implementTest() throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException { - final Network network = mock(Network.class); - when(network.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Lswitch); - when(network.getId()).thenReturn(NETWORK_ID); - - final NetworkOffering offering = mock(NetworkOffering.class); - when(offering.getId()).thenReturn(NETWORK_ID); - when(offering.getTrafficType()).thenReturn(TrafficType.Guest); - when(offering.getGuestType()).thenReturn(GuestType.Isolated); - - mock(DeployDestination.class); - - final Domain dom = mock(Domain.class); - when(dom.getName()).thenReturn("domain"); - final Account acc = mock(Account.class); - when(acc.getAccountName()).thenReturn("accountname"); - final ReservationContext context = mock(ReservationContext.class); - when(context.getDomain()).thenReturn(dom); - when(context.getAccount()).thenReturn(acc); - } - - @Test - public void applyIpTest() throws ResourceUnavailableException { - final Network network = mock(Network.class); - when(network.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Lswitch); - when(network.getId()).thenReturn(NETWORK_ID); - when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID); - - final NetworkOffering offering = mock(NetworkOffering.class); - when(offering.getId()).thenReturn(NETWORK_ID); - when(offering.getTrafficType()).thenReturn(TrafficType.Guest); - when(offering.getGuestType()).thenReturn(GuestType.Isolated); - - final List<PublicIpAddress> ipAddresses = new ArrayList<PublicIpAddress>(); - final PublicIpAddress pipReleased = mock(PublicIpAddress.class); - final PublicIpAddress pipAllocated = mock(PublicIpAddress.class); - final Ip ipReleased = new Ip("42.10.10.10"); - final Ip ipAllocated = new Ip("10.10.10.10"); - when(pipAllocated.getState()).thenReturn(IpAddress.State.Allocated); - when(pipAllocated.getAddress()).thenReturn(ipAllocated); - when(pipAllocated.getNetmask()).thenReturn("255.255.255.0"); - when(pipReleased.getState()).thenReturn(IpAddress.State.Releasing); - when(pipReleased.getAddress()).thenReturn(ipReleased); - when(pipReleased.getNetmask()).thenReturn("255.255.255.0"); - ipAddresses.add(pipAllocated); - ipAddresses.add(pipReleased); - - final Set<Service> services = new HashSet<Service>(); - services.add(Service.SourceNat); - services.add(Service.StaticNat); - services.add(Service.PortForwarding); - - final List<NiciraNvpDeviceVO> deviceList = new ArrayList<NiciraNvpDeviceVO>(); - final NiciraNvpDeviceVO nndVO = mock(NiciraNvpDeviceVO.class); - final NiciraNvpRouterMappingVO nnrmVO = mock(NiciraNvpRouterMappingVO.class); - when(niciraNvpRouterMappingDao.findByNetworkId(NETWORK_ID)).thenReturn(nnrmVO); - when(nnrmVO.getLogicalRouterUuid()).thenReturn("abcde"); - when(nndVO.getHostId()).thenReturn(NETWORK_ID); - final HostVO hvo = mock(HostVO.class); - when(hvo.getId()).thenReturn(NETWORK_ID); - when(hvo.getDetail("l3gatewayserviceuuid")).thenReturn("abcde"); - when(hostDao.findById(NETWORK_ID)).thenReturn(hvo); - deviceList.add(nndVO); - when(niciraNvpDao.listByPhysicalNetwork(NETWORK_ID)).thenReturn(deviceList); - - final ConfigurePublicIpsOnLogicalRouterAnswer answer = mock(ConfigurePublicIpsOnLogicalRouterAnswer.class); - when(answer.getResult()).thenReturn(true); - when(agentManager.easySend(eq(NETWORK_ID), any(ConfigurePublicIpsOnLogicalRouterCommand.class))).thenReturn(answer); - - assertTrue(element.applyIps(network, ipAddresses, services)); - - verify(agentManager, atLeast(1)).easySend(eq(NETWORK_ID), argThat(new ArgumentMatcher<ConfigurePublicIpsOnLogicalRouterCommand>() { - @Override - public boolean matches(final Object argument) { - final ConfigurePublicIpsOnLogicalRouterCommand command = (ConfigurePublicIpsOnLogicalRouterCommand)argument; - if (command.getPublicCidrs().size() == 1) - return true; - return false; - } - })); - } -} \ No newline at end of file