From 8c8161ec7962d5765da697c380072981ea90a7fb Mon Sep 17 00:00:00 2001 From: vicglarson Date: Fri, 22 Apr 2011 20:30:16 +0400 Subject: [PATCH] Live tests fix --- .../openstack/nova/NovaAsyncClientTest.java | 2 +- .../ParseServerFromJsonResponseTest.java | 52 +- .../DeleteServersInVariousStatesLiveTest.java | 1 + .../nova/live/NovaClientLiveTest.java | 150 +- .../nova/live/NovaComputeServiceLiveTest.java | 1403 +++++++++-------- 5 files changed, 793 insertions(+), 815 deletions(-) diff --git a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/NovaAsyncClientTest.java b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/NovaAsyncClientTest.java index 6bb67d04f8..6728c0acf1 100644 --- a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/NovaAsyncClientTest.java +++ b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/NovaAsyncClientTest.java @@ -385,7 +385,7 @@ public class NovaAsyncClientTest extends RestClientTest { Method method = NovaAsyncClient.class.getMethod("changeAdminPass", int.class, String.class); HttpRequest request = processor.createRequest(method, 2, "foo"); - assertRequestLineEquals(request, "POST http://endpoint/vapiversion/servers/2/action HTTP/1.1"); + assertRequestLineEquals(request, "POST http://endpoint/vapiversion/servers/2/action HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, "{\"changePassword\":{\"adminPass\":\"foo\"}}", MediaType.APPLICATION_JSON, false); diff --git a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseServerFromJsonResponseTest.java b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseServerFromJsonResponseTest.java index 1ff4c2d596..1d862030bc 100644 --- a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseServerFromJsonResponseTest.java +++ b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseServerFromJsonResponseTest.java @@ -18,31 +18,33 @@ */ package org.jclouds.openstack.nova.functions; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.Key; -import com.google.inject.TypeLiteral; -import org.jclouds.http.HttpResponse; -import org.jclouds.http.functions.UnwrapOnlyJsonValue; -import org.jclouds.io.Payloads; -import org.jclouds.json.config.GsonModule; -import org.jclouds.openstack.nova.domain.Address; -import org.jclouds.openstack.nova.domain.Addresses; -import org.jclouds.openstack.nova.domain.Server; -import org.jclouds.openstack.nova.domain.ServerStatus; -import org.testng.annotations.Test; - -import java.io.InputStream; -import java.net.UnknownHostException; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; -import java.util.SimpleTimeZone; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.TypeLiteral; +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.UnwrapOnlyJsonValue; +import org.jclouds.io.Payloads; +import org.jclouds.json.config.GsonModule; +import org.jclouds.openstack.nova.domain.Address; +import org.jclouds.openstack.nova.domain.Addresses; +import org.jclouds.openstack.nova.domain.Server; +import org.jclouds.openstack.nova.domain.ServerStatus; +import org.testng.annotations.Test; + +import java.io.InputStream; +import java.net.UnknownHostException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.SimpleTimeZone; + +import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals; diff --git a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/live/DeleteServersInVariousStatesLiveTest.java b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/live/DeleteServersInVariousStatesLiveTest.java index 3098c1f34b..280688e794 100644 --- a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/live/DeleteServersInVariousStatesLiveTest.java +++ b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/live/DeleteServersInVariousStatesLiveTest.java @@ -29,6 +29,7 @@ import org.jclouds.ssh.SshClient; import org.testng.annotations.AfterMethod; import org.testng.annotations.Test; +import java.security.SecureRandom; import java.util.Map; /** diff --git a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/live/NovaClientLiveTest.java b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/live/NovaClientLiveTest.java index e21623074e..5207c3a609 100644 --- a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/live/NovaClientLiveTest.java +++ b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/live/NovaClientLiveTest.java @@ -19,41 +19,24 @@ package org.jclouds.openstack.nova.live; -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.inject.Injector; -import com.google.inject.Module; import org.jclouds.domain.Credentials; import org.jclouds.http.HttpResponseException; import org.jclouds.io.Payload; -import org.jclouds.logging.slf4j.config.SLF4JLoggingModule; import org.jclouds.net.IPSocket; -import org.jclouds.openstack.nova.NovaClient; import org.jclouds.openstack.nova.domain.*; import org.jclouds.openstack.nova.options.RebuildServerOptions; -import org.jclouds.predicates.RetryablePredicate; -import org.jclouds.predicates.SocketOpen; -import org.jclouds.rest.RestContextFactory; 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.BeforeTest; import org.testng.annotations.Test; import java.io.IOException; -import java.security.SecureRandom; -import java.util.Map; -import java.util.Properties; import java.util.Set; -import java.util.concurrent.TimeUnit; -import static org.jclouds.openstack.nova.live.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.*; +import static org.testng.Assert.assertEquals; /** * Tests behavior of {@code NovaClient} @@ -62,9 +45,9 @@ import static org.testng.Assert.*; */ // disabled [Web Hosting #129069 @Test(groups = "live", sequential = true) -public class NovaClientLiveTest { +public class NovaClientLiveTest extends ClientBase{ - private int testImageId = 95; +<<<<<<< .mine private int testImageId = 95; protected NovaClient client; protected SshClient.Factory sshFactory; private Predicate socketTester; @@ -75,8 +58,8 @@ public class NovaClientLiveTest { private String adminPass; Map metadata = ImmutableMap.of("jclouds", "rackspace"); private int createdImageId; - - +=======>>>>>>> .theirs +<<<<<<< .mine @BeforeTest public void setupClient() throws IOException { Properties properties = setupOverrides(setupProperties(this.getClass())); @@ -95,7 +78,7 @@ public class NovaClientLiveTest { keyPair = setupKeyPair(properties); } - @Test +=======>>>>>>> .theirs @Test public void testListServers() throws Exception { Set response = client.listServers(); assert null != response; @@ -228,18 +211,21 @@ public class NovaClientLiveTest { } + + @Test(enabled = true) public void testCreateServer() throws Exception { - String imageRef = client.getImage(testImageId).getURI().toASCIIString(); +<<<<<<< .mine String imageRef = client.getImage(testImageId).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()); +======= Server server = getDefaultServerImmediately(); +>>>>>>> .theirs assertNotNull(server.getAdminPass()); assertEquals(server.getStatus(), ServerStatus.BUILD); - serverId = server.getId(); - adminPass = server.getAdminPass(); + int serverId = server.getId(); + String adminPass = server.getAdminPass(); blockUntilServerActive(serverId); blockUntilPublicAddress(serverId); client.getServer(serverId).getAddresses().getPublicAddresses().iterator().next().getAddress(); @@ -281,33 +267,34 @@ public class NovaClientLiveTest { @Test(enabled = true, timeOut = 300000) public void testServerDetails() throws Exception { - if (!isServerExist(serverId)) testCreateServer(); - Server server = client.getServer(serverId); - + Server server = getDefaultServerImmediately(); assertNotNull(server.getHostId(), "Host id: "); assertEquals(server.getStatus(), ServerStatus.ACTIVE); - assertNotNull(server.getAddresses()); // check metadata assertEquals(server.getMetadata(), metadata); - +<<<<<<< .mine assertTrue(server.getImageRef().endsWith(String.valueOf(testImageId))); - // listAddresses tests.. - assertEquals(client.getAddresses(serverId), server.getAddresses()); +======= assertTrue(server.getImageRef().endsWith(String.valueOf(testImageId))); +>>>>>>> .theirs // listAddresses tests.. + assertEquals(client.getAddresses(server.getId()), server.getAddresses()); assertEquals(server.getAddresses().getPublicAddresses().size(), 1); - assertEquals(client.listPublicAddresses(serverId), server.getAddresses().getPublicAddresses()); + assertEquals(client.listPublicAddresses(server.getId()), server.getAddresses().getPublicAddresses()); assertEquals(server.getAddresses().getPrivateAddresses().size(), 1); - assertEquals(client.listPrivateAddresses(serverId), server.getAddresses().getPrivateAddresses()); +<<<<<<< .mine assertEquals(client.listPrivateAddresses(serverId), server.getAddresses().getPrivateAddresses()); assertPassword(server, adminPass); assertTrue(server.getFlavorRef().endsWith("1")); - assert server.getProgress() >= 0 : "newDetails.getProgress()" + server.getProgress(); +======= assertEquals(client.listPrivateAddresses(server.getId()), server.getAddresses().getPrivateAddresses()); + assertPassword(server, server.getAdminPass()); + assertTrue(server.getFlavorRef().endsWith("1")); +>>>>>>> .theirs assert server.getProgress() >= 0 : "newDetails.getProgress()" + server.getProgress(); } private void assertPassword(Server server, String pass) throws IOException { IPSocket socket = new IPSocket(Iterables.get(server.getAddresses().getPublicAddresses(), 0).getAddress(), 22); - socketTester.apply(socket); + //socketTester.apply(socket); SshClient client = sshFactory.create(socket, new Credentials("root", keyPair.get("private"))); try { @@ -323,8 +310,8 @@ public class NovaClientLiveTest { @Test(enabled = true, timeOut = 5 * 60 * 1000) public void testRenameServer() throws Exception { - if (!isServerExist(serverId)) testCreateServer(); - Server server = client.getServer(serverId); + Server server = getDefaultServerImmediately(); + int serverId = server.getId(); String oldName = server.getName(); client.renameServer(serverId, oldName + "new"); blockUntilServerActive(serverId); @@ -333,49 +320,51 @@ public class NovaClientLiveTest { @Test(enabled = true, timeOut = 5 * 60 * 1000) public void testChangePassword() throws Exception { - if (!isServerExist(serverId)) testCreateServer(); + int serverId = getDefaultServerImmediately().getId(); blockUntilServerActive(serverId); client.changeAdminPass(serverId, "elmo"); assertPassword(client.getServer(serverId), "elmo"); - this.adminPass = "elmo"; + } @Test(enabled = true, timeOut = 10 * 600 * 1000) public void testCreateImage() throws Exception { - if (!isServerExist(serverId)) testCreateServer(); - Image image = client.createImageFromServer("hoofie", serverId); + Server server = getDefaultServerImmediately(); + Image image = getDefaultImageImmediately(server); + blockUntilImageActive(image.getId()); assertEquals("hoofie", image.getName()); - assertEquals(serverId, image.getServerRef()); - createdImageId = image.getId(); - blockUntilImageActive(createdImageId); + assertEquals(image.getServerRef(), ""); } + @Test(enabled = true, timeOut = 10 * 60 * 1000) public void testRebuildServer() throws Exception { - if (!isServerExist(serverId)) testCreateServer(); - client.rebuildServer(serverId, new RebuildServerOptions().withImage(String.valueOf(createdImageId))); - blockUntilServerActive(serverId); + Server server = getDefaultServerImmediately(); + Image image = getDefaultImageImmediately(server); + client.rebuildServer(server.getId(), new RebuildServerOptions().withImage(String.valueOf(image.getId()))); + blockUntilServerActive(server.getId()); // issue Web Hosting #119580 createdImageId comes back incorrect after rebuild - assertEquals(createdImageId, client.getServer(serverId).getImageRef()); + assertEquals(image.getURI(), client.getServer(server.getId()).getImageRef()); } @Test(enabled = true, timeOut = 10 * 60 * 1000) public void testRebootHard() throws Exception { - if (!isServerExist(serverId)) testCreateServer(); - client.rebootServer(serverId, RebootType.HARD); - blockUntilServerActive(serverId); + Server server = getDefaultServerImmediately(); + client.rebootServer(server.getId(), RebootType.HARD); + blockUntilServerActive(server.getId()); } @Test(enabled = true, timeOut = 10 * 60 * 1000) public void testRebootSoft() throws Exception { - if (!isServerExist(serverId)) testCreateServer(); - client.rebootServer(serverId, RebootType.SOFT); - blockUntilServerActive(serverId); + Server server = getDefaultServerImmediately(); + client.rebootServer(server.getId(), RebootType.SOFT); + blockUntilServerActive(server.getId()); } @Test(enabled = false, timeOut = 60000, dependsOnMethods = "testRebootSoft") public void testRevertResize() throws Exception { - if (!isServerExist(serverId)) testCreateServer(); + Server server = getDefaultServerImmediately(); + int serverId = server.getId(); client.resizeServer(serverId, 2); blockUntilServerVerifyResize(serverId); client.revertResizeServer(serverId); @@ -385,7 +374,8 @@ public class NovaClientLiveTest { @Test(enabled = false, timeOut = 10 * 60 * 1000) public void testConfirmResize() throws Exception { - if (!isServerExist(serverId)) testCreateServer(); + Server server = getDefaultServerImmediately(); + int serverId = server.getId(); client.resizeServer(serverId, 2); blockUntilServerVerifyResize(serverId); client.confirmResizeServer(serverId); @@ -395,45 +385,25 @@ public class NovaClientLiveTest { @Test(enabled = true, timeOut = 60000) void deleteServer2() throws Exception { - if (!isServerExist(serverId)) testCreateServer(); - if (serverId > 0) { - client.deleteServer(serverId); - waitServerDeleted(serverId); - } - serverId = 0; + Server server = getDefaultServerImmediately(); + int serverId = server.getId(); + client.deleteServer(serverId); + waitServerDeleted(serverId); } @Test(enabled = true, timeOut = 60000) void testDeleteImage() throws Exception { - if (createdImageId <= 0) { - testCreateImage(); - } - if (createdImageId > 0) { - client.deleteImage(createdImageId); - assert client.getImage(createdImageId) == null; - } - //TODO all servers created from the image should be deleted + Image image = getDefaultImageImmediately(getDefaultServerImmediately()); + client.deleteImage(image.getId()); + assert client.getImage(image.getId()) == null; } @Test(enabled = true, timeOut = 60000) void deleteServer1() throws Exception { - 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)) { - System.out.println("Await deleted server" + serverId); - Thread.sleep(1000); - } + Server server = getDefaultServerImmediately(); + int serverId = server.getId(); + client.deleteServer(serverId); + waitServerDeleted(serverId); } @Test diff --git a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/live/NovaComputeServiceLiveTest.java b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/live/NovaComputeServiceLiveTest.java index 76599ad707..40fddcb043 100644 --- a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/live/NovaComputeServiceLiveTest.java +++ b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/live/NovaComputeServiceLiveTest.java @@ -1,699 +1,704 @@ -/** - * - * Copyright (C) 2011 Cloud Conscious, LLC. - * - * ==================================================================== - * 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.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.*; -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.http.handlers.BackoffLimitedRetryHandler; -import org.jclouds.logging.slf4j.config.SLF4JLoggingModule; -import org.jclouds.net.IPSocket; -import org.jclouds.openstack.nova.NovaAsyncClient; -import org.jclouds.openstack.nova.NovaClient; -import org.jclouds.predicates.RetryablePredicate; -import org.jclouds.predicates.SocketOpen; -import org.jclouds.rest.AuthorizationException; -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.JschSshClient; -import org.jclouds.ssh.jsch.config.JschSshClientModule; -import org.testng.annotations.AfterTest; -import org.testng.annotations.BeforeGroups; -import org.testng.annotations.Test; - -import java.io.IOException; -import java.net.URISyntaxException; -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.live.PropertyHelper.overridePropertyFromSystemProperty; -import static org.jclouds.openstack.nova.live.PropertyHelper.setupKeyPair; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; - -/** - * Generally disabled, as it incurs higher fees. - * - * @author Adrian Cole - */ -@Test(groups = "novalive", enabled = true, sequential = true) -public class NovaComputeServiceLiveTest { - - protected String group; - - protected RetryablePredicate socketTester; - protected SortedSet nodes; - protected ComputeServiceContext context; - protected ComputeService computeService; - - - protected Map keyPair; - - protected String provider; - protected String identity; - protected String credential; - protected String endpoint; - protected String apiversion; - - private Properties overrides; - - public NovaComputeServiceLiveTest() { - provider = "nova"; - } - - - 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(); - 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.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 SLF4JLoggingModule(), getSshModule()), properties); - computeService = context.getComputeService(); - } - - - @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(socketOpen, 60, 1, TimeUnit.SECONDS); - } - - - protected JschSshClientModule getSshModule() { - return new JschSshClientModule(); - } - - @Test - public void testAssignability() throws Exception { - @SuppressWarnings("unused") - RestContext tmContext = new ComputeServiceContextFactory() - .createContext(provider, identity, credential, Collections.singleton(new JschSshClientModule()), overrides).getProviderSpecificContext(); - } - - - protected void checkNodes(Iterable nodes, String tag) throws IOException { - _checkNodes(nodes, tag); - - for (NodeMetadata node : nodes) { - assertEquals(node.getLocation().getScope(), LocationScope.HOST); - } - } - - protected void _checkNodes(Iterable 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); -// doCheckJavaIsInstalledViaSsh(node); -// } - } - } - - - @Test(enabled = true, expectedExceptions = AuthorizationException.class) - public void testCorrectAuthException() throws Exception { - Properties properties = new Properties(); - properties.putAll(overrides); - properties.remove(provider + ".identity"); - ComputeServiceContext context = null; - try { - context = new ComputeServiceContextFactory(setupRestProperties()).createContext(provider, "MOMMA", "MIA", ImmutableSet - .of(new SLF4JLoggingModule()), properties); - context.getComputeService().listNodes(); - } finally { - if (context != null) - context.close(); - } - } - - @Test(enabled = true) - public void testImagesCache() throws Exception { - computeService.listImages(); - long time = System.currentTimeMillis(); - 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 { - 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.. - private String awaitForPublicAddressAssigned(String nodeId) throws InterruptedException { - while (true) { - Set addresses = computeService.getNodeMetadata(nodeId).getPublicAddresses(); - System.out.println(addresses); - if (addresses != null) - if (!addresses.isEmpty()) return addresses.iterator().next(); - Thread.sleep(1000); - } - } - - private void awaitForSshPort(String address, Credentials credentials) throws URISyntaxException { - IPSocket socket = new IPSocket(address, 22); - - JschSshClient ssh = new JschSshClient( - new BackoffLimitedRetryHandler(), socket, 10000, credentials.identity, null, credentials.credential.getBytes()); - while (true) { - try { - System.out.println("ping: " + socket); - ssh.connect(); - return; - } catch (SshException ignore) { - } - } - } - - - @Test(enabled = true) - public void testAScriptExecutionAfterBootWithBasicTemplate() throws Exception { - String group = this.group + "r"; - - computeService.destroyNodesMatching(inGroup(group)); - - Template template = getDefaultTemplateBuilder().options( - computeService.templateOptions() - .overrideCredentialsWith(new Credentials("root", keyPair.get("private"))) - .blockUntilRunning(true)) - .build(); - - try { - Set nodes = computeService.createNodesInGroup(group, 1, template); - - System.out.println("=================================================="); - System.out.println("================ Created ==================="); - - String address = awaitForPublicAddressAssigned(get(nodes, nodes.size() - 1).getId()); - awaitForSshPort(address, new Credentials("root", keyPair.get("private"))); - - OperatingSystem os = get(nodes, 0).getOperatingSystem(); - try { - Map responses = runJavaInstallationScriptWithCreds(group, os, new Credentials( - "root", "romeo")); - assert false : "shouldn't pass with a bad password\n" + responses; - } catch (RunScriptOnNodesException ignore) { - if (!getRootCause(ignore).getMessage().contains("Auth fail")) throw ignore; - } - - System.out.println("=================================================="); - System.out.println("================ Auth failed ==================="); - - for (Map.Entry response : computeService.runScriptOnNodesMatching( - runningInGroup(group), Statements.exec("echo hello"), - overrideCredentialsWith(new Credentials("root", keyPair.get("private"))).wrapInInitScript(false).runAsRoot(false)).entrySet()) - assert response.getValue().getOutput().trim().equals("hello") : response.getKey() + ": " - + response.getValue(); - - System.out.println("=================================================="); - System.out.println("================ Script ==================="); - - //TODO runJavaInstallationScriptWithCreds(group, os, new Credentials("root", keyPair.get("private"))); - //no response? if os is null (ZYPPER) - - checkNodes(nodes, group); - - Credentials good = nodes.iterator().next().getCredentials(); - //TODO check good is being private key .overrideCredentialsWith - //TODO test for .blockOnPort - - } finally { - computeService.destroyNodesMatching(inGroup(group)); - } - } - - @Test(enabled = true) - public void testTemplateMatch() throws Exception { - Template template = buildTemplate(getDefaultTemplateBuilder()); - Template toMatch = computeService.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 { - computeService.destroyNodesMatching(inGroup(group)); - } catch (NoSuchElementException e) { - - } - - try { - nodes = newTreeSet(computeService.createNodesInGroup(group, 2, getDefaultTemplateBuilder().build())); - } 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(), getDefaultTemplateBuilder().build().getLocation()); - assertLocationSameOrChild(node2.getLocation(), getDefaultTemplateBuilder().build().getLocation()); - checkImageIdMatchesTemplate(node1); - checkImageIdMatchesTemplate(node2); - checkOsMatchesTemplate(node1); - checkOsMatchesTemplate(node2); - } - - protected void checkImageIdMatchesTemplate(NodeMetadata node) { - if (node.getImageId() != null) - assertEquals(node.getImageId(), getDefaultTemplateBuilder().build().getImage().getId()); - } - - protected void checkOsMatchesTemplate(NodeMetadata node) { - if (node.getOperatingSystem() != null) - assert node.getOperatingSystem().getFamily().equals(getDefaultTemplateBuilder().build().getImage().getOperatingSystem().getFamily()) : String - .format("expecting family %s but got %s", getDefaultTemplateBuilder().build().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) - public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired() throws Exception { - testCreateTwoNodesWithRunScript(); - initializeContextAndClient(overrides); - - TreeSet nodes = newTreeSet(computeService.createNodesInGroup(group, 1, getDefaultTemplateBuilder().build())); - checkNodes(nodes, group); - NodeMetadata node = nodes.first(); - this.nodes.add(node); - assertEquals(nodes.size(), 1); - assertLocationSameOrChild(node.getLocation(), getDefaultTemplateBuilder().build().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 runJavaInstallationScriptWithCreds(final String group, OperatingSystem os, - Credentials creds) throws RunScriptOnNodesException { - return computeService.runScriptOnNodesMatching(runningInGroup(group), buildScript(os), overrideCredentialsWith(creds) - .nameTask("runJavaInstallationScriptWithCreds")); - - } - - - protected Template buildTemplate(TemplateBuilder templateBuilder) { - return templateBuilder.build(); - } - - @Test(enabled = true, dependsOnMethods = "testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired") - public void testGet() throws Exception { - Map metadataMap = newLinkedHashMap(uniqueIndex(filter(computeService - .listNodesDetailsMatching(all()), and(inGroup(group), not(TERMINATED))), - new Function() { - - @Override - public String apply(NodeMetadata from) { - return from.getId(); - } - - })); - for (NodeMetadata node : nodes) { - metadataMap.remove(node.getId()); - NodeMetadata metadata = computeService.getNodeMetadata(node.getId()); - assertEquals(metadata.getProviderId(), node.getProviderId()); - assertEquals(metadata.getGroup(), node.getGroup()); - assertLocationSameOrChild(metadata.getLocation(), getDefaultTemplateBuilder().build().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 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 { - computeService.rebootNodesMatching(inGroup(group));// TODO test - // validation - testGet(); - } - - @Test(enabled = true, dependsOnMethods = "testReboot") - public void testSuspendResume() throws Exception { - computeService.suspendNodesMatching(inGroup(group)); - - Set stoppedNodes = refreshNodes(); - - assert Iterables.all(stoppedNodes, new Predicate() { - - @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; - - computeService.resumeNodesMatching(inGroup(group)); - testGet(); - } - - @Test(enabled = true, dependsOnMethods = "testSuspendResume") - public void testListNodes() throws Exception { - for (ComputeMetadata node : computeService.listNodes()) { - assert node.getProviderId() != null; - assert node.getLocation() != null; - assertEquals(node.getType(), ComputeType.NODE); - } - } - - @Test(enabled = true, dependsOnMethods = "testSuspendResume") - public void testGetNodesWithDetails() throws Exception { - for (NodeMetadata node : computeService.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() { - int toDestroy = refreshNodes().size(); - Set destroyed = computeService.destroyNodesMatching(inGroup(group)); - assertEquals(toDestroy, destroyed.size()); - 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 refreshNodes() { - return filter(computeService.listNodesDetailsMatching(all()), and(inGroup(group), not(TERMINATED))); - } - - @Test(enabled = true) - public void testCreateAndRunAService() throws Exception { - - String group = this.group + "s"; - try { - computeService.destroyNodesMatching(inGroup(group)); - } catch (Exception e) { - - } - - Template template = getDefaultTemplateBuilder().options(blockOnComplete(false).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(computeService.createNodesInGroup(group, 1, template)); - - //checkHttpGet(node); - } finally { - computeService.destroyNodesMatching(inGroup(group)); - } - - } - - @Test(enabled = true) - public void testTemplateOptions() throws Exception { - TemplateOptions options = new TemplateOptions().withMetadata(); - 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 : computeService.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 : computeService.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 { - computeService.destroyNodesMatching(inGroup(group)); - } catch (Exception e) { - - } - // no inbound ports - TemplateOptions options = computeService.templateOptions().blockUntilRunning(false).inboundPorts(); - try { - long time = System.currentTimeMillis(); - Set 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 { - computeService.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 : computeService.listHardwareProfiles()) { - assert hardware.getProviderId() != null; - assert getCores(hardware) > 0; - assert hardware.getVolumes().size() >= 0; - assert hardware.getRam() > 0; - assertEquals(hardware.getType(), ComputeType.HARDWARE); - } - } - - private TemplateBuilder getDefaultTemplateBuilder() { - return computeService.templateBuilder().imageId("95"); - } - - @Test(enabled = true) - public void testCompareSizes() throws Exception { - TemplateBuilder templateBuilder = getDefaultTemplateBuilder(); - - 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); - 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); - } - - - 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(); - } -} +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * 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.Function; +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.common.collect.Sets; +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.http.handlers.BackoffLimitedRetryHandler; +import org.jclouds.io.Payload; +import org.jclouds.io.Payloads; +import org.jclouds.logging.slf4j.config.SLF4JLoggingModule; +import org.jclouds.net.IPSocket; +import org.jclouds.openstack.nova.NovaAsyncClient; +import org.jclouds.openstack.nova.NovaClient; +import org.jclouds.predicates.RetryablePredicate; +import org.jclouds.predicates.SocketOpen; +import org.jclouds.rest.AuthorizationException; +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.JschSshClient; +import org.jclouds.ssh.jsch.config.JschSshClientModule; +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeGroups; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.Test; + +import java.io.IOException; +import java.net.URISyntaxException; +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.live.PropertyHelper.overridePropertyFromSystemProperty; +import static org.jclouds.openstack.nova.live.PropertyHelper.setupKeyPair; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; + +/** + * Generally disabled, as it incurs higher fees. + * + * @author Adrian Cole + */ +@Test(groups = "novalive", enabled = true, sequential = true) +public class NovaComputeServiceLiveTest { + + protected String group; + + protected RetryablePredicate socketTester; + protected ComputeServiceContext context; + protected ComputeService computeService; + + + protected Map keyPair; + + protected String provider; + protected String identity; + protected String credential; + protected String endpoint; + protected String apiversion; + + private Properties overrides; + + public NovaComputeServiceLiveTest() { + provider = "nova"; + } + + + 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(); + 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.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 SLF4JLoggingModule(), getSshModule()), properties); + computeService = context.getComputeService(); + } + + + @BeforeTest + public void setupClient() throws InterruptedException, ExecutionException, TimeoutException, IOException { + group = "compute service test 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(socketOpen, 60, 1, TimeUnit.SECONDS); + } + + + protected JschSshClientModule getSshModule() { + return new JschSshClientModule(); + } + + @Test + public void testAssignability() throws Exception { + @SuppressWarnings("unused") + RestContext tmContext = new ComputeServiceContextFactory() + .createContext(provider, identity, credential, Collections.singleton(new JschSshClientModule()), overrides).getProviderSpecificContext(); + } + + + protected void checkNodes(Iterable nodes, String tag) throws IOException { + _checkNodes(nodes, tag); + + for (NodeMetadata node : nodes) { + assertEquals(node.getLocation().getScope(), LocationScope.HOST); + } + } + + protected void _checkNodes(Iterable 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); +// doCheckJavaIsInstalledViaSsh(node); +// } + } + } + + + @Test(enabled = true, expectedExceptions = AuthorizationException.class) + public void testCorrectAuthException() throws Exception { + Properties properties = new Properties(); + properties.putAll(overrides); + properties.remove(provider + ".identity"); + ComputeServiceContext context = null; + try { + context = new ComputeServiceContextFactory(setupRestProperties()).createContext(provider, "MOMMA", "MIA", ImmutableSet + .of(new SLF4JLoggingModule()), properties); + context.getComputeService().listNodes(); + } finally { + if (context != null) + context.close(); + } + } + + @Test(enabled = true) + public void testImagesCache() throws Exception { + computeService.listImages(); + long time = System.currentTimeMillis(); + 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 { + 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.. + private String awaitForPublicAddressAssigned(String nodeId) throws InterruptedException { + while (true) { + Set addresses = computeService.getNodeMetadata(nodeId).getPublicAddresses(); + System.out.println(addresses); + if (addresses != null) + if (!addresses.isEmpty()) return addresses.iterator().next(); + Thread.sleep(1000); + } + } + + private void awaitForSshPort(String address, Credentials credentials) throws URISyntaxException { + IPSocket socket = new IPSocket(address, 22); + + JschSshClient ssh = new JschSshClient( + new BackoffLimitedRetryHandler(), socket, 10000, credentials.identity, null, credentials.credential.getBytes()); + while (true) { + try { + System.out.println("ping: " + socket); + ssh.connect(); + return; + } catch (SshException ignore) { + } + } + } + + + @Test(enabled = true) + public void testAScriptExecutionAfterBootWithBasicTemplate() throws Exception { + String group = this.group; + + computeService.destroyNodesMatching(inGroup(group)); + + Template template = getDefaultTemplateBuilder().options( + computeService.templateOptions() + .overrideCredentialsWith(new Credentials("root", keyPair.get("private"))) + .blockUntilRunning(true)) + .build(); + + try { + Set nodes = computeService.createNodesInGroup(group, 1, template); + + System.out.println("=================================================="); + System.out.println("================ Created ==================="); + + String address = awaitForPublicAddressAssigned(get(nodes, nodes.size() - 1).getId()); + awaitForSshPort(address, new Credentials("root", keyPair.get("private"))); + + OperatingSystem os = get(nodes, 0).getOperatingSystem(); + try { + Map responses = runJavaInstallationScriptWithCreds(group, os, new Credentials( + "root", "romeo")); + assert false : "shouldn't pass with a bad password\n" + responses; + } catch (RunScriptOnNodesException ignore) { + if (!getRootCause(ignore).getMessage().contains("Auth fail")) throw ignore; + } + + System.out.println("=================================================="); + System.out.println("================ Auth failed ==================="); + + for (Map.Entry response : computeService.runScriptOnNodesMatching( + runningInGroup(group), Statements.exec("echo hello"), + overrideCredentialsWith(new Credentials("root", keyPair.get("private"))).wrapInInitScript(false).runAsRoot(false)).entrySet()) + assert response.getValue().getOutput().trim().equals("hello") : response.getKey() + ": " + + response.getValue(); + + System.out.println("=================================================="); + System.out.println("================ Script ==================="); + + //TODO runJavaInstallationScriptWithCreds(group, os, new Credentials("root", keyPair.get("private"))); + //TODO no response? if os is null (ZYPPER) + + checkNodes(nodes, group); + + Credentials good = nodes.iterator().next().getCredentials(); + //TODO check good is being private key .overrideCredentialsWith + //TODO test for .blockOnPort + + } finally { + computeService.destroyNodesMatching(inGroup(group)); + } + } + + @Test(enabled = true) + public void testTemplateMatch() throws Exception { + Template template = buildTemplate(getDefaultTemplateBuilder()); + Template toMatch = computeService.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) + public void testCreateTwoNodesWithRunScript() throws Exception { + computeService.destroyNodesMatching(inGroup(group)); + + SortedSet nodes = newTreeSet(computeService.createNodesInGroup(group, 2, getDefaultTemplateBuilder().build())); + 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(), getDefaultTemplateBuilder().build().getLocation()); + assertLocationSameOrChild(node2.getLocation(), getDefaultTemplateBuilder().build().getLocation()); + checkImageIdMatchesTemplate(node1); + checkImageIdMatchesTemplate(node2); +// checkOsMatchesTemplate(node1); +// checkOsMatchesTemplate(node2); + } + + protected void checkImageIdMatchesTemplate(NodeMetadata node) { + if (node.getImageId() != null) + assertEquals(node.getImageId(), getDefaultTemplateBuilder().build().getImage().getId()); + } + +// protected void checkOsMatchesTemplate(NodeMetadata node) { +// if (node.getOperatingSystem() != null) +// assert node.getOperatingSystem().getFamily().equals(getDefaultTemplateBuilder().build().getImage().getOperatingSystem().getFamily()) : String +// .format("expecting family %s but got %s", getDefaultTemplateBuilder().build().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); + } + } + + private NodeMetadata createDefaultNode() throws RunNodesException { + return computeService.createNodesInGroup(group, 1, getDefaultTemplateBuilder().build()) + .iterator().next(); + } + + @Test(enabled = true) + public void testCreateAnotherNodeWithANewContextToEnsureSharedMemIsntRequired() throws Exception { + testCreateTwoNodesWithRunScript(); + initializeContextAndClient(overrides); + + TreeSet nodes = newTreeSet(computeService.createNodesInGroup(group, 1, getDefaultTemplateBuilder().build())); + checkNodes(nodes, group); + NodeMetadata node = nodes.first(); + assertEquals(nodes.size(), 1); + assertLocationSameOrChild(node.getLocation(), getDefaultTemplateBuilder().build().getLocation()); +// checkOsMatchesTemplate(node); + } + + @Test(enabled = true) + public void testCredentialsCache() throws Exception { + LinkedList nodes = new LinkedList(); + nodes.add(createDefaultNode()); + initializeContextAndClient(overrides); + nodes.add(createDefaultNode()); + initializeContextAndClient(overrides); + for (NodeMetadata node : nodes) + assert (context.getCredentialStore().get("node#" + node.getId()) != null) : "credentials for " + node.getId(); + } + + protected Map runJavaInstallationScriptWithCreds(final String group, OperatingSystem os, + Credentials creds) throws RunScriptOnNodesException { + return computeService.runScriptOnNodesMatching(runningInGroup(group), buildScript(os), overrideCredentialsWith(creds) + .nameTask("runJavaInstallationScriptWithCreds")); + + } + + + protected Template buildTemplate(TemplateBuilder templateBuilder) { + return templateBuilder.build(); + } + + @Test(enabled = true) + public void testGet() throws Exception { + Set nodes = Sets.newHashSet(createDefaultNode()); + Map metadataMap = newLinkedHashMap(uniqueIndex(filter(computeService + .listNodesDetailsMatching(all()), and(inGroup(group), not(TERMINATED))), + new Function() { + + @Override + public String apply(NodeMetadata from) { + return from.getId(); + } + + })); + for (NodeMetadata node : nodes) { + metadataMap.remove(node.getId()); + NodeMetadata metadata = computeService.getNodeMetadata(node.getId()); + assertEquals(metadata.getProviderId(), node.getProviderId()); + assertEquals(metadata.getGroup(), node.getGroup()); + assertLocationSameOrChild(metadata.getLocation(), getDefaultTemplateBuilder().build().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(), nodes); + } + + protected void assertNodeZero(Collection metadataSet, Set nodes) { + assert metadataSet.size() == 0 : String.format("nodes left in set: [%s] which didn't match set: [%s]", + metadataSet, nodes); + } + + @Test(enabled = true) + public void testReboot() throws Exception { + computeService.rebootNodesMatching(inGroup(group));// TODO test + // validation + testGet(); + } + + @Test(enabled = true) + public void testSuspendResume() throws Exception { + computeService.suspendNodesMatching(inGroup(group)); + + Set stoppedNodes = refreshNodes(); + + assert Iterables.all(stoppedNodes, new Predicate() { + + @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; + + computeService.resumeNodesMatching(inGroup(group)); + testGet(); + } + + @Test(enabled = true) + public void testListNodes() throws Exception { + for (ComputeMetadata node : computeService.listNodes()) { + assert node.getProviderId() != null; + assert node.getLocation() != null; + assertEquals(node.getType(), ComputeType.NODE); + } + } + + @Test(enabled = true) + public void testGetNodesWithDetails() throws Exception { + for (NodeMetadata node : computeService.listNodesDetailsMatching(all())) { + assert node.getProviderId() != null : node; + assert node.getLocation() != null : node; + assertEquals(node.getType(), ComputeType.NODE); + assert node.getProviderId() != null : node; + // nullable + // assert nodeMetadata.getImage() != null : node; + // user specified name is not always supported + // assert nodeMetadata.getName() != null : nodeMetadata; + if (node.getState() == NodeState.RUNNING) { + assert node.getPublicAddresses() != null : node; + assert node.getPublicAddresses().size() > 0 || node.getPrivateAddresses().size() > 0 : node; + assertNotNull(node.getPrivateAddresses()); + } + } + } + + @Test(enabled = true) + public void testDestroyNodes() { + int toDestroy = refreshNodes().size(); + Set destroyed = computeService.destroyNodesMatching(inGroup(group)); + assertEquals(toDestroy, destroyed.size()); + 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 refreshNodes() { + return filter(computeService.listNodesDetailsMatching(all()), and(inGroup(group), not(TERMINATED))); + } + + @Test(enabled = true) + public void testCreateAndRunAService() throws Exception { + + String group = this.group; + try { + computeService.destroyNodesMatching(inGroup(group)); + } catch (Exception e) { + + } + + Template template = getDefaultTemplateBuilder().options(blockOnComplete(false).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(computeService.createNodesInGroup(group, 1, template)); + + //checkHttpGet(node); + } finally { + computeService.destroyNodesMatching(inGroup(group)); + } + + } + + @Test(enabled = true) + public void testTemplateOptions() throws Exception { + TemplateOptions options = new TemplateOptions().withMetadata(); + 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 : computeService.listImages()) { + assert image.getProviderId() != null : image; + // image.getLocationId() can be null, if it is a location-free image + assertEquals(image.getType(), ComputeType.IMAGE); + } + } + + @Test + public void testGetAssignableLocations() throws Exception { + for (Location location : computeService.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; + try { + computeService.destroyNodesMatching(inGroup(group)); + } catch (Exception e) { + + } + // no inbound ports + TemplateOptions options = computeService.templateOptions().blockUntilRunning(false).inboundPorts(); + try { + long time = System.currentTimeMillis(); + Set 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 { + computeService.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 : computeService.listHardwareProfiles()) { + assert hardware.getProviderId() != null; + assert getCores(hardware) > 0; + assert hardware.getVolumes().size() >= 0; + assert hardware.getRam() > 0; + assertEquals(hardware.getType(), ComputeType.HARDWARE); + } + } + + private TemplateBuilder getDefaultTemplateBuilder() { + return computeService.templateBuilder().imageId("95").options(getDefaultTemplateOptions()); + } + + private TemplateOptions getDefaultTemplateOptions() { + return TemplateOptions.Builder.installPrivateKey(Payloads.newStringPayload(keyPair.get("private"))); + } + + + + + + @Test(enabled = true) + public void testCompareSizes() throws Exception { + TemplateBuilder templateBuilder = getDefaultTemplateBuilder(); + + 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); + System.out.printf("biggest %s%n", biggest); + + assertEquals(defaultSize, smallest); + + assert getCores(smallest) <= getCores(fastest); + assert getCores(biggest) <= getCores(fastest); + + assert biggest.getRam() >= fastest.getRam(); + assert biggest.getRam() >= smallest.getRam(); + + assert getCores(fastest) >= getCores(biggest); + assert getCores(fastest) >= getCores(smallest); + } + + + 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 { + testDestroyNodes(); + context.close(); + } +}