Merge pull request #158 from richardcloudsoft/cloudstack-template-live-test

Cloudstack template live tests
This commit is contained in:
Adrian Cole 2011-11-16 07:26:08 -08:00
commit 2f2277683c
1 changed files with 64 additions and 8 deletions

View File

@ -18,16 +18,21 @@
*/ */
package org.jclouds.cloudstack.features; package org.jclouds.cloudstack.features;
import static org.jclouds.cloudstack.options.ListTemplatesOptions.Builder.zoneId; import com.google.common.base.Predicate;
import static org.testng.Assert.assertEquals; import com.google.common.collect.Iterables;
import static org.testng.Assert.assertTrue; import org.jclouds.cloudstack.domain.*;
import org.jclouds.cloudstack.options.CreateTemplateOptions;
import java.util.Set; import org.jclouds.cloudstack.options.ListNetworksOptions;
import org.jclouds.cloudstack.options.ListVolumesOptions;
import org.jclouds.cloudstack.domain.Template; import org.testng.annotations.AfterGroups;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.collect.Iterables; import javax.annotation.Nullable;
import java.util.Random;
import java.util.Set;
import static org.jclouds.cloudstack.options.ListTemplatesOptions.Builder.zoneId;
import static org.testng.Assert.*;
/** /**
* Tests behavior of {@code TemplateClientLiveTest} * Tests behavior of {@code TemplateClientLiveTest}
@ -37,6 +42,9 @@ import com.google.common.collect.Iterables;
@Test(groups = "live", singleThreaded = true, testName = "TemplateClientLiveTest") @Test(groups = "live", singleThreaded = true, testName = "TemplateClientLiveTest")
public class TemplateClientLiveTest extends BaseCloudStackClientLiveTest { public class TemplateClientLiveTest extends BaseCloudStackClientLiveTest {
private VirtualMachine vm;
private Template template;
public void testListTemplates() throws Exception { public void testListTemplates() throws Exception {
Set<Template> response = client.getTemplateClient().listTemplates(); Set<Template> response = client.getTemplateClient().listTemplates();
assert null != response; assert null != response;
@ -65,4 +73,52 @@ public class TemplateClientLiveTest extends BaseCloudStackClientLiveTest {
} }
} }
public void testCreateTemplate() throws Exception {
Zone zone = Iterables.getFirst(client.getZoneClient().listZones(), null);
assertNotNull(zone);
Iterable<Network> networks = client.getNetworkClient().listNetworks(ListNetworksOptions.Builder.zoneId(zone.getId()).isDefault(true));
networks = Iterables.filter(networks, new Predicate<Network>() {
@Override
public boolean apply(@Nullable Network network) {
return network != null && network.getState().equals("Implemented");
}
});
assertEquals(Iterables.size(networks), 1);
Network network = Iterables.getOnlyElement(networks, null);
assertNotNull(network);
// Create a VM and stop it
Long templateId = (imageId != null && !"".equals(imageId)) ? new Long(imageId) : null;
vm = VirtualMachineClientLiveTest.createVirtualMachineInNetwork(network, templateId, client, jobComplete, virtualMachineRunning);
assert jobComplete.apply(client.getVirtualMachineClient().stopVirtualMachine(vm.getId())) : vm;
// Work out the VM's volume
Set<Volume> volumes = client.getVolumeClient().listVolumes(ListVolumesOptions.Builder.virtualMachineId(vm.getId()));
assertEquals(volumes.size(), 1);
Volume volume = Iterables.getOnlyElement(volumes);
// Create a template
String tmplName = "jclouds-" + Integer.toHexString(new Random().nextInt());
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);
assert jobComplete.apply(response.getJobId()) : vm;
template = client.getTemplateClient().getTemplateInZone(response.getId(), vm.getZoneId());
// Assertions
assertNotNull(template);
}
@AfterGroups(groups = "live")
protected void tearDown() {
if (vm != null) {
assert jobComplete.apply(client.getVirtualMachineClient().stopVirtualMachine(vm.getId())) : vm;
assert jobComplete.apply(client.getVirtualMachineClient().destroyVirtualMachine(vm.getId())) : vm;
assert virtualMachineDestroyed.apply(vm);
}
if (template != null) {
client.getTemplateClient().deleteTemplate(template.getId());
}
super.tearDown();
}
} }