Fixing testModifySshKeysCommand in the LibvirtComputingResourceTest class - The test was okay, but when running in an environment where a /root/.ssh/id_rsa existed, it would return true then fail - We now mock the calls to methods that return the key paths, instead of relying in the static variables
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/f575206a Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/f575206a Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/f575206a Branch: refs/heads/CLOUDSTACK-8301 Commit: f575206ad4348fb7fc0fe312a1e797bac23d011b Parents: b284b84 Author: wilderrodrigues <[email protected]> Authored: Fri May 8 19:53:03 2015 +0200 Committer: wilderrodrigues <[email protected]> Committed: Fri May 8 19:53:03 2015 +0200 ---------------------------------------------------------------------- .../LibvirtModifySshKeysCommandWrapper.java | 27 ++++++++++++-------- .../wrapper/LibvirtUtilitiesHelper.java | 13 ++++++++++ .../resource/LibvirtComputingResourceTest.java | 7 +++++ 3 files changed, 37 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f575206a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtModifySshKeysCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtModifySshKeysCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtModifySshKeysCommandWrapper.java index d5943c5..1295e7d 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtModifySshKeysCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtModifySshKeysCommandWrapper.java @@ -38,21 +38,28 @@ public final class LibvirtModifySshKeysCommandWrapper extends CommandWrapper<Mod @Override public Answer execute(final ModifySshKeysCommand command, final LibvirtComputingResource libvirtComputingResource) { - final File sshKeysDir = new File(LibvirtComputingResource.SSHKEYSPATH); + + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper(); + + final String sshkeyspath = libvirtUtilitiesHelper.retrieveSshKeysPath(); + final String sshpubkeypath = libvirtUtilitiesHelper.retrieveSshPubKeyPath(); + final String sshprvkeypath = libvirtUtilitiesHelper.retrieveSshPrvKeyPath(); + + final File sshKeysDir = new File(sshkeyspath); String result = null; if (!sshKeysDir.exists()) { // Change permissions for the 700 final Script script = new Script("mkdir", libvirtComputingResource.getTimeout(), s_logger); script.add("-m", "700"); - script.add(LibvirtComputingResource.SSHKEYSPATH); + script.add(sshkeyspath); script.execute(); if (!sshKeysDir.exists()) { - s_logger.debug("failed to create directory " + LibvirtComputingResource.SSHKEYSPATH); + s_logger.debug("failed to create directory " + sshkeyspath); } } - final File pubKeyFile = new File(LibvirtComputingResource.SSHPUBKEYPATH); + final File pubKeyFile = new File(sshpubkeypath); if (!pubKeyFile.exists()) { try { pubKeyFile.createNewFile(); @@ -66,16 +73,16 @@ public final class LibvirtModifySshKeysCommandWrapper extends CommandWrapper<Mod try (FileOutputStream pubkStream = new FileOutputStream(pubKeyFile)) { pubkStream.write(command.getPubKey().getBytes()); } catch (final FileNotFoundException e) { - result = "File" + LibvirtComputingResource.SSHPUBKEYPATH + "is not found:" + result = "File" + sshpubkeypath + "is not found:" + e.toString(); s_logger.debug(result); } catch (final IOException e) { - result = "Write file " + LibvirtComputingResource.SSHPUBKEYPATH + ":" + e.toString(); + result = "Write file " + sshpubkeypath + ":" + e.toString(); s_logger.debug(result); } } - final File prvKeyFile = new File(LibvirtComputingResource.SSHPRVKEYPATH); + final File prvKeyFile = new File(sshprvkeypath); if (!prvKeyFile.exists()) { try { prvKeyFile.createNewFile(); @@ -92,14 +99,14 @@ public final class LibvirtModifySshKeysCommandWrapper extends CommandWrapper<Mod prvKStream.write(prvKey.getBytes()); } } catch (final FileNotFoundException e) { - result = "File" + LibvirtComputingResource.SSHPRVKEYPATH + "is not found:" + e.toString(); + result = "File" + sshprvkeypath + "is not found:" + e.toString(); s_logger.debug(result); } catch (final IOException e) { - result = "Write file " + LibvirtComputingResource.SSHPRVKEYPATH + ":" + e.toString(); + result = "Write file " + sshprvkeypath + ":" + e.toString(); s_logger.debug(result); } final Script script = new Script("chmod", libvirtComputingResource.getTimeout(), s_logger); - script.add("600", LibvirtComputingResource.SSHPRVKEYPATH); + script.add("600", sshprvkeypath); script.execute(); } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f575206a/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 74a41c6..dfff12d 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 @@ -25,6 +25,7 @@ import javax.naming.ConfigurationException; import org.libvirt.Connect; import org.libvirt.LibvirtException; +import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; import com.cloud.hypervisor.kvm.resource.LibvirtConnection; import com.cloud.storage.StorageLayer; import com.cloud.storage.template.Processor; @@ -69,4 +70,16 @@ public class LibvirtUtilitiesHelper { public Connect getConnectionByType(final String hvsType) throws LibvirtException { return LibvirtConnection.getConnectionByType(hvsType); } + + public String retrieveSshKeysPath() { + return LibvirtComputingResource.SSHKEYSPATH; + } + + public String retrieveSshPubKeyPath() { + return LibvirtComputingResource.SSHPUBKEYPATH; + } + + public String retrieveSshPrvKeyPath() { + return LibvirtComputingResource.SSHPRVKEYPATH; + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f575206a/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 4a1dcc4..36227a5 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 @@ -1711,6 +1711,13 @@ public class LibvirtComputingResourceTest { public void testModifySshKeysCommand() { final ModifySshKeysCommand command = new ModifySshKeysCommand("", ""); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class); + when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper); + + when(libvirtUtilitiesHelper.retrieveSshKeysPath()).thenReturn("/path/keys"); + when(libvirtUtilitiesHelper.retrieveSshPubKeyPath()).thenReturn("/path/pub/keys"); + when(libvirtUtilitiesHelper.retrieveSshPrvKeyPath()).thenReturn("/path/pvt/keys"); + when(libvirtComputingResource.getTimeout()).thenReturn(0); final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
