mirror of https://github.com/apache/jclouds.git
Merge pull request #329 from andreaturli/develop
issue 384: fixed Clone and CreateAndRegister tests
This commit is contained in:
commit
5d9d03ed7e
|
@ -82,7 +82,8 @@ public class CloneAndRegisterMachineFromIMachineIfNotAlreadyExists implements Fu
|
|||
}
|
||||
|
||||
private boolean machineNotFoundException(VBoxException e) {
|
||||
return e.getMessage().contains("VirtualBox error: Could not find a registered machine named ");
|
||||
return e.getMessage().contains("VirtualBox error: Could not find a registered machine named ") ||
|
||||
e.getMessage().contains("Could not find a registered machine with UUID {");
|
||||
}
|
||||
|
||||
private IMachine cloneMachine(VmSpec vmSpec, IMachine master) {
|
||||
|
|
|
@ -81,8 +81,10 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Functi
|
|||
public IMachine apply(@Nullable IMachineSpec launchSpecification) {
|
||||
final IVirtualBox vBox = manager.get().getVBox();
|
||||
String vmName = launchSpecification.getVmSpec().getVmName();
|
||||
String vmId = launchSpecification.getVmSpec().getVmId();
|
||||
|
||||
try {
|
||||
vBox.findMachine(vmName);
|
||||
vBox.findMachine(vmId);
|
||||
throw new IllegalStateException("Machine " + vmName + " is already registered.");
|
||||
} catch (VBoxException e) {
|
||||
if (machineNotFoundException(e))
|
||||
|
@ -93,7 +95,8 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExists implements Functi
|
|||
}
|
||||
|
||||
private boolean machineNotFoundException(VBoxException e) {
|
||||
return e.getMessage().contains("VirtualBox error: Could not find a registered machine named ");
|
||||
return e.getMessage().contains("VirtualBox error: Could not find a registered machine named ") ||
|
||||
e.getMessage().contains("Could not find a registered machine with UUID {");
|
||||
}
|
||||
|
||||
private IMachine createMachine(IVirtualBox vBox, IMachineSpec machineSpec) {
|
||||
|
|
|
@ -20,9 +20,12 @@
|
|||
package org.jclouds.virtualbox.functions;
|
||||
|
||||
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
|
||||
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_INSTALLATION_KEY_SEQUENCE;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import com.google.inject.Injector;
|
||||
|
||||
import org.jclouds.config.ValueOfConfigurationKeyOrNull;
|
||||
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
||||
import org.jclouds.virtualbox.domain.*;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
|
@ -33,20 +36,21 @@ import org.virtualbox_4_1.ISession;
|
|||
import org.virtualbox_4_1.StorageBus;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
/**
|
||||
* @author Andrea Turli
|
||||
*/
|
||||
@Test(groups = "live", singleThreaded = true, testName = "CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest")
|
||||
public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||
public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends
|
||||
BaseVirtualBoxClientLiveTest {
|
||||
|
||||
private static final boolean IS_LINKED_CLONE = true;
|
||||
|
||||
private VmSpec clonedVmSpec;
|
||||
private IMachineSpec sourceMachineSpec;
|
||||
|
||||
|
||||
private CleanupMode mode = CleanupMode.Full;
|
||||
|
||||
@Override
|
||||
|
@ -54,24 +58,44 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba
|
|||
public void setupClient() {
|
||||
super.setupClient();
|
||||
String sourceName = VIRTUALBOX_IMAGE_PREFIX
|
||||
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
||||
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass()
|
||||
.getSimpleName());
|
||||
String cloneName = VIRTUALBOX_IMAGE_PREFIX
|
||||
+ "Clone#" + CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName()
|
||||
);
|
||||
+ "Clone#"
|
||||
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass()
|
||||
.getSimpleName());
|
||||
|
||||
StorageController ideController = StorageController.builder().name("IDE Controller").bus(StorageBus.IDE)
|
||||
.attachISO(0, 0, operatingSystemIso).attachHardDisk(
|
||||
HardDisk.builder().diskpath(adminDisk).controllerPort(0).deviceSlot(1).autoDelete(true).build()).attachISO(1, 1,
|
||||
guestAdditionsIso).build();
|
||||
StorageController ideController = StorageController
|
||||
.builder()
|
||||
.name("IDE Controller")
|
||||
.bus(StorageBus.IDE)
|
||||
.attachISO(0, 0, operatingSystemIso)
|
||||
.attachHardDisk(
|
||||
HardDisk.builder().diskpath(adminDisk).controllerPort(0)
|
||||
.deviceSlot(1).autoDelete(true).build())
|
||||
.attachISO(1, 1, guestAdditionsIso).build();
|
||||
|
||||
VmSpec sourceVmSpec = VmSpec.builder().id(sourceName).name(sourceName)
|
||||
.osTypeId("").memoryMB(512).cleanUpMode(CleanupMode.Full)
|
||||
.controller(ideController).forceOverwrite(true).build();
|
||||
|
||||
Injector injector = context.utils().injector();
|
||||
Function<String, String> configProperties = injector
|
||||
.getInstance(ValueOfConfigurationKeyOrNull.class);
|
||||
IsoSpec isoSpec = IsoSpec
|
||||
.builder()
|
||||
.sourcePath(operatingSystemIso)
|
||||
.installationScript(
|
||||
configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE)
|
||||
.replace("HOSTNAME", sourceVmSpec.getVmName()))
|
||||
.preConfiguration(preconfigurationUri).build();
|
||||
|
||||
VmSpec sourceVmSpec = VmSpec.builder().id(sourceName).name(sourceName).osTypeId("").memoryMB(512).cleanUpMode(
|
||||
CleanupMode.Full).controller(ideController).forceOverwrite(true).build();
|
||||
IsoSpec isoSpec = IsoSpec.builder().build();
|
||||
NetworkSpec networkSpec = NetworkSpec.builder().build();
|
||||
sourceMachineSpec = IMachineSpec.builder().iso(isoSpec).vm(sourceVmSpec).network(networkSpec).build();
|
||||
sourceMachineSpec = IMachineSpec.builder().iso(isoSpec).vm(sourceVmSpec)
|
||||
.network(networkSpec).build();
|
||||
|
||||
clonedVmSpec = VmSpec.builder().id(cloneName).name(cloneName).memoryMB(512).cleanUpMode(mode)
|
||||
.forceOverwrite(true).build();
|
||||
clonedVmSpec = VmSpec.builder().id(cloneName).name(cloneName)
|
||||
.memoryMB(512).cleanUpMode(mode).forceOverwrite(true).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -81,15 +105,18 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba
|
|||
|
||||
if (source.getCurrentSnapshot() != null) {
|
||||
ISession session = manager.get().openMachineSession(source);
|
||||
session.getConsole().deleteSnapshot(source.getCurrentSnapshot().getId());
|
||||
session.getConsole().deleteSnapshot(
|
||||
source.getCurrentSnapshot().getId());
|
||||
session.unlockMachine();
|
||||
}
|
||||
|
||||
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, clonedVmSpec,
|
||||
IS_LINKED_CLONE).apply(source);
|
||||
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(
|
||||
manager, workingDir, clonedVmSpec, IS_LINKED_CLONE)
|
||||
.apply(source);
|
||||
assertEquals(clone.getName(), clonedVmSpec.getVmName());
|
||||
} finally {
|
||||
for (VmSpec spec : ImmutableSet.of(clonedVmSpec, sourceMachineSpec.getVmSpec()))
|
||||
for (VmSpec spec : ImmutableSet.of(clonedVmSpec,
|
||||
sourceMachineSpec.getVmSpec()))
|
||||
undoVm(spec);
|
||||
}
|
||||
|
||||
|
@ -98,11 +125,13 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba
|
|||
private IMachine getSourceNode() {
|
||||
try {
|
||||
Injector injector = context.utils().injector();
|
||||
return injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(
|
||||
return injector.getInstance(
|
||||
CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(
|
||||
sourceMachineSpec);
|
||||
} catch (IllegalStateException e) {
|
||||
// already created
|
||||
return manager.get().getVBox().findMachine(sourceMachineSpec.getVmSpec().getVmId());
|
||||
return manager.get().getVBox()
|
||||
.findMachine(sourceMachineSpec.getVmSpec().getVmId());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -57,8 +57,8 @@ import static org.testng.Assert.assertTrue;
|
|||
public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
|
||||
|
||||
Map<OsFamily, Map<String, String>> map = new BaseComputeServiceContextModule() {
|
||||
}.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule())
|
||||
.getInstance(Json.class));
|
||||
}.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice
|
||||
.createInjector(new GsonModule()).getInstance(Json.class));
|
||||
|
||||
private VmSpec vmSpecification;
|
||||
|
||||
|
@ -67,37 +67,47 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
|
|||
public void setupClient() {
|
||||
super.setupClient();
|
||||
String vmName = VIRTUALBOX_IMAGE_PREFIX
|
||||
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass().getSimpleName());
|
||||
+ CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, getClass()
|
||||
.getSimpleName());
|
||||
|
||||
HardDisk hardDisk = HardDisk.builder().diskpath(adminDisk).autoDelete(true)
|
||||
.controllerPort(0).deviceSlot(1).build();
|
||||
StorageController ideController = StorageController.builder().name("IDE Controller").bus(StorageBus.IDE)
|
||||
.attachISO(0, 0, operatingSystemIso)
|
||||
.attachHardDisk(hardDisk)
|
||||
HardDisk hardDisk = HardDisk.builder().diskpath(adminDisk)
|
||||
.autoDelete(true).controllerPort(0).deviceSlot(1).build();
|
||||
StorageController ideController = StorageController.builder()
|
||||
.name("IDE Controller").bus(StorageBus.IDE)
|
||||
.attachISO(0, 0, operatingSystemIso).attachHardDisk(hardDisk)
|
||||
.attachISO(1, 1, guestAdditionsIso).build();
|
||||
vmSpecification = VmSpec.builder().id("jclouds-image-iso-1").name(vmName).memoryMB(512).osTypeId("")
|
||||
.controller(ideController)
|
||||
.forceOverwrite(true)
|
||||
vmSpecification = VmSpec.builder().id(vmName).name(vmName).memoryMB(512)
|
||||
.osTypeId("").controller(ideController).forceOverwrite(true)
|
||||
.cleanUpMode(CleanupMode.Full).build();
|
||||
undoVm(vmSpecification);
|
||||
}
|
||||
|
||||
public void testCreateImageMachineFromIso() throws Exception {
|
||||
Injector injector = context.utils().injector();
|
||||
Function<String, String> configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class);
|
||||
Function<String, String> configProperties = injector
|
||||
.getInstance(ValueOfConfigurationKeyOrNull.class);
|
||||
|
||||
IMachineSpec machineSpec = IMachineSpec.builder().vm(vmSpecification)
|
||||
.iso(IsoSpec.builder()
|
||||
IMachineSpec machineSpec = IMachineSpec
|
||||
.builder()
|
||||
.vm(vmSpecification)
|
||||
.iso(IsoSpec
|
||||
.builder()
|
||||
.sourcePath(operatingSystemIso)
|
||||
.installationScript(configProperties
|
||||
.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE)
|
||||
.replace("HOSTNAME", vmSpecification.getVmName()))
|
||||
.preConfiguration(preconfigurationUri)
|
||||
.build())
|
||||
.network(NetworkSpec.builder()
|
||||
.natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build())
|
||||
.build()).build();
|
||||
IMachine imageMachine = injector.getInstance(CreateAndInstallVm.class).apply(machineSpec);
|
||||
.installationScript(
|
||||
configProperties.apply(
|
||||
VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace(
|
||||
"HOSTNAME", vmSpecification.getVmName()))
|
||||
.preConfiguration(preconfigurationUri).build())
|
||||
.network(
|
||||
NetworkSpec
|
||||
.builder()
|
||||
.natNetworkAdapter(
|
||||
0,
|
||||
NatAdapter.builder()
|
||||
.tcpRedirectRule("127.0.0.1", 2222, "", 22)
|
||||
.build()).build()).build();
|
||||
IMachine imageMachine = injector.getInstance(CreateAndInstallVm.class)
|
||||
.apply(machineSpec);
|
||||
|
||||
IMachineToImage iMachineToImage = new IMachineToImage(manager, map);
|
||||
Image newImage = iMachineToImage.apply(imageMachine);
|
||||
|
|
|
@ -21,6 +21,8 @@ package org.jclouds.virtualbox.functions;
|
|||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.fail;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import com.google.inject.Injector;
|
||||
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
|
||||
import org.jclouds.virtualbox.domain.*;
|
||||
|
@ -54,8 +56,10 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends B
|
|||
@Test
|
||||
public void testCreateNewMachine() throws Exception {
|
||||
String vmName = "jclouds-test-create-1-node";
|
||||
String vmId = UUID.randomUUID().toString();
|
||||
|
||||
VmSpec vmSpec = VmSpec.builder()
|
||||
.id(vmName)
|
||||
.id(vmId)
|
||||
.name(vmName)
|
||||
.memoryMB(512)
|
||||
.controller(ideController)
|
||||
|
@ -84,7 +88,11 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends B
|
|||
@Test
|
||||
public void testCreateNewMachineWithBadOsType() throws Exception {
|
||||
String vmName = "jclouds-test-create-2-node";
|
||||
VmSpec vmSpec = VmSpec.builder().id(vmName).name(vmName).memoryMB(512).controller(ideController)
|
||||
String vmId = UUID.randomUUID().toString();
|
||||
|
||||
VmSpec vmSpec = VmSpec.builder()
|
||||
.id(vmId)
|
||||
.name(vmName).memoryMB(512).controller(ideController)
|
||||
.cleanUpMode(mode).osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build();
|
||||
IsoSpec isoSpec = IsoSpec.builder()
|
||||
.sourcePath(operatingSystemIso)
|
||||
|
|
|
@ -30,9 +30,11 @@ import static org.easymock.classextension.EasyMock.verify;
|
|||
import java.net.URI;
|
||||
|
||||
import org.easymock.EasyMock;
|
||||
import org.jclouds.virtualbox.domain.HardDisk;
|
||||
import org.jclouds.virtualbox.domain.IMachineSpec;
|
||||
import org.jclouds.virtualbox.domain.IsoSpec;
|
||||
import org.jclouds.virtualbox.domain.NetworkSpec;
|
||||
import org.jclouds.virtualbox.domain.StorageController;
|
||||
import org.jclouds.virtualbox.domain.VmSpec;
|
||||
import org.jclouds.virtualbox.util.MachineUtils;
|
||||
import org.testng.annotations.Test;
|
||||
|
@ -41,6 +43,7 @@ import org.virtualbox_4_1.IMachine;
|
|||
import org.virtualbox_4_1.ISession;
|
||||
import org.virtualbox_4_1.IVirtualBox;
|
||||
import org.virtualbox_4_1.LockType;
|
||||
import org.virtualbox_4_1.StorageBus;
|
||||
import org.virtualbox_4_1.VBoxException;
|
||||
import org.virtualbox_4_1.VirtualBoxManager;
|
||||
|
||||
|
@ -61,7 +64,8 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
|
|||
IVirtualBox vBox = createMock(IVirtualBox.class);
|
||||
Supplier<URI> preconfiguration = createNiceMock(Supplier.class);
|
||||
String vmName = "jclouds-image-my-ubuntu-image";
|
||||
VmSpec vmSpec = VmSpec.builder().id(vmName).name(vmName).osTypeId("").memoryMB(1024).cleanUpMode(
|
||||
StorageController ideController = StorageController.builder().name("IDE Controller").bus(StorageBus.IDE).build();
|
||||
VmSpec vmSpec = VmSpec.builder().id(vmName).name(vmName).osTypeId("").memoryMB(1024).controller(ideController).cleanUpMode(
|
||||
CleanupMode.Full).build();
|
||||
IMachineSpec machineSpec = IMachineSpec.builder()
|
||||
.iso(IsoSpec.builder().sourcePath("some.iso").installationScript("").preConfiguration(preconfiguration).build())
|
||||
|
@ -74,8 +78,8 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
|
|||
expect(vBox.composeMachineFilename(vmName, "/tmp/workingDir")).andReturn("settingsFile");
|
||||
|
||||
StringBuilder errorMessageBuilder = new StringBuilder();
|
||||
errorMessageBuilder.append("VirtualBox error: Could not find a registered machine named ");
|
||||
errorMessageBuilder.append("'jclouds-image-virtualbox-iso-to-machine-test' (0x80BB0001)");
|
||||
errorMessageBuilder.append("VirtualBox error: Could not find a registered machine with UUID {");
|
||||
errorMessageBuilder.append("'jclouds-image-virtualbox-iso-to-machine-test'} (0x80BB0001)");
|
||||
String errorMessage = errorMessageBuilder.toString();
|
||||
VBoxException vBoxException = new VBoxException(createNiceMock(Throwable.class), errorMessage);
|
||||
|
||||
|
@ -102,7 +106,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
|
|||
verify(manager, createdMachine, vBox, session);
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = IllegalStateException.class, enabled=false)
|
||||
@Test(expectedExceptions = IllegalStateException.class)
|
||||
public void testFailIfMachineIsAlreadyRegistered() throws Exception {
|
||||
|
||||
MachineUtils machineUtils = createMock(MachineUtils.class);
|
||||
|
@ -119,7 +123,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
|
|||
|
||||
replay(manager, vBox, machineUtils);
|
||||
|
||||
VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").memoryMB(1024).cleanUpMode(
|
||||
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName).osTypeId("").memoryMB(1024).cleanUpMode(
|
||||
CleanupMode.Full).build();
|
||||
|
||||
IMachineSpec machineSpec = IMachineSpec.builder()
|
||||
|
@ -152,7 +156,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
|
|||
|
||||
replay(manager, vBox, machineUtils);
|
||||
|
||||
VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").cleanUpMode(CleanupMode.Full)
|
||||
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName).osTypeId("").cleanUpMode(CleanupMode.Full)
|
||||
.memoryMB(1024).build();
|
||||
IMachineSpec machineSpec = IMachineSpec.builder()
|
||||
.iso(IsoSpec.builder()
|
||||
|
|
|
@ -69,7 +69,7 @@ public class IsLinkedClonesLiveTest extends BaseVirtualBoxClientLiveTest {
|
|||
.build();
|
||||
masterStorageController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE).attachISO(0, 0,
|
||||
operatingSystemIso).attachHardDisk(hardDisk).attachISO(1, 1, guestAdditionsIso).build();
|
||||
VmSpec masterSpec = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).osTypeId(osTypeId).controller(
|
||||
VmSpec masterSpec = VmSpec.builder().id(vmName).name(vmName).memoryMB(512).osTypeId(osTypeId).controller(
|
||||
masterStorageController).forceOverwrite(true).cleanUpMode(CleanupMode.Full).build();
|
||||
masterMachineSpec = IMachineSpec.builder()
|
||||
.iso(IsoSpec.builder()
|
||||
|
|
Loading…
Reference in New Issue