TemplateClientLiveTest was not properly cleaning up the VMs and templates it created

This commit is contained in:
Richard Downer 2011-12-05 09:47:16 +02:00
parent da56c047a1
commit 1d60c0d819
1 changed files with 21 additions and 15 deletions

View File

@ -45,7 +45,8 @@ import static org.testng.Assert.*;
public class TemplateClientLiveTest extends BaseCloudStackClientLiveTest { public class TemplateClientLiveTest extends BaseCloudStackClientLiveTest {
private static final String IMPORT_VHD_URL = "http://www.frontiertown.co.uk/jclouds/empty.vhd"; private static final String IMPORT_VHD_URL = "http://www.frontiertown.co.uk/jclouds/empty.vhd";
private VirtualMachine vm; private VirtualMachine vmForCreation;
private VirtualMachine vmForRegistration;
private Template createdTemplate; private Template createdTemplate;
private Template registeredTemplate; private Template registeredTemplate;
@ -98,20 +99,20 @@ public class TemplateClientLiveTest extends BaseCloudStackClientLiveTest {
// Create a VM and stop it // Create a VM and stop it
Long templateId = (imageId != null && !"".equals(imageId)) ? new Long(imageId) : null; Long templateId = (imageId != null && !"".equals(imageId)) ? new Long(imageId) : null;
vm = VirtualMachineClientLiveTest.createVirtualMachineInNetwork(network, templateId, client, jobComplete, virtualMachineRunning); vmForCreation = VirtualMachineClientLiveTest.createVirtualMachineInNetwork(network, templateId, client, jobComplete, virtualMachineRunning);
assert jobComplete.apply(client.getVirtualMachineClient().stopVirtualMachine(vm.getId())) : vm; assert jobComplete.apply(client.getVirtualMachineClient().stopVirtualMachine(vmForCreation.getId())) : vmForCreation;
// Work out the VM's volume // Work out the VM's volume
Set<Volume> volumes = client.getVolumeClient().listVolumes(ListVolumesOptions.Builder.virtualMachineId(vm.getId())); Set<Volume> volumes = client.getVolumeClient().listVolumes(ListVolumesOptions.Builder.virtualMachineId(vmForCreation.getId()));
assertEquals(volumes.size(), 1); assertEquals(volumes.size(), 1);
Volume volume = Iterables.getOnlyElement(volumes); Volume volume = Iterables.getOnlyElement(volumes);
// Create a template // Create a template
String tmplName = "jclouds-" + Integer.toHexString(new Random().nextInt()); String tmplName = "jclouds-" + Integer.toHexString(new Random().nextInt());
CreateTemplateOptions options = CreateTemplateOptions.Builder.volumeId(volume.getId()); CreateTemplateOptions options = CreateTemplateOptions.Builder.volumeId(volume.getId());
AsyncCreateResponse response = client.getTemplateClient().createTemplate(TemplateMetadata.builder().name(tmplName).osTypeId(vm.getGuestOSId()).displayText("jclouds live testCreateTemplate").build(), options); AsyncCreateResponse response = client.getTemplateClient().createTemplate(TemplateMetadata.builder().name(tmplName).osTypeId(vmForCreation.getGuestOSId()).displayText("jclouds live testCreateTemplate").build(), options);
assert jobComplete.apply(response.getJobId()) : vm; assert jobComplete.apply(response.getJobId()) : vmForCreation;
createdTemplate = client.getTemplateClient().getTemplateInZone(response.getId(), vm.getZoneId()); createdTemplate = client.getTemplateClient().getTemplateInZone(response.getId(), vmForCreation.getZoneId());
// Assertions // Assertions
assertNotNull(createdTemplate); assertNotNull(createdTemplate);
@ -174,25 +175,30 @@ public class TemplateClientLiveTest extends BaseCloudStackClientLiveTest {
assertTrue(new RetryablePredicate<Template>(templateReadyPredicate, 60000).apply(registeredTemplate)); assertTrue(new RetryablePredicate<Template>(templateReadyPredicate, 60000).apply(registeredTemplate));
// Create a VM that uses this template // Create a VM that uses this template
vm = VirtualMachineClientLiveTest.createVirtualMachineInNetwork(network, registeredTemplate.getId(), client, jobComplete, virtualMachineRunning); vmForRegistration = VirtualMachineClientLiveTest.createVirtualMachineInNetwork(network, registeredTemplate.getId(), client, jobComplete, virtualMachineRunning);
assertNotNull(vm); assertNotNull(vmForRegistration);
} }
@AfterGroups(groups = "live") @AfterGroups(groups = "live")
protected void tearDown() { protected void tearDown() {
if (vm != null) { if (vmForCreation != null) {
assert jobComplete.apply(client.getVirtualMachineClient().stopVirtualMachine(vm.getId())) : vm; assert jobComplete.apply(client.getVirtualMachineClient().stopVirtualMachine(vmForCreation.getId())) : vmForCreation;
assert jobComplete.apply(client.getVirtualMachineClient().destroyVirtualMachine(vm.getId())) : vm; assert jobComplete.apply(client.getVirtualMachineClient().destroyVirtualMachine(vmForCreation.getId())) : vmForCreation;
assert virtualMachineDestroyed.apply(vm); assert virtualMachineDestroyed.apply(vmForCreation);
}
if (vmForRegistration != null) {
assert jobComplete.apply(client.getVirtualMachineClient().stopVirtualMachine(vmForRegistration.getId())) : vmForRegistration;
assert jobComplete.apply(client.getVirtualMachineClient().destroyVirtualMachine(vmForRegistration.getId())) : vmForRegistration;
assert virtualMachineDestroyed.apply(vmForRegistration);
} }
if (createdTemplate != null) { if (createdTemplate != null) {
AsyncCreateResponse deleteJob = client.getTemplateClient().deleteTemplate(createdTemplate.getId()); AsyncCreateResponse deleteJob = client.getTemplateClient().deleteTemplate(createdTemplate.getId());
assertTrue(jobComplete.apply(deleteJob.getJobId())); assertTrue(jobComplete.apply(deleteJob.getJobId()));
client.getTemplateClient().deleteTemplate(createdTemplate.getId());
} }
if (registeredTemplate != null) { if (registeredTemplate != null) {
client.getTemplateClient().deleteTemplate(registeredTemplate.getId()); AsyncCreateResponse deleteJob = client.getTemplateClient().deleteTemplate(registeredTemplate.getId());
assertTrue(jobComplete.apply(deleteJob.getJobId()));
} }
super.tearDown(); super.tearDown();
} }