PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/vkris/jclouds
Java | 172 lines | 124 code | 27 blank | 21 comment | 1 complexity | 932e26811c6658a2851d219d94be5fde 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.domain.Attachment;
  37. import org.jclouds.ec2.domain.BlockDevice;
  38. import org.jclouds.ec2.domain.InstanceState;
  39. import org.jclouds.ec2.domain.RootDeviceType;
  40. import org.testng.annotations.BeforeTest;
  41. import org.testng.annotations.Test;
  42. import com.google.common.base.Function;
  43. import com.google.common.base.Supplier;
  44. import com.google.common.base.Suppliers;
  45. import com.google.common.cache.Cache;
  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.common.collect.Maps;
  52. import com.google.inject.Guice;
  53. /**
  54. * @author Adrian Cole
  55. */
  56. @Test(groups = "unit")
  57. public class AWSRunningInstanceToNodeMetadataTest {
  58. private static final String defaultRegion = "us-east-1";
  59. static Location provider = new LocationBuilder().scope(LocationScope.REGION).id(defaultRegion).description(
  60. defaultRegion).build();
  61. private DateService dateService;
  62. @BeforeTest
  63. protected void setUpInjector() {
  64. dateService = Guice.createInjector().getInstance(DateService.class);
  65. assert dateService != null;
  66. }
  67. @Test
  68. public void test2Nodes() {
  69. AWSRunningInstanceToNodeMetadata parser = createNodeParser(ImmutableSet.<Hardware> of(), ImmutableSet
  70. .<Location> of(), ImmutableSet.<Image> of(), ImmutableMap.<String, Credentials> of());
  71. ImmutableSet<AWSRunningInstance> contents = ImmutableSet.of(new AWSRunningInstance.Builder()
  72. .region(defaultRegion).instanceId("i-911444f0").imageId("ami-63be790a").instanceState(
  73. InstanceState.RUNNING).privateDnsName("ip-10-212-81-7.ec2.internal").dnsName(
  74. "ec2-174-129-173-155.compute-1.amazonaws.com").keyName("jclouds#zkclustertest#us-east-1#23")
  75. .amiLaunchIndex("0").instanceType("t1.micro").launchTime(
  76. dateService.iso8601DateParse("2011-08-16T13:40:50.000Z")).availabilityZone("us-east-1c")
  77. .kernelId("aki-427d952b").monitoringState(MonitoringState.DISABLED).privateIpAddress("10.212.81.7")
  78. .ipAddress("174.129.173.155").securityGroupIdToNames(
  79. ImmutableMap.<String, String> of("sg-ef052b86", "jclouds#zkclustertest#us-east-1"))
  80. .rootDeviceType(RootDeviceType.EBS).rootDeviceName("/dev/sda1").device(
  81. "/dev/sda1",
  82. new BlockDevice("vol-5829fc32", Attachment.Status.ATTACHED, dateService
  83. .iso8601DateParse("2011-08-16T13:41:19.000Z"), true))
  84. .virtualizationType("paravirtual").build(),//
  85. new AWSRunningInstance.Builder().region(defaultRegion).instanceId("i-931444f2").imageId("ami-63be790a")
  86. .instanceState(InstanceState.RUNNING).privateDnsName("ip-10-212-185-8.ec2.internal").dnsName(
  87. "ec2-50-19-207-248.compute-1.amazonaws.com").keyName(
  88. "jclouds#zkclustertest#us-east-1#23").amiLaunchIndex("0").instanceType("t1.micro")
  89. .launchTime(dateService.iso8601DateParse("2011-08-16T13:40:50.000Z")).availabilityZone(
  90. "us-east-1c").kernelId("aki-427d952b").monitoringState(MonitoringState.DISABLED)
  91. .privateIpAddress("10.212.185.8").ipAddress("50.19.207.248").securityGroupIdToNames(
  92. ImmutableMap.<String, String> of("sg-ef052b86", "jclouds#zkclustertest#us-east-1"))
  93. .rootDeviceType(RootDeviceType.EBS).rootDeviceName("/dev/sda1").device(
  94. "/dev/sda1",
  95. new BlockDevice("vol-5029fc3a", Attachment.Status.ATTACHED, dateService
  96. .iso8601DateParse("2011-08-16T13:41:19.000Z"), true)).virtualizationType(
  97. "paravirtual").build());
  98. assertEquals(parser.apply(Iterables.get(contents, 0)), new NodeMetadataBuilder().state(NodeState.RUNNING).group(
  99. "zkclustertest").hostname("ip-10-212-81-7").privateAddresses(ImmutableSet.of("10.212.81.7"))
  100. .publicAddresses(ImmutableSet.of("174.129.173.155")).imageId("us-east-1/ami-63be790a").id(
  101. "us-east-1/i-911444f0").providerId("i-911444f0").build());
  102. assertEquals(parser.apply(Iterables.get(contents, 1)), new NodeMetadataBuilder().state(NodeState.RUNNING).group(
  103. "zkclustertest").hostname("ip-10-212-185-8").privateAddresses(ImmutableSet.of("10.212.185.8"))
  104. .publicAddresses(ImmutableSet.of("50.19.207.248")).imageId("us-east-1/ami-63be790a").id(
  105. "us-east-1/i-931444f2").providerId("i-931444f2").build());
  106. }
  107. protected AWSRunningInstanceToNodeMetadata createNodeParser(final ImmutableSet<Hardware> hardware,
  108. final ImmutableSet<Location> locations, Set<org.jclouds.compute.domain.Image> images,
  109. Map<String, Credentials> credentialStore) {
  110. Map<InstanceState, NodeState> instanceToNodeState = EC2ComputeServiceDependenciesModule.instanceToNodeState;
  111. final ImmutableMap<RegionAndName, Image> backing = Maps.uniqueIndex(images, new Function<Image, RegionAndName>() {
  112. @Override
  113. public RegionAndName apply(Image from) {
  114. return new RegionAndName(from.getLocation().getId(), from.getProviderId());
  115. }
  116. });
  117. Cache<RegionAndName, Image> instanceToImage = CacheBuilder.newBuilder().build(new CacheLoader<RegionAndName, Image> (){
  118. @Override
  119. public Image load(RegionAndName key) throws Exception {
  120. return backing.get(key);
  121. }
  122. });
  123. return createNodeParser(hardware, locations, credentialStore, instanceToNodeState, instanceToImage);
  124. }
  125. private AWSRunningInstanceToNodeMetadata createNodeParser(final ImmutableSet<Hardware> hardware,
  126. final ImmutableSet<Location> locations, Map<String, Credentials> credentialStore,
  127. Map<InstanceState, NodeState> instanceToNodeState, Cache<RegionAndName, ? extends Image> instanceToImage) {
  128. Supplier<Set<? extends Location>> locationSupplier = new Supplier<Set<? extends Location>>() {
  129. @Override
  130. public Set<? extends Location> get() {
  131. return locations;
  132. }
  133. };
  134. Supplier<Set<? extends Hardware>> hardwareSupplier = new Supplier<Set<? extends Hardware>>() {
  135. @Override
  136. public Set<? extends Hardware> get() {
  137. return hardware;
  138. }
  139. };
  140. AWSRunningInstanceToNodeMetadata parser = new AWSRunningInstanceToNodeMetadata(instanceToNodeState,
  141. credentialStore, Suppliers.<Cache<RegionAndName, ? extends Image>> ofInstance(instanceToImage),
  142. locationSupplier, hardwareSupplier);
  143. return parser;
  144. }
  145. }