Merge pull request #233 from aledsage/Issue-763-MoreTesting

Issue 763: more testing
This commit is contained in:
Adrian Cole 2011-12-13 21:58:00 -08:00
commit 2ab45a495d
6 changed files with 184 additions and 109 deletions

View File

@ -22,7 +22,6 @@ import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import org.jclouds.compute.BaseComputeServiceLiveTest;
@ -53,7 +52,6 @@ import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.annotations.Test;
import com.google.common.base.Predicate;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables;
@ -239,45 +237,6 @@ public class EC2ComputeServiceLiveTest extends BaseComputeServiceLiveTest {
}
}
/**
* e.g. on aws-ec2: timeByImageId=534ms; timeByOsFamily=11587ms.
* Expecting it to be at least 2 times faster seems reasonable, including on other ec2 flavours.
*/
@Test(enabled = true)
public void testTemplateBuildsFasterByImageIdThanBySearchingAllImages() throws Exception {
Stopwatch stopwatch = new Stopwatch();
// Find any image, and get its id
template = buildTemplate(client.templateBuilder());
String imageId = template.getImage().getId();
// Build a template using that specific image-id
context.close();
setupClient();
stopwatch.start();
client.templateBuilder().imageId(imageId).build();
stopwatch.stop();
long timeByImageId = stopwatch.elapsedMillis();
// Build a template using that specific image-id
context.close();
setupClient();
stopwatch.reset();
stopwatch.start();
try {
client.templateBuilder().osFamily(OsFamily.UBUNTU).build();
} catch (NoSuchElementException e) {
// ignore; we are only interested in how long it took to establish this fact!
}
stopwatch.stop();
long timeByOsFamily = stopwatch.elapsedMillis();
System.out.println("testTemplateBuildsFasterByImageIdThanBySearchingAllImages: " +
"timeByImageId="+timeByImageId+"ms; timeByOsFamily="+timeByOsFamily+"ms");
assertTrue((timeByImageId*2) < timeByOsFamily, "timeByImageId="+timeByImageId+"; timeByOsFamily="+timeByOsFamily);
}
protected RunningInstance getInstance(InstanceClient instanceClient, String id) {
RunningInstance instance = Iterables.getOnlyElement(Iterables.getOnlyElement(instanceClient
.describeInstancesInRegion(null, id)));

View File

@ -0,0 +1,79 @@
package org.jclouds.ec2.compute;
import static org.jclouds.http.internal.TrackingJavaUrlHttpCommandExecutorService.getJavaArgsForRequestAtIndex;
import static org.jclouds.http.internal.TrackingJavaUrlHttpCommandExecutorService.getJavaMethodForRequest;
import static org.jclouds.http.internal.TrackingJavaUrlHttpCommandExecutorService.getJavaMethodForRequestAtIndex;
import static org.testng.Assert.assertEquals;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import org.jclouds.compute.BaseTemplateBuilderLiveTest;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.compute.domain.Template;
import org.jclouds.ec2.options.DescribeAvailabilityZonesOptions;
import org.jclouds.ec2.options.DescribeImagesOptions;
import org.jclouds.ec2.options.DescribeRegionsOptions;
import org.jclouds.ec2.services.AMIAsyncClient;
import org.jclouds.ec2.services.AvailabilityZoneAndRegionAsyncClient;
import org.jclouds.http.HttpCommand;
import org.jclouds.http.internal.TrackingJavaUrlHttpCommandExecutorService;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.testng.annotations.Test;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.inject.Module;
public abstract class EC2TemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest {
@Test
public void testTemplateBuilderCanUseImageIdWithoutFetchingAllImages() throws Exception {
Template defaultTemplate = context.getComputeService().templateBuilder().build();
String defaultImageId = defaultTemplate.getImage().getId();
String defaultImageProviderId = defaultTemplate.getImage().getProviderId();
ComputeServiceContext context = null;
try {
// Track http commands
final List<HttpCommand> commandsInvoked = Lists.newArrayList();
context = new ComputeServiceContextFactory(setupRestProperties()).createContext(provider,
ImmutableSet.<Module> of(new Log4JLoggingModule(),
TrackingJavaUrlHttpCommandExecutorService.newTrackingModule(commandsInvoked)),
setupProperties());
Template template = context.getComputeService().templateBuilder().imageId(defaultImageId)
.build();
assertEquals(template.getImage(), defaultTemplate.getImage());
Collection<HttpCommand> filteredCommandsInvoked = Collections2.filter(commandsInvoked, new Predicate<HttpCommand>() {
private final Collection<Method> ignored = ImmutableSet.of(
AvailabilityZoneAndRegionAsyncClient.class.getMethod("describeRegions", DescribeRegionsOptions[].class),
AvailabilityZoneAndRegionAsyncClient.class.getMethod("describeAvailabilityZonesInRegion", String.class, DescribeAvailabilityZonesOptions[].class));
@Override
public boolean apply(HttpCommand input) {
return !ignored.contains(getJavaMethodForRequest(input));
}
});
assert filteredCommandsInvoked.size() == 1 : commandsInvoked;
assertEquals(getJavaMethodForRequestAtIndex(filteredCommandsInvoked, 0), AMIAsyncClient.class
.getMethod("describeImagesInRegion", String.class, DescribeImagesOptions[].class));
assertDescribeImagesOptionsEquals((DescribeImagesOptions[])getJavaArgsForRequestAtIndex(filteredCommandsInvoked, 0).get(1),
defaultImageProviderId);
} finally {
if (context != null)
context.close();
}
}
private static void assertDescribeImagesOptionsEquals(DescribeImagesOptions[] actual, String expectedImageId) {
assertEquals(actual.length, 1);
assertEquals(actual[0].getImageIds(), ImmutableSet.of(expectedImageId));
}
}

View File

@ -163,8 +163,10 @@ public abstract class BaseTemplateBuilderLiveTest extends BaseVersionedServiceLi
}
@Test
public void testTemplateBuilderCanUseImageId() {
public void testTemplateBuilderCanUseImageId() throws Exception {
Template defaultTemplate = context.getComputeService().templateBuilder().build();
context.close();
setupClient();
Template template = context.getComputeService().templateBuilder().imageId(defaultTemplate.getImage().getId())
.locationId(defaultTemplate.getLocation().getId()).build();
@ -281,6 +283,10 @@ public abstract class BaseTemplateBuilderLiveTest extends BaseVersionedServiceLi
} finally {
if (context != null)
context.close();
// Need to clear persisted credentials; otherwise next time a ComputeServiceContext is created
// then it will have these "foo" credentials!
context.credentialStore().clear();
}
}

