From ec2a83197d6e9cd623a978ccf1302ac07e9c4e84 Mon Sep 17 00:00:00 2001 From: Mike Arnold Date: Fri, 9 Mar 2012 14:19:01 -0600 Subject: [PATCH 1/3] updating live test to work with other nova installations --- .../features/FloatingIPClientExpectTest.java | 18 ++++++++++++++++++ .../features/FloatingIPClientLiveTest.java | 12 +++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/FloatingIPClientExpectTest.java b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/FloatingIPClientExpectTest.java index e1d95586ad..8f1a3ec534 100644 --- a/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/FloatingIPClientExpectTest.java +++ b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/FloatingIPClientExpectTest.java @@ -1,3 +1,21 @@ +/** + * 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.features; import com.google.common.collect.ImmutableMultimap; diff --git a/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/FloatingIPClientLiveTest.java b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/FloatingIPClientLiveTest.java index cc1847457f..f2157e23b7 100644 --- a/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/FloatingIPClientLiveTest.java +++ b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/FloatingIPClientLiveTest.java @@ -96,7 +96,7 @@ public class FloatingIPClientLiveTest extends BaseNovaClientLiveTest { for (String regionId : context.getApi().getConfiguredRegions()) { FloatingIPClient client = context.getApi().getFloatingIPClientForRegion(regionId); ServerClient serverClient = context.getApi().getServerClientForRegion(regionId); - Server server = serverClient.createServer("test", "121", "100"); + Server server = serverClient.createServer("test", imageIdForRegion(regionId), flavorRefForRegion(regionId)); blockUntilServerActive(server.getId(), serverClient); FloatingIP floatingIP = client.allocate(); assertNotNull(floatingIP); @@ -111,6 +111,16 @@ public class FloatingIPClientLiveTest extends BaseNovaClientLiveTest { } } + private String imageIdForRegion(String regionId) { + ImageClient imageClient = context.getApi().getImageClientForRegion(regionId); + return Iterables.getLast(imageClient.listImages()).getId(); + } + + private String flavorRefForRegion(String regionId) { + FlavorClient flavorClient = context.getApi().getFlavorClientForRegion(regionId); + return Iterables.getLast(flavorClient.listFlavors()).getId(); + } + private void blockUntilServerActive(String serverId, ServerClient client) throws InterruptedException { Server currentDetails = null; for (currentDetails = client.getServer(serverId); currentDetails.getStatus() != ServerStatus.ACTIVE; currentDetails = client From 940dc4c29b1aacc01c0ef0e1270dbe45cea20373 Mon Sep 17 00:00:00 2001 From: Mike Arnold Date: Fri, 9 Mar 2012 14:45:45 -0600 Subject: [PATCH 2/3] adding more tests and fixing others --- .../v1_1/features/KeyPairClientLiveTest.java | 80 +++++++++++++++++++ .../v1_1/parse/ParseFloatingIPListTest.java | 4 - .../nova/v1_1/parse/ParseFloatingIPTest.java | 7 +- 3 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/KeyPairClientLiveTest.java diff --git a/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/KeyPairClientLiveTest.java b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/KeyPairClientLiveTest.java new file mode 100644 index 0000000000..937acebc44 --- /dev/null +++ b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/KeyPairClientLiveTest.java @@ -0,0 +1,80 @@ +/** + * 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.features; + +import org.jclouds.openstack.nova.v1_1.domain.KeyPair; +import org.jclouds.openstack.nova.v1_1.internal.BaseNovaClientLiveTest; +import org.testng.annotations.Test; + +import java.util.Map; +import java.util.Set; + +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +/** + * Tests behavior of {@code KeyPairClient} + * + * @author Michael Arnold + */ +@Test(groups = "live", testName = "KeyPairClientLiveTest") +public class KeyPairClientLiveTest extends BaseNovaClientLiveTest { + + public void testListKeyPairs() throws Exception { + for (String regionId : context.getApi().getConfiguredRegions()) { + KeyPairClient client = context.getApi().getKeyPairClientForRegion(regionId); + Set> keyPairsList = client.listKeyPairs(); + assertNotNull(keyPairsList); + } + } + + public void testCreateAndDeleteKeyPair() throws Exception { + final String KEYPAIR_NAME = "testkp"; + for(String regionId : context.getApi().getConfiguredRegions()) { + KeyPairClient client = context.getApi().getKeyPairClientForRegion(regionId); + KeyPair keyPair = null; + try { + keyPair = client.createKeyPair(KEYPAIR_NAME); + assertNotNull(keyPair); + } finally { + if (keyPair != null) { + client.deleteKeyPair(KEYPAIR_NAME); + } + } + } + } + + public void testCreateAndDeleteKeyPairWithPublicKey() throws Exception { + final String KEYPAIR_NAME = "testkp"; + final String PUBLIC_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCrrBREFxz3002l1HuXz0+UOdJQ/mOYD5DiJwwB/TOybwIKQJPOxJWA9gBoo4k9dthTKBTaEYbzrll7iZcp59E80S6mNiAr3mUgi+x5Y8uyXeJ2Ws+h6peVyFVUu9epkwpcTd1GVfdcVWsTajwDz9+lxCDhl0RZKDFoT0scTxbj/w== nova@nv-aw2az2-api0002"; + + for(String regionId : context.getApi().getConfiguredRegions()) { + KeyPairClient client = context.getApi().getKeyPairClientForRegion(regionId); + KeyPair keyPair = null; + try { + keyPair = client.createKeyPairWithPublicKey(KEYPAIR_NAME, PUBLIC_KEY); + assertNotNull(keyPair); + } finally { + if (keyPair != null) { + client.deleteKeyPair(KEYPAIR_NAME); + } + } + } + } +} diff --git a/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/parse/ParseFloatingIPListTest.java b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/parse/ParseFloatingIPListTest.java index 9754203c46..31f97b4fb2 100644 --- a/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/parse/ParseFloatingIPListTest.java +++ b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/parse/ParseFloatingIPListTest.java @@ -23,9 +23,6 @@ import com.google.inject.Guice; import com.google.inject.Injector; import org.jclouds.json.BaseSetParserTest; import org.jclouds.json.config.GsonModule; -import org.jclouds.openstack.domain.Link; -import org.jclouds.openstack.domain.Link.Relation; -import org.jclouds.openstack.domain.Resource; import org.jclouds.openstack.nova.v1_1.config.NovaParserModule; import org.jclouds.openstack.nova.v1_1.domain.FloatingIP; import org.jclouds.rest.annotations.SelectJson; @@ -33,7 +30,6 @@ import org.testng.annotations.Test; import javax.ws.rs.Consumes; import javax.ws.rs.core.MediaType; -import java.net.URI; import java.util.Set; /** diff --git a/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/parse/ParseFloatingIPTest.java b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/parse/ParseFloatingIPTest.java index df7d262b42..3abd25ead2 100644 --- a/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/parse/ParseFloatingIPTest.java +++ b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/parse/ParseFloatingIPTest.java @@ -18,25 +18,20 @@ */ package org.jclouds.openstack.nova.v1_1.parse; -import com.google.common.collect.ImmutableSet; import com.google.inject.Guice; import com.google.inject.Injector; import org.jclouds.json.BaseItemParserTest; import org.jclouds.json.config.GsonModule; -import org.jclouds.openstack.domain.Link; -import org.jclouds.openstack.domain.Link.Relation; import org.jclouds.openstack.nova.v1_1.config.NovaParserModule; -import org.jclouds.openstack.nova.v1_1.domain.Flavor; import org.jclouds.openstack.nova.v1_1.domain.FloatingIP; import org.jclouds.rest.annotations.SelectJson; import org.testng.annotations.Test; import javax.ws.rs.Consumes; import javax.ws.rs.core.MediaType; -import java.net.URI; /** - * @author Jeremy Daggett + * @author Michael Arnold */ @Test(groups = "unit", testName = "ParseFloatingIPTest") public class ParseFloatingIPTest extends BaseItemParserTest { From b322e265a543d8c1aad62c519f661791f3cfb4ab Mon Sep 17 00:00:00 2001 From: Mike Arnold Date: Fri, 9 Mar 2012 21:47:51 -0600 Subject: [PATCH 3/3] adding KeyPairClient tests --- .../openstack/nova/v1_1/domain/KeyPair.java | 51 ++++++++++++- .../features/KeyPairClientExpectTest.java | 69 +++++++++++++++++ .../nova/v1_1/parse/ParseKeyPairListTest.java | 76 +++++++++++++++++++ .../src/test/resources/keypair_list.json | 4 +- 4 files changed, 197 insertions(+), 3 deletions(-) create mode 100644 labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/KeyPairClientExpectTest.java create mode 100644 labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/parse/ParseKeyPairListTest.java diff --git a/labs/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/domain/KeyPair.java b/labs/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/domain/KeyPair.java index 8deb6529b5..35a7a5ade1 100644 --- a/labs/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/domain/KeyPair.java +++ b/labs/openstack-nova/src/main/java/org/jclouds/openstack/nova/v1_1/domain/KeyPair.java @@ -24,7 +24,7 @@ import org.jclouds.javax.annotation.Nullable; import com.google.gson.annotations.SerializedName; -public class KeyPair { +public class KeyPair implements Comparable{ public static Builder builder() { return new Builder(); } @@ -116,6 +116,55 @@ public class KeyPair { return this.fingerprint; } + @Override + public int compareTo(KeyPair o) { + return this.fingerprint.compareTo(o.getFingerprint()); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((publicKey == null) ? 0 : publicKey.hashCode()); + result = prime * result + ((privateKey == null) ? 0 : privateKey.hashCode()); + result = prime * result + + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((fingerprint == null) ? 0 : fingerprint.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + KeyPair other = (KeyPair) obj; + if (publicKey == null) { + if (other.publicKey != null) + return false; + } else if (!publicKey.equals(other.publicKey)) + return false; + if (privateKey == null) { + if (other.privateKey != null) + return false; + } else if (!privateKey.equals(other.privateKey)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + if (fingerprint == null) { + if (other.fingerprint != null) + return false; + } else if (!fingerprint.equals(other.fingerprint)) + return false; + return true; + } + @Override public String toString() { return toStringHelper("").add("publicKey", publicKey) diff --git a/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/KeyPairClientExpectTest.java b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/KeyPairClientExpectTest.java new file mode 100644 index 0000000000..4cf4f04aff --- /dev/null +++ b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/KeyPairClientExpectTest.java @@ -0,0 +1,69 @@ +/** + * 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.features; + +import com.google.common.collect.ImmutableMultimap; +import com.google.common.collect.ImmutableSet; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.jclouds.openstack.nova.v1_1.NovaClient; +import org.jclouds.openstack.nova.v1_1.internal.BaseNovaRestClientExpectTest; +import org.jclouds.openstack.nova.v1_1.parse.ParseKeyPairListTest; +import org.testng.annotations.Test; + +import java.net.URI; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +/** + * Tests annotation parsing of {@code KeyPairAsyncClient} + * + * @author Michael Arnold + */ +@Test(groups = "unit", testName = "KeyPairClientExpectTest") +public class KeyPairClientExpectTest extends BaseNovaRestClientExpectTest { + + public void testListKeyPairsWhenResponseIs2xx() throws Exception { + HttpRequest listKeyPairs = HttpRequest + .builder() + .method("GET") + .endpoint( + URI.create("https://compute.north.host/v1.1/3456/os-keypairs")) + .headers( + ImmutableMultimap. builder() + .put("Accept", "application/json") + .put("X-Auth-Token", authToken).build()).build(); + + HttpResponse listKeyPairsResponse = HttpResponse.builder().statusCode(200) + .payload(payloadFromResource("/keypair_list.json")).build(); + + NovaClient clientWhenFloatingIPsExist = requestsSendResponses( + keystoneAuthWithAccessKeyAndSecretKey, responseWithKeystoneAccess, + listKeyPairs, listKeyPairsResponse); + + assertEquals(clientWhenFloatingIPsExist.getConfiguredRegions(), + ImmutableSet.of("North")); + + assertEquals(clientWhenFloatingIPsExist.getKeyPairClientForRegion("North") + .listKeyPairs().toString(), new ParseKeyPairListTest().expected() + .toString()); + } +} diff --git a/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/parse/ParseKeyPairListTest.java b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/parse/ParseKeyPairListTest.java new file mode 100644 index 0000000000..10b921011b --- /dev/null +++ b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/parse/ParseKeyPairListTest.java @@ -0,0 +1,76 @@ +/** + * 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.parse; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.inject.Guice; +import com.google.inject.Injector; +import org.jclouds.json.BaseParserTest; +import org.jclouds.json.BaseSetParserTest; +import org.jclouds.json.config.GsonModule; +import org.jclouds.openstack.nova.v1_1.config.NovaParserModule; +import org.jclouds.openstack.nova.v1_1.domain.KeyPair; +import org.jclouds.rest.annotations.SelectJson; +import org.testng.annotations.Test; + +import javax.ws.rs.Consumes; +import javax.ws.rs.core.MediaType; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * + * @author Michael Arnold + */ +@Test(groups = "unit", testName = "ParseKeyPairListTest") +public class ParseKeyPairListTest extends BaseParserTest { + + @Override + public String resource() { + return "/keypair_list.json"; + } + + @Override + @SelectJson("keypairs") + @Consumes(MediaType.APPLICATION_JSON) + public Set> expected() { + Map kp1 = new HashMap(); + kp1.put("keypair", KeyPair + .builder() + .publicKey("ssh-rsa AAAXB3NzaC1yc2EAAAADAQABAAAAgQCy9EC3O7Ff80vPEfAHDQob61PGwcpYc5KE7tEZnZhrB9n0NyHPRm0E0M+ls3fcTa04HDi+R0DzmRwoyhHQJyI658v8kWZZcuvFjKCcsgsSh/dzdX0xTreLIzSOzt5U7RnZYfshP5cmxtF99yrEY3M/swdin0L+fXsTSkR1B42STQ== nova@nv-aw2az1-api0001") + .name("default") + .fingerprint("ab:0c:f4:f3:54:c0:5d:3f:ed:62:ad:d3:94:7c:79:7c") + .build()); + Map kp2 = new HashMap(); + kp2.put("keypair", KeyPair + .builder() + .publicKey("ssh-rsa AAAXB3NzaC1yc2EAAAADAQABAAAAgQDFNyGjgs6c9akgmZ2ou/fJf7Pdrc23hC95/gM/33OrG4GZABACE4DTioa/PGN+7rHv9YUavUCtXrWayhGniKq/wCuI5fo5TO4AmDNv7/sCGHIHFumADSIoLx0vFhGJIetXEWxL9r0lfFC7//6yZM2W3KcGjbMtlPXqBT9K9PzdyQ== nova@nv-aw2az1-api0001") + .name("testkeypair") + .fingerprint("d2:1f:c9:2b:d8:90:77:5f:15:64:27:e3:9f:77:1d:e4") + .build()); + return ImmutableSet.of(kp1,kp2); + } + + protected Injector injector() { + return Guice.createInjector(new NovaParserModule(), new GsonModule()); + } + +} diff --git a/labs/openstack-nova/src/test/resources/keypair_list.json b/labs/openstack-nova/src/test/resources/keypair_list.json index 1c14e2fbe1..28040ab767 100644 --- a/labs/openstack-nova/src/test/resources/keypair_list.json +++ b/labs/openstack-nova/src/test/resources/keypair_list.json @@ -2,14 +2,14 @@ "keypairs": [ { "keypair": { - "public_key": "ssh-rsa AAAXB3NzaC1yc2EAAAADAQABAAAAgQCy9EC3O7Ff80vPEfAHDQob61PGwcpYc5KE7tEZnZhrB9n0NyHPRm0E0M+ls3fcTa04HDi+R0DzmRwoyhHQJyI658v8kWZZcuvFjKCcsgsSh/dzdX0xTreLIzSOzt5U7RnZYfshP5cmxtF99yrEY3M/swdin0L+fXsTSkR1B42STQ== nova@nv-aw2az1-api0001\n", + "public_key": "ssh-rsa AAAXB3NzaC1yc2EAAAADAQABAAAAgQCy9EC3O7Ff80vPEfAHDQob61PGwcpYc5KE7tEZnZhrB9n0NyHPRm0E0M+ls3fcTa04HDi+R0DzmRwoyhHQJyI658v8kWZZcuvFjKCcsgsSh/dzdX0xTreLIzSOzt5U7RnZYfshP5cmxtF99yrEY3M/swdin0L+fXsTSkR1B42STQ== nova@nv-aw2az1-api0001", "name": "default", "fingerprint": "ab:0c:f4:f3:54:c0:5d:3f:ed:62:ad:d3:94:7c:79:7c" } }, { "keypair": { - "public_key": "ssh-rsa AAAXB3NzaC1yc2EAAAADAQABAAAAgQDFNyGjgs6c9akgmZ2ou/fJf7Pdrc23hC95/gM/33OrG4GZABACE4DTioa/PGN+7rHv9YUavUCtXrWayhGniKq/wCuI5fo5TO4AmDNv7/sCGHIHFumADSIoLx0vFhGJIetXEWxL9r0lfFC7//6yZM2W3KcGjbMtlPXqBT9K9PzdyQ== nova@nv-aw2az1-api0001\n", + "public_key": "ssh-rsa AAAXB3NzaC1yc2EAAAADAQABAAAAgQDFNyGjgs6c9akgmZ2ou/fJf7Pdrc23hC95/gM/33OrG4GZABACE4DTioa/PGN+7rHv9YUavUCtXrWayhGniKq/wCuI5fo5TO4AmDNv7/sCGHIHFumADSIoLx0vFhGJIetXEWxL9r0lfFC7//6yZM2W3KcGjbMtlPXqBT9K9PzdyQ== nova@nv-aw2az1-api0001", "name": "testkeypair", "fingerprint": "d2:1f:c9:2b:d8:90:77:5f:15:64:27:e3:9f:77:1d:e4" }