mirror of https://github.com/apache/jclouds.git
Merge pull request #159 from andreisavu/vlan-test-fix
Improved the experimental test to be able to cleanup existing networks
This commit is contained in:
commit
fdeb26f91e
|
@ -22,12 +22,14 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
|||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
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;
|
||||
|
@ -39,6 +41,10 @@ import com.google.common.collect.Iterables;
|
|||
* @author Adrian Cole
|
||||
*/
|
||||
public class GetCurrentUser implements Supplier<User> {
|
||||
|
||||
@Resource
|
||||
protected Logger logger = Logger.NULL;
|
||||
|
||||
private final CloudStackClient client;
|
||||
private final String identity;
|
||||
|
||||
|
@ -60,10 +66,9 @@ public class GetCurrentUser implements Supplier<User> {
|
|||
users));
|
||||
}
|
||||
|
||||
if (currentUser.getAccountType() != Account.Type.USER)
|
||||
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())));
|
||||
if (currentUser.getAccountType() != Account.Type.USER) {
|
||||
logger.warn("Expecting an user account: {}", currentUser);
|
||||
}
|
||||
return currentUser;
|
||||
}
|
||||
}
|
|
@ -19,18 +19,23 @@
|
|||
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.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,10 +48,30 @@ import org.testng.annotations.Test;
|
|||
*/
|
||||
@Test(groups = "live", testName = "CloudStackExperimentLiveTest")
|
||||
public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest {
|
||||
|
||||
public CloudStackExperimentLiveTest() {
|
||||
provider = "cloudstack";
|
||||
}
|
||||
|
||||
protected void deleteNetworksInZoneWithVlanId(long zoneId, String vlanId) {
|
||||
Set<Network> 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 = URI.create("vlan://" + vlanId);
|
||||
for(Network net : networks) {
|
||||
if (broadcastUri.equals(net.getBroadcastURI())) {
|
||||
long jobId = domainAdminContext.getApi().getNetworkClient().deleteNetwork(net.getId());
|
||||
adminJobComplete.apply(jobId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAndExperiment() {
|
||||
if (!domainAdminEnabled) {
|
||||
|
@ -55,6 +80,7 @@ public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest {
|
|||
}
|
||||
|
||||
String group = prefix + "-vlan";
|
||||
String vlanId = "2";
|
||||
Network network = null;
|
||||
Set<? extends NodeMetadata> nodes = null;
|
||||
try {
|
||||
|
@ -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
|
||||
deleteNetworksInZoneWithVlanId(zoneId, vlanId);
|
||||
|
||||
// find a network offering that supports vlans in our zone
|
||||
long offeringId = get(
|
||||
context.getApi().getOfferingClient().listNetworkOfferings(specifyVLAN(true).zoneId(zoneId)), 0).getId();
|
||||
|
@ -74,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());
|
||||
|
@ -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()));
|
||||
|
|
|
@ -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<CloudStackClient, CloudStackAsyncClient> context;
|
||||
protected CloudStackClient client;
|
||||
protected CloudStackClient adminClient;
|
||||
protected User user;
|
||||
|
||||
protected Predicate<IPSocket> socketTester;
|
||||
protected RetryablePredicate<Long> jobComplete;
|
||||
protected RetryablePredicate<Long> adminJobComplete;
|
||||
protected RetryablePredicate<VirtualMachine> virtualMachineRunning;
|
||||
protected RetryablePredicate<VirtualMachine> 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<Long>(new JobComplete(client), 1200, 1, 5, TimeUnit.SECONDS);
|
||||
injector.injectMembers(jobComplete);
|
||||
adminJobComplete = new RetryablePredicate<Long>(new JobComplete(adminClient), 1200, 1, 5, TimeUnit.SECONDS);
|
||||
injector.injectMembers(adminJobComplete);
|
||||
virtualMachineRunning = new RetryablePredicate<VirtualMachine>(new VirtualMachineRunning(client), 600, 5, 5,
|
||||
TimeUnit.SECONDS);
|
||||
injector.injectMembers(virtualMachineRunning);
|
||||
|
@ -209,10 +215,10 @@ public class BaseCloudStackClientLiveTest extends BaseVersionedServiceLiveTest {
|
|||
users));
|
||||
}
|
||||
|
||||
if (currentUser.getAccountType() != type)
|
||||
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))));
|
||||
if (currentUser.getAccountType() != type) {
|
||||
Logger.getAnonymousLogger().warning(
|
||||
String.format("Expecting an user with type %s. Got: %s", type.toString(), currentUser.toString()));
|
||||
}
|
||||
return currentUser;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue