Fixes in live tests

This commit is contained in:
vicglarson 2011-04-20 17:39:01 +04:00 committed by Dmitri Babaev
parent 1035516c2d
commit 3899ff2f99
4 changed files with 328 additions and 194 deletions

View File

@ -25,7 +25,6 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.Injector;
import com.google.inject.Module;
import org.jclouds.Constants;
import org.jclouds.domain.Credentials;
import org.jclouds.http.HttpResponseException;
import org.jclouds.io.Payload;
@ -40,7 +39,7 @@ import org.jclouds.ssh.SshClient;
import org.jclouds.ssh.jsch.config.JschSshClientModule;
import org.jclouds.util.Strings2;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
@ -50,8 +49,7 @@ import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.openstack.nova.PropertyHelper.overridePropertyFromSystemProperty;
import static org.jclouds.openstack.nova.PropertyHelper.*;
import static org.jclouds.openstack.nova.options.CreateServerOptions.Builder.withFile;
import static org.jclouds.openstack.nova.options.ListOptions.Builder.withDetails;
import static org.testng.Assert.*;
@ -69,67 +67,19 @@ public class NovaClientLiveTest {
protected SshClient.Factory sshFactory;
private Predicate<IPSocket> socketTester;
protected String provider = "nova";
protected String identity;
protected String credential;
protected String endpoint;
protected String apiversion;
private String ip;
private String adminPass2;
private String serverPrefix = System.getProperty("user.name") + ".cs";
protected Map<String, String> keyPair;
private int serverId;
private String adminPass;
Map<String, String> metadata = ImmutableMap.of("jclouds", "rackspace");
private int createdImageId;
protected Properties setupProperties() throws IOException {
Properties overrides = new Properties();
overrides.load(this.getClass().getResourceAsStream("/test.properties"));
overridePropertyFromSystemProperty(overrides, "test." + provider + ".endpoint");
overridePropertyFromSystemProperty(overrides, "test." + provider + ".apiversion");
overridePropertyFromSystemProperty(overrides, "test." + provider + ".identity");
overridePropertyFromSystemProperty(overrides, "test." + provider + ".credential");
overridePropertyFromSystemProperty(overrides, "test.initializer");
overrides.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
overrides.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true");
return overrides;
}
protected void setupCredentials(Properties properties) {
identity = checkNotNull(properties.getProperty("test." + provider + ".identity"), "test." + provider + ".identity");
credential = checkNotNull(properties.getProperty("test." + provider + ".credential"), "test." + provider
+ ".credential");
endpoint = properties.getProperty("test." + provider + ".endpoint");
apiversion = properties.getProperty("test." + provider + ".apiversion");
}
protected void updateProperties(final Properties properties) {
properties.setProperty(provider + ".identity", identity);
properties.setProperty(provider + ".credential", credential);
if (endpoint != null)
properties.setProperty(provider + ".endpoint", endpoint);
if (apiversion != null)
properties.setProperty(provider + ".apiversion", apiversion);
}
@BeforeGroups(groups = {"live"})
@BeforeTest
public void setupClient() throws IOException {
Properties overrides = setupProperties();
setupCredentials(overrides);
updateProperties(overrides);
Properties properties = setupOverrides(setupProperties(this.getClass()));
String identity = "admin";
String credential = "d744752f-20d3-4d75-979f-f62f16033b07";
// ComputeServiceContextFactory contextFactory = new ComputeServiceContextFactory();
// ComputeServiceContext context = contextFactory.createContext(provider, identity, credential, Collections.singleton(new JschSshClientModule()), overrides);
Injector injector = new RestContextFactory().createContextBuilder(provider, identity, credential,
ImmutableSet.<Module>of(new SLF4JLoggingModule(), new JschSshClientModule()), overrides)
Injector injector = new RestContextFactory().createContextBuilder(provider,
ImmutableSet.<Module>of(new SLF4JLoggingModule(), new JschSshClientModule()), properties)
.buildInjector();
client = injector.getInstance(NovaClient.class);
@ -138,16 +88,16 @@ public class NovaClientLiveTest {
SocketOpen socketOpen = injector.getInstance(SocketOpen.class);
socketTester = new RetryablePredicate<IPSocket>(socketOpen, 120, 1, TimeUnit.SECONDS);
injector.injectMembers(socketOpen); // add logger
keyPair = setupKeyPair(properties);
}
@Test
public void testListServers() throws Exception {
Set<Server> response = client.listServers();
assert null != response;
long initialContainerCount = response.size();
assertTrue(initialContainerCount >= 0);
}
@Test
@ -216,6 +166,14 @@ public class NovaClientLiveTest {
Set<Server> response = client.listServers(withDetails());
assert null != response;
assertTrue(response.size() >= 0);
for (Server server : response) {
Server newDetails = client.getServer(server.getId());
System.out.println("====");
System.out.println(server);
System.out.println(newDetails);
System.out.println("====");
}
for (Server server : response) {
Server newDetails = client.getServer(server.getId());
assertEquals(server, newDetails);
@ -269,7 +227,7 @@ public class NovaClientLiveTest {
@Test(enabled = true)
public void testCreateServer() throws Exception {
String imageRef = client.getImage(13).getURI().toASCIIString();
String imageRef = client.getImage(95).getURI().toASCIIString();
String flavorRef = client.getFlavor(1).getURI().toASCIIString();
String serverName = serverPrefix + "createserver" + new SecureRandom().nextInt();
Server server = client.createServer(serverName, imageRef, flavorRef, withFile("/etc/jclouds.txt",
@ -280,10 +238,17 @@ public class NovaClientLiveTest {
serverId = server.getId();
adminPass = server.getAdminPass();
blockUntilServerActive(serverId);
Thread.sleep(2000);
blockUntilPublicAddress(serverId);
client.getServer(serverId).getAddresses().getPublicAddresses().iterator().next().getAddress();
}
private void blockUntilPublicAddress(int serverId) throws InterruptedException {
while (client.getServer(serverId).getAddresses().getPublicAddresses().isEmpty()) {
System.out.println("Awaiting public address");
Thread.sleep(1000);
}
}
private void blockUntilServerActive(int serverId) throws InterruptedException {
Server currentDetails;
for (currentDetails = client.getServer(serverId); currentDetails.getStatus() != ServerStatus.ACTIVE; currentDetails = client
@ -313,7 +278,7 @@ public class NovaClientLiveTest {
@Test(enabled = true, timeOut = 300000)
public void testServerDetails() throws Exception {
if (serverId <= 0) testCreateServer();
if (!isServerExist(serverId)) testCreateServer();
Server server = client.getServer(serverId);
assertNotNull(server.getHostId(), "Host id: ");
@ -324,7 +289,7 @@ public class NovaClientLiveTest {
assertEquals(server.getMetadata(), metadata);
assertEquals(server.getImageRef(), endpoint + "/v1.1/images/13");
assertEquals(server.getImageRef(), "endpoint" + "/v1.1/images/95");
// listAddresses tests..
assertEquals(client.getAddresses(serverId), server.getAddresses());
assertEquals(server.getAddresses().getPublicAddresses().size(), 1);
@ -332,7 +297,7 @@ public class NovaClientLiveTest {
assertEquals(server.getAddresses().getPrivateAddresses().size(), 1);
assertEquals(client.listPrivateAddresses(serverId), server.getAddresses().getPrivateAddresses());
assertPassword(server, adminPass);
assertEquals(server.getFlavorRef(), endpoint + "/v1.1/flavors/1");
assertEquals(server.getFlavorRef(), "endpoint" + "/v1.1/flavors/1");
assert server.getProgress() >= 0 : "newDetails.getProgress()" + server.getProgress();
}
@ -341,7 +306,7 @@ public class NovaClientLiveTest {
IPSocket socket = new IPSocket(Iterables.get(server.getAddresses().getPublicAddresses(), 0).getAddress(), 22);
socketTester.apply(socket);
SshClient client = sshFactory.create(socket, new Credentials("root", pass));
SshClient client = sshFactory.create(socket, new Credentials("root", keyPair.get("private")));
try {
client.connect();
Payload etcPasswd = client.get("/etc/jclouds.txt");
@ -355,7 +320,7 @@ public class NovaClientLiveTest {
@Test(enabled = true, timeOut = 5 * 60 * 1000)
public void testRenameServer() throws Exception {
if (serverId <= 0) testCreateServer();
if (!isServerExist(serverId)) testCreateServer();
Server server = client.getServer(serverId);
String oldName = server.getName();
client.renameServer(serverId, oldName + "new");
@ -365,7 +330,7 @@ public class NovaClientLiveTest {
@Test(enabled = true, timeOut = 5 * 60 * 1000)
public void testChangePassword() throws Exception {
if (serverId <= 0) testCreateServer();
if (!isServerExist(serverId)) testCreateServer();
blockUntilServerActive(serverId);
client.changeAdminPass(serverId, "elmo");
assertPassword(client.getServer(serverId), "elmo");
@ -374,7 +339,7 @@ public class NovaClientLiveTest {
@Test(enabled = true, timeOut = 10 * 600 * 1000)
public void testCreateImage() throws Exception {
if (serverId <= 0) testCreateServer();
if (!isServerExist(serverId)) testCreateServer();
Image image = client.createImageFromServer("hoofie", serverId);
assertEquals("hoofie", image.getName());
assertEquals(serverId, image.getServerRef());
@ -384,7 +349,7 @@ public class NovaClientLiveTest {
@Test(enabled = true, timeOut = 10 * 60 * 1000)
public void testRebuildServer() throws Exception {
if (serverId <= 0) testCreateServer();
if (!isServerExist(serverId)) testCreateServer();
client.rebuildServer(serverId, new RebuildServerOptions().withImage(String.valueOf(createdImageId)));
blockUntilServerActive(serverId);
// issue Web Hosting #119580 createdImageId comes back incorrect after rebuild
@ -393,21 +358,21 @@ public class NovaClientLiveTest {
@Test(enabled = true, timeOut = 10 * 60 * 1000)
public void testRebootHard() throws Exception {
if (serverId <= 0) testCreateServer();
if (!isServerExist(serverId)) testCreateServer();
client.rebootServer(serverId, RebootType.HARD);
blockUntilServerActive(serverId);
}
@Test(enabled = true, timeOut = 10 * 60 * 1000)
public void testRebootSoft() throws Exception {
if (serverId <= 0) testCreateServer();
if (!isServerExist(serverId)) testCreateServer();
client.rebootServer(serverId, RebootType.SOFT);
blockUntilServerActive(serverId);
}
@Test(enabled = false, timeOut = 60000, dependsOnMethods = "testRebootSoft")
public void testRevertResize() throws Exception {
if (serverId <= 0) testCreateServer();
if (!isServerExist(serverId)) testCreateServer();
client.resizeServer(serverId, 2);
blockUntilServerVerifyResize(serverId);
client.revertResizeServer(serverId);
@ -417,7 +382,7 @@ public class NovaClientLiveTest {
@Test(enabled = false, timeOut = 10 * 60 * 1000)
public void testConfirmResize() throws Exception {
if (serverId <= 0) testCreateServer();
if (!isServerExist(serverId)) testCreateServer();
client.resizeServer(serverId, 2);
blockUntilServerVerifyResize(serverId);
client.confirmResizeServer(serverId);
@ -427,11 +392,12 @@ public class NovaClientLiveTest {
@Test(enabled = true, timeOut = 60000)
void deleteServer2() throws Exception {
if (serverId <= 0) testCreateServer();
if (!isServerExist(serverId)) testCreateServer();
if (serverId > 0) {
client.deleteServer(serverId);
waitServerDeleted(serverId);
}
serverId = 0;
}
@Test(enabled = true, timeOut = 60000)
@ -443,25 +409,43 @@ public class NovaClientLiveTest {
client.deleteImage(createdImageId);
assert client.getImage(createdImageId) == null;
}
//TODO all servers created from the image should be deleted
}
@Test(enabled = true, timeOut = 60000)
void deleteServer1() throws Exception {
if (serverId <= 0) testCreateServer();
if (!isServerExist(serverId)) testCreateServer();
if (serverId > 0) {
client.deleteServer(serverId);
waitServerDeleted(serverId);
}
serverId = 0;
}
private boolean isServerExist(int serverId) {
return client.getServer(serverId) != null;
}
private void waitServerDeleted(int serverId) throws InterruptedException {
while (null != client.getServer(serverId)) Thread.sleep(1000);
while (null != client.getServer(serverId)) {
System.out.println("Await deleted server" + serverId);
Thread.sleep(1000);
}
}
@Test
public void testDeleteAllCreatedServers() {
for (Server server : client.listServers()) {
if (server.getName().startsWith(serverPrefix)) {
client.deleteServer(server.getId());
System.out.println("Deleted server: " + server);
}
}
}
@AfterTest
void deleteServersOnEnd() {
if (serverId > 0) {
client.deleteServer(serverId);
}
testDeleteAllCreatedServers();
}
}

View File

@ -1,5 +1,14 @@
package org.jclouds.openstack.nova;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;
import org.jclouds.Constants;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
/**
@ -8,10 +17,40 @@ import java.util.Properties;
public class PropertyHelper {
public static String provider = "nova";
public static void overridePropertyFromSystemProperty(final Properties properties, String propertyName) {
if ((System.getProperty(propertyName) != null) && !System.getProperty(propertyName).equals("${" + propertyName + "}"))
properties.setProperty(propertyName, System.getProperty(propertyName));
}
public static Map<String, String> setupKeyPair(Properties properties) throws FileNotFoundException, IOException {
return ImmutableMap.<String, String>of(
"private", Files.toString(new File(properties.getProperty("test.ssh.keyfile.private")), Charsets.UTF_8),
"public", Files.toString(new File(properties.getProperty("test.ssh.keyfile.public")), Charsets.UTF_8));
}
public static Properties setupProperties(Class clazz) throws IOException {
Properties properties = new Properties();
properties.load(clazz.getResourceAsStream("/test.properties"));
overridePropertyFromSystemProperty(properties, "test." + provider + ".endpoint");
overridePropertyFromSystemProperty(properties, "test." + provider + ".apiversion");
overridePropertyFromSystemProperty(properties, "test." + provider + ".identity");
overridePropertyFromSystemProperty(properties, "test." + provider + ".credential");
overridePropertyFromSystemProperty(properties, "test.initializer");
return properties;
}
public static Properties setupOverrides(final Properties properties) {
properties.setProperty(provider + ".identity", properties.getProperty("test." + provider + ".identity"));
properties.setProperty(provider + ".credential", properties.getProperty("test." + provider + ".credential"));
properties.setProperty(provider + ".endpoint", properties.getProperty("test." + provider + ".endpoint"));
properties.setProperty(provider + ".apiversion", "test." + provider + ".apiversion");
properties.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
properties.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true");
return properties;
}
}

View File

@ -18,63 +18,15 @@
*/
package org.jclouds.openstack.nova.compute;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.not;
import static com.google.common.base.Throwables.getRootCause;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.get;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.collect.Maps.newLinkedHashMap;
import static com.google.common.collect.Maps.uniqueIndex;
import static com.google.common.collect.Sets.filter;
import static com.google.common.collect.Sets.newTreeSet;
import static org.jclouds.compute.ComputeTestUtils.buildScript;
import static org.jclouds.compute.options.TemplateOptions.Builder.blockOnComplete;
import static org.jclouds.compute.options.TemplateOptions.Builder.overrideCredentialsWith;
import static org.jclouds.compute.predicates.NodePredicates.TERMINATED;
import static org.jclouds.compute.predicates.NodePredicates.all;
import static org.jclouds.compute.predicates.NodePredicates.inGroup;
import static org.jclouds.compute.predicates.NodePredicates.runningInGroup;
import static org.jclouds.compute.util.ComputeServiceUtils.getCores;
import static org.jclouds.openstack.nova.PropertyHelper.overridePropertyFromSystemProperty;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.Guice;
import com.google.inject.Module;
import org.jclouds.Constants;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.compute.ComputeTestUtils;
import org.jclouds.compute.RunNodesException;
import org.jclouds.compute.RunScriptData;
import org.jclouds.compute.RunScriptOnNodesException;
import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.compute.domain.ComputeType;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.NodeState;
import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.*;
import org.jclouds.compute.domain.*;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location;
@ -96,15 +48,31 @@ import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.io.Files;
import com.google.inject.Guice;
import com.google.inject.Module;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.not;
import static com.google.common.base.Throwables.getRootCause;
import static com.google.common.collect.Iterables.*;
import static com.google.common.collect.Maps.newLinkedHashMap;
import static com.google.common.collect.Maps.uniqueIndex;
import static com.google.common.collect.Sets.filter;
import static com.google.common.collect.Sets.newTreeSet;
import static org.jclouds.compute.ComputeTestUtils.buildScript;
import static org.jclouds.compute.options.TemplateOptions.Builder.blockOnComplete;
import static org.jclouds.compute.options.TemplateOptions.Builder.overrideCredentialsWith;
import static org.jclouds.compute.predicates.NodePredicates.*;
import static org.jclouds.compute.predicates.NodePredicates.all;
import static org.jclouds.compute.util.ComputeServiceUtils.getCores;
import static org.jclouds.openstack.nova.PropertyHelper.overridePropertyFromSystemProperty;
import static org.jclouds.openstack.nova.PropertyHelper.setupKeyPair;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
/**
* Generally disabled, as it incurs higher fees.
@ -119,7 +87,7 @@ public class NovaComputeServiceLiveTest {
protected RetryablePredicate<IPSocket> socketTester;
protected SortedSet<NodeMetadata> nodes;
protected ComputeServiceContext context;
protected ComputeService client;
protected ComputeService computeService;
protected Template template;
protected Map<String, String> keyPair;
@ -181,13 +149,7 @@ public class NovaComputeServiceLiveTest {
context.close();
context = new ComputeServiceContextFactory(setupRestProperties()).createContext(provider, ImmutableSet.of(
new SLF4JLoggingModule(), getSshModule()), properties);
client = context.getComputeService();
}
public static Map<String, String> setupKeyPair(Properties properties) throws FileNotFoundException, IOException {
return ImmutableMap.<String, String>of(
"private", Files.toString(new File(properties.getProperty("test.ssh.keyfile.private")), Charsets.UTF_8),
"public", Files.toString(new File(properties.getProperty("test.ssh.keyfile.public")), Charsets.UTF_8));
computeService = context.getComputeService();
}
@ -270,33 +232,33 @@ public class NovaComputeServiceLiveTest {
@Test(enabled = true)
public void testImagesCache() throws Exception {
client.listImages();
computeService.listImages();
long time = System.currentTimeMillis();
client.listImages();
computeService.listImages();
long duration = System.currentTimeMillis() - time;
assert duration < 1000 : String.format("%dms to get images", duration);
}
@Test(enabled = true, expectedExceptions = NoSuchElementException.class)
public void testCorrectExceptionRunningNodesNotFound() throws Exception {
client.runScriptOnNodesMatching(runningInGroup("zebras-are-awesome"), buildScript(new OperatingSystem.Builder()
computeService.runScriptOnNodesMatching(runningInGroup("zebras-are-awesome"), buildScript(new OperatingSystem.Builder()
.family(OsFamily.UBUNTU).description("ffoo").build()));
}
// since surefire and eclipse don't otherwise guarantee the order, we are
// starting this one alphabetically before create2nodes..
@Test(enabled = true, dependsOnMethods = {"testCompareSizes"})
@Test(enabled = true)
public void testAScriptExecutionAfterBootWithBasicTemplate() throws Exception {
String group = this.group + "r";
try {
client.destroyNodesMatching(inGroup(group));
computeService.destroyNodesMatching(inGroup(group));
} catch (Exception e) {
}
TemplateOptions options = client.templateOptions().blockOnPort(22, 120);
Template template = getDefaultTemplateBuilder().options(computeService.templateOptions().blockOnPort(22, 120)).build();
try {
Set<? extends NodeMetadata> nodes = client.createNodesInGroup(group, 1, options);
Set<? extends NodeMetadata> nodes = computeService.createNodesInGroup(group, 1, template);
Credentials good = nodes.iterator().next().getCredentials();
assert good.identity != null : nodes;
assert good.credential != null : nodes;
@ -310,7 +272,7 @@ public class NovaComputeServiceLiveTest {
assert getRootCause(e).getMessage().contains("Auth fail") : e;
}
for (Map.Entry<? extends NodeMetadata, ExecResponse> response : client.runScriptOnNodesMatching(
for (Map.Entry<? extends NodeMetadata, ExecResponse> response : computeService.runScriptOnNodesMatching(
runningInGroup(group), Statements.exec("echo hello"),
overrideCredentialsWith(good).wrapInInitScript(false).runAsRoot(false)).entrySet())
assert response.getValue().getOutput().trim().equals("hello") : response.getKey() + ": "
@ -321,14 +283,14 @@ public class NovaComputeServiceLiveTest {
checkNodes(nodes, group);
} finally {
client.destroyNodesMatching(inGroup(group));
computeService.destroyNodesMatching(inGroup(group));
}
}
@Test(enabled = true, dependsOnMethods = {"testImagesCache"})
public void testTemplateMatch() throws Exception {
template = buildTemplate(client.templateBuilder());
Template toMatch = client.templateBuilder().imageId(template.getImage().getId()).build();
template = buildTemplate(computeService.templateBuilder());
Template toMatch = computeService.templateBuilder().imageId(template.getImage().getId()).build();
assertEquals(toMatch.getImage(), template.getImage());
}
@ -339,13 +301,13 @@ public class NovaComputeServiceLiveTest {
@Test(enabled = true, dependsOnMethods = "testCompareSizes")
public void testCreateTwoNodesWithRunScript() throws Exception {
try {
client.destroyNodesMatching(inGroup(group));
computeService.destroyNodesMatching(inGroup(group));
} catch (NoSuchElementException e) {
}
refreshTemplate();
try {
nodes = newTreeSet(client.createNodesInGroup(group, 2, template));
nodes = newTreeSet(computeService.createNodesInGroup(group, 2, template));
} catch (RunNodesException e) {
nodes = newTreeSet(concat(e.getSuccessfulNodes(), e.getNodeErrors().keySet()));
throw e;
@ -366,7 +328,7 @@ public class NovaComputeServiceLiveTest {
}
private void refreshTemplate() {
template = buildTemplate(client.templateBuilder());
template = buildTemplate(computeService.templateBuilder());
template.getOptions().installPrivateKey(keyPair.get("private")).authorizePublicKey(keyPair.get("public"))
.runScript(buildScript(template.getImage().getOperatingSystem()));
@ -396,7 +358,7 @@ public class NovaComputeServiceLiveTest {
public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired() throws Exception {
initializeContextAndClient(overrides);
refreshTemplate();
TreeSet<NodeMetadata> nodes = newTreeSet(client.createNodesInGroup(group, 1, template));
TreeSet<NodeMetadata> nodes = newTreeSet(computeService.createNodesInGroup(group, 1, template));
checkNodes(nodes, group);
NodeMetadata node = nodes.first();
this.nodes.add(node);
@ -415,7 +377,7 @@ public class NovaComputeServiceLiveTest {
protected Map<? extends NodeMetadata, ExecResponse> runScriptWithCreds(final String group, OperatingSystem os,
Credentials creds) throws RunScriptOnNodesException {
try {
return client.runScriptOnNodesMatching(runningInGroup(group), buildScript(os), overrideCredentialsWith(creds)
return computeService.runScriptOnNodesMatching(runningInGroup(group), buildScript(os), overrideCredentialsWith(creds)
.nameTask("runScriptWithCreds"));
} catch (SshException e) {
throw e;
@ -429,7 +391,7 @@ public class NovaComputeServiceLiveTest {
@Test(enabled = true, dependsOnMethods = "testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired")
public void testGet() throws Exception {
Map<String, ? extends NodeMetadata> metadataMap = newLinkedHashMap(uniqueIndex(filter(client
Map<String, ? extends NodeMetadata> metadataMap = newLinkedHashMap(uniqueIndex(filter(computeService
.listNodesDetailsMatching(all()), and(inGroup(group), not(TERMINATED))),
new Function<NodeMetadata, String>() {
@ -441,7 +403,7 @@ public class NovaComputeServiceLiveTest {
}));
for (NodeMetadata node : nodes) {
metadataMap.remove(node.getId());
NodeMetadata metadata = client.getNodeMetadata(node.getId());
NodeMetadata metadata = computeService.getNodeMetadata(node.getId());
assertEquals(metadata.getProviderId(), node.getProviderId());
assertEquals(metadata.getGroup(), node.getGroup());
assertLocationSameOrChild(metadata.getLocation(), template.getLocation());
@ -462,14 +424,14 @@ public class NovaComputeServiceLiveTest {
@Test(enabled = true, dependsOnMethods = "testGet")
public void testReboot() throws Exception {
client.rebootNodesMatching(inGroup(group));// TODO test
computeService.rebootNodesMatching(inGroup(group));// TODO test
// validation
testGet();
}
@Test(enabled = true, dependsOnMethods = "testReboot")
public void testSuspendResume() throws Exception {
client.suspendNodesMatching(inGroup(group));
computeService.suspendNodesMatching(inGroup(group));
Set<? extends NodeMetadata> stoppedNodes = refreshNodes();
@ -485,13 +447,13 @@ public class NovaComputeServiceLiveTest {
}) : stoppedNodes;
client.resumeNodesMatching(inGroup(group));
computeService.resumeNodesMatching(inGroup(group));
testGet();
}
@Test(enabled = true, dependsOnMethods = "testSuspendResume")
public void testListNodes() throws Exception {
for (ComputeMetadata node : client.listNodes()) {
for (ComputeMetadata node : computeService.listNodes()) {
assert node.getProviderId() != null;
assert node.getLocation() != null;
assertEquals(node.getType(), ComputeType.NODE);
@ -500,7 +462,7 @@ public class NovaComputeServiceLiveTest {
@Test(enabled = true, dependsOnMethods = "testSuspendResume")
public void testGetNodesWithDetails() throws Exception {
for (NodeMetadata node : client.listNodesDetailsMatching(all())) {
for (NodeMetadata node : computeService.listNodesDetailsMatching(all())) {
assert node.getProviderId() != null : node;
assert node.getLocation() != null : node;
assertEquals(node.getType(), ComputeType.NODE);
@ -522,16 +484,16 @@ public class NovaComputeServiceLiveTest {
@Test(enabled = true, dependsOnMethods = {"testListNodes", "testGetNodesWithDetails"})
public void testDestroyNodes() {
int toDestroy = refreshNodes().size();
Set<? extends NodeMetadata> destroyed = client.destroyNodesMatching(inGroup(group));
Set<? extends NodeMetadata> destroyed = computeService.destroyNodesMatching(inGroup(group));
assertEquals(toDestroy, destroyed.size());
for (NodeMetadata node : filter(client.listNodesDetailsMatching(all()), inGroup(group))) {
for (NodeMetadata node : filter(computeService.listNodesDetailsMatching(all()), inGroup(group))) {
assert node.getState() == NodeState.TERMINATED : node;
assertEquals(context.getCredentialStore().get("node#" + node.getId()), null);
}
}
private Set<? extends NodeMetadata> refreshNodes() {
return filter(client.listNodesDetailsMatching(all()), and(inGroup(group), not(TERMINATED)));
return filter(computeService.listNodesDetailsMatching(all()), and(inGroup(group), not(TERMINATED)));
}
@Test(enabled = true)
@ -539,12 +501,12 @@ public class NovaComputeServiceLiveTest {
String group = this.group + "s";
try {
client.destroyNodesMatching(inGroup(group));
computeService.destroyNodesMatching(inGroup(group));
} catch (Exception e) {
}
template = client.templateBuilder().options(blockOnComplete(false).blockOnPort(8080, 600).inboundPorts(22, 8080))
template = getDefaultTemplateBuilder().options(blockOnComplete(false).blockOnPort(8080, 600).inboundPorts(22, 8080))
.build();
// note this is a dependency on the template resolution
@ -552,11 +514,11 @@ public class NovaComputeServiceLiveTest {
RunScriptData.createScriptInstallAndStartJBoss(keyPair.get("public"), template.getImage()
.getOperatingSystem()));
try {
NodeMetadata node = getOnlyElement(client.createNodesInGroup(group, 1, template));
NodeMetadata node = getOnlyElement(computeService.createNodesInGroup(group, 1, template));
checkHttpGet(node);
} finally {
client.destroyNodesMatching(inGroup(group));
computeService.destroyNodesMatching(inGroup(group));
}
}
@ -564,12 +526,12 @@ public class NovaComputeServiceLiveTest {
@Test(enabled = true/* , dependsOnMethods = "testCompareSizes" */)
public void testTemplateOptions() throws Exception {
TemplateOptions options = new TemplateOptions().withMetadata();
Template t = client.templateBuilder().smallest().options(options).build();
Template t = computeService.templateBuilder().smallest().options(options).build();
assert t.getOptions().isIncludeMetadata() : "The metadata option should be 'true' " + "for the created template";
}
public void testListImages() throws Exception {
for (Image image : client.listImages()) {
for (Image image : computeService.listImages()) {
assert image.getProviderId() != null : image;
// image.getLocationId() can be null, if it is a location-free image
assertEquals(image.getType(), ComputeType.IMAGE);
@ -578,7 +540,7 @@ public class NovaComputeServiceLiveTest {
@Test(groups = {"integration", "live"})
public void testGetAssignableLocations() throws Exception {
for (Location location : client.listAssignableLocations()) {
for (Location location : computeService.listAssignableLocations()) {
System.err.printf("location %s%n", location);
assert location.getId() != null : location;
assert location != location.getParent() : location;
@ -611,21 +573,21 @@ public class NovaComputeServiceLiveTest {
public void testOptionToNotBlock() throws Exception {
String group = this.group + "block";
try {
client.destroyNodesMatching(inGroup(group));
computeService.destroyNodesMatching(inGroup(group));
} catch (Exception e) {
}
// no inbound ports
TemplateOptions options = client.templateOptions().blockUntilRunning(false).inboundPorts();
TemplateOptions options = computeService.templateOptions().blockUntilRunning(false).inboundPorts();
try {
long time = System.currentTimeMillis();
Set<? extends NodeMetadata> nodes = client.createNodesInGroup(group, 1, options);
Set<? extends NodeMetadata> nodes = computeService.createNodesInGroup(group, 1, options);
NodeMetadata node = getOnlyElement(nodes);
assert node.getState() != NodeState.RUNNING;
long duration = System.currentTimeMillis() - time;
assert duration < 30 * 1000 : "duration longer than 30 seconds!: " + duration / 1000;
} finally {
client.destroyNodesMatching(inGroup(group));
computeService.destroyNodesMatching(inGroup(group));
}
}
@ -635,7 +597,7 @@ public class NovaComputeServiceLiveTest {
}
public void testListSizes() throws Exception {
for (Hardware hardware : client.listHardwareProfiles()) {
for (Hardware hardware : computeService.listHardwareProfiles()) {
assert hardware.getProviderId() != null;
assert getCores(hardware) > 0;
assert hardware.getVolumes().size() >= 0;
@ -644,13 +606,19 @@ public class NovaComputeServiceLiveTest {
}
}
private TemplateBuilder getDefaultTemplateBuilder() {
return computeService.templateBuilder().imageId("95");
}
@Test(enabled = true)
public void testCompareSizes() throws Exception {
Hardware defaultSize = client.templateBuilder().build().getHardware();
TemplateBuilder templateBuilder = getDefaultTemplateBuilder();
Hardware smallest = client.templateBuilder().smallest().build().getHardware();
Hardware fastest = client.templateBuilder().fastest().build().getHardware();
Hardware biggest = client.templateBuilder().biggest().build().getHardware();
Hardware defaultSize = templateBuilder.build().getHardware();
Hardware smallest = templateBuilder.smallest().build().getHardware();
Hardware fastest = templateBuilder.fastest().build().getHardware();
Hardware biggest = templateBuilder.biggest().build().getHardware();
System.out.printf("smallest %s%n", smallest);
System.out.printf("fastest %s%n", fastest);

View File

@ -0,0 +1,143 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.live;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableMap;
import org.jclouds.Constants;
import org.jclouds.http.HttpResponseException;
import org.jclouds.net.IPSocket;
import org.jclouds.openstack.nova.NovaClient;
import org.jclouds.openstack.nova.domain.Server;
import org.jclouds.openstack.nova.domain.ServerStatus;
import org.jclouds.ssh.SshClient;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.openstack.nova.PropertyHelper.overridePropertyFromSystemProperty;
import static org.jclouds.openstack.nova.options.CreateServerOptions.Builder.withFile;
/**
* @author Victor Galkin
*/
@Test(groups = "live", sequential = true)
public class DeleteServersInVariousStatesLiveTest {
protected NovaClient client;
protected SshClient.Factory sshFactory;
private Predicate<IPSocket> socketTester;
protected String provider = "nova";
protected String identity;
protected String credential;
protected String endpoint;
protected String apiversion;
Map<String, String> metadata = ImmutableMap.of("jclouds", "rackspace");
Server server = null;
protected Properties setupProperties() throws IOException {
Properties overrides = new Properties();
overrides.load(this.getClass().getResourceAsStream("/test.properties"));
overridePropertyFromSystemProperty(overrides, "test." + provider + ".endpoint");
overridePropertyFromSystemProperty(overrides, "test." + provider + ".apiversion");
overridePropertyFromSystemProperty(overrides, "test." + provider + ".identity");
overridePropertyFromSystemProperty(overrides, "test." + provider + ".credential");
overridePropertyFromSystemProperty(overrides, "test.initializer");
overrides.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
overrides.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true");
return overrides;
}
protected void setupCredentials(Properties properties) {
identity = checkNotNull(properties.getProperty("test." + provider + ".identity"), "test." + provider + ".identity");
credential = checkNotNull(properties.getProperty("test." + provider + ".credential"), "test." + provider
+ ".credential");
endpoint = properties.getProperty("test." + provider + ".endpoint");
apiversion = properties.getProperty("test." + provider + ".apiversion");
}
protected void updateProperties(final Properties properties) {
properties.setProperty(provider + ".identity", identity);
properties.setProperty(provider + ".credential", credential);
if (endpoint != null)
properties.setProperty(provider + ".endpoint", endpoint);
if (apiversion != null)
properties.setProperty(provider + ".apiversion", apiversion);
}
@Test(expectedExceptions = HttpResponseException.class, expectedExceptionsMessageRegExp = ".*Internal Server Error.*")
public void testCreateServerWithUnknownImage() throws Exception {
try {
server = client.createServer("serverName", String.valueOf(88888888), "1", withFile("/etc/jclouds.txt",
"rackspace".getBytes()).withMetadata(metadata));
} catch (HttpResponseException e) {
throw e;
}
}
@Test(expectedExceptions = HttpResponseException.class, expectedExceptionsMessageRegExp = ".*Internal Server Error.*")
public void testCreateServerWithUnknownFlavor() throws Exception {
try {
server = client.createServer("serverName", String.valueOf(13), "88888888", withFile("/etc/jclouds.txt",
"rackspace".getBytes()).withMetadata(metadata));
} catch (HttpResponseException e) {
throw e;
}
}
@AfterMethod
public void after() {
if (server != null) client.deleteServer(server.getId());
}
@Test(enabled = true)
public void testCreateServer() throws Exception {
// String imageRef = client.getImage(13).getURI().toASCIIString();
// String flavorRef = client.getFlavor(1).getURI().toASCIIString();
// String serverName = serverPrefix + "createserver" + new SecureRandom().nextInt();
// Server server = client.createServer(serverName, imageRef, flavorRef, withFile("/etc/jclouds.txt",
// "rackspace".getBytes()).withMetadata(metadata));
//
// assertNotNull(server.getAdminPass());
// assertEquals(server.getStatus(), ServerStatus.BUILD);
// serverId = server.getId();
// adminPass = server.getAdminPass();
// blockUntilServerActive(serverId);
// client.getServer(serverId).getAddresses().getPublicAddresses().iterator().next().getAddress();
}
private void blockUntilServerActive(int serverId) throws InterruptedException {
Server currentDetails;
for (currentDetails = client.getServer(serverId); currentDetails.getStatus() != ServerStatus.ACTIVE; currentDetails = client
.getServer(serverId)) {
System.out.printf("blocking on status active%n%s%n", currentDetails);
Thread.sleep(5 * 1000);
}
}
}