make adminContext more robust

This commit is contained in:
danikov 2012-04-03 11:11:49 +01:00
parent 075dc00a13
commit f20b7a902a
5 changed files with 136 additions and 77 deletions

View File

@ -87,29 +87,33 @@ public class CatalogClientLiveTest extends BaseVCloudDirectorClientLiveTest {
if (adminCatalog != null) return;
catalogClient = context.getApi().getCatalogClient();
Reference orgRef = Iterables.getFirst(context.getApi().getOrgClient().getOrgList().getOrgs(), null).toAdminReference(endpoint);
AdminCatalog newCatalog = AdminCatalog.builder()
.name(name("Test Catalog "))
.description("created by CatalogClientLiveTest")
.build();
AdminCatalogClient adminCatalogClient = adminContext.getApi().getCatalogClient();
adminCatalog = adminCatalogClient.createCatalog(orgRef.getHref(), newCatalog);
catalogRef = find(adminCatalog.getLinks(), and(relEquals("alternate"), typeEquals(VCloudDirectorMediaType.CATALOG)));
Metadata newMetadata = Metadata.builder()
.entry(MetadataEntry.builder().entry("KEY", "MARMALADE").build())
.build();
Task mergeCatalogMetadata = adminCatalogClient.getMetadataClient().mergeMetadata(adminCatalog.getHref(), newMetadata);
checkTask(mergeCatalogMetadata);
assertTrue(retryTaskSuccess.apply(mergeCatalogMetadata), String.format(TASK_COMPLETE_TIMELY, "setupRequiredClients"));
if (adminContext != null) {
AdminCatalog newCatalog = AdminCatalog.builder()
.name(name("Test Catalog "))
.description("created by CatalogClientLiveTest")
.build();
AdminCatalogClient adminCatalogClient = adminContext.getApi().getCatalogClient();
adminCatalog = adminCatalogClient.createCatalog(orgRef.getHref(), newCatalog);
catalogRef = find(adminCatalog.getLinks(), and(relEquals("alternate"), typeEquals(VCloudDirectorMediaType.CATALOG)));
Metadata newMetadata = Metadata.builder()
.entry(MetadataEntry.builder().entry("KEY", "MARMALADE").build())
.build();
Task mergeCatalogMetadata = adminCatalogClient.getMetadataClient().mergeMetadata(adminCatalog.getHref(), newMetadata);
checkTask(mergeCatalogMetadata);
assertTrue(retryTaskSuccess.apply(mergeCatalogMetadata), String.format(TASK_COMPLETE_TIMELY, "setupRequiredClients"));
} else {
catalogRef = Reference.builder().href(catalogURI).build();
}
}
@AfterClass(alwaysRun = true)
public void tearDown() {
if (catalogItem != null)
catalogClient.deleteCatalogItem(catalogItem.getHref());
catalogClient.deleteCatalogItem(catalogItem.getHref());
if (media != null)
context.getApi().getMediaClient().deleteMedia(media.getHref());

View File

@ -57,19 +57,21 @@ public class NetworkClientLiveTest extends BaseVCloudDirectorClientLiveTest {
* Convenience reference to API client.
*/
protected NetworkClient networkClient;
private boolean metadataSet = false;
@Override
@BeforeClass(alwaysRun = true)
public void setupRequiredClients() {
networkClient = context.getApi().getNetworkClient();
adminContext.getApi().getNetworkClient().getMetadataClient().setMetadata(toAdminUri(networkURI),
"key", MetadataValue.builder().value("value").build());
}
@AfterClass(groups = { "live" })
@AfterClass(alwaysRun = true)
public void cleanUp() throws Exception {
adminContext.getApi().getNetworkClient().getMetadataClient()
.deleteMetadataEntry(toAdminUri(networkURI), "key");
if (metadataSet) {
adminContext.getApi().getNetworkClient().getMetadataClient()
.deleteMetadataEntry(toAdminUri(networkURI), "key");
}
}
@Test(description = "GET /network/{id}")
@ -87,8 +89,18 @@ public class NetworkClientLiveTest extends BaseVCloudDirectorClientLiveTest {
Checks.checkOrgNetwork(network);
}
@Test(description = "GET /network/{id}/metadata")
private void setupMetadata() {
adminContext.getApi().getNetworkClient().getMetadataClient().setMetadata(toAdminUri(networkURI),
"key", MetadataValue.builder().value("value").build());
metadataSet = true;
}
@Test(description = "GET /network/{id}/metadata", dependsOnMethods = { "testGetNetwork" })
public void testGetMetadata() {
if (adminContext != null) {
setupMetadata();
}
Metadata metadata = networkClient.getMetadataClient().getMetadata(networkURI);
// required for testing
assertFalse(Iterables.isEmpty(metadata.getMetadataEntries()),
@ -109,7 +121,7 @@ public class NetworkClientLiveTest extends BaseVCloudDirectorClientLiveTest {
}
}
@Test(description = "GET /network/{id}/metadata/{key}")
@Test(description = "GET /network/{id}/metadata/{key}", dependsOnMethods = { "testGetMetadata" })
public void testGetMetadataValue() {
MetadataValue metadataValue = networkClient.getMetadataClient().getMetadataValue(networkURI, "key");

View File

@ -70,7 +70,7 @@ public class OrgClientLiveTest extends BaseVCloudDirectorClientLiveTest {
public void cleanUp() throws Exception {
if (metadataSet) {
adminContext.getApi().getOrgClient().getMetadataClient()
.deleteMetadataEntry(toAdminUri(orgURI), "key");
.deleteMetadataEntry(toAdminUri(orgURI), "KEY");
}
}
@ -112,15 +112,18 @@ public class OrgClientLiveTest extends BaseVCloudDirectorClientLiveTest {
checkOrg(org);
}
@Test(description = "orgClient admin metadata setup", dependsOnMethods = { "testGetOrg" })
public void testSetupMetadata() {
private void setupMetadata() {
adminContext.getApi().getOrgClient().getMetadataClient().setMetadata(toAdminUri(orgURI),
"KEY", MetadataValue.builder().value("VALUE").build());
metadataSet = true;
}
@Test(description = "GET /org/{id}/metadata", dependsOnMethods = { "testSetupMetadata" })
@Test(description = "GET /org/{id}/metadata", dependsOnMethods = { "testGetOrg" })
public void testGetOrgMetadata() {
if (adminContext != null) {
setupMetadata();
}
// Call the method being tested
Metadata metadata = orgClient.getMetadataClient().getMetadata(orgURI);

View File

@ -83,6 +83,7 @@ public class VdcClientLiveTest extends BaseVCloudDirectorClientLiveTest {
private VAppTemplate clonedVAppTemplate;
private VAppTemplate capturedVAppTemplate;
private VAppTemplate uploadedVAppTemplate;
private boolean metadataSet = false;
@AfterClass(alwaysRun = true)
public void cleanUp() throws Exception {
@ -105,8 +106,10 @@ public class VdcClientLiveTest extends BaseVCloudDirectorClientLiveTest {
cleanUpVApp(composedVApp);
}
adminContext.getApi().getVdcClient().getMetadataClient()
.deleteMetadataEntry(toAdminUri(vdcURI), "key");
if (metadataSet) {
adminContext.getApi().getVdcClient().getMetadataClient()
.deleteMetadataEntry(toAdminUri(vdcURI), "key");
}
}
@Override
@ -307,14 +310,18 @@ public class VdcClientLiveTest extends BaseVCloudDirectorClientLiveTest {
}
@Test(description = "vdcClient admin metadata configuration", dependsOnMethods = { "testGetVdc" } )
public void testSetupMetadata() {
private void setupMetadata() {
adminContext.getApi().getVdcClient().getMetadataClient().setMetadata(toAdminUri(vdcURI),
"key", MetadataValue.builder().value("value").build());
metadataSet = true;
}
@Test(description = "GET /vdc/{id}/metadata", dependsOnMethods = { "testSetupMetadata" } )
@Test(description = "GET /vdc/{id}/metadata", dependsOnMethods = { "testGetVdc" } )
public void testGetMetadata() {
if(adminContext != null) {
setupMetadata();
}
Metadata metadata = vdcClient.getMetadataClient().getMetadata(vdcURI);
// required for testing

View File

@ -18,6 +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;
@ -127,6 +128,7 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseVersionedServ
protected Session session;
protected String catalogId;
protected URI catalogURI;
protected URI vAppTemplateURI;
protected URI mediaURI;
protected URI networkURI;
@ -136,6 +138,9 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseVersionedServ
protected final Set<String> vAppNames = Sets.newLinkedHashSet();
protected static final Random random = new Random();
private User createdAdminUser;
private User createdUser;
protected BaseVCloudDirectorClientLiveTest() {
provider = "vcloud-director";
}
@ -169,44 +174,63 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseVersionedServ
VCloudDirectorContext rootContext = VCloudDirectorContext.class.cast(
new RestContextFactory().createContext(provider, identity, credential, ImmutableSet.<Module> of(
new Log4JLoggingModule(), new SshjSshClientModule()), overrides));
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"));
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.<Module> of(
new Log4JLoggingModule(), new SshjSshClientModule()), overrides)).getAdminContext();
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.<Module> of(
new Log4JLoggingModule(), new SshjSshClientModule()), overrides);
} else {
context = rootContext;
}
String adminIdentity = "testAdmin"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String adminCredential = "testAdminPassword";
rootContext.getAdminContext().getApi().getUserClient().createUser(orgRef.getHref(), User.builder()
.name(adminIdentity)
.password(adminCredential)
.description("test user with user-level privileges") //TODO desc
.role(getRoleReferenceFor(DefaultRoles.ORG_ADMIN))
.deployedVmQuota(REQUIRED_ADMIN_VM_QUOTA)
.isEnabled(true)
.build());
rootContext.close(); rootContext = null;
adminContext = VCloudDirectorContext.class.cast(new RestContextFactory().createContext(provider, adminIdentity, adminCredential, ImmutableSet.<Module> of(
new Log4JLoggingModule(), new SshjSshClientModule()), overrides)).getAdminContext();
adminSession = adminContext.getApi().getCurrentSession();
adminContext.utils().injector().injectMembers(this);
String userIdentity = "test"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String userCredential = "testPassword";
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.<Module> of(
new Log4JLoggingModule(), new SshjSshClientModule()), overrides);
session = context.getApi().getCurrentSession();
context.utils().injector().injectMembers(this);
@ -214,6 +238,20 @@ 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 Reference getRoleReferenceFor(String name) {
RoleReferences roles = adminContext.getApi().getQueryClient().roleReferencesQueryAll();
// wrapped in a builder to strip out unwanted xml cruft that the api chokes on
@ -245,6 +283,9 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseVersionedServ
// TODO change properties to URI, not id
protected void initTestParametersFromPropertiesOrLazyDiscover() {
catalogId = Strings.emptyToNull(System.getProperty("test." + provider + ".catalog-id"));
if (catalogId != null) {
catalogURI = URI.create(endpoint + "/catalog/" + catalogId);
}
String vAppTemplateId = Strings.emptyToNull(System.getProperty("test." + provider + ".vapptemplate-id"));
if (vAppTemplateId != null)
@ -287,14 +328,6 @@ public abstract class BaseVCloudDirectorClientLiveTest extends BaseVersionedServ
}
}
}
@AfterClass(alwaysRun = true)
protected void tearDown() {
if (context != null)
context.close();
if (adminContext != null)
adminContext.close();
}
public URI toAdminUri(Reference ref) {
return toAdminUri(ref.getHref());