From 9a460c57d05821d632c95b583bc73bdafb7840d5 Mon Sep 17 00:00:00 2001 From: andreaturli Date: Sun, 18 Dec 2011 19:24:08 +0000 Subject: [PATCH] issue 384: small fixes to UnregisterMachine to destroy all the media attached --- ...sterMachineIfExistsAndDeleteItsMedia.java} | 34 +++++++++------ .../functions/CreateAndInstallVmLiveTest.java | 43 +++++++++++-------- ...hineFromIsoIfNotAlreadyExistsLiveTest.java | 14 +++--- ...MachineIfExistsAndDeleteItsMediaTest.java} | 4 +- 4 files changed, 55 insertions(+), 40 deletions(-) rename sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/{UnregisterMachineIfExists.java => UnregisterMachineIfExistsAndDeleteItsMedia.java} (73%) rename sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/{UnregisterMachineIfExistsTest.java => UnregisterMachineIfExistsAndDeleteItsMediaTest.java} (92%) diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExists.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMedia.java similarity index 73% rename from sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExists.java rename to sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMedia.java index 1a73c71508..1a90296d15 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExists.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMedia.java @@ -19,7 +19,6 @@ package org.jclouds.virtualbox.functions.admin; - import java.util.List; import javax.annotation.Nullable; @@ -37,8 +36,9 @@ import org.virtualbox_4_1.VBoxException; import org.virtualbox_4_1.VirtualBoxManager; import com.google.common.base.Function; +import com.google.common.base.Throwables; -public class UnregisterMachineIfExists implements Function { +public class UnregisterMachineIfExistsAndDeleteItsMedia implements Function { @Resource @Named(ComputeServiceConstants.COMPUTE_LOGGER) @@ -47,7 +47,7 @@ public class UnregisterMachineIfExists implements Function { private VirtualBoxManager manager; private CleanupMode mode; - public UnregisterMachineIfExists(VirtualBoxManager manager, CleanupMode mode) { + public UnregisterMachineIfExistsAndDeleteItsMedia(VirtualBoxManager manager, CleanupMode mode) { this.manager = manager; this.mode = mode; } @@ -62,23 +62,31 @@ public class UnregisterMachineIfExists implements Function { } catch (VBoxException e) { ErrorCode errorCode = ErrorCode.valueOf(e); switch (errorCode) { - case VBOX_E_OBJECT_NOT_FOUND: - logger.debug("Machine %s does not exists, cannot unregister", vmName); - break; - default: - throw e; + case VBOX_E_OBJECT_NOT_FOUND: + logger.debug("Machine %s does not exists, cannot unregister", + vmName); + break; + default: + throw e; } } /** - * deletion of all files is currently disabled on Windows/x64 to -prevent a crash + * deletion of all files is currently disabled on Windows/x64 to prevent a + * crash */ try { - IProgress deletion = machine.delete(mediaToBeDeleted); - deletion.waitForCompletion(-1); + IProgress deletion = machine.delete(mediaToBeDeleted); + deletion.waitForCompletion(-1); } catch (Exception e) { - e.printStackTrace(); + logger.error(e, "Problem in deleting the media attached to %s", machine.getName()); + propagate(e); } return null; } + + protected T propagate(Exception e) { + Throwables.propagate(e); + assert false; + return null; + } } 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 4554116f65..eeab8c27d9 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,9 +19,19 @@ package org.jclouds.virtualbox.functions; -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.inject.Guice; +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.domain.ExecutionType.HEADLESS; +import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest; +import static org.testng.Assert.assertTrue; + +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import javax.annotation.Nullable; + import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.config.BaseComputeServiceContextModule; import org.jclouds.compute.domain.Image; @@ -34,24 +44,21 @@ import org.jclouds.net.IPSocket; import org.jclouds.predicates.InetSocketAddressConnect; import org.jclouds.predicates.RetryablePredicate; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; -import org.jclouds.virtualbox.domain.*; -import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExists; +import org.jclouds.virtualbox.domain.NatAdapter; +import org.jclouds.virtualbox.domain.StorageController; +import org.jclouds.virtualbox.domain.VmSpec; +import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExistsAndDeleteItsMedia; import org.jclouds.virtualbox.util.PropertyUtils; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; -import org.virtualbox_4_1.*; +import org.virtualbox_4_1.CleanupMode; +import org.virtualbox_4_1.IMachine; +import org.virtualbox_4_1.StorageBus; +import org.virtualbox_4_1.VirtualBoxManager; -import javax.annotation.Nullable; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.TimeUnit; - -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.domain.ExecutionType.HEADLESS; -import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest; -import static org.testng.Assert.assertTrue; +import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.inject.Guice; /** * @author Andrea Turli, Mattias Holmqvist @@ -75,7 +82,7 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest { public void setUp() throws Exception { identity = "toor"; credential = "password"; - new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName); + new UnregisterMachineIfExistsAndDeleteItsMedia(manager, CleanupMode.Full).apply(vmName); } public void testCreateImageMachineFromIso() 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 6b04272833..3306ff1764 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 @@ -19,18 +19,18 @@ package org.jclouds.virtualbox.functions; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.fail; + import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.jclouds.virtualbox.domain.ErrorCode; import org.jclouds.virtualbox.domain.VmSpec; -import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExists; +import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExistsAndDeleteItsMedia; import org.testng.annotations.Test; import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.VBoxException; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.fail; - /** * @author Mattias Holmqvist */ @@ -39,19 +39,19 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends B @Test public void testCreateNewMachine() throws Exception { String vmName = "jclouds-test-create-1-node"; - new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName); + new UnregisterMachineIfExistsAndDeleteItsMedia(manager, CleanupMode.Full).apply(vmName); VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName) .osTypeId("Debian").forceOverwrite(true).build(); IMachine debianNode = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification); IMachine machine = manager.getVBox().findMachine(vmName); assertEquals(debianNode.getName(), machine.getName()); - new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName); + new UnregisterMachineIfExistsAndDeleteItsMedia(manager, CleanupMode.Full).apply(vmName); } @Test public void testCreateNewMachineWithBadOsType() throws Exception { String vmName = "jclouds-test-create-2-node"; - new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName); + new UnregisterMachineIfExistsAndDeleteItsMedia(manager, CleanupMode.Full).apply(vmName); VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName) .osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build(); try { diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMediaTest.java similarity index 92% rename from sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsTest.java rename to sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMediaTest.java index e0c3628c4d..91b120928d 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/admin/UnregisterMachineIfExistsAndDeleteItsMediaTest.java @@ -30,7 +30,7 @@ import static org.easymock.classextension.EasyMock.expect; import static org.easymock.classextension.EasyMock.*; @Test(groups = "unit", testName = "UnregisterMachineIfExistsTest") -public class UnregisterMachineIfExistsTest { +public class UnregisterMachineIfExistsAndDeleteItsMediaTest { @Test public void testUnregisterExistingMachine() throws Exception { @@ -50,7 +50,7 @@ public class UnregisterMachineIfExistsTest { replay(manager, vBox, registeredMachine); - new UnregisterMachineIfExists(manager, mode).apply(vmName); + new UnregisterMachineIfExistsAndDeleteItsMedia(manager, mode).apply(vmName); } }