Renamed DetachDistroMediumFromMachine and inject port and device instead of hard-coding.

This commit is contained in:
Mattias Holmqvist 2011-11-27 13:12:23 +01:00
parent 40e603d7e0
commit d06394514f
3 changed files with 40 additions and 27 deletions

View File

@ -67,7 +67,7 @@ public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExists implements Fu
VirtualBoxManager manager, ComputeServiceContext context,
String settingsFile, String osTypeId, String vmId,
boolean forceOverwrite, String cloneName, String hostId,
String snashotName, String snapshotDesc, String controllerIDE) {
String snapshotName, String snapshotDesc, String controllerIDE) {
this.manager = manager;
this.context = context;
this.settingsFile = settingsFile;
@ -76,7 +76,7 @@ public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExists implements Fu
this.forceOverwrite = forceOverwrite;
this.cloneName = cloneName;
this.hostId = hostId;
this.snapshotName = snashotName;
this.snapshotName = snapshotName;
this.snapshotDesc = snapshotDesc;
this.controllerIDE = controllerIDE;
}
@ -123,20 +123,24 @@ public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExists implements Fu
// TODO this behavior can be improved
String bridgedInterface = activeBridgedInterfaces.get(0);
ensureBridgedNetworkingIsAppliedToMachine(cloneName, macAddress, bridgedInterface);
long adapterSlot = 0l;
ensureBridgedNetworkingIsAppliedToMachine(adapterSlot, cloneName, macAddress, bridgedInterface);
// detach iso
ensureMachineHasDistroMediumDetached(cloneName, controllerIDE);
// TODO: also hard-coded values here
int controllerPort = 0;
int device = 0;
ensureMachineHasDistroMediumDetached(cloneName, controllerIDE, controllerPort, device);
return clonedMachine;
}
private void ensureBridgedNetworkingIsAppliedToMachine(String vmName, String macAddress, String hostInterface) {
lockMachineAndApply(manager, Write, vmName, new AttachBridgedAdapterToMachine(0l, macAddress, hostInterface));
private void ensureBridgedNetworkingIsAppliedToMachine(long adapterSlot, String vmName, String macAddress, String hostInterface) {
lockMachineAndApply(manager, Write, vmName, new AttachBridgedAdapterToMachine(adapterSlot, macAddress, hostInterface));
}
private void ensureMachineHasDistroMediumDetached(String vmName, String controllerIDE) {
lockMachineAndApply(manager, Write, vmName, new DetachDistroMediumToMachine(checkNotNull(controllerIDE, "controllerIDE")));
private void ensureMachineHasDistroMediumDetached(String vmName, String controllerIDE, int controllerPort, int device) {
lockMachineAndApply(manager, Write, vmName, new DetachDistroMediumFromMachine(checkNotNull(controllerIDE, "controllerIDE"), controllerPort, device));
}
}

View File

@ -1,4 +1,4 @@
/**
/*
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@ -29,19 +29,21 @@ import com.google.common.base.Function;
/**
* @author Andrea Turli
*/
public class DetachDistroMediumToMachine implements Function<IMachine, Void> {
public class DetachDistroMediumFromMachine implements Function<IMachine, Void> {
private final String controller;
private int controllerPort;
private int device;
public DetachDistroMediumToMachine(String controller) {
public DetachDistroMediumFromMachine(String controller, int controllerPort, int device) {
this.controller = controller;
this.controllerPort = controllerPort;
this.device = device;
}
@Override
public Void apply(@Nullable IMachine machine) {
try {
int controllerPort = 0;
int device = 0;
machine.detachDevice(controller, controllerPort, device);
machine.saveSettings();
} catch (VBoxException e) {
@ -52,7 +54,7 @@ public class DetachDistroMediumToMachine implements Function<IMachine, Void> {
}
private boolean alreadyDetached(VBoxException e) {
return e.getMessage().indexOf("is already detached from port") != -1;
return e.getMessage().contains("is already detached from port");
}
}

View File

@ -31,8 +31,8 @@ import org.virtualbox_4_1.VBoxException;
/**
* @author Andrea Turli
*/
@Test(groups = "unit", testName = "DetachDistroMediumToMachineTest")
public class DetachDistroMediumToMachineTest {
@Test(groups = "unit", testName = "DetachDistroMediumFromMachineTest")
public class DetachDistroMediumFromMachineTest {
@Test
public void testDetachDistroMedium() throws Exception {
@ -40,12 +40,15 @@ public class DetachDistroMediumToMachineTest {
String controller = "IDE Controller";
IMachine machine = createMock(IMachine.class);
int controllerPort = 0;
int device = 1;
machine.saveSettings();
machine.detachDevice(controller, 0, 0);
machine.detachDevice(controller, controllerPort, device);
replay(machine);
new DetachDistroMediumToMachine(controller).apply(machine);
new DetachDistroMediumFromMachine(controller, controllerPort, device).apply(machine);
verify(machine);
@ -65,14 +68,16 @@ public class DetachDistroMediumToMachineTest {
errorBuilder.append("of this virtual machine (0x80BB000C)");
String isoAlreadyAttachedException = errorBuilder.toString();
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class),
isoAlreadyAttachedException);
machine.detachDevice(controller, 0, 0);
int controllerPort = 0;
int device = 1;
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), isoAlreadyAttachedException);
machine.detachDevice(controller, controllerPort, device);
expectLastCall().andThrow(isoAttachedException);
replay(machine);
new DetachDistroMediumToMachine(controller).apply(machine);
new DetachDistroMediumFromMachine(controller, controllerPort, device).apply(machine);
verify(machine);
@ -81,7 +86,7 @@ public class DetachDistroMediumToMachineTest {
@Test(expectedExceptions = VBoxException.class)
public void testFailOnOtherVBoxErrors() throws Exception {
String controller = "IDE Controller";
String controllerName = "IDE Controller";
IMachine machine = createNiceMock(IMachine.class);
@ -90,14 +95,16 @@ public class DetachDistroMediumToMachineTest {
errorBuilder.append("Some other VBox error");
String isoAlreadyAttachedException = errorBuilder.toString();
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class),
isoAlreadyAttachedException);
machine.detachDevice(controller, 0, 0);
int controllerPort = 0;
int device = 1;
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), isoAlreadyAttachedException);
machine.detachDevice(controllerName, controllerPort, device);
expectLastCall().andThrow(isoAttachedException);
replay(machine);
new DetachDistroMediumToMachine(controller).apply(machine);
new DetachDistroMediumFromMachine(controllerName, controllerPort, device).apply(machine);
}