mirror of
https://github.com/apache/cloudstack.git
synced 2025-11-02 20:02:29 +01:00
CLOUDSTACK-8607 - Improving unit tests
- Adding more unit tests as suggested by Daan and Rajani. - Mocking SshHelper and the Script classes, used by Citrix and Libvirt resources respectively.
This commit is contained in:
parent
86297e70be
commit
e348e76eee
@ -19,8 +19,6 @@
|
||||
|
||||
package com.cloud.hypervisor.kvm.resource.wrapper;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.agent.api.Answer;
|
||||
import com.cloud.agent.api.UpdateHostPasswordCommand;
|
||||
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
|
||||
@ -31,15 +29,14 @@ import com.cloud.utils.script.Script;
|
||||
@ResourceWrapper(handles = UpdateHostPasswordCommand.class)
|
||||
public final class LibvirtUpdateHostPasswordCommandWrapper extends CommandWrapper<UpdateHostPasswordCommand, Answer, LibvirtComputingResource> {
|
||||
|
||||
private static final Logger s_logger = Logger.getLogger(LibvirtUpdateHostPasswordCommandWrapper.class);
|
||||
private static final int TIMEOUT = 10000;
|
||||
|
||||
@Override
|
||||
public Answer execute(final UpdateHostPasswordCommand command, final LibvirtComputingResource libvirtComputingResource) {
|
||||
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
|
||||
|
||||
final String username = command.getUsername();
|
||||
final String newPassword = command.getNewPassword();
|
||||
|
||||
final Script script = new Script(libvirtComputingResource.getUpdateHostPasswdPath(), TIMEOUT, s_logger);
|
||||
final Script script = libvirtUtilitiesHelper.buildScript(libvirtComputingResource.getUpdateHostPasswdPath());
|
||||
script.add(username, newPassword);
|
||||
final String result = script.execute();
|
||||
|
||||
|
||||
@ -31,15 +31,16 @@ import com.cloud.storage.StorageLayer;
|
||||
import com.cloud.storage.template.Processor;
|
||||
import com.cloud.storage.template.QCOW2Processor;
|
||||
import com.cloud.storage.template.TemplateLocation;
|
||||
import com.cloud.utils.script.Script;
|
||||
|
||||
/**
|
||||
* This class is used to wrap the calls to several static methods. By doing so, we make easier to mock this class
|
||||
* and the methods wrapped here.
|
||||
*
|
||||
* Please do not instantiate this class directly, but inject it using the {@code @Inject} annotation.
|
||||
*/
|
||||
public class LibvirtUtilitiesHelper {
|
||||
|
||||
public static final int TIMEOUT = 10000;
|
||||
|
||||
public Connect getConnectionByVmName(final String vmName) throws LibvirtException {
|
||||
return LibvirtConnection.getConnectionByVmName(vmName);
|
||||
}
|
||||
@ -90,4 +91,9 @@ public class LibvirtUtilitiesHelper {
|
||||
public Connect retrieveQemuConnection(final String qemuURI) throws LibvirtException {
|
||||
return new Connect(qemuURI);
|
||||
}
|
||||
|
||||
public Script buildScript(final String scriptPath) {
|
||||
final Script script = new Script(scriptPath, TIMEOUT);
|
||||
return script;
|
||||
}
|
||||
}
|
||||
@ -168,6 +168,7 @@ import com.cloud.storage.template.TemplateLocation;
|
||||
import com.cloud.template.VirtualMachineTemplate.BootloaderType;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
import com.cloud.utils.script.Script;
|
||||
import com.cloud.vm.DiskProfile;
|
||||
import com.cloud.vm.VirtualMachine;
|
||||
import com.cloud.vm.VirtualMachine.PowerState;
|
||||
@ -5076,12 +5077,45 @@ public class LibvirtComputingResourceTest {
|
||||
|
||||
@Test
|
||||
public void testUpdateHostPasswordCommand() {
|
||||
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
|
||||
final Script script = Mockito.mock(Script.class);
|
||||
|
||||
final String hostIp = "127.0.0.1";
|
||||
final String username = "root";
|
||||
final String newPassword = "password";
|
||||
|
||||
final UpdateHostPasswordCommand command = new UpdateHostPasswordCommand(username, newPassword, hostIp);
|
||||
|
||||
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
|
||||
when(libvirtComputingResource.getUpdateHostPasswdPath()).thenReturn("/tmp");
|
||||
when(libvirtUtilitiesHelper.buildScript(libvirtComputingResource.getUpdateHostPasswdPath())).thenReturn(script);
|
||||
|
||||
when(script.execute()).thenReturn(null);
|
||||
|
||||
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
|
||||
assertNotNull(wrapper);
|
||||
|
||||
final Answer answer = wrapper.execute(command, libvirtComputingResource);
|
||||
|
||||
assertTrue(answer.getResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateHostPasswordCommandFail() {
|
||||
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
|
||||
final Script script = Mockito.mock(Script.class);
|
||||
|
||||
final String hostIp = "127.0.0.1";
|
||||
final String username = "root";
|
||||
final String newPassword = "password";
|
||||
|
||||
final UpdateHostPasswordCommand command = new UpdateHostPasswordCommand(username, newPassword, hostIp);
|
||||
|
||||
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
|
||||
when(libvirtComputingResource.getUpdateHostPasswdPath()).thenReturn("/tmp");
|
||||
when(libvirtUtilitiesHelper.buildScript(libvirtComputingResource.getUpdateHostPasswdPath())).thenReturn(script);
|
||||
|
||||
when(script.execute()).thenReturn("#FAIL");
|
||||
|
||||
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
|
||||
assertNotNull(wrapper);
|
||||
|
||||
@ -103,6 +103,7 @@ import com.cloud.exception.InternalErrorException;
|
||||
import com.cloud.host.Host.Type;
|
||||
import com.cloud.hypervisor.Hypervisor.HypervisorType;
|
||||
import com.cloud.hypervisor.xenserver.resource.wrapper.xenbase.CitrixRequestWrapper;
|
||||
import com.cloud.hypervisor.xenserver.resource.wrapper.xenbase.XenServerUtilitiesHelper;
|
||||
import com.cloud.network.Networks;
|
||||
import com.cloud.network.Networks.BroadcastDomainType;
|
||||
import com.cloud.network.Networks.TrafficType;
|
||||
@ -259,6 +260,8 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
|
||||
protected String _configDriveSRName = "ConfigDriveISOs";
|
||||
protected String _attachIsoDeviceNum = "3";
|
||||
|
||||
protected XenServerUtilitiesHelper xenServerUtilitiesHelper = new XenServerUtilitiesHelper();
|
||||
|
||||
protected int _wait;
|
||||
// Hypervisor specific params with generic value, may need to be overridden
|
||||
// for specific versions
|
||||
@ -288,6 +291,10 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
|
||||
return _password.peek();
|
||||
}
|
||||
|
||||
public XenServerUtilitiesHelper getXenServerUtilitiesHelper() {
|
||||
return xenServerUtilitiesHelper;
|
||||
}
|
||||
|
||||
protected StorageSubsystemCommandHandler buildStorageHandler() {
|
||||
final XenServerStorageProcessor processor = new XenServerStorageProcessor(this);
|
||||
return new StorageSubsystemCommandHandlerBase(processor);
|
||||
@ -5266,7 +5273,6 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void deleteLocalFolder(final String directory) throws Exception {
|
||||
if (directory == null || directory.isEmpty()) {
|
||||
final String msg = "Invalid directory path (null/empty) detected. Cannot delete specified directory.";
|
||||
@ -5283,4 +5289,4 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -19,6 +19,8 @@
|
||||
|
||||
package com.cloud.hypervisor.xenserver.resource.wrapper.xenbase;
|
||||
|
||||
import static com.cloud.hypervisor.xenserver.resource.wrapper.xenbase.XenServerUtilitiesHelper.SCRIPT_CMD_PATH;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.agent.api.Answer;
|
||||
@ -28,13 +30,11 @@ import com.cloud.hypervisor.xenserver.resource.CitrixResourceBase;
|
||||
import com.cloud.resource.CommandWrapper;
|
||||
import com.cloud.resource.ResourceWrapper;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.ssh.SshHelper;
|
||||
|
||||
@ResourceWrapper(handles = UpdateHostPasswordCommand.class)
|
||||
public final class CitrixUpdateHostPasswordCommandWrapper extends CommandWrapper<UpdateHostPasswordCommand, Answer, CitrixResourceBase> {
|
||||
|
||||
private static final Logger s_logger = Logger.getLogger(CitrixUpdateHostPasswordCommandWrapper.class);
|
||||
private static final int TIMEOUT = 10000;
|
||||
|
||||
@Override
|
||||
public Answer execute(final UpdateHostPasswordCommand command, final CitrixResourceBase citrixResourceBase) {
|
||||
@ -42,19 +42,14 @@ public final class CitrixUpdateHostPasswordCommandWrapper extends CommandWrapper
|
||||
final String username = command.getUsername();
|
||||
final String newPassword = command.getNewPassword();
|
||||
|
||||
final StringBuffer cmdLine = new StringBuffer();
|
||||
cmdLine.append("sh /opt/cloud/bin/");
|
||||
cmdLine.append(VRScripts.UPDATE_HOST_PASSWD);
|
||||
cmdLine.append(' ');
|
||||
cmdLine.append(username);
|
||||
cmdLine.append(' ');
|
||||
cmdLine.append(newPassword);
|
||||
final XenServerUtilitiesHelper xenServerUtilitiesHelper = citrixResourceBase.getXenServerUtilitiesHelper();
|
||||
final String cmdLine = xenServerUtilitiesHelper.buildCommandLine(SCRIPT_CMD_PATH, VRScripts.UPDATE_HOST_PASSWD, username, newPassword);
|
||||
|
||||
Pair<Boolean, String> result;
|
||||
|
||||
try {
|
||||
s_logger.debug("Executing command in Host: " + cmdLine);
|
||||
result = SshHelper.sshExecute(hostIp, 22, username, null, citrixResourceBase.getPwdFromQueue(), cmdLine.toString(), 60000, 60000, TIMEOUT);
|
||||
final String hostPassword = citrixResourceBase.getPwdFromQueue();
|
||||
result = xenServerUtilitiesHelper.executeSshWrapper(hostIp, 22, username, null, hostPassword, cmdLine.toString());
|
||||
} catch (final Exception e) {
|
||||
return new Answer(command, false, e.getMessage());
|
||||
}
|
||||
|
||||
@ -0,0 +1,45 @@
|
||||
// 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.xenbase;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.ssh.SshHelper;
|
||||
|
||||
|
||||
/**
|
||||
* This class is used to wrap the calls to several static methods. By doing so, we make easier to mock this class
|
||||
* and the methods wrapped here.
|
||||
*/
|
||||
public class XenServerUtilitiesHelper {
|
||||
|
||||
public static final int TIMEOUT = 10000;
|
||||
public static final String SCRIPT_CMD_PATH = "sh /opt/cloud/bin/";
|
||||
|
||||
public Pair<Boolean, String> executeSshWrapper(final String hostIp, final int port, final String username, final File pemFile, final String hostPasswd, final String command) throws Exception {
|
||||
final Pair<Boolean, String> result = SshHelper.sshExecute(hostIp, port, username, pemFile, hostPasswd, command, 60000, 60000, TIMEOUT);
|
||||
return result;
|
||||
}
|
||||
|
||||
public String buildCommandLine(final String scriptPath, final String script, final String username, final String newPassword) {
|
||||
final StringBuilder cmdLine = new StringBuilder();
|
||||
cmdLine.append(scriptPath).append(script).append(' ').append(username).append(' ').append(newPassword);
|
||||
|
||||
return cmdLine.toString();
|
||||
}
|
||||
}
|
||||
@ -130,7 +130,7 @@ import com.cloud.storage.Storage.ImageFormat;
|
||||
import com.cloud.storage.Storage.StoragePoolType;
|
||||
import com.cloud.storage.VMTemplateStorageResourceAssoc;
|
||||
import com.cloud.storage.resource.StorageSubsystemCommandHandler;
|
||||
import com.cloud.utils.ExecutionResult;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.vm.DiskProfile;
|
||||
import com.cloud.vm.VirtualMachine;
|
||||
import com.xensource.xenapi.Connection;
|
||||
@ -1320,24 +1320,141 @@ public class CitrixRequestWrapperTest {
|
||||
assertFalse(answer.getResult());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testUpdateHostPasswordCommand() {
|
||||
final ExecutionResult executionResult = Mockito.mock(ExecutionResult.class);
|
||||
final XenServerUtilitiesHelper xenServerUtilitiesHelper = Mockito.mock(XenServerUtilitiesHelper.class);
|
||||
final Pair<Boolean, String> result = Mockito.mock(Pair.class);
|
||||
|
||||
final UpdateHostPasswordCommand updatePwd = new UpdateHostPasswordCommand("test", "123", "127.0.0.1");
|
||||
|
||||
final StringBuffer buff = new StringBuffer();
|
||||
buff.append(updatePwd.getUsername());
|
||||
buff.append(' ');
|
||||
buff.append(updatePwd.getNewPassword());
|
||||
when(citrixResourceBase.getPwdFromQueue()).thenReturn("password");
|
||||
|
||||
when(citrixResourceBase.executeInVR(updatePwd.getHostIp(), VRScripts.UPDATE_HOST_PASSWD, buff.toString())).thenReturn(executionResult);
|
||||
final String hostIp = updatePwd.getHostIp();
|
||||
final String username = updatePwd.getUsername();
|
||||
final String hostPasswd = citrixResourceBase.getPwdFromQueue();
|
||||
final String newPassword = updatePwd.getNewPassword();
|
||||
|
||||
final StringBuilder cmdLine = new StringBuilder();
|
||||
cmdLine.append(XenServerUtilitiesHelper.SCRIPT_CMD_PATH).append(VRScripts.UPDATE_HOST_PASSWD).append(' ').append(username).append(' ').append(newPassword);
|
||||
|
||||
when(citrixResourceBase.getXenServerUtilitiesHelper()).thenReturn(xenServerUtilitiesHelper);
|
||||
when(xenServerUtilitiesHelper.buildCommandLine(XenServerUtilitiesHelper.SCRIPT_CMD_PATH, VRScripts.UPDATE_HOST_PASSWD, username, newPassword)).thenReturn(cmdLine.toString());
|
||||
|
||||
try {
|
||||
when(xenServerUtilitiesHelper.executeSshWrapper(hostIp, 22, username, null, hostPasswd, cmdLine.toString())).thenReturn(result);
|
||||
when(result.first()).thenReturn(true);
|
||||
when(result.second()).thenReturn("");
|
||||
} catch (final Exception e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
|
||||
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
|
||||
assertNotNull(wrapper);
|
||||
|
||||
final Answer answer = wrapper.execute(updatePwd, citrixResourceBase);
|
||||
|
||||
verify(citrixResourceBase, times(2)).getPwdFromQueue();
|
||||
verify(citrixResourceBase, times(1)).getXenServerUtilitiesHelper();
|
||||
verify(xenServerUtilitiesHelper, times(1)).buildCommandLine(XenServerUtilitiesHelper.SCRIPT_CMD_PATH, VRScripts.UPDATE_HOST_PASSWD, username, newPassword);
|
||||
try {
|
||||
verify(xenServerUtilitiesHelper, times(1)).executeSshWrapper(hostIp, 22, username, null, hostPasswd, cmdLine.toString());
|
||||
} catch (final Exception e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
verify(result, times(1)).first();
|
||||
verify(result, times(1)).second();
|
||||
|
||||
assertTrue(answer.getResult());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testUpdateHostPasswordCommandFail() {
|
||||
final XenServerUtilitiesHelper xenServerUtilitiesHelper = Mockito.mock(XenServerUtilitiesHelper.class);
|
||||
final Pair<Boolean, String> result = Mockito.mock(Pair.class);
|
||||
|
||||
final UpdateHostPasswordCommand updatePwd = new UpdateHostPasswordCommand("test", "123", "127.0.0.1");
|
||||
|
||||
when(citrixResourceBase.getPwdFromQueue()).thenReturn("password");
|
||||
|
||||
final String hostIp = updatePwd.getHostIp();
|
||||
final String username = updatePwd.getUsername();
|
||||
final String hostPasswd = citrixResourceBase.getPwdFromQueue();
|
||||
final String newPassword = updatePwd.getNewPassword();
|
||||
|
||||
final StringBuilder cmdLine = new StringBuilder();
|
||||
cmdLine.append(XenServerUtilitiesHelper.SCRIPT_CMD_PATH).append(VRScripts.UPDATE_HOST_PASSWD).append(' ').append(username).append(' ').append(newPassword);
|
||||
|
||||
when(citrixResourceBase.getXenServerUtilitiesHelper()).thenReturn(xenServerUtilitiesHelper);
|
||||
when(xenServerUtilitiesHelper.buildCommandLine(XenServerUtilitiesHelper.SCRIPT_CMD_PATH, VRScripts.UPDATE_HOST_PASSWD, username, newPassword)).thenReturn(cmdLine.toString());
|
||||
|
||||
try {
|
||||
when(xenServerUtilitiesHelper.executeSshWrapper(hostIp, 22, username, null, hostPasswd, cmdLine.toString())).thenReturn(result);
|
||||
when(result.first()).thenReturn(false);
|
||||
when(result.second()).thenReturn("");
|
||||
} catch (final Exception e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
|
||||
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
|
||||
assertNotNull(wrapper);
|
||||
|
||||
final Answer answer = wrapper.execute(updatePwd, citrixResourceBase);
|
||||
|
||||
verify(citrixResourceBase, times(2)).getPwdFromQueue();
|
||||
verify(citrixResourceBase, times(1)).getXenServerUtilitiesHelper();
|
||||
verify(xenServerUtilitiesHelper, times(1)).buildCommandLine(XenServerUtilitiesHelper.SCRIPT_CMD_PATH, VRScripts.UPDATE_HOST_PASSWD, username, newPassword);
|
||||
try {
|
||||
verify(xenServerUtilitiesHelper, times(1)).executeSshWrapper(hostIp, 22, username, null, hostPasswd, cmdLine.toString());
|
||||
} catch (final Exception e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
verify(result, times(1)).first();
|
||||
verify(result, times(1)).second();
|
||||
|
||||
assertFalse(answer.getResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateHostPasswordCommandException() {
|
||||
final XenServerUtilitiesHelper xenServerUtilitiesHelper = Mockito.mock(XenServerUtilitiesHelper.class);
|
||||
|
||||
final UpdateHostPasswordCommand updatePwd = new UpdateHostPasswordCommand("test", "123", "127.0.0.1");
|
||||
|
||||
when(citrixResourceBase.getPwdFromQueue()).thenReturn("password");
|
||||
|
||||
final String hostIp = updatePwd.getHostIp();
|
||||
final String username = updatePwd.getUsername();
|
||||
final String hostPasswd = citrixResourceBase.getPwdFromQueue();
|
||||
final String newPassword = updatePwd.getNewPassword();
|
||||
|
||||
final StringBuilder cmdLine = new StringBuilder();
|
||||
cmdLine.append(XenServerUtilitiesHelper.SCRIPT_CMD_PATH).append(VRScripts.UPDATE_HOST_PASSWD).append(' ').append(username).append(' ').append(newPassword);
|
||||
|
||||
when(citrixResourceBase.getXenServerUtilitiesHelper()).thenReturn(xenServerUtilitiesHelper);
|
||||
when(xenServerUtilitiesHelper.buildCommandLine(XenServerUtilitiesHelper.SCRIPT_CMD_PATH, VRScripts.UPDATE_HOST_PASSWD, username, newPassword)).thenReturn(cmdLine.toString());
|
||||
|
||||
try {
|
||||
when(xenServerUtilitiesHelper.executeSshWrapper(hostIp, 22, username, null, hostPasswd, cmdLine.toString())).thenThrow(new Exception("testing failure"));
|
||||
} catch (final Exception e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
|
||||
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
|
||||
assertNotNull(wrapper);
|
||||
|
||||
final Answer answer = wrapper.execute(updatePwd, citrixResourceBase);
|
||||
|
||||
verify(citrixResourceBase, times(2)).getPwdFromQueue();
|
||||
verify(citrixResourceBase, times(1)).getXenServerUtilitiesHelper();
|
||||
verify(xenServerUtilitiesHelper, times(1)).buildCommandLine(XenServerUtilitiesHelper.SCRIPT_CMD_PATH, VRScripts.UPDATE_HOST_PASSWD, username, newPassword);
|
||||
try {
|
||||
verify(xenServerUtilitiesHelper, times(1)).executeSshWrapper(hostIp, 22, username, null, hostPasswd, cmdLine.toString());
|
||||
} catch (final Exception e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
|
||||
assertFalse(answer.getResult());
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user