PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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