PageRenderTime 51ms CodeModel.GetById 16ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/providers/softlayer/src/test/java/org/jclouds/softlayer/compute/functions/VirtualGuestToNodeMetadataTest.java

https://github.com/richardcloudsoft/legacy-jclouds
Java | 218 lines | 143 code | 44 blank | 31 comment | 0 complexity | 8ed4f99cb6550cfb3ae657652ba23c19 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.softlayer.compute.functions;
 20
 21import static org.easymock.EasyMock.createNiceMock;
 22import static org.testng.Assert.assertEquals;
 23
 24import java.util.Set;
 25
 26import org.jclouds.compute.domain.Hardware;
 27import org.jclouds.compute.domain.HardwareBuilder;
 28import org.jclouds.compute.domain.Image;
 29import org.jclouds.compute.domain.ImageBuilder;
 30import org.jclouds.compute.domain.NodeMetadata;
 31import org.jclouds.compute.domain.NodeMetadata.Status;
 32import org.jclouds.compute.domain.NodeMetadataBuilder;
 33import org.jclouds.compute.domain.OperatingSystem;
 34import org.jclouds.compute.functions.GroupNamingConvention;
 35import org.jclouds.domain.Location;
 36import org.jclouds.softlayer.SoftLayerClient;
 37import org.jclouds.softlayer.domain.VirtualGuest;
 38import org.jclouds.softlayer.parse.ParseBadVirtualGuest;
 39import org.jclouds.softlayer.parse.ParseVirtualGuestHaltedTest;
 40import org.jclouds.softlayer.parse.ParseVirtualGuestPausedTest;
 41import org.jclouds.softlayer.parse.ParseVirtualGuestRunningTest;
 42import org.jclouds.softlayer.parse.ParseVirtualGuestWithNoPasswordTest;
 43import org.testng.annotations.Test;
 44
 45import com.google.common.base.Function;
 46import com.google.common.base.Supplier;
 47import com.google.common.base.Suppliers;
 48import com.google.common.collect.ImmutableSet;
 49import com.google.inject.Guice;
 50
 51/**
 52 * @author Adrian Cole
 53 */
 54@Test(groups = "unit", testName = "VirtualGuestToNodeMetadataTest")
 55public class VirtualGuestToNodeMetadataTest {
 56   GroupNamingConvention.Factory namingConvention = Guice.createInjector().getInstance(GroupNamingConvention.Factory.class);
 57
 58   @Test
 59   public void testApplyWhereVirtualGuestWithNoPassword() {
 60
 61      // notice if we've already parsed this properly here, we can rely on it.
 62      VirtualGuest guest = new ParseVirtualGuestWithNoPasswordTest().expected();
 63
 64      // setup so that we have an expected Location to be parsed from the guest.
 65      Location expectedLocation = DatacenterToLocationTest.function.apply(guest.getDatacenter());
 66      Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
 67            .<Location> of(expectedLocation));
 68
 69      VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(
 70            locationSupplier, new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention);
 71
 72      NodeMetadata node = parser.apply(guest);
 73
 74      assertEquals(
 75            node,
 76            new NodeMetadataBuilder().ids("416788").name("node1000360500").hostname("node1000360500")
 77                  .location(expectedLocation).status(Status.PENDING)
 78                  .publicAddresses(ImmutableSet.of("173.192.29.186"))
 79                  .privateAddresses(ImmutableSet.of("10.37.102.194"))
 80                  .hardware(new GetHardwareForVirtualGuestMock().getHardware(guest))
 81                  .imageId(new GetImageForVirtualGuestMock().getImage(guest).getId())
 82                  .operatingSystem(new GetImageForVirtualGuestMock().getImage(guest).getOperatingSystem()).build());
 83
 84   }
 85
 86   @Test
 87   public void testApplyWhereVirtualIsBad() {
 88
 89      // notice if we've already parsed this properly here, we can rely on it.
 90      VirtualGuest guest = new ParseBadVirtualGuest().expected();
 91
 92      // no location here
 93      Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
 94            .<Location> of());
 95
 96      VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier,
 97            new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention);
 98
 99      NodeMetadata node = parser.apply(guest);
