/apis/cloudservers/src/test/java/org/jclouds/cloudservers/compute/functions/ServerToNodeMetadataTest.java

https://github.com/danikov/jclouds · Java · 168 lines · 128 code · 19 blank · 21 comment · 0 complexity · 63cfd4c7732bcc8cf21305d9dcf7d0e1 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.cloudservers.compute.functions;
  20. import static org.testng.Assert.assertEquals;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import org.jclouds.cloudservers.compute.config.CloudServersComputeServiceContextModule;
  24. import org.jclouds.cloudservers.domain.Server;
  25. import org.jclouds.cloudservers.domain.ServerStatus;
  26. import org.jclouds.cloudservers.functions.ParseServerFromJsonResponseTest;
  27. import org.jclouds.compute.domain.Hardware;
  28. import org.jclouds.compute.domain.HardwareBuilder;
  29. import org.jclouds.compute.domain.Image;
  30. import org.jclouds.compute.domain.NodeMetadata;
  31. import org.jclouds.compute.domain.NodeMetadataBuilder;
  32. import org.jclouds.compute.domain.NodeState;
  33. import org.jclouds.compute.domain.OperatingSystem;
  34. import org.jclouds.compute.domain.OsFamily;
  35. import org.jclouds.compute.domain.Processor;
  36. import org.jclouds.compute.domain.Volume;
  37. import org.jclouds.compute.domain.VolumeBuilder;
  38. import org.jclouds.compute.functions.GroupNamingConvention;
  39. import org.jclouds.domain.Location;
  40. import org.jclouds.domain.LocationBuilder;
  41. import org.jclouds.domain.LocationScope;
  42. import org.testng.annotations.Test;
  43. import com.google.common.base.Suppliers;
  44. import com.google.common.collect.ImmutableList;
  45. import com.google.common.collect.ImmutableMap;
  46. import com.google.common.collect.ImmutableSet;
  47. import com.google.inject.Guice;
  48. /**
  49. * @author Adrian Cole
  50. */
  51. @Test(groups = "unit", testName = "ServerToNodeMetadataTest")
  52. public class ServerToNodeMetadataTest {
  53. Location provider = new LocationBuilder().scope(LocationScope.ZONE).id("dallas").description("description").build();
  54. GroupNamingConvention.Factory namingConvention = Guice.createInjector().getInstance(GroupNamingConvention.Factory.class);
  55. @Test
  56. public void testApplyWhereImageAndHardwareNotFound() {
  57. Map<ServerStatus, NodeState> serverStateToNodeState = CloudServersComputeServiceContextModule.serverToNodeState;
  58. Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of();
  59. Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of();
  60. Server server = ParseServerFromJsonResponseTest.parseServer();
  61. ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
  62. .ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares), namingConvention);
  63. NodeMetadata metadata = parser.apply(server);
  64. assertEquals(
  65. metadata,
  66. new NodeMetadataBuilder()
  67. .state(NodeState.PENDING)
  68. .publicAddresses(ImmutableSet.of("67.23.10.132", "67.23.10.131"))
  69. .privateAddresses(ImmutableSet.of("10.176.42.16"))
  70. .imageId("2")
  71. .id("1234")
  72. .providerId("1234")
  73. .name("sample-server")
  74. .group("sample")
  75. .hostname("sample-server")
  76. .location(
  77. new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0")
  78. .description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build())
  79. .userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
  80. }
  81. @Test
  82. public void testApplyWhereImageFoundAndHardwareNotFound() {
  83. Map<ServerStatus, NodeState> serverStateToNodeState = CloudServersComputeServiceContextModule.serverToNodeState;
  84. org.jclouds.compute.domain.Image jcImage = CloudServersImageToImageTest.convertImage();
  85. Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of(jcImage);
  86. Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of();
  87. Server server = ParseServerFromJsonResponseTest.parseServer();
  88. ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
  89. .ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares), namingConvention);
  90. NodeMetadata metadata = parser.apply(server);
  91. assertEquals(
  92. metadata,
  93. new NodeMetadataBuilder()
  94. .state(NodeState.PENDING)
  95. .publicAddresses(ImmutableSet.of("67.23.10.132", "67.23.10.131"))
  96. .privateAddresses(ImmutableSet.of("10.176.42.16"))
  97. .imageId("2")
  98. .operatingSystem(
  99. new OperatingSystem.Builder().family(OsFamily.CENTOS).description("CentOS 5.2").version("5.2")
  100. .is64Bit(true).build())
  101. .id("1234")
  102. .providerId("1234")
  103. .name("sample-server")
  104. .hostname("sample-server")
  105. .group("sample")
  106. .location(
  107. new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0")
  108. .description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build())
  109. .userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
  110. }
  111. @Test
  112. public void testApplyWhereImageAndHardwareFound() {
  113. Map<ServerStatus, NodeState> serverStateToNodeState = CloudServersComputeServiceContextModule.serverToNodeState;
  114. Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of(CloudServersImageToImageTest.convertImage());
  115. Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of(FlavorToHardwareTest.convertFlavor());
  116. Server server = ParseServerFromJsonResponseTest.parseServer();
  117. ServerToNodeMetadata parser = new ServerToNodeMetadata(serverStateToNodeState, Suppliers.<Set<? extends Image>> ofInstance(images), Suppliers
  118. .ofInstance(provider), Suppliers.<Set<? extends Hardware>> ofInstance(hardwares), namingConvention);
  119. NodeMetadata metadata = parser.apply(server);
  120. assertEquals(
  121. metadata,
  122. new NodeMetadataBuilder()
  123. .state(NodeState.PENDING)
  124. .publicAddresses(ImmutableSet.of("67.23.10.132", "67.23.10.131"))
  125. .privateAddresses(ImmutableSet.of("10.176.42.16"))
  126. .imageId("2")
  127. .hardware(
  128. new HardwareBuilder()
  129. .ids("1")
  130. .name("256 MB Server")
  131. .processors(ImmutableList.of(new Processor(1.0, 1.0)))
  132. .ram(256)
  133. .volumes(
  134. ImmutableList.of(new VolumeBuilder().type(Volume.Type.LOCAL).size(10.0f)
  135. .durable(true).bootDevice(true).build())).build())
  136. .operatingSystem(
  137. new OperatingSystem.Builder().family(OsFamily.CENTOS).description("CentOS 5.2").version("5.2")
  138. .is64Bit(true).build())
  139. .id("1234")
  140. .providerId("1234")
  141. .name("sample-server")
  142. .group("sample")
  143. .hostname("sample-server")
  144. .location(
  145. new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0")
  146. .description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build())
  147. .userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build());
  148. }
  149. }