openstack-nova-ec2: adding NovaEC2KeyPairClient with importKeyPair expect and live tests

This commit is contained in:
Adam Lowe 2012-05-03 12:38:57 +01:00
parent 05521685cd
commit 136e1eeff3
4 changed files with 179 additions and 2 deletions

View File

@ -8,9 +8,9 @@ import javax.ws.rs.core.MediaType;
import org.jclouds.Constants; import org.jclouds.Constants;
import org.jclouds.date.DateService; import org.jclouds.date.DateService;
import org.jclouds.date.internal.SimpleDateFormatDateService; import org.jclouds.date.internal.SimpleDateFormatDateService;
import org.jclouds.ec2.EC2Client;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.nova.ec2.NovaEC2Client;
import org.jclouds.openstack.nova.ec2.config.NovaEC2RestClientModule; import org.jclouds.openstack.nova.ec2.config.NovaEC2RestClientModule;
import org.jclouds.rest.ConfiguresRestClient; import org.jclouds.rest.ConfiguresRestClient;
import org.jclouds.rest.internal.BaseRestClientExpectTest; import org.jclouds.rest.internal.BaseRestClientExpectTest;
@ -19,7 +19,7 @@ import com.google.common.collect.ImmutableMultimap;
import com.google.inject.Module; import com.google.inject.Module;
import com.google.inject.Provides; import com.google.inject.Provides;
public abstract class BaseNovaEC2RestClientExpectTest extends BaseRestClientExpectTest<EC2Client> { public abstract class BaseNovaEC2RestClientExpectTest extends BaseRestClientExpectTest<NovaEC2Client> {
protected static final String CONSTANT_DATE = "2012-04-16T15:54:08.897Z"; protected static final String CONSTANT_DATE = "2012-04-16T15:54:08.897Z";
protected DateService dateService = new SimpleDateFormatDateService(); protected DateService dateService = new SimpleDateFormatDateService();
protected URI endpoint = URI.create("http://localhost:8773/services/Cloud/"); protected URI endpoint = URI.create("http://localhost:8773/services/Cloud/");

View File

@ -0,0 +1,35 @@
package org.jclouds.openstack.nova.ec2.services;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import org.jclouds.ec2.domain.KeyPair;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.nova.ec2.internal.BaseNovaEC2RestClientExpectTest;
import com.google.common.collect.ImmutableMultimap;
/**
* @author Adam Lowe
*/
public class NovaEC2KeyPairClientExpectTest extends BaseNovaEC2RestClientExpectTest {
public void testImportKeyPair() {
NovaEC2KeyPairClient client = requestsSendResponses(
describeAvailabilityZonesRequest,
describeAvailabilityZonesResponse,
HttpRequest.builder().method("POST")
.endpoint(URI.create("http://localhost:8773/services/Cloud/"))
.headers(ImmutableMultimap.of("Host", "localhost:8773"))
.payload(payloadFromStringWithContentType("Action=ImportKeyPair&KeyName=mykey&PublicKeyMaterial=c3NoLXJzYSBBQQ%3D%3D&Signature=wOOKOlDfJezRkx7NKcyOyaBQuY7PoVE3HFa9495RL7s%3D&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2012-04-16T15%3A54%3A08.897Z&Version=2009-04-04&AWSAccessKeyId=identity", "application/x-www-form-urlencoded")).build(),
HttpResponse.builder().statusCode(200).payload(payloadFromResource("/nova_ec2_import_keypair_response.xml")).build()
).getKeyPairServices();
KeyPair result = client.importKeyPairInRegion(null, "mykey", "ssh-rsa AA");
assertEquals(result.getKeyName(), "aplowe-nova-ec22");
assertEquals(result.getSha1OfPrivateKey(), "e3:fd:de:f6:4c:36:7d:9b:8f:2f:4c:20:f8:ae:b0:ea");
}
}

View File

@ -0,0 +1,136 @@
/**
* 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.ec2.services;
import static com.google.common.collect.Sets.newTreeSet;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.fail;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import org.jclouds.compute.ComputeTestUtils;
import org.jclouds.compute.internal.BaseComputeServiceContextLiveTest;
import org.jclouds.crypto.SshKeys;
import org.jclouds.ec2.domain.KeyPair;
import org.jclouds.openstack.nova.ec2.NovaEC2ApiMetadata;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* Tests behavior of {@code NovaKeyPairClient}
*
* @author Adam Lowe
*/
@Test(groups = "live", singleThreaded = true)
public class NovaEC2KeyPairClientLiveTest extends BaseComputeServiceContextLiveTest {
public static final String PREFIX = System.getProperty("user.name") + "-nova-ec2";
public NovaEC2KeyPairClientLiveTest() {
provider = "openstack-nova-ec2";
}
private NovaEC2KeyPairClient client;
private Set<String> regions;
@Override
@BeforeClass(groups = { "integration", "live" })
public void setupContext() {
super.setupContext();
client = view.unwrap(NovaEC2ApiMetadata.CONTEXT_TOKEN).getApi().getKeyPairServices();
regions = view.unwrap(NovaEC2ApiMetadata.CONTEXT_TOKEN).getApi().getAvailabilityZoneAndRegionServices().describeRegions().keySet();
}
@Test
void testDescribeKeyPairs() {
for (String region : regions) {
SortedSet<KeyPair> allResults = newTreeSet(client.describeKeyPairsInRegion(region));
assertNotNull(allResults);
if (allResults.size() >= 1) {
KeyPair pair = allResults.last();
SortedSet<KeyPair> result = newTreeSet(client.describeKeyPairsInRegion(region, pair.getKeyName()));
assertNotNull(result);
KeyPair compare = result.last();
assertEquals(compare, pair);
}
}
}
@Test
void testCreateKeyPair() {
String keyName = PREFIX + "1";
cleanupKeyPair(keyName);
try {
KeyPair keyPair = client.createKeyPairInRegion(null, keyName);
checkKeyPair(keyName, keyPair);
assertNotNull(keyPair.getKeyMaterial());
} finally {
cleanupKeyPair(keyName);
}
}
protected void cleanupKeyPair(String keyName) {
try {
client.deleteKeyPairInRegion(null, keyName);
} catch (Exception e) {
}
client.deleteKeyPairInRegion(null, keyName);
}
@Test
void testImportKeyPair() throws IOException {
String keyName = PREFIX + "2";
cleanupKeyPair(keyName);
Map<String, String> myKey = ComputeTestUtils.setupKeyPair();
try {
KeyPair keyPair = client.importKeyPairInRegion(null, keyName, myKey.get("public"));
checkKeyPair(keyName, keyPair);
// check the fingerprint of public key (in the sha10OfPrivateKey field)
assertEquals(keyPair.getSha1OfPrivateKey(), SshKeys.fingerprintPublicKey(myKey.get("public")));
// try again to see if there's an error
try {
client.importKeyPairInRegion(null, keyName, myKey.get("public"));
fail("Duplicate call importKeyPairInRegion should have failed!");
} catch (IllegalStateException e) {
}
} finally {
cleanupKeyPair(keyName);
}
}
protected void checkKeyPair(String keyName, KeyPair keyPair) {
assertNotNull(keyPair);
assertNotNull(keyPair.getSha1OfPrivateKey());
assertEquals(keyPair.getKeyName(), keyName);
Set<KeyPair> twoResults = client.describeKeyPairsInRegion(null, keyName);
assertNotNull(twoResults);
assertEquals(twoResults.size(), 1);
KeyPair listPair = twoResults.iterator().next();
assertEquals(listPair.getKeyName(), keyPair.getKeyName());
assertEquals(listPair.getSha1OfPrivateKey(), keyPair.getSha1OfPrivateKey());
}
}

View File

@ -0,0 +1,6 @@
<?xml version="1.0" ?>
<ImportKeyPairResponse xmlns="http://ec2.amazonaws.com/doc/2009-04-04/">
<requestId>req-dd54edc5-4beb-4b40-9694-0209fcf50459</requestId>
<keyName>aplowe-nova-ec22</keyName>
<keyFingerprint>e3:fd:de:f6:4c:36:7d:9b:8f:2f:4c:20:f8:ae:b0:ea</keyFingerprint>
</ImportKeyPairResponse>