Added test properties template. Rearranged tests.

This commit is contained in:
unknown 2011-04-08 17:52:32 +04:00 committed by Dmitri Babaev
parent 4019137687
commit 8fb5ec9555
4 changed files with 1217 additions and 580 deletions

View File

@ -21,7 +21,7 @@
--> -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4.0.0.xsd" > xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.jclouds</groupId> <groupId>org.jclouds</groupId>
@ -42,6 +42,11 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.jclouds.provider</groupId>
<artifactId>aws-ec2</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.jclouds.common</groupId> <groupId>org.jclouds.common</groupId>
<artifactId>openstack-common</artifactId> <artifactId>openstack-common</artifactId>
@ -131,6 +136,14 @@
<name>test.nova.credential</name> <name>test.nova.credential</name>
<value>${test.nova.credential}</value> <value>${test.nova.credential}</value>
</property> </property>
<property>
<name>test.nova.credential</name>
<value>${test.ssh.keyfile.public}</value>
</property>
<property>
<name>test.nova.credential</name>
<value>${test.ssh.keyfile.private}</value>
</property>
<property> <property>
<name>test.initializer</name> <name>test.initializer</name>
<value>${test.initializer}</value> <value>${test.initializer}</value>

View File

@ -18,40 +18,21 @@
*/ */
package org.jclouds.openstack.nova; package org.jclouds.openstack.nova;
import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.base.Predicate;
import static org.jclouds.openstack.nova.options.CreateServerOptions.Builder.withFile; import com.google.common.collect.ImmutableMap;
import static org.jclouds.openstack.nova.options.CreateSharedIpGroupOptions.Builder.withServer; import com.google.common.collect.ImmutableSet;
import static org.jclouds.openstack.nova.options.ListOptions.Builder.withDetails; import com.google.common.collect.Iterables;
import static org.testng.Assert.assertEquals; import com.google.inject.Injector;
import static org.testng.Assert.assertNotNull; import com.google.inject.Module;
import static org.testng.Assert.assertTrue;
import java.io.IOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.security.SecureRandom;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.jclouds.Constants; import org.jclouds.Constants;
import org.jclouds.openstack.nova.domain.BackupSchedule;
import org.jclouds.openstack.nova.domain.DailyBackup;
import org.jclouds.openstack.nova.domain.Flavor;
import org.jclouds.openstack.nova.domain.Image;
import org.jclouds.openstack.nova.domain.ImageStatus;
import org.jclouds.openstack.nova.domain.RebootType;
import org.jclouds.openstack.nova.domain.Server;
import org.jclouds.openstack.nova.domain.ServerStatus;
import org.jclouds.openstack.nova.domain.SharedIpGroup;
import org.jclouds.openstack.nova.domain.WeeklyBackup;
import org.jclouds.openstack.nova.options.RebuildServerOptions;
import org.jclouds.compute.domain.ExecResponse; import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.http.HttpResponseException; import org.jclouds.http.HttpResponseException;
import org.jclouds.io.Payload; import org.jclouds.io.Payload;
import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.net.IPSocket; import org.jclouds.net.IPSocket;
import org.jclouds.openstack.nova.domain.*;
import org.jclouds.openstack.nova.options.RebuildServerOptions;
import org.jclouds.predicates.RetryablePredicate; import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.predicates.SocketOpen; import org.jclouds.predicates.SocketOpen;
import org.jclouds.rest.RestContextFactory; import org.jclouds.rest.RestContextFactory;
@ -63,12 +44,19 @@ import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Predicate; import java.io.IOException;
import com.google.common.collect.ImmutableMap; import java.lang.reflect.UndeclaredThrowableException;
import com.google.common.collect.ImmutableSet; import java.security.SecureRandom;
import com.google.common.collect.Iterables; import java.util.Map;
import com.google.inject.Injector; import java.util.Properties;
import com.google.inject.Module; import java.util.Set;
import java.util.concurrent.TimeUnit;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.openstack.nova.options.CreateServerOptions.Builder.withFile;
import static org.jclouds.openstack.nova.options.CreateSharedIpGroupOptions.Builder.withServer;
import static org.jclouds.openstack.nova.options.ListOptions.Builder.withDetails;
import static org.testng.Assert.*;
/** /**
* Tests behavior of {@code NovaClient} * Tests behavior of {@code NovaClient}
@ -88,43 +76,67 @@ public class NovaClientLiveTest {
protected String endpoint; protected String endpoint;
protected String apiversion; protected String apiversion;
protected void setupCredentials() { private void overridePropertyFromSystemProperty(final Properties properties, String propertyName) {
identity = checkNotNull(System.getProperty("test." + provider + ".identity"), "test." + provider + ".identity"); if ((System.getProperty(propertyName) != null) && !System.getProperty(propertyName).equals("${" + propertyName + "}"))
credential = checkNotNull(System.getProperty("test." + provider + ".credential"), "test." + provider properties.setProperty(propertyName, System.getProperty(propertyName));
+ ".credential");
endpoint = System.getProperty("test." + provider + ".endpoint");
apiversion = System.getProperty("test." + provider + ".apiversion");
} }
protected Properties setupProperties() {
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);
}
protected Properties setupProperties() throws IOException {
Properties overrides = new Properties(); 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_TRUST_ALL_CERTS, "true");
overrides.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true"); overrides.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true");
overrides.setProperty(provider + ".identity", identity);
overrides.setProperty(provider + ".credential", credential);
if (endpoint != null)
overrides.setProperty(provider + ".endpoint", endpoint);
if (apiversion != null)
overrides.setProperty(provider + ".apiversion", apiversion);
return overrides; return overrides;
} }
@BeforeGroups(groups = { "live" }) @BeforeGroups(groups = {"live"})
public void setupClient() { public void setupClient() throws IOException {
setupCredentials();
Properties overrides = setupProperties(); Properties overrides = setupProperties();
setupCredentials(overrides);
updateProperties(overrides);
Injector injector = new RestContextFactory().createContextBuilder(provider, String identity = "admin";
ImmutableSet.<Module> of(new Log4JLoggingModule(), new JschSshClientModule()), overrides) 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 Log4JLoggingModule(), new JschSshClientModule()), overrides)
.buildInjector(); .buildInjector();
client = injector.getInstance(NovaClient.class); client = injector.getInstance(NovaClient.class);
sshFactory = injector.getInstance(SshClient.Factory.class); sshFactory = injector.getInstance(SshClient.Factory.class);
SocketOpen socketOpen = injector.getInstance(SocketOpen.class); SocketOpen socketOpen = injector.getInstance(SocketOpen.class);
socketTester = new RetryablePredicate<IPSocket>(socketOpen, 120, 1, TimeUnit.SECONDS); socketTester = new RetryablePredicate<IPSocket>(socketOpen, 120, 1, TimeUnit.SECONDS);
injector.injectMembers(socketOpen); // add logger injector.injectMembers(socketOpen); // add logger
} }
@Test
public void testListServers() throws Exception { public void testListServers() throws Exception {
Set<Server> response = client.listServers(); Set<Server> response = client.listServers();
@ -134,6 +146,7 @@ public class NovaClientLiveTest {
} }
@Test
public void testListServersDetail() throws Exception { public void testListServersDetail() throws Exception {
Set<Server> response = client.listServers(withDetails()); Set<Server> response = client.listServers(withDetails());
assert null != response; assert null != response;
@ -141,6 +154,7 @@ public class NovaClientLiveTest {
assertTrue(initialContainerCount >= 0); assertTrue(initialContainerCount >= 0);
} }
@Test
public void testListImages() throws Exception { public void testListImages() throws Exception {
Set<Image> response = client.listImages(); Set<Image> response = client.listImages();
assert null != response; assert null != response;
@ -153,6 +167,7 @@ public class NovaClientLiveTest {
} }
@Test
public void testListImagesDetail() throws Exception { public void testListImagesDetail() throws Exception {
Set<Image> response = client.listImages(withDetails()); Set<Image> response = client.listImages(withDetails());
assert null != response; assert null != response;
@ -165,6 +180,7 @@ public class NovaClientLiveTest {
} }
} }
@Test
public void testGetImagesDetail() throws Exception { public void testGetImagesDetail() throws Exception {
Set<Image> response = client.listImages(withDetails()); Set<Image> response = client.listImages(withDetails());
assert null != response; assert null != response;
@ -191,6 +207,7 @@ public class NovaClientLiveTest {
assert client.getServer(12312987) == null; assert client.getServer(12312987) == null;
} }
@Test
public void testGetServersDetail() throws Exception { public void testGetServersDetail() throws Exception {
Set<Server> response = client.listServers(withDetails()); Set<Server> response = client.listServers(withDetails());
assert null != response; assert null != response;
@ -314,7 +331,7 @@ public class NovaClientLiveTest {
private String adminPass2; private String adminPass2;
private int imageId; private int imageId;
@Test(enabled = false) @Test(enabled = true)
public void testCreateServer() throws Exception { public void testCreateServer() throws Exception {
int imageId = 14362; int imageId = 14362;
int flavorId = 1; int flavorId = 1;
@ -392,6 +409,7 @@ public class NovaClientLiveTest {
/** /**
* this tests "personality" as the file looked up was sent during server creation * this tests "personality" as the file looked up was sent during server creation
*/ */
private void checkPassOk(Server newDetails, String pass) throws IOException { private void checkPassOk(Server newDetails, String pass) throws IOException {
try { try {
doCheckPass(newDetails, pass); doCheckPass(newDetails, pass);
@ -590,8 +608,8 @@ public class NovaClientLiveTest {
assertEquals(new Integer(2), client.getServer(serverId2).getFlavorId()); assertEquals(new Integer(2), client.getServer(serverId2).getFlavorId());
} }
@Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = { "testRebootSoft", "testRevertResize", @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = {"testRebootSoft", "testRevertResize",
"testConfirmResize" }) "testConfirmResize"})
void deleteServer2() { void deleteServer2() {
if (serverId2 > 0) { if (serverId2 > 0) {
client.deleteServer(serverId2); client.deleteServer(serverId2);
@ -615,7 +633,7 @@ public class NovaClientLiveTest {
} }
} }
@Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = { "deleteServer1" }) @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = {"deleteServer1"})
void testDeleteSharedIpGroup() { void testDeleteSharedIpGroup() {
if (sharedIpGroupId > 0) { if (sharedIpGroupId > 0) {
client.deleteSharedIpGroup(sharedIpGroupId); client.deleteSharedIpGroup(sharedIpGroupId);

View File

@ -18,71 +18,670 @@
*/ */
package org.jclouds.openstack.nova.compute; package org.jclouds.openstack.nova.compute;
import static org.testng.Assert.assertEquals; import com.google.common.base.Charsets;
import com.google.common.base.Function;
import java.io.IOException; 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 org.jclouds.Constants;
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;
import org.jclouds.domain.LocationScope;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.net.IPSocket;
import org.jclouds.openstack.nova.NovaAsyncClient; import org.jclouds.openstack.nova.NovaAsyncClient;
import org.jclouds.openstack.nova.NovaClient; import org.jclouds.openstack.nova.NovaClient;
import org.jclouds.compute.BaseComputeServiceLiveTest; import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.compute.ComputeServiceContextFactory; import org.jclouds.predicates.SocketOpen;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.rest.AuthorizationException;
import org.jclouds.domain.LocationScope;
import org.jclouds.rest.RestContext; import org.jclouds.rest.RestContext;
import org.jclouds.rest.RestContextFactory;
import org.jclouds.scriptbuilder.domain.Statements;
import org.jclouds.ssh.SshClient;
import org.jclouds.ssh.SshException;
import org.jclouds.ssh.jsch.config.JschSshClientModule; import org.jclouds.ssh.jsch.config.JschSshClientModule;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.io.File;
import java.io.FileNotFoundException;
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.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
/** /**
*
* Generally disabled, as it incurs higher fees. * Generally disabled, as it incurs higher fees.
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
@Test(groups = "live", enabled = true, sequential = true) @Test(groups = "novalive", enabled = true, sequential = true)
public class NovaComputeServiceLiveTest extends BaseComputeServiceLiveTest { public class NovaComputeServiceLiveTest {
protected String group;
protected RetryablePredicate<IPSocket> socketTester;
protected SortedSet<NodeMetadata> nodes;
protected ComputeServiceContext context;
protected ComputeService client;
protected Template template;
protected Map<String, String> keyPair;
protected String provider;
protected String identity;
protected String credential;
protected String endpoint;
protected String apiversion;
private Properties overrides;
public NovaComputeServiceLiveTest() { public NovaComputeServiceLiveTest() {
provider = "nova"; provider = "nova";
} }
@Override
private void overridePropertyFromSystemProperty(final Properties properties, String propertyName) {
if ((System.getProperty(propertyName) != null) && !System.getProperty(propertyName).equals("${" + propertyName + "}"))
properties.setProperty(propertyName, System.getProperty(propertyName));
}
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);
}
protected Properties setupProperties() {
Properties overrides = new Properties();
try {
overrides.load(this.getClass().getResourceAsStream("/test.properties"));
} catch (IOException e) {
throw new RuntimeException("Can't load properties");
}
overridePropertyFromSystemProperty(overrides, "test." + provider + ".endpoint");
overridePropertyFromSystemProperty(overrides, "test." + provider + ".apiversion");
overridePropertyFromSystemProperty(overrides, "test." + provider + ".identity");
overridePropertyFromSystemProperty(overrides, "test." + provider + ".credential");
overridePropertyFromSystemProperty(overrides, "test.ssh.keyfile.public");
overridePropertyFromSystemProperty(overrides, "test.ssh.keyfile.private");
overridePropertyFromSystemProperty(overrides, "test.initializer");
overrides.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
overrides.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true");
return overrides;
}
protected Properties setupRestProperties() {
return RestContextFactory.getPropertiesFromResource("/rest.properties");
}
private void initializeContextAndClient(Properties properties) throws IOException {
if (context != null)
context.close();
context = new ComputeServiceContextFactory(setupRestProperties()).createContext(provider, ImmutableSet.of(
new Log4JLoggingModule(), 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));
}
@BeforeGroups(groups = {"novalive"})
public void setupClient() throws InterruptedException, ExecutionException, TimeoutException, IOException {
if (group == null)
group = checkNotNull(provider, "provider");
if (group.indexOf('-') == -1)
group = group + "-";
Properties properties = setupProperties();
setupCredentials(properties);
updateProperties(properties);
overrides = properties;
keyPair = setupKeyPair(properties);
initializeContextAndClient(properties);
buildSocketTester();
}
protected void buildSocketTester() {
SocketOpen socketOpen = Guice.createInjector(getSshModule()).getInstance(SocketOpen.class);
socketTester = new RetryablePredicate<IPSocket>(socketOpen, 60, 1, TimeUnit.SECONDS);
}
protected JschSshClientModule getSshModule() { protected JschSshClientModule getSshModule() {
return new JschSshClientModule(); return new JschSshClientModule();
} }
@Test
public void testAssignability() throws Exception { public void testAssignability() throws Exception {
@SuppressWarnings("unused") @SuppressWarnings("unused")
RestContext<NovaClient, NovaAsyncClient> tmContext = new ComputeServiceContextFactory() RestContext<NovaClient, NovaAsyncClient> tmContext = new ComputeServiceContextFactory()
.createContext(provider, identity, credential).getProviderSpecificContext(); .createContext(provider, identity, credential, Collections.singleton(new JschSshClientModule()), overrides).getProviderSpecificContext();
} }
@Override
protected void checkNodes(Iterable<? extends NodeMetadata> nodes, String tag) throws IOException { protected void checkNodes(Iterable<? extends NodeMetadata> nodes, String tag) throws IOException {
super.checkNodes(nodes, tag); _checkNodes(nodes, tag);
for (NodeMetadata node : nodes) { for (NodeMetadata node : nodes) {
assertEquals(node.getLocation().getScope(), LocationScope.HOST); assertEquals(node.getLocation().getScope(), LocationScope.HOST);
} }
} }
@Test(enabled = true, dependsOnMethods = "testReboot", expectedExceptions = UnsupportedOperationException.class) protected void _checkNodes(Iterable<? extends NodeMetadata> nodes, String group) throws IOException {
for (NodeMetadata node : nodes) {
assertNotNull(node.getProviderId());
assertNotNull(node.getGroup());
assertEquals(node.getGroup(), group);
assertEquals(node.getState(), NodeState.RUNNING);
Credentials fromStore = context.getCredentialStore().get("node#" + node.getId());
assertEquals(fromStore, node.getCredentials());
assert node.getPublicAddresses().size() >= 1 || node.getPrivateAddresses().size() >= 1 : "no ips in" + node;
assertNotNull(node.getCredentials());
if (node.getCredentials().identity != null) {
assertNotNull(node.getCredentials().identity);
assertNotNull(node.getCredentials().credential);
sshPing(node);
}
}
}
@Test(enabled = true, expectedExceptions = AuthorizationException.class)
public void testCorrectAuthException() throws Exception {
ComputeServiceContext context = null;
try {
context = new ComputeServiceContextFactory(setupRestProperties()).createContext(provider, "MOMMA", "MIA", ImmutableSet
.<Module>of(new Log4JLoggingModule()), overrides);
context.getComputeService().listNodes();
} catch (AuthorizationException e) {
throw e;
} catch (RuntimeException e) {
e.printStackTrace();
throw e;
} finally {
if (context != null)
context.close();
}
}
@Test(enabled = true)
public void testImagesCache() throws Exception {
client.listImages();
long time = System.currentTimeMillis();
client.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()
.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"})
public void testAScriptExecutionAfterBootWithBasicTemplate() throws Exception {
String group = this.group + "r";
try {
client.destroyNodesMatching(inGroup(group));
} catch (Exception e) {
}
TemplateOptions options = client.templateOptions().blockOnPort(22, 120);
try {
Set<? extends NodeMetadata> nodes = client.createNodesInGroup(group, 1, options);
Credentials good = nodes.iterator().next().getCredentials();
assert good.identity != null : nodes;
assert good.credential != null : nodes;
OperatingSystem os = get(nodes, 0).getOperatingSystem();
try {
Map<? extends NodeMetadata, ExecResponse> responses = runScriptWithCreds(group, os, new Credentials(
good.identity, "romeo"));
assert false : "shouldn't pass with a bad password\n" + responses;
} catch (RunScriptOnNodesException e) {
assert getRootCause(e).getMessage().contains("Auth fail") : e;
}
for (Map.Entry<? extends NodeMetadata, ExecResponse> response : client.runScriptOnNodesMatching(
runningInGroup(group), Statements.exec("echo hello"),
overrideCredentialsWith(good).wrapInInitScript(false).runAsRoot(false)).entrySet())
assert response.getValue().getOutput().trim().equals("hello") : response.getKey() + ": "
+ response.getValue();
runScriptWithCreds(group, os, good);
checkNodes(nodes, group);
} finally {
client.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();
assertEquals(toMatch.getImage(), template.getImage());
}
protected void checkHttpGet(NodeMetadata node) {
ComputeTestUtils.checkHttpGet(context.utils().http(), node, 8080);
}
@Test(enabled = true, dependsOnMethods = "testCompareSizes")
public void testCreateTwoNodesWithRunScript() throws Exception {
try {
client.destroyNodesMatching(inGroup(group));
} catch (NoSuchElementException e) {
}
refreshTemplate();
try {
nodes = newTreeSet(client.createNodesInGroup(group, 2, template));
} catch (RunNodesException e) {
nodes = newTreeSet(concat(e.getSuccessfulNodes(), e.getNodeErrors().keySet()));
throw e;
}
assertEquals(nodes.size(), 2);
checkNodes(nodes, group);
NodeMetadata node1 = nodes.first();
NodeMetadata node2 = nodes.last();
// credentials aren't always the same
// assertEquals(node1.getCredentials(), node2.getCredentials());
assertLocationSameOrChild(node1.getLocation(), template.getLocation());
assertLocationSameOrChild(node2.getLocation(), template.getLocation());
checkImageIdMatchesTemplate(node1);
checkImageIdMatchesTemplate(node2);
checkOsMatchesTemplate(node1);
checkOsMatchesTemplate(node2);
}
private void refreshTemplate() {
template = buildTemplate(client.templateBuilder());
template.getOptions().installPrivateKey(keyPair.get("private")).authorizePublicKey(keyPair.get("public"))
.runScript(buildScript(template.getImage().getOperatingSystem()));
}
protected void checkImageIdMatchesTemplate(NodeMetadata node) {
if (node.getImageId() != null)
assertEquals(node.getImageId(), template.getImage().getId());
}
protected void checkOsMatchesTemplate(NodeMetadata node) {
if (node.getOperatingSystem() != null)
assert node.getOperatingSystem().getFamily().equals(template.getImage().getOperatingSystem().getFamily()) : String
.format("expecting family %s but got %s", template.getImage().getOperatingSystem().getFamily(), node
.getOperatingSystem());
}
void assertLocationSameOrChild(Location test, Location expected) {
if (!test.equals(expected)) {
assertEquals(test.getParent().getId(), expected.getId());
} else {
assertEquals(test, expected);
}
}
@Test(enabled = true, dependsOnMethods = "testCreateTwoNodesWithRunScript")
public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired() throws Exception {
initializeContextAndClient(overrides);
refreshTemplate();
TreeSet<NodeMetadata> nodes = newTreeSet(client.createNodesInGroup(group, 1, template));
checkNodes(nodes, group);
NodeMetadata node = nodes.first();
this.nodes.add(node);
assertEquals(nodes.size(), 1);
assertLocationSameOrChild(node.getLocation(), template.getLocation());
checkOsMatchesTemplate(node);
}
@Test(enabled = true, dependsOnMethods = "testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired")
public void testCredentialsCache() throws Exception {
initializeContextAndClient(overrides);
for (NodeMetadata node : nodes)
assert (context.getCredentialStore().get("node#" + node.getId()) != null) : "credentials for " + node.getId();
}
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)
.nameTask("runScriptWithCreds"));
} catch (SshException e) {
throw e;
}
}
protected Template buildTemplate(TemplateBuilder templateBuilder) {
return templateBuilder.build();
}
@Test(enabled = true, dependsOnMethods = "testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired")
public void testGet() throws Exception {
Map<String, ? extends NodeMetadata> metadataMap = newLinkedHashMap(uniqueIndex(filter(client
.listNodesDetailsMatching(all()), and(inGroup(group), not(TERMINATED))),
new Function<NodeMetadata, String>() {
@Override
public String apply(NodeMetadata from) {
return from.getId();
}
}));
for (NodeMetadata node : nodes) {
metadataMap.remove(node.getId());
NodeMetadata metadata = client.getNodeMetadata(node.getId());
assertEquals(metadata.getProviderId(), node.getProviderId());
assertEquals(metadata.getGroup(), node.getGroup());
assertLocationSameOrChild(metadata.getLocation(), template.getLocation());
checkImageIdMatchesTemplate(metadata);
checkOsMatchesTemplate(metadata);
assert (metadata.getState() == NodeState.RUNNING) : metadata;
// due to DHCP the addresses can actually change in-between runs.
assertEquals(metadata.getPrivateAddresses().size(), node.getPrivateAddresses().size());
assertEquals(metadata.getPublicAddresses().size(), node.getPublicAddresses().size());
}
assertNodeZero(metadataMap.values());
}
protected void assertNodeZero(Collection<? extends NodeMetadata> metadataSet) {
assert metadataSet.size() == 0 : String.format("nodes left in set: [%s] which didn't match set: [%s]",
metadataSet, nodes);
}
@Test(enabled = true, dependsOnMethods = "testGet")
public void testReboot() throws Exception {
client.rebootNodesMatching(inGroup(group));// TODO test
// validation
testGet();
}
@Test(enabled = true, dependsOnMethods = "testReboot")
public void testSuspendResume() throws Exception { public void testSuspendResume() throws Exception {
super.testSuspendResume(); client.suspendNodesMatching(inGroup(group));
Set<? extends NodeMetadata> stoppedNodes = refreshNodes();
assert Iterables.all(stoppedNodes, new Predicate<NodeMetadata>() {
@Override
public boolean apply(NodeMetadata input) {
boolean returnVal = input.getState() == NodeState.SUSPENDED;
if (!returnVal)
System.err.printf("warning: node %s in state %s%n", input.getId(), input.getState());
return returnVal;
}
}) : stoppedNodes;
client.resumeNodesMatching(inGroup(group));
testGet();
} }
@Test(enabled = true, dependsOnMethods = "testSuspendResume") @Test(enabled = true, dependsOnMethods = "testSuspendResume")
@Override
public void testGetNodesWithDetails() throws Exception {
super.testGetNodesWithDetails();
}
@Test(enabled = true, dependsOnMethods = "testSuspendResume")
@Override
public void testListNodes() throws Exception { public void testListNodes() throws Exception {
super.testListNodes(); for (ComputeMetadata node : client.listNodes()) {
assert node.getProviderId() != null;
assert node.getLocation() != null;
assertEquals(node.getType(), ComputeType.NODE);
}
} }
@Test(enabled = true, dependsOnMethods = { "testListNodes", "testGetNodesWithDetails" }) @Test(enabled = true, dependsOnMethods = "testSuspendResume")
@Override public void testGetNodesWithDetails() throws Exception {
for (NodeMetadata node : client.listNodesDetailsMatching(all())) {
assert node.getProviderId() != null : node;
assert node.getLocation() != null : node;
assertEquals(node.getType(), ComputeType.NODE);
assert node instanceof NodeMetadata;
NodeMetadata nodeMetadata = (NodeMetadata) node;
assert nodeMetadata.getProviderId() != null : nodeMetadata;
// nullable
// assert nodeMetadata.getImage() != null : node;
// user specified name is not always supported
// assert nodeMetadata.getName() != null : nodeMetadata;
if (nodeMetadata.getState() == NodeState.RUNNING) {
assert nodeMetadata.getPublicAddresses() != null : nodeMetadata;
assert nodeMetadata.getPublicAddresses().size() > 0 || nodeMetadata.getPrivateAddresses().size() > 0 : nodeMetadata;
assertNotNull(nodeMetadata.getPrivateAddresses());
}
}
}
@Test(enabled = true, dependsOnMethods = {"testListNodes", "testGetNodesWithDetails"})
public void testDestroyNodes() { public void testDestroyNodes() {
super.testDestroyNodes(); int toDestroy = refreshNodes().size();
Set<? extends NodeMetadata> destroyed = client.destroyNodesMatching(inGroup(group));
assertEquals(toDestroy, destroyed.size());
for (NodeMetadata node : filter(client.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)));
}
@Test(enabled = true)
public void testCreateAndRunAService() throws Exception {
String group = this.group + "s";
try {
client.destroyNodesMatching(inGroup(group));
} catch (Exception e) {
}
template = client.templateBuilder().options(blockOnComplete(false).blockOnPort(8080, 600).inboundPorts(22, 8080))
.build();
// note this is a dependency on the template resolution
template.getOptions().runScript(
RunScriptData.createScriptInstallAndStartJBoss(keyPair.get("public"), template.getImage()
.getOperatingSystem()));
try {
NodeMetadata node = getOnlyElement(client.createNodesInGroup(group, 1, template));
checkHttpGet(node);
} finally {
client.destroyNodesMatching(inGroup(group));
}
}
@Test(enabled = true/* , dependsOnMethods = "testCompareSizes" */)
public void testTemplateOptions() throws Exception {
TemplateOptions options = new TemplateOptions().withMetadata();
Template t = client.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()) {
assert image.getProviderId() != null : image;
// image.getLocationId() can be null, if it is a location-free image
assertEquals(image.getType(), ComputeType.IMAGE);
}
}
@Test(groups = {"integration", "live"})
public void testGetAssignableLocations() throws Exception {
for (Location location : client.listAssignableLocations()) {
System.err.printf("location %s%n", location);
assert location.getId() != null : location;
assert location != location.getParent() : location;
assert location.getScope() != null : location;
switch (location.getScope()) {
case PROVIDER:
assertProvider(location);
break;
case REGION:
assertProvider(location.getParent());
break;
case ZONE:
Location provider = location.getParent().getParent();
// zone can be a direct descendant of provider
if (provider == null)
provider = location.getParent();
assertProvider(provider);
break;
case HOST:
Location provider2 = location.getParent().getParent().getParent();
// zone can be a direct descendant of provider
if (provider2 == null)
provider2 = location.getParent().getParent();
assertProvider(provider2);
break;
}
}
}
public void testOptionToNotBlock() throws Exception {
String group = this.group + "block";
try {
client.destroyNodesMatching(inGroup(group));
} catch (Exception e) {
}
// no inbound ports
TemplateOptions options = client.templateOptions().blockUntilRunning(false).inboundPorts();
try {
long time = System.currentTimeMillis();
Set<? extends NodeMetadata> nodes = client.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));
}
}
private void assertProvider(Location provider) {
assertEquals(provider.getScope(), LocationScope.PROVIDER);
assertEquals(provider.getParent(), null);
}
public void testListSizes() throws Exception {
for (Hardware hardware : client.listHardwareProfiles()) {
assert hardware.getProviderId() != null;
assert getCores(hardware) > 0;
assert hardware.getVolumes().size() >= 0;
assert hardware.getRam() > 0;
assertEquals(hardware.getType(), ComputeType.HARDWARE);
}
}
@Test(enabled = true)
public void testCompareSizes() throws Exception {
Hardware defaultSize = client.templateBuilder().build().getHardware();
Hardware smallest = client.templateBuilder().smallest().build().getHardware();
Hardware fastest = client.templateBuilder().fastest().build().getHardware();
Hardware biggest = client.templateBuilder().biggest().build().getHardware();
System.out.printf("smallest %s%n", smallest);
System.out.printf("fastest %s%n", fastest);
System.out.printf("biggest %s%n", biggest);
assertEquals(defaultSize, smallest);
assert getCores(smallest) <= getCores(fastest) : String.format("%d ! <= %d", smallest, fastest);
assert getCores(biggest) <= getCores(fastest) : String.format("%d ! <= %d", biggest, fastest);
assert biggest.getRam() >= fastest.getRam() : String.format("%d ! >= %d", biggest, fastest);
assert biggest.getRam() >= smallest.getRam() : String.format("%d ! >= %d", biggest, smallest);
assert getCores(fastest) >= getCores(biggest) : String.format("%d ! >= %d", fastest, biggest);
assert getCores(fastest) >= getCores(smallest) : String.format("%d ! >= %d", fastest, smallest);
}
private void sshPing(NodeMetadata node) throws IOException {
for (int i = 0; i < 5; i++) {// retry loop TODO replace with predicate.
try {
doCheckJavaIsInstalledViaSsh(node);
return;
} catch (SshException e) {
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e1) {
}
continue;
}
}
}
protected void doCheckJavaIsInstalledViaSsh(NodeMetadata node) throws IOException {
SshClient ssh = context.utils().sshForNode().apply(node);
try {
ssh.connect();
ExecResponse hello = ssh.exec("echo hello");
assertEquals(hello.getOutput().trim(), "hello");
ExecResponse exec = ssh.exec("java -version");
assert exec.getError().indexOf("1.6") != -1 || exec.getOutput().indexOf("1.6") != -1 : exec + "\n"
+ ssh.exec("cat /tmp/bootstrap/stdout.log /tmp/bootstrap/stderr.log");
} finally {
if (ssh != null)
ssh.disconnect();
}
}
@AfterTest
protected void cleanup() throws InterruptedException, ExecutionException, TimeoutException {
if (nodes != null) {
testDestroyNodes();
}
context.close();
} }
} }

View File

@ -0,0 +1,7 @@
test.nova.endpoint=http://172.18.34.40:8774
test.nova.apiversion=1.1
test.nova.identity=admin
test.nova.credential=d744752f-20d3-4d75-979f-f62f16033b07
test.initializer=
test.ssh.keyfile.private=f:/gigaspace/distr/gigaspaces.pem
test.ssh.keyfile.public=f:/gigaspace/distr/gigaspaces.pem