From e27db18cfde4526fe4ab0135b17b3f0d0341d024 Mon Sep 17 00:00:00 2001 From: Andrea Turli Date: Wed, 28 Mar 2012 16:00:49 +0100 Subject: [PATCH] issue 384: vbox 1.5.1 --- ...VirtualBoxComputeServiceContextModule.java | 5 +- .../jclouds/virtualbox/domain/BridgedIf.java | 5 +- .../virtualbox/domain/NetworkAdapter.java | 6 +- .../domain/NetworkInterfaceCard.java | 4 +- .../functions/AttachNicToMachine.java | 48 +++-- ...MachineFromIMachineIfNotAlreadyExists.java | 2 +- .../functions/CreateAndInstallVm.java | 24 ++- .../functions/IMachineToNodeMetadata.java | 59 +++++- .../functions/IMachineToSshClient.java | 8 +- .../LaunchMachineIfNotAlreadyRunning.java | 5 + .../functions/MastersLoadingCache.java | 3 +- .../virtualbox/functions/NodeCreator.java | 49 +---- .../statements/EnableNetworkInterface.java | 88 +++++++++ .../statements/InstallGuestAdditions.java | 2 + .../virtualbox/util/MachineController.java | 187 ++++++++++-------- .../jclouds/virtualbox/util/MachineUtils.java | 79 +++++++- .../functions/cleanupUdevIfNeeded.sh | 6 +- .../installModuleAssistantIfNeeded.sh | 2 +- .../BaseVirtualBoxClientLiveTest.java | 11 -- ...rtualBoxComputeServiceAdapterLiveTest.java | 3 +- .../compute/VirtualBoxExperimentLiveTest.java | 5 +- ...romIMachineIfNotAlreadyExistsLiveTest.java | 91 +++------ .../functions/CreateAndInstallVmLiveTest.java | 91 ++++++--- ...hineFromIsoIfNotAlreadyExistsLiveTest.java | 140 ++++++------- .../functions/IMachineToNodeMetadataTest.java | 15 +- ...eveActiveBridgedInterfacesExpectTest.java} | 20 +- .../StartVBoxIfNotAlreadyRunningLiveTest.java | 10 +- .../GuestAdditionsInstallerLiveTest.java | 166 ++++++++-------- .../IMachinePredicatesLiveTest.java | 127 ++++++------ .../InstallGuestAdditionsLiveTest.java | 17 +- .../util/MachineControllerLiveTest.java | 5 +- 31 files changed, 747 insertions(+), 536 deletions(-) create mode 100644 labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/EnableNetworkInterface.java rename labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/{RetrieveActiveBridgedInterfacesLiveTest.java => RetrieveActiveBridgedInterfacesExpectTest.java} (79%) diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java index 7ca72d7108..a5a5b4a9cd 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java @@ -27,6 +27,7 @@ import java.net.URI; import java.util.Map; import java.util.Set; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; import javax.inject.Named; import javax.inject.Singleton; @@ -158,7 +159,7 @@ public class VirtualBoxComputeServiceContextModule extends bind(new TypeLiteral>() { }).to(IMachineToSshClient.class); - bind(ExecutionType.class).toInstance(ExecutionType.GUI); + bind(ExecutionType.class).toInstance(ExecutionType.HEADLESS); bind(LockType.class).toInstance(LockType.Write); } @@ -213,7 +214,7 @@ public class VirtualBoxComputeServiceContextModule extends @Provides @Singleton protected Predicate sshResponds(SshResponds sshResponds, Timeouts timeouts) { - return new RetryablePredicate(sshResponds, timeouts.nodeRunning); + return new RetryablePredicate(sshResponds, timeouts.nodeRunning, 500l, TimeUnit.MILLISECONDS); } @Override diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/BridgedIf.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/BridgedIf.java index 2584986fa3..a3b2d80b40 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/BridgedIf.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/BridgedIf.java @@ -166,15 +166,12 @@ public class BridgedIf { public boolean equals(Object o) { if (this == o) return true; - if (o instanceof VmSpec) { + if (o instanceof BridgedIf) { BridgedIf other = (BridgedIf) o; return Objects.equal(name, other.name) - && Objects.equal(guid, other.guid) && Objects.equal(dhcp, other.dhcp) && Objects.equal(ipAddress, other.ipAddress) && Objects.equal(networkMask, other.networkMask) - && Objects.equal(ipv6Address, other.ipv6Address) - && Objects.equal(ipv6NetworkMask, other.ipv6NetworkMask) && Objects.equal(mediumType, other.mediumType) && Objects.equal(status, other.status); } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkAdapter.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkAdapter.java index 54ccac7392..9b6c203a82 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkAdapter.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkAdapter.java @@ -167,10 +167,10 @@ public class NetworkAdapter { @Override public String toString() { - return "NetworkAdapter{" + "networkAttachmentType= "+ + return "NetworkAdapter{networkAttachmentType="+ networkAttachmentType + - "macAddress= " + macAddress + - "redirectRules= " + redirectRules + + ", macAddress=" + macAddress + + ", redirectRules=" + redirectRules + '}'; } } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkInterfaceCard.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkInterfaceCard.java index 8d338bd108..57f565e802 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkInterfaceCard.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkInterfaceCard.java @@ -101,9 +101,9 @@ public class NetworkInterfaceCard { @Override public String toString() { - return "NetworkInterfaceCard{" + "slot= "+ + return "NetworkInterfaceCard{slot="+ slot + - "networkAdapter= " + networkAdapter + + ", networkAdapter=" + networkAdapter + '}'; } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachNicToMachine.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachNicToMachine.java index daaa28c610..586bc41afd 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachNicToMachine.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachNicToMachine.java @@ -33,38 +33,34 @@ import com.google.common.base.Function; */ public class AttachNicToMachine implements Function { - private final String vmName; - private final MachineUtils machineUtils; + private final String vmName; + private final MachineUtils machineUtils; + public AttachNicToMachine(String vmName, MachineUtils machineUtils) { + this.vmName = checkNotNull(vmName, "vmName"); + this.machineUtils = checkNotNull(machineUtils, "machineUtils"); + } - public AttachNicToMachine(String vmName, MachineUtils machineUtils) { - this.vmName = checkNotNull(vmName, "vmName"); - this.machineUtils = checkNotNull(machineUtils, "machineUtils"); - } - - @Override + @Override public Void apply(@Nullable NetworkInterfaceCard nic) { - if(hasNatAdapter(nic)) { - return machineUtils.writeLockMachineAndApply(vmName, new AttachNATAdapterToMachineIfNotAlreadyExists(nic)); - - } else if (hasBridgedAdapter(nic)) { - return machineUtils.writeLockMachineAndApply(vmName, new AttachBridgedAdapterToMachine(nic)); + if (hasNatAdapter(nic)) { + return machineUtils.writeLockMachineAndApply(vmName, new AttachNATAdapterToMachineIfNotAlreadyExists(nic)); + } else if (hasBridgedAdapter(nic)) { + return machineUtils.writeLockMachineAndApply(vmName, new AttachBridgedAdapterToMachine(nic)); } else if (hasHostOnlyAdapter(nic)) { return machineUtils.writeLockMachineAndApply(vmName, new AttachHostOnlyAdapter(nic)); - } else - return null; - } - - private boolean hasNatAdapter(NetworkInterfaceCard nic) { - return nic.getNetworkAdapter().getNetworkAttachmentType() - .equals(NetworkAttachmentType.NAT); - } - - private boolean hasBridgedAdapter(NetworkInterfaceCard nic) { - return nic.getNetworkAdapter().getNetworkAttachmentType() - .equals(NetworkAttachmentType.Bridged); + } else + return null; } - + + private boolean hasNatAdapter(NetworkInterfaceCard nic) { + return nic.getNetworkAdapter().getNetworkAttachmentType().equals(NetworkAttachmentType.NAT); + } + + private boolean hasBridgedAdapter(NetworkInterfaceCard nic) { + return nic.getNetworkAdapter().getNetworkAttachmentType().equals(NetworkAttachmentType.Bridged); + } + private boolean hasHostOnlyAdapter(NetworkInterfaceCard nic) { return nic.getNetworkAdapter().getNetworkAttachmentType().equals(NetworkAttachmentType.HostOnly); } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIMachineIfNotAlreadyExists.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIMachineIfNotAlreadyExists.java index 4584b6221b..0fb2f36414 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIMachineIfNotAlreadyExists.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIMachineIfNotAlreadyExists.java @@ -112,7 +112,7 @@ public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExists implements Fu IProgress progress = currentSnapshot.getMachine().cloneTo(clonedMachine, CloneMode.MachineState, options); progress.waitForCompletion(-1); - logger.debug("clone done"); + logger.debug(String.format("Machine %s is cloned correctly", clonedMachine.getName())); // registering manager.get().getVBox().registerMachine(clonedMachine); diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java index 6b2c34b447..ad3bc8983f 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java @@ -48,6 +48,7 @@ import org.virtualbox_4_1.IMachine; import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.base.Splitter; +import com.google.common.base.Throwables; import com.google.common.cache.LoadingCache; import com.google.common.collect.Iterables; import com.google.common.util.concurrent.Futures; @@ -105,19 +106,20 @@ public class CreateAndInstallVm implements Function { configureOsInstallationWithKeyboardSequence(vmName, installationKeySequence); + // the OS installation is a long process: let's delay the check for ssh of 30 sec + try { + Thread.sleep(30000l); + } catch (InterruptedException e) { + Throwables.propagate(e); + } + SshClient client = sshClientForIMachine.apply(vm); logger.debug(">> awaiting installation to finish node(%s)", vmName); checkState(sshResponds.apply(client), "timed out waiting for guest %s to be accessible via ssh", vmName); - logger.debug(">> awaiting installation of guest additions on vm: %s", vmName); - NodeMetadata nodeMetadata = imachineToNodeMetadata.apply(vm); - ListenableFuture execInstallGA = machineUtils.runScriptOnNode(nodeMetadata, - new InstallGuestAdditions(vmSpec, version), RunScriptOptions.NONE); - ExecResponse gaInstallationResponse = Futures.getUnchecked(execInstallGA); - checkState(gaInstallationResponse.getExitStatus() == 0); logger.debug(">> awaiting post-installation actions on vm: %s", vmName); ListenableFuture execCleanup = machineUtils.runScriptOnNode(nodeMetadata, @@ -125,9 +127,15 @@ public class CreateAndInstallVm implements Function { ExecResponse cleanupResponse = Futures.getUnchecked(execCleanup); checkState(cleanupResponse.getExitStatus() == 0); - logger.debug("<< installation of image complete. Powering down node(%s)", vmName); + + logger.debug(">> awaiting installation of guest additions on vm: %s", vmName); + ListenableFuture execInstallGA = machineUtils.runScriptOnNode(nodeMetadata, + new InstallGuestAdditions(vmSpec, version), RunScriptOptions.NONE); + ExecResponse gaInstallationResponse = Futures.getUnchecked(execInstallGA); + checkState(gaInstallationResponse.getExitStatus() == 0); + + machineController.ensureMachineIsShutdown(vmName); - machineController.ensureMachineHasPowerDown(vmName); return vm; } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadata.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadata.java index 7096aa01c0..3ac45ac871 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadata.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadata.java @@ -19,12 +19,12 @@ package org.jclouds.virtualbox.functions; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; import static org.jclouds.virtualbox.config.VirtualBoxComputeServiceContextModule.machineToNodeState; import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_NAME_SEPARATOR; import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_PREFIX; +import java.util.List; + import javax.annotation.Resource; import javax.inject.Named; @@ -37,6 +37,8 @@ import org.jclouds.domain.LocationScope; import org.jclouds.domain.LoginCredentials; import org.jclouds.javax.annotation.Nullable; import org.jclouds.logging.Logger; +import org.jclouds.virtualbox.util.MachineUtils; +import org.testng.collections.Lists; import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.INetworkAdapter; import org.virtualbox_4_1.MachineState; @@ -46,13 +48,23 @@ import com.google.common.base.Function; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; +import com.google.inject.Inject; +import com.google.inject.Singleton; +@Singleton public class IMachineToNodeMetadata implements Function { @Resource @Named(ComputeServiceConstants.COMPUTE_LOGGER) protected Logger logger = Logger.NULL; + + private final MachineUtils machineUtils; + @Inject + public IMachineToNodeMetadata(MachineUtils machineUtils) { + this.machineUtils = machineUtils; + } + @Override public NodeMetadata apply(@Nullable IMachine vm) { @@ -85,6 +97,7 @@ public class IMachineToNodeMetadata implements Function logger.debug("Setting virtualbox node to: " + nodeState + " from machine state: " + vmState); + /* // nat adapter INetworkAdapter natAdapter = vm.getNetworkAdapter(0l); checkNotNull(natAdapter, "slot 0 networkadapter"); @@ -115,11 +128,53 @@ public class IMachineToNodeMetadata implements Function nodeMetadataBuilder.privateAddresses(ImmutableSet.of((NodeCreator.VMS_NETWORK + ipTermination) + "")); nodeMetadataBuilder.publicAddresses(ImmutableSet.of((NodeCreator.VMS_NETWORK + ipTermination) + "")); } + */ + nodeMetadataBuilder = getIpAddresses(vm, nodeMetadataBuilder); LoginCredentials loginCredentials = new LoginCredentials("toor", "password", null, true); nodeMetadataBuilder.credentials(loginCredentials); return nodeMetadataBuilder.build(); } + + private NodeMetadataBuilder getIpAddresses(IMachine vm, NodeMetadataBuilder nodeMetadataBuilder) { + List publicIpAddresses = Lists.newArrayList(); + List privateIpAddresses = Lists.newArrayList(); + + for(long slot = 0; slot < 4; slot ++) { + INetworkAdapter adapter = vm.getNetworkAdapter(slot); + if(adapter != null) { + if (adapter.getAttachmentType() == NetworkAttachmentType.NAT) { + publicIpAddresses.add(adapter.getNatDriver().getHostIP()); + for (String nameProtocolnumberAddressInboudportGuestTargetport : adapter.getNatDriver().getRedirects()) { + Iterable stuff = Splitter.on(',').split(nameProtocolnumberAddressInboudportGuestTargetport); + String protocolNumber = Iterables.get(stuff, 1); + String hostAddress = Iterables.get(stuff, 2); + String inboundPort = Iterables.get(stuff, 3); + String targetPort = Iterables.get(stuff, 5); + if ("1".equals(protocolNumber) && "22".equals(targetPort)) { + int inPort = Integer.parseInt(inboundPort); + nodeMetadataBuilder.publicAddresses(ImmutableSet.of(hostAddress)).loginPort(inPort); + } + //privateIpAddresses.add((NodeCreator.VMS_NETWORK + ipTermination) + ""); + } + // TODO this could be a public and private address + } else if (adapter.getAttachmentType() == NetworkAttachmentType.Bridged) { + String clientIpAddress = machineUtils.getIpAddressFromBridgedNIC(vm.getName()); + //privateIpAddresses.add(clientIpAddress); + publicIpAddresses.add(clientIpAddress); + + } else if (adapter.getAttachmentType() == NetworkAttachmentType.HostOnly) { + String clientIpAddress = machineUtils.getIpAddressFromHostOnlyNIC(vm.getName()); + publicIpAddresses.add(clientIpAddress); + + } + } + } + nodeMetadataBuilder.publicAddresses(publicIpAddresses); + nodeMetadataBuilder.privateAddresses(privateIpAddresses); + + return nodeMetadataBuilder; + } } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToSshClient.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToSshClient.java index 56e05d11f7..e4ffe87a42 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToSshClient.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToSshClient.java @@ -39,6 +39,7 @@ import org.jclouds.ssh.SshClient; import org.jclouds.virtualbox.domain.BridgedIf; import org.jclouds.virtualbox.statements.GetIPAddressFromMAC; import org.jclouds.virtualbox.statements.ScanNetworkWithPing; +import org.jclouds.virtualbox.util.MachineUtils; import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.INetworkAdapter; import org.virtualbox_4_1.NetworkAttachmentType; @@ -59,14 +60,16 @@ public class IMachineToSshClient implements Function { private final SshClient.Factory sshClientFactory; private final RunScriptOnNode.Factory scriptRunnerFactory; private final Supplier hostSupplier; + private final MachineUtils machineUtils; @Inject public IMachineToSshClient(SshClient.Factory sshClientFactory, RunScriptOnNode.Factory scriptRunnerFactory, - Supplier hostSupplier) { + Supplier hostSupplier, MachineUtils machineUtils) { this.sshClientFactory = sshClientFactory; this.scriptRunnerFactory = scriptRunnerFactory; this.hostSupplier = hostSupplier; + this.machineUtils = machineUtils; } @Override @@ -104,6 +107,9 @@ public class IMachineToSshClient implements Function { NetworkAttachmentType.Bridged)) { String network = "1.1.1.1"; clientIpAddress = getIpAddressFromBridgedNIC(networkAdapter, network); + } else if (networkAdapter.getAttachmentType().equals( + NetworkAttachmentType.HostOnly)) { + clientIpAddress = machineUtils.getIpAddressFromHostOnlyNIC(vm.getName()); } checkNotNull(clientIpAddress, "clientIpAddress"); diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/LaunchMachineIfNotAlreadyRunning.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/LaunchMachineIfNotAlreadyRunning.java index 524efdd711..9c9ee59aa7 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/LaunchMachineIfNotAlreadyRunning.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/LaunchMachineIfNotAlreadyRunning.java @@ -76,6 +76,11 @@ public class LaunchMachineIfNotAlreadyRunning implements Function { // TODO parameterize public static final int MASTER_PORT = 2222; + public static final String HOST_ONLY_IFACE_NAME = "vboxnet0"; @Resource @Named(ComputeServiceConstants.COMPUTE_LOGGER) @@ -170,7 +171,7 @@ public class MastersLoadingCache extends AbstractLoadingCache { NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter) .slot(0L).build(); - + NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build(); MasterSpec masterSpec = MasterSpec diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java index e94645327d..0d2a1a0697 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/NodeCreator.java @@ -24,8 +24,6 @@ import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_NAME_SEPARATOR; import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_PREFIX; -import java.util.concurrent.atomic.AtomicInteger; - import javax.inject.Inject; import javax.inject.Singleton; @@ -44,49 +42,35 @@ import org.jclouds.virtualbox.domain.NetworkSpec; import org.jclouds.virtualbox.domain.NodeSpec; import org.jclouds.virtualbox.domain.VmSpec; import org.jclouds.virtualbox.statements.DeleteGShadowLock; -import org.jclouds.virtualbox.statements.SetIpAddress; +import org.jclouds.virtualbox.statements.EnableNetworkInterface; import org.jclouds.virtualbox.util.MachineController; import org.jclouds.virtualbox.util.MachineUtils; import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.IMachine; -import org.virtualbox_4_1.INetworkAdapter; import org.virtualbox_4_1.IProgress; import org.virtualbox_4_1.ISession; import org.virtualbox_4_1.NetworkAttachmentType; import org.virtualbox_4_1.VirtualBoxManager; import com.google.common.base.Function; -import com.google.common.base.Splitter; import com.google.common.base.Supplier; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Iterables; /** * Creates nodes, by cloning a master vm and based on the provided {@link NodeSpec}. Must be * synchronized mainly because of snapshot creation (must be synchronized on a per-master-basis). * - * @author David Alves + * @author David Alves, Andrea Turli * */ @Singleton public class NodeCreator implements Function> { - // TODO parameterize - public static final int NODE_PORT_INIT = 3000; - - // TODO parameterize - public static final String VMS_NETWORK = "192.168.86."; - // TODO parameterize public static final String HOST_ONLY_IFACE_NAME = "vboxnet0"; - // TODO parameterize - public static final boolean USE_LINKED = true; - private final Supplier manager; private final Function cloner; - private final AtomicInteger nodePorts; - private final AtomicInteger nodeIps; private final MachineUtils machineUtils; private final MachineController machineController; @@ -95,8 +79,6 @@ public class NodeCreator implements Function stuff = Splitter.on(',').split(nameProtocolnumberAddressInboudportGuestTargetport); - String protocolNumber = Iterables.get(stuff, 1); - String inboundPort = Iterables.get(stuff, 3); - String targetPort = Iterables.get(stuff, 5); - if ("1".equals(protocolNumber) && "22".equals(targetPort)) { - int inPort = Integer.parseInt(inboundPort); - nodeMetadataBuilder.loginPort(inPort); - } - } + nodeMetadataBuilder.publicAddresses(ImmutableSet.of(machineUtils.getIpAddressFromHostOnlyNIC(clone.getName()))); LoginCredentials loginCredentials = new LoginCredentials("toor", "password", null, true); nodeMetadataBuilder.credentials(loginCredentials); diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/EnableNetworkInterface.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/EnableNetworkInterface.java new file mode 100644 index 0000000000..224fd2f7f8 --- /dev/null +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/EnableNetworkInterface.java @@ -0,0 +1,88 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds 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 org.jclouds.virtualbox.statements; + +import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.scriptbuilder.domain.Statements.exec; + +import java.util.List; + +import org.jclouds.scriptbuilder.domain.OsFamily; +import org.jclouds.scriptbuilder.domain.Statement; +import org.jclouds.scriptbuilder.domain.StatementList; +import org.jclouds.virtualbox.domain.NetworkInterfaceCard; +import org.testng.collections.Lists; + +/** + * Up the network interface chosen + * + * @author Andrea Turli + * + */ +public class EnableNetworkInterface implements Statement { + + private final StatementList statements; + + public EnableNetworkInterface(NetworkInterfaceCard networkInterfaceCard) { + int slot = (int) networkInterfaceCard.getSlot(); + String iface = null; + switch (slot) { + case 0: + iface = "eth0"; + break; + case 1: + iface = "eth1"; + break; + case 2: + iface = "eth2"; + break; + case 3: + iface = "eth3"; + break; + default: + throw new IllegalArgumentException("slot must be 0,1,2,3 (was: " + slot + ")"); + } + this.statements = new StatementList(getStatements(iface)); + } + + private List getStatements(String iface) { + /* + * auto eth0 + */ + List statements = Lists.newArrayList(); + statements.add(exec(String.format("echo auto %s >> /etc/network/interfaces", iface))); // + statements.add(exec(String.format("echo iface %s inet dhcp >> /etc/network/interfaces", iface))); // + statements.add(exec("/etc/init.d/networking restart")); + return statements; + } + + @Override + public Iterable functionDependencies(OsFamily family) { + return statements.functionDependencies(family); + } + + @Override + public String render(OsFamily family) { + if (checkNotNull(family, "family") == OsFamily.WINDOWS) + throw new UnsupportedOperationException("windows not yet implemented"); + return statements.render(family); + } + +} diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/InstallGuestAdditions.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/InstallGuestAdditions.java index 0dd1ba188f..06d4ec128e 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/InstallGuestAdditions.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/InstallGuestAdditions.java @@ -88,6 +88,8 @@ public class InstallGuestAdditions implements Statement { statements.add(exec(String.format("mount -o loop {tmp}{fs}%s %s", vboxGuestAdditionsIso, mountPoint))); } statements.add(exec(String.format("%s%s", mountPoint, "/VBoxLinuxAdditions.run"))); // + statements.add(exec("service vboxadd setup")); // + statements.add(exec("VBoxService")); // statements.add(exec(String.format("echo VBoxService > /etc/rc.local"))); // statements.add(exec(String.format("echo exit 0 >> /etc/rc.local"))); // statements.add(exec(String.format("umount %s", mountPoint))); diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineController.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineController.java index 5a3caf4e16..3a59f68f18 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineController.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineController.java @@ -20,8 +20,6 @@ package org.jclouds.virtualbox.util; import static com.google.common.base.Preconditions.checkNotNull; -import java.awt.print.Printable; - import javax.annotation.Resource; import javax.inject.Named; import javax.inject.Singleton; @@ -38,6 +36,7 @@ import org.virtualbox_4_1.VirtualBoxManager; import com.google.common.base.Function; import com.google.common.base.Supplier; +import com.google.common.base.Throwables; import com.google.inject.Inject; /** @@ -68,9 +67,11 @@ public class MachineController { ISession session = null; while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Running)) { try { - session = machineUtils.applyForMachine(vmName, new LaunchMachineIfNotAlreadyRunning(manager.get(), executionType, "")); + session = machineUtils.applyForMachine(vmName, new LaunchMachineIfNotAlreadyRunning(manager.get(), + executionType, "")); } catch (RuntimeException e) { - if (e.getMessage().contains("org.virtualbox_4_1.VBoxException: VirtualBox error: The given session is busy (0x80BB0007)")) { + if (e.getMessage().contains( + "org.virtualbox_4_1.VBoxException: VirtualBox error: The given session is busy (0x80BB0007)")) { throw e; } else if (e.getMessage().contains("VirtualBox error: The object is not ready")) { continue; @@ -82,82 +83,108 @@ public class MachineController { return checkNotNull(session, "session"); } - public ISession ensureMachineHasPowerDown(String vmName) { - ISession session = manager.get().getSessionObject(); - while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.PoweredOff)) { - try { - session = machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function() { - @Override - public ISession apply(ISession session) { - IProgress powerDownProgress = session.getConsole().powerDown(); - powerDownProgress.waitForCompletion(-1); - return session; - } - }); - } catch (RuntimeException e) { - // sometimes the machine might be powered of between the while - // test and the call to - // lockSessionOnMachineAndApply - if (e.getMessage().contains("Invalid machine state: PoweredOff")) { - throw e; - } else if (e.getMessage().contains("VirtualBox error: The object is not ready")) { - continue; - } else { - throw e; - } - } - } - return checkNotNull(session, "session"); - } + public ISession ensureMachineHasPowerDown(String vmName) { + ISession session = manager.get().getSessionObject(); + while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.PoweredOff)) { + try { + session = machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, + new Function() { + @Override + public ISession apply(ISession session) { + IProgress powerDownProgress = session.getConsole().powerDown(); + powerDownProgress.waitForCompletion(-1); + return session; + } + }); + } catch (RuntimeException e) { + // sometimes the machine might be powered of between the while + // test and the call to + // lockSessionOnMachineAndApply + if (e.getMessage().contains("Invalid machine state: PoweredOff")) { + throw e; + } else if (e.getMessage().contains("VirtualBox error: The object is not ready")) { + continue; + } else { + throw e; + } + } + } + return checkNotNull(session, "session"); + } + + /** + * if machine supports ACPI it can be shutdown gently - not powerdown() + * http://askubuntu.com/questions/82015/shutting-down-ubuntu-server-running-in-headless-virtualbox + */ + public ISession ensureMachineIsShutdown(String vmName) { + ISession session = machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, + new Function() { + @Override + public ISession apply(ISession session) { + session.getConsole().powerButton(); + return session; + } + }); + int count = 0; + while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.PoweredOff) && count < 10) { + try { + Thread.sleep(500l * count); + } catch (InterruptedException e) { + Throwables.propagate(e); + } + count++; + } + return checkNotNull(session, "session"); + } - public void ensureMachineIsPaused(String vmName) { - while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Paused)) { - try { - machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function() { - @Override - public Void apply(ISession session) { - session.getConsole().pause(); - return null; - } - }); - } catch (RuntimeException e) { - // sometimes the machine might be powered of between the while - // test and the call to - // lockSessionOnMachineAndApply - if (e.getMessage().contains("Invalid machine state: Paused")) { - return; - } else if (e.getMessage().contains("VirtualBox error: The object is not ready")) { - continue; - } else { - throw e; - } - } - } - } - - public void ensureMachineIsResumed(String vmName) { - while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Running)) { - try { - machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function() { - @Override - public Void apply(ISession session) { - session.getConsole().resume(); - return null; - } - }); - } catch (RuntimeException e) { - // sometimes the machine might be powered of between the while - // test and the call to - // lockSessionOnMachineAndApply - if (e.getMessage().contains("Invalid machine state: Resumed")) { - return; - } else if (e.getMessage().contains("VirtualBox error: The object is not ready")) { - continue; - } else { - throw e; - } - } - } - } + public void ensureMachineIsPaused(String vmName) { + while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Paused)) { + try { + machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function() { + @Override + public Void apply(ISession session) { + session.getConsole().pause(); + return null; + } + }); + } catch (RuntimeException e) { + // sometimes the machine might be powered of between the while + // test and the call to + // lockSessionOnMachineAndApply + if (e.getMessage().contains("Invalid machine state: Paused")) { + return; + } else if (e.getMessage().contains("VirtualBox error: The object is not ready")) { + continue; + } else { + throw e; + } + } + } + } + + public void ensureMachineIsResumed(String vmName) { + while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Running)) { + try { + machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function() { + @Override + public Void apply(ISession session) { + session.getConsole().resume(); + return null; + } + }); + } catch (RuntimeException e) { + // sometimes the machine might be powered of between the while + // test and the call to + // lockSessionOnMachineAndApply + if (e.getMessage().contains("Invalid machine state: Resumed")) { + return; + } else if (e.getMessage().contains("VirtualBox error: The object is not ready")) { + continue; + } else { + throw e; + } + } + } + } } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java index 4f934921eb..26133e05ad 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java @@ -18,8 +18,11 @@ */ package org.jclouds.virtualbox.util; -import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.annotation.Resource; import javax.inject.Named; @@ -43,6 +46,7 @@ import org.virtualbox_4_1.VirtualBoxManager; import com.google.common.base.Function; import com.google.common.base.Supplier; +import com.google.common.base.Throwables; import com.google.common.util.concurrent.ListenableFuture; import com.google.inject.Inject; @@ -187,7 +191,7 @@ public class MachineUtils { * @return the result from applying the function to the session. */ protected T lockSessionOnMachineAndApply(String machineId, LockType type, Function function) { - int retries = 5; + int retries = 15; ISession session = lockSession(machineId, type, retries); try { return function.apply(session); @@ -206,10 +210,6 @@ public class MachineUtils { while (true) { try { IMachine immutableMachine = manager.get().getVBox().findMachine(machineId); - try { - Thread.sleep(300L); - } catch (InterruptedException e1) { - } session = manager.get().getSessionObject(); immutableMachine.lockMachine(session, type); break; @@ -264,4 +264,71 @@ public class MachineUtils { return e.getMessage().contains("VirtualBox error: Could not find a registered machine named ") || e.getMessage().contains("Could not find a registered machine with UUID {"); } + + public String getIpAddressFromBridgedNIC(String machineName) { + String ip = ""; + int attempt = 0; + while (!isIpv4(ip) && attempt < 10) { + ip = this.lockSessionOnMachineAndApply(machineName, LockType.Shared, new Function() { + @Override + public String apply(ISession session) { + String ip = session.getMachine().getGuestPropertyValue("/VirtualBox/GuestInfo/Net/0/V4/IP"); + return ip; + } + }); + attempt++; + long sleepTime = 1000 * attempt; + logger.debug("Instance %s is still not ready. Attempt n:%d. Sleeping for %d millisec", machineName, attempt, + sleepTime); + try { + Thread.sleep(sleepTime); + } catch (InterruptedException e) { + Throwables.propagate(e); + } + } + return ip; + } + + private boolean isIpv4(String s) { + Pattern pattern = Pattern.compile(this.IP_V4_ADDRESS_PATTERN); + Matcher matcher = pattern.matcher(s); + return matcher.matches(); + } + + public String getIpAddressFromHostOnlyNIC(String machineName) { + // TODO using a caching mechanism to avoid to call everytime this vboxmanage api call + String currentIp = "", previousIp = "1.1.1.1"; + int attempt = 0, count = 0; + while(count < 5) { + currentIp = ""; + attempt = 0; + while (!isIpv4(currentIp) && attempt < 5) { + currentIp = this.lockSessionOnMachineAndApply(machineName, LockType.Shared, new Function() { + @Override + public String apply(ISession session) { + return session.getMachine().getGuestPropertyValue("/VirtualBox/GuestInfo/Net/0/V4/IP"); + } + }); + attempt++; + } + if(previousIp.equals(currentIp)) { + count++; + delayer(500l * (count + 1)); + } else { + count = 0; + delayer(5000l); + } + previousIp = currentIp; + } + return currentIp; + } + + private void delayer(long millisec) { + try { + Thread.sleep(millisec); + } catch (InterruptedException e) { + Throwables.propagate(e); + } + } + } diff --git a/labs/virtualbox/src/main/resources/functions/cleanupUdevIfNeeded.sh b/labs/virtualbox/src/main/resources/functions/cleanupUdevIfNeeded.sh index a6192b169d..fcc3618e80 100644 --- a/labs/virtualbox/src/main/resources/functions/cleanupUdevIfNeeded.sh +++ b/labs/virtualbox/src/main/resources/functions/cleanupUdevIfNeeded.sh @@ -1,9 +1,9 @@ function cleanupUdevIfNeeded { if [ -f '/etc/udev/rules.d/70-persistent-net.rules' ] then - rm /etc/udev/rules.d/70-persistent-net.rules; - mkdir /etc/udev/rules.d/70-persistent-net.rules; - rm -rf /dev/.udev/; + rm /etc/udev/rules.d/70-persistent-net.rules + mkdir /etc/udev/rules.d/70-persistent-net.rules + rm -rf /dev/.udev/ rm /lib/udev/rules.d/75-persistent-net-generator.rules fi } \ No newline at end of file diff --git a/labs/virtualbox/src/main/resources/functions/installModuleAssistantIfNeeded.sh b/labs/virtualbox/src/main/resources/functions/installModuleAssistantIfNeeded.sh index 80ca5ec68d..e3c73dca77 100644 --- a/labs/virtualbox/src/main/resources/functions/installModuleAssistantIfNeeded.sh +++ b/labs/virtualbox/src/main/resources/functions/installModuleAssistantIfNeeded.sh @@ -4,7 +4,7 @@ function installModuleAssistantIfNeeded { if [ $OSNAME = 'Ubuntu' ] then echo "OS is Ubuntu" - apt-get -f -y -qq --force-yes install dkms build-essential linux-headers-`uname -r` module-assistant; + apt-get -f -y -qq --force-yes install dkms build-essential linux-headers-`uname -r` module-assistant acpid; m-a prepare -i return 0 fi diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/BaseVirtualBoxClientLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/BaseVirtualBoxClientLiveTest.java index b72c6a1efd..75ab267429 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/BaseVirtualBoxClientLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/BaseVirtualBoxClientLiveTest.java @@ -163,24 +163,13 @@ public class BaseVirtualBoxClientLiveTest extends BaseVersionedServiceLiveTest { checkNotNull(mastersCache.apply(template.getImage())); } - - protected void undoVm(VmSpec vmSpecification) { - machineUtils.writeLockMachineAndApply(vmSpecification.getVmId(), new UnregisterMachineIfExistsAndDeleteItsMedia( - vmSpecification)); - } - protected void undoVm(String vmNameOrId) { IMachine vm = null; try { - System.out.println("1: " + manager.get().getSessionObject().getState()); - vm = manager.get().getVBox().findMachine(vmNameOrId); VmSpec vmSpec = new IMachineToVmSpec().apply(vm); - System.out.println("2: " + vm.getSessionState()); int attempts = 0; while (attempts < 10 && !vm.getSessionState().equals(SessionState.Unlocked)) { - // TODO understand why this behavior and see if IEvent can help - System.out.println("Unlocking ..."); attempts++; try { Thread.sleep(200l); diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterLiveTest.java index a427a05b0a..a3d61ba27b 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterLiveTest.java @@ -50,8 +50,7 @@ public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClien public void testCreatedNodeHasExpectedNameAndWeCanConnectViaSsh() { String group = "foo"; String name = "foo-ef4"; - String machineName = VIRTUALBOX_NODE_PREFIX + "default-ubuntu-11.04-i386-" + group + "-" + name; - + String machineName = VIRTUALBOX_NODE_PREFIX + "default-ubuntu-11.04-i386-" + "0x0-" + group + "-0x0-" + name; Template template = context.getComputeService().templateBuilder().build(); machine = adapter.createNodeWithGroupEncodedIntoName(group, name, template); diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxExperimentLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxExperimentLiveTest.java index b4d86b48f4..6415236e77 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxExperimentLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxExperimentLiveTest.java @@ -67,10 +67,9 @@ public class VirtualBoxExperimentLiveTest { @Test public void testLaunchCluster() throws RunNodesException { - int numNodes = 4; + int numNodes = 2; final String clusterName = "test-launch-cluster"; - Set nodes = context.getComputeService().createNodesInGroup(clusterName, numNodes, - TemplateOptions.Builder.runScript(AdminAccess.standard())); + Set nodes = context.getComputeService().createNodesInGroup(clusterName, numNodes, TemplateOptions.Builder.runScript(AdminAccess.standard())); assertEquals(numNodes, nodes.size(), "wrong number of nodes"); for (NodeMetadata node : nodes) { assertTrue(node.getGroup().equals("test-launch-cluster")); diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest.java index 8912476737..ed73ee4c54 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest.java @@ -23,12 +23,9 @@ import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE; import static org.testng.Assert.assertEquals; -import java.util.Set; - import org.jclouds.config.ValueOfConfigurationKeyOrNull; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.jclouds.virtualbox.domain.CloneSpec; -import org.jclouds.virtualbox.domain.ExecutionType; import org.jclouds.virtualbox.domain.HardDisk; import org.jclouds.virtualbox.domain.IsoSpec; import org.jclouds.virtualbox.domain.MasterSpec; @@ -37,11 +34,11 @@ import org.jclouds.virtualbox.domain.NetworkInterfaceCard; import org.jclouds.virtualbox.domain.NetworkSpec; import org.jclouds.virtualbox.domain.StorageController; import org.jclouds.virtualbox.domain.VmSpec; +import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.IMachine; -import org.virtualbox_4_1.ISession; import org.virtualbox_4_1.NetworkAttachmentType; import org.virtualbox_4_1.StorageBus; @@ -56,20 +53,14 @@ import com.google.inject.Injector; @Test(groups = "live", singleThreaded = true, testName = "CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest") public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClientLiveTest { - private static final boolean IS_LINKED_CLONE = true; - - private MasterSpec sourceMachineSpec; - private CleanupMode mode = CleanupMode.Full; - private VmSpec clonedVmSpec; - private NetworkSpec cloneNetworkSpec; + private MasterSpec machineSpec; + private String instanceName; @Override @BeforeClass(groups = "live") public void setupClient() { super.setupClient(); - String sourceName = VIRTUALBOX_IMAGE_PREFIX - + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName()); - String cloneName = VIRTUALBOX_IMAGE_PREFIX + "Clone#" + instanceName = VIRTUALBOX_IMAGE_PREFIX + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName()); StorageController ideController = StorageController @@ -78,10 +69,10 @@ public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest exten .bus(StorageBus.IDE) .attachISO(0, 0, operatingSystemIso) .attachHardDisk( - HardDisk.builder().diskpath(adminDisk(sourceName)).controllerPort(0).deviceSlot(1) + HardDisk.builder().diskpath(adminDisk(instanceName)).controllerPort(0).deviceSlot(1) .autoDelete(true).build()).attachISO(1, 1, guestAdditionsIso).build(); - VmSpec sourceVmSpec = VmSpec.builder().id(sourceName).name(sourceName).osTypeId("").memoryMB(512) + VmSpec instanceVmSpec = VmSpec.builder().id(instanceName).name(instanceName).osTypeId("").memoryMB(512) .cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).build(); Injector injector = context.utils().injector(); @@ -91,65 +82,47 @@ public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExistsLiveTest exten .sourcePath(operatingSystemIso) .installationScript( configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME", - sourceVmSpec.getVmName())).build(); - - NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.Bridged) - .build(); + instanceVmSpec.getVmName())).build(); + NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT) + .tcpRedirectRule("127.0.0.1", 2222, "", 22).build(); NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter) .build(); - this.cloneNetworkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build(); - - sourceMachineSpec = MasterSpec.builder().iso(isoSpec).vm(sourceVmSpec).network(cloneNetworkSpec).build(); - - this.clonedVmSpec = VmSpec.builder().id(cloneName).name(cloneName).memoryMB(512).cleanUpMode(mode) - .forceOverwrite(true).build(); + NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build(); + machineSpec = MasterSpec.builder().iso(isoSpec).vm(instanceVmSpec).network(networkSpec).build(); } - + @Test - public void testCloneMachineFromAnotherMachine() throws Exception { - CloneSpec cloneSpec = null; - try { + public void testCloneMachineFromAnotherMachine() { + IMachine source = getVmWithGuestAdditionsInstalled(); + CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec()).network(machineSpec.getNetworkSpec()) + .master(source).linked(true).build(); + IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils) + .apply(cloneSpec); + assertEquals(clone.getName(), cloneSpec.getVmSpec().getVmName()); - IMachine source = getSourceNode(); - - cloneSpec = CloneSpec.builder().vm(clonedVmSpec).network(cloneNetworkSpec).master(source) - .linked(IS_LINKED_CLONE).build(); - - if (source.getCurrentSnapshot() != null) { - ISession session = manager.get().openMachineSession(source); - session.getConsole().deleteSnapshot(source.getCurrentSnapshot().getId()); - session.unlockMachine(); - } - - IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils) - .apply(cloneSpec); - assertEquals(clone.getName(), cloneSpec.getVmSpec().getVmName()); - - new LaunchMachineIfNotAlreadyRunning(manager.get(), ExecutionType.GUI, "").apply(clone); - - // TODO ssh into the node - - } finally { - Set specs = cloneSpec == null ? ImmutableSet.of(sourceMachineSpec.getVmSpec()) : ImmutableSet.of( - cloneSpec.getVmSpec(), sourceMachineSpec.getVmSpec()); - for (VmSpec spec : specs) { - undoVm(spec); - } - - } } - private IMachine getSourceNode() { + private IMachine getVmWithGuestAdditionsInstalled() { + MasterSpec masterSpecForTest = super.getMasterSpecForTest(); try { Injector injector = context.utils().injector(); - return injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(sourceMachineSpec); + return injector.getInstance(CreateAndInstallVm.class).apply(masterSpecForTest); } catch (IllegalStateException e) { // already created - return manager.get().getVBox().findMachine(sourceMachineSpec.getVmSpec().getVmId()); + return manager.get().getVBox().findMachine(masterSpecForTest.getVmSpec().getVmId()); } } + + @Override + @AfterClass(groups = "live") + protected void tearDown() throws Exception { + for (String vmName : ImmutableSet.of(instanceName)) { + undoVm(vmName); + } + super.tearDown(); + } } \ No newline at end of file diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java index d70ab876e3..eb28872e36 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java @@ -35,7 +35,7 @@ import org.jclouds.json.Json; import org.jclouds.json.config.GsonModule; import org.jclouds.ssh.SshClient; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; -import org.jclouds.virtualbox.domain.ExecutionType; +import org.jclouds.virtualbox.domain.CloneSpec; import org.jclouds.virtualbox.domain.HardDisk; import org.jclouds.virtualbox.domain.IsoSpec; import org.jclouds.virtualbox.domain.MasterSpec; @@ -73,13 +73,14 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest { }.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule()) .getInstance(Json.class)); - private VmSpec vmSpecification; - private MasterSpec masterSpec; private Injector injector; private Function sshClientForIMachine; private Predicate sshResponds; - private String vmName; + + private MasterSpec machineSpec; + private String instanceName; + /* @Override @BeforeClass(groups = "live") public void setupClient() { @@ -97,12 +98,13 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest { injector = context.utils().injector(); Function configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class); - NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT) + NetworkAdapter natAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT) .tcpRedirectRule("127.0.0.1", 2222, "", 22).build(); - NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter) - .build(); - NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build(); + NetworkInterfaceCard networkInterfaceCard1 = NetworkInterfaceCard.builder().slot(0l) + .addNetworkAdapter(natAdapter).build(); + + NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard1).build(); masterSpec = MasterSpec .builder() @@ -113,25 +115,51 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest { .installationScript( configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME", vmSpecification.getVmName())).build()).network(networkSpec).build(); + }*/ + + @Override + @BeforeClass(groups = "live") + public void setupClient() { + super.setupClient(); + instanceName = VIRTUALBOX_IMAGE_PREFIX + + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName()); - undoVm(vmSpecification); - } + StorageController ideController = StorageController + .builder() + .name("IDE Controller") + .bus(StorageBus.IDE) + .attachISO(0, 0, operatingSystemIso) + .attachHardDisk( + HardDisk.builder().diskpath(adminDisk(instanceName)).controllerPort(0).deviceSlot(1) + .autoDelete(true).build()).attachISO(1, 1, guestAdditionsIso).build(); - @Test - public void testCreateImageMachineFromIso() throws Exception { - IMachine imageMachine = getVmWithGuestAdditionsInstalled(); - IMachineToImage iMachineToImage = new IMachineToImage(manager, map); - Image newImage = iMachineToImage.apply(imageMachine); - assertEquals(vmName, newImage.getName()); + VmSpec instanceVmSpec = VmSpec.builder().id(instanceName).name(instanceName).osTypeId("").memoryMB(512) + .cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).build(); + + injector = context.utils().injector(); + Function configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class); + IsoSpec isoSpec = IsoSpec + .builder() + .sourcePath(operatingSystemIso) + .installationScript( + configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME", + instanceVmSpec.getVmName())).build(); + + NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT) + .tcpRedirectRule("127.0.0.1", 2222, "", 22).build(); + NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter) + .build(); + + NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build(); + machineSpec = MasterSpec.builder().iso(isoSpec).vm(instanceVmSpec).network(networkSpec).build(); } @Test public void testGuestAdditionsAreInstalled() throws Exception { try { - IMachine machine = getVmWithGuestAdditionsInstalled(); + IMachine machine = cloneFromMaster(); + machineController.ensureMachineIsLaunched(machine.getName()); - machineUtils.applyForMachine(machine.getName(), new LaunchMachineIfNotAlreadyRunning(manager.get(), - ExecutionType.GUI, "")); sshClientForIMachine = injector.getInstance(IMachineToSshClient.class); SshClient client = sshClientForIMachine.apply(machine); @@ -141,35 +169,44 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest { String vboxVersion = Iterables.get( Splitter.on('r').split(context.getProviderSpecificContext().getBuildVersion()), 0); - assertEquals(vboxVersion, machineUtils.sharedLockMachineAndApplyToSession(machine.getName(), - new Function() { + assertEquals(vboxVersion, + machineUtils.sharedLockMachineAndApplyToSession(machine.getName(), new Function() { @Override public String apply(ISession session) { return session.getMachine().getGuestPropertyValue("/VirtualBox/GuestAdd/Version"); } })); } finally { - for (VmSpec spec : ImmutableSet.of(vmSpecification)) { - machineController.ensureMachineHasPowerDown(spec.getVmName()); + for (VmSpec spec : ImmutableSet.of(machineSpec.getVmSpec())) { + machineController.ensureMachineIsShutdown(spec.getVmName()); } } } + private IMachine cloneFromMaster() { + IMachine source = getVmWithGuestAdditionsInstalled(); + CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec()).network(machineSpec.getNetworkSpec()) + .master(source).linked(true).build(); + return new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils) + .apply(cloneSpec); + } + private IMachine getVmWithGuestAdditionsInstalled() { + MasterSpec masterSpecForTest = super.getMasterSpecForTest(); try { Injector injector = context.utils().injector(); - return injector.getInstance(CreateAndInstallVm.class).apply(masterSpec); + return injector.getInstance(CreateAndInstallVm.class).apply(masterSpecForTest); } catch (IllegalStateException e) { // already created - return manager.get().getVBox().findMachine(masterSpec.getVmSpec().getVmId()); + return manager.get().getVBox().findMachine(masterSpecForTest.getVmSpec().getVmId()); } } @Override @AfterClass(groups = "live") protected void tearDown() throws Exception { - for (VmSpec spec : ImmutableSet.of(vmSpecification)) { - undoVm(spec); + for (String vmName : ImmutableSet.of(instanceName)) { + undoVm(vmName); } super.tearDown(); } diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java index 982a40529f..de5ae9c8a3 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java @@ -33,6 +33,7 @@ import org.jclouds.virtualbox.domain.NetworkInterfaceCard; import org.jclouds.virtualbox.domain.NetworkSpec; import org.jclouds.virtualbox.domain.StorageController; import org.jclouds.virtualbox.domain.VmSpec; +import org.testng.annotations.AfterMethod; import org.testng.annotations.Test; import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.IMachine; @@ -46,95 +47,82 @@ import com.google.inject.Injector; * @author Mattias Holmqvist */ @Test(groups = "live", singleThreaded = true, testName = "CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest") -public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends - BaseVirtualBoxClientLiveTest { +public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClientLiveTest { - private String ideControllerName; - private CleanupMode mode; + private String ideControllerName; + private CleanupMode mode; + private String vmName = ""; - @Override - public void setupClient() { - super.setupClient(); - ideControllerName = "IDE Controller"; - mode = CleanupMode.Full; - } + @Override + public void setupClient() { + super.setupClient(); + ideControllerName = "IDE Controller"; + mode = CleanupMode.Full; + } + + @Test + public void testCreateNewMachine() throws Exception { + vmName = "jclouds-test-create-1-node"; + String vmId = UUID.randomUUID().toString(); - @Test - public void testCreateNewMachine() throws Exception { - String vmName = "jclouds-test-create-1-node"; - String vmId = UUID.randomUUID().toString(); - StorageController ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE) .attachISO(0, 0, operatingSystemIso) .attachHardDisk(HardDisk.builder().diskpath(adminDisk(vmName)).controllerPort(0).deviceSlot(1).build()) .attachISO(1, 1, guestAdditionsIso).build(); - VmSpec vmSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512) - .controller(ideController).cleanUpMode(mode).osTypeId("Debian") - .forceOverwrite(true).build(); + VmSpec vmSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).controller(ideController).cleanUpMode(mode) + .osTypeId("Debian").forceOverwrite(true).build(); - NetworkAdapter networkAdapter = NetworkAdapter.builder() - .networkAttachmentType(NetworkAttachmentType.NAT) - .tcpRedirectRule("127.0.0.1", 2222, "", 22).build(); - NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard - .builder().addNetworkAdapter(networkAdapter).build(); + NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT) + .tcpRedirectRule("127.0.0.1", 2222, "", 22).build(); + NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter) + .build(); - NetworkSpec networkSpec = NetworkSpec.builder() - .addNIC(networkInterfaceCard).build(); + NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build(); - MasterSpec machineSpec = MasterSpec - .builder() - .iso(IsoSpec.builder().sourcePath(operatingSystemIso) - .installationScript("").build()).vm(vmSpec) - .network(networkSpec).build(); - undoVm(vmSpec); - try { - IMachine debianNode = context.utils() - .injector() - .getInstance( - CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class) - .apply(machineSpec); - IMachine machine = manager.get().getVBox().findMachine(vmName); - assertEquals(debianNode.getName(), machine.getName()); - } finally { - undoVm(vmSpec); - } - } + MasterSpec machineSpec = MasterSpec.builder() + .iso(IsoSpec.builder().sourcePath(operatingSystemIso).installationScript("").build()).vm(vmSpec) + .network(networkSpec).build(); + // undoVm(vmSpec); + IMachine debianNode = context.utils().injector() + .getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(machineSpec); + IMachine machine = manager.get().getVBox().findMachine(vmName); + assertEquals(debianNode.getName(), machine.getName()); - @Test - public void testCreateNewMachineWithBadOsType() throws Exception { - String vmName = "jclouds-test-create-2-node"; - String vmId = UUID.randomUUID().toString(); - - StorageController ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE) - .attachISO(0, 0, operatingSystemIso) - .attachHardDisk(HardDisk.builder().diskpath(adminDisk(vmName)).controllerPort(0).deviceSlot(1).build()) - .attachISO(1, 1, guestAdditionsIso).build(); + } - VmSpec vmSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512) - .controller(ideController).cleanUpMode(mode) - .osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build(); - IsoSpec isoSpec = IsoSpec.builder().sourcePath(operatingSystemIso) - .installationScript("").build(); - NetworkSpec networkSpec = NetworkSpec.builder().build(); - MasterSpec machineSpec = MasterSpec.builder().iso(isoSpec).vm(vmSpec) - .network(networkSpec).build(); - undoVm(vmSpec); - try { - Injector injector = context.utils().injector(); - injector.getInstance( - CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class) - .apply(machineSpec); - fail(); - } catch (VBoxException e) { - ErrorCode errorCode = ErrorCode.valueOf(e); - // According to the documentation VBOX_E_OBJECT_NOT_FOUND - // if osTypeId is not found. - assertEquals(errorCode, ErrorCode.VBOX_E_OBJECT_NOT_FOUND); - } finally { - undoVm(vmSpec); - } + @Test + public void testCreateNewMachineWithBadOsType() throws Exception { + vmName = "jclouds-test-create-2-node"; + String vmId = UUID.randomUUID().toString(); - } + StorageController ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE) + .attachISO(0, 0, operatingSystemIso) + .attachHardDisk(HardDisk.builder().diskpath(adminDisk(vmName)).controllerPort(0).deviceSlot(1).build()) + .attachISO(1, 1, guestAdditionsIso).build(); + + VmSpec vmSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).controller(ideController).cleanUpMode(mode) + .osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build(); + IsoSpec isoSpec = IsoSpec.builder().sourcePath(operatingSystemIso).installationScript("").build(); + NetworkSpec networkSpec = NetworkSpec.builder().build(); + MasterSpec machineSpec = MasterSpec.builder().iso(isoSpec).vm(vmSpec).network(networkSpec).build(); + try { + Injector injector = context.utils().injector(); + injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(machineSpec); + fail(); + } catch (VBoxException e) { + ErrorCode errorCode = ErrorCode.valueOf(e); + // According to the documentation VBOX_E_OBJECT_NOT_FOUND + // if osTypeId is not found. + assertEquals(errorCode, ErrorCode.VBOX_E_OBJECT_NOT_FOUND); + } + } + + @Override + @AfterMethod(groups = "live") + protected void tearDown() throws Exception { + undoVm(vmName); + super.tearDown(); + } } diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadataTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadataTest.java index 221342acb9..58a91e7d6b 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadataTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadataTest.java @@ -29,6 +29,7 @@ import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_ import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_NODE_PREFIX; import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.virtualbox.util.MachineUtils; import org.testng.annotations.Test; import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.INATEngine; @@ -62,10 +63,11 @@ public class IMachineToNodeMetadataTest { expect(natEng.getRedirects()).andReturn(ImmutableList.of("0,1,127.0.0.1,2222,,22")); INetworkAdapter hostOnly = createNiceMock(INetworkAdapter.class); + MachineUtils machineUtils = createNiceMock(MachineUtils.class); - replay(vm, nat, natEng, hostOnly); + replay(vm, nat, natEng, hostOnly, machineUtils); - NodeMetadata node = new IMachineToNodeMetadata().apply(vm); + NodeMetadata node = new IMachineToNodeMetadata(machineUtils).apply(vm); assertEquals(MASTER_NAME, node.getName()); assertEquals(0, node.getPrivateAddresses().size()); @@ -99,17 +101,14 @@ public class IMachineToNodeMetadataTest { expect(nat.getNatDriver()).andReturn(natEng).anyTimes(); expect(natEng.getHostIP()).andReturn("127.0.0.1").once(); expect(natEng.getRedirects()).andReturn(ImmutableList.of("0,1,127.0.0.1,3000,,22")); + MachineUtils machineUtils = createNiceMock(MachineUtils.class); - replay(vm, nat, natEng, hostOnly); + replay(vm, nat, natEng, hostOnly, machineUtils); - NodeMetadata node = new IMachineToNodeMetadata().apply(vm); + NodeMetadata node = new IMachineToNodeMetadata(machineUtils).apply(vm); assertEquals(name, node.getName()); assertEquals(group, node.getGroup()); - assertEquals(1, node.getPrivateAddresses().size()); - assertEquals((NodeCreator.VMS_NETWORK + 2), Iterables.get(node.getPrivateAddresses(), 0)); assertEquals(1, node.getPublicAddresses().size()); - assertEquals((NodeCreator.VMS_NETWORK + 2), Iterables.get(node.getPublicAddresses(), 0)); - assertEquals(22, node.getLoginPort()); } } diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/RetrieveActiveBridgedInterfacesLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/RetrieveActiveBridgedInterfacesExpectTest.java similarity index 79% rename from labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/RetrieveActiveBridgedInterfacesLiveTest.java rename to labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/RetrieveActiveBridgedInterfacesExpectTest.java index c88c71c782..3e17be9400 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/RetrieveActiveBridgedInterfacesLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/RetrieveActiveBridgedInterfacesExpectTest.java @@ -26,6 +26,7 @@ import java.util.List; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.jclouds.virtualbox.domain.BridgedIf; +import org.jclouds.virtualbox.domain.BridgedIf.Builder; import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; @@ -33,8 +34,8 @@ import com.google.common.collect.ImmutableList; /** * @author Andrea Turli */ -@Test(groups = "live", singleThreaded = true, testName = "RetrieveActiveBridgedInterfacesLiveTest") -public class RetrieveActiveBridgedInterfacesLiveTest extends BaseVirtualBoxClientLiveTest { +@Test(singleThreaded = true, testName = "RetrieveActiveBridgedInterfacesExpectTest") +public class RetrieveActiveBridgedInterfacesExpectTest { public static final String TEST1 = "Name: eth0\n" + "GUID: 30687465-0000-4000-8000-00261834d0cb\n" + "Dhcp: Disabled\n" @@ -48,19 +49,16 @@ public class RetrieveActiveBridgedInterfacesLiveTest extends BaseVirtualBoxClien + "HardwareAddress: 5a:3d:ed:99:3f:ed\n" + "MediumType: Ethernet\n" + "Status: Down\n" + "VBoxNetworkName: HostInterfaceNetworking-vbox0\n"; - public static final List expectedBridgedInterfaces = ImmutableList.of("eth0", "vbox0"); + public static final List expectedBridgedInterfaces = ImmutableList.of( + BridgedIf.builder().name("eth0").ip("209.x.x.x").networkMask("255.255.255.0").mediumType("Ethernet") + .status("Up").build(), + BridgedIf.builder().name("vbox0").ip("192.168.56.1").networkMask("255.255.255.0").mediumType("Ethernet") + .status("Down").build()); @Test public void retrieveBridgedInterfaceNamesTest() { List activeBridgedInterfaces = retrieveBridgedInterfaceNames(TEST1); - assertEquals(activeBridgedInterfaces, expectedBridgedInterfaces); - } - - @Test - public void retrieveAvailableBridgedInterfaceInfoTest() { - List bridgedInterface = context.utils().injector().getInstance(RetrieveActiveBridgedInterfaces.class) - .apply(host.get()); - assertFalse(bridgedInterface.isEmpty()); + assertEquals(activeBridgedInterfaces, expectedBridgedInterfaces); } } \ No newline at end of file diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartVBoxIfNotAlreadyRunningLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartVBoxIfNotAlreadyRunningLiveTest.java index 5e30870f5b..6be7199b3b 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartVBoxIfNotAlreadyRunningLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/StartVBoxIfNotAlreadyRunningLiveTest.java @@ -57,10 +57,11 @@ public class StartVBoxIfNotAlreadyRunningLiveTest { URI provider = URI.create("http://localhost:18083/"); String identity = "adminstrator"; String credential = "12345"; + expect(client.seconds(3)).andReturn(client); expect(client.apply(new IPSocket(provider.getHost(), provider.getPort()))).andReturn(true); - manager.connect(provider.toASCIIString(), identity, credential); + manager.connect(provider.toASCIIString(), "", ""); replay(manager, runScriptOnNodeFactory, client); @@ -84,14 +85,15 @@ public class StartVBoxIfNotAlreadyRunningLiveTest { URI provider = URI.create("http://localhost:18083/"); String identity = "adminstrator"; String credential = "12345"; - + + expect(client.seconds(3)).andReturn(client); expect(client.apply(new IPSocket(provider.getHost(), provider.getPort()))).andReturn(false); expect( runScriptOnNodeFactory.create(host, Statements.exec("VBoxManage setproperty websrvauthlibrary null"), runAsRoot(false).wrapInInitScript(false))).andReturn(runScriptOnNode); expect(runScriptOnNode.init()).andReturn(runScriptOnNode); expect(runScriptOnNode.call()).andReturn(new ExecResponse("", "", 0)); - + expect(client.apply(new IPSocket(provider.getHost(), provider.getPort()))).andReturn(true); expect( runScriptOnNodeFactory.create(host, Statements.exec("vboxwebsrv -t 10000 -v -b"), runAsRoot(false) .wrapInInitScript(false).blockOnComplete(false).nameTask("vboxwebsrv"))).andReturn( @@ -99,7 +101,7 @@ public class StartVBoxIfNotAlreadyRunningLiveTest { expect(runScriptOnNode.init()).andReturn(runScriptOnNode); expect(runScriptOnNode.call()).andReturn(new ExecResponse("", "", 0)); - manager.connect(provider.toASCIIString(), identity, credential); + manager.connect(provider.toASCIIString(), "", ""); replay(manager, runScriptOnNodeFactory, runScriptOnNode, client); new StartVBoxIfNotAlreadyRunning((Function) Functions.constant(manager), runScriptOnNodeFactory, client, diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/GuestAdditionsInstallerLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/GuestAdditionsInstallerLiveTest.java index 194a3cd944..da7617d484 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/GuestAdditionsInstallerLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/GuestAdditionsInstallerLiveTest.java @@ -27,13 +27,10 @@ import static org.testng.Assert.assertTrue; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.jclouds.compute.domain.ExecResponse; -import org.jclouds.compute.domain.NodeMetadata; -import org.jclouds.compute.options.RunScriptOptions; import org.jclouds.config.ValueOfConfigurationKeyOrNull; +import org.jclouds.ssh.SshClient; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.jclouds.virtualbox.domain.CloneSpec; -import org.jclouds.virtualbox.domain.ExecutionType; import org.jclouds.virtualbox.domain.HardDisk; import org.jclouds.virtualbox.domain.IsoSpec; import org.jclouds.virtualbox.domain.MasterSpec; @@ -44,8 +41,7 @@ import org.jclouds.virtualbox.domain.StorageController; import org.jclouds.virtualbox.domain.VmSpec; import org.jclouds.virtualbox.functions.CloneAndRegisterMachineFromIMachineIfNotAlreadyExists; import org.jclouds.virtualbox.functions.CreateAndInstallVm; -import org.jclouds.virtualbox.functions.LaunchMachineIfNotAlreadyRunning; -import org.jclouds.virtualbox.statements.InstallGuestAdditions; +import org.jclouds.virtualbox.functions.IMachineToSshClient; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.virtualbox_4_1.CleanupMode; @@ -56,107 +52,99 @@ import org.virtualbox_4_1.StorageBus; import com.google.common.base.CaseFormat; import com.google.common.base.Function; +import com.google.common.base.Predicate; import com.google.common.collect.ImmutableSet; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; import com.google.inject.Injector; /** * @author Andrea Turli */ @Test(groups = "live", singleThreaded = true, testName = "GuestAdditionsInstallerLiveTest") -public class GuestAdditionsInstallerLiveTest extends - BaseVirtualBoxClientLiveTest { +public class GuestAdditionsInstallerLiveTest extends BaseVirtualBoxClientLiveTest { - private MasterSpec machineSpec; + private Injector injector; + private Function sshClientForIMachine; + private Predicate sshResponds; - @Override - @BeforeClass(groups = "live") - public void setupClient() { - super.setupClient(); - String instanceName = VIRTUALBOX_IMAGE_PREFIX - + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass() - .getSimpleName()); + private MasterSpec machineSpec; - StorageController ideController = StorageController - .builder() - .name("IDE Controller") - .bus(StorageBus.IDE) - .attachISO(0, 0, operatingSystemIso) - .attachHardDisk( - HardDisk.builder().diskpath(adminDisk(instanceName)) - .controllerPort(0).deviceSlot(1) - .autoDelete(true).build()) - .attachISO(1, 1, guestAdditionsIso).build(); + @Override + @BeforeClass(groups = "live") + public void setupClient() { + super.setupClient(); + injector = context.utils().injector(); - VmSpec instanceVmSpec = VmSpec.builder().id(instanceName) - .name(instanceName).osTypeId("").memoryMB(512) - .cleanUpMode(CleanupMode.Full).controller(ideController) - .forceOverwrite(true).build(); + String instanceName = VIRTUALBOX_IMAGE_PREFIX + + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName()); - Injector injector = context.utils().injector(); - Function configProperties = injector - .getInstance(ValueOfConfigurationKeyOrNull.class); - IsoSpec isoSpec = IsoSpec - .builder() - .sourcePath(operatingSystemIso) - .installationScript( - configProperties.apply( - VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace( - "HOSTNAME", instanceVmSpec.getVmName())) - .build(); + StorageController ideController = StorageController + .builder() + .name("IDE Controller") + .bus(StorageBus.IDE) + .attachISO(0, 0, operatingSystemIso) + .attachHardDisk( + HardDisk.builder().diskpath(adminDisk(instanceName)).controllerPort(0).deviceSlot(1) + .autoDelete(true).build()).attachISO(1, 1, guestAdditionsIso).build(); - NetworkAdapter networkAdapter = NetworkAdapter.builder() - .networkAttachmentType(NetworkAttachmentType.NAT) - .tcpRedirectRule("127.0.0.1", 2222, "", 22).build(); - NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard - .builder().addNetworkAdapter(networkAdapter).build(); + VmSpec instanceVmSpec = VmSpec.builder().id(instanceName).name(instanceName).osTypeId("").memoryMB(512) + .cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).build(); - NetworkSpec networkSpec = NetworkSpec.builder() - .addNIC(networkInterfaceCard).build(); - machineSpec = MasterSpec.builder().iso(isoSpec) - .vm(instanceVmSpec).network(networkSpec).build(); - } + Function configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class); + IsoSpec isoSpec = IsoSpec + .builder() + .sourcePath(operatingSystemIso) + .installationScript( + configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME", + instanceVmSpec.getVmName())).build(); - @Test - public void testGuestAdditionsAreInstalled() throws Exception { - IMachine machine = null; - try { - machine = cloneFromMaster(); - machineUtils.applyForMachine(machine.getName(), - new LaunchMachineIfNotAlreadyRunning(manager.get(), - ExecutionType.GUI, "")); - assertTrue(machineUtils.sharedLockMachineAndApplyToSession( - machine.getName(), - new Function() { - @Override - public Boolean apply(ISession session) { - String s = session - .getMachine() - .getGuestPropertyValue( - "/VirtualBox/GuestInfo/Net/0/V4/IP"); - return isIpv4(s); - } + NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT) + .tcpRedirectRule("127.0.0.1", 2222, "", 22).build(); + NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter) + .build(); + + NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build(); + machineSpec = MasterSpec.builder().iso(isoSpec).vm(instanceVmSpec).network(networkSpec).build(); + } + + @Test + public void testGuestAdditionsAreInstalled() throws Exception { + IMachine machine = null; + try { + machine = cloneFromMaster(); + machineController.ensureMachineIsLaunched(machine.getName()); + sshClientForIMachine = injector.getInstance(IMachineToSshClient.class); + SshClient client = sshClientForIMachine.apply(machine); + + sshResponds = injector.getInstance(SshResponds.class); + checkState(sshResponds.apply(client), "timed out waiting for guest %s to be accessible via ssh", + machine.getName()); + + assertTrue(machineUtils.sharedLockMachineAndApplyToSession(machine.getName(), + new Function() { + @Override + public Boolean apply(ISession session) { + String s = session.getMachine().getGuestPropertyValue("/VirtualBox/GuestInfo/Net/0/V4/IP"); + return isIpv4(s); + } + + private boolean isIpv4(String s) { + Pattern pattern = Pattern.compile(machineUtils.IP_V4_ADDRESS_PATTERN); + Matcher matcher = pattern.matcher(s); + return matcher.matches(); + } + })); + } finally { + for (String vmNameOrId : ImmutableSet.of(machine.getName())) { + machineController.ensureMachineHasPowerDown(vmNameOrId); + undoVm(vmNameOrId); + } + } + } - private boolean isIpv4(String s) { - Pattern pattern = Pattern - .compile(machineUtils.IP_V4_ADDRESS_PATTERN); - Matcher matcher = pattern.matcher(s); - return matcher.matches(); - } - })); - } finally { - for (String vmNameOrId : ImmutableSet.of(machine.getName())) { - machineController.ensureMachineHasPowerDown(vmNameOrId); - undoVm(vmNameOrId); - } - } - } - private IMachine cloneFromMaster() { IMachine source = getVmWithGuestAdditionsInstalled(); - CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec()) - .network(machineSpec.getNetworkSpec()).master(source).linked(true).build(); + CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec()).network(machineSpec.getNetworkSpec()) + .master(source).linked(true).build(); return new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils) .apply(cloneSpec); } diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/IMachinePredicatesLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/IMachinePredicatesLiveTest.java index 26b1a6167f..830053e18e 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/IMachinePredicatesLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/IMachinePredicatesLiveTest.java @@ -20,11 +20,11 @@ package org.jclouds.virtualbox.predicates; import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX; +import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE; import static org.jclouds.virtualbox.predicates.IMachinePredicates.isLinkedClone; import static org.testng.Assert.assertTrue; -import java.util.Set; - +import org.jclouds.config.ValueOfConfigurationKeyOrNull; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.jclouds.virtualbox.domain.CloneSpec; import org.jclouds.virtualbox.domain.HardDisk; @@ -36,10 +36,9 @@ import org.jclouds.virtualbox.domain.NetworkSpec; import org.jclouds.virtualbox.domain.StorageController; import org.jclouds.virtualbox.domain.VmSpec; import org.jclouds.virtualbox.functions.CloneAndRegisterMachineFromIMachineIfNotAlreadyExists; -import org.jclouds.virtualbox.functions.CreateAndRegisterMachineFromIsoIfNotAlreadyExists; -import org.testng.annotations.AfterMethod; +import org.jclouds.virtualbox.functions.CreateAndInstallVm; +import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.IMachine; @@ -47,6 +46,7 @@ import org.virtualbox_4_1.NetworkAttachmentType; import org.virtualbox_4_1.StorageBus; import com.google.common.base.CaseFormat; +import com.google.common.base.Function; import com.google.common.collect.ImmutableSet; import com.google.inject.Injector; @@ -56,68 +56,75 @@ import com.google.inject.Injector; */ @Test(groups = "live", singleThreaded = true, testName = "IMachinePredicatesLiveTest") public class IMachinePredicatesLiveTest extends BaseVirtualBoxClientLiveTest { + + private MasterSpec machineSpec; + private String instanceName; - private String osTypeId = ""; - private String ideControllerName = "IDE Controller"; - private String cloneName; - private String vmName; - private StorageController masterStorageController; - private MasterSpec masterMachineSpec; - private NetworkSpec networkSpec; - private CloneSpec cloneSpec; + @Override + @BeforeClass(groups = "live") + public void setupClient() { + super.setupClient(); + instanceName = VIRTUALBOX_IMAGE_PREFIX + + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName()); - @Override - @BeforeClass(groups = "live") - public void setupClient() { - super.setupClient(); - vmName = VIRTUALBOX_IMAGE_PREFIX + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName()); + StorageController ideController = StorageController + .builder() + .name("IDE Controller") + .bus(StorageBus.IDE) + .attachISO(0, 0, operatingSystemIso) + .attachHardDisk( + HardDisk.builder().diskpath(adminDisk(instanceName)).controllerPort(0).deviceSlot(1) + .autoDelete(true).build()).attachISO(1, 1, guestAdditionsIso).build(); - cloneName = VIRTUALBOX_IMAGE_PREFIX + "Clone#" - + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName()); + VmSpec instanceVmSpec = VmSpec.builder().id(instanceName).name(instanceName).osTypeId("").memoryMB(512) + .cleanUpMode(CleanupMode.Full).controller(ideController).forceOverwrite(true).build(); - HardDisk hardDisk = HardDisk.builder().diskpath(adminDisk(vmName)).autoDelete(true).controllerPort(0) - .deviceSlot(1).build(); - masterStorageController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE) - .attachISO(0, 0, operatingSystemIso).attachHardDisk(hardDisk).attachISO(1, 1, guestAdditionsIso).build(); - VmSpec masterSpec = VmSpec.builder().id(vmName).name(vmName).memoryMB(512).osTypeId(osTypeId) - .controller(masterStorageController).forceOverwrite(true).cleanUpMode(CleanupMode.Full).build(); - masterMachineSpec = MasterSpec.builder() - .iso(IsoSpec.builder().sourcePath(operatingSystemIso).installationScript("").build()).vm(masterSpec) - .network(NetworkSpec.builder().build()).build(); + Injector injector = context.utils().injector(); + Function configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class); + IsoSpec isoSpec = IsoSpec + .builder() + .sourcePath(operatingSystemIso) + .installationScript( + configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace("HOSTNAME", + instanceVmSpec.getVmName())).build(); - NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.Bridged) - .build(); - NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter) - .build(); + NetworkAdapter networkAdapter = NetworkAdapter.builder().networkAttachmentType(NetworkAttachmentType.NAT) + .tcpRedirectRule("127.0.0.1", 2222, "", 22).build(); + NetworkInterfaceCard networkInterfaceCard = NetworkInterfaceCard.builder().addNetworkAdapter(networkAdapter) + .build(); - this.networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build(); + NetworkSpec networkSpec = NetworkSpec.builder().addNIC(networkInterfaceCard).build(); + machineSpec = MasterSpec.builder().iso(isoSpec).vm(instanceVmSpec).network(networkSpec).build(); - } + } + + @Test + public void testCloneMachineFromAnotherMachine() { + IMachine source = getVmWithGuestAdditionsInstalled(); + CloneSpec cloneSpec = CloneSpec.builder().vm(machineSpec.getVmSpec()).network(machineSpec.getNetworkSpec()) + .master(source).linked(true).build(); + IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils) + .apply(cloneSpec); + assertTrue(isLinkedClone().apply(clone)); + } - @Test - public void testLinkedClone() { - - Injector injector = context.utils().injector(); - IMachine master = injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply( - masterMachineSpec); - - VmSpec clonedVmSpec = VmSpec.builder().id(cloneName).name(cloneName).memoryMB(512).cleanUpMode(CleanupMode.Full) - .forceOverwrite(true).build(); - - this.cloneSpec = CloneSpec.builder().vm(clonedVmSpec).network(networkSpec).master(master).linked(true).build(); - - IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, machineUtils) - .apply(cloneSpec); - - assertTrue(isLinkedClone().apply(clone)); - } - - @BeforeMethod - @AfterMethod - void cleanUpVms() { - Set specs = cloneSpec != null ? ImmutableSet.of(cloneSpec.getVmSpec(), masterMachineSpec.getVmSpec()) - : ImmutableSet.of(masterMachineSpec.getVmSpec()); - for (VmSpec spec : specs) - this.undoVm(spec); + private IMachine getVmWithGuestAdditionsInstalled() { + MasterSpec masterSpecForTest = super.getMasterSpecForTest(); + try { + Injector injector = context.utils().injector(); + return injector.getInstance(CreateAndInstallVm.class).apply(masterSpecForTest); + } catch (IllegalStateException e) { + // already created + return manager.get().getVBox().findMachine(masterSpecForTest.getVmSpec().getVmId()); + } + } + + @Override + @AfterClass(groups = "live") + protected void tearDown() throws Exception { + for (String vmName : ImmutableSet.of(instanceName)) { + undoVm(vmName); + } + super.tearDown(); } } diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/statements/InstallGuestAdditionsLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/statements/InstallGuestAdditionsLiveTest.java index 0d21e5f036..58ceeba796 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/statements/InstallGuestAdditionsLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/statements/InstallGuestAdditionsLiveTest.java @@ -45,9 +45,15 @@ public class InstallGuestAdditionsLiveTest extends BaseVirtualBoxClientLiveTest InstallGuestAdditions installer = new InstallGuestAdditions(vmSpecification, "4.1.8"); String scripts = installer.render(OsFamily.UNIX); assertEquals("installModuleAssistantIfNeeded || return 1\n" + "mount -t iso9660 /dev/sr1 /mnt\n" - + "/mnt/VBoxLinuxAdditions.run\n" + "umount /mnt\n", scripts); + + "/mnt/VBoxLinuxAdditions.run\n" + + "service vboxadd setup\n" + + "VBoxService\n" + + "echo VBoxService > /etc/rc.local\n" + + "echo exit 0 >> /etc/rc.local\n" + + "umount /mnt\n" + , scripts); } - + public void testIsoNotPresent() { StorageController ideController = StorageController.builder().name("IDE Controller").bus(StorageBus.IDE).build(); @@ -61,7 +67,12 @@ public class InstallGuestAdditionsLiveTest extends BaseVirtualBoxClientLiveTest + "setupPublicCurl || return 1\n" + "(mkdir -p /tmp/ && cd /tmp/ && [ ! -f VBoxGuestAdditions_4.1.8.iso ] && curl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 -C - -X GET http://download.virtualbox.org/virtualbox/4.1.8/VBoxGuestAdditions_4.1.8.iso >VBoxGuestAdditions_4.1.8.iso)\n" + "mount -o loop /tmp/VBoxGuestAdditions_4.1.8.iso /mnt\n" - + "/mnt/VBoxLinuxAdditions.run\n" + "umount /mnt\n", scripts); + + "/mnt/VBoxLinuxAdditions.run\n" + + "service vboxadd setup\n" + + "VBoxService\n" + + "echo VBoxService > /etc/rc.local\n" + + "echo exit 0 >> /etc/rc.local\n" + + "umount /mnt\n", scripts); } } \ No newline at end of file diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/util/MachineControllerLiveTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/util/MachineControllerLiveTest.java index b719909b39..c1da4d7121 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/util/MachineControllerLiveTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/util/MachineControllerLiveTest.java @@ -19,7 +19,6 @@ package org.jclouds.virtualbox.util; -import static com.google.common.base.Preconditions.checkState; import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX; import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE; import static org.testng.AssertJUnit.assertTrue; @@ -97,12 +96,10 @@ public class MachineControllerLiveTest extends BaseVirtualBoxClientLiveTest { @Test public void testEnsureMachineisLaunchedAndSessionIsUnlocked() { - IMachine vm = cloneFromMaster(); + cloneFromMaster(); ISession cloneMachineSession = machineController.ensureMachineIsLaunched(instanceName); - System.out.println("lock - cloneMachineSession " + cloneMachineSession.toString()); assertTrue(cloneMachineSession.getState() == SessionState.Unlocked); cloneMachineSession = machineController.ensureMachineHasPowerDown(instanceName); - System.out.println("unlock - cloneMachineSession " + cloneMachineSession.toString()); assertTrue(cloneMachineSession.getState() == SessionState.Unlocked); }