From dc5c04951bec32c12471fdc996436b7827ed04e2 Mon Sep 17 00:00:00 2001 From: andreisavu Date: Wed, 16 Nov 2011 11:37:27 +0200 Subject: [PATCH 1/2] Improved the experimental test to be able to cleanup existing networks --- .../cloudstack/suppliers/GetCurrentUser.java | 8 +++-- .../compute/CloudStackExperimentLiveTest.java | 33 ++++++++++++++++++- .../BaseCloudStackClientLiveTest.java | 13 ++++++-- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/suppliers/GetCurrentUser.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/suppliers/GetCurrentUser.java index b05efafa3a..0eb9af3592 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/suppliers/GetCurrentUser.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/suppliers/GetCurrentUser.java @@ -21,6 +21,7 @@ package org.jclouds.cloudstack.suppliers; import static com.google.common.base.Preconditions.checkNotNull; import java.util.NoSuchElementException; +import java.util.logging.Logger; import javax.inject.Inject; @@ -60,10 +61,13 @@ public class GetCurrentUser implements Supplier { users)); } - if (currentUser.getAccountType() != Account.Type.USER) - throw new IllegalArgumentException(String.format( + if (currentUser.getAccountType() != Account.Type.USER) { + Logger.getAnonymousLogger().warning("You are using an administrative account to start a machine"); + /* throw new IllegalArgumentException(String.format( "invalid account type: %s, please specify an apiKey of a USER, for example: %s", currentUser.getAccountType(), Iterables.filter(users, UserPredicates.isUserAccount()))); + */ + } return currentUser; } } \ No newline at end of file diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackExperimentLiveTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackExperimentLiveTest.java index b41bf6f9ce..f2074adff0 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackExperimentLiveTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackExperimentLiveTest.java @@ -19,18 +19,24 @@ package org.jclouds.cloudstack.compute; import static com.google.common.collect.Iterables.concat; +import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.get; import static com.google.common.collect.Sets.newTreeSet; import static org.jclouds.cloudstack.options.CreateNetworkOptions.Builder.vlan; import static org.jclouds.cloudstack.options.ListNetworkOfferingsOptions.Builder.specifyVLAN; +import java.net.URI; +import java.net.URISyntaxException; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions; import org.jclouds.cloudstack.domain.Network; +import org.jclouds.cloudstack.domain.NetworkType; +import org.jclouds.cloudstack.domain.TrafficType; import org.jclouds.cloudstack.features.BaseCloudStackClientLiveTest; +import org.jclouds.cloudstack.options.ListNetworksOptions; import org.jclouds.compute.RunNodesException; import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.Template; @@ -43,12 +49,32 @@ import org.testng.annotations.Test; */ @Test(groups = "live", testName = "CloudStackExperimentLiveTest") public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest { + public CloudStackExperimentLiveTest() { provider = "cloudstack"; } + protected void deleteNetworkByVLAN(String vlan, long zoneId) throws URISyntaxException { + Set networks = domainAdminContext.getApi().getNetworkClient().listNetworks( + ListNetworksOptions.Builder + .isDefault(false) + .isSystem(false) + .zoneId(zoneId) + .trafficType(TrafficType.GUEST) + ); + + // Warning: the vlan id is not set in the response - using an workaround + URI broadcastUri = new URI("vlan://" + vlan); + for(Network net : networks) { + if (broadcastUri.equals(net.getBroadcastURI())) { + long jobId = domainAdminContext.getApi().getNetworkClient().deleteNetwork(net.getId()); + adminJobComplete.apply(jobId); + } + } + } + @Test - public void testAndExperiment() { + public void testAndExperiment() throws URISyntaxException { if (!domainAdminEnabled) { Logger.getAnonymousLogger().log(Level.SEVERE, "domainAdmin credentials not present, skipping test"); return; @@ -65,6 +91,9 @@ public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest { // get the zone we are launching into long zoneId = Long.parseLong(template.getLocation().getId()); + // cleanup before running the test + deleteNetworkByVLAN("2", zoneId); + // find a network offering that supports vlans in our zone long offeringId = get( context.getApi().getOfferingClient().listNetworkOfferings(specifyVLAN(true).zoneId(zoneId)), 0).getId(); @@ -82,6 +111,8 @@ public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest { // launch the VM nodes = computeContext.getComputeService().createNodesInGroup(group, 1, template); + assert nodes.size() > 0; + } catch (RunNodesException e) { Logger.getAnonymousLogger().log(Level.SEVERE, "error creating nodes", e); nodes = newTreeSet(concat(e.getSuccessfulNodes(), e.getNodeErrors().keySet())); diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java index bc34626651..7ab180a198 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java @@ -27,6 +27,7 @@ import java.util.NoSuchElementException; import java.util.Properties; import java.util.Set; import java.util.concurrent.TimeUnit; +import java.util.logging.Logger; import org.jclouds.cloudstack.CloudStackAsyncClient; import org.jclouds.cloudstack.CloudStackClient; @@ -128,10 +129,12 @@ public class BaseCloudStackClientLiveTest extends BaseVersionedServiceLiveTest { protected ComputeServiceContext computeContext; protected RestContext context; protected CloudStackClient client; + protected CloudStackClient adminClient; protected User user; protected Predicate socketTester; protected RetryablePredicate jobComplete; + protected RetryablePredicate adminJobComplete; protected RetryablePredicate virtualMachineRunning; protected RetryablePredicate virtualMachineDestroyed; protected SshClient.Factory sshFactory; @@ -179,6 +182,7 @@ public class BaseCloudStackClientLiveTest extends BaseVersionedServiceLiveTest { domainAdminContext = domainAdminComputeContext.getProviderSpecificContext(); domainAdminClient = domainAdminContext.getApi(); domainAdminUser = verifyCurrentUserIsOfType(domainAdminContext, Account.Type.DOMAIN_ADMIN); + adminClient = domainAdminContext.getApi(); } injector = Guice.createInjector(new SshjSshClientModule(), new Log4JLoggingModule()); @@ -187,6 +191,8 @@ public class BaseCloudStackClientLiveTest extends BaseVersionedServiceLiveTest { injector.injectMembers(socketTester); jobComplete = new RetryablePredicate(new JobComplete(client), 1200, 1, 5, TimeUnit.SECONDS); injector.injectMembers(jobComplete); + adminJobComplete = new RetryablePredicate(new JobComplete(adminClient), 1200, 1, 5, TimeUnit.SECONDS); + injector.injectMembers(adminJobComplete); virtualMachineRunning = new RetryablePredicate(new VirtualMachineRunning(client), 600, 5, 5, TimeUnit.SECONDS); injector.injectMembers(virtualMachineRunning); @@ -209,10 +215,13 @@ public class BaseCloudStackClientLiveTest extends BaseVersionedServiceLiveTest { users)); } - if (currentUser.getAccountType() != type) - throw new IllegalArgumentException(String.format( + if (currentUser.getAccountType() != type) { + Logger.getAnonymousLogger().warning("You are using an administrative account for testing"); + /* throw new IllegalArgumentException(String.format( "invalid account type: %s, please specify an apiKey of %s, for example: %s", currentUser.getAccountType(), type, Iterables.filter(users, UserPredicates.accountTypeEquals(type)))); + */ + } return currentUser; } From f4c1e5b88255e77ff4610765aa720ffb9b66787b Mon Sep 17 00:00:00 2001 From: andreisavu Date: Wed, 16 Nov 2011 14:40:32 +0200 Subject: [PATCH 2/2] Addressed some of the offline comments made by Adrian --- .../cloudstack/suppliers/GetCurrentUser.java | 13 +++++++------ .../compute/CloudStackExperimentLiveTest.java | 12 ++++++------ .../features/BaseCloudStackClientLiveTest.java | 7 ++----- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/suppliers/GetCurrentUser.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/suppliers/GetCurrentUser.java index 0eb9af3592..f8a439fb4d 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/suppliers/GetCurrentUser.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/suppliers/GetCurrentUser.java @@ -21,14 +21,15 @@ package org.jclouds.cloudstack.suppliers; import static com.google.common.base.Preconditions.checkNotNull; import java.util.NoSuchElementException; -import java.util.logging.Logger; +import javax.annotation.Resource; import javax.inject.Inject; import org.jclouds.cloudstack.CloudStackClient; import org.jclouds.cloudstack.domain.Account; import org.jclouds.cloudstack.domain.User; import org.jclouds.cloudstack.predicates.UserPredicates; +import org.jclouds.logging.Logger; import org.jclouds.rest.annotations.Identity; import com.google.common.base.Predicate; @@ -40,6 +41,10 @@ import com.google.common.collect.Iterables; * @author Adrian Cole */ public class GetCurrentUser implements Supplier { + + @Resource + protected Logger logger = Logger.NULL; + private final CloudStackClient client; private final String identity; @@ -62,11 +67,7 @@ public class GetCurrentUser implements Supplier { } if (currentUser.getAccountType() != Account.Type.USER) { - Logger.getAnonymousLogger().warning("You are using an administrative account to start a machine"); - /* throw new IllegalArgumentException(String.format( - "invalid account type: %s, please specify an apiKey of a USER, for example: %s", - currentUser.getAccountType(), Iterables.filter(users, UserPredicates.isUserAccount()))); - */ + logger.warn("Expecting an user account: {}", currentUser); } return currentUser; } diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackExperimentLiveTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackExperimentLiveTest.java index f2074adff0..c1e282f6a9 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackExperimentLiveTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/CloudStackExperimentLiveTest.java @@ -33,7 +33,6 @@ import java.util.logging.Logger; import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions; import org.jclouds.cloudstack.domain.Network; -import org.jclouds.cloudstack.domain.NetworkType; import org.jclouds.cloudstack.domain.TrafficType; import org.jclouds.cloudstack.features.BaseCloudStackClientLiveTest; import org.jclouds.cloudstack.options.ListNetworksOptions; @@ -54,7 +53,7 @@ public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest { provider = "cloudstack"; } - protected void deleteNetworkByVLAN(String vlan, long zoneId) throws URISyntaxException { + protected void deleteNetworksInZoneWithVlanId(long zoneId, String vlanId) { Set networks = domainAdminContext.getApi().getNetworkClient().listNetworks( ListNetworksOptions.Builder .isDefault(false) @@ -64,7 +63,7 @@ public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest { ); // Warning: the vlan id is not set in the response - using an workaround - URI broadcastUri = new URI("vlan://" + vlan); + URI broadcastUri = URI.create("vlan://" + vlanId); for(Network net : networks) { if (broadcastUri.equals(net.getBroadcastURI())) { long jobId = domainAdminContext.getApi().getNetworkClient().deleteNetwork(net.getId()); @@ -74,13 +73,14 @@ public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest { } @Test - public void testAndExperiment() throws URISyntaxException { + public void testAndExperiment() { if (!domainAdminEnabled) { Logger.getAnonymousLogger().log(Level.SEVERE, "domainAdmin credentials not present, skipping test"); return; } String group = prefix + "-vlan"; + String vlanId = "2"; Network network = null; Set nodes = null; try { @@ -92,7 +92,7 @@ public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest { long zoneId = Long.parseLong(template.getLocation().getId()); // cleanup before running the test - deleteNetworkByVLAN("2", zoneId); + deleteNetworksInZoneWithVlanId(zoneId, vlanId); // find a network offering that supports vlans in our zone long offeringId = get( @@ -103,7 +103,7 @@ public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest { .getNetworkClient() // startIP/endIP/netmask/gateway must be specified together .createNetworkInZone(zoneId, offeringId, group, group, - vlan("2").startIP("192.168.1.2").netmask("255.255.255.0").gateway("192.168.1.1")); + vlan(vlanId).startIP("192.168.1.2").netmask("255.255.255.0").gateway("192.168.1.1")); // set options to specify this network id template.getOptions().as(CloudStackTemplateOptions.class).networkId(network.getId()); diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java index 7ab180a198..9af6961b06 100644 --- a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/features/BaseCloudStackClientLiveTest.java @@ -216,11 +216,8 @@ public class BaseCloudStackClientLiveTest extends BaseVersionedServiceLiveTest { } if (currentUser.getAccountType() != type) { - Logger.getAnonymousLogger().warning("You are using an administrative account for testing"); - /* throw new IllegalArgumentException(String.format( - "invalid account type: %s, please specify an apiKey of %s, for example: %s", - currentUser.getAccountType(), type, Iterables.filter(users, UserPredicates.accountTypeEquals(type)))); - */ + Logger.getAnonymousLogger().warning( + String.format("Expecting an user with type %s. Got: %s", type.toString(), currentUser.toString())); } return currentUser; }