example of how to trace http requests made in live tests

This commit is contained in:
Adrian Cole 2011-12-09 09:28:56 -08:00
parent 094e537488
commit a6d6b48c26
1 changed files with 82 additions and 5 deletions

View File

@ -23,9 +23,20 @@ import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGIONS;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.Set; 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.domain.Region;
import org.jclouds.aws.ec2.reference.AWSEC2Constants; import org.jclouds.aws.ec2.reference.AWSEC2Constants;
import org.jclouds.compute.BaseTemplateBuilderLiveTest; import org.jclouds.compute.BaseTemplateBuilderLiveTest;
@ -37,14 +48,30 @@ import org.jclouds.compute.domain.Template;
import org.jclouds.ec2.compute.predicates.EC2ImagePredicates; import org.jclouds.ec2.compute.predicates.EC2ImagePredicates;
import org.jclouds.ec2.domain.InstanceType; import org.jclouds.ec2.domain.InstanceType;
import org.jclouds.ec2.domain.RootDeviceType; import org.jclouds.ec2.domain.RootDeviceType;
import org.jclouds.ec2.options.DescribeAvailabilityZonesOptions;
import org.jclouds.ec2.options.DescribeRegionsOptions;
import org.jclouds.ec2.reference.EC2Constants; 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.logging.log4j.config.Log4JLoggingModule; import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableSet; 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.Module;
import com.google.inject.TypeLiteral;
/** /**
* *
@ -300,21 +327,58 @@ 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 @Test
public void testTemplateBuilderWithLessRegions() throws IOException { public void testTemplateBuilderWithLessRegions() throws IOException, SecurityException, NoSuchMethodException {
ComputeServiceContext context = null; ComputeServiceContext context = null;
try { try {
Properties overrides = setupProperties(); Properties overrides = setupProperties();
// set regions to only 1 // set regions to only 1
overrides.setProperty(PROPERTY_REGIONS, Region.US_EAST_1); overrides.setProperty(PROPERTY_REGIONS, Region.US_EAST_1);
context = new ComputeServiceContextFactory().createContext(provider, final List<HttpCommand> commandsInvoked = Lists.newArrayList();
ImmutableSet.<Module> of(new Log4JLoggingModule()), overrides); context = new ComputeServiceContextFactory().createContext(provider, ImmutableSet.<Module> of(
new Log4JLoggingModule(), new AbstractModule() {
@Override
protected void configure() {
bind(JavaUrlHttpCommandExecutorService.class).to(
TrackingJavaUrlHttpCommandExecutorService.class);
bind(new TypeLiteral<List<HttpCommand>>() {
}).toInstance(commandsInvoked);
}
}), overrides);
assertOnlyOnRegionQueriedForAvailabilityZone(commandsInvoked);
assert context.getComputeService().listImages().size() < this.context.getComputeService().listImages().size(); assert context.getComputeService().listImages().size() < this.context.getComputeService().listImages().size();
Template template = context.getComputeService().templateBuilder().imageId("us-east-1/ami-ccb35ea5").build(); Template template = context.getComputeService().templateBuilder().imageId("us-east-1/ami-ccb35ea5").build();
System.out.println(template.getHardware());
assert (template.getImage().getProviderId().startsWith("ami-")) : template; assert (template.getImage().getProviderId().startsWith("ami-")) : template;
assertEquals(template.getImage().getOperatingSystem().getVersion(), "5.4"); assertEquals(template.getImage().getOperatingSystem().getVersion(), "5.4");
assertEquals(template.getImage().getOperatingSystem().is64Bit(), true); assertEquals(template.getImage().getOperatingSystem().is64Bit(), true);
@ -324,7 +388,7 @@ public class AWSEC2TemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest {
assertEquals(template.getLocation().getId(), "us-east-1"); assertEquals(template.getLocation().getId(), "us-east-1");
assertEquals(getCores(template.getHardware()), 2.0d); assertEquals(getCores(template.getHardware()), 2.0d);
assertEquals(template.getHardware().getId(), "m1.large"); // because it assertEquals(template.getHardware().getId(), "m1.large"); // because it
// is 64bit // is 64bit
} finally { } finally {
if (context != null) if (context != null)
@ -332,6 +396,19 @@ public class AWSEC2TemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest {
} }
} }
private static void assertOnlyOnRegionQueriedForAvailabilityZone(List<HttpCommand> commandsInvoked)
throws NoSuchMethodException {
assert commandsInvoked.size() == 2 : commandsInvoked;
assertEquals(getJavaMethodForRequestAtIndex(commandsInvoked, 0), AvailabilityZoneAndRegionAsyncClient.class
.getMethod("describeRegions", DescribeRegionsOptions[].class));
assertEquals(getJavaMethodForRequestAtIndex(commandsInvoked, 1), AvailabilityZoneAndRegionAsyncClient.class
.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 @Test
public void testTemplateBuilderCanUseImageIdFromNonDefaultOwner() { public void testTemplateBuilderCanUseImageIdFromNonDefaultOwner() {
// This is the id of a public image, not owned by one of the four default owners // This is the id of a public image, not owned by one of the four default owners