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