From ef0b6891131ab03e13c5b1fcd2f0ef9c927cb3b2 Mon Sep 17 00:00:00 2001 From: Mattias Holmqvist Date: Sun, 15 Jan 2012 23:41:11 +0100 Subject: [PATCH] First step on IsoSpec/NetworkSpec/IMachineSpec. Also reverted ubuntu version to 11.04. --- .../VirtualBoxPropertiesBuilder.java | 3 +- .../virtualbox/domain/IMachineSpec.java | 113 +++++++++++++++++ .../jclouds/virtualbox/domain/IsoSpec.java | 116 ++++++++++++++++++ .../virtualbox/domain/NetworkSpec.java | 87 +++++++++++++ .../org/jclouds/virtualbox/domain/VmSpec.java | 45 ++----- .../functions/CreateAndInstallVm.java | 104 +++++++--------- ...isterMachineFromIsoIfNotAlreadyExists.java | 24 ++-- .../jclouds/virtualbox/domain/VmSpecTest.java | 3 - ...hineFromIsoIfNotAlreadyExistsLiveTest.java | 44 ++++--- .../functions/CreateAndInstallVmLiveTest.java | 59 +++++---- ...hineFromIsoIfNotAlreadyExistsLiveTest.java | 62 +++++++--- ...rMachineFromIsoIfNotAlreadyExistsTest.java | 60 ++++++--- ...rMachineIfExistsAndDeleteItsMediaTest.java | 8 +- .../predicates/IsLinkedClonesLiveTest.java | 23 ++-- 14 files changed, 545 insertions(+), 206 deletions(-) create mode 100644 sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/IMachineSpec.java create mode 100644 sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/IsoSpec.java create mode 100644 sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkSpec.java diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/VirtualBoxPropertiesBuilder.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/VirtualBoxPropertiesBuilder.java index 7b2014b818..2225281d11 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/VirtualBoxPropertiesBuilder.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/VirtualBoxPropertiesBuilder.java @@ -64,7 +64,8 @@ public class VirtualBoxPropertiesBuilder extends PropertiesBuilder { properties.put(VIRTUALBOX_ISO_URL, "http://releases.ubuntu.com/11.04/ubuntu-11.04-server-i386.iso"); properties.put(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE, " " + "/install/vmlinuz noapic preseed/url=PRECONFIGURATION_URL " - + "debian-installer=en_US auto locale=en_US kbd-chooser/method=us " + "hostname=" + "HOSTNAME " + + "debian-installer=en_US auto locale=en_US kbd-chooser/method=us " + + "hostname=" + "HOSTNAME " + "fb=false debconf/frontend=noninteractive " + "keyboard-configuration/layout=USA keyboard-configuration/variant=USA console-setup/ask_detect=false " + "initrd=/install/initrd.gz -- "); diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/IMachineSpec.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/IMachineSpec.java new file mode 100644 index 0000000000..b753d49b7b --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/IMachineSpec.java @@ -0,0 +1,113 @@ +/* + * 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.domain; + +import com.google.common.base.Objects; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * A complete specification of a "master" node, including the ISO, networking setup + * and the physical machine specification. + */ +public class IMachineSpec { + + private VmSpec vmSpec; + private IsoSpec isoSpec; + private NetworkSpec networkSpec; + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + + private VmSpec vmSpec; + private IsoSpec isoSpec; + private NetworkSpec networkSpec; + + public Builder vm(VmSpec vmSpec) { + this.vmSpec = vmSpec; + return this; + } + + public Builder network(NetworkSpec networkSpec) { + this.networkSpec = networkSpec; + return this; + } + + public Builder iso(IsoSpec isoSpec) { + this.isoSpec = isoSpec; + return this; + } + + public IMachineSpec build() { + return new IMachineSpec(vmSpec, isoSpec, networkSpec); + } + + } + + public IMachineSpec(VmSpec vmSpec, IsoSpec isoSpec, NetworkSpec networkSpec) { + checkNotNull(vmSpec, "vmSpec"); + checkNotNull(isoSpec, "isoSpec"); + checkNotNull(networkSpec, "networkSpec"); + this.vmSpec = vmSpec; + this.isoSpec = isoSpec; + this.networkSpec = networkSpec; + } + + public VmSpec getVmSpec() { + return vmSpec; + } + + public IsoSpec getIsoSpec() { + return isoSpec; + } + + public NetworkSpec getNetworkSpec() { + return networkSpec; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o instanceof VmSpec) { + IMachineSpec other = (IMachineSpec) o; + return Objects.equal(vmSpec, other.vmSpec) && + Objects.equal(isoSpec, other.isoSpec) && + Objects.equal(networkSpec, other.networkSpec); + } + return false; + } + + @Override + public int hashCode() { + return Objects.hashCode(vmSpec,isoSpec,networkSpec); + } + + @Override + public String toString() { + return "IMachineSpec{" + + "vmSpec=" + vmSpec + + ", isoSpec=" + isoSpec + + ", networkSpec=" + networkSpec + + '}'; + } +} diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/IsoSpec.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/IsoSpec.java new file mode 100644 index 0000000000..2b0bc46293 --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/IsoSpec.java @@ -0,0 +1,116 @@ +/* + * 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.domain; + +import com.google.common.base.Objects; +import com.google.common.base.Supplier; +import org.jclouds.virtualbox.Preconfiguration; + +import java.net.URI; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * The information needed to create a machine from a .iso file. + */ +public class IsoSpec { + + private final String installationKeySequence; + private final String sourcePath; + private final Supplier preConfigurationUri; + + public IsoSpec(String sourcePath, String installationKeySequence, @Preconfiguration Supplier preConfigurationUri) { + checkNotNull(sourcePath, "sourcePath"); + checkNotNull(installationKeySequence, "installationKeySequence"); + checkNotNull(preConfigurationUri, "preConfigurationUri"); + this.sourcePath = sourcePath; + this.installationKeySequence = installationKeySequence; + this.preConfigurationUri = preConfigurationUri; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + + private String installationSequence; + private Supplier preConfigurationUri; + private String sourcePath; + + public Builder installationScript(String installationSequence) { + this.installationSequence = installationSequence; + return this; + } + + public Builder preConfiguration(Supplier preConfigurationUri) { + this.preConfigurationUri = preConfigurationUri; + return this; + } + + public Builder sourcePath(String sourcePath) { + this.sourcePath = sourcePath; + return this; + } + + + public IsoSpec build() { + return new IsoSpec(sourcePath, installationSequence, preConfigurationUri); + } + } + + public String getInstallationKeySequence() { + return installationKeySequence; + } + + public Supplier getPreConfigurationUri() { + return preConfigurationUri; + } + + public String getSourcePath() { + return sourcePath; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o instanceof VmSpec) { + IsoSpec other = (IsoSpec) o; + return Objects.equal(sourcePath, other.sourcePath) && + Objects.equal(installationKeySequence, other.installationKeySequence) && + Objects.equal(preConfigurationUri, other.preConfigurationUri); + } + return false; + } + + @Override + public int hashCode() { + return Objects.hashCode(sourcePath, installationKeySequence, preConfigurationUri); + } + + @Override + public String toString() { + return "IsoSpec{" + + "sourcePath='" + sourcePath + '\'' + + "installationKeySequence='" + installationKeySequence + '\'' + + ", preConfigurationUri=" + preConfigurationUri + + '}'; + } +} diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkSpec.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkSpec.java new file mode 100644 index 0000000000..72bb8e5dcb --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/NetworkSpec.java @@ -0,0 +1,87 @@ +/* + * 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.domain; + +import com.google.common.base.Objects; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Describes the network configuration for a VirtualBox machine. + */ +public class NetworkSpec { + + private final Map natNetworkAdapters; + + public NetworkSpec(final Map natNetworkAdapters) { + checkNotNull(natNetworkAdapters, "natNetworkAdapters"); + this.natNetworkAdapters = natNetworkAdapters; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + + private Map natNetworkAdapters = new HashMap(); + + public Builder natNetworkAdapter(int slot, NatAdapter adapter) { + this.natNetworkAdapters.put((long) slot, adapter); + return this; + } + + public NetworkSpec build() { + return new NetworkSpec(natNetworkAdapters); + } + } + + + public Map getNatNetworkAdapters() { + return Collections.unmodifiableMap(natNetworkAdapters); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o instanceof VmSpec) { + NetworkSpec other = (NetworkSpec) o; + return Objects.equal(natNetworkAdapters, other.natNetworkAdapters); + } + return false; + } + + @Override + public int hashCode() { + return Objects.hashCode(natNetworkAdapters); + } + + + @Override + public String toString() { + return "NetworkSpec{" + + "natNetworkAdapters=" + natNetworkAdapters + + '}'; + } +} diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/VmSpec.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/VmSpec.java index 8f61cdc827..2b9a0c640e 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/VmSpec.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/VmSpec.java @@ -18,18 +18,13 @@ */ package org.jclouds.virtualbox.domain; -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; - -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - +import com.google.common.base.Objects; import org.virtualbox_4_1.CleanupMode; -import com.google.common.base.Objects; +import java.util.*; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; /** * A description of a Virtual Machine in VirtualBox. @@ -41,16 +36,14 @@ public class VmSpec { private final String vmId; private final long memory; private final boolean forceOverwrite; - private final Map natNetworkAdapters; private final Set controllers; private final CleanupMode cleanupMode; - public VmSpec(String vmId, String vmName, String osTypeId, long memory, boolean forceOverwrite, Set controllers, Map natNetworkAdapters, CleanupMode cleanupMode) { + public VmSpec(String vmId, String vmName, String osTypeId, long memory, boolean forceOverwrite, Set controllers, CleanupMode cleanupMode) { checkNotNull(vmId, "vmId"); checkNotNull(vmName, "vmName"); checkArgument(memory > 0, "memory must be > 0"); checkNotNull(controllers, "controllers"); - checkNotNull(natNetworkAdapters, "natNetworkAdapters"); checkNotNull(cleanupMode, "cleanupMode"); this.vmId = vmId; this.vmName = vmName; @@ -58,7 +51,6 @@ public class VmSpec { this.memory = memory; this.controllers = controllers; this.forceOverwrite = forceOverwrite; - this.natNetworkAdapters = natNetworkAdapters; this.cleanupMode = cleanupMode; } @@ -74,10 +66,9 @@ public class VmSpec { private String id; private String osTypeId = ""; private boolean forceOverwrite; - private Map natNetworkAdapters = new HashMap(); private long memory; private CleanupMode cleanUpMode; - + public Builder controller(StorageController controller) { controllers.add(controller); return this; @@ -103,30 +94,24 @@ public class VmSpec { return this; } - public Builder natNetworkAdapter(int slot, NatAdapter adapter) { - this.natNetworkAdapters.put((long) slot, adapter); - return this; - } - public Builder memoryMB(int memorySize) { this.memory = (long) memorySize; return this; } - + public Builder cleanUpMode(CleanupMode cleanupMode) { this.cleanUpMode = cleanupMode; return this; - } + } public VmSpec build() { checkNotNull(name, "name"); checkNotNull(id, "id"); checkArgument(memory > 0, "Memory must be set"); - return new VmSpec(id, name, osTypeId, memory, forceOverwrite, controllers, natNetworkAdapters, cleanUpMode); + return new VmSpec(id, name, osTypeId, memory, forceOverwrite, controllers, cleanUpMode); } - - } + public String getVmId() { return vmId; } @@ -151,10 +136,6 @@ public class VmSpec { return Collections.unmodifiableSet(controllers); } - public Map getNatNetworkAdapters() { - return Collections.unmodifiableMap(natNetworkAdapters); - } - public CleanupMode getCleanupMode() { return cleanupMode; } @@ -169,7 +150,6 @@ public class VmSpec { Objects.equal(osTypeId, other.osTypeId) && Objects.equal(memory, other.memory) && Objects.equal(forceOverwrite, other.forceOverwrite) && - Objects.equal(natNetworkAdapters, other.natNetworkAdapters) && Objects.equal(controllers, other.controllers) && Objects.equal(cleanupMode, other.cleanupMode); } @@ -178,7 +158,7 @@ public class VmSpec { @Override public int hashCode() { - return Objects.hashCode(vmId, vmName, osTypeId, memory, forceOverwrite, natNetworkAdapters, controllers); + return Objects.hashCode(vmId, vmName, osTypeId, memory, forceOverwrite, controllers); } @Override @@ -189,7 +169,6 @@ public class VmSpec { ", memory='" + memory + '\'' + ", vmId='" + vmId + '\'' + ", forceOverwrite=" + forceOverwrite + - ", natNetworkAdapters=" + natNetworkAdapters + ", controllers=" + controllers + ", cleanupMode=" + cleanupMode + '}'; diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java index 9d86fd6a3a..e128bfbe44 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java @@ -18,30 +18,20 @@ */ package org.jclouds.virtualbox.functions; -import static com.google.common.base.Preconditions.checkState; -import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot; -import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE; -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 java.net.URI; -import java.util.List; - -import javax.annotation.Resource; -import javax.inject.Named; -import javax.inject.Singleton; - +import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.common.base.Supplier; +import com.google.inject.Inject; import org.jclouds.compute.callables.RunScriptOnNode; import org.jclouds.compute.callables.RunScriptOnNode.Factory; import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.reference.ComputeServiceConstants; -import org.jclouds.config.ValueOfConfigurationKeyOrNull; import org.jclouds.logging.Logger; import org.jclouds.scriptbuilder.domain.Statements; import org.jclouds.ssh.SshClient; -import org.jclouds.virtualbox.Preconfiguration; import org.jclouds.virtualbox.domain.ExecutionType; +import org.jclouds.virtualbox.domain.IMachineSpec; +import org.jclouds.virtualbox.domain.IsoSpec; import org.jclouds.virtualbox.domain.VmSpec; import org.jclouds.virtualbox.settings.KeyboardScancodes; import org.virtualbox_4_1.IMachine; @@ -49,16 +39,19 @@ import org.virtualbox_4_1.IProgress; import org.virtualbox_4_1.ISession; import org.virtualbox_4_1.VirtualBoxManager; -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.base.Splitter; -import com.google.common.base.Supplier; -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; -import com.google.inject.Inject; +import javax.annotation.Resource; +import javax.inject.Named; +import javax.inject.Singleton; +import java.net.URI; + +import static com.google.common.base.Preconditions.checkState; +import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot; +import static org.jclouds.virtualbox.util.MachineUtils.applyForMachine; +import static org.jclouds.virtualbox.util.MachineUtils.lockSessionOnMachineAndApply; +import static org.virtualbox_4_1.LockType.Shared; @Singleton -public class CreateAndInstallVm implements Function { +public class CreateAndInstallVm implements Function { @Resource @Named(ComputeServiceConstants.COMPUTE_LOGGER) @@ -66,9 +59,7 @@ public class CreateAndInstallVm implements Function { private final Supplier manager; private final CreateAndRegisterMachineFromIsoIfNotAlreadyExists createAndRegisterMachineFromIsoIfNotAlreadyExists; - private final ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull; - private final Supplier preconfiguration; private final Predicate sshResponds; private final ExecutionType executionType; @@ -78,66 +69,53 @@ public class CreateAndInstallVm implements Function { private final Function sshClientForIMachine; @Inject - public CreateAndInstallVm( - Supplier manager, - CreateAndRegisterMachineFromIsoIfNotAlreadyExists CreateAndRegisterMachineFromIsoIfNotAlreadyExists, - ValueOfConfigurationKeyOrNull valueOfConfigurationKeyOrNull, - Predicate sshResponds, - Function sshClientForIMachine, - Supplier host, RunScriptOnNode.Factory scriptRunner, - @Preconfiguration Supplier preconfiguration, - ExecutionType executionType) { + public CreateAndInstallVm(Supplier manager, + CreateAndRegisterMachineFromIsoIfNotAlreadyExists CreateAndRegisterMachineFromIsoIfNotAlreadyExists, + Predicate sshResponds, Function sshClientForIMachine, + Supplier host, RunScriptOnNode.Factory scriptRunner, ExecutionType executionType) { this.manager = manager; this.createAndRegisterMachineFromIsoIfNotAlreadyExists = CreateAndRegisterMachineFromIsoIfNotAlreadyExists; - this.valueOfConfigurationKeyOrNull = valueOfConfigurationKeyOrNull; this.sshResponds = sshResponds; this.sshClientForIMachine = sshClientForIMachine; this.scriptRunner = scriptRunner; this.host = host; - this.preconfiguration = preconfiguration; this.executionType = executionType; } @Override - public IMachine apply(VmSpec vmSpec) { + public IMachine apply(IMachineSpec machineSpec) { + + VmSpec vmSpec = machineSpec.getVmSpec(); + IsoSpec isoSpec = machineSpec.getIsoSpec(); + String vmName = vmSpec.getVmName(); - // note this may not be reachable, as this likely uses the 10.2.2 address - URI preconfigurationUri = preconfiguration.get(); - String keySequence = valueOfConfigurationKeyOrNull - .apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE) - .replace("PRECONFIGURATION_URL", - preconfigurationUri.toASCIIString()) - .replace("HOSTNAME", vmName); - - final IMachine vm = createAndRegisterMachineFromIsoIfNotAlreadyExists - .apply(vmSpec); + final IMachine vm = createAndRegisterMachineFromIsoIfNotAlreadyExists.apply(machineSpec); // Launch machine and wait for it to come online ensureMachineIsLaunched(vmName); - sendKeyboardSequence(keySequence, vmName); + URI uri = isoSpec.getPreConfigurationUri().get(); + String installationKeySequence = isoSpec.getInstallationKeySequence() + .replace("PRECONFIGURATION_URL", uri.toASCIIString()); + sendKeyboardSequence(installationKeySequence, vmName); 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); + checkState(sshResponds.apply(client), "timed out waiting for guest %s to be accessible via ssh", vmName); - logger.debug("<< installation of image complete. Powering down node(%s)", - vmName); - lockSessionOnMachineAndApply(manager.get(), Shared, vmName, - new Function() { + logger.debug("<< installation of image complete. Powering down node(%s)", vmName); + lockSessionOnMachineAndApply(manager.get(), Shared, vmName, new Function() { - @Override - public Void apply(ISession session) { - IProgress powerDownProgress = session.getConsole() - .powerDown(); - powerDownProgress.waitForCompletion(-1); - return null; - } + @Override + public Void apply(ISession session) { + IProgress powerDownProgress = session.getConsole().powerDown(); + powerDownProgress.waitForCompletion(-1); + return null; + } - }); + }); return vm; } diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExists.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExists.java index 5947b6ebe1..392911c6f9 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExists.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExists.java @@ -36,12 +36,7 @@ import javax.inject.Singleton; import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.logging.Logger; import org.jclouds.virtualbox.config.VirtualBoxConstants; -import org.jclouds.virtualbox.domain.DeviceDetails; -import org.jclouds.virtualbox.domain.HardDisk; -import org.jclouds.virtualbox.domain.IsoImage; -import org.jclouds.virtualbox.domain.NatAdapter; -import org.jclouds.virtualbox.domain.StorageController; -import org.jclouds.virtualbox.domain.VmSpec; +import org.jclouds.virtualbox.domain.*; import org.virtualbox_4_1.AccessMode; import org.virtualbox_4_1.DeviceType; import org.virtualbox_4_1.IMachine; @@ -57,7 +52,7 @@ import com.google.common.base.Supplier; * @author Mattias Holmqvist */ @Singleton -public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Function { +public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Function { @Resource @Named(ComputeServiceConstants.COMPUTE_LOGGER) @@ -74,9 +69,9 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Functi } @Override - public IMachine apply(@Nullable VmSpec launchSpecification) { + public IMachine apply(@Nullable IMachineSpec launchSpecification) { final IVirtualBox vBox = manager.get().getVBox(); - String vmName = launchSpecification.getVmName(); + String vmName = launchSpecification.getVmSpec().getVmName(); try { vBox.findMachine(vmName); throw new IllegalStateException("Machine " + vmName + " is already registered."); @@ -92,17 +87,20 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Functi return e.getMessage().contains("VirtualBox error: Could not find a registered machine named "); } - private IMachine createMachine(IVirtualBox vBox, VmSpec vmSpec) { + private IMachine createMachine(IVirtualBox vBox, IMachineSpec machineSpec) { + VmSpec vmSpec = machineSpec.getVmSpec(); String settingsFile = vBox.composeMachineFilename(vmSpec.getVmName(), workingDir); IMachine newMachine = vBox.createMachine(settingsFile, vmSpec.getVmName(), vmSpec.getOsTypeId(), vmSpec.getVmId(), vmSpec.isForceOverwrite()); manager.get().getVBox().registerMachine(newMachine); - ensureConfiguration(vmSpec); + ensureConfiguration(machineSpec); return newMachine; } - private void ensureConfiguration(VmSpec vmSpec) { + private void ensureConfiguration(IMachineSpec machineSpec) { + VmSpec vmSpec = machineSpec.getVmSpec(); + NetworkSpec networkSpec = machineSpec.getNetworkSpec(); String vmName = vmSpec.getVmName(); // Change RAM @@ -118,7 +116,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Functi setupDvdsForController(vmSpec, vmName, controller); // NAT - Map natNetworkAdapters = vmSpec.getNatNetworkAdapters(); + Map natNetworkAdapters = networkSpec.getNatNetworkAdapters(); for (Map.Entry natAdapterAndSlot : natNetworkAdapters.entrySet()) { long slotId = natAdapterAndSlot.getKey(); NatAdapter natAdapter = natAdapterAndSlot.getValue(); diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/VmSpecTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/VmSpecTest.java index 3b1596f089..8d9ec5d11c 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/VmSpecTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/VmSpecTest.java @@ -70,9 +70,6 @@ public class VmSpecTest { .osTypeId("Ubuntu") .memoryMB(1024) .cleanUpMode(CleanupMode.Full) - .natNetworkAdapter( - 0, - NatAdapter.builder().tcpRedirectRule("localhost", 2222, "", 22).build()) .forceOverwrite(true) .controller( StorageController.builder().name("Controller") diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java index d4558d151c..e71553a2a8 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java @@ -22,10 +22,9 @@ package org.jclouds.virtualbox.functions; import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX; import static org.testng.Assert.assertEquals; +import com.google.inject.Injector; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; -import org.jclouds.virtualbox.domain.HardDisk; -import org.jclouds.virtualbox.domain.StorageController; -import org.jclouds.virtualbox.domain.VmSpec; +import org.jclouds.virtualbox.domain.*; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.virtualbox_4_1.CleanupMode; @@ -45,7 +44,8 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba private static final boolean IS_LINKED_CLONE = true; private VmSpec clonedVmSpec; - private VmSpec sourceVmSpec; + private IMachineSpec sourceMachineSpec; + private CleanupMode mode = CleanupMode.Full; @@ -53,22 +53,25 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba @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#" + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName() - ); + String sourceName = VIRTUALBOX_IMAGE_PREFIX + + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName()); + String cloneName = VIRTUALBOX_IMAGE_PREFIX + + "Clone#" + 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).controllerPort(0).deviceSlot(1).autoDelete(true).build()).attachISO(1, 1, - guestAdditionsIso).build(); + .attachISO(0, 0, operatingSystemIso).attachHardDisk( + HardDisk.builder().diskpath(adminDisk).controllerPort(0).deviceSlot(1).autoDelete(true).build()).attachISO(1, 1, + guestAdditionsIso).build(); - sourceVmSpec = VmSpec.builder().id(sourceName).name(sourceName).osTypeId("").memoryMB(512).cleanUpMode( - CleanupMode.Full).controller(ideController).forceOverwrite(true).build(); + VmSpec sourceVmSpec = VmSpec.builder().id(sourceName).name(sourceName).osTypeId("").memoryMB(512).cleanUpMode( + CleanupMode.Full).controller(ideController).forceOverwrite(true).build(); + IsoSpec isoSpec = IsoSpec.builder().build(); + NetworkSpec networkSpec = NetworkSpec.builder().build(); + sourceMachineSpec = IMachineSpec.builder().iso(isoSpec).vm(sourceVmSpec).network(networkSpec).build(); clonedVmSpec = VmSpec.builder().id(cloneName).name(cloneName).memoryMB(512).cleanUpMode(mode) - .forceOverwrite(true).build(); + .forceOverwrite(true).build(); } @Test @@ -83,10 +86,10 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba } IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, clonedVmSpec, - IS_LINKED_CLONE).apply(source); + IS_LINKED_CLONE).apply(source); assertEquals(clone.getName(), clonedVmSpec.getVmName()); } finally { - for (VmSpec spec : ImmutableSet.of(clonedVmSpec, sourceVmSpec)) + for (VmSpec spec : ImmutableSet.of(clonedVmSpec, sourceMachineSpec.getVmSpec())) undoVm(spec); } @@ -94,11 +97,12 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba private IMachine getSourceNode() { try { - return context.utils().injector().getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply( - sourceVmSpec); + Injector injector = context.utils().injector(); + return injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply( + sourceMachineSpec); } catch (IllegalStateException e) { // already created - return manager.get().getVBox().findMachine(sourceVmSpec.getVmId()); + return manager.get().getVBox().findMachine(sourceMachineSpec.getVmSpec().getVmId()); } } } \ No newline at end of file diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java index b3249d1376..911bab1f6d 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndInstallVmLiveTest.java @@ -19,28 +19,19 @@ package org.jclouds.virtualbox.functions; -import static com.google.common.base.Predicates.equalTo; -import static com.google.common.collect.Iterables.any; -import static com.google.common.collect.Iterables.transform; -import static org.testng.Assert.assertTrue; -import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX; - -import java.util.Map; -import java.util.Set; - -import javax.annotation.Nullable; - +import com.google.common.base.CaseFormat; +import com.google.common.base.Function; +import com.google.inject.Guice; +import com.google.inject.Injector; import org.jclouds.compute.config.BaseComputeServiceContextModule; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.OsFamily; import org.jclouds.compute.reference.ComputeServiceConstants; +import org.jclouds.config.ValueOfConfigurationKeyOrNull; import org.jclouds.json.Json; import org.jclouds.json.config.GsonModule; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; -import org.jclouds.virtualbox.domain.HardDisk; -import org.jclouds.virtualbox.domain.NatAdapter; -import org.jclouds.virtualbox.domain.StorageController; -import org.jclouds.virtualbox.domain.VmSpec; +import org.jclouds.virtualbox.domain.*; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -48,9 +39,16 @@ import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.StorageBus; -import com.google.common.base.CaseFormat; -import com.google.common.base.Function; -import com.google.inject.Guice; +import javax.annotation.Nullable; +import java.util.Map; +import java.util.Set; + +import static com.google.common.base.Predicates.equalTo; +import static com.google.common.collect.Iterables.any; +import static com.google.common.collect.Iterables.transform; +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.Assert.assertTrue; /** * @author Andrea Turli, Mattias Holmqvist @@ -68,11 +66,11 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest { @BeforeClass(groups = "live") public void setupClient() { super.setupClient(); - String vmName = VIRTUALBOX_IMAGE_PREFIX - + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName()); + String vmName = VIRTUALBOX_IMAGE_PREFIX + + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName()); HardDisk hardDisk = HardDisk.builder().diskpath(adminDisk).autoDelete(true) - .controllerPort(0).deviceSlot(1).build(); + .controllerPort(0).deviceSlot(1).build(); StorageController ideController = StorageController.builder().name("IDE Controller").bus(StorageBus.IDE) .attachISO(0, 0, operatingSystemIso) .attachHardDisk(hardDisk) @@ -80,14 +78,26 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest { vmSpecification = VmSpec.builder().id("jclouds-image-iso-1").name(vmName).memoryMB(512).osTypeId("") .controller(ideController) .forceOverwrite(true) - .cleanUpMode(CleanupMode.Full) - .natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build()).build(); + .cleanUpMode(CleanupMode.Full).build(); undoVm(vmSpecification); } public void testCreateImageMachineFromIso() throws Exception { + Injector injector = context.utils().injector(); + Function configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class); - IMachine imageMachine = context.utils().injector().getInstance(CreateAndInstallVm.class).apply(vmSpecification); + IMachineSpec machineSpec = IMachineSpec.builder().vm(vmSpecification) + .iso(IsoSpec.builder() + .sourcePath(operatingSystemIso) + .installationScript(configProperties + .apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE) + .replace("HOSTNAME", vmSpecification.getVmName())) + .preConfiguration(preconfigurationUri) + .build()) + .network(NetworkSpec.builder() + .natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build()) + .build()).build(); + IMachine imageMachine = injector.getInstance(CreateAndInstallVm.class).apply(machineSpec); IMachineToImage iMachineToImage = new IMachineToImage(manager, map); Image newImage = iMachineToImage.apply(imageMachine); @@ -107,6 +117,7 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest { } }; } + @Override @AfterClass(groups = "live") protected void tearDown() throws Exception { diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java index 9f8664d64c..36bdbe2ce0 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest.java @@ -21,11 +21,9 @@ package org.jclouds.virtualbox.functions; import static org.testng.Assert.assertEquals; import static org.testng.Assert.fail; +import com.google.inject.Injector; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; -import org.jclouds.virtualbox.domain.ErrorCode; -import org.jclouds.virtualbox.domain.HardDisk; -import org.jclouds.virtualbox.domain.StorageController; -import org.jclouds.virtualbox.domain.VmSpec; +import org.jclouds.virtualbox.domain.*; import org.testng.annotations.Test; import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.IMachine; @@ -47,38 +45,62 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends B super.setupClient(); ideControllerName = "IDE Controller"; mode = CleanupMode.Full; - ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE).attachISO(0, 0, - operatingSystemIso).attachHardDisk( - HardDisk.builder().diskpath(adminDisk).controllerPort(0).deviceSlot(1).build()).attachISO(1, 1, - guestAdditionsIso).build(); + ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE) + .attachISO(0, 0, operatingSystemIso) + .attachHardDisk(HardDisk.builder().diskpath(adminDisk).controllerPort(0).deviceSlot(1).build()).attachISO(1, 1, + guestAdditionsIso).build(); } @Test public void testCreateNewMachine() throws Exception { String vmName = "jclouds-test-create-1-node"; - VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName).memoryMB(512).controller(ideController) - .cleanUpMode(mode).osTypeId("Debian").forceOverwrite(true).build(); - undoVm(launchSpecification); + VmSpec vmSpec = VmSpec.builder() + .id(vmName) + .name(vmName) + .memoryMB(512) + .controller(ideController) + .cleanUpMode(mode) + .osTypeId("Debian") + .forceOverwrite(true).build(); + IMachineSpec machineSpec = IMachineSpec.builder() + .iso(IsoSpec.builder() + .sourcePath(operatingSystemIso) + .installationScript("") + .preConfiguration(preconfigurationUri) + .build()) + .vm(vmSpec) + .network(NetworkSpec.builder().build()).build(); + undoVm(vmSpec); try { IMachine debianNode = context.utils().injector().getInstance( - CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(launchSpecification); + CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(machineSpec); IMachine machine = manager.get().getVBox().findMachine(vmName); assertEquals(debianNode.getName(), machine.getName()); } finally { - undoVm(launchSpecification); + undoVm(vmSpec); } } @Test public void testCreateNewMachineWithBadOsType() throws Exception { String vmName = "jclouds-test-create-2-node"; - VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName).memoryMB(512).controller(ideController) - .cleanUpMode(mode).osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build(); - - undoVm(launchSpecification); + VmSpec vmSpec = VmSpec.builder().id(vmName).name(vmName).memoryMB(512).controller(ideController) + .cleanUpMode(mode).osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build(); + IsoSpec isoSpec = IsoSpec.builder() + .sourcePath(operatingSystemIso) + .installationScript("") + .preConfiguration(preconfigurationUri) + .build(); + NetworkSpec networkSpec = NetworkSpec.builder().build(); + IMachineSpec machineSpec = IMachineSpec.builder() + .iso(isoSpec) + .vm(vmSpec) + .network(networkSpec).build(); + undoVm(vmSpec); try { - context.utils().injector().getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply( - launchSpecification); + Injector injector = context.utils().injector(); + injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class) + .apply(machineSpec); fail(); } catch (VBoxException e) { ErrorCode errorCode = ErrorCode.valueOf(e); @@ -86,7 +108,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends B // if osTypeId is not found. assertEquals(errorCode, ErrorCode.VBOX_E_OBJECT_NOT_FOUND); } finally { - undoVm(launchSpecification); + undoVm(vmSpec); } } diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest.java index d285e3fb22..a0918deddd 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest.java @@ -27,7 +27,14 @@ import static org.easymock.classextension.EasyMock.createNiceMock; import static org.easymock.classextension.EasyMock.replay; import static org.easymock.classextension.EasyMock.verify; +import com.google.common.base.Supplier; +import com.google.inject.Key; +import com.google.inject.TypeLiteral; import org.easymock.EasyMock; +import org.jclouds.virtualbox.Preconfiguration; +import org.jclouds.virtualbox.domain.IMachineSpec; +import org.jclouds.virtualbox.domain.IsoSpec; +import org.jclouds.virtualbox.domain.NetworkSpec; import org.jclouds.virtualbox.domain.VmSpec; import org.testng.annotations.Test; import org.virtualbox_4_1.CleanupMode; @@ -40,6 +47,8 @@ import org.virtualbox_4_1.VirtualBoxManager; import com.google.common.base.Suppliers; +import java.net.URI; + /** * @author Mattias Holmqvist */ @@ -51,11 +60,14 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest { VirtualBoxManager manager = createMock(VirtualBoxManager.class); IVirtualBox vBox = createMock(IVirtualBox.class); + Supplier preconfiguration = createNiceMock(Supplier.class); String vmName = "jclouds-image-my-ubuntu-image"; - - VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName).osTypeId("").memoryMB(1024).cleanUpMode( - CleanupMode.Full).build(); - + VmSpec vmSpec = VmSpec.builder().id(vmName).name(vmName).osTypeId("").memoryMB(1024).cleanUpMode( + CleanupMode.Full).build(); + IMachineSpec machineSpec = IMachineSpec.builder() + .iso(IsoSpec.builder().sourcePath("some.iso").installationScript("").preConfiguration(preconfiguration).build()) + .vm(vmSpec) + .network(NetworkSpec.builder().build()).build(); IMachine createdMachine = createMock(IMachine.class); ISession session = createMock(ISession.class); @@ -71,9 +83,9 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest { expect(vBox.findMachine(vmName)).andThrow(vBoxException); expect(vBox.createMachine(anyString(), eq(vmName), anyString(), anyString(), anyBoolean())).andReturn( - createdMachine).anyTimes(); + createdMachine).anyTimes(); vBox.registerMachine(createdMachine); - + expect(vBox.findMachine(vmName)).andReturn(createdMachine).anyTimes(); expect(manager.getSessionObject()).andReturn(session); expect(session.getMachine()).andReturn(createdMachine); @@ -81,13 +93,13 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest { createdMachine.setMemorySize(1024l); createdMachine.saveSettings(); session.unlockMachine(); - - + + //TODO: this mock test is not finished. - + replay(manager, createdMachine, vBox, session); - new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(Suppliers.ofInstance(manager), "/tmp/workingDir").apply(launchSpecification); + new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(Suppliers.ofInstance(manager), "/tmp/workingDir").apply(machineSpec); verify(manager, createdMachine, vBox, session); } @@ -97,6 +109,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest { VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class); IVirtualBox vBox = createNiceMock(IVirtualBox.class); + Supplier preconfiguration = createNiceMock(Supplier.class); String vmName = "jclouds-image-my-ubuntu-image"; IMachine registeredMachine = createMock(IMachine.class); @@ -107,8 +120,16 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest { replay(manager, vBox); VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").memoryMB(1024).cleanUpMode( - CleanupMode.Full).build(); - new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(Suppliers.ofInstance(manager), "/tmp/workingDir").apply(launchSpecification); + CleanupMode.Full).build(); + + IMachineSpec machineSpec = IMachineSpec.builder() + .iso(IsoSpec.builder() + .sourcePath("some.iso") + .installationScript("dostuff") + .preConfiguration(preconfiguration).build()) + .vm(launchSpecification) + .network(NetworkSpec.builder().build()).build(); + new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(Suppliers.ofInstance(manager), "/tmp/workingDir").apply(machineSpec); } @Test(expectedExceptions = VBoxException.class) @@ -116,6 +137,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest { VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class); IVirtualBox vBox = createNiceMock(IVirtualBox.class); + Supplier preconfiguration = createNiceMock(Supplier.class); String vmName = "jclouds-image-my-ubuntu-image"; String errorMessage = "VirtualBox error: Soem other VBox error"; @@ -129,12 +151,20 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest { replay(manager, vBox); VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").cleanUpMode(CleanupMode.Full) - .memoryMB(1024).build(); - new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(Suppliers.ofInstance(manager), "/tmp/workingDir").apply(launchSpecification); + .memoryMB(1024).build(); + IMachineSpec machineSpec = IMachineSpec.builder() + .iso(IsoSpec.builder() + .sourcePath("some.iso") + .installationScript("dostuff") + .preConfiguration(preconfiguration).build()) + .vm(launchSpecification) + .network(NetworkSpec.builder().build()).build(); + + new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(Suppliers.ofInstance(manager), "/tmp/workingDir").apply(machineSpec); } private String anyString() { - return EasyMock. anyObject(); + return EasyMock.anyObject(); } } diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMediaTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMediaTest.java index 3946717d24..b6382fd5aa 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMediaTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMediaTest.java @@ -28,10 +28,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import org.jclouds.virtualbox.domain.HardDisk; -import org.jclouds.virtualbox.domain.NatAdapter; -import org.jclouds.virtualbox.domain.StorageController; -import org.jclouds.virtualbox.domain.VmSpec; +import org.jclouds.virtualbox.domain.*; import org.testng.annotations.Test; import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.IMachine; @@ -66,8 +63,7 @@ public class UnregisterMachineIfExistsAndDeleteItsMediaTest { VmSpec vmSpecification = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).osTypeId(osTypeId) .controller(ideController) .forceOverwrite(true) - .cleanUpMode(CleanupMode.Full) - .natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build()).build(); + .cleanUpMode(CleanupMode.Full).build(); expect(manager.getVBox()).andReturn(vBox).anyTimes(); expect(vBox.findMachine(vmName)).andReturn(registeredMachine); diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/IsLinkedClonesLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/IsLinkedClonesLiveTest.java index b4a86d8bcf..3459020af7 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/IsLinkedClonesLiveTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/predicates/IsLinkedClonesLiveTest.java @@ -22,10 +22,9 @@ import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; +import com.google.inject.Injector; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; -import org.jclouds.virtualbox.domain.HardDisk; -import org.jclouds.virtualbox.domain.StorageController; -import org.jclouds.virtualbox.domain.VmSpec; +import org.jclouds.virtualbox.domain.*; import org.jclouds.virtualbox.functions.CloneAndRegisterMachineFromIMachineIfNotAlreadyExists; import org.jclouds.virtualbox.functions.CreateAndRegisterMachineFromIsoIfNotAlreadyExists; import org.testng.annotations.AfterMethod; @@ -53,7 +52,7 @@ public class IsLinkedClonesLiveTest extends BaseVirtualBoxClientLiveTest { private String cloneName; private String vmName; private StorageController masterStorageController; - private VmSpec masterSpec; + private IMachineSpec masterMachineSpec; private VmSpec cloneSpec; @Override @@ -70,8 +69,15 @@ public class IsLinkedClonesLiveTest extends BaseVirtualBoxClientLiveTest { .build(); masterStorageController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE).attachISO(0, 0, operatingSystemIso).attachHardDisk(hardDisk).attachISO(1, 1, guestAdditionsIso).build(); - masterSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).osTypeId(osTypeId).controller( + VmSpec masterSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).osTypeId(osTypeId).controller( masterStorageController).forceOverwrite(true).cleanUpMode(CleanupMode.Full).build(); + masterMachineSpec = IMachineSpec.builder() + .iso(IsoSpec.builder() + .sourcePath(operatingSystemIso) + .preConfiguration(preconfigurationUri) + .installationScript("").build()) + .vm(masterSpec) + .network(NetworkSpec.builder().build()).build(); cloneSpec = VmSpec.builder().id(cloneName).name(cloneName).memoryMB(512).cleanUpMode(CleanupMode.Full) .forceOverwrite(true).build(); @@ -80,8 +86,9 @@ public class IsLinkedClonesLiveTest extends BaseVirtualBoxClientLiveTest { @Test public void testLinkedClone() { - IMachine master = context.utils().injector().getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class) - .apply(masterSpec); + Injector injector = context.utils().injector(); + IMachine master = injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class) + .apply(masterMachineSpec); IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, cloneSpec, IS_LINKED_CLONE).apply(master); @@ -102,7 +109,7 @@ public class IsLinkedClonesLiveTest extends BaseVirtualBoxClientLiveTest { @BeforeMethod @AfterMethod void cleanUpVms() { - for (VmSpec spec : ImmutableSet.of(cloneSpec, masterSpec)) + for (VmSpec spec : ImmutableSet.of(cloneSpec, masterMachineSpec.getVmSpec())) this.undoVm(spec); } }