diff --git a/sandbox-apis/nova/pom.xml b/sandbox-apis/nova/pom.xml index 13e3dda25b..4c7d2d609f 100644 --- a/sandbox-apis/nova/pom.xml +++ b/sandbox-apis/nova/pom.xml @@ -21,7 +21,7 @@ --> + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4.0.0.xsd"> 4.0.0 org.jclouds @@ -42,6 +42,11 @@ + + org.jclouds.provider + aws-ec2 + ${project.version} + org.jclouds.common openstack-common @@ -131,6 +136,14 @@ test.nova.credential ${test.nova.credential} + + test.nova.credential + ${test.ssh.keyfile.public} + + + test.nova.credential + ${test.ssh.keyfile.private} + test.initializer ${test.initializer} diff --git a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/NovaClientLiveTest.java b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/NovaClientLiveTest.java index abbd1a4d0a..e23449bd10 100644 --- a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/NovaClientLiveTest.java +++ b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/NovaClientLiveTest.java @@ -18,40 +18,21 @@ */ package org.jclouds.openstack.nova; -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.assertEquals; -import static org.testng.Assert.assertNotNull; -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 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.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.domain.Credentials; import org.jclouds.http.HttpResponseException; import org.jclouds.io.Payload; import org.jclouds.logging.log4j.config.Log4JLoggingModule; 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.SocketOpen; import org.jclouds.rest.RestContextFactory; @@ -63,576 +44,613 @@ import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; -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 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 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} - * + * * @author Adrian Cole */ // disabled [Web Hosting #129069 @Test(groups = "live", sequential = true) public class NovaClientLiveTest { - protected NovaClient client; - protected SshClient.Factory sshFactory; - private Predicate socketTester; - protected String provider = "nova"; - protected String identity; - protected String credential; - protected String endpoint; - protected String apiversion; + protected NovaClient client; + protected SshClient.Factory sshFactory; + private Predicate socketTester; + protected String provider = "nova"; + protected String identity; + protected String credential; + protected String endpoint; + protected String apiversion; - protected void setupCredentials() { - identity = checkNotNull(System.getProperty("test." + provider + ".identity"), "test." + provider + ".identity"); - credential = checkNotNull(System.getProperty("test." + provider + ".credential"), "test." + provider - + ".credential"); - endpoint = System.getProperty("test." + provider + ".endpoint"); - apiversion = System.getProperty("test." + provider + ".apiversion"); - } + 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 Properties setupProperties() { - Properties overrides = new Properties(); - overrides.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "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; - } - @BeforeGroups(groups = { "live" }) - public void setupClient() { - setupCredentials(); - Properties overrides = 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"); + } - Injector injector = new RestContextFactory().createContextBuilder(provider, - ImmutableSet. of(new Log4JLoggingModule(), new JschSshClientModule()), overrides) - .buildInjector(); + 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); + } - client = injector.getInstance(NovaClient.class); - sshFactory = injector.getInstance(SshClient.Factory.class); - SocketOpen socketOpen = injector.getInstance(SocketOpen.class); - socketTester = new RetryablePredicate(socketOpen, 120, 1, TimeUnit.SECONDS); - injector.injectMembers(socketOpen); // add logger - } + protected Properties setupProperties() throws IOException { + Properties overrides = new Properties(); + overrides.load(this.getClass().getResourceAsStream("/test.properties")); + overridePropertyFromSystemProperty(overrides, "test." + provider + ".endpoint"); + overridePropertyFromSystemProperty(overrides, "test." + provider + ".apiversion"); + overridePropertyFromSystemProperty(overrides, "test." + provider + ".identity"); + overridePropertyFromSystemProperty(overrides, "test." + provider + ".credential"); + overridePropertyFromSystemProperty(overrides, "test.initializer"); + overrides.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true"); + overrides.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true"); - public void testListServers() throws Exception { + return overrides; + } - Set response = client.listServers(); - assert null != response; - long initialContainerCount = response.size(); - assertTrue(initialContainerCount >= 0); + @BeforeGroups(groups = {"live"}) + public void setupClient() throws IOException { + Properties overrides = setupProperties(); + setupCredentials(overrides); + updateProperties(overrides); - } + String identity = "admin"; + String credential = "d744752f-20d3-4d75-979f-f62f16033b07"; +// ComputeServiceContextFactory contextFactory = new ComputeServiceContextFactory(); +// ComputeServiceContext context = contextFactory.createContext(provider, identity, credential, Collections.singleton(new JschSshClientModule()), overrides); - public void testListServersDetail() throws Exception { - Set response = client.listServers(withDetails()); - assert null != response; - long initialContainerCount = response.size(); - assertTrue(initialContainerCount >= 0); - } + Injector injector = new RestContextFactory().createContextBuilder(provider, identity, credential, + ImmutableSet.of(new Log4JLoggingModule(), new JschSshClientModule()), overrides) + .buildInjector(); - public void testListImages() throws Exception { - Set response = client.listImages(); - assert null != response; - long imageCount = response.size(); - assertTrue(imageCount >= 1); - for (Image image : response) { - assertTrue(image.getId() >= 0); - assert null != image.getName() : image; - } + client = injector.getInstance(NovaClient.class); - } + sshFactory = injector.getInstance(SshClient.Factory.class); + SocketOpen socketOpen = injector.getInstance(SocketOpen.class); + socketTester = new RetryablePredicate(socketOpen, 120, 1, TimeUnit.SECONDS); + injector.injectMembers(socketOpen); // add logger + } - public void testListImagesDetail() throws Exception { - Set response = client.listImages(withDetails()); - assert null != response; - long imageCount = response.size(); - assertTrue(imageCount >= 0); - for (Image image : response) { - assertTrue(image.getId() >= 1); - assert null != image.getName() : image; - assert null != image.getStatus() : image; - } - } + @Test + public void testListServers() throws Exception { - public void testGetImagesDetail() throws Exception { - Set response = client.listImages(withDetails()); - assert null != response; - long imageCount = response.size(); - assertTrue(imageCount >= 0); - for (Image image : response) { - try { - Image newDetails = client.getImage(image.getId()); - assertEquals(image, newDetails); - } catch (HttpResponseException e) {// Ticket #9867 - if (e.getResponse().getStatusCode() != 400) - throw e; - } - } - } + Set response = client.listServers(); + assert null != response; + long initialContainerCount = response.size(); + assertTrue(initialContainerCount >= 0); - @Test - public void testGetImageDetailsNotFound() throws Exception { - assert client.getImage(12312987) == null; - } + } - @Test - public void testGetServerDetailsNotFound() throws Exception { - assert client.getServer(12312987) == null; - } + @Test + public void testListServersDetail() throws Exception { + Set response = client.listServers(withDetails()); + assert null != response; + long initialContainerCount = response.size(); + assertTrue(initialContainerCount >= 0); + } - public void testGetServersDetail() throws Exception { - Set response = client.listServers(withDetails()); - assert null != response; - long serverCount = response.size(); - assertTrue(serverCount >= 0); - for (Server server : response) { - Server newDetails = client.getServer(server.getId()); - assertEquals(server, newDetails); - } - } + @Test + public void testListImages() throws Exception { + Set response = client.listImages(); + assert null != response; + long imageCount = response.size(); + assertTrue(imageCount >= 1); + for (Image image : response) { + assertTrue(image.getId() >= 0); + assert null != image.getName() : image; + } - public void testListFlavors() throws Exception { - Set response = client.listFlavors(); - assert null != response; - long flavorCount = response.size(); - assertTrue(flavorCount >= 1); - for (Flavor flavor : response) { - assertTrue(flavor.getId() >= 0); - assert null != flavor.getName() : flavor; - } + } - } + @Test + public void testListImagesDetail() throws Exception { + Set response = client.listImages(withDetails()); + assert null != response; + long imageCount = response.size(); + assertTrue(imageCount >= 0); + for (Image image : response) { + assertTrue(image.getId() >= 1); + assert null != image.getName() : image; + assert null != image.getStatus() : image; + } + } - public void testListFlavorsDetail() throws Exception { - Set response = client.listFlavors(withDetails()); - assert null != response; - long flavorCount = response.size(); - assertTrue(flavorCount >= 0); - for (Flavor flavor : response) { - assertTrue(flavor.getId() >= 1); - assert null != flavor.getName() : flavor; - assert null != flavor.getDisk() : flavor; - assert null != flavor.getRam() : flavor; - } - } + @Test + public void testGetImagesDetail() throws Exception { + Set response = client.listImages(withDetails()); + assert null != response; + long imageCount = response.size(); + assertTrue(imageCount >= 0); + for (Image image : response) { + try { + Image newDetails = client.getImage(image.getId()); + assertEquals(image, newDetails); + } catch (HttpResponseException e) {// Ticket #9867 + if (e.getResponse().getStatusCode() != 400) + throw e; + } + } + } - public void testGetFlavorsDetail() throws Exception { - Set response = client.listFlavors(withDetails()); - assert null != response; - long flavorCount = response.size(); - assertTrue(flavorCount >= 0); - for (Flavor flavor : response) { - Flavor newDetails = client.getFlavor(flavor.getId()); - assertEquals(flavor, newDetails); - } - } + @Test + public void testGetImageDetailsNotFound() throws Exception { + assert client.getImage(12312987) == null; + } - @Test - public void testGetFlavorDetailsNotFound() throws Exception { - assert client.getFlavor(12312987) == null; - } + @Test + public void testGetServerDetailsNotFound() throws Exception { + assert client.getServer(12312987) == null; + } - public void testListSharedIpGroups() throws Exception { - Set response = client.listSharedIpGroups(); - assert null != response; - long sharedIpGroupCount = response.size(); - assertTrue(sharedIpGroupCount >= 0); - for (SharedIpGroup sharedIpGroup : response) { - assertTrue(sharedIpGroup.getId() >= 0); - assert null != sharedIpGroup.getName() : sharedIpGroup; - } + @Test + public void testGetServersDetail() throws Exception { + Set response = client.listServers(withDetails()); + assert null != response; + long serverCount = response.size(); + assertTrue(serverCount >= 0); + for (Server server : response) { + Server newDetails = client.getServer(server.getId()); + assertEquals(server, newDetails); + } + } - } + public void testListFlavors() throws Exception { + Set response = client.listFlavors(); + assert null != response; + long flavorCount = response.size(); + assertTrue(flavorCount >= 1); + for (Flavor flavor : response) { + assertTrue(flavor.getId() >= 0); + assert null != flavor.getName() : flavor; + } - public void testListSharedIpGroupsDetail() throws Exception { - Set response = client.listSharedIpGroups(withDetails()); - assert null != response; - long sharedIpGroupCount = response.size(); - assertTrue(sharedIpGroupCount >= 0); - for (SharedIpGroup sharedIpGroup : response) { - assertTrue(sharedIpGroup.getId() >= 1); - assert null != sharedIpGroup.getName() : sharedIpGroup; - assert null != sharedIpGroup.getServers() : sharedIpGroup; - } - } + } - public void testGetSharedIpGroupsDetail() throws Exception { - Set response = client.listSharedIpGroups(withDetails()); - assert null != response; - long sharedIpGroupCount = response.size(); - assertTrue(sharedIpGroupCount >= 0); - for (SharedIpGroup sharedIpGroup : response) { - SharedIpGroup newDetails = client.getSharedIpGroup(sharedIpGroup.getId()); - assertEquals(sharedIpGroup, newDetails); - } - } + public void testListFlavorsDetail() throws Exception { + Set response = client.listFlavors(withDetails()); + assert null != response; + long flavorCount = response.size(); + assertTrue(flavorCount >= 0); + for (Flavor flavor : response) { + assertTrue(flavor.getId() >= 1); + assert null != flavor.getName() : flavor; + assert null != flavor.getDisk() : flavor; + assert null != flavor.getRam() : flavor; + } + } - @Test - public void testGetSharedIpGroupDetailsNotFound() throws Exception { - assert client.getSharedIpGroup(12312987) == null; - } + public void testGetFlavorsDetail() throws Exception { + Set response = client.listFlavors(withDetails()); + assert null != response; + long flavorCount = response.size(); + assertTrue(flavorCount >= 0); + for (Flavor flavor : response) { + Flavor newDetails = client.getFlavor(flavor.getId()); + assertEquals(flavor, newDetails); + } + } - @Test(enabled = false, timeOut = 5 * 60 * 1000, dependsOnMethods = "testCreateServer") - public void testCreateSharedIpGroup() throws Exception { - SharedIpGroup sharedIpGroup = null; - while (sharedIpGroup == null) { - String sharedIpGroupName = serverPrefix + "createSharedIpGroup" + new SecureRandom().nextInt(); - try { - sharedIpGroup = client.createSharedIpGroup(sharedIpGroupName, withServer(serverId)); - } catch (UndeclaredThrowableException e) { - HttpResponseException htpe = (HttpResponseException) e.getCause().getCause(); - if (htpe.getResponse().getStatusCode() == 400) - continue; - throw e; - } - } - assertNotNull(sharedIpGroup.getName()); - sharedIpGroupId = sharedIpGroup.getId(); - // Response doesn't include the server id Web Hosting #119311 - // assertEquals(sharedIpGroup.getServers(), ImmutableList.of(serverId)); - } + @Test + public void testGetFlavorDetailsNotFound() throws Exception { + assert client.getFlavor(12312987) == null; + } - private int sharedIpGroupId; + public void testListSharedIpGroups() throws Exception { + Set response = client.listSharedIpGroups(); + assert null != response; + long sharedIpGroupCount = response.size(); + assertTrue(sharedIpGroupCount >= 0); + for (SharedIpGroup sharedIpGroup : response) { + assertTrue(sharedIpGroup.getId() >= 0); + assert null != sharedIpGroup.getName() : sharedIpGroup; + } - private String serverPrefix = System.getProperty("user.name") + ".cs"; - private int serverId; - private String adminPass; - Map metadata = ImmutableMap.of("jclouds", "rackspace"); - private String ip; - private int serverId2; - private String adminPass2; - private int imageId; + } - @Test(enabled = false) - public void testCreateServer() throws Exception { - int imageId = 14362; - int flavorId = 1; - Server server = null; - while (server == null) { - String serverName = serverPrefix + "createserver" + new SecureRandom().nextInt(); - try { - server = client.createServer(serverName, imageId, flavorId, withFile("/etc/jclouds.txt", - "rackspace".getBytes()).withMetadata(metadata)); - } catch (UndeclaredThrowableException e) { - HttpResponseException htpe = (HttpResponseException) e.getCause().getCause(); - if (htpe.getResponse().getStatusCode() == 400) - continue; - throw e; - } - } - assertNotNull(server.getAdminPass()); - serverId = server.getId(); - adminPass = server.getAdminPass(); - ip = server.getAddresses().getPublicAddresses().iterator().next(); - assertEquals(server.getStatus(), ServerStatus.BUILD); - blockUntilServerActive(serverId); - } + public void testListSharedIpGroupsDetail() throws Exception { + Set response = client.listSharedIpGroups(withDetails()); + assert null != response; + long sharedIpGroupCount = response.size(); + assertTrue(sharedIpGroupCount >= 0); + for (SharedIpGroup sharedIpGroup : response) { + assertTrue(sharedIpGroup.getId() >= 1); + assert null != sharedIpGroup.getName() : sharedIpGroup; + assert null != sharedIpGroup.getServers() : sharedIpGroup; + } + } - private void blockUntilServerActive(int serverId) throws InterruptedException { - Server currentDetails = null; - for (currentDetails = client.getServer(serverId); currentDetails.getStatus() != ServerStatus.ACTIVE; currentDetails = client - .getServer(serverId)) { - System.out.printf("blocking on status active%n%s%n", currentDetails); - Thread.sleep(5 * 1000); - } - } + public void testGetSharedIpGroupsDetail() throws Exception { + Set response = client.listSharedIpGroups(withDetails()); + assert null != response; + long sharedIpGroupCount = response.size(); + assertTrue(sharedIpGroupCount >= 0); + for (SharedIpGroup sharedIpGroup : response) { + SharedIpGroup newDetails = client.getSharedIpGroup(sharedIpGroup.getId()); + assertEquals(sharedIpGroup, newDetails); + } + } - private void blockUntilServerVerifyResize(int serverId) throws InterruptedException { - Server currentDetails = null; - for (currentDetails = client.getServer(serverId); currentDetails.getStatus() != ServerStatus.VERIFY_RESIZE; currentDetails = client - .getServer(serverId)) { - System.out.printf("blocking on status verify resize%n%s%n", currentDetails); - Thread.sleep(5 * 1000); - } - } + @Test + public void testGetSharedIpGroupDetailsNotFound() throws Exception { + assert client.getSharedIpGroup(12312987) == null; + } - private void blockUntilImageActive(int imageId) throws InterruptedException { - Image currentDetails = null; - for (currentDetails = client.getImage(imageId); currentDetails.getStatus() != ImageStatus.ACTIVE; currentDetails = client - .getImage(imageId)) { - System.out.printf("blocking on status active%n%s%n", currentDetails); - Thread.sleep(5 * 1000); - } - } + @Test(enabled = false, timeOut = 5 * 60 * 1000, dependsOnMethods = "testCreateServer") + public void testCreateSharedIpGroup() throws Exception { + SharedIpGroup sharedIpGroup = null; + while (sharedIpGroup == null) { + String sharedIpGroupName = serverPrefix + "createSharedIpGroup" + new SecureRandom().nextInt(); + try { + sharedIpGroup = client.createSharedIpGroup(sharedIpGroupName, withServer(serverId)); + } catch (UndeclaredThrowableException e) { + HttpResponseException htpe = (HttpResponseException) e.getCause().getCause(); + if (htpe.getResponse().getStatusCode() == 400) + continue; + throw e; + } + } + assertNotNull(sharedIpGroup.getName()); + sharedIpGroupId = sharedIpGroup.getId(); + // Response doesn't include the server id Web Hosting #119311 + // assertEquals(sharedIpGroup.getServers(), ImmutableList.of(serverId)); + } - @Test(enabled = false, timeOut = 5 * 60 * 1000, dependsOnMethods = "testCreateServer") - public void testServerDetails() throws Exception { - Server server = client.getServer(serverId); + private int sharedIpGroupId; - assertNotNull(server.getHostId()); - assertEquals(server.getStatus(), ServerStatus.ACTIVE); - assert server.getProgress() >= 0 : "newDetails.getProgress()" + server.getProgress(); - assertEquals(new Integer(14362), server.getImageId()); - assertEquals(new Integer(1), server.getFlavorId()); - assertNotNull(server.getAddresses()); - // listAddresses tests.. - assertEquals(client.getAddresses(serverId), server.getAddresses()); - assertEquals(server.getAddresses().getPublicAddresses().size(), 1); - assertEquals(client.listPublicAddresses(serverId), server.getAddresses().getPublicAddresses()); - assertEquals(server.getAddresses().getPrivateAddresses().size(), 1); - assertEquals(client.listPrivateAddresses(serverId), server.getAddresses().getPrivateAddresses()); + private String serverPrefix = System.getProperty("user.name") + ".cs"; + private int serverId; + private String adminPass; + Map metadata = ImmutableMap.of("jclouds", "rackspace"); + private String ip; + private int serverId2; + private String adminPass2; + private int imageId; - // check metadata - assertEquals(server.getMetadata(), metadata); + @Test(enabled = true) + public void testCreateServer() throws Exception { + int imageId = 14362; + int flavorId = 1; + Server server = null; + while (server == null) { + String serverName = serverPrefix + "createserver" + new SecureRandom().nextInt(); + try { + server = client.createServer(serverName, imageId, flavorId, withFile("/etc/jclouds.txt", + "rackspace".getBytes()).withMetadata(metadata)); + } catch (UndeclaredThrowableException e) { + HttpResponseException htpe = (HttpResponseException) e.getCause().getCause(); + if (htpe.getResponse().getStatusCode() == 400) + continue; + throw e; + } + } + assertNotNull(server.getAdminPass()); + serverId = server.getId(); + adminPass = server.getAdminPass(); + ip = server.getAddresses().getPublicAddresses().iterator().next(); + assertEquals(server.getStatus(), ServerStatus.BUILD); + blockUntilServerActive(serverId); + } - checkPassOk(server, adminPass); - } + private void blockUntilServerActive(int serverId) throws InterruptedException { + Server currentDetails = null; + for (currentDetails = client.getServer(serverId); currentDetails.getStatus() != ServerStatus.ACTIVE; currentDetails = client + .getServer(serverId)) { + System.out.printf("blocking on status active%n%s%n", currentDetails); + Thread.sleep(5 * 1000); + } + } - /** - * this tests "personality" as the file looked up was sent during server creation - */ - private void checkPassOk(Server newDetails, String pass) throws IOException { - try { - doCheckPass(newDetails, pass); - } catch (SshException e) {// try twice in case there is a network timeout - try { - Thread.sleep(10 * 1000); - } catch (InterruptedException e1) { - } - doCheckPass(newDetails, pass); - } - } + private void blockUntilServerVerifyResize(int serverId) throws InterruptedException { + Server currentDetails = null; + for (currentDetails = client.getServer(serverId); currentDetails.getStatus() != ServerStatus.VERIFY_RESIZE; currentDetails = client + .getServer(serverId)) { + System.out.printf("blocking on status verify resize%n%s%n", currentDetails); + Thread.sleep(5 * 1000); + } + } - private void doCheckPass(Server newDetails, String pass) throws IOException { - IPSocket socket = new IPSocket(Iterables.get(newDetails.getAddresses().getPublicAddresses(), 0), 22); - socketTester.apply(socket); + private void blockUntilImageActive(int imageId) throws InterruptedException { + Image currentDetails = null; + for (currentDetails = client.getImage(imageId); currentDetails.getStatus() != ImageStatus.ACTIVE; currentDetails = client + .getImage(imageId)) { + System.out.printf("blocking on status active%n%s%n", currentDetails); + Thread.sleep(5 * 1000); + } + } - SshClient client = sshFactory.create(socket, new Credentials("root", pass)); - try { - client.connect(); - Payload etcPasswd = client.get("/etc/jclouds.txt"); - String etcPasswdContents = Strings2.toStringAndClose(etcPasswd.getInput()); - assertEquals("rackspace", etcPasswdContents.trim()); - } finally { - if (client != null) - client.disconnect(); - } - } + @Test(enabled = false, timeOut = 5 * 60 * 1000, dependsOnMethods = "testCreateServer") + public void testServerDetails() throws Exception { + Server server = client.getServer(serverId); - private ExecResponse exec(Server details, String pass, String command) throws IOException { - IPSocket socket = new IPSocket(Iterables.get(details.getAddresses().getPublicAddresses(), 0), 22); - socketTester.apply(socket); - SshClient client = sshFactory.create(socket, new Credentials("root", pass)); - try { - client.connect(); - return client.exec(command); - } finally { - if (client != null) - client.disconnect(); - } - } + assertNotNull(server.getHostId()); + assertEquals(server.getStatus(), ServerStatus.ACTIVE); + assert server.getProgress() >= 0 : "newDetails.getProgress()" + server.getProgress(); + assertEquals(new Integer(14362), server.getImageId()); + assertEquals(new Integer(1), server.getFlavorId()); + assertNotNull(server.getAddresses()); + // listAddresses tests.. + assertEquals(client.getAddresses(serverId), server.getAddresses()); + assertEquals(server.getAddresses().getPublicAddresses().size(), 1); + assertEquals(client.listPublicAddresses(serverId), server.getAddresses().getPublicAddresses()); + assertEquals(server.getAddresses().getPrivateAddresses().size(), 1); + assertEquals(client.listPrivateAddresses(serverId), server.getAddresses().getPrivateAddresses()); - @Test(enabled = false, timeOut = 5 * 60 * 1000, dependsOnMethods = "testCreateServer") - public void testRenameServer() throws Exception { - Server server = client.getServer(serverId); - String oldName = server.getName(); - client.renameServer(serverId, oldName + "new"); - blockUntilServerActive(serverId); - assertEquals(oldName + "new", client.getServer(serverId).getName()); - } + // check metadata + assertEquals(server.getMetadata(), metadata); - @Test(enabled = false, timeOut = 5 * 60 * 1000, dependsOnMethods = "testCreateServer") - public void testChangePassword() throws Exception { - client.changeAdminPass(serverId, "elmo"); - blockUntilServerActive(serverId); - checkPassOk(client.getServer(serverId), "elmo"); - this.adminPass = "elmo"; - } + checkPassOk(server, adminPass); + } - @Test(enabled = false, timeOut = 5 * 60 * 1000, dependsOnMethods = "testCreateSharedIpGroup") - public void testCreateServerIp() throws Exception { - int imageId = 14362; - int flavorId = 1; - Server server = null; - while (server == null) { - String serverName = serverPrefix + "createserver" + new SecureRandom().nextInt(); - try { - server = client - .createServer(serverName, imageId, flavorId, withFile("/etc/jclouds.txt", "rackspace".getBytes()) - .withMetadata(metadata).withSharedIpGroup(sharedIpGroupId).withSharedIp(ip)); - } catch (UndeclaredThrowableException e) { - HttpResponseException htpe = (HttpResponseException) e.getCause().getCause(); - if (htpe.getResponse().getStatusCode() == 400) - continue; - throw e; - } - } - assertNotNull(server.getAdminPass()); - serverId2 = server.getId(); - adminPass2 = server.getAdminPass(); - blockUntilServerActive(serverId2); - assertIpConfigured(server, adminPass2); - assert server.getAddresses().getPublicAddresses().contains(ip) : server.getAddresses() + " doesn't contain " + ip; - assertEquals(server.getSharedIpGroupId(), new Integer(sharedIpGroupId)); - } + /** + * this tests "personality" as the file looked up was sent during server creation + */ - private void assertIpConfigured(Server server, String password) { - try { - ExecResponse response = exec(server, password, "ifconfig -a"); - assert response.getOutput().indexOf(ip) > 0 : String.format("server %s didn't get ip %s%n%s", server, ip, - response); - } catch (Exception e) { - e.printStackTrace(); - } catch (AssertionError e) { - e.printStackTrace(); - } - } + private void checkPassOk(Server newDetails, String pass) throws IOException { + try { + doCheckPass(newDetails, pass); + } catch (SshException e) {// try twice in case there is a network timeout + try { + Thread.sleep(10 * 1000); + } catch (InterruptedException e1) { + } + doCheckPass(newDetails, pass); + } + } - @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testCreateServerIp") - public void testUnshare() throws Exception { - client.unshareIp(ip, serverId2); - blockUntilServerActive(serverId2); - Server server = client.getServer(serverId2); - assert !server.getAddresses().getPublicAddresses().contains(ip) : server.getAddresses(); - assertIpNotConfigured(server, adminPass2); - } + private void doCheckPass(Server newDetails, String pass) throws IOException { + IPSocket socket = new IPSocket(Iterables.get(newDetails.getAddresses().getPublicAddresses(), 0), 22); + socketTester.apply(socket); - private void assertIpNotConfigured(Server server, String password) { - try { - ExecResponse response = exec(server, password, "ifconfig -a"); - assert response.getOutput().indexOf(ip) == -1 : String.format("server %s still has get ip %s%n%s", server, ip, - response); - } catch (Exception e) { - e.printStackTrace(); - } catch (AssertionError e) { - e.printStackTrace(); - } - } + SshClient client = sshFactory.create(socket, new Credentials("root", pass)); + try { + client.connect(); + Payload etcPasswd = client.get("/etc/jclouds.txt"); + String etcPasswdContents = Strings2.toStringAndClose(etcPasswd.getInput()); + assertEquals("rackspace", etcPasswdContents.trim()); + } finally { + if (client != null) + client.disconnect(); + } + } - @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testUnshare") - public void testShareConfig() throws Exception { - client.shareIp(ip, serverId2, sharedIpGroupId, true); - blockUntilServerActive(serverId2); - Server server = client.getServer(serverId2); - assert server.getAddresses().getPublicAddresses().contains(ip) : server.getAddresses(); - assertIpConfigured(server, adminPass2); - testUnshare(); - } + private ExecResponse exec(Server details, String pass, String command) throws IOException { + IPSocket socket = new IPSocket(Iterables.get(details.getAddresses().getPublicAddresses(), 0), 22); + socketTester.apply(socket); + SshClient client = sshFactory.create(socket, new Credentials("root", pass)); + try { + client.connect(); + return client.exec(command); + } finally { + if (client != null) + client.disconnect(); + } + } - @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testShareConfig") - public void testShareNoConfig() throws Exception { - client.shareIp(ip, serverId2, sharedIpGroupId, false); - blockUntilServerActive(serverId2); - Server server = client.getServer(serverId2); - assert server.getAddresses().getPublicAddresses().contains(ip) : server.getAddresses(); - assertIpNotConfigured(server, adminPass2); - testUnshare(); - } + @Test(enabled = false, timeOut = 5 * 60 * 1000, dependsOnMethods = "testCreateServer") + public void testRenameServer() throws Exception { + Server server = client.getServer(serverId); + String oldName = server.getName(); + client.renameServer(serverId, oldName + "new"); + blockUntilServerActive(serverId); + assertEquals(oldName + "new", client.getServer(serverId).getName()); + } - @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testShareNoConfig") - public void testBackup() throws Exception { - assertEquals(new BackupSchedule(), client.getBackupSchedule(serverId)); - BackupSchedule dailyWeekly = new BackupSchedule(); - dailyWeekly.setEnabled(true); - dailyWeekly.setWeekly(WeeklyBackup.FRIDAY); - dailyWeekly.setDaily(DailyBackup.H_0400_0600); - client.replaceBackupSchedule(serverId, dailyWeekly); - client.deleteBackupSchedule(serverId); - // disables, doesn't delete: Web Hosting #119571 - assertEquals(client.getBackupSchedule(serverId).isEnabled(), false); - } + @Test(enabled = false, timeOut = 5 * 60 * 1000, dependsOnMethods = "testCreateServer") + public void testChangePassword() throws Exception { + client.changeAdminPass(serverId, "elmo"); + blockUntilServerActive(serverId); + checkPassOk(client.getServer(serverId), "elmo"); + this.adminPass = "elmo"; + } - @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testBackup") - public void testCreateImage() throws Exception { - Image image = client.createImageFromServer("hoofie", serverId); - assertEquals("hoofie", image.getName()); - assertEquals(new Integer(serverId), image.getServerId()); - imageId = image.getId(); - blockUntilImageActive(imageId); - } + @Test(enabled = false, timeOut = 5 * 60 * 1000, dependsOnMethods = "testCreateSharedIpGroup") + public void testCreateServerIp() throws Exception { + int imageId = 14362; + int flavorId = 1; + Server server = null; + while (server == null) { + String serverName = serverPrefix + "createserver" + new SecureRandom().nextInt(); + try { + server = client + .createServer(serverName, imageId, flavorId, withFile("/etc/jclouds.txt", "rackspace".getBytes()) + .withMetadata(metadata).withSharedIpGroup(sharedIpGroupId).withSharedIp(ip)); + } catch (UndeclaredThrowableException e) { + HttpResponseException htpe = (HttpResponseException) e.getCause().getCause(); + if (htpe.getResponse().getStatusCode() == 400) + continue; + throw e; + } + } + assertNotNull(server.getAdminPass()); + serverId2 = server.getId(); + adminPass2 = server.getAdminPass(); + blockUntilServerActive(serverId2); + assertIpConfigured(server, adminPass2); + assert server.getAddresses().getPublicAddresses().contains(ip) : server.getAddresses() + " doesn't contain " + ip; + assertEquals(server.getSharedIpGroupId(), new Integer(sharedIpGroupId)); + } - @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testCreateImage") - public void testRebuildServer() throws Exception { - client.rebuildServer(serverId, new RebuildServerOptions().withImage(imageId)); - blockUntilServerActive(serverId); - // issue Web Hosting #119580 imageId comes back incorrect after rebuild - // assertEquals(new Integer(imageId), client.getServer(serverId).getImageId()); - } + private void assertIpConfigured(Server server, String password) { + try { + ExecResponse response = exec(server, password, "ifconfig -a"); + assert response.getOutput().indexOf(ip) > 0 : String.format("server %s didn't get ip %s%n%s", server, ip, + response); + } catch (Exception e) { + e.printStackTrace(); + } catch (AssertionError e) { + e.printStackTrace(); + } + } - @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testRebuildServer") - public void testRebootHard() throws Exception { - client.rebootServer(serverId, RebootType.HARD); - blockUntilServerActive(serverId); - } + @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testCreateServerIp") + public void testUnshare() throws Exception { + client.unshareIp(ip, serverId2); + blockUntilServerActive(serverId2); + Server server = client.getServer(serverId2); + assert !server.getAddresses().getPublicAddresses().contains(ip) : server.getAddresses(); + assertIpNotConfigured(server, adminPass2); + } - @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testRebootHard") - public void testRebootSoft() throws Exception { - client.rebootServer(serverId, RebootType.SOFT); - blockUntilServerActive(serverId); - } + private void assertIpNotConfigured(Server server, String password) { + try { + ExecResponse response = exec(server, password, "ifconfig -a"); + assert response.getOutput().indexOf(ip) == -1 : String.format("server %s still has get ip %s%n%s", server, ip, + response); + } catch (Exception e) { + e.printStackTrace(); + } catch (AssertionError e) { + e.printStackTrace(); + } + } - @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testRebootSoft") - public void testRevertResize() throws Exception { - client.resizeServer(serverId, 2); - blockUntilServerVerifyResize(serverId); - client.revertResizeServer(serverId); - blockUntilServerActive(serverId); - assertEquals(new Integer(1), client.getServer(serverId).getFlavorId()); - } + @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testUnshare") + public void testShareConfig() throws Exception { + client.shareIp(ip, serverId2, sharedIpGroupId, true); + blockUntilServerActive(serverId2); + Server server = client.getServer(serverId2); + assert server.getAddresses().getPublicAddresses().contains(ip) : server.getAddresses(); + assertIpConfigured(server, adminPass2); + testUnshare(); + } - @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testRebootSoft") - public void testConfirmResize() throws Exception { - client.resizeServer(serverId2, 2); - blockUntilServerVerifyResize(serverId2); - client.confirmResizeServer(serverId2); - blockUntilServerActive(serverId2); - assertEquals(new Integer(2), client.getServer(serverId2).getFlavorId()); - } + @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testShareConfig") + public void testShareNoConfig() throws Exception { + client.shareIp(ip, serverId2, sharedIpGroupId, false); + blockUntilServerActive(serverId2); + Server server = client.getServer(serverId2); + assert server.getAddresses().getPublicAddresses().contains(ip) : server.getAddresses(); + assertIpNotConfigured(server, adminPass2); + testUnshare(); + } - @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = { "testRebootSoft", "testRevertResize", - "testConfirmResize" }) - void deleteServer2() { - if (serverId2 > 0) { - client.deleteServer(serverId2); - assert client.getServer(serverId2) == null; - } - } + @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testShareNoConfig") + public void testBackup() throws Exception { + assertEquals(new BackupSchedule(), client.getBackupSchedule(serverId)); + BackupSchedule dailyWeekly = new BackupSchedule(); + dailyWeekly.setEnabled(true); + dailyWeekly.setWeekly(WeeklyBackup.FRIDAY); + dailyWeekly.setDaily(DailyBackup.H_0400_0600); + client.replaceBackupSchedule(serverId, dailyWeekly); + client.deleteBackupSchedule(serverId); + // disables, doesn't delete: Web Hosting #119571 + assertEquals(client.getBackupSchedule(serverId).isEnabled(), false); + } - @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "deleteServer2") - void testDeleteImage() { - if (imageId > 0) { - client.deleteImage(imageId); - assert client.getImage(imageId) == null; - } - } + @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testBackup") + public void testCreateImage() throws Exception { + Image image = client.createImageFromServer("hoofie", serverId); + assertEquals("hoofie", image.getName()); + assertEquals(new Integer(serverId), image.getServerId()); + imageId = image.getId(); + blockUntilImageActive(imageId); + } - @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testDeleteImage") - void deleteServer1() { - if (serverId > 0) { - client.deleteServer(serverId); - assert client.getServer(serverId) == null; - } - } + @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testCreateImage") + public void testRebuildServer() throws Exception { + client.rebuildServer(serverId, new RebuildServerOptions().withImage(imageId)); + blockUntilServerActive(serverId); + // issue Web Hosting #119580 imageId comes back incorrect after rebuild + // assertEquals(new Integer(imageId), client.getServer(serverId).getImageId()); + } - @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = { "deleteServer1" }) - void testDeleteSharedIpGroup() { - if (sharedIpGroupId > 0) { - client.deleteSharedIpGroup(sharedIpGroupId); - assert client.getSharedIpGroup(sharedIpGroupId) == null; - } - } + @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testRebuildServer") + public void testRebootHard() throws Exception { + client.rebootServer(serverId, RebootType.HARD); + blockUntilServerActive(serverId); + } - @AfterTest - void deleteServersOnEnd() { - if (serverId > 0) { - client.deleteServer(serverId); - } - if (serverId2 > 0) { - client.deleteServer(serverId2); - } - if (sharedIpGroupId > 0) { - client.deleteSharedIpGroup(sharedIpGroupId); - } - } + @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testRebootHard") + public void testRebootSoft() throws Exception { + client.rebootServer(serverId, RebootType.SOFT); + blockUntilServerActive(serverId); + } + + @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testRebootSoft") + public void testRevertResize() throws Exception { + client.resizeServer(serverId, 2); + blockUntilServerVerifyResize(serverId); + client.revertResizeServer(serverId); + blockUntilServerActive(serverId); + assertEquals(new Integer(1), client.getServer(serverId).getFlavorId()); + } + + @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testRebootSoft") + public void testConfirmResize() throws Exception { + client.resizeServer(serverId2, 2); + blockUntilServerVerifyResize(serverId2); + client.confirmResizeServer(serverId2); + blockUntilServerActive(serverId2); + assertEquals(new Integer(2), client.getServer(serverId2).getFlavorId()); + } + + @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = {"testRebootSoft", "testRevertResize", + "testConfirmResize"}) + void deleteServer2() { + if (serverId2 > 0) { + client.deleteServer(serverId2); + assert client.getServer(serverId2) == null; + } + } + + @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "deleteServer2") + void testDeleteImage() { + if (imageId > 0) { + client.deleteImage(imageId); + assert client.getImage(imageId) == null; + } + } + + @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = "testDeleteImage") + void deleteServer1() { + if (serverId > 0) { + client.deleteServer(serverId); + assert client.getServer(serverId) == null; + } + } + + @Test(enabled = false, timeOut = 10 * 60 * 1000, dependsOnMethods = {"deleteServer1"}) + void testDeleteSharedIpGroup() { + if (sharedIpGroupId > 0) { + client.deleteSharedIpGroup(sharedIpGroupId); + assert client.getSharedIpGroup(sharedIpGroupId) == null; + } + } + + @AfterTest + void deleteServersOnEnd() { + if (serverId > 0) { + client.deleteServer(serverId); + } + if (serverId2 > 0) { + client.deleteServer(serverId2); + } + if (sharedIpGroupId > 0) { + client.deleteSharedIpGroup(sharedIpGroupId); + } + } } diff --git a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/NovaComputeServiceLiveTest.java b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/NovaComputeServiceLiveTest.java index 88bc827faa..ded18d8dd6 100644 --- a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/NovaComputeServiceLiveTest.java +++ b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/NovaComputeServiceLiveTest.java @@ -18,71 +18,670 @@ */ package org.jclouds.openstack.nova.compute; -import static org.testng.Assert.assertEquals; - -import java.io.IOException; - +import com.google.common.base.Charsets; +import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.common.io.Files; +import com.google.inject.Guice; +import com.google.inject.Module; +import 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.NovaClient; -import org.jclouds.compute.BaseComputeServiceLiveTest; -import org.jclouds.compute.ComputeServiceContextFactory; -import org.jclouds.compute.domain.NodeMetadata; -import org.jclouds.domain.LocationScope; +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.config.JschSshClientModule; +import org.testng.annotations.AfterTest; +import org.testng.annotations.BeforeGroups; 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. - * + * * @author Adrian Cole */ -@Test(groups = "live", enabled = true, sequential = true) -public class NovaComputeServiceLiveTest extends BaseComputeServiceLiveTest { - public NovaComputeServiceLiveTest() { - provider = "nova"; - } +@Test(groups = "novalive", enabled = true, sequential = true) +public class NovaComputeServiceLiveTest { - @Override - protected JschSshClientModule getSshModule() { - return new JschSshClientModule(); - } + protected String group; - public void testAssignability() throws Exception { - @SuppressWarnings("unused") - RestContext tmContext = new ComputeServiceContextFactory() - .createContext(provider, identity, credential).getProviderSpecificContext(); - } + protected RetryablePredicate socketTester; + protected SortedSet nodes; + protected ComputeServiceContext context; + protected ComputeService client; - @Override - protected void checkNodes(Iterable nodes, String tag) throws IOException { - super.checkNodes(nodes, tag); - for (NodeMetadata node : nodes) { - assertEquals(node.getLocation().getScope(), LocationScope.HOST); - } - } + protected Template template; + protected Map keyPair; - @Test(enabled = true, dependsOnMethods = "testReboot", expectedExceptions = UnsupportedOperationException.class) - public void testSuspendResume() throws Exception { - super.testSuspendResume(); - } + protected String provider; + protected String identity; + protected String credential; + protected String endpoint; + protected String apiversion; - @Test(enabled = true, dependsOnMethods = "testSuspendResume") - @Override - public void testGetNodesWithDetails() throws Exception { - super.testGetNodesWithDetails(); - } + private Properties overrides; - @Test(enabled = true, dependsOnMethods = "testSuspendResume") - @Override - public void testListNodes() throws Exception { - super.testListNodes(); - } + public NovaComputeServiceLiveTest() { + provider = "nova"; + } - @Test(enabled = true, dependsOnMethods = { "testListNodes", "testGetNodesWithDetails" }) - @Override - public void testDestroyNodes() { - super.testDestroyNodes(); - } + + 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 setupKeyPair(Properties properties) throws FileNotFoundException, IOException { + return ImmutableMap.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(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); + 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 + .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 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 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 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 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 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 metadataMap = newLinkedHashMap(uniqueIndex(filter(client + .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 = 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 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 { + client.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; + + client.resumeNodesMatching(inGroup(group)); + testGet(); + } + + @Test(enabled = true, dependsOnMethods = "testSuspendResume") + public void testListNodes() throws Exception { + for (ComputeMetadata node : client.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 : 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() { + int toDestroy = refreshNodes().size(); + Set 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 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 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(); + } } diff --git a/sandbox-apis/nova/src/test/resources/test.properties.template b/sandbox-apis/nova/src/test/resources/test.properties.template new file mode 100644 index 0000000000..9b7f844768 --- /dev/null +++ b/sandbox-apis/nova/src/test/resources/test.properties.template @@ -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 \ No newline at end of file