From 6a03592786a6ad756421f5966ed6ebe670f1c569 Mon Sep 17 00:00:00 2001 From: Aled Sage Date: Thu, 15 Mar 2012 18:00:53 +0000 Subject: [PATCH] Issue #830: added metadata live tests for VApp --- .../vcloud/director/v1_5/domain/Checks.java | 31 +++++++- .../v1_5/features/VAppClientLiveTest.java | 75 ++++++++++++++++++- .../features/VAppTemplateClientLiveTest.java | 25 +++---- 3 files changed, 114 insertions(+), 17 deletions(-) diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/domain/Checks.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/domain/Checks.java index 529c678b88..641317845c 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/domain/Checks.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/domain/Checks.java @@ -22,6 +22,7 @@ import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.C import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.MUST_BE_WELL_FORMED_FMT; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.MUST_CONTAIN_FMT; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.NOT_NULL_OBJECT_FMT; +import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_DEL; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_ATTRB_REQ; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_EQ; import static org.jclouds.vcloud.director.v1_5.VCloudDirectorLiveTestConstants.OBJ_FIELD_GTE_0; @@ -37,6 +38,7 @@ import static org.testng.Assert.fail; import java.net.URI; import java.util.Collection; +import java.util.Map; import java.util.Set; import java.util.UUID; @@ -59,6 +61,7 @@ import org.jclouds.vcloud.director.v1_5.domain.ovf.VirtualHardwareSection; import org.jclouds.vcloud.director.v1_5.domain.ovf.VirtualSystem; import org.jclouds.vcloud.director.v1_5.domain.ovf.environment.EnvironmentType; +import com.beust.jcommander.internal.Maps; import com.google.common.base.Splitter; import com.google.common.collect.Iterables; import com.google.common.net.InetAddresses; @@ -542,18 +545,42 @@ public class Checks { } public static void checkMetadataValueFor(String client, MetadataValue metadataValue) { + checkMetadataValueFor(client, metadataValue, "value"); + } + + public static void checkMetadataValueFor(String client, MetadataValue metadataValue, String expectedValue) { // Check required fields String value = metadataValue.getValue(); assertNotNull(value, String.format(OBJ_FIELD_ATTRB_REQ, client, "MetadataEntry", metadataValue.toString(), "value")); - assertEquals(value, "value", - String.format(OBJ_FIELD_EQ, client, "metadataEntry.value", "value", value)); + assertEquals(value, expectedValue, + String.format(OBJ_FIELD_EQ, client, "metadataEntry.value", expectedValue, value)); // Check parent type checkResourceType(metadataValue); } + public static void checkMetadataKeyAbsentFor(String client, Metadata metadata, String key) { + Map metadataMap = metadataToMap(metadata); + assertFalse(metadataMap.containsKey(key), + String.format(OBJ_DEL, client+" metadata key", key)); + } + + public static void checkMetadataFor(String client, Metadata metadata, Map expectedMap) { + Map actualMap = Checks.metadataToMap(metadata); + assertEquals(actualMap, expectedMap, + String.format(OBJ_FIELD_EQ, client, "metadata entries", expectedMap, actualMap)); + } + + public static Map metadataToMap(Metadata metadata) { + Map result = Maps.newLinkedHashMap(); + for (MetadataEntry entry : metadata.getMetadataEntries()) { + result.put(entry.getKey(), entry.getValue()); + } + return result; + } + public static void checkVApp(VApp vApp) { // Check optional fields Owner owner = vApp.getOwner(); diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VAppClientLiveTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VAppClientLiveTest.java index 5779c285da..d70b9b4f0e 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VAppClientLiveTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VAppClientLiveTest.java @@ -42,6 +42,9 @@ import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; import java.io.IOException; +import java.net.URI; +import java.util.Map; +import java.util.Random; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -49,6 +52,7 @@ import org.jclouds.vcloud.director.v1_5.VCloudDirectorException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; import org.jclouds.vcloud.director.v1_5.domain.AccessSetting; import org.jclouds.vcloud.director.v1_5.domain.AccessSettings; +import org.jclouds.vcloud.director.v1_5.domain.Checks; import org.jclouds.vcloud.director.v1_5.domain.ControlAccessParams; import org.jclouds.vcloud.director.v1_5.domain.DeployVAppParams; import org.jclouds.vcloud.director.v1_5.domain.GuestCustomizationSection; @@ -56,6 +60,9 @@ import org.jclouds.vcloud.director.v1_5.domain.InstantiateVAppTemplateParams; import org.jclouds.vcloud.director.v1_5.domain.InstantiationParams; import org.jclouds.vcloud.director.v1_5.domain.LeaseSettingsSection; import org.jclouds.vcloud.director.v1_5.domain.MediaInsertOrEjectParams; +import org.jclouds.vcloud.director.v1_5.domain.Metadata; +import org.jclouds.vcloud.director.v1_5.domain.MetadataEntry; +import org.jclouds.vcloud.director.v1_5.domain.MetadataValue; import org.jclouds.vcloud.director.v1_5.domain.NetworkConfigSection; import org.jclouds.vcloud.director.v1_5.domain.NetworkConfiguration; import org.jclouds.vcloud.director.v1_5.domain.NetworkConnectionSection; @@ -91,8 +98,10 @@ import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.base.Strings; import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; +import com.google.common.collect.Maps; /** * Tests behavior of {@code VAppClient} @@ -115,7 +124,7 @@ public class VAppClientLiveTest extends BaseVCloudDirectorClientLiveTest { protected VAppClient vAppClient; protected VAppTemplateClient vAppTemplateClient; protected VdcClient vdcClient; - protected MetadataClient metadataClient; + protected MetadataClient.Writeable metadataClient; /* * Objects shared between tests. @@ -127,6 +136,8 @@ public class VAppClientLiveTest extends BaseVCloudDirectorClientLiveTest { private VApp vApp; private VAppTemplate vAppTemplate; + private final Random random = new Random(); + @BeforeClass(inheritGroups = true) @Override public void setupRequiredClients() { @@ -771,6 +782,68 @@ public class VAppClientLiveTest extends BaseVCloudDirectorClientLiveTest { // assertEquals(modified.getInfo(), "New Info"); } + @Test(testName = "GET /vApp/{id}/metadata", dependsOnMethods = { "testGetVApp" }) + public void testGetMetadata() { + Metadata metadata = metadataClient.getMetadata(vApp.getHref()); + + Checks.checkMetadataFor(VAPP, metadata); + } + + @Test(testName = "PUT & GET /vApp/{id}/metadata", dependsOnMethods = { "testGetMetadata" }) + public void testSetAndGetMetadataValue() { + // Store a value + String key = ""+random.nextInt(); + String value = ""+random.nextInt(); + MetadataValue metadataValue = MetadataValue.builder().value(value).build(); + metadataClient.setMetadata(vApp.getHref(), key, metadataValue); + + // Retrieve the value, and assert it was set correctly + MetadataValue newMetadataValue = metadataClient.getMetadataValue(vApp.getHref(), key); + Checks.checkMetadataValueFor(VAPP, newMetadataValue, value); + } + + @Test(testName = "DELETE /vApp/{id}/metadata/{key}", dependsOnMethods = { "testSetAndGetMetadataValue" }) + public void testDeleteMetadataEntry() { + // Store a value, to be deleted + String key = ""+random.nextInt(); + MetadataValue metadataValue = MetadataValue.builder().value("myval").build(); + metadataClient.setMetadata(vApp.getHref(), key, metadataValue); + + // Delete the entry + Task task = metadataClient.deleteMetadataEntry(vApp.getHref(), key); + retryTaskSuccess.apply(task); + + // Confirm the entry has been deleted + Metadata newMetadata = metadataClient.getMetadata(vApp.getHref()); + Checks.checkMetadataKeyAbsentFor(VAPP, newMetadata, key); + } + + @Test(testName = "POST /vApp/{id}/metadata", dependsOnMethods = { "testGetMetadata" }) + public void testMergeMetadata() { + Metadata oldMetadata = metadataClient.getMetadata(vApp.getHref()); + Map oldMetadataMap = Checks.metadataToMap(oldMetadata); + + // Store a value, to be deleted + String key = ""+random.nextInt(); + String value = ""+random.nextInt(); + Metadata addedMetadata = Metadata.builder() + .entry(MetadataEntry.builder() + .key(key) + .value(value) + .build()) + .build(); + Task task = metadataClient.mergeMetadata(vApp.getHref(), addedMetadata); + retryTaskSuccess.apply(task); + + // Confirm the entry contains everything that was there, and everything that was being added + Metadata newMetadata = metadataClient.getMetadata(vApp.getHref()); + Map expectedMetadataMap = ImmutableMap.builder() + .putAll(oldMetadataMap) + .put(key, value) + .build(); + Checks.checkMetadataFor(VAPP, newMetadata, expectedMetadataMap); + } + /** * @see VAppClient#deleteVApp(URI) */ diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VAppTemplateClientLiveTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VAppTemplateClientLiveTest.java index 595357cf4a..8b4c27b701 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VAppTemplateClientLiveTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VAppTemplateClientLiveTest.java @@ -53,8 +53,8 @@ import org.jclouds.vcloud.director.v1_5.internal.BaseVCloudDirectorClientLiveTes import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import com.beust.jcommander.internal.Maps; import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; @@ -200,7 +200,8 @@ public class VAppTemplateClientLiveTest extends BaseVCloudDirectorClientLiveTest // FIXME Cleanup after ourselves.. Metadata oldMetadata = vappTemplateClient.getVAppTemplateMetadata(vAppTemplateURI); - + Map oldMetadataMap = Checks.metadataToMap(oldMetadata); + String uid = ""+random.nextInt(); String key = "mykey-"+uid; String val = "myval-"+uid; @@ -211,16 +212,11 @@ public class VAppTemplateClientLiveTest extends BaseVCloudDirectorClientLiveTest retryTaskSuccess.apply(task); Metadata newMetadata = vappTemplateClient.getVAppTemplateMetadata(vAppTemplateURI); - Map newMetadataMap = metadataToMap(newMetadata); - assertEquals(newMetadataMap.get(key), val, "newMetadata="+newMetadata); - } - - private Map metadataToMap(Metadata metadata) { - Map result = Maps.newLinkedHashMap(); - for (MetadataEntry entry : metadata.getMetadataEntries()) { - result.put(entry.getKey(), entry.getValue()); - } - return result; + Map expectedMetadataMap = ImmutableMap.builder() + .putAll(oldMetadataMap) + .put(key, val) + .build(); + Checks.checkMetadataFor("vAppTemplate", newMetadata, expectedMetadataMap); } @Test @@ -247,12 +243,13 @@ public class VAppTemplateClientLiveTest extends BaseVCloudDirectorClientLiveTest final Task task = vappTemplateClient.editVAppTemplateMetadataValue(vAppTemplateURI, key, metadataValue); retryTaskSuccess.apply(task); + // Then delete the entry final Task deletionTask = vappTemplateClient.deleteVAppTemplateMetadataValue(vAppTemplateURI, key); retryTaskSuccess.apply(deletionTask); + // Then confirm the entry is not there Metadata newMetadata = vappTemplateClient.getVAppTemplateMetadata(vAppTemplateURI); - Map newMetadataMap = metadataToMap(newMetadata); - assertFalse(newMetadataMap.containsKey(key), "newMetadata="+newMetadata); + Checks.checkMetadataKeyAbsentFor("vAppTemplate", newMetadata, key); } @Test // FIXME Failing because template does not have a guest customization section to be got