issue 384: small fixes to UnregisterMachine to destroy all the media attached

This commit is contained in:
andreaturli 2011-12-18 19:24:08 +00:00
parent cbe1f4d161
commit 9a460c57d0
4 changed files with 55 additions and 40 deletions

View File

@ -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<String, Void> {
public class UnregisterMachineIfExistsAndDeleteItsMedia implements Function<String, Void> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
@ -47,7 +47,7 @@ public class UnregisterMachineIfExists implements Function<String, Void> {
private VirtualBoxManager manager;
private CleanupMode mode;
public UnregisterMachineIfExists(VirtualBoxManager manager, CleanupMode mode) {
public UnregisterMachineIfExistsAndDeleteItsMedia(VirtualBoxManager manager, CleanupMode mode) {
this.manager = manager;
this.mode = mode;
}
@ -63,22 +63,30 @@ public class UnregisterMachineIfExists implements Function<String, Void> {
ErrorCode errorCode = ErrorCode.valueOf(e);
switch (errorCode) {
case VBOX_E_OBJECT_NOT_FOUND:
logger.debug("Machine %s does not exists, cannot unregister", vmName);
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);
} catch (Exception e) {
e.printStackTrace();
logger.error(e, "Problem in deleting the media attached to %s", machine.getName());
propagate(e);
}
return null;
}
protected <T> T propagate(Exception e) {
Throwables.propagate(e);
assert false;
return null;
}
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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);
}
}