PageRenderTime 915ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/compute/functions/ServerInRegionToNodeMetadataTest.java

http://github.com/jclouds/jclouds
Java | 366 lines | 269 code | 72 blank | 25 comment | 9 complexity | 0dc52ba348c81faa7f72b0af27cb155b MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jclouds.openstack.nova.v2_0.compute.functions;
  18. import static org.testng.Assert.assertEquals;
  19. import static org.testng.Assert.assertNotNull;
  20. import static org.testng.Assert.assertNull;
  21. import java.net.URI;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import org.jclouds.compute.domain.Hardware;
  25. import org.jclouds.compute.domain.HardwareBuilder;
  26. import org.jclouds.compute.domain.Image;
  27. import org.jclouds.compute.domain.ImageBuilder;
  28. import org.jclouds.compute.domain.NodeMetadata;
  29. import org.jclouds.compute.domain.OperatingSystem;
  30. import org.jclouds.compute.domain.OsFamily;
  31. import org.jclouds.compute.functions.GroupNamingConvention;
  32. import org.jclouds.date.internal.SimpleDateFormatDateService;
  33. import org.jclouds.domain.Location;
  34. import org.jclouds.domain.LocationBuilder;
  35. import org.jclouds.domain.LocationScope;
  36. import org.jclouds.openstack.nova.v2_0.compute.config.NovaComputeServiceContextModule;
  37. import org.jclouds.openstack.nova.v2_0.domain.Server;
  38. import org.jclouds.openstack.nova.v2_0.domain.regionscoped.ServerInRegion;
  39. import org.jclouds.openstack.nova.v2_0.parse.ParseServerTest;
  40. import org.jclouds.openstack.nova.v2_0.parse.ParseServerWithAddressExtensionsTest;
  41. import org.jclouds.openstack.nova.v2_0.parse.ParseServerWithoutImageTest;
  42. import org.jclouds.openstack.v2_0.domain.Link;
  43. import org.jclouds.openstack.v2_0.domain.Resource;
  44. import org.testng.annotations.Test;
  45. import com.google.common.base.Supplier;
  46. import com.google.common.base.Suppliers;
  47. import com.google.common.collect.ImmutableMap;
  48. import com.google.common.collect.ImmutableSet;
  49. import com.google.inject.Guice;
  50. /**
  51. * Tests for the function for transforming a nova specific Server into a generic
  52. * NodeMetadata object.
  53. */
  54. @Test(testName = "ServerInRegionToNodeMetadataTest")
  55. public class ServerInRegionToNodeMetadataTest {
  56. Location provider = new LocationBuilder().scope(LocationScope.PROVIDER).id("openstack-nova")
  57. .description("openstack-nova").build();
  58. Location region = new LocationBuilder().id("az-1.region-a.geo-1").description("az-1.region-a.geo-1")
  59. .scope(LocationScope.REGION).parent(provider).build();
  60. Supplier<Map<String, Location>> locationIndex = Suppliers.<Map<String, Location>> ofInstance(ImmutableMap
  61. .<String, Location> of("az-1.region-a.geo-1", region));
  62. GroupNamingConvention.Factory namingConvention = Guice.createInjector().getInstance(GroupNamingConvention.Factory.class);
  63. @Test
  64. public void testWhenNoHardwareOrImageMatchServerScopedIdsImageIdIsStillSet() {
  65. Hardware existingHardware = new HardwareBuilder().id("az-1.region-a.geo-1/FOOOOOOOO").providerId("FOOOOOOOO")
  66. .location(region).build();
  67. Image existingImage = new ImageBuilder().id("az-1.region-a.geo-1/FOOOOOOOO")
  68. .operatingSystem(OperatingSystem.builder().family(OsFamily.LINUX).description("foobuntu").build())
  69. .providerId("FOOOOOOOO").description("foobuntu").location(region).status(Image.Status.AVAILABLE).build();
  70. checkHardwareAndImageStatus(null, existingHardware, "az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f6f006e54",
  71. null, existingImage);
  72. }
  73. @Test
  74. public void testWhenNoHardwareAndImageMatchServerScopedIdsHardwareOperatingSystemAndImageIdAreSet() {
  75. Hardware existingHardware = new HardwareBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f216543fd")
  76. .providerId("52415800-8b69-11e0-9b19-734f216543fd").location(region).build();
  77. Image existingImage = new ImageBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f6f006e54")
  78. .operatingSystem(OperatingSystem.builder().family(OsFamily.LINUX).description("foobuntu").build())
  79. .providerId("52415800-8b69-11e0-9b19-734f6f006e54").description("foobuntu").status(Image.Status.AVAILABLE)
  80. .location(region).build();
  81. checkHardwareAndImageStatus(existingHardware, existingHardware, existingImage.getId(),
  82. existingImage.getOperatingSystem(), existingImage);
  83. }
  84. @Test
  85. public void testNullAccessIPs() {
  86. Hardware existingHardware = new HardwareBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f216543fd")
  87. .providerId("52415800-8b69-11e0-9b19-734f216543fd").location(region).build();
  88. Image existingImage = new ImageBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f6f006e54")
  89. .operatingSystem(OperatingSystem.builder().family(OsFamily.LINUX).description("foobuntu").build())
  90. .providerId("52415800-8b69-11e0-9b19-734f6f006e54").description("foobuntu").status(Image.Status.AVAILABLE)
  91. .location(region).build();
  92. Set<Image> images = existingImage == null ? ImmutableSet.<Image> of() : ImmutableSet.of(existingImage);
  93. Set<Hardware> hardwares = existingHardware == null ? ImmutableSet.<Hardware> of() : ImmutableSet
  94. .of(existingHardware);
  95. Server serverToConvert = new ParseServerTest().expected().toBuilder()
  96. .accessIPv4(null)
  97. .accessIPv6(null)
  98. .build();
  99. ServerInRegion serverInRegionToConvert = new ServerInRegion(serverToConvert, "az-1.region-a.geo-1");
  100. ServerInRegionToNodeMetadata converter = new ServerInRegionToNodeMetadata(
  101. NovaComputeServiceContextModule.toPortableNodeStatus, locationIndex, Suppliers
  102. .<Set<? extends Image>> ofInstance(images), Suppliers
  103. .<Set<? extends Hardware>> ofInstance(hardwares), namingConvention);
  104. NodeMetadata convertedNodeMetadata = converter.apply(serverInRegionToConvert);
  105. assertNotNull(convertedNodeMetadata.getPrivateAddresses());
  106. assertEquals(convertedNodeMetadata.getPrivateAddresses(), ImmutableSet.of("10.176.42.16"));
  107. assertNotNull(convertedNodeMetadata.getPublicAddresses());
  108. // note jclouds doesn't yet support ipv6 b/c not tested yet
  109. assertEquals(convertedNodeMetadata.getPublicAddresses(), ImmutableSet.of("67.23.10.132", "67.23.10.131"));
  110. }
  111. @Test
  112. public void testDuplicateAccessIPs() {
  113. Hardware existingHardware = new HardwareBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f216543fd")
  114. .providerId("52415800-8b69-11e0-9b19-734f216543fd").location(region).build();
  115. Image existingImage = new ImageBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f6f006e54")
  116. .operatingSystem(OperatingSystem.builder().family(OsFamily.LINUX).description("foobuntu").build())
  117. .providerId("52415800-8b69-11e0-9b19-734f6f006e54").description("foobuntu").status(Image.Status.AVAILABLE)
  118. .location(region).build();
  119. Set<Image> images = existingImage == null ? ImmutableSet.<Image> of() : ImmutableSet.of(existingImage);
  120. Set<Hardware> hardwares = existingHardware == null ? ImmutableSet.<Hardware> of() : ImmutableSet
  121. .of(existingHardware);
  122. Server serverToConvert = new ParseServerTest().expected().toBuilder()
  123. .accessIPv4("67.23.10.132")
  124. .accessIPv6("::babe:67.23.10.132")
  125. .build();
  126. ServerInRegion serverInRegionToConvert = new ServerInRegion(serverToConvert, "az-1.region-a.geo-1");
  127. ServerInRegionToNodeMetadata converter = new ServerInRegionToNodeMetadata(
  128. NovaComputeServiceContextModule.toPortableNodeStatus, locationIndex, Suppliers
  129. .<Set<? extends Image>> ofInstance(images), Suppliers
  130. .<Set<? extends Hardware>> ofInstance(hardwares), namingConvention);
  131. NodeMetadata convertedNodeMetadata = converter.apply(serverInRegionToConvert);
  132. assertNotNull(convertedNodeMetadata.getPrivateAddresses());
  133. assertEquals(convertedNodeMetadata.getPrivateAddresses(), ImmutableSet.of("10.176.42.16"));
  134. assertNotNull(convertedNodeMetadata.getPublicAddresses());
  135. // note jclouds doesn't yet support ipv6 b/c not tested yet
  136. assertEquals(convertedNodeMetadata.getPublicAddresses(), ImmutableSet.of("67.23.10.132", "67.23.10.131"));
  137. }
  138. @Test
  139. public void testAlternateAccessIPs() {
  140. Hardware existingHardware = new HardwareBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f216543fd")
  141. .providerId("52415800-8b69-11e0-9b19-734f216543fd").location(region).build();
  142. Image existingImage = new ImageBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f6f006e54")
  143. .operatingSystem(OperatingSystem.builder().family(OsFamily.LINUX).description("foobuntu").build())
  144. .providerId("52415800-8b69-11e0-9b19-734f6f006e54").description("foobuntu").status(Image.Status.AVAILABLE)
  145. .location(region).build();
  146. Set<Image> images = existingImage == null ? ImmutableSet.<Image> of() : ImmutableSet.of(existingImage);
  147. Set<Hardware> hardwares = existingHardware == null ? ImmutableSet.<Hardware> of() : ImmutableSet
  148. .of(existingHardware);
  149. Server serverToConvert = new ParseServerTest().expected().toBuilder()
  150. .accessIPv4("76.32.1.231")
  151. .accessIPv6("::babe:76.32.1.231")
  152. .build();
  153. ServerInRegion serverInRegionToConvert = new ServerInRegion(serverToConvert, "az-1.region-a.geo-1");
  154. ServerInRegionToNodeMetadata converter = new ServerInRegionToNodeMetadata(
  155. NovaComputeServiceContextModule.toPortableNodeStatus, locationIndex, Suppliers
  156. .<Set<? extends Image>> ofInstance(images), Suppliers
  157. .<Set<? extends Hardware>> ofInstance(hardwares), namingConvention);
  158. NodeMetadata convertedNodeMetadata = converter.apply(serverInRegionToConvert);
  159. assertNotNull(convertedNodeMetadata.getPrivateAddresses());
  160. assertEquals(convertedNodeMetadata.getPrivateAddresses(), ImmutableSet.of("10.176.42.16"));
  161. assertNotNull(convertedNodeMetadata.getPublicAddresses());
  162. // note jclouds doesn't yet support ipv6 b/c not tested yet
  163. assertEquals(convertedNodeMetadata.getPublicAddresses(), ImmutableSet.of("67.23.10.132", "67.23.10.131", "76.32.1.231"));
  164. }
  165. @Test
  166. public void testPortableNodeStatusNotNull() {
  167. for (Server.Status serverStatus : Server.Status.values()) {
  168. assertNotNull(NovaComputeServiceContextModule.toPortableNodeStatus.get(serverStatus));
  169. }
  170. }
  171. @Test
  172. public void testServerWithoutImage() {
  173. Hardware existingHardware = new HardwareBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f216543fd")
  174. .providerId("52415800-8b69-11e0-9b19-734f216543fd").location(region).build();
  175. Image existingImage = new ImageBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f6f006e54")
  176. .operatingSystem(OperatingSystem.builder().family(OsFamily.LINUX).description("foobuntu").build())
  177. .providerId("52415800-8b69-11e0-9b19-734f6f006e54").description("foobuntu").status(Image.Status.AVAILABLE)
  178. .location(region).build();
  179. Server serverToConvert = new ParseServerWithoutImageTest().expected();
  180. ServerInRegion serverInRegionToConvert = new ServerInRegion(serverToConvert, "az-1.region-a.geo-1");
  181. ServerInRegionToNodeMetadata converter = new ServerInRegionToNodeMetadata(
  182. NovaComputeServiceContextModule.toPortableNodeStatus, locationIndex,
  183. Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.of(existingImage)),
  184. Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet.of(existingHardware)),
  185. namingConvention);
  186. NodeMetadata convertedNodeMetadata = converter.apply(serverInRegionToConvert);
  187. assertNull(convertedNodeMetadata.getImageId());
  188. }
  189. @Test
  190. public void testFloatingIp() {
  191. Hardware existingHardware = new HardwareBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f216543fd")
  192. .providerId("52415800-8b69-11e0-9b19-734f216543fd").location(region).build();
  193. Image existingImage = new ImageBuilder().id("az-1.region-a.geo-1/52415800-8b69-11e0-9b19-734f6f006e54")
  194. .operatingSystem(OperatingSystem.builder().family(OsFamily.LINUX).description("foobuntu").build())
  195. .providerId("52415800-8b69-11e0-9b19-734f6f006e54").description("foobuntu").status(Image.Status.AVAILABLE)
  196. .location(region).build();
  197. Server serverToConvert = new ParseServerWithAddressExtensionsTest().expected();
  198. ServerInRegion serverInRegionToConvert = new ServerInRegion(serverToConvert, "az-1.region-a.geo-1");
  199. ServerInRegionToNodeMetadata converter = new ServerInRegionToNodeMetadata(
  200. NovaComputeServiceContextModule.toPortableNodeStatus, locationIndex,
  201. Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.of(existingImage)),
  202. Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet.of(existingHardware)),
  203. namingConvention);
  204. NodeMetadata convertedNodeMetadata = converter.apply(serverInRegionToConvert);
  205. assertEquals(convertedNodeMetadata.getPrivateAddresses(), ImmutableSet.of("172.16.130.24"));
  206. assertEquals(convertedNodeMetadata.getPublicAddresses(), ImmutableSet.of("10.8.54.75"));
  207. }
  208. // TODO: clean up this syntax
  209. private void checkHardwareAndImageStatus(Hardware expectedHardware, Hardware existingHardware,
  210. String expectedImageId, OperatingSystem expectedOs, Image existingImage) {
  211. Set<Image> images = existingImage == null ? ImmutableSet.<Image> of() : ImmutableSet.of(existingImage);
  212. Set<Hardware> hardwares = existingHardware == null ? ImmutableSet.<Hardware> of() : ImmutableSet
  213. .of(existingHardware);
  214. Server serverToConvert = new ParseServerTest().expected();
  215. ServerInRegion serverInRegionToConvert = new ServerInRegion(serverToConvert, "az-1.region-a.geo-1");
  216. ServerInRegionToNodeMetadata converter = new ServerInRegionToNodeMetadata(
  217. NovaComputeServiceContextModule.toPortableNodeStatus, locationIndex, Suppliers
  218. .<Set<? extends Image>> ofInstance(images), Suppliers
  219. .<Set<? extends Hardware>> ofInstance(hardwares), namingConvention);
  220. NodeMetadata convertedNodeMetadata = converter.apply(serverInRegionToConvert);
  221. assertEquals(serverInRegionToConvert.slashEncode(), convertedNodeMetadata.getId());
  222. assertEquals(serverToConvert.getId(), convertedNodeMetadata.getProviderId());
  223. assertEquals(convertedNodeMetadata.getLocation().getScope(), LocationScope.HOST);
  224. assertEquals(convertedNodeMetadata.getLocation().getId(), "e4d909c290d0fb1ca068ffaddf22cbd0");
  225. assertEquals(convertedNodeMetadata.getLocation().getParent(), locationIndex.get().get("az-1.region-a.geo-1"));
  226. assertEquals(serverToConvert.getName(), convertedNodeMetadata.getName());
  227. assertEquals(convertedNodeMetadata.getGroup(), "sample");
  228. assertEquals(convertedNodeMetadata.getImageId(), expectedImageId);
  229. assertEquals(convertedNodeMetadata.getOperatingSystem(), expectedOs);
  230. assertEquals(convertedNodeMetadata.getHardware(), expectedHardware);
  231. assertEquals(NovaComputeServiceContextModule.toPortableNodeStatus.get(serverToConvert.getStatus()),
  232. convertedNodeMetadata.getStatus());
  233. assertNotNull(convertedNodeMetadata.getPrivateAddresses());
  234. assertEquals(convertedNodeMetadata.getPrivateAddresses(), ImmutableSet.of("10.176.42.16"));
  235. assertNotNull(convertedNodeMetadata.getPublicAddresses());
  236. // note jclouds doesn't yet support ipv6 b/c not tested yet
  237. assertEquals(convertedNodeMetadata.getPublicAddresses(), ImmutableSet.of("67.23.10.132", "67.23.10.131"));
  238. assertNotNull(convertedNodeMetadata.getUserMetadata());
  239. assertEquals(convertedNodeMetadata.getUserMetadata(),
  240. ImmutableMap.<String, String> of("Server Label", "Web Head 1", "Image Version", "2.1"));
  241. URI expectedURI = URI.create("http://servers.api.openstack.org/v2/1234/servers/71752");
  242. assertEquals(convertedNodeMetadata.getUri(), expectedURI);
  243. }
  244. @Test
  245. public void testNewServerWithoutHostIdSetsRegionAsLocation() {
  246. Set<Image> images = ImmutableSet.<Image> of();
  247. Set<Hardware> hardwares = ImmutableSet.<Hardware> of();
  248. Server serverToConvert = expectedServer();
  249. ServerInRegion serverInRegionToConvert = new ServerInRegion(serverToConvert, "az-1.region-a.geo-1");
  250. ServerInRegionToNodeMetadata converter = new ServerInRegionToNodeMetadata(
  251. NovaComputeServiceContextModule.toPortableNodeStatus, locationIndex, Suppliers
  252. .<Set<? extends Image>> ofInstance(images), Suppliers
  253. .<Set<? extends Hardware>> ofInstance(hardwares), namingConvention);
  254. NodeMetadata convertedNodeMetadata = converter.apply(serverInRegionToConvert);
  255. assertEquals(serverInRegionToConvert.slashEncode(), convertedNodeMetadata.getId());
  256. assertEquals(serverToConvert.getId(), convertedNodeMetadata.getProviderId());
  257. assertEquals(convertedNodeMetadata.getLocation(), region);
  258. URI expectedURI = URI.create("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v2/37936628937291/servers/71752");
  259. assertEquals(convertedNodeMetadata.getUri(), expectedURI);
  260. }
  261. public Server expectedServer() {
  262. return Server
  263. .builder()
  264. .id("71752")
  265. .uuid("47491020-6a78-4f63-9475-23195ac4515c")
  266. .tenantId("37936628937291")
  267. .userId("54297837463082")
  268. .name("test-e92")
  269. .updated(new SimpleDateFormatDateService().iso8601SecondsDateParse("2012-03-19T06:21:13Z"))
  270. .created(new SimpleDateFormatDateService().iso8601SecondsDateParse("2012-03-19T06:21:13Z"))
  271. .status(Server.Status.BUILD)
  272. .image(
  273. Resource
  274. .builder()
  275. .id("1241")
  276. .links(
  277. Link.create(
  278. Link.Relation.BOOKMARK,
  279. URI.create("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/37936628937291/images/1241")))
  280. .build())
  281. .flavor(
  282. Resource
  283. .builder()
  284. .id("100")
  285. .links(
  286. Link.create(
  287. Link.Relation.BOOKMARK,
  288. URI.create("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/37936628937291/flavors/100")))
  289. .build())
  290. .links(
  291. Link.create(Link.Relation.SELF, URI.create("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/v2/37936628937291/servers/71752")),
  292. Link.create(Link.Relation.BOOKMARK, URI.create("https://az-1.region-a.geo-1.compute.hpcloudsvc.com/37936628937291/servers/71752"))).build();
  293. }
  294. }