From 0613f3390c74c56b3f7d1876d241da3a2937cdae Mon Sep 17 00:00:00 2001 From: Mike Arnold Date: Thu, 8 Mar 2012 10:39:59 -0600 Subject: [PATCH] adding live tests for floating ip client --- .../features/FloatingIPClientLiveTest.java | 126 +++++++++++++++++- 1 file changed, 121 insertions(+), 5 deletions(-) diff --git a/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/FloatingIPClientLiveTest.java b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/FloatingIPClientLiveTest.java index 93115a5735..cc1847457f 100644 --- a/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/FloatingIPClientLiveTest.java +++ b/labs/openstack-nova/src/test/java/org/jclouds/openstack/nova/v1_1/features/FloatingIPClientLiveTest.java @@ -18,15 +18,19 @@ */ package org.jclouds.openstack.nova.v1_1.features; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; - -import java.util.Set; - +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; +import com.google.common.collect.Multimap; +import com.google.common.util.concurrent.ListenableFuture; +import org.jclouds.openstack.nova.v1_1.domain.Address; import org.jclouds.openstack.nova.v1_1.domain.FloatingIP; import org.jclouds.openstack.nova.v1_1.domain.Server; +import org.jclouds.openstack.nova.v1_1.domain.ServerStatus; import org.jclouds.openstack.nova.v1_1.internal.BaseNovaClientLiveTest; +import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; +import java.util.Set; +import static org.testng.Assert.*; /** * Tests behavior of {@code ServerClient} @@ -36,6 +40,8 @@ import org.testng.annotations.Test; @Test(groups = "live", testName = "FloatingIPClientLiveTest") public class FloatingIPClientLiveTest extends BaseNovaClientLiveTest { + private static final int INCONSISTENCY_WINDOW = 5000; + @Test public void testListFloatingIPs() throws Exception { for (String regionId : context.getApi().getConfiguredRegions()) { @@ -56,4 +62,114 @@ public class FloatingIPClientLiveTest extends BaseNovaClientLiveTest { } } } + + @Test + public void testAllocateAndDeallocateFloatingIPs() throws Exception { + for (String regionId : context.getApi().getConfiguredRegions()) { + FloatingIPClient client = context.getApi().getFloatingIPClientForRegion(regionId); + FloatingIP floatingIP = client.allocate(); + assertNotNull(floatingIP); + + Set response = client.listFloatingIPs(); + boolean ipInSet = false; + for (FloatingIP ip : response) { + if (ip.getId().equals(floatingIP.getId())) ipInSet = true; + } + assertTrue(ipInSet); + + client.deallocate(floatingIP.getId()); + + response = client.listFloatingIPs(); + ipInSet = false; + for (FloatingIP ip : response) { + if (ip.getId().equals(floatingIP.getId())) { + ipInSet = true; + } + } + assertFalse(ipInSet); + } + } + + + @Test + public void testAddAndRemoveFloatingIp() throws Exception { + for (String regionId : context.getApi().getConfiguredRegions()) { + FloatingIPClient client = context.getApi().getFloatingIPClientForRegion(regionId); + ServerClient serverClient = context.getApi().getServerClientForRegion(regionId); + Server server = serverClient.createServer("test", "121", "100"); + blockUntilServerActive(server.getId(), serverClient); + FloatingIP floatingIP = client.allocate(); + assertNotNull(floatingIP); + try { + client.addFloatingIP(server.getId(), floatingIP.getIp()); + assertEventually(new ServerHasFloatingIP(serverClient, server.getId(), floatingIP.getIp())); + } + finally { + client.removeFloatingIP(server.getId(), floatingIP.getIp()); + serverClient.deleteServer(server.getId()); + } + } + } + + private void blockUntilServerActive(String serverId, ServerClient client) 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); + } + } + + protected static void assertEventually(Runnable assertion) { + long start = System.currentTimeMillis(); + AssertionError error = null; + for (int i = 0; i < 30; i++) { + try { + assertion.run(); + if (i > 0) + System.err.printf("%d attempts and %dms asserting %s%n", i + 1, System.currentTimeMillis() - start, + assertion.getClass().getSimpleName()); + return; + } catch (AssertionError e) { + error = e; + } + try { + Thread.sleep(INCONSISTENCY_WINDOW / 30); + } catch (InterruptedException e) { + } + } + if (error != null) + throw error; + + } + + public static final class ServerHasFloatingIP implements Runnable { + private final ServerClient client; + private final String serverId; + private final String floatingIP; + + public ServerHasFloatingIP(ServerClient serverClient, String serverId, String floatingIP) { + this.client = serverClient; + this.serverId = serverId; + this.floatingIP = floatingIP; + } + + public void run() { + try { + Server server = client.getServer(serverId); + boolean ipInServerAddresses = false; + Multimapaddresses = server.getAddresses(); + for (Address address : addresses.values()){ + if (address.getAddr().equals(floatingIP)) { + ipInServerAddresses = true; + } + } + assertTrue(ipInServerAddresses); + } catch (Exception e) { + throw new AssertionError(e); + } + } + } + } +