diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorClientLiveTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorClientLiveTest.java index 7c652978de..1471fb62fc 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorClientLiveTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/BaseVCloudDirectorClientLiveTest.java @@ -18,9 +18,7 @@ */ package org.jclouds.vcloud.director.v1_5.internal; -import static com.google.common.base.Objects.equal; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.ENTITY_NON_NULL; -import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.REF_REQ_LIVE; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.TASK_COMPLETE_TIMELY; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; @@ -41,13 +39,9 @@ import javax.inject.Inject; import org.jclouds.compute.BaseVersionedServiceLiveTest; import org.jclouds.date.DateService; import org.jclouds.logging.Logger; -import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.jclouds.predicates.RetryablePredicate; import org.jclouds.rest.RestContext; -import org.jclouds.rest.RestContextFactory; -import org.jclouds.sshj.config.SshjSshClientModule; import org.jclouds.vcloud.director.testng.FormatApiResultsListener; -import org.jclouds.vcloud.director.v1_5.VCloudDirectorContext; import org.jclouds.vcloud.director.v1_5.VCloudDirectorException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; import org.jclouds.vcloud.director.v1_5.admin.VCloudDirectorAdminAsyncClient; @@ -81,7 +75,9 @@ import org.jclouds.vcloud.director.v1_5.predicates.TaskSuccess; import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorAsyncClient; import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorClient; import org.testng.annotations.AfterClass; +import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeSuite; import org.testng.annotations.Listeners; import org.testng.annotations.Test; @@ -95,7 +91,6 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.google.inject.Guice; -import com.google.inject.Module; /** * Tests behavior of {@link VCloudDirectorClient} and acts as parent for other client live tests. @@ -138,15 +133,14 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseVersionedServ protected final Set vAppNames = Sets.newLinkedHashSet(); protected static final Random random = new Random(); - private User createdAdminUser; - private User createdUser; - protected BaseVCloudDirectorClientLiveTest() { provider = "vcloud-director"; } protected DateService dateService; + private static VCloudDirectorTestSession testSession; + @BeforeClass(alwaysRun = true) protected void setupDateService() { dateService = Guice.createInjector().getInstance(DateService.class); @@ -165,70 +159,35 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseVersionedServ protected void initTaskSuccessLong(TaskSuccess taskSuccess) { retryTaskSuccessLong = new RetryablePredicate(taskSuccess, LONG_TASK_TIMEOUT_SECONDS * 1000L); } + + @BeforeSuite(alwaysRun = true) + protected void setupTestSession() { + setupCredentials(); + Properties overrides = setupProperties(); + testSession = VCloudDirectorTestSession.builder() + .identity(identity) + .credential(credential) + .provider(provider) + .overrides(overrides) + .endpoint(endpoint) + .build(); + } + + @AfterSuite(alwaysRun = true) + protected void tearDownTestSession() { + testSession.close(); + } @BeforeClass(alwaysRun = true) protected void setupContext() throws Exception { setupCredentials(); - Properties overrides = setupProperties(); - VCloudDirectorContext rootContext = VCloudDirectorContext.class.cast( - new RestContextFactory().createContext(provider, identity, credential, ImmutableSet. of( - new Log4JLoggingModule(), new SshjSshClientModule()), overrides)); + context = testSession.getUserContext(); + adminContext = testSession.getAdminContext(); - if (rootContext.getApi().getCurrentSession().getLinks().contains(Link.builder() - .rel("down") - .type("application/vnd.vmware.admin.vcloud+xml") - .href(URI.create(endpoint+"/admin/")) - .build())) { - - adminContext = rootContext.getAdminContext(); - - rootContext.utils().injector().injectMembers(this); - Reference orgRef = Iterables.getFirst(rootContext.getApi().getOrgClient().getOrgList().getOrgs(), null) - .toAdminReference(endpoint); - assertNotNull(orgRef, String.format(REF_REQ_LIVE, "admin org")); - - Reference userRef = Iterables.find(adminContext.getApi().getOrgClient().getOrg(orgRef.getHref()).getUsers(), - ReferencePredicates.nameEquals(adminContext.getApi().getCurrentSession().getUser())); - - User user = adminContext.getApi().getUserClient().getUser(userRef.getHref()); - Reference orgAdmin = user.getRole(); - assertTrue(equal(orgAdmin.getName(), DefaultRoles.ORG_ADMIN), "must give org admin or user-only credentials"); - - String adminIdentity = "testAdmin"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); - String adminCredential = "testAdminPassword"; - - createdAdminUser = rootContext.getAdminContext().getApi().getUserClient().createUser(orgRef.getHref(), User.builder() - .name(adminIdentity) - .password(adminCredential) - .description("test user with user-level privileges") - .role(orgAdmin) - .deployedVmQuota(REQUIRED_ADMIN_VM_QUOTA) - .isEnabled(true) - .build()); - - rootContext.close(); rootContext = null; - - adminContext = VCloudDirectorContext.class.cast(new RestContextFactory().createContext(provider, adminIdentity, adminCredential, ImmutableSet. of( - new Log4JLoggingModule(), new SshjSshClientModule()), overrides)).getAdminContext(); + if(adminContext != null) { adminSession = adminContext.getApi().getCurrentSession(); adminContext.utils().injector().injectMembers(this); - String userIdentity = "test"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); - String userCredential = "testPassword"; - - createdUser = adminContext.getApi().getUserClient().createUser(orgRef.getHref(), User.builder() - .name(userIdentity) - .password(userCredential) - .description("test user with user-level privileges") - .role(getRoleReferenceFor(DefaultRoles.USER)) - .deployedVmQuota(REQUIRED_USER_VM_QUOTA) - .isEnabled(true) - .build()); - - context = new RestContextFactory().createContext(provider, userIdentity, userCredential, ImmutableSet. of( - new Log4JLoggingModule(), new SshjSshClientModule()), overrides); - } else { - context = rootContext; } session = context.getApi().getCurrentSession(); @@ -238,21 +197,15 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseVersionedServ setupRequiredClients(); } - @AfterClass(alwaysRun = true) - protected void tearDown() { - if (createdAdminUser != null) { - adminContext.getApi().getUserClient().deleteUser(createdAdminUser.getHref()); - } - if (createdUser != null) { - adminContext.getApi().getUserClient().deleteUser(createdUser.getHref()); - } - if (context != null) - context.close(); - if (adminContext != null) - adminContext.close(); + public static String datetimeStamp() { + return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); } public Reference getRoleReferenceFor(String name) { + return getRoleReferenceFor(name, adminContext); + } + + public static Reference getRoleReferenceFor(String name, RestContext adminContext) { RoleReferences roles = adminContext.getApi().getQueryClient().roleReferencesQueryAll(); // wrapped in a builder to strip out unwanted xml cruft that the api chokes on return Reference.builder().fromReference(Iterables.find(roles.getReferences(), ReferencePredicates.nameEquals(name))).build(); @@ -264,7 +217,7 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseVersionedServ public User randomTestUser(String prefix, Reference role) { return User.builder() - .name(name(prefix)+random.nextInt(999999)) + .name(name(prefix)+datetimeStamp()) .fullName("testFullName") .emailAddress("test@test.com") .telephone("555-1234") diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/VCloudDirectorTestSession.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/VCloudDirectorTestSession.java new file mode 100644 index 0000000000..053871ab5e --- /dev/null +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/internal/VCloudDirectorTestSession.java @@ -0,0 +1,160 @@ +package org.jclouds.vcloud.director.v1_5.internal; + +import static com.google.common.base.Objects.equal; +import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.REF_REQ_LIVE; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; + +import java.net.URI; +import java.util.Properties; + +import org.jclouds.logging.log4j.config.Log4JLoggingModule; +import org.jclouds.rest.RestContext; +import org.jclouds.rest.RestContextFactory; +import org.jclouds.sshj.config.SshjSshClientModule; +import org.jclouds.vcloud.director.v1_5.VCloudDirectorContext; +import org.jclouds.vcloud.director.v1_5.admin.VCloudDirectorAdminAsyncClient; +import org.jclouds.vcloud.director.v1_5.admin.VCloudDirectorAdminClient; +import org.jclouds.vcloud.director.v1_5.domain.Link; +import org.jclouds.vcloud.director.v1_5.domain.Reference; +import org.jclouds.vcloud.director.v1_5.domain.Role.DefaultRoles; +import org.jclouds.vcloud.director.v1_5.domain.User; +import org.jclouds.vcloud.director.v1_5.predicates.ReferencePredicates; +import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorAsyncClient; +import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorClient; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.inject.Module; + +public class VCloudDirectorTestSession { + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + + private String provider; + private String identity; + private String credential; + private Properties overrides; + private String endpoint; + + public Builder provider(String provider) { + this.provider = provider; + return this; + } + + public Builder identity(String identity) { + this.identity = identity; + return this; + } + + public Builder credential(String credential) { + this.credential = credential; + return this; + } + + public Builder overrides(Properties overrides) { + this.overrides = overrides; + return this; + } + + public Builder endpoint(String endpoint) { + this.endpoint = endpoint; + return this; + } + + public VCloudDirectorTestSession build() { + return new VCloudDirectorTestSession(provider, identity, credential, overrides, endpoint); + } + } + + private RestContext adminContext; + private RestContext userContext; + + private User createdAdminUser; + private User createdUser; + + private VCloudDirectorTestSession(String provider, String identity, String credential, Properties overrides, String endpoint) { + VCloudDirectorContext rootContext = VCloudDirectorContext.class.cast( + new RestContextFactory().createContext(provider, identity, credential, ImmutableSet. of( + new Log4JLoggingModule(), new SshjSshClientModule()), overrides)); + + if (rootContext.getApi().getCurrentSession().getLinks().contains(Link.builder() + .rel("down") + .type("application/vnd.vmware.admin.vcloud+xml") + .href(URI.create(endpoint+"/admin/")) + .build())) { + + adminContext = rootContext.getAdminContext(); + + Reference orgRef = Iterables.getFirst(rootContext.getApi().getOrgClient().getOrgList().getOrgs(), null) + .toAdminReference(endpoint); + assertNotNull(orgRef, String.format(REF_REQ_LIVE, "admin org")); + + Reference userRef = Iterables.find(adminContext.getApi().getOrgClient().getOrg(orgRef.getHref()).getUsers(), + ReferencePredicates.nameEquals(adminContext.getApi().getCurrentSession().getUser())); + + User user = adminContext.getApi().getUserClient().getUser(userRef.getHref()); + Reference orgAdmin = user.getRole(); + assertTrue(equal(orgAdmin.getName(), DefaultRoles.ORG_ADMIN), "must give org admin or user-only credentials"); + + String adminIdentity = "testAdmin"+BaseVCloudDirectorClientLiveTest.datetimeStamp(); + String adminCredential = "testAdminPassword"; + + createdAdminUser = rootContext.getAdminContext().getApi().getUserClient().createUser(orgRef.getHref(), User.builder() + .name(adminIdentity) + .password(adminCredential) + .description("test user with user-level privileges") + .role(orgAdmin) + .deployedVmQuota(BaseVCloudDirectorClientLiveTest.REQUIRED_ADMIN_VM_QUOTA) + .isEnabled(true) + .build()); + + rootContext.close(); rootContext = null; + + adminContext = VCloudDirectorContext.class.cast(new RestContextFactory().createContext(provider, adminIdentity, adminCredential, ImmutableSet. of( + new Log4JLoggingModule(), new SshjSshClientModule()), overrides)).getAdminContext(); + String userIdentity = "test"+BaseVCloudDirectorClientLiveTest.datetimeStamp(); + String userCredential = "testPassword"; + + createdUser = adminContext.getApi().getUserClient().createUser(orgRef.getHref(), User.builder() + .name(userIdentity) + .password(userCredential) + .description("test user with user-level privileges") + .role(BaseVCloudDirectorClientLiveTest.getRoleReferenceFor(DefaultRoles.USER, adminContext)) + .deployedVmQuota(BaseVCloudDirectorClientLiveTest.REQUIRED_USER_VM_QUOTA) + .isEnabled(true) + .build()); + + userContext = new RestContextFactory().createContext(provider, userIdentity, userCredential, ImmutableSet. of( + new Log4JLoggingModule(), new SshjSshClientModule()), overrides); + } else { + userContext = rootContext; + } + } + + public void close() { + if (createdUser != null) { + adminContext.getApi().getUserClient().deleteUser(createdUser.getHref()); + } + if (userContext != null) + userContext.close(); + if (createdAdminUser != null) { + // TODO: may have to preserve root context if we can't delete the user for it's own context here + adminContext.getApi().getUserClient().deleteUser(createdAdminUser.getHref()); + } + if (adminContext != null) + adminContext.close(); + } + + public RestContext getUserContext() { + return userContext; + } + + public RestContext getAdminContext() { + return adminContext; + } +}