issue 384: fixed Clone and CreateAndRegister - aledsage comments addressed

This commit is contained in:
andreaturli 2012-01-22 22:30:47 +00:00
parent 6739644809
commit f3386f40a7
2 changed files with 83 additions and 59 deletions

View File

@ -43,14 +43,14 @@ import com.google.common.collect.ImmutableSet;
* @author Andrea Turli * @author Andrea Turli
*/ */
@Test(groups = "live", singleThreaded = true, testName = "CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest") @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 static final boolean IS_LINKED_CLONE = true;
private VmSpec clonedVmSpec; private VmSpec clonedVmSpec;
private IMachineSpec sourceMachineSpec; private IMachineSpec sourceMachineSpec;
private CleanupMode mode = CleanupMode.Full; private CleanupMode mode = CleanupMode.Full;
@Override @Override
@ -58,35 +58,44 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba
public void setupClient() { public void setupClient() {
super.setupClient(); super.setupClient();
String sourceName = VIRTUALBOX_IMAGE_PREFIX 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 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) StorageController ideController = StorageController
.attachISO(0, 0, operatingSystemIso).attachHardDisk( .builder()
HardDisk.builder().diskpath(adminDisk).controllerPort(0).deviceSlot(1).autoDelete(true).build()).attachISO(1, 1, .name("IDE Controller")
guestAdditionsIso).build(); .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();
VmSpec sourceVmSpec = VmSpec.builder().id(sourceName).name(sourceName).osTypeId("").memoryMB(512).cleanUpMode(
CleanupMode.Full).controller(ideController).forceOverwrite(true).build();
Injector injector = context.utils().injector(); Injector injector = context.utils().injector();
Function<String, String> configProperties = injector.getInstance(ValueOfConfigurationKeyOrNull.class); Function<String, String> configProperties = injector
IsoSpec isoSpec = IsoSpec.builder() .getInstance(ValueOfConfigurationKeyOrNull.class);
.sourcePath(operatingSystemIso) IsoSpec isoSpec = IsoSpec
.installationScript(configProperties .builder()
.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE) .sourcePath(operatingSystemIso)
.replace("HOSTNAME", sourceVmSpec.getVmName())) .installationScript(
.preConfiguration(preconfigurationUri) configProperties.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE)
.build(); .replace("HOSTNAME", sourceVmSpec.getVmName()))
//IsoSpec isoSpec = IsoSpec.builder().build(); .preConfiguration(preconfigurationUri).build();
NetworkSpec networkSpec = NetworkSpec.builder().build();
sourceMachineSpec = IMachineSpec.builder().iso(isoSpec).vm(sourceVmSpec).network(networkSpec).build();
clonedVmSpec = VmSpec.builder().id(cloneName).name(cloneName).memoryMB(512).cleanUpMode(mode) NetworkSpec networkSpec = NetworkSpec.builder().build();
.forceOverwrite(true).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();
} }
@Test @Test
@ -96,15 +105,18 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba
if (source.getCurrentSnapshot() != null) { if (source.getCurrentSnapshot() != null) {
ISession session = manager.get().openMachineSession(source); ISession session = manager.get().openMachineSession(source);
session.getConsole().deleteSnapshot(source.getCurrentSnapshot().getId()); session.getConsole().deleteSnapshot(
source.getCurrentSnapshot().getId());
session.unlockMachine(); session.unlockMachine();
} }
IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(manager, workingDir, clonedVmSpec, IMachine clone = new CloneAndRegisterMachineFromIMachineIfNotAlreadyExists(
IS_LINKED_CLONE).apply(source); manager, workingDir, clonedVmSpec, IS_LINKED_CLONE)
.apply(source);
assertEquals(clone.getName(), clonedVmSpec.getVmName()); assertEquals(clone.getName(), clonedVmSpec.getVmName());
} finally { } finally {
for (VmSpec spec : ImmutableSet.of(clonedVmSpec, sourceMachineSpec.getVmSpec())) for (VmSpec spec : ImmutableSet.of(clonedVmSpec,
sourceMachineSpec.getVmSpec()))
undoVm(spec); undoVm(spec);
} }
@ -113,11 +125,13 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba
private IMachine getSourceNode() { private IMachine getSourceNode() {
try { try {
Injector injector = context.utils().injector(); Injector injector = context.utils().injector();
return injector.getInstance(CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply( return injector.getInstance(
sourceMachineSpec); CreateAndRegisterMachineFromIsoIfNotAlreadyExists.class).apply(
sourceMachineSpec);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// already created // already created
return manager.get().getVBox().findMachine(sourceMachineSpec.getVmSpec().getVmId()); return manager.get().getVBox()
.findMachine(sourceMachineSpec.getVmSpec().getVmId());
} }
} }
} }

View File

@ -57,8 +57,8 @@ import static org.testng.Assert.assertTrue;
public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest { public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
Map<OsFamily, Map<String, String>> map = new BaseComputeServiceContextModule() { Map<OsFamily, Map<String, String>> map = new BaseComputeServiceContextModule() {
}.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule()) }.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice
.getInstance(Json.class)); .createInjector(new GsonModule()).getInstance(Json.class));
private VmSpec vmSpecification; private VmSpec vmSpecification;
@ -67,37 +67,47 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
public void setupClient() { public void setupClient() {
super.setupClient(); super.setupClient();
String vmName = VIRTUALBOX_IMAGE_PREFIX 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) HardDisk hardDisk = HardDisk.builder().diskpath(adminDisk)
.controllerPort(0).deviceSlot(1).build(); .autoDelete(true).controllerPort(0).deviceSlot(1).build();
StorageController ideController = StorageController.builder().name("IDE Controller").bus(StorageBus.IDE) StorageController ideController = StorageController.builder()
.attachISO(0, 0, operatingSystemIso) .name("IDE Controller").bus(StorageBus.IDE)
.attachHardDisk(hardDisk) .attachISO(0, 0, operatingSystemIso).attachHardDisk(hardDisk)
.attachISO(1, 1, guestAdditionsIso).build(); .attachISO(1, 1, guestAdditionsIso).build();
vmSpecification = VmSpec.builder().id(vmName).name(vmName).memoryMB(512).osTypeId("") vmSpecification = VmSpec.builder().id(vmName).name(vmName).memoryMB(512)
.controller(ideController) .osTypeId("").controller(ideController).forceOverwrite(true)
.forceOverwrite(true) .cleanUpMode(CleanupMode.Full).build();
.cleanUpMode(CleanupMode.Full).build();
undoVm(vmSpecification); undoVm(vmSpecification);
} }
public void testCreateImageMachineFromIso() throws Exception { public void testCreateImageMachineFromIso() throws Exception {
Injector injector = context.utils().injector(); 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) IMachineSpec machineSpec = IMachineSpec
.iso(IsoSpec.builder() .builder()
.sourcePath(operatingSystemIso) .vm(vmSpecification)
.installationScript(configProperties .iso(IsoSpec
.apply(VIRTUALBOX_INSTALLATION_KEY_SEQUENCE) .builder()
.replace("HOSTNAME", vmSpecification.getVmName())) .sourcePath(operatingSystemIso)
.preConfiguration(preconfigurationUri) .installationScript(
.build()) configProperties.apply(
.network(NetworkSpec.builder() VIRTUALBOX_INSTALLATION_KEY_SEQUENCE).replace(
.natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build()) "HOSTNAME", vmSpecification.getVmName()))
.build()).build(); .preConfiguration(preconfigurationUri).build())
IMachine imageMachine = injector.getInstance(CreateAndInstallVm.class).apply(machineSpec); .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); IMachineToImage iMachineToImage = new IMachineToImage(manager, map);
Image newImage = iMachineToImage.apply(imageMachine); Image newImage = iMachineToImage.apply(imageMachine);