PageRenderTime 997ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/compute/functions/AWSRunningInstanceToNodeMetadataTest.java

http://github.com/jclouds/jclouds
Java | 217 lines | 172 code | 29 blank | 16 comment | 1 complexity | eff6d60332034a5ae08ad3bd37a80f41 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.aws.ec2.compute.functions;
  18. import static org.testng.Assert.assertEquals;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import org.jclouds.aws.ec2.AWSEC2ApiMetadata;
  22. import org.jclouds.aws.ec2.domain.AWSRunningInstance;
  23. import org.jclouds.aws.ec2.domain.MonitoringState;
  24. import org.jclouds.compute.domain.Hardware;
  25. import org.jclouds.compute.domain.Image;
  26. import org.jclouds.compute.domain.NodeMetadataBuilder;
  27. import org.jclouds.compute.domain.NodeMetadata.Status;
  28. import org.jclouds.compute.functions.GroupNamingConvention;
  29. import org.jclouds.date.DateService;
  30. import org.jclouds.domain.Credentials;
  31. import org.jclouds.domain.Location;
  32. import org.jclouds.domain.LocationBuilder;
  33. import org.jclouds.domain.LocationScope;
  34. import org.jclouds.ec2.compute.config.EC2ComputeServiceDependenciesModule;
  35. import org.jclouds.ec2.compute.domain.RegionAndName;
  36. import org.jclouds.ec2.compute.functions.ImagesToRegionAndIdMap;
  37. import org.jclouds.ec2.domain.Attachment;
  38. import org.jclouds.ec2.domain.BlockDevice;
  39. import org.jclouds.ec2.domain.Hypervisor;
  40. import org.jclouds.ec2.domain.InstanceState;
  41. import org.jclouds.ec2.domain.RootDeviceType;
  42. import org.testng.annotations.BeforeTest;
  43. import org.testng.annotations.Test;
  44. import com.google.common.base.Supplier;
  45. import com.google.common.base.Suppliers;
  46. import com.google.common.cache.CacheBuilder;
  47. import com.google.common.cache.CacheLoader;
  48. import com.google.common.cache.LoadingCache;
  49. import com.google.common.collect.ImmutableMap;
  50. import com.google.common.collect.ImmutableSet;
  51. import com.google.common.collect.Iterables;
  52. import com.google.inject.AbstractModule;
  53. import com.google.inject.Guice;
  54. import com.google.inject.name.Names;
  55. @Test(groups = "unit")
  56. public class AWSRunningInstanceToNodeMetadataTest {
  57. private static final String defaultRegion = "us-east-1";
  58. static Location provider = new LocationBuilder().scope(LocationScope.REGION).id(defaultRegion).description(
  59. defaultRegion).build();
  60. private DateService dateService;
  61. @BeforeTest
  62. protected void setUpInjector() {
  63. dateService = Guice.createInjector().getInstance(DateService.class);
  64. assert dateService != null;
  65. }
  66. @Test
  67. public void test2Nodes() {
  68. AWSRunningInstanceToNodeMetadata parser = createNodeParser(ImmutableSet.<Hardware> of(), ImmutableSet
  69. .<Location> of(), ImmutableSet.<Image> of(), ImmutableMap.<String, Credentials> of());
  70. ImmutableSet<AWSRunningInstance> contents = ImmutableSet.of(new AWSRunningInstance.Builder()
  71. .region(defaultRegion)
  72. .instanceId("i-911444f0")
  73. .imageId("ami-63be790a")
  74. .instanceState(InstanceState.RUNNING)
  75. .rawState("running")
  76. .privateDnsName("ip-10-212-81-7.ec2.internal")
  77. .dnsName("ec2-174-129-173-155.compute-1.amazonaws.com")
  78. .keyName("jclouds#zkclustertest#23")
  79. .amiLaunchIndex("0")
  80. .instanceType("t1.micro")
  81. .launchTime(dateService.iso8601DateParse("2011-08-16T13:40:50.000Z"))
  82. .availabilityZone("us-east-1c")
  83. .kernelId("aki-427d952b")
  84. .monitoringState(MonitoringState.DISABLED)
  85. .privateIpAddress("10.212.81.7")
  86. .ipAddress("174.129.173.155")
  87. .securityGroupIdToNames(ImmutableMap.<String, String> of("sg-ef052b86", "jclouds#zkclustertest"))
  88. .rootDeviceType(RootDeviceType.EBS)
  89. .rootDeviceName("/dev/sda1")
  90. .device("/dev/sda1", new BlockDevice("vol-5829fc32", Attachment.Status.ATTACHED, dateService.iso8601DateParse("2011-08-16T13:41:19.000Z"), true))
  91. .virtualizationType("paravirtual")
  92. .tag("Name", "foo")
  93. .tag("Empty", "")
  94. .hypervisor(Hypervisor.XEN)
  95. .build(),
  96. new AWSRunningInstance.Builder()
  97. .region(defaultRegion)
  98. .instanceId("i-931444f2")
  99. .imageId("ami-63be790a")
  100. .instanceState(InstanceState.RUNNING)
  101. .rawState("running")
  102. .privateDnsName("ip-10-212-185-8.ec2.internal")
  103. .dnsName("ec2-50-19-207-248.compute-1.amazonaws.com")
  104. .keyName("jclouds#zkclustertest#23")
  105. .amiLaunchIndex("0")
  106. .instanceType("t1.micro")
  107. .launchTime(dateService.iso8601DateParse("2011-08-16T13:40:50.000Z"))
  108. .availabilityZone("us-east-1c")
  109. .kernelId("aki-427d952b")
  110. .monitoringState(MonitoringState.DISABLED)
  111. .privateIpAddress("10.212.185.8")
  112. .ipAddress("50.19.207.248")
  113. .securityGroupIdToNames(ImmutableMap.<String, String>of("sg-ef052b86", "jclouds#zkclustertest"))
  114. .rootDeviceType(RootDeviceType.EBS)
  115. .rootDeviceName("/dev/sda1")
  116. .device("/dev/sda1", new BlockDevice("vol-5029fc3a", Attachment.Status.ATTACHED, dateService.iso8601DateParse("2011-08-16T13:41:19.000Z"), true))
  117. .virtualizationType("paravirtual")
  118. .hypervisor(Hypervisor.XEN)
  119. .build());
  120. assertEquals(
  121. parser.apply(Iterables.get(contents, 0)).toString(),
  122. new NodeMetadataBuilder()
  123. .status(Status.RUNNING)
  124. .backendStatus("running")
  125. .group("zkclustertest")
  126. .name("foo")
  127. .hostname("ip-10-212-81-7")
  128. .privateAddresses(ImmutableSet.of("10.212.81.7"))
  129. .publicAddresses(ImmutableSet.of("174.129.173.155"))
  130. .imageId("us-east-1/ami-63be790a")
  131. .id("us-east-1/i-911444f0")
  132. .providerId("i-911444f0")
  133. .tags(ImmutableSet.of("Empty"))
  134. .userMetadata(ImmutableMap.of("Name", "foo")).build().toString());
  135. assertEquals(
  136. parser.apply(Iterables.get(contents, 1)).toString(),
  137. new NodeMetadataBuilder()
  138. .status(Status.RUNNING)
  139. .backendStatus("running")
  140. .group("zkclustertest")
  141. .hostname("ip-10-212-185-8")
  142. .privateAddresses(ImmutableSet.of("10.212.185.8"))
  143. .publicAddresses(ImmutableSet.of("50.19.207.248"))
  144. .imageId("us-east-1/ami-63be790a")
  145. .id("us-east-1/i-931444f2")
  146. .providerId("i-931444f2")
  147. .build().toString());
  148. }
  149. protected AWSRunningInstanceToNodeMetadata createNodeParser(final ImmutableSet<Hardware> hardware,
  150. final ImmutableSet<Location> locations, Set<org.jclouds.compute.domain.Image> images,
  151. Map<String, Credentials> credentialStore) {
  152. Map<InstanceState, Status> instanceToNodeStatus = EC2ComputeServiceDependenciesModule.toPortableNodeStatus;
  153. final Map<RegionAndName, ? extends Image> backing = ImagesToRegionAndIdMap.imagesToMap(images);
  154. LoadingCache<RegionAndName, Image> instanceToImage = CacheBuilder.newBuilder().build(new CacheLoader<RegionAndName, Image>() {
  155. @Override
  156. public Image load(RegionAndName key) throws Exception {
  157. return backing.get(key);
  158. }
  159. });
  160. return createNodeParser(hardware, locations, credentialStore, instanceToNodeStatus, instanceToImage);
  161. }
  162. private AWSRunningInstanceToNodeMetadata createNodeParser(final ImmutableSet<Hardware> hardware,
  163. final ImmutableSet<Location> locations, Map<String, Credentials> credentialStore,
  164. Map<InstanceState, Status> instanceToNodeStatus, LoadingCache<RegionAndName, ? extends Image> instanceToImage) {
  165. Supplier<Set<? extends Location>> locationSupplier = new Supplier<Set<? extends Location>>() {
  166. @Override
  167. public Set<? extends Location> get() {
  168. return locations;
  169. }
  170. };
  171. Supplier<Set<? extends Hardware>> hardwareSupplier = new Supplier<Set<? extends Hardware>>() {
  172. @Override
  173. public Set<? extends Hardware> get() {
  174. return hardware;
  175. }
  176. };
  177. GroupNamingConvention.Factory namingConvention = Guice.createInjector(new AbstractModule() {
  178. @Override
  179. protected void configure() {
  180. Names.bindProperties(binder(), new AWSEC2ApiMetadata().getDefaultProperties());
  181. }
  182. }).getInstance(GroupNamingConvention.Factory.class);
  183. AWSRunningInstanceToNodeMetadata parser = new AWSRunningInstanceToNodeMetadata(instanceToNodeStatus,
  184. credentialStore, Suppliers.<LoadingCache<RegionAndName, ? extends Image>> ofInstance(instanceToImage),
  185. locationSupplier, hardwareSupplier, namingConvention);
  186. return parser;
  187. }
  188. }