From 13f1a68b6766e362ab246f8ef7175b770982d00b Mon Sep 17 00:00:00 2001 From: Mattias Holmqvist Date: Wed, 30 Nov 2011 08:27:29 +0100 Subject: [PATCH] Removed settingsFile until properly supported. Added live tests for AttachDistroMediumToMachine. --- ...isterMachineFromIsoIfNotAlreadyExists.java | 15 ++-- .../virtualbox/functions/IsoToIMachine.java | 6 +- .../AttachDistroMediumToMachineLiveTest.java | 80 +++++++++++++++++++ ...hineFromIsoIfNotAlreadyExistsLiveTest.java | 2 +- ...hineFromIsoIfNotAlreadyExistsLiveTest.java | 4 +- ...rMachineFromIsoIfNotAlreadyExistsTest.java | 6 +- .../functions/IsoToIMachineLiveTest.java | 6 +- 7 files changed, 96 insertions(+), 23 deletions(-) create mode 100644 sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/AttachDistroMediumToMachineLiveTest.java 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 e9d4dcc3c2..e7681147b1 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 @@ -19,29 +19,26 @@ package org.jclouds.virtualbox.functions; -import javax.annotation.Nullable; - +import com.google.common.base.Function; import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IVirtualBox; import org.virtualbox_4_1.VBoxException; import org.virtualbox_4_1.VirtualBoxManager; -import com.google.common.base.Function; +import javax.annotation.Nullable; /** * @author Mattias Holmqvist */ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Function { - private String settingsFile; private String osTypeId; private String vmId; private boolean forceOverwrite; private VirtualBoxManager manager; - public CreateAndRegisterMachineFromIsoIfNotAlreadyExists(String settingsFile, String osTypeId, String vmId, - boolean forceOverwrite, VirtualBoxManager manager) { - this.settingsFile = settingsFile; + public CreateAndRegisterMachineFromIsoIfNotAlreadyExists(String osTypeId, String vmId, + boolean forceOverwrite, VirtualBoxManager manager) { this.osTypeId = osTypeId; this.vmId = vmId; this.forceOverwrite = forceOverwrite; @@ -68,7 +65,9 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Functi } private IMachine createMachine(IVirtualBox vBox, String vmName) { - IMachine newMachine = vBox.createMachine(settingsFile, vmName, osTypeId, vmId, forceOverwrite); + // TODO: add support for settingsfile + String settingsFile1 = null; + IMachine newMachine = vBox.createMachine(settingsFile1, vmName, osTypeId, vmId, forceOverwrite); manager.getVBox().registerMachine(newMachine); return newMachine; } diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java index 1fa341b379..91fec429ef 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java @@ -61,7 +61,6 @@ public class IsoToIMachine implements Function { private VirtualBoxManager manager; private String adminDisk; private String diskFormat; - private String settingsFile; private String vmName; private String osTypeId; private String vmId; @@ -75,14 +74,13 @@ public class IsoToIMachine implements Function { private int webServerPort; @Inject - public IsoToIMachine(VirtualBoxManager manager, String adminDisk, String diskFormat, String settingsFile, + public IsoToIMachine(VirtualBoxManager manager, String adminDisk, String diskFormat, String vmName, String osTypeId, String vmId, boolean forceOverwrite, String controllerIDE, ComputeServiceContext context, String hostId, String guestId, Predicate socketTester, String webServerHost, int webServerPort) { this.manager = manager; this.adminDisk = adminDisk; this.diskFormat = diskFormat; - this.settingsFile = settingsFile; this.vmName = vmName; this.osTypeId = osTypeId; this.vmId = vmId; @@ -101,7 +99,7 @@ public class IsoToIMachine implements Function { ensureWebServerIsRunning(); - final IMachine vm = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(settingsFile, osTypeId, vmId, forceOverwrite, + final IMachine vm = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(osTypeId, vmId, forceOverwrite, manager).apply(vmName); final String defaultWorkingDir = System.getProperty("user.home") + "/jclouds-virtualbox-test"; diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/AttachDistroMediumToMachineLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/AttachDistroMediumToMachineLiveTest.java new file mode 100644 index 0000000000..814b0cd268 --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/AttachDistroMediumToMachineLiveTest.java @@ -0,0 +1,80 @@ +/* + * 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.functions; + +import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; +import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExists; +import org.jclouds.virtualbox.util.MachineUtils; +import org.testng.annotations.Test; +import org.virtualbox_4_1.CleanupMode; +import org.virtualbox_4_1.IMedium; +import org.virtualbox_4_1.LockType; + +import java.io.File; + +import static org.jclouds.virtualbox.util.MachineUtils.lockMachineAndApply; +import static org.testng.Assert.assertTrue; + +/** + * @author Mattias Holmqvist + */ +public class AttachDistroMediumToMachineLiveTest extends BaseVirtualBoxClientLiveTest { + + @Test + public void testAttachMediumToMachine() throws Exception { + String nodeName = "test-attach-medium-node-1"; + String controllerName = "My Controller"; + new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(nodeName); + String path = System.getProperty("user.home") + "/jclouds-virtualbox-test/test-attach-medium-1.vdi"; + new CreateAndRegisterMachineFromIsoIfNotAlreadyExists("Debian", nodeName, true, manager) + .apply(nodeName); + lockMachineAndApply(manager, LockType.Write, nodeName, + new AddIDEControllerIfNotExists(controllerName)); + IMedium medium = new CreateMediumIfNotAlreadyExists(manager, "vdi", true).apply(path); + lockMachineAndApply(manager, LockType.Write, nodeName, + new AttachDistroMediumToMachine(controllerName, medium)); + new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(nodeName); + medium.close(); + assertFileCanBeDeleted(path); + } + + @Test + public void testAttachMediumToMachineTwice() throws Exception { + String nodeName = "test-attach-medium-node-2"; + String controllerName = "My Controller"; + new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(nodeName); + String path = System.getProperty("user.home") + "/jclouds-virtualbox-test/test-attach-medium-2.vdi"; + new CreateAndRegisterMachineFromIsoIfNotAlreadyExists("Debian", nodeName, true, manager) + .apply(nodeName); + lockMachineAndApply(manager, LockType.Write, nodeName, new AddIDEControllerIfNotExists(controllerName)); + IMedium medium = new CreateMediumIfNotAlreadyExists(manager, "vdi", true).apply(path); + lockMachineAndApply(manager, LockType.Write, nodeName, new AttachDistroMediumToMachine(controllerName, medium)); + lockMachineAndApply(manager, LockType.Write, nodeName, new AttachDistroMediumToMachine(controllerName, medium)); + new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(nodeName); + medium.close(); + assertFileCanBeDeleted(path); + } + + private void assertFileCanBeDeleted(String path) { + File file = new File(path); + boolean mediumDeleted = file.delete(); + assertTrue(mediumDeleted); + } +} 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 11d1e69bfb..6927b32c5d 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 @@ -85,7 +85,7 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba try { Predicate socketTester = new RetryablePredicate(new InetSocketAddressConnect(), 10, 1, TimeUnit.SECONDS); return new IsoToIMachine(manager, adminDisk, diskFormat, - settingsFile, vmName, osTypeId, vmId, forceOverwrite, + vmName, osTypeId, vmId, forceOverwrite, controllerIDE, localHostContext, hostId, guestId, socketTester, "127.0.0.1", 8080).apply(isoName); } catch (IllegalStateException e) { 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 3fe74fa286..29e0fa2333 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 @@ -38,7 +38,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends B @Test public void testCreateNewMachine() throws Exception { new UnregisterMachineIfExists(manager, CleanupMode.Full).apply("jclouds-test-create-1-node"); - IMachine debianNode = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(null, "Debian", "jclouds-test-create-1", true, manager) + IMachine debianNode = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists("Debian", "jclouds-test-create-1", true, manager) .apply("jclouds-test-create-1-node"); IMachine machine = manager.getVBox().findMachine("jclouds-test-create-1-node"); assertEquals(debianNode.getName(), machine.getName()); @@ -49,7 +49,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends B public void testCreateNewMachineWithBadOsType() throws Exception { new UnregisterMachineIfExists(manager, CleanupMode.Full).apply("jclouds-test-create-2-node"); try { - new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(null, "SomeWeirdUnknownOs", "jclouds-test-create-2", true, manager) + new CreateAndRegisterMachineFromIsoIfNotAlreadyExists("SomeWeirdUnknownOs", "jclouds-test-create-2", true, manager) .apply("jclouds-test-create-2-node"); fail(); } catch (VBoxException e) { 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 0cbd5d9126..8545038705 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 @@ -68,7 +68,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest { replay(manager, vBox); - new CreateAndRegisterMachineFromIsoIfNotAlreadyExists("", "", "", false, manager).apply(vmName); + new CreateAndRegisterMachineFromIsoIfNotAlreadyExists("", "", false, manager).apply(vmName); verify(manager, vBox); } @@ -87,7 +87,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest { replay(manager, vBox); - new CreateAndRegisterMachineFromIsoIfNotAlreadyExists("", "", "", false, manager).apply(vmName); + new CreateAndRegisterMachineFromIsoIfNotAlreadyExists("", "", false, manager).apply(vmName); } @Test(expectedExceptions = VBoxException.class) @@ -107,7 +107,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest { replay(manager, vBox); - new CreateAndRegisterMachineFromIsoIfNotAlreadyExists("", "", "", false, manager).apply(vmName); + new CreateAndRegisterMachineFromIsoIfNotAlreadyExists("", "", false, manager).apply(vmName); } diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IsoToIMachineLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IsoToIMachineLiveTest.java index 5eaddc9693..d0459ed21f 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IsoToIMachineLiveTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IsoToIMachineLiveTest.java @@ -20,7 +20,6 @@ package org.jclouds.virtualbox.functions; import static com.google.common.base.Predicates.equalTo; -import static com.google.common.base.Predicates.in; import static com.google.common.collect.Iterables.any; import static com.google.common.collect.Iterables.transform; import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest; @@ -36,7 +35,6 @@ import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.config.BaseComputeServiceContextModule; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.OsFamily; -import org.jclouds.compute.predicates.SocketOpenPredicates; import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.domain.Credentials; import org.jclouds.json.Json; @@ -44,7 +42,6 @@ import org.jclouds.json.config.GsonModule; import org.jclouds.net.IPSocket; import org.jclouds.predicates.InetSocketAddressConnect; import org.jclouds.predicates.RetryablePredicate; -import org.jclouds.predicates.SocketOpen; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExists; import org.testng.annotations.BeforeGroups; @@ -67,7 +64,6 @@ public class IsoToIMachineLiveTest extends BaseVirtualBoxClientLiveTest { }.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule()) .getInstance(Json.class)); - private String settingsFile = null; private boolean forceOverwrite = true; private String vmId = "jclouds-image-iso-1"; private String osTypeId = ""; @@ -92,7 +88,7 @@ public class IsoToIMachineLiveTest extends BaseVirtualBoxClientLiveTest { ComputeServiceContext localHostContext = computeServiceForLocalhostAndGuest(hostId, "localhost", guestId, "localhost", new Credentials("toor", "password")); Predicate socketTester = new RetryablePredicate(new InetSocketAddressConnect(), 10, 1, TimeUnit.SECONDS); - IMachine imageMachine = new IsoToIMachine(manager, adminDisk, diskFormat, settingsFile, vmName, osTypeId, vmId, + IMachine imageMachine = new IsoToIMachine(manager, adminDisk, diskFormat, vmName, osTypeId, vmId, forceOverwrite, controllerIDE, localHostContext, hostId, guestId, socketTester, "127.0.0.1", 8080) .apply("ubuntu-11.04-server-i386.iso");