PageRenderTime 732ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/providers/profitbricks/src/test/java/org/jclouds/profitbricks/compute/function/ServerToNodeMetadataTest.java

http://github.com/jclouds/jclouds
Java | 204 lines | 169 code | 19 blank | 16 comment | 0 complexity | 9413ca9a2501db6d2d40cca9e4d579c9 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.profitbricks.compute.function;
  18. import static org.easymock.EasyMock.expect;
  19. import static org.easymock.EasyMock.replay;
  20. import static org.easymock.EasyMock.verify;
  21. import static org.jclouds.profitbricks.domain.Location.DE_FRA;
  22. import static org.testng.Assert.assertEquals;
  23. import static org.testng.Assert.assertNotNull;
  24. import java.util.Set;
  25. import org.easymock.EasyMock;
  26. import org.jclouds.compute.domain.HardwareBuilder;
  27. import org.jclouds.compute.domain.NodeMetadata;
  28. import org.jclouds.compute.domain.NodeMetadataBuilder;
  29. import org.jclouds.compute.domain.OperatingSystem;
  30. import org.jclouds.compute.domain.OsFamily;
  31. import org.jclouds.compute.domain.Processor;
  32. import org.jclouds.compute.domain.Volume;
  33. import org.jclouds.compute.domain.VolumeBuilder;
  34. import org.jclouds.compute.functions.GroupNamingConvention;
  35. import org.jclouds.domain.Location;
  36. import org.jclouds.domain.LocationBuilder;
  37. import org.jclouds.domain.LocationScope;
  38. import org.jclouds.profitbricks.ProfitBricksApi;
  39. import org.jclouds.profitbricks.ProfitBricksApiMetadata;
  40. import org.jclouds.profitbricks.domain.AvailabilityZone;
  41. import org.jclouds.profitbricks.domain.DataCenter;
  42. import org.jclouds.profitbricks.domain.Nic;
  43. import org.jclouds.profitbricks.domain.OsType;
  44. import org.jclouds.profitbricks.domain.ProvisioningState;
  45. import org.jclouds.profitbricks.domain.Server;
  46. import org.jclouds.profitbricks.domain.Storage;
  47. import org.jclouds.profitbricks.features.DataCenterApi;
  48. import org.testng.annotations.AfterMethod;
  49. import org.testng.annotations.BeforeMethod;
  50. import org.testng.annotations.Test;
  51. import com.google.common.base.Supplier;
  52. import com.google.common.collect.ImmutableList;
  53. import com.google.common.collect.ImmutableSet;
  54. import com.google.inject.AbstractModule;
  55. import com.google.inject.Guice;
  56. import com.google.inject.name.Names;
  57. @Test(groups = "unit", testName = "ServerToNodeMetadataTest")
  58. public class ServerToNodeMetadataTest {
  59. private ServerToNodeMetadata fnNodeMetadata;
  60. private ProfitBricksApi api;
  61. private DataCenterApi dataCenterApi;
  62. @BeforeMethod
  63. public void setup() {
  64. Supplier<Set<? extends Location>> locationsSupply = new Supplier<Set<? extends Location>>() {
  65. @Override
  66. public Set<? extends Location> get() {
  67. return ImmutableSet.of(
  68. new LocationBuilder()
  69. .id("de/fra")
  70. .description("de/fra")
  71. .scope(LocationScope.ZONE)
  72. .parent(new LocationBuilder()
  73. .id("de")
  74. .description("de")
  75. .scope(LocationScope.REGION)
  76. .build())
  77. .build());
  78. }
  79. };
  80. GroupNamingConvention.Factory namingConvention = Guice.createInjector(new AbstractModule() {
  81. @Override
  82. protected void configure() {
  83. Names.bindProperties(binder(), new ProfitBricksApiMetadata().getDefaultProperties());
  84. }
  85. }).getInstance(GroupNamingConvention.Factory.class);
  86. dataCenterApi = EasyMock.createMock(DataCenterApi.class);
  87. api = EasyMock.createMock(ProfitBricksApi.class);
  88. expect(dataCenterApi.getDataCenter("mock")).andReturn(
  89. DataCenter.builder().id("mock").version(10).location(DE_FRA).build());
  90. expect(api.dataCenterApi()).andReturn(dataCenterApi);
  91. replay(dataCenterApi, api);
  92. this.fnNodeMetadata = new ServerToNodeMetadata(new StorageToVolume(), locationsSupply, api, namingConvention);
  93. }
  94. @AfterMethod
  95. public void tearDown() {
  96. verify(api, dataCenterApi);
  97. }
  98. @Test
  99. public void testServerToNodeMetadata() {
  100. Server server = Server.builder()
  101. .dataCenter(DataCenter.builder()
  102. .id("mock")
  103. .version(10)
  104. .location(org.jclouds.profitbricks.domain.Location.DE_FRA)
  105. .build())
  106. .id("qwertyui-qwer-qwer-qwer-qwertyyuiiop")
  107. .name("mock-facebook-node")
  108. .cores(4)
  109. .ram(4096)
  110. .hasInternetAccess(true)
  111. .state(ProvisioningState.AVAILABLE)
  112. .status(Server.Status.RUNNING)
  113. .osType(OsType.LINUX)
  114. .availabilityZone(AvailabilityZone.AUTO)
  115. .isCpuHotPlug(true)
  116. .isRamHotPlug(true)
  117. .isNicHotPlug(true)
  118. .isNicHotUnPlug(true)
  119. .isDiscVirtioHotPlug(true)
  120. .isDiscVirtioHotUnPlug(true)
  121. .storages(ImmutableList.<Storage>of(
  122. Storage.builder()
  123. .bootDevice(true)
  124. .busType(Storage.BusType.VIRTIO)
  125. .deviceNumber(1)
  126. .size(40f)
  127. .id("qswdefrg-qaws-qaws-defe-rgrgdsvcxbrh")
  128. .name("facebook-storage")
  129. .build()
  130. )
  131. )
  132. .nics(ImmutableList.<Nic>of(
  133. Nic.builder()
  134. .id("qwqwqwqw-wewe-erer-rtrt-tytytytytyty")
  135. .lanId(1)
  136. .dataCenterId("12345678-abcd-efgh-ijkl-987654321000")
  137. .internetAccess(true)
  138. .serverId("qwertyui-qwer-qwer-qwer-qwertyyuiiop")
  139. .macAddress("02:01:09:cd:f0:b0")
  140. .ips( ImmutableList.<String>of("173.252.120.6"))
  141. .build()
  142. )
  143. )
  144. .build();
  145. NodeMetadata expected = fnNodeMetadata.apply(server);
  146. assertNotNull(expected);
  147. NodeMetadata actual = new NodeMetadataBuilder()
  148. .group("mock")
  149. .ids(server.id())
  150. .name(server.name())
  151. .backendStatus("AVAILABLE")
  152. .status(NodeMetadata.Status.RUNNING)
  153. .hardware(new HardwareBuilder()
  154. .ids("cpu=4,ram=4096,disk=40")
  155. .name("cpu=4,ram=4096,disk=40")
  156. .ram(server.ram())
  157. .processor(new Processor(server.cores(), 1d))
  158. .hypervisor("kvm")
  159. .volume(new VolumeBuilder()
  160. .bootDevice(true)
  161. .size(40f)
  162. .id("qswdefrg-qaws-qaws-defe-rgrgdsvcxbrh")
  163. .durable(true)
  164. .type(Volume.Type.LOCAL)
  165. .build())
  166. .build())
  167. .operatingSystem(new OperatingSystem.Builder()
  168. .description(OsFamily.LINUX.value())
  169. .family(OsFamily.LINUX)
  170. .build())
  171. .location(new LocationBuilder()
  172. .id("de/fra")
  173. .description("de/fra")
  174. .scope(LocationScope.ZONE)
  175. .parent(new LocationBuilder()
  176. .id("de")
  177. .description("de")
  178. .scope(LocationScope.REGION)
  179. .build())
  180. .build())
  181. .publicAddresses(ImmutableList.<String>of("173.252.120.6"))
  182. .build();
  183. assertEquals(actual, expected);
  184. }
  185. }