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 java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import org.jclouds.cloudstack.CloudStackClient;
|
import org.jclouds.cloudstack.CloudStackClient;
|
||||||
import org.jclouds.cloudstack.domain.Account;
|
import org.jclouds.cloudstack.domain.Account;
|
||||||
import org.jclouds.cloudstack.domain.User;
|
import org.jclouds.cloudstack.domain.User;
|
||||||
import org.jclouds.cloudstack.predicates.UserPredicates;
|
import org.jclouds.cloudstack.predicates.UserPredicates;
|
||||||
|
import org.jclouds.logging.Logger;
|
||||||
import org.jclouds.rest.annotations.Identity;
|
import org.jclouds.rest.annotations.Identity;
|
||||||
|
|
||||||
import com.google.common.base.Predicate;
|
import com.google.common.base.Predicate;
|
||||||
|
@ -39,6 +41,10 @@ import com.google.common.collect.Iterables;
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
*/
|
*/
|
||||||
public class GetCurrentUser implements Supplier<User> {
|
public class GetCurrentUser implements Supplier<User> {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
protected Logger logger = Logger.NULL;
|
||||||
|
|
||||||
private final CloudStackClient client;
|
private final CloudStackClient client;
|
||||||
private final String identity;
|
private final String identity;
|
||||||
|
|
||||||
|
@ -60,10 +66,9 @@ public class GetCurrentUser implements Supplier<User> {
|
||||||
users));
|
users));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentUser.getAccountType() != Account.Type.USER)
|
if (currentUser.getAccountType() != Account.Type.USER) {
|
||||||
throw new IllegalArgumentException(String.format(
|
logger.warn("Expecting an user account: {}", currentUser);
|
||||||
"invalid account type: %s, please specify an apiKey of a USER, for example: %s",
|
}
|
||||||
currentUser.getAccountType(), Iterables.filter(users, UserPredicates.isUserAccount())));
|
|
||||||
return currentUser;
|
return currentUser;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -19,18 +19,23 @@
|
||||||
package org.jclouds.cloudstack.compute;
|
package org.jclouds.cloudstack.compute;
|
||||||
|
|
||||||
import static com.google.common.collect.Iterables.concat;
|
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.Iterables.get;
|
||||||
import static com.google.common.collect.Sets.newTreeSet;
|
import static com.google.common.collect.Sets.newTreeSet;
|
||||||
import static org.jclouds.cloudstack.options.CreateNetworkOptions.Builder.vlan;
|
import static org.jclouds.cloudstack.options.CreateNetworkOptions.Builder.vlan;
|
||||||
import static org.jclouds.cloudstack.options.ListNetworkOfferingsOptions.Builder.specifyVLAN;
|
import static org.jclouds.cloudstack.options.ListNetworkOfferingsOptions.Builder.specifyVLAN;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
|
import org.jclouds.cloudstack.compute.options.CloudStackTemplateOptions;
|
||||||
import org.jclouds.cloudstack.domain.Network;
|
import org.jclouds.cloudstack.domain.Network;
|
||||||
|
import org.jclouds.cloudstack.domain.TrafficType;
|
||||||
import org.jclouds.cloudstack.features.BaseCloudStackClientLiveTest;
|
import org.jclouds.cloudstack.features.BaseCloudStackClientLiveTest;
|
||||||
|
import org.jclouds.cloudstack.options.ListNetworksOptions;
|
||||||
import org.jclouds.compute.RunNodesException;
|
import org.jclouds.compute.RunNodesException;
|
||||||
import org.jclouds.compute.domain.NodeMetadata;
|
import org.jclouds.compute.domain.NodeMetadata;
|
||||||
import org.jclouds.compute.domain.Template;
|
import org.jclouds.compute.domain.Template;
|
||||||
|
@ -43,10 +48,30 @@ import org.testng.annotations.Test;
|
||||||
*/
|
*/
|
||||||
@Test(groups = "live", testName = "CloudStackExperimentLiveTest")
|
@Test(groups = "live", testName = "CloudStackExperimentLiveTest")
|
||||||
public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest {
|
public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest {
|
||||||
|
|
||||||
public CloudStackExperimentLiveTest() {
|
public CloudStackExperimentLiveTest() {
|
||||||
provider = "cloudstack";
|
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
|
@Test
|
||||||
public void testAndExperiment() {
|
public void testAndExperiment() {
|
||||||
if (!domainAdminEnabled) {
|
if (!domainAdminEnabled) {
|
||||||
|
@ -55,6 +80,7 @@ public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
String group = prefix + "-vlan";
|
String group = prefix + "-vlan";
|
||||||
|
String vlanId = "2";
|
||||||
Network network = null;
|
Network network = null;
|
||||||
Set<? extends NodeMetadata> nodes = null;
|
Set<? extends NodeMetadata> nodes = null;
|
||||||
try {
|
try {
|
||||||
|
@ -65,6 +91,9 @@ public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest {
|
||||||
// get the zone we are launching into
|
// get the zone we are launching into
|
||||||
long zoneId = Long.parseLong(template.getLocation().getId());
|
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
|
// find a network offering that supports vlans in our zone
|
||||||
long offeringId = get(
|
long offeringId = get(
|
||||||
context.getApi().getOfferingClient().listNetworkOfferings(specifyVLAN(true).zoneId(zoneId)), 0).getId();
|
context.getApi().getOfferingClient().listNetworkOfferings(specifyVLAN(true).zoneId(zoneId)), 0).getId();
|
||||||
|
@ -74,7 +103,7 @@ public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest {
|
||||||
.getNetworkClient()
|
.getNetworkClient()
|
||||||
// startIP/endIP/netmask/gateway must be specified together
|
// startIP/endIP/netmask/gateway must be specified together
|
||||||
.createNetworkInZone(zoneId, offeringId, group, group,
|
.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
|
// set options to specify this network id
|
||||||
template.getOptions().as(CloudStackTemplateOptions.class).networkId(network.getId());
|
template.getOptions().as(CloudStackTemplateOptions.class).networkId(network.getId());
|
||||||
|
@ -82,6 +111,8 @@ public class CloudStackExperimentLiveTest extends BaseCloudStackClientLiveTest {
|
||||||
// launch the VM
|
// launch the VM
|
||||||
nodes = computeContext.getComputeService().createNodesInGroup(group, 1, template);
|
nodes = computeContext.getComputeService().createNodesInGroup(group, 1, template);
|
||||||
|
|
||||||
|
assert nodes.size() > 0;
|
||||||
|
|
||||||
} catch (RunNodesException e) {
|
} catch (RunNodesException e) {
|
||||||
Logger.getAnonymousLogger().log(Level.SEVERE, "error creating nodes", e);
|
Logger.getAnonymousLogger().log(Level.SEVERE, "error creating nodes", e);
|
||||||
nodes = newTreeSet(concat(e.getSuccessfulNodes(), e.getNodeErrors().keySet()));
|
nodes = newTreeSet(concat(e.getSuccessfulNodes(), e.getNodeErrors().keySet()));
|
||||||
|
|
|
@ -27,6 +27,7 @@ import java.util.NoSuchElementException;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.jclouds.cloudstack.CloudStackAsyncClient;
|
import org.jclouds.cloudstack.CloudStackAsyncClient;
|
||||||
import org.jclouds.cloudstack.CloudStackClient;
|
import org.jclouds.cloudstack.CloudStackClient;
|
||||||
|
@ -128,10 +129,12 @@ public class BaseCloudStackClientLiveTest extends BaseVersionedServiceLiveTest {
|
||||||
protected ComputeServiceContext computeContext;
|
protected ComputeServiceContext computeContext;
|
||||||
protected RestContext<CloudStackClient, CloudStackAsyncClient> context;
|
protected RestContext<CloudStackClient, CloudStackAsyncClient> context;
|
||||||
protected CloudStackClient client;
|
protected CloudStackClient client;
|
||||||
|
protected CloudStackClient adminClient;
|
||||||
protected User user;
|
protected User user;
|
||||||
|
|
||||||
protected Predicate<IPSocket> socketTester;
|
protected Predicate<IPSocket> socketTester;
|
||||||
protected RetryablePredicate<Long> jobComplete;
|
protected RetryablePredicate<Long> jobComplete;
|
||||||
|
protected RetryablePredicate<Long> adminJobComplete;
|
||||||
protected RetryablePredicate<VirtualMachine> virtualMachineRunning;
|
protected RetryablePredicate<VirtualMachine> virtualMachineRunning;
|
||||||
protected RetryablePredicate<VirtualMachine> virtualMachineDestroyed;
|
protected RetryablePredicate<VirtualMachine> virtualMachineDestroyed;
|
||||||
protected SshClient.Factory sshFactory;
|
protected SshClient.Factory sshFactory;
|
||||||
|
@ -179,6 +182,7 @@ public class BaseCloudStackClientLiveTest extends BaseVersionedServiceLiveTest {
|
||||||
domainAdminContext = domainAdminComputeContext.getProviderSpecificContext();
|
domainAdminContext = domainAdminComputeContext.getProviderSpecificContext();
|
||||||
domainAdminClient = domainAdminContext.getApi();
|
domainAdminClient = domainAdminContext.getApi();
|
||||||
domainAdminUser = verifyCurrentUserIsOfType(domainAdminContext, Account.Type.DOMAIN_ADMIN);
|
domainAdminUser = verifyCurrentUserIsOfType(domainAdminContext, Account.Type.DOMAIN_ADMIN);
|
||||||
|
adminClient = domainAdminContext.getApi();
|
||||||
}
|
}
|
||||||
|
|
||||||
injector = Guice.createInjector(new SshjSshClientModule(), new Log4JLoggingModule());
|
injector = Guice.createInjector(new SshjSshClientModule(), new Log4JLoggingModule());
|
||||||
|
@ -187,6 +191,8 @@ public class BaseCloudStackClientLiveTest extends BaseVersionedServiceLiveTest {
|
||||||
injector.injectMembers(socketTester);
|
injector.injectMembers(socketTester);
|
||||||
jobComplete = new RetryablePredicate<Long>(new JobComplete(client), 1200, 1, 5, TimeUnit.SECONDS);
|
jobComplete = new RetryablePredicate<Long>(new JobComplete(client), 1200, 1, 5, TimeUnit.SECONDS);
|
||||||
injector.injectMembers(jobComplete);
|
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,
|
virtualMachineRunning = new RetryablePredicate<VirtualMachine>(new VirtualMachineRunning(client), 600, 5, 5,
|
||||||
TimeUnit.SECONDS);
|
TimeUnit.SECONDS);
|
||||||
injector.injectMembers(virtualMachineRunning);
|
injector.injectMembers(virtualMachineRunning);
|
||||||
|
@ -209,10 +215,10 @@ public class BaseCloudStackClientLiveTest extends BaseVersionedServiceLiveTest {
|
||||||
users));
|
users));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentUser.getAccountType() != type)
|
if (currentUser.getAccountType() != type) {
|
||||||
throw new IllegalArgumentException(String.format(
|
Logger.getAnonymousLogger().warning(
|
||||||
"invalid account type: %s, please specify an apiKey of %s, for example: %s",
|
String.format("Expecting an user with type %s. Got: %s", type.toString(), currentUser.toString()));
|
||||||
currentUser.getAccountType(), type, Iterables.filter(users, UserPredicates.accountTypeEquals(type))));
|
}
|
||||||
return currentUser;
|
return currentUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue