From c8939ac0da29aa8ba8b45199b855ca41d7c8efbc Mon Sep 17 00:00:00 2001 From: Tim Miller Date: Wed, 4 Apr 2012 16:09:34 -0700 Subject: [PATCH] add tests for createImage, switch argument order. --- .../nova/v1_1/features/ServerAsyncClient.java | 2 +- .../nova/v1_1/features/ServerClient.java | 6 +- .../v1_1/features/ServerClientExpectTest.java | 66 +++++++++++++++++-- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/features/ServerAsyncClient.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/features/ServerAsyncClient.java index 62ef9e4181..2640f7439e 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/features/ServerAsyncClient.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/features/ServerAsyncClient.java @@ -196,6 +196,6 @@ public interface ServerAsyncClient { @Payload("%7B\"createImage\":%7B\"name\":\"{name}\", \"metadata\": %7B%7D%7D%7D") @ExceptionParser(MapHttp4xxCodesToExceptions.class) @ResponseParser(ParseImageIdFromLocationHeader.class) - ListenableFuture createImageFromServer(@PathParam("id") String id, @PayloadParam("name") String name); + ListenableFuture createImageFromServer(@PayloadParam("name") String name, @PathParam("id") String id); } diff --git a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/features/ServerClient.java b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/features/ServerClient.java index 9835affeb8..51003d3340 100644 --- a/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/features/ServerClient.java +++ b/apis/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/features/ServerClient.java @@ -160,13 +160,13 @@ public interface ServerClient { /** * Create an image from a server. * - * @param id - * id of the server * @param name * The name of the new image + * @param id + * id of the server * * @return ID of the new / updated image */ - String createImageFromServer(String id, String name); + String createImageFromServer(String name, String id); } diff --git a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/ServerClientExpectTest.java b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/ServerClientExpectTest.java index c59690c0d9..7c0edb65e6 100644 --- a/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/ServerClientExpectTest.java +++ b/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/ServerClientExpectTest.java @@ -20,6 +20,7 @@ package org.jclouds.openstack.nova.v1_1.features; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; +import static org.testng.Assert.fail; import java.net.URI; @@ -37,7 +38,7 @@ import com.google.common.collect.ImmutableSet; /** * Tests annotation parsing of {@code ServerAsyncClient} - * + * * @author Adrian Cole */ @Test(groups = "unit", testName = "ServerAsyncClientTest") @@ -80,7 +81,7 @@ public class ServerClientExpectTest extends BaseNovaClientExpectTest { assertTrue(clientWhenNoServersExist.getServerClientForZone("az-1.region-a.geo-1").listServers().isEmpty()); } - + public void testCreateServerWhenResponseIs202() throws Exception { HttpRequest createServer = HttpRequest .builder() @@ -93,7 +94,7 @@ public class ServerClientExpectTest extends BaseNovaClientExpectTest { "{\"server\":{\"name\":\"test-e92\",\"imageRef\":\"1241\",\"flavorRef\":\"100\"}}","application/json")) .build(); - + HttpResponse createServerResponse = HttpResponse.builder().statusCode(202).message("HTTP/1.1 202 Accepted") .payload(payloadFromResourceWithContentType("/new_server.json","application/json; charset=UTF-8")).build(); @@ -117,7 +118,7 @@ public class ServerClientExpectTest extends BaseNovaClientExpectTest { "{\"server\":{\"name\":\"test-e92\",\"imageRef\":\"1241\",\"flavorRef\":\"100\",\"security_groups\":[{\"name\":\"group2\"},{\"name\":\"group1\"}]}}","application/json")) .build(); - + HttpResponse createServerResponse = HttpResponse.builder().statusCode(202).message("HTTP/1.1 202 Accepted") .payload(payloadFromResourceWithContentType("/new_server.json","application/json; charset=UTF-8")).build(); @@ -130,4 +131,61 @@ public class ServerClientExpectTest extends BaseNovaClientExpectTest { new ParseCreatedServerTest().expected().toString()); } + public void testCreateImageWhenResponseIs2xx() throws Exception { + String serverId = "123"; + String imageId = "456"; + String imageName = "foo"; + + HttpRequest createImage = HttpRequest + .builder() + .method("POST") + .endpoint(URI.create("https://compute.north.host/v1.1/3456/servers/" + serverId + "/action")) + .headers( + ImmutableMultimap. builder().put("Accept", "application/json") + .put("X-Auth-Token", authToken).build()) + .payload(payloadFromStringWithContentType( + "{\"createImage\":{\"name\":\"" + imageName + "\", \"metadata\": {}}}", "application/json")) + .build(); + + HttpResponse createImageResponse = HttpResponse.builder() + .statusCode(200) + .headers( + ImmutableMultimap. builder() + .put("Location", "https://compute.north.host/v1.1/3456/images/" + imageId).build()).build(); + + NovaClient clientWhenServerExists = requestsSendResponses(keystoneAuthWithUsernameAndPassword, + responseWithKeystoneAccess, createImage, createImageResponse); + + assertEquals(clientWhenServerExists.getServerClientForZone("az-1.region-a.geo-1").createImageFromServer(imageName, serverId), + imageId); + } + + public void testCreateImageWhenResponseIs404IsEmpty() throws Exception { + String serverId = "123"; + String imageName = "foo"; + + HttpRequest createImage = HttpRequest + .builder() + .method("POST") + .endpoint(URI.create("https://compute.north.host/v1.1/3456/servers/" + serverId + "/action")) + .headers( + ImmutableMultimap. builder().put("Accept", "application/json") + .put("X-Auth-Token", authToken) + .put("Content-Type", "application/json").build()) + .payload(payloadFromStringWithContentType( + "{\"createImage\":{\"name\": " + imageName + ", \"metadata\": {}}}", "application/json")) + .build(); + + HttpResponse createImageResponse = HttpResponse.builder().statusCode(404).build(); + NovaClient clientWhenServerDoesNotExist = requestsSendResponses(keystoneAuthWithUsernameAndPassword, + responseWithKeystoneAccess, createImage, createImageResponse); + + try { + clientWhenServerDoesNotExist.getServerClientForZone("az-1.region-a.geo-1").createImageFromServer(imageName, serverId); + fail("Expected an exception."); + } catch (Exception e) { + ; + } + } + }