mirror of https://github.com/apache/jclouds.git
issue 384: small fixes to UnregisterMachine to destroy all the media attached
This commit is contained in:
parent
cbe1f4d161
commit
9a460c57d0
|
@ -19,7 +19,6 @@
|
||||||
|
|
||||||
package org.jclouds.virtualbox.functions.admin;
|
package org.jclouds.virtualbox.functions.admin;
|
||||||
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
@ -37,8 +36,9 @@ import org.virtualbox_4_1.VBoxException;
|
||||||
import org.virtualbox_4_1.VirtualBoxManager;
|
import org.virtualbox_4_1.VirtualBoxManager;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
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
|
@Resource
|
||||||
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
|
||||||
|
@ -47,7 +47,7 @@ public class UnregisterMachineIfExists implements Function<String, Void> {
|
||||||
private VirtualBoxManager manager;
|
private VirtualBoxManager manager;
|
||||||
private CleanupMode mode;
|
private CleanupMode mode;
|
||||||
|
|
||||||
public UnregisterMachineIfExists(VirtualBoxManager manager, CleanupMode mode) {
|
public UnregisterMachineIfExistsAndDeleteItsMedia(VirtualBoxManager manager, CleanupMode mode) {
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
this.mode = mode;
|
this.mode = mode;
|
||||||
}
|
}
|
||||||
|
@ -63,22 +63,30 @@ public class UnregisterMachineIfExists implements Function<String, Void> {
|
||||||
ErrorCode errorCode = ErrorCode.valueOf(e);
|
ErrorCode errorCode = ErrorCode.valueOf(e);
|
||||||
switch (errorCode) {
|
switch (errorCode) {
|
||||||
case VBOX_E_OBJECT_NOT_FOUND:
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* deletion of all files is currently disabled on Windows/x64 to
|
* deletion of all files is currently disabled on Windows/x64 to prevent a
|
||||||
prevent a crash
|
* crash
|
||||||
*/
|
*/
|
||||||
try {
|
try {
|
||||||
IProgress deletion = machine.delete(mediaToBeDeleted);
|
IProgress deletion = machine.delete(mediaToBeDeleted);
|
||||||
deletion.waitForCompletion(-1);
|
deletion.waitForCompletion(-1);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
logger.error(e, "Problem in deleting the media attached to %s", machine.getName());
|
||||||
|
propagate(e);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected <T> T propagate(Exception e) {
|
||||||
|
Throwables.propagate(e);
|
||||||
|
assert false;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -19,9 +19,19 @@
|
||||||
|
|
||||||
package org.jclouds.virtualbox.functions;
|
package org.jclouds.virtualbox.functions;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import static com.google.common.base.Predicates.equalTo;
|
||||||
import com.google.common.base.Predicate;
|
import static com.google.common.collect.Iterables.any;
|
||||||
import com.google.inject.Guice;
|
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.ComputeServiceContext;
|
||||||
import org.jclouds.compute.config.BaseComputeServiceContextModule;
|
import org.jclouds.compute.config.BaseComputeServiceContextModule;
|
||||||
import org.jclouds.compute.domain.Image;
|
import org.jclouds.compute.domain.Image;
|
||||||
|
@ -34,24 +44,21 @@ import org.jclouds.net.IPSocket;
|
||||||
import org.jclouds.predicates.InetSocketAddressConnect;
|
import org.jclouds.predicates.InetSocketAddressConnect;
|
||||||
import org.jclouds.predicates.RetryablePredicate;
|
import org.jclouds.predicates.RetryablePredicate;
|
||||||
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
||||||
import org.jclouds.virtualbox.domain.*;
|
import org.jclouds.virtualbox.domain.NatAdapter;
|
||||||
import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExists;
|
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.jclouds.virtualbox.util.PropertyUtils;
|
||||||
import org.testng.annotations.BeforeGroups;
|
import org.testng.annotations.BeforeGroups;
|
||||||
import org.testng.annotations.Test;
|
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 com.google.common.base.Function;
|
||||||
import java.util.Map;
|
import com.google.common.base.Predicate;
|
||||||
import java.util.Set;
|
import com.google.inject.Guice;
|
||||||
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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Andrea Turli, Mattias Holmqvist
|
* @author Andrea Turli, Mattias Holmqvist
|
||||||
|
@ -75,7 +82,7 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
identity = "toor";
|
identity = "toor";
|
||||||
credential = "password";
|
credential = "password";
|
||||||
new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName);
|
new UnregisterMachineIfExistsAndDeleteItsMedia(manager, CleanupMode.Full).apply(vmName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testCreateImageMachineFromIso() throws Exception {
|
public void testCreateImageMachineFromIso() throws Exception {
|
||||||
|
|
|
@ -19,18 +19,18 @@
|
||||||
|
|
||||||
package org.jclouds.virtualbox.functions;
|
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.BaseVirtualBoxClientLiveTest;
|
||||||
import org.jclouds.virtualbox.domain.ErrorCode;
|
import org.jclouds.virtualbox.domain.ErrorCode;
|
||||||
import org.jclouds.virtualbox.domain.VmSpec;
|
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.testng.annotations.Test;
|
||||||
import org.virtualbox_4_1.CleanupMode;
|
import org.virtualbox_4_1.CleanupMode;
|
||||||
import org.virtualbox_4_1.IMachine;
|
import org.virtualbox_4_1.IMachine;
|
||||||
import org.virtualbox_4_1.VBoxException;
|
import org.virtualbox_4_1.VBoxException;
|
||||||
|
|
||||||
import static org.testng.Assert.assertEquals;
|
|
||||||
import static org.testng.Assert.fail;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Mattias Holmqvist
|
* @author Mattias Holmqvist
|
||||||
*/
|
*/
|
||||||
|
@ -39,19 +39,19 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends B
|
||||||
@Test
|
@Test
|
||||||
public void testCreateNewMachine() throws Exception {
|
public void testCreateNewMachine() throws Exception {
|
||||||
String vmName = "jclouds-test-create-1-node";
|
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)
|
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName)
|
||||||
.osTypeId("Debian").forceOverwrite(true).build();
|
.osTypeId("Debian").forceOverwrite(true).build();
|
||||||
IMachine debianNode = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification);
|
IMachine debianNode = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification);
|
||||||
IMachine machine = manager.getVBox().findMachine(vmName);
|
IMachine machine = manager.getVBox().findMachine(vmName);
|
||||||
assertEquals(debianNode.getName(), machine.getName());
|
assertEquals(debianNode.getName(), machine.getName());
|
||||||
new UnregisterMachineIfExists(manager, CleanupMode.Full).apply(vmName);
|
new UnregisterMachineIfExistsAndDeleteItsMedia(manager, CleanupMode.Full).apply(vmName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreateNewMachineWithBadOsType() throws Exception {
|
public void testCreateNewMachineWithBadOsType() throws Exception {
|
||||||
String vmName = "jclouds-test-create-2-node";
|
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)
|
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName)
|
||||||
.osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build();
|
.osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build();
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -30,7 +30,7 @@ import static org.easymock.classextension.EasyMock.expect;
|
||||||
import static org.easymock.classextension.EasyMock.*;
|
import static org.easymock.classextension.EasyMock.*;
|
||||||
|
|
||||||
@Test(groups = "unit", testName = "UnregisterMachineIfExistsTest")
|
@Test(groups = "unit", testName = "UnregisterMachineIfExistsTest")
|
||||||
public class UnregisterMachineIfExistsTest {
|
public class UnregisterMachineIfExistsAndDeleteItsMediaTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUnregisterExistingMachine() throws Exception {
|
public void testUnregisterExistingMachine() throws Exception {
|
||||||
|
@ -50,7 +50,7 @@ public class UnregisterMachineIfExistsTest {
|
||||||
|
|
||||||
replay(manager, vBox, registeredMachine);
|
replay(manager, vBox, registeredMachine);
|
||||||
|
|
||||||
new UnregisterMachineIfExists(manager, mode).apply(vmName);
|
new UnregisterMachineIfExistsAndDeleteItsMedia(manager, mode).apply(vmName);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue