This closes #146

Refactoring the NetworkUsageCommand wrapper in order to cope with new design
   - Unit tests added: 100% coverage
This commit is contained in:
wilderrodrigues 2015-04-03 17:23:16 +02:00
parent 02d1cdd49a
commit 096d1b93b0
5 changed files with 174 additions and 48 deletions

View File

@ -25,10 +25,6 @@ import javax.ejb.Local;
import org.apache.log4j.Logger;
import org.apache.xmlrpc.XmlRpcException;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.NetworkUsageAnswer;
import com.cloud.agent.api.NetworkUsageCommand;
import com.cloud.resource.ServerResource;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.script.Script;
@ -43,15 +39,6 @@ public class XcpServerResource extends CitrixResourceBase {
private final static Logger s_logger = Logger.getLogger(XcpServerResource.class);
private final static long mem_32m = 33554432L;
@Override
public Answer executeRequest(final Command cmd) {
if (cmd instanceof NetworkUsageCommand) {
return execute((NetworkUsageCommand)cmd);
} else {
return super.executeRequest(cmd);
}
}
@Override
protected List<File> getPatchFiles() {
final List<File> files = new ArrayList<File>();
@ -65,23 +52,6 @@ public class XcpServerResource extends CitrixResourceBase {
return files;
}
protected NetworkUsageAnswer execute(final NetworkUsageCommand cmd) {
try {
final Connection conn = getConnection();
if (cmd.getOption() != null && cmd.getOption().equals("create")) {
final String result = networkUsage(conn, cmd.getPrivateIP(), "create", null);
final NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, result, 0L, 0L);
return answer;
}
final long[] stats = getNetworkStats(conn, cmd.getPrivateIP());
final NetworkUsageAnswer answer = new NetworkUsageAnswer(cmd, "", stats[0], stats[1]);
return answer;
} catch (final Exception ex) {
s_logger.warn("Failed to get network usage stats due to ", ex);
return new NetworkUsageAnswer(cmd, ex);
}
}
/**
XCP provides four memory configuration fields through which
administrators can control this behaviour:

View File

@ -25,10 +25,7 @@ import javax.ejb.Local;
import org.apache.log4j.Logger;
import org.apache.xmlrpc.XmlRpcException;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.StartupCommand;
import com.cloud.hypervisor.xenserver.resource.wrapper.CitrixRequestWrapper;
import com.cloud.resource.ServerResource;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.script.Script;
@ -45,17 +42,6 @@ import com.xensource.xenapi.VLAN;
public class XenServer56Resource extends CitrixResourceBase {
private final static Logger s_logger = Logger.getLogger(XenServer56Resource.class);
@Override
public Answer executeRequest(final Command cmd) {
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
try {
return wrapper.execute(cmd, this);
} catch (final Exception e) {
return super.executeRequest(cmd);
}
}
@Override
protected List<File> getPatchFiles() {
final List<File> files = new ArrayList<File>();
@ -193,8 +179,4 @@ public class XenServer56Resource extends CitrixResourceBase {
final StartupCommand[] cmds = super.initialize();
return cmds;
}
public XenServer56Resource() {
super();
}
}

View File

@ -90,6 +90,7 @@ import com.cloud.agent.api.storage.MigrateVolumeCommand;
import com.cloud.agent.api.storage.PrimaryStorageDownloadCommand;
import com.cloud.agent.api.storage.ResizeVolumeCommand;
import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase;
import com.cloud.hypervisor.xenserver.resource.XcpServerResource;
import com.cloud.hypervisor.xenserver.resource.XenServer56FP1Resource;
import com.cloud.hypervisor.xenserver.resource.XenServer56Resource;
import com.cloud.hypervisor.xenserver.resource.XenServer610Resource;
@ -204,6 +205,11 @@ public class CitrixRequestWrapper extends RequestWrapper {
xenServer610Commands.put(MigrateWithStorageCompleteCommand.class, new XenServer610MigrateWithStorageCompleteCommandWrapper());
xenServer610Commands.put(MigrateVolumeCommand.class, new XenServer610MigrateVolumeCommandWrapper());
resources.put(XenServer610Resource.class, xenServer610Commands);
// XcpServerResource commands
final Hashtable<Class<? extends Command>, CommandWrapper> xcpServerResourceCommand = new Hashtable<Class<? extends Command>, CommandWrapper>();
xcpServerResourceCommand.put(NetworkUsageCommand.class, new XcpServerNetworkUsageCommandWrapper());
resources.put(XcpServerResource.class, xcpServerResourceCommand);
}
public static CitrixRequestWrapper getInstance() {

View File

@ -0,0 +1,52 @@
//
// 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.hypervisor.xenserver.resource.wrapper;
import org.apache.log4j.Logger;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.NetworkUsageAnswer;
import com.cloud.agent.api.NetworkUsageCommand;
import com.cloud.hypervisor.xenserver.resource.XcpServerResource;
import com.cloud.resource.CommandWrapper;
import com.xensource.xenapi.Connection;
public final class XcpServerNetworkUsageCommandWrapper extends CommandWrapper<NetworkUsageCommand, Answer, XcpServerResource> {
private static final Logger s_logger = Logger.getLogger(XcpServerNetworkUsageCommandWrapper.class);
@Override
public Answer execute(final NetworkUsageCommand command, final XcpServerResource xcpServerResource) {
try {
final Connection conn = xcpServerResource.getConnection();
if (command.getOption() != null && command.getOption().equals("create")) {
final String result = xcpServerResource.networkUsage(conn, command.getPrivateIP(), "create", null);
final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, result, 0L, 0L);
return answer;
}
final long[] stats = xcpServerResource.getNetworkStats(conn, command.getPrivateIP());
final NetworkUsageAnswer answer = new NetworkUsageAnswer(command, "", stats[0], stats[1]);
return answer;
} catch (final Exception ex) {
s_logger.warn("Failed to get network usage stats due to ", ex);
return new NetworkUsageAnswer(command, ex);
}
}
}

View File

@ -0,0 +1,116 @@
// 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.hypervisor.xenserver.resource.wrapper;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.modules.junit4.PowerMockRunner;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.NetworkUsageCommand;
import com.cloud.hypervisor.xenserver.resource.XcpServerResource;
import com.cloud.utils.exception.CloudRuntimeException;
import com.xensource.xenapi.Connection;
@RunWith(PowerMockRunner.class)
public class XcpServerWrapperTest {
@Mock
protected XcpServerResource XcpServerResource;
@Test
public void testNetworkUsageCommandCreate() {
final Connection conn = Mockito.mock(Connection.class);
final String privateIP = "192.168.0.10";
final String domRName = "dom";
final String option = "create";
final boolean forVpc = true;
final NetworkUsageCommand usageCommand = new NetworkUsageCommand(privateIP, domRName, option, forVpc);
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
when(XcpServerResource.getConnection()).thenReturn(conn);
when(XcpServerResource.networkUsage(conn, usageCommand.getPrivateIP(), "create", null)).thenReturn("success");
final Answer answer = wrapper.execute(usageCommand, XcpServerResource);
verify(XcpServerResource, times(1)).getConnection();
assertTrue(answer.getResult());
}
@Test
public void testNetworkUsageCommandGet() {
final Connection conn = Mockito.mock(Connection.class);
final String privateIP = "192.168.0.10";
final String domRName = "dom";
final boolean forVpc = true;
final String gatewayIp = "172.16.0.10";
final NetworkUsageCommand usageCommand = new NetworkUsageCommand(privateIP, domRName, forVpc, gatewayIp);
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
when(XcpServerResource.getConnection()).thenReturn(conn);
when(XcpServerResource.getNetworkStats(conn, usageCommand.getPrivateIP())).thenReturn(new long[]{1l, 1l});
final Answer answer = wrapper.execute(usageCommand, XcpServerResource);
verify(XcpServerResource, times(1)).getConnection();
assertTrue(answer.getResult());
}
@Test
public void testNetworkUsageCommandExceptiopn() {
final Connection conn = Mockito.mock(Connection.class);
final String privateIP = "192.168.0.10";
final String domRName = "dom";
final String option = null;
final boolean forVpc = true;
final NetworkUsageCommand usageCommand = new NetworkUsageCommand(privateIP, domRName, option, forVpc);
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
when(XcpServerResource.getConnection()).thenReturn(conn);
when(XcpServerResource.networkUsage(conn, usageCommand.getPrivateIP(), "create", null)).thenThrow(new CloudRuntimeException("FAILED"));
final Answer answer = wrapper.execute(usageCommand, XcpServerResource);
verify(XcpServerResource, times(1)).getConnection();
assertFalse(answer.getResult());
}
}