View File

@ -0,0 +1,88 @@
package org.jclouds.http.internal;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import org.jclouds.Constants;
import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.IOExceptionRetryHandler;
import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
/**
* Records all http commands submitted, storing them in the given List<HttpCommand>.
*
* @author Adrian Cole
*/
@Singleton
public class TrackingJavaUrlHttpCommandExecutorService extends JavaUrlHttpCommandExecutorService {
private final List<HttpCommand> commandsInvoked;
/**
* Creates a guice module, which will bind in TrackingJavaUrlHttpCommandExecutorService and also bind
* the given list so that it is used by the tracker.
*/
public static Module newTrackingModule(final List<HttpCommand> commandsInvoked) {
return new AbstractModule() {
@Override
protected void configure() {
bind(JavaUrlHttpCommandExecutorService.class).to(
TrackingJavaUrlHttpCommandExecutorService.class);
bind(new TypeLiteral<List<HttpCommand>>() {
}).toInstance(commandsInvoked);
}
};
}
public static Method getJavaMethodForRequestAtIndex(final Collection<HttpCommand> commandsInvoked, int index) {
return getJavaMethodForRequest(Iterables.get(commandsInvoked, index));
}
public static Method getJavaMethodForRequest(HttpCommand commandInvoked) {
return GeneratedHttpRequest.class.cast(commandInvoked.getCurrentRequest()).getJavaMethod();
}
@SuppressWarnings("unchecked")
public static List<Object> getJavaArgsForRequestAtIndex(final Collection<HttpCommand> commandsInvoked, int index) {
return GeneratedHttpRequest.class.cast(Iterables.get(commandsInvoked, index).getCurrentRequest()).getArgs();
}
@Inject
public TrackingJavaUrlHttpCommandExecutorService(HttpUtils utils,
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioWorkerExecutor,
DelegatingRetryHandler retryHandler, IOExceptionRetryHandler ioRetryHandler,
DelegatingErrorHandler errorHandler, HttpWire wire, @Named("untrusted") HostnameVerifier verifier,
@Named("untrusted") Supplier<SSLContext> untrustedSSLContextProvider, List<HttpCommand> commandsInvoked)
throws SecurityException, NoSuchFieldException {
super(utils, ioWorkerExecutor, retryHandler, ioRetryHandler, errorHandler, wire, verifier,
untrustedSSLContextProvider);
this.commandsInvoked = commandsInvoked;
}
@Override
public Future<HttpResponse> submit(HttpCommand command) {
commandsInvoked.add(command);
return super.submit(command);
}
}

View File

@ -19,32 +19,23 @@
package org.jclouds.aws.ec2.compute;
import static org.jclouds.compute.util.ComputeServiceUtils.getCores;
import static org.jclouds.http.internal.TrackingJavaUrlHttpCommandExecutorService.getJavaMethodForRequestAtIndex;
import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGIONS;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import org.jclouds.Constants;
import org.jclouds.aws.domain.Region;
import org.jclouds.aws.ec2.reference.AWSEC2Constants;
import org.jclouds.compute.BaseTemplateBuilderLiveTest;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.OsFamilyVersion64Bit;
import org.jclouds.compute.domain.Template;
import org.jclouds.ec2.compute.EC2TemplateBuilderLiveTest;
import org.jclouds.ec2.compute.predicates.EC2ImagePredicates;
import org.jclouds.ec2.domain.InstanceType;
import org.jclouds.ec2.domain.RootDeviceType;
@ -53,32 +44,22 @@ import org.jclouds.ec2.options.DescribeRegionsOptions;
import org.jclouds.ec2.reference.EC2Constants;
import org.jclouds.ec2.services.AvailabilityZoneAndRegionAsyncClient;
import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.IOExceptionRetryHandler;
import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.http.internal.HttpWire;
import org.jclouds.http.internal.JavaUrlHttpCommandExecutorService;
import org.jclouds.http.internal.TrackingJavaUrlHttpCommandExecutorService;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.testng.annotations.Test;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
/**
*
* @author Adrian Cole
*/
@Test(groups = "live")
public class AWSEC2TemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest {
public class AWSEC2TemplateBuilderLiveTest extends EC2TemplateBuilderLiveTest {
public AWSEC2TemplateBuilderLiveTest() {
provider = "aws-ec2";
@ -327,31 +308,6 @@ public class AWSEC2TemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest {
}
}
@Singleton
public static class TrackingJavaUrlHttpCommandExecutorService extends JavaUrlHttpCommandExecutorService {
private final List<HttpCommand> commandsInvoked;
@Inject
public TrackingJavaUrlHttpCommandExecutorService(HttpUtils utils,
@Named(Constants.PROPERTY_IO_WORKER_THREADS) ExecutorService ioWorkerExecutor,
DelegatingRetryHandler retryHandler, IOExceptionRetryHandler ioRetryHandler,
DelegatingErrorHandler errorHandler, HttpWire wire, @Named("untrusted") HostnameVerifier verifier,
@Named("untrusted") Supplier<SSLContext> untrustedSSLContextProvider, List<HttpCommand> commandsInvoked)
throws SecurityException, NoSuchFieldException {
super(utils, ioWorkerExecutor, retryHandler, ioRetryHandler, errorHandler, wire, verifier,
untrustedSSLContextProvider);
this.commandsInvoked = commandsInvoked;
}
@Override
public Future<HttpResponse> submit(HttpCommand command) {
commandsInvoked.add(command);
return super.submit(command);
}
}
@Test
public void testTemplateBuilderWithLessRegions() throws IOException, SecurityException, NoSuchMethodException {
ComputeServiceContext context = null;
@ -362,19 +318,10 @@ public class AWSEC2TemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest {
final List<HttpCommand> commandsInvoked = Lists.newArrayList();
context = new ComputeServiceContextFactory().createContext(provider, ImmutableSet.<Module> of(
new Log4JLoggingModule(), new AbstractModule() {
new Log4JLoggingModule(), TrackingJavaUrlHttpCommandExecutorService.newTrackingModule(commandsInvoked)),
overrides);
@Override
protected void configure() {
bind(JavaUrlHttpCommandExecutorService.class).to(
TrackingJavaUrlHttpCommandExecutorService.class);
bind(new TypeLiteral<List<HttpCommand>>() {
}).toInstance(commandsInvoked);
}
}), overrides);
assertOnlyOnRegionQueriedForAvailabilityZone(commandsInvoked);
assertOnlyOneRegionQueriedForAvailabilityZone(commandsInvoked);
assert context.getComputeService().listImages().size() < this.context.getComputeService().listImages().size();
@ -396,7 +343,7 @@ public class AWSEC2TemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest {
}
}
private static void assertOnlyOnRegionQueriedForAvailabilityZone(List<HttpCommand> commandsInvoked)
private static void assertOnlyOneRegionQueriedForAvailabilityZone(List<HttpCommand> commandsInvoked)
throws NoSuchMethodException {
assert commandsInvoked.size() == 2 : commandsInvoked;
assertEquals(getJavaMethodForRequestAtIndex(commandsInvoked, 0), AvailabilityZoneAndRegionAsyncClient.class
@ -405,10 +352,6 @@ public class AWSEC2TemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest {
.getMethod("describeAvailabilityZonesInRegion", String.class, DescribeAvailabilityZonesOptions[].class));
}
private static Method getJavaMethodForRequestAtIndex(final List<HttpCommand> commandsInvoked, int index) {
return GeneratedHttpRequest.class.cast(commandsInvoked.get(index).getCurrentRequest()).getJavaMethod();
}
@Test
public void testTemplateBuilderCanUseImageIdFromNonDefaultOwner() {
// This is the id of a public image, not owned by one of the four default owners

View File

@ -25,11 +25,11 @@ import java.io.IOException;
import java.util.Set;
import org.jclouds.aws.util.AWSUtils;
import org.jclouds.compute.BaseTemplateBuilderLiveTest;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.OsFamilyVersion64Bit;
import org.jclouds.compute.domain.Template;
import org.jclouds.domain.LocationScope;
import org.jclouds.ec2.compute.EC2TemplateBuilderLiveTest;
import org.jclouds.ec2.compute.util.EC2ComputeUtils;
import org.testng.annotations.Test;
@ -42,7 +42,7 @@ import com.google.common.collect.ImmutableSet;
* @author Adrian Cole
*/
@Test(groups = "live")
public class EucalyptusPartnerCloudEucalyptusTemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest {
public class EucalyptusPartnerCloudEucalyptusTemplateBuilderLiveTest extends EC2TemplateBuilderLiveTest {
public EucalyptusPartnerCloudEucalyptusTemplateBuilderLiveTest() {
provider = "eucalyptus-partnercloud-ec2";