cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wid...@apache.org
Subject [16/50] git commit: updated refs/heads/CLOUDSTACK-8581 to 1a2e80b
Date Fri, 10 Jul 2015 10:08:11 GMT
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.


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/e348e76e
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/e348e76e
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/e348e76e

Branch: refs/heads/CLOUDSTACK-8581
Commit: e348e76eee60d11eecf4c127cc2c9d03a72a96cb
Parents: 86297e7
Author: wilderrodrigues <wrodrigues@schubergphilis.com>
Authored: Mon Jul 6 14:58:17 2015 +0200
Committer: wilderrodrigues <wrodrigues@schubergphilis.com>
Committed: Mon Jul 6 14:58:17 2015 +0200

----------------------------------------------------------------------
 ...LibvirtUpdateHostPasswordCommandWrapper.java |   9 +-
 .../wrapper/LibvirtUtilitiesHelper.java         |  10 +-
 .../resource/LibvirtComputingResourceTest.java  |  34 +++++
 .../xenserver/resource/CitrixResourceBase.java  |  10 +-
 .../CitrixUpdateHostPasswordCommandWrapper.java |  17 +--
 .../xenbase/XenServerUtilitiesHelper.java       |  45 +++++++
 .../xenbase/CitrixRequestWrapperTest.java       | 131 ++++++++++++++++++-
 7 files changed, 228 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e348e76e/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUpdateHostPasswordCommandWrapper.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUpdateHostPasswordCommandWrapper.java
b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUpdateHostPasswordCommandWrapper.java
index 4d7d234..b0fd1c8 100644
--- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUpdateHostPasswordCommandWrapper.java
+++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUpdateHostPasswordCommandWrapper.java
@@ -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();
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e348e76e/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java
b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java
index 8c8f6e6..1a1a6d3 100644
--- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java
+++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java
@@ -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;
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e348e76e/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java
b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java
index 8ccdce5..42ebf86 100644
--- a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java
+++ b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java
@@ -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);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e348e76e/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java
b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java
index b205f2d..48e721f 100644
--- a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java
+++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java
@@ -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
         }
     }
 
-}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e348e76e/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixUpdateHostPasswordCommandWrapper.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixUpdateHostPasswordCommandWrapper.java
b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixUpdateHostPasswordCommandWrapper.java
index 9e58678..015d9f4 100644
--- a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixUpdateHostPasswordCommandWrapper.java
+++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixUpdateHostPasswordCommandWrapper.java
@@ -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());
         }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e348e76e/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/XenServerUtilitiesHelper.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/XenServerUtilitiesHelper.java
b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/XenServerUtilitiesHelper.java
new file mode 100644
index 0000000..9a35406
--- /dev/null
+++ b/plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/XenServerUtilitiesHelper.java
@@ -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();
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e348e76e/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixRequestWrapperTest.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixRequestWrapperTest.java
b/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixRequestWrapperTest.java
index 91f59be..39dd075 100644
--- a/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixRequestWrapperTest.java
+++ b/plugins/hypervisors/xenserver/test/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixRequestWrapperTest.java
@@ -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());
     }
 


Mime
View raw message