100
101      assertEquals(
102            node,
103            new NodeMetadataBuilder().ids("413348").name("foo-ef4").hostname("foo-ef4").group("foo")
104                  .status(Status.PENDING).hardware(new GetHardwareForVirtualGuestMock().getHardware(guest))
105                  .imageId(new GetImageForVirtualGuestMock().getImage(guest).getId())
106                  .operatingSystem(new GetImageForVirtualGuestMock().getImage(guest).getOperatingSystem()).build());
107
108   }
109
110   @Test
111   public void testApplyWhereVirtualGuestIsHalted() {
112
113      // notice if we've already parsed this properly here, we can rely on it.
114      VirtualGuest guest = new ParseVirtualGuestHaltedTest().expected();
115
116      // setup so that we have an expected Location to be parsed from the guest.
117      Location expectedLocation = DatacenterToLocationTest.function.apply(guest.getDatacenter());
118      Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
119            .<Location> of(expectedLocation));
120
121      VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier,
122            new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention);
123
124      NodeMetadata node = parser.apply(guest);
125
126      assertEquals(
127            node,
128            new NodeMetadataBuilder().ids("416700").name("node1703810489").hostname("node1703810489")
129                  .location(expectedLocation).status(Status.PENDING)
130                  .publicAddresses(ImmutableSet.of("173.192.29.187"))
131                  .privateAddresses(ImmutableSet.of("10.37.102.195"))
132                  .hardware(new GetHardwareForVirtualGuestMock().getHardware(guest))
133                  .imageId(new GetImageForVirtualGuestMock().getImage(guest).getId())
134                  .operatingSystem(new GetImageForVirtualGuestMock().getImage(guest).getOperatingSystem()).build());
135
136   }
137
138   @Test
139   public void testApplyWhereVirtualGuestIsPaused() {
140
141      // notice if we've already parsed this properly here, we can rely on it.
142      VirtualGuest guest = new ParseVirtualGuestPausedTest().expected();
143
144      // setup so that we have an expected Location to be parsed from the guest.
145      Location expectedLocation = DatacenterToLocationTest.function.apply(guest.getDatacenter());
146      Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
147            .<Location> of(expectedLocation));
148
149      VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier,
150            new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention);
151
152      NodeMetadata node = parser.apply(guest);
153
154      assertEquals(
155            node,
156            new NodeMetadataBuilder().ids("416700").name("node1703810489").hostname("node1703810489")
157                  .location(expectedLocation).status(Status.SUSPENDED)
158                  .publicAddresses(ImmutableSet.of("173.192.29.187"))
159                  .privateAddresses(ImmutableSet.of("10.37.102.195"))
160                  .hardware(new GetHardwareForVirtualGuestMock().getHardware(guest))
161                  .imageId(new GetImageForVirtualGuestMock().getImage(guest).getId())
162                  .operatingSystem(new GetImageForVirtualGuestMock().getImage(guest).getOperatingSystem()).build());
163
164   }
165
166   @Test
167   public void testApplyWhereVirtualGuestIsRunning() {
168
169      // notice if we've already parsed this properly here, we can rely on it.
170      VirtualGuest guest = new ParseVirtualGuestRunningTest().expected();
171
172      // setup so that we have an expected Location to be parsed from the guest.
173      Location expectedLocation = DatacenterToLocationTest.function.apply(guest.getDatacenter());
174      Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
175            .<Location> of(expectedLocation));
176
177      VirtualGuestToNodeMetadata parser = new VirtualGuestToNodeMetadata(locationSupplier,
178            new GetHardwareForVirtualGuestMock(), new GetImageForVirtualGuestMock(), namingConvention);
179
180      NodeMetadata node = parser.apply(guest);
181
182      assertEquals(
183            node,
184            new NodeMetadataBuilder().ids("416700").name("node1703810489").hostname("node1703810489")
185                  .location(expectedLocation).status(Status.RUNNING)
186                  .publicAddresses(ImmutableSet.of("173.192.29.187"))
187                  .privateAddresses(ImmutableSet.of("10.37.102.195"))
188                  .hardware(new GetHardwareForVirtualGuestMock().getHardware(guest))
189                  .imageId(new GetImageForVirtualGuestMock().getImage(guest).getId())
190                  .operatingSystem(new GetImageForVirtualGuestMock().getImage(guest).getOperatingSystem()).build());
191
192   }
193
194   private static class GetHardwareForVirtualGuestMock extends VirtualGuestToNodeMetadata.GetHardwareForVirtualGuest {
195      @SuppressWarnings("unchecked")
196      public GetHardwareForVirtualGuestMock() {
197         super(createNiceMock(SoftLayerClient.class), createNiceMock(Function.class));
198      }
199
200      @Override
201      public Hardware getHardware(VirtualGuest guest) {
202         return new HardwareBuilder().ids("mocked hardware").build();
203      }
204   }
205
206   private static class GetImageForVirtualGuestMock extends VirtualGuestToNodeMetadata.GetImageForVirtualGuest {
207      public GetImageForVirtualGuestMock() {
208         super(null);
209      }
210
211      @Override
212      public Image getImage(VirtualGuest guest) {
213         return new ImageBuilder().ids("123").description("mocked image")
214               .operatingSystem(OperatingSystem.builder().description("foo os").build())
215               .status(Image.Status.AVAILABLE).build();
216      }
217   }
218}