Do not create new users just for the tests, run them with the passed in credentials

This commit is contained in:
Andrew Donald Kennedy 2012-04-13 16:01:21 +01:00
parent dab1dd69fc
commit 437bb83496
4 changed files with 27 additions and 65 deletions

View File

@ -82,7 +82,6 @@ public abstract class BaseContextLiveTest<C extends Closeable> {
} }
protected void initializeContext() { protected void initializeContext() {
if (context != null)
Closeables.closeQuietly(context); Closeables.closeQuietly(context);
context = createContext(setupProperties(), setupModules()); context = createContext(setupProperties(), setupModules());
} }

View File

@ -197,6 +197,8 @@ public class VCloudDirectorMediaType {
public static final String ENTITY = "application/vnd.vmware.vcloud.entity+xml"; public static final String ENTITY = "application/vnd.vmware.vcloud.entity+xml";
public static final String ADMIN = "application/vnd.vmware.admin.vcloud+xml";
/** /**
* All acceptable media types. * All acceptable media types.
*/ */
@ -220,7 +222,7 @@ public class VCloudDirectorMediaType {
ADMIN_ORG_NETWORK, USER, ROLE, DEPLOY_VAPP_PARAMS, RECOMPOSE_VAPP_PARAMS, ADMIN_ORG_NETWORK, USER, ROLE, DEPLOY_VAPP_PARAMS, RECOMPOSE_VAPP_PARAMS,
RELOCATE_VM_PARAMS, UNDEPLOY_VAPP_PARAMS, ADMIN_VDC, MEDIA_PARAMS, RELOCATE_VM_PARAMS, UNDEPLOY_VAPP_PARAMS, ADMIN_VDC, MEDIA_PARAMS,
RUNTIME_INFO_SECTION, SCREEN_TICKET, VAPP_NETWORK, RUNTIME_INFO_SECTION, SCREEN_TICKET, VAPP_NETWORK,
TEXT_XML, ADMIN_VDC, NETWORK_POOL, ADMIN_ORG, ENTITY TEXT_XML, ADMIN_VDC, NETWORK_POOL, ADMIN_ORG, ENTITY, ADMIN
); );
// NOTE These lists must be updated whenever a new media type constant is added. // NOTE These lists must be updated whenever a new media type constant is added.

View File

@ -179,8 +179,6 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseContextLiveTe
.overrides(overrides) .overrides(overrides)
.build(); .build();
System.err.println("*** " + endpoint + " ***");
context = testSession.getUserContext(); context = testSession.getUserContext();
adminContext = testSession.getAdminContext(); adminContext = testSession.getAdminContext();

View File

@ -2,6 +2,7 @@ package org.jclouds.vcloud.director.v1_5.internal;
import static com.google.common.base.Objects.equal; import static com.google.common.base.Objects.equal;
import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.REF_REQ_LIVE; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.REF_REQ_LIVE;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
@ -14,16 +15,19 @@ import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.rest.RestContext; import org.jclouds.rest.RestContext;
import org.jclouds.sshj.config.SshjSshClientModule; import org.jclouds.sshj.config.SshjSshClientModule;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorContext; import org.jclouds.vcloud.director.v1_5.VCloudDirectorContext;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
import org.jclouds.vcloud.director.v1_5.admin.VCloudDirectorAdminAsyncClient; 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.admin.VCloudDirectorAdminClient;
import org.jclouds.vcloud.director.v1_5.domain.Link; 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.Reference;
import org.jclouds.vcloud.director.v1_5.domain.Role.DefaultRoles; 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.domain.User;
import org.jclouds.vcloud.director.v1_5.predicates.LinkPredicates;
import org.jclouds.vcloud.director.v1_5.predicates.ReferencePredicates; 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.VCloudDirectorAsyncClient;
import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorClient; import org.jclouds.vcloud.director.v1_5.user.VCloudDirectorClient;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
@ -73,11 +77,8 @@ public class VCloudDirectorTestSession implements Closeable {
} }
} }
private VCloudDirectorContext userContext;
private RestContext<VCloudDirectorAdminClient, VCloudDirectorAdminAsyncClient> adminContext; private RestContext<VCloudDirectorAdminClient, VCloudDirectorAdminAsyncClient> adminContext;
private RestContext<VCloudDirectorClient, VCloudDirectorAsyncClient> userContext;
private User createdAdminUser;
private User createdUser;
private VCloudDirectorTestSession(String provider, String identity, String credential, Properties overrides, String endpoint) { private VCloudDirectorTestSession(String provider, String identity, String credential, Properties overrides, String endpoint) {
ContextBuilder builder = ContextBuilder.newBuilder(provider) ContextBuilder builder = ContextBuilder.newBuilder(provider)
@ -85,74 +86,36 @@ public class VCloudDirectorTestSession implements Closeable {
.endpoint(endpoint) .endpoint(endpoint)
.modules(ImmutableSet.<Module> of(new Log4JLoggingModule(), new SshjSshClientModule())) .modules(ImmutableSet.<Module> of(new Log4JLoggingModule(), new SshjSshClientModule()))
.overrides(overrides); .overrides(overrides);
VCloudDirectorContext rootContext = VCloudDirectorContext.class.cast(builder.build());
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.value()), "must give org admin or user-only credentials");
String adminIdentity = "testAdmin"+BaseVCloudDirectorClientLiveTest.getTestDateTimeStamp();
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());
Closeables.closeQuietly(rootContext);
builder.credentials(adminIdentity, adminCredential);
adminContext = VCloudDirectorContext.class.cast(builder.build()).getAdminContext();
String userIdentity = "test"+BaseVCloudDirectorClientLiveTest.getTestDateTimeStamp();
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.value(), adminContext))
.deployedVmQuota(BaseVCloudDirectorClientLiveTest.REQUIRED_USER_VM_QUOTA)
.isEnabled(true)
.build());
builder.credentials(userIdentity, userCredential);
userContext = VCloudDirectorContext.class.cast(builder.build()); userContext = VCloudDirectorContext.class.cast(builder.build());
} else {
userContext = rootContext; // Look for the admin link in the current session
Link admin = Iterables.tryFind(
userContext.getApi().getCurrentSession().getLinks(),
Predicates.and(LinkPredicates.relEquals(Link.Rel.DOWN), ReferencePredicates.typeEquals(VCloudDirectorMediaType.ADMIN)))
.orNull();
// Get the admin context if the link exists
if (admin != null) {
adminContext = userContext.getAdminContext();
// Lookup the user details
Reference orgRef = Iterables.getFirst(userContext.getApi().getOrgClient().getOrgList().getOrgs(), null)
.toAdminReference(endpoint);
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());
// Check that the user has the org admin role
Reference userRole = user.getRole();
assertEquals(userRole.getName(), DefaultRoles.ORG_ADMIN.value());
} }
} }
@Override @Override
public void close() { public void close() {
if (createdUser != null) { Closeables.closeQuietly(userContext);
adminContext.getApi().getUserClient().deleteUser(createdUser.getHref()); Closeables.closeQuietly(adminContext);
}
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<VCloudDirectorClient, VCloudDirectorAsyncClient> getUserContext() { public RestContext<VCloudDirectorClient, VCloudDirectorAsyncClient> getUserContext() {