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