implemented ec2 expect tests

This commit is contained in:
David Ribeiro Alves 2012-05-10 08:42:45 +01:00
parent fc159d1686
commit 129d827355
10 changed files with 345 additions and 18 deletions

View File

@ -108,9 +108,9 @@ public class EC2ImageExtension implements ImageExtension {
return Futures.makeListenable(executor.submit(new Callable<Image>() {
@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);
}

View File

@ -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<ImageExtension>() {
}).to(EC2ImageExtension.class);
bind(new TypeLiteral<PredicateWithResult<String, Image>>() {
}).to(GetImageWhenStatusAvailablePredicateWithResult.class);
}
/**

View File

@ -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)));

View File

@ -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<T> extends BaseRestClientExpectTest<T> implements
Function<ComputeServiceContext, T> {
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<EC2Client, EC2AsyncClient> {
@Override
@Provides
protected String provideTimeStamp(final DateService dateService,
@Named(Constants.PROPERTY_SESSION_INTERVAL) final int expiration) {
return CONSTANT_DATE;
}
}
@Override
public T createClient(Function<HttpRequest, HttpResponse> fn, Module module, Properties props) {
return apply(createComputeServiceContext(fn, module, props));
}
private ComputeServiceContext createComputeServiceContext(Function<HttpRequest, HttpResponse> 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();
}
}

View File

@ -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<Injector> {
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<HttpRequest, HttpResponse> requestResponseMap = ImmutableMap.<HttpRequest, HttpResponse> builder()
.put(describeRegionsRequest, describeRegionsResponse).put(describeImagesRequest0, describeImagesResponse0)
.put(describeImagesRequest1, describeImagesResponse1).put(describeImagesRequest2, describeImagesResponse2)
.build();
public void testReturnsFalseOnQueuedAndSavingAndTrueOnActive() {
Injector injector = requestsSendResponses(requestResponseMap);
PredicateWithResult<String, Image> 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<String, Image> predicate = injector
.getInstance(GetImageWhenStatusAvailablePredicateWithResult.class);
predicate.apply("us-east-1/ami-1");
}
@Override
public Injector apply(ComputeServiceContext input) {
return input.utils().injector();
}
}

View File

@ -0,0 +1,24 @@
<DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2009-11-30/">
<imagesSet>
<item>
<imageId>ami-0</imageId>
<imageLocation>ec2-public-images/fedora-8-i386-base-v1.04.manifest.xml
</imageLocation>
<imageState>available</imageState>
<imageOwnerId>206029621532</imageOwnerId>
<isPublic>false</isPublic>
<productCodes>
<item>
<productCode>9961934F</productCode>
</item>
</productCodes>
<architecture>i386</architecture>
<imageType>machine</imageType>
<kernelId>aki-4438dd2d</kernelId>
<ramdiskId>ari-4538dd2c</ramdiskId>
<rootDeviceType>instance-store</rootDeviceType>
<blockDeviceMapping />
<virtualizationType>paravirtual</virtualizationType>
</item>
</imagesSet>
</DescribeImagesResponse>

View File

@ -0,0 +1,24 @@
<DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2009-11-30/">
<imagesSet>
<item>
<imageId>ami-1</imageId>
<imageLocation>ec2-public-images/fedora-8-i386-base-v1.04.manifest.xml
</imageLocation>
<imageState>deregistered</imageState>
<imageOwnerId>206029621532</imageOwnerId>
<isPublic>false</isPublic>
<productCodes>
<item>
<productCode>9961934F</productCode>
</item>
</productCodes>
<architecture>i386</architecture>
<imageType>machine</imageType>
<kernelId>aki-4438dd2d</kernelId>
<ramdiskId>ari-4538dd2c</ramdiskId>
<rootDeviceType>instance-store</rootDeviceType>
<blockDeviceMapping />
<virtualizationType>paravirtual</virtualizationType>
</item>
</imagesSet>
</DescribeImagesResponse>

View File

@ -0,0 +1,24 @@
<DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/2009-11-30/">
<imagesSet>
<item>
<imageId>ami-2</imageId>
<imageLocation>ec2-public-images/fedora-8-i386-base-v1.04.manifest.xml
</imageLocation>
<imageState>unrecognized</imageState>
<imageOwnerId>206029621532</imageOwnerId>
<isPublic>false</isPublic>
<productCodes>
<item>
<productCode>9961934F</productCode>
</item>
</productCodes>
<architecture>i386</architecture>
<imageType>machine</imageType>
<kernelId>aki-4438dd2d</kernelId>
<ramdiskId>ari-4538dd2c</ramdiskId>
<rootDeviceType>instance-store</rootDeviceType>
<blockDeviceMapping />
<virtualizationType>paravirtual</virtualizationType>
</item>
</imagesSet>
</DescribeImagesResponse>

View File

@ -21,17 +21,21 @@ import com.google.inject.Provides;
public abstract class BaseNovaEC2RestClientExpectTest extends BaseRestClientExpectTest<EC2Client> {
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;
}
}

View File

@ -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<Injector> {