Issue 158: Move cancel and start a virtual server live test.

This commit is contained in:
Jason King 2011-09-23 13:57:48 +01:00
parent 338c8ba90d
commit a4a771fb8c
3 changed files with 83 additions and 53 deletions

View File

@ -34,6 +34,11 @@ public interface SoftLayerConstants {
*/
public static final String PROPERTY_SOFTLAYER_VIRTUALGUEST_PACKAGE_NAME = "jclouds.softlayer.virtualguest.package-name";
/**
* Name of the package used for ordering virtual guests.
*/
public static final String CLOUD_SERVER_PACKAGE_NAME = "Cloud Server";
public static final Set<ProductItemPrice> DEFAULT_VIRTUAL_GUEST_PRICES = ImmutableSet.<ProductItemPrice>builder()
.add(ProductItemPrice.builder().id(1639).build()) // 100 GB (SAN)
.add(ProductItemPrice.builder().id(21).build()) // 1 IP Address

View File

@ -22,10 +22,8 @@ import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.jclouds.softlayer.compute.functions.ProductItems;
import org.jclouds.softlayer.domain.*;
import org.jclouds.softlayer.reference.SoftLayerConstants;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
@ -34,6 +32,7 @@ import java.util.Set;
import static org.jclouds.softlayer.predicates.ProductItemPredicates.*;
import static org.jclouds.softlayer.predicates.ProductPackagePredicates.named;
import static org.jclouds.softlayer.reference.SoftLayerConstants.CLOUD_SERVER_PACKAGE_NAME;
import static org.testng.Assert.*;
/**
@ -54,8 +53,6 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
cloudServerProductPackage = client.getProductPackage(cloudServerPackageId);
}
private static final String CLOUD_SERVER_PACKAGE_NAME = "Cloud Server";
private ProductPackageClient client;
private AccountClient accountClient;
@ -144,48 +141,6 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
assert new Integer(1693).equals(price.getId());
}
@Test
public void testPricesForLaunchingGuestVM() {
Iterable<ProductItem> ramItems = Iterables.filter(cloudServerProductPackage.getItems(),
Predicates.and(categoryCode("ram"), capacity(2.0f)));
Map<Float, ProductItem> ramToProductItem = Maps.uniqueIndex(ramItems, ProductItems.capacity());
ProductItemPrice ramPrice = ProductItems.price().apply(ramToProductItem.get(2.0f));
Iterable<ProductItem> cpuItems = Iterables.filter(cloudServerProductPackage.getItems(), Predicates.and(units("PRIVATE_CORE"), capacity(2.0f)));
Map<Float, ProductItem> coresToProductItem = Maps.uniqueIndex(cpuItems, ProductItems.capacity());
ProductItemPrice cpuPrice = ProductItems.price().apply(coresToProductItem.get(2.0f));
Iterable<ProductItem> operatingSystems = Iterables.filter(cloudServerProductPackage.getItems(), categoryCode("os"));
Map<String, ProductItem> osToProductItem = Maps.uniqueIndex(operatingSystems, ProductItems.description());
ProductItemPrice osPrice = ProductItems.price().apply(osToProductItem.get("Ubuntu Linux 8 LTS Hardy Heron - Minimal Install (64 bit)"));
Set<ProductItemPrice> prices = Sets.<ProductItemPrice>newLinkedHashSet();
prices.addAll(SoftLayerConstants.DEFAULT_VIRTUAL_GUEST_PRICES);
prices.add(ramPrice);
prices.add(cpuPrice);
prices.add(osPrice);
VirtualGuest guest = VirtualGuest.builder().domain("jclouds.org")
.hostname("livetest")
.build();
String location = ""+Iterables.get(cloudServerProductPackage.getDatacenters(),0).getId();
ProductOrder order = ProductOrder.builder()
.packageId(cloudServerPackageId)
.location(location)
.quantity(1)
.useHourlyPricing(true)
.prices(prices)
.virtualGuest(guest)
.build();
//ProductOrderReceipt receipt = context.getApi().getVirtualGuestClient().orderVirtualGuest(order);
//TODO: There must be a more concise way of expressing this logic.
}
private void checkProductItem(ProductItem item) {
assert item.getId() > 0 : item;
assert item.getDescription() != null : item;

View File

@ -18,16 +18,25 @@
*/
package org.jclouds.softlayer.features;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.util.Set;
import org.jclouds.softlayer.domain.BillingItemVirtualGuest;
import org.jclouds.softlayer.domain.VirtualGuest;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.jclouds.softlayer.compute.functions.ProductItems;
import org.jclouds.softlayer.domain.*;
import org.jclouds.softlayer.reference.SoftLayerConstants;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import static org.jclouds.softlayer.predicates.ProductItemPredicates.*;
import static org.jclouds.softlayer.predicates.ProductPackagePredicates.named;
import static org.jclouds.softlayer.reference.SoftLayerConstants.CLOUD_SERVER_PACKAGE_NAME;
import static org.testng.Assert.*;
/**
* Tests behavior of {@code VirtualGuestClient}
*
@ -35,6 +44,9 @@ import org.testng.annotations.Test;
*/
@Test(groups = "live")
public class VirtualGuestClientLiveTest extends BaseSoftLayerClientLiveTest {
private static final String TEST_HOSTNAME_PREFIX = "livetest";
@BeforeGroups(groups = { "live" })
public void setupClient() {
super.setupClient();
@ -55,6 +67,64 @@ public class VirtualGuestClientLiveTest extends BaseSoftLayerClientLiveTest {
}
}
@Test
public void testCancelAndPlaceOrder() {
// TODO: Should also check if there are active transactions before trying to cancel.
// objectMask: virtualGuests.activeTransaction
for( VirtualGuest guest: client.listVirtualGuests()) {
if (guest.getHostname().startsWith(TEST_HOSTNAME_PREFIX)) {
BillingItemVirtualGuest billingItem = guest.getBillingItem();
if(billingItem!=null) {
client.cancelService(billingItem.getId());
}
}
}
int pkgId = Iterables.find(context.getApi().getAccountClient().getActivePackages(),named(CLOUD_SERVER_PACKAGE_NAME)).getId();
ProductPackage productPackage = context.getApi().getProductPackageClient().getProductPackage(pkgId);
Iterable<ProductItem> ramItems = Iterables.filter(productPackage.getItems(),
Predicates.and(categoryCode("ram"), capacity(2.0f)));
Map<Float, ProductItem> ramToProductItem = Maps.uniqueIndex(ramItems, ProductItems.capacity());
ProductItemPrice ramPrice = ProductItems.price().apply(ramToProductItem.get(2.0f));
Iterable<ProductItem> cpuItems = Iterables.filter(productPackage.getItems(), Predicates.and(units("PRIVATE_CORE"), capacity(2.0f)));
Map<Float, ProductItem> coresToProductItem = Maps.uniqueIndex(cpuItems, ProductItems.capacity());
ProductItemPrice cpuPrice = ProductItems.price().apply(coresToProductItem.get(2.0f));
Iterable<ProductItem> operatingSystems = Iterables.filter(productPackage.getItems(), categoryCode("os"));
Map<String, ProductItem> osToProductItem = Maps.uniqueIndex(operatingSystems, ProductItems.description());
ProductItemPrice osPrice = ProductItems.price().apply(osToProductItem.get("Ubuntu Linux 8 LTS Hardy Heron - Minimal Install (64 bit)"));
Set<ProductItemPrice> prices = Sets.<ProductItemPrice>newLinkedHashSet();
prices.addAll(SoftLayerConstants.DEFAULT_VIRTUAL_GUEST_PRICES);
prices.add(ramPrice);
prices.add(cpuPrice);
prices.add(osPrice);
VirtualGuest guest = VirtualGuest.builder().domain("jclouds.org")
.hostname(TEST_HOSTNAME_PREFIX+new Random().nextInt())
.build();
String location = ""+Iterables.get(productPackage.getDatacenters(),0).getId();
ProductOrder order = ProductOrder.builder()
.packageId(pkgId)
.location(location)
.quantity(1)
.useHourlyPricing(true)
.prices(prices)
.virtualGuest(guest)
.build();
ProductOrderReceipt receipt = context.getApi().getVirtualGuestClient().orderVirtualGuest(order);
assertNotNull(receipt);
}
private void checkVirtualGuest(VirtualGuest vg) {
if (vg.getBillingItem()==null) return;//Quotes and shutting down guests
checkBillingItem(vg.getBillingItem());