Merge branch 'issue-830-vapp-metadata' of https://github.com/aledsage/jclouds

* 'issue-830-vapp-metadata' of https://github.com/aledsage/jclouds:
  Issue #830: added metadata live tests for VApp
This commit is contained in:
Adrian Cole 2012-03-15 14:10:32 -07:00
commit ef84b65b6a
3 changed files with 114 additions and 17 deletions

View File

@ -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<String,String> metadataMap = metadataToMap(metadata);
assertFalse(metadataMap.containsKey(key),
String.format(OBJ_DEL, client+" metadata key", key));
}
public static void checkMetadataFor(String client, Metadata metadata, Map<String, String> expectedMap) {
Map<String,String> actualMap = Checks.metadataToMap(metadata);
assertEquals(actualMap, expectedMap,
String.format(OBJ_FIELD_EQ, client, "metadata entries", expectedMap, actualMap));
}
public static Map<String,String> metadataToMap(Metadata metadata) {
Map<String,String> 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();

View File

@ -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<String,String> 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<String,String> expectedMetadataMap = ImmutableMap.<String,String>builder()
.putAll(oldMetadataMap)
.put(key, value)
.build();
Checks.checkMetadataFor(VAPP, newMetadata, expectedMetadataMap);
}
/**
* @see VAppClient#deleteVApp(URI)
*/

View File

@ -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,6 +200,7 @@ public class VAppTemplateClientLiveTest extends BaseVCloudDirectorClientLiveTest
// FIXME Cleanup after ourselves..
Metadata oldMetadata = vappTemplateClient.getVAppTemplateMetadata(vAppTemplateURI);
Map<String,String> oldMetadataMap = Checks.metadataToMap(oldMetadata);
String uid = ""+random.nextInt();
String key = "mykey-"+uid;
@ -211,16 +212,11 @@ public class VAppTemplateClientLiveTest extends BaseVCloudDirectorClientLiveTest
retryTaskSuccess.apply(task);
Metadata newMetadata = vappTemplateClient.getVAppTemplateMetadata(vAppTemplateURI);
Map<String,String> newMetadataMap = metadataToMap(newMetadata);
assertEquals(newMetadataMap.get(key), val, "newMetadata="+newMetadata);
}
private Map<String,String> metadataToMap(Metadata metadata) {
Map<String,String> result = Maps.newLinkedHashMap();
for (MetadataEntry entry : metadata.getMetadataEntries()) {
result.put(entry.getKey(), entry.getValue());
}
return result;
Map<String,String> expectedMetadataMap = ImmutableMap.<String,String>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<String,String> 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