PageRenderTime 691ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/apis/vcloud/src/test/java/org/jclouds/vcloud/compute/functions/VAppToNodeMetadataTest.java

http://github.com/jclouds/jclouds
Java | 228 lines | 179 code | 28 blank | 21 comment | 0 complexity | 081d23185ebee3a3e896b21ff4ebd22d MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jclouds.vcloud.compute.functions;
  18. import static org.testng.Assert.assertEquals;
  19. import static org.testng.Assert.assertNotNull;
  20. import java.io.InputStream;
  21. import java.util.Map;
  22. import java.util.NoSuchElementException;
  23. import java.util.Set;
  24. import javax.inject.Singleton;
  25. import org.jclouds.cim.xml.ResourceAllocationSettingDataHandler;
  26. import org.jclouds.collect.Memoized;
  27. import org.jclouds.compute.domain.Hardware;
  28. import org.jclouds.compute.domain.NodeMetadata;
  29. import org.jclouds.domain.Credentials;
  30. import org.jclouds.domain.Location;
  31. import org.jclouds.domain.LocationBuilder;
  32. import org.jclouds.domain.LocationScope;
  33. import org.jclouds.http.functions.ParseSax;
  34. import org.jclouds.http.functions.ParseSax.Factory;
  35. import org.jclouds.http.functions.config.SaxParserModule;
  36. import org.jclouds.vcloud.VCloudApiMetadata;
  37. import org.jclouds.vcloud.compute.config.VCloudComputeServiceDependenciesModule;
  38. import org.jclouds.vcloud.domain.ReferenceType;
  39. import org.jclouds.vcloud.domain.Status;
  40. import org.jclouds.vcloud.domain.VApp;
  41. import org.jclouds.vcloud.domain.internal.VAppImpl;
  42. import org.jclouds.vcloud.xml.VAppHandler;
  43. import org.jclouds.vcloud.xml.ovf.VCloudResourceAllocationSettingDataHandler;
  44. import org.testng.annotations.Test;
  45. import com.google.common.base.Function;
  46. import com.google.common.base.Joiner;
  47. import com.google.common.base.Supplier;
  48. import com.google.common.base.Suppliers;
  49. import com.google.common.collect.ImmutableMap;
  50. import com.google.common.collect.ImmutableSet;
  51. import com.google.common.collect.Maps;
  52. import com.google.inject.AbstractModule;
  53. import com.google.inject.Guice;
  54. import com.google.inject.Injector;
  55. import com.google.inject.Provides;
  56. import com.google.inject.TypeLiteral;
  57. import com.google.inject.name.Names;
  58. /**
  59. * Tests behavior of {@code VAppToNodeMetadata}
  60. *
  61. * @author Adrian Cole
  62. */
  63. @Test(groups = "unit")
  64. public class VAppToNodeMetadataTest {
  65. public Injector createInjectorWithLocation(final Location location) {
  66. return Guice.createInjector(new SaxParserModule(), new AbstractModule() {
  67. @Override
  68. protected void configure() {
  69. Names.bindProperties(binder(), new VCloudApiMetadata().getDefaultProperties());
  70. bind(new TypeLiteral<Function<ReferenceType, Location>>() {
  71. }).to(new TypeLiteral<FindLocationForResource>() {
  72. });
  73. bind(new TypeLiteral<Function<VApp, Hardware>>() {
  74. }).to(new TypeLiteral<HardwareForVApp>() {
  75. });
  76. bind(ResourceAllocationSettingDataHandler.class).to(VCloudResourceAllocationSettingDataHandler.class);
  77. }
  78. @Memoized
  79. @Singleton
  80. @Provides
  81. Supplier<Set<? extends Location>> supplyLocations() {
  82. return Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet.<Location> of(location));
  83. }
  84. @Singleton
  85. @Provides
  86. Map<String, Credentials> supplyCreds() {
  87. return Maps.newConcurrentMap();
  88. }
  89. @Singleton
  90. @Provides
  91. protected Map<Status, NodeMetadata.Status> provideVAppStatusToNodeStatus() {
  92. return VCloudComputeServiceDependenciesModule.toPortableNodeStatus;
  93. }
  94. });
  95. }
  96. public void testWhenVDCIsLocation() {
  97. Location location = new LocationBuilder().id("https://1.1.1.1/api/v1.0/vdc/1").description("description")
  98. .scope(LocationScope.PROVIDER).build();
  99. Injector injector = createInjectorWithLocation(location);
  100. InputStream is = getClass().getResourceAsStream("/vapp-pool.xml");
  101. Factory factory = injector.getInstance(ParseSax.Factory.class);
  102. VApp result = factory.create(injector.getInstance(VAppHandler.class)).parse(is);
  103. VAppToNodeMetadata converter = injector.getInstance(VAppToNodeMetadata.class);
  104. NodeMetadata node = converter.apply(result);
  105. assertNotNull(node);
  106. assertEquals(node.getUserMetadata(), ImmutableMap.<String, String>of());
  107. assertEquals(node.getTags(), ImmutableSet.<String>of());
  108. assertEquals(node.getLocation(), location);
  109. assertEquals(node.getPrivateAddresses(), ImmutableSet.of("172.16.7.230"));
  110. assertEquals(node.getPublicAddresses(), ImmutableSet.of());
  111. }
  112. public void testWithMetadataParseException() {
  113. Location location = new LocationBuilder().id("https://1.1.1.1/api/v1.0/vdc/1").description("description")
  114. .scope(LocationScope.PROVIDER).build();
  115. Injector injector = createInjectorWithLocation(location);
  116. InputStream is = getClass().getResourceAsStream("/vapp-pool.xml");
  117. Factory factory = injector.getInstance(ParseSax.Factory.class);
  118. VApp result = factory.create(injector.getInstance(VAppHandler.class)).parse(is);
  119. VAppToNodeMetadata converter = injector.getInstance(VAppToNodeMetadata.class);
  120. ImmutableMap<String, String> metadata = ImmutableMap.<String, String>of();
  121. ImmutableSet<String> tags = ImmutableSet.<String>of();
  122. String description = " user=user_ssoid_1\nuid=3b7bb605-bb30-4e62-a3de-9076b052dee7 label='foo-DEVELOPMENT' date=2013-01-22 17:39:28.252";
  123. result = new VAppImpl(result.getName(), result.getType(), result.getHref(), result.getStatus(), result.getVDC(),
  124. description, result.getTasks(), result.isOvfDescriptorUploaded(), result.getChildren(),
  125. result.getNetworkSection());
  126. NodeMetadata node = converter.apply(result);
  127. assertNotNull(node);
  128. assertEquals(node.getUserMetadata(), metadata);
  129. assertEquals(node.getTags(), tags);
  130. }
  131. public void testWithMetadataNoNewLines() {
  132. Location location = new LocationBuilder().id("https://1.1.1.1/api/v1.0/vdc/1").description("description")
  133. .scope(LocationScope.PROVIDER).build();
  134. Injector injector = createInjectorWithLocation(location);
  135. InputStream is = getClass().getResourceAsStream("/vapp-pool.xml");
  136. Factory factory = injector.getInstance(ParseSax.Factory.class);
  137. VApp result = factory.create(injector.getInstance(VAppHandler.class)).parse(is);
  138. VAppToNodeMetadata converter = injector.getInstance(VAppToNodeMetadata.class);
  139. ImmutableMap<String, String> metadata = ImmutableMap.<String, String>of();
  140. ImmutableSet<String> tags = ImmutableSet.<String>of();
  141. String description = " user=user_ssoid_1 uid=3b7bb605-bb30-4e62-a3de-9076b052dee7 label='foo-DEVELOPMENT' date=2013-01-22 17:39:28.252";
  142. result = new VAppImpl(result.getName(), result.getType(), result.getHref(), result.getStatus(), result.getVDC(),
  143. description, result.getTasks(), result.isOvfDescriptorUploaded(), result.getChildren(),
  144. result.getNetworkSection());
  145. NodeMetadata node = converter.apply(result);
  146. assertNotNull(node);
  147. assertEquals(node.getUserMetadata(), metadata);
  148. assertEquals(node.getTags(), tags);
  149. }
  150. public void testWithEncodedMetadata() {
  151. Location location = new LocationBuilder().id("https://1.1.1.1/api/v1.0/vdc/1").description("description")
  152. .scope(LocationScope.PROVIDER).build();
  153. Injector injector = createInjectorWithLocation(location);
  154. InputStream is = getClass().getResourceAsStream("/vapp-pool.xml");
  155. Factory factory = injector.getInstance(ParseSax.Factory.class);
  156. VApp result = factory.create(injector.getInstance(VAppHandler.class)).parse(is);
  157. VAppToNodeMetadata converter = injector.getInstance(VAppToNodeMetadata.class);
  158. ImmutableMap<String, String> metadata = ImmutableMap.<String, String>of("foo", "bar");
  159. ImmutableSet<String> tags = ImmutableSet.<String>of("tag1", "tag2");
  160. String description = Joiner
  161. .on('\n')
  162. .withKeyValueSeparator("=")
  163. .join(ImmutableMap.<String, String> builder().putAll(metadata)
  164. .put("jclouds_tags", Joiner.on(',').join(tags)).build());
  165. result = new VAppImpl(result.getName(), result.getType(), result.getHref(), result.getStatus(), result.getVDC(),
  166. description, result.getTasks(), result.isOvfDescriptorUploaded(), result.getChildren(),
  167. result.getNetworkSection());
  168. NodeMetadata node = converter.apply(result);
  169. assertNotNull(node);
  170. assertEquals(node.getUserMetadata(), metadata);
  171. assertEquals(node.getTags(), tags);
  172. }
  173. public void testGracefulWhenNoIPs() {
  174. Location location = new LocationBuilder().id("https://1.1.1.1/api/v1.0/vdc/1").description("description")
  175. .scope(LocationScope.PROVIDER).build();
  176. Injector injector = createInjectorWithLocation(location);
  177. InputStream is = getClass().getResourceAsStream("/vapp-none.xml");
  178. Factory factory = injector.getInstance(ParseSax.Factory.class);
  179. VApp result = factory.create(injector.getInstance(VAppHandler.class)).parse(is);
  180. VAppToNodeMetadata converter = injector.getInstance(VAppToNodeMetadata.class);
  181. NodeMetadata node = converter.apply(result);
  182. assertNotNull(node);
  183. assertEquals(node.getLocation(), location);
  184. assertEquals(node.getPrivateAddresses(), ImmutableSet.of());
  185. assertEquals(node.getPublicAddresses(), ImmutableSet.of());
  186. }
  187. @Test(expectedExceptions = NoSuchElementException.class)
  188. public void testGracefulWhenVDCIsNotLocation() {
  189. Location location = new LocationBuilder().id("https://1.1.1.1/api/v1.0/vdc/11111").description("description")
  190. .scope(LocationScope.PROVIDER).build();
  191. Injector injector = createInjectorWithLocation(location);
  192. InputStream is = getClass().getResourceAsStream("/vapp-pool.xml");
  193. Factory factory = injector.getInstance(ParseSax.Factory.class);
  194. VApp result = factory.create(injector.getInstance(VAppHandler.class)).parse(is);
  195. VAppToNodeMetadata converter = injector.getInstance(VAppToNodeMetadata.class);
  196. NodeMetadata node = converter.apply(result);
  197. assertNotNull(node);
  198. assertEquals(node.getLocation(), location);
  199. }
  200. }