From 129d82735516235920c22aeffa06b34240fa50e0 Mon Sep 17 00:00:00 2001 From: David Ribeiro Alves Date: Thu, 10 May 2012 08:42:45 +0100 Subject: [PATCH] implemented ec2 expect tests --- .../ec2/compute/EC2ImageExtension.java | 6 +- .../EC2ComputeServiceDependenciesModule.java | 4 + ...henStatusAvailablePredicateWithResult.java | 15 +- .../BaseEC2ComputeServiceExpectTest.java | 88 ++++++++++++ ...vailablePredicateWithResultExpectTest.java | 132 ++++++++++++++++++ .../describe_images_imageextension0.xml | 24 ++++ .../describe_images_imageextension1.xml | 24 ++++ .../describe_images_imageextension2.xml | 24 ++++ .../BaseNovaEC2RestClientExpectTest.java | 22 +-- ...veStatusPredicateWithResultExpectTest.java | 24 ++++ 10 files changed, 345 insertions(+), 18 deletions(-) create mode 100644 apis/ec2/src/test/java/org/jclouds/ec2/compute/BaseEC2ComputeServiceExpectTest.java create mode 100644 apis/ec2/src/test/java/org/jclouds/ec2/compute/predicates/GetImageWhenStatusAvailablePredicateWithResultExpectTest.java create mode 100644 apis/ec2/src/test/resources/describe_images_imageextension0.xml create mode 100644 apis/ec2/src/test/resources/describe_images_imageextension1.xml create mode 100644 apis/ec2/src/test/resources/describe_images_imageextension2.xml diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/EC2ImageExtension.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/EC2ImageExtension.java index 276fb230a4..32b244d78f 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/EC2ImageExtension.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/EC2ImageExtension.java @@ -108,9 +108,9 @@ public class EC2ImageExtension implements ImageExtension { return Futures.makeListenable(executor.submit(new Callable() { @Override public Image call() throws Exception { - return Retryables.retryGettingResultOrFailing(imageReadyPredicate, imageId, maxWait, waitPeriod, - TimeUnit.SECONDS, "Image was not created within the time limit, Giving up! [Limit: " + maxWait - + " secs.]"); + return Retryables.retryGettingResultOrFailing(imageReadyPredicate, region + "/" + imageId, maxWait, + waitPeriod, TimeUnit.SECONDS, "Image was not created within the time limit, Giving up! [Limit: " + + maxWait + " secs.]"); } }), executor); } diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2ComputeServiceDependenciesModule.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2ComputeServiceDependenciesModule.java index 9b2beea9e4..d3368edd76 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2ComputeServiceDependenciesModule.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/config/EC2ComputeServiceDependenciesModule.java @@ -49,11 +49,13 @@ import org.jclouds.ec2.compute.loaders.CreateSecurityGroupIfNeeded; import org.jclouds.ec2.compute.loaders.LoadPublicIpForInstanceOrNull; import org.jclouds.ec2.compute.loaders.RegionAndIdToImage; import org.jclouds.ec2.compute.options.EC2TemplateOptions; +import org.jclouds.ec2.compute.predicates.GetImageWhenStatusAvailablePredicateWithResult; import org.jclouds.ec2.compute.predicates.SecurityGroupPresent; import org.jclouds.ec2.domain.InstanceState; import org.jclouds.ec2.domain.KeyPair; import org.jclouds.ec2.domain.RunningInstance; import org.jclouds.ec2.reference.EC2Constants; +import org.jclouds.predicates.PredicateWithResult; import org.jclouds.predicates.RetryablePredicate; import com.google.common.base.Function; @@ -108,6 +110,8 @@ public class EC2ComputeServiceDependenciesModule extends AbstractModule { bind(WindowsLoginCredentialsFromEncryptedData.class); bind(new TypeLiteral() { }).to(EC2ImageExtension.class); + bind(new TypeLiteral>() { + }).to(GetImageWhenStatusAvailablePredicateWithResult.class); } /** diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/predicates/GetImageWhenStatusAvailablePredicateWithResult.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/predicates/GetImageWhenStatusAvailablePredicateWithResult.java index 8db7a7067c..4e6333ad77 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/predicates/GetImageWhenStatusAvailablePredicateWithResult.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/predicates/GetImageWhenStatusAvailablePredicateWithResult.java @@ -3,8 +3,10 @@ package org.jclouds.ec2.compute.predicates; import static com.google.common.base.Preconditions.checkNotNull; import javax.annotation.Resource; +import javax.inject.Inject; import javax.inject.Named; +import org.jclouds.aws.util.AWSUtils; import org.jclouds.compute.domain.Image; import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.ec2.EC2Client; @@ -21,22 +23,23 @@ public final class GetImageWhenStatusAvailablePredicateWithResult implements Pre @Named(ComputeServiceConstants.COMPUTE_LOGGER) protected Logger logger = Logger.NULL; - private final String region; private final EC2Client ec2Client; private final EC2ImageParser ec2ImageToImage; private org.jclouds.ec2.domain.Image result; private RuntimeException lastFailure; - public GetImageWhenStatusAvailablePredicateWithResult(EC2Client ec2Client, EC2ImageParser ec2ImageToImage, - String region) { - this.region = region; + @Inject + public GetImageWhenStatusAvailablePredicateWithResult(EC2Client ec2Client, EC2ImageParser ec2ImageToImage) { this.ec2Client = ec2Client; this.ec2ImageToImage = ec2ImageToImage; } @Override public boolean apply(String input) { - result = checkNotNull(findImage(input)); + String[] parts = AWSUtils.parseHandle(input); + String region = parts[0]; + String imageId = parts[1]; + result = checkNotNull(findImage(imageId, region)); switch (result.getImageState()) { case AVAILABLE: logger.info("<< Image %s is available for use.", input); @@ -60,7 +63,7 @@ public final class GetImageWhenStatusAvailablePredicateWithResult implements Pre return lastFailure; } - private org.jclouds.ec2.domain.Image findImage(String id) { + private org.jclouds.ec2.domain.Image findImage(String id, String region) { return Iterables.getOnlyElement(ec2Client.getAMIServices().describeImagesInRegion(region, new DescribeImagesOptions().imageIds(id))); diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/compute/BaseEC2ComputeServiceExpectTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/compute/BaseEC2ComputeServiceExpectTest.java new file mode 100644 index 0000000000..f663fde11b --- /dev/null +++ b/apis/ec2/src/test/java/org/jclouds/ec2/compute/BaseEC2ComputeServiceExpectTest.java @@ -0,0 +1,88 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.jclouds.ec2.compute; + +import java.util.Properties; + +import javax.inject.Named; + +import org.jclouds.Constants; +import org.jclouds.apis.ApiMetadata; +import org.jclouds.compute.ComputeServiceContext; +import org.jclouds.date.DateService; +import org.jclouds.date.internal.SimpleDateFormatDateService; +import org.jclouds.ec2.EC2ApiMetadata; +import org.jclouds.ec2.EC2AsyncClient; +import org.jclouds.ec2.EC2Client; +import org.jclouds.ec2.config.EC2RestClientModule; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.jclouds.rest.ConfiguresRestClient; +import org.jclouds.rest.internal.BaseRestClientExpectTest; + +import com.google.common.base.Function; +import com.google.inject.Module; +import com.google.inject.Provides; + +/** + * + * @author David Alves + */ +public abstract class BaseEC2ComputeServiceExpectTest extends BaseRestClientExpectTest implements + Function { + + protected static final String CONSTANT_DATE = "2012-04-16T15:54:08.897Z"; + protected DateService dateService = new SimpleDateFormatDateService(); + + public BaseEC2ComputeServiceExpectTest() { + provider = "ec2"; + } + + @ConfiguresRestClient + private static final class TestEC2RestClientModule extends EC2RestClientModule { + @Override + @Provides + protected String provideTimeStamp(final DateService dateService, + @Named(Constants.PROPERTY_SESSION_INTERVAL) final int expiration) { + return CONSTANT_DATE; + } + } + + @Override + public T createClient(Function fn, Module module, Properties props) { + return apply(createComputeServiceContext(fn, module, props)); + } + + private ComputeServiceContext createComputeServiceContext(Function fn, Module module, + Properties props) { + return createInjector(fn, module, props).getInstance(ComputeServiceContext.class); + } + + @Override + protected Module createModule() { + return new TestEC2RestClientModule(); + } + + @Override + protected ApiMetadata createApiMetadata() { + return new EC2ApiMetadata(); + } + +} diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/compute/predicates/GetImageWhenStatusAvailablePredicateWithResultExpectTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/compute/predicates/GetImageWhenStatusAvailablePredicateWithResultExpectTest.java new file mode 100644 index 0000000000..744119fba3 --- /dev/null +++ b/apis/ec2/src/test/java/org/jclouds/ec2/compute/predicates/GetImageWhenStatusAvailablePredicateWithResultExpectTest.java @@ -0,0 +1,132 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.jclouds.ec2.compute.predicates; + +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; + +import java.net.URI; +import java.util.Map; + +import javax.ws.rs.core.MediaType; + +import org.jclouds.compute.ComputeServiceContext; +import org.jclouds.compute.domain.Image; +import org.jclouds.ec2.compute.BaseEC2ComputeServiceExpectTest; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.jclouds.predicates.PredicateWithResult; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMultimap; +import com.google.inject.Injector; + +/** + * + * @author David Alves + */ +@Test(groups = "unit", testName = "GetImageWhenStatusAvailablePredicateWithResultExpectTest") +public class GetImageWhenStatusAvailablePredicateWithResultExpectTest extends BaseEC2ComputeServiceExpectTest { + + protected HttpRequest describeRegionsRequest = HttpRequest + .builder() + .method("POST") + .endpoint(URI.create("https://ec2.us-east-1.amazonaws.com/")) + .headers(ImmutableMultimap.of("Host", "ec2.us-east-1.amazonaws.com")) + .payload(payloadFromStringWithContentType( + "Action=DescribeRegions&Signature=s5OXKqaaeKhJW5FVrRntuMsUL4Ed5fjzgUWeukU96ko%3D&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2012-04-16T15%3A54%3A08.897Z&Version=2010-06-15&AWSAccessKeyId=identity", + "application/x-www-form-urlencoded")).build(); + + protected HttpRequest describeImagesRequest0 = HttpRequest + .builder() + .method("POST") + .endpoint(URI.create("https://ec2.us-east-1.amazonaws.com/")) + .headers(ImmutableMultimap.of("Host", "ec2.us-east-1.amazonaws.com")) + .payload(payloadFromStringWithContentType( + "Action=DescribeImages&ImageId.1=ami-0&Signature=k9douTXFWkAZecPiZfBLUm3LIS3bTLanMV%2F%2BWrB1jFA%3D&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2012-04-16T15%3A54%3A08.897Z&Version=2010-06-15&AWSAccessKeyId=identity", + "application/x-www-form-urlencoded")).build(); + + protected HttpRequest describeImagesRequest1 = HttpRequest + .builder() + .method("POST") + .endpoint(URI.create("https://ec2.us-east-1.amazonaws.com/")) + .headers(ImmutableMultimap.of("Host", "ec2.us-east-1.amazonaws.com")) + .payload(payloadFromStringWithContentType( + "Action=DescribeImages&ImageId.1=ami-1&Signature=IVunQEvp8vTKTIxXex2Uh5SWQY1PJCx0ExUe9FRujBY%3D&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2012-04-16T15%3A54%3A08.897Z&Version=2010-06-15&AWSAccessKeyId=identity", + "application/x-www-form-urlencoded")).build(); + + protected HttpRequest describeImagesRequest2 = HttpRequest + .builder() + .method("POST") + .endpoint(URI.create("https://ec2.us-east-1.amazonaws.com/")) + .headers(ImmutableMultimap.of("Host", "ec2.us-east-1.amazonaws.com")) + .payload(payloadFromStringWithContentType( + "Action=DescribeImages&ImageId.1=ami-2&Signature=8TfP8BJlg1hiY6EqUbS73A7PQO7dlpqnRMyi7hPu76U%3D&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2012-04-16T15%3A54%3A08.897Z&Version=2010-06-15&AWSAccessKeyId=identity", + "application/x-www-form-urlencoded")).build(); + + protected HttpResponse describeRegionsResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/regionEndpoints.xml", MediaType.APPLICATION_XML)).build(); + + protected HttpResponse describeImagesResponse0 = HttpResponse + .builder() + .statusCode(200) + .payload(payloadFromResourceWithContentType("/describe_images_imageextension0.xml", + MediaType.APPLICATION_XML)).build(); + + protected HttpResponse describeImagesResponse1 = HttpResponse + .builder() + .statusCode(200) + .payload(payloadFromResourceWithContentType("/describe_images_imageextension1.xml", + MediaType.APPLICATION_XML)).build(); + + protected HttpResponse describeImagesResponse2 = HttpResponse + .builder() + .statusCode(200) + .payload(payloadFromResourceWithContentType("/describe_images_imageextension2.xml", + MediaType.APPLICATION_XML)).build(); + + private final Map requestResponseMap = ImmutableMap. builder() + .put(describeRegionsRequest, describeRegionsResponse).put(describeImagesRequest0, describeImagesResponse0) + .put(describeImagesRequest1, describeImagesResponse1).put(describeImagesRequest2, describeImagesResponse2) + .build(); + + public void testReturnsFalseOnQueuedAndSavingAndTrueOnActive() { + Injector injector = requestsSendResponses(requestResponseMap); + PredicateWithResult predicate = injector + .getInstance(GetImageWhenStatusAvailablePredicateWithResult.class); + assertTrue(predicate.apply("us-east-1/ami-0")); + assertFalse(predicate.apply("us-east-1/ami-2")); + } + + @Test(groups = "unit", testName = "GetImageWhenStatusAvailablePredicateWithResultExpectTest", expectedExceptions = IllegalStateException.class) + public void testFailsOnOtherStatuses() { + Injector injector = requestsSendResponses(requestResponseMap); + PredicateWithResult predicate = injector + .getInstance(GetImageWhenStatusAvailablePredicateWithResult.class); + predicate.apply("us-east-1/ami-1"); + } + + @Override + public Injector apply(ComputeServiceContext input) { + return input.utils().injector(); + } + +} diff --git a/apis/ec2/src/test/resources/describe_images_imageextension0.xml b/apis/ec2/src/test/resources/describe_images_imageextension0.xml new file mode 100644 index 0000000000..fde8b1d86b --- /dev/null +++ b/apis/ec2/src/test/resources/describe_images_imageextension0.xml @@ -0,0 +1,24 @@ + + + + ami-0 + ec2-public-images/fedora-8-i386-base-v1.04.manifest.xml + + available + 206029621532 + false + + + 9961934F + + + i386 + machine + aki-4438dd2d + ari-4538dd2c + instance-store + + paravirtual + + + \ No newline at end of file diff --git a/apis/ec2/src/test/resources/describe_images_imageextension1.xml b/apis/ec2/src/test/resources/describe_images_imageextension1.xml new file mode 100644 index 0000000000..5b2bc97519 --- /dev/null +++ b/apis/ec2/src/test/resources/describe_images_imageextension1.xml @@ -0,0 +1,24 @@ + + + + ami-1 + ec2-public-images/fedora-8-i386-base-v1.04.manifest.xml + + deregistered + 206029621532 + false + + + 9961934F + + + i386 + machine + aki-4438dd2d + ari-4538dd2c + instance-store + + paravirtual + + + \ No newline at end of file diff --git a/apis/ec2/src/test/resources/describe_images_imageextension2.xml b/apis/ec2/src/test/resources/describe_images_imageextension2.xml new file mode 100644 index 0000000000..bef5c03b41 --- /dev/null +++ b/apis/ec2/src/test/resources/describe_images_imageextension2.xml @@ -0,0 +1,24 @@ + + + + ami-2 + ec2-public-images/fedora-8-i386-base-v1.04.manifest.xml + + unrecognized + 206029621532 + false + + + 9961934F + + + i386 + machine + aki-4438dd2d + ari-4538dd2c + instance-store + + paravirtual + + + \ No newline at end of file diff --git a/apis/openstack-nova-ec2/src/test/java/org/jclouds/openstack/nova/ec2/internal/BaseNovaEC2RestClientExpectTest.java b/apis/openstack-nova-ec2/src/test/java/org/jclouds/openstack/nova/ec2/internal/BaseNovaEC2RestClientExpectTest.java index 5f34f76241..deb7f8bc44 100644 --- a/apis/openstack-nova-ec2/src/test/java/org/jclouds/openstack/nova/ec2/internal/BaseNovaEC2RestClientExpectTest.java +++ b/apis/openstack-nova-ec2/src/test/java/org/jclouds/openstack/nova/ec2/internal/BaseNovaEC2RestClientExpectTest.java @@ -21,17 +21,21 @@ import com.google.inject.Provides; public abstract class BaseNovaEC2RestClientExpectTest extends BaseRestClientExpectTest { protected static final String CONSTANT_DATE = "2012-04-16T15:54:08.897Z"; + protected DateService dateService = new SimpleDateFormatDateService(); protected URI endpoint = URI.create("http://localhost:8773/services/Cloud/"); - - protected HttpRequest describeAvailabilityZonesRequest = HttpRequest.builder().method("POST") - .endpoint(endpoint) - .headers(ImmutableMultimap.of("Host", "localhost:8773")) - .payload(payloadFromStringWithContentType("Action=DescribeAvailabilityZones&Signature=S3fa5fybw4KAq4o11IpKHlqwx3cVJdKfeAKw3FIJYvM%3D&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2012-04-16T15%3A54%3A08.897Z&Version=2009-04-04&AWSAccessKeyId=identity", MediaType.APPLICATION_FORM_URLENCODED)) - .build(); - protected HttpResponse describeAvailabilityZonesResponse = HttpResponse.builder() - .statusCode(200).payload(payloadFromResourceWithContentType("/nova_ec2_availabilityZones.xml", MediaType.APPLICATION_XML)).build(); + protected HttpRequest describeAvailabilityZonesRequest = HttpRequest + .builder() + .method("POST") + .endpoint(endpoint) + .headers(ImmutableMultimap.of("Host", "localhost:8773")) + .payload(payloadFromStringWithContentType( + "Action=DescribeAvailabilityZones&Signature=s5OXKqaaeKhJW5FVrRntuMsUL4Ed5fjzgUWeukU96ko%3D&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2012-04-16T15%3A54%3A08.897Z&Version=2009-04-04&AWSAccessKeyId=identity", + MediaType.APPLICATION_FORM_URLENCODED)).build(); + protected HttpResponse describeAvailabilityZonesResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResourceWithContentType("/nova_ec2_availabilityZones.xml", MediaType.APPLICATION_XML)) + .build(); public BaseNovaEC2RestClientExpectTest() { provider = "openstack-nova-ec2"; @@ -42,7 +46,7 @@ public abstract class BaseNovaEC2RestClientExpectTest extends BaseRestClientExpe @Override @Provides protected String provideTimeStamp(final DateService dateService, - @Named(Constants.PROPERTY_SESSION_INTERVAL) final int expiration) { + @Named(Constants.PROPERTY_SESSION_INTERVAL) final int expiration) { return CONSTANT_DATE; } } diff --git a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/compute/predicates/GetImageWhenImageInZoneHasActiveStatusPredicateWithResultExpectTest.java b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/compute/predicates/GetImageWhenImageInZoneHasActiveStatusPredicateWithResultExpectTest.java index a72be63ce4..a6c52ec9bb 100644 --- a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/compute/predicates/GetImageWhenImageInZoneHasActiveStatusPredicateWithResultExpectTest.java +++ b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/compute/predicates/GetImageWhenImageInZoneHasActiveStatusPredicateWithResultExpectTest.java @@ -1,3 +1,22 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.jclouds.openstack.nova.v1_1.compute.predicates; import static junit.framework.Assert.assertEquals; @@ -17,6 +36,11 @@ import org.testng.annotations.Test; import com.google.common.collect.ImmutableMap; import com.google.inject.Injector; +/** + * + * @author David Alves + * + */ @Test(groups = "unit", testName = "GetImageWhenImageInZoneHasActiveStatucPredicateWithResultExpectTest") public class GetImageWhenImageInZoneHasActiveStatusPredicateWithResultExpectTest extends BaseNovaComputeServiceContextExpectTest {