PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/alasdairhodge/jclouds
Java | 201 lines | 155 code | 25 blank | 21 comment | 1 complexity | d46f372882497bb2a1edcdc5b55ca31c MD5 | raw file
  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.aws.ec2.compute.functions;
  20. import static org.testng.Assert.assertEquals;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import org.jclouds.aws.ec2.domain.AWSRunningInstance;
  24. import org.jclouds.aws.ec2.domain.MonitoringState;
  25. import org.jclouds.compute.domain.Hardware;
  26. import org.jclouds.compute.domain.Image;
  27. import org.jclouds.compute.domain.NodeMetadataBuilder;
  28. import org.jclouds.compute.domain.NodeState;
  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.InstanceState;
  40. import org.jclouds.ec2.domain.RootDeviceType;
  41. import org.testng.annotations.BeforeTest;
  42. import org.testng.annotations.Test;
  43. import com.google.common.base.Supplier;
  44. import com.google.common.base.Suppliers;
  45. import com.google.common.cache.LoadingCache;
  46. import com.google.common.cache.CacheBuilder;
  47. import com.google.common.cache.CacheLoader;
  48. import com.google.common.collect.ImmutableMap;
  49. import com.google.common.collect.ImmutableSet;
  50. import com.google.common.collect.Iterables;
  51. import com.google.inject.Guice;
  52. /**
  53. * @author Adrian Cole
  54. */
  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. .privateDnsName("ip-10-212-81-7.ec2.internal")
  76. .dnsName("ec2-174-129-173-155.compute-1.amazonaws.com")
  77. .keyName("jclouds#zkclustertest#us-east-1#23")
  78. .amiLaunchIndex("0")
  79. .instanceType("t1.micro")
  80. .launchTime(dateService.iso8601DateParse("2011-08-16T13:40:50.000Z"))
  81. .availabilityZone("us-east-1c")
  82. .kernelId("aki-427d952b")
  83. .monitoringState(MonitoringState.DISABLED)
  84. .privateIpAddress("10.212.81.7")
  85. .ipAddress("174.129.173.155")
  86. .securityGroupIdToNames(ImmutableMap.<String, String> of("sg-ef052b86", "jclouds#zkclustertest#us-east-1"))
  87. .rootDeviceType(RootDeviceType.EBS)
  88. .rootDeviceName("/dev/sda1")
  89. .device("/dev/sda1", new BlockDevice("vol-5829fc32", Attachment.Status.ATTACHED, dateService.iso8601DateParse("2011-08-16T13:41:19.000Z"), true))
  90. .virtualizationType("paravirtual")
  91. .tag("Name", "foo")
  92. .tag("Empty", "")
  93. .build(),//
  94. new AWSRunningInstance.Builder()
  95. .region(defaultRegion)
  96. .instanceId("i-931444f2")
  97. .imageId("ami-63be790a")
  98. .instanceState(InstanceState.RUNNING)
  99. .privateDnsName("ip-10-212-185-8.ec2.internal")
  100. .dnsName("ec2-50-19-207-248.compute-1.amazonaws.com")
  101. .keyName("jclouds#zkclustertest#us-east-1#23")
  102. .amiLaunchIndex("0")
  103. .instanceType("t1.micro")
  104. .launchTime(dateService.iso8601DateParse("2011-08-16T13:40:50.000Z"))
  105. .availabilityZone("us-east-1c")
  106. .kernelId("aki-427d952b")
  107. .monitoringState(MonitoringState.DISABLED)
  108. .privateIpAddress("10.212.185.8")
  109. .ipAddress("50.19.207.248")
  110. .securityGroupIdToNames(ImmutableMap.<String, String>of("sg-ef052b86", "jclouds#zkclustertest#us-east-1"))
  111. .rootDeviceType(RootDeviceType.EBS)
  112. .rootDeviceName("/dev/sda1")
  113. .device("/dev/sda1", new BlockDevice("vol-5029fc3a", Attachment.Status.ATTACHED, dateService.iso8601DateParse("2011-08-16T13:41:19.000Z"), true))
  114. .virtualizationType("paravirtual")
  115. .build());
  116. assertEquals(
  117. parser.apply(Iterables.get(contents, 0)).toString(),
  118. new NodeMetadataBuilder()
  119. .state(NodeState.RUNNING)
  120. .group("zkclustertest")
  121. .name("foo")
  122. .hostname("ip-10-212-81-7")
  123. .privateAddresses(ImmutableSet.of("10.212.81.7"))
  124. .publicAddresses(ImmutableSet.of("174.129.173.155"))
  125. .imageId("us-east-1/ami-63be790a")
  126. .id("us-east-1/i-911444f0")
  127. .providerId("i-911444f0")
  128. .tags(ImmutableSet.of("Empty"))
  129. .userMetadata(ImmutableMap.of("Name", "foo")).build().toString());
  130. assertEquals(
  131. parser.apply(Iterables.get(contents, 1)),
  132. new NodeMetadataBuilder()
  133. .state(NodeState.RUNNING)
  134. .group("zkclustertest")
  135. .hostname("ip-10-212-185-8")
  136. .privateAddresses(ImmutableSet.of("10.212.185.8"))
  137. .publicAddresses(ImmutableSet.of("50.19.207.248"))
  138. .imageId("us-east-1/ami-63be790a")
  139. .id("us-east-1/i-931444f2")
  140. .providerId("i-931444f2")
  141. .build());
  142. }
  143. protected AWSRunningInstanceToNodeMetadata createNodeParser(final ImmutableSet<Hardware> hardware,
  144. final ImmutableSet<Location> locations, Set<org.jclouds.compute.domain.Image> images,
  145. Map<String, Credentials> credentialStore) {
  146. Map<InstanceState, NodeState> instanceToNodeState = EC2ComputeServiceDependenciesModule.instanceToNodeState;
  147. final Map<RegionAndName, ? extends Image> backing = ImagesToRegionAndIdMap.imagesToMap(images);
  148. LoadingCache<RegionAndName, Image> instanceToImage = CacheBuilder.newBuilder().build(new CacheLoader<RegionAndName, Image> (){
  149. @Override
  150. public Image load(RegionAndName key) throws Exception {
  151. return backing.get(key);
  152. }
  153. });
  154. return createNodeParser(hardware, locations, credentialStore, instanceToNodeState, instanceToImage);
  155. }
  156. private AWSRunningInstanceToNodeMetadata createNodeParser(final ImmutableSet<Hardware> hardware,
  157. final ImmutableSet<Location> locations, Map<String, Credentials> credentialStore,
  158. Map<InstanceState, NodeState> instanceToNodeState, LoadingCache<RegionAndName, ? extends Image> instanceToImage) {
  159. Supplier<Set<? extends Location>> locationSupplier = new Supplier<Set<? extends Location>>() {
  160. @Override
  161. public Set<? extends Location> get() {
  162. return locations;
  163. }
  164. };
  165. Supplier<Set<? extends Hardware>> hardwareSupplier = new Supplier<Set<? extends Hardware>>() {
  166. @Override
  167. public Set<? extends Hardware> get() {
  168. return hardware;
  169. }
  170. };
  171. AWSRunningInstanceToNodeMetadata parser = new AWSRunningInstanceToNodeMetadata(instanceToNodeState,
  172. credentialStore, Suppliers.<LoadingCache<RegionAndName, ? extends Image>> ofInstance(instanceToImage),
  173. locationSupplier, hardwareSupplier);
  174. return parser;
  175. }
  176. }