issue 384: modify SshDaemonIsRunning<IMachine> in SshAvailable<String> to address jclouds's comment

This commit is contained in:
andreaturli 2011-12-01 22:13:43 +00:00
parent c86e771a9c
commit c263c7b126
2 changed files with 38 additions and 21 deletions

View File

@ -19,7 +19,6 @@
package org.jclouds.virtualbox.predicates; package org.jclouds.virtualbox.predicates;
import static com.google.common.base.Throwables.propagate;
import static org.jclouds.compute.options.RunScriptOptions.Builder.wrapInInitScript; import static org.jclouds.compute.options.RunScriptOptions.Builder.wrapInInitScript;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -30,7 +29,6 @@ import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.ssh.SshException; import org.jclouds.ssh.SshException;
import org.virtualbox_4_1.IMachine;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
@ -38,35 +36,38 @@ import com.google.common.base.Predicate;
* *
* @author Andrea Turli * @author Andrea Turli
*/ */
public class SshDaemonIsRunning implements Predicate<IMachine> { public class SshAvailable implements Predicate<String> {
@Resource @Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER) @Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
private final ComputeServiceContext context; private final ComputeServiceContext context;
private final String nodeId;
public SshDaemonIsRunning(ComputeServiceContext context, String nodeId) { public SshAvailable(ComputeServiceContext context) {
this.context = context; this.context = context;
this.nodeId = nodeId;
} }
@Override @Override
public boolean apply(@Nullable IMachine machine) { public boolean apply(@Nullable String nodeId) {
boolean sshDeamonIsRunning = false; boolean sshDaemonIsRunning = false;
try { try {
if (context.getComputeService() if (context.getComputeService()
.runScriptOnNode(nodeId, "id", wrapInInitScript(false)) .runScriptOnNode(nodeId, "id", wrapInInitScript(false).runAsRoot(false))
.getExitCode() == 0) { .getExitCode() == 0) {
logger.debug("Got response from ssh daemon."); logger.debug("Got response from ssh daemon running on %s", nodeId);
sshDeamonIsRunning = true; sshDaemonIsRunning = true;
} }
} catch (SshException e) { } catch (SshException e) {
logger.debug("No response from ssh daemon..."); logger.debug("No response from ssh daemon running on %s", nodeId);
propagate(e); return sshDaemonIsRunning;
} }
return sshDeamonIsRunning; return sshDaemonIsRunning;
}
@Override
public String toString() {
return "SSH Daemon is available";
} }
} }

View File

@ -2,6 +2,9 @@ package org.jclouds.virtualbox.predicates;
import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest; import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest;
import static org.jclouds.virtualbox.util.MachineUtils.applyForMachine; import static org.jclouds.virtualbox.util.MachineUtils.applyForMachine;
import static org.jclouds.virtualbox.util.MachineUtils.lockSessionOnMachineAndApply;
import static org.virtualbox_4_1.LockType.Shared;
import static org.testng.Assert.assertTrue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -16,12 +19,15 @@ import org.jclouds.virtualbox.functions.IsoToIMachine;
import org.jclouds.virtualbox.functions.LaunchMachineIfNotAlreadyRunning; import org.jclouds.virtualbox.functions.LaunchMachineIfNotAlreadyRunning;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.VirtualBoxManager; import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
@Test(groups = "live", singleThreaded = true, testName = "IsoToIMachineLiveTest") @Test(groups = "live", singleThreaded = true, testName = "IsoToIMachineLiveTest")
public class SshDaemonIsRunningLiveTest extends BaseVirtualBoxClientLiveTest { public class SshAvailableLiveTest extends BaseVirtualBoxClientLiveTest {
private boolean forceOverwrite = true; private boolean forceOverwrite = true;
private String vmId = "jclouds-image-iso-1"; private String vmId = "jclouds-image-iso-1";
@ -43,13 +49,23 @@ public class SshDaemonIsRunningLiveTest extends BaseVirtualBoxClientLiveTest {
hostId, "localhost", guestId, "localhost", new Credentials("toor", hostId, "localhost", guestId, "localhost", new Credentials("toor",
"password")); "password"));
IMachine nodeWithSshDaemonRunning = getNodeWithSshDaemonRunning(manager, getNodeWithSshDaemonRunning(manager, localHostContext);
localHostContext);
ensureMachineIsLaunched(vmName); ensureMachineIsLaunched(vmName);
RetryablePredicate<IMachine> predicate = new RetryablePredicate<IMachine>( RetryablePredicate<String> predicate = new RetryablePredicate<String>(
new SshDaemonIsRunning(localHostContext, guestId), 5, 1, new SshAvailable(localHostContext), 5, 1,
TimeUnit.SECONDS); TimeUnit.SECONDS);
predicate.apply(nodeWithSshDaemonRunning); assertTrue(predicate.apply(guestId));
lockSessionOnMachineAndApply(manager, Shared, vmName, new Function<ISession, Void>() {
@Override
public Void apply(ISession session) {
IProgress powerDownProgress = session.getConsole().powerDown();
powerDownProgress.waitForCompletion(-1);
return null;
}
});
} }
private IMachine getNodeWithSshDaemonRunning(VirtualBoxManager manager, private IMachine getNodeWithSshDaemonRunning(VirtualBoxManager manager,