PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/functions/VirtualMachineToNodeMetadataTest.java

https://github.com/richardcloudsoft/legacy-jclouds
Java | 192 lines | 139 code | 30 blank | 23 comment | 0 complexity | 9546bc616a3720b3f2819f75e5d9fce1 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.cloudstack.compute.functions;
  20. import static org.testng.Assert.assertEquals;
  21. import java.net.UnknownHostException;
  22. import java.util.Set;
  23. import org.jclouds.cloudstack.domain.GuestIPType;
  24. import org.jclouds.cloudstack.domain.IPForwardingRule;
  25. import org.jclouds.cloudstack.domain.NIC;
  26. import org.jclouds.cloudstack.domain.TrafficType;
  27. import org.jclouds.cloudstack.domain.VirtualMachine;
  28. import org.jclouds.cloudstack.parse.ListVirtualMachinesResponseTest;
  29. import org.jclouds.compute.domain.Hardware;
  30. import org.jclouds.compute.domain.HardwareBuilder;
  31. import org.jclouds.compute.domain.Image;
  32. import org.jclouds.compute.domain.NodeMetadata;
  33. import org.jclouds.compute.domain.NodeMetadata.Status;
  34. import org.jclouds.compute.domain.NodeMetadataBuilder;
  35. import org.jclouds.compute.functions.GroupNamingConvention;
  36. import org.jclouds.date.internal.SimpleDateFormatDateService;
  37. import org.jclouds.domain.Location;
  38. import org.jclouds.rest.ResourceNotFoundException;
  39. import org.testng.annotations.Test;
  40. import com.google.common.base.Supplier;
  41. import com.google.common.base.Suppliers;
  42. import com.google.common.cache.CacheBuilder;
  43. import com.google.common.cache.CacheLoader;
  44. import com.google.common.collect.ImmutableSet;
  45. import com.google.common.collect.Iterables;
  46. import com.google.inject.Guice;
  47. /**
  48. * @author Adrian Cole, Andrei Savu
  49. */
  50. @Test(groups = "unit", testName = "VirtualMachineToNodeMetadataTest")
  51. public class VirtualMachineToNodeMetadataTest {
  52. GroupNamingConvention.Factory namingConvention = Guice.createInjector().getInstance(GroupNamingConvention.Factory.class);
  53. @Test
  54. public void testApplyWhereVirtualMachineWithIPForwardingRule() throws UnknownHostException {
  55. Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
  56. .<Location>of(ZoneToLocationTest.one, ZoneToLocationTest.two));
  57. Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
  58. .<Image>of(TemplateToImageTest.one, TemplateToImageTest.two));
  59. VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier,
  60. CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build(
  61. new CacheLoader<String, Set<IPForwardingRule>>() {
  62. @Override
  63. public Set<IPForwardingRule> load(String arg0) throws Exception {
  64. return ImmutableSet.of(IPForwardingRule.builder().id("1234l").IPAddress("1.1.1.1").build());
  65. }
  66. }), namingConvention);
  67. // notice if we've already parsed this properly here, we can rely on it.
  68. VirtualMachine guest = Iterables.get(new ListVirtualMachinesResponseTest().expected(), 0);
  69. NodeMetadata node = parser.apply(guest);
  70. assertEquals(
  71. node.toString(),
  72. new NodeMetadataBuilder().id("54").providerId("54").name("i-3-54-VM").group("i-3-54")
  73. .location(ZoneToLocationTest.one).status(Status.PENDING).hostname("i-3-54-VM")
  74. .privateAddresses(ImmutableSet.of("10.1.1.18")).publicAddresses(ImmutableSet.of("1.1.1.1"))
  75. .hardware(addHypervisor(ServiceOfferingToHardwareTest.one, "XenServer"))
  76. .imageId(TemplateToImageTest.one.getId())
  77. .operatingSystem(TemplateToImageTest.one.getOperatingSystem()).build().toString());
  78. }
  79. @Test
  80. public void testApplyWhereVirtualMachineHasNoIpForwardingRuleAndAPublicIP() throws UnknownHostException {
  81. Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
  82. .<Location>of(ZoneToLocationTest.one, ZoneToLocationTest.two));
  83. Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
  84. .<Image>of(TemplateToImageTest.one, TemplateToImageTest.two));
  85. VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier,
  86. CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build(
  87. new CacheLoader<String, Set<IPForwardingRule>>() {
  88. @Override
  89. public Set<IPForwardingRule> load(String arg0) throws Exception {
  90. return ImmutableSet.of();
  91. }
  92. }), namingConvention);
  93. VirtualMachine guest =VirtualMachine.builder()
  94. .id("54")
  95. .name("i-3-54-VM")
  96. .displayName("i-3-54-VM")
  97. .account("adrian")
  98. .domainId("1")
  99. .domain("ROOT")
  100. .created(new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-02-16T14:28:37-0800"))
  101. .state(VirtualMachine.State.STARTING)
  102. .isHAEnabled(false)
  103. .zoneId("1")
  104. .zoneName("San Jose 1")
  105. .templateId("2")
  106. .templateName("CentOS 5.3(64-bit) no GUI (XenServer)")
  107. .templateDisplayText("CentOS 5.3(64-bit) no GUI (XenServer)")
  108. .passwordEnabled(false)
  109. .serviceOfferingId("1")
  110. .serviceOfferingName("Small Instance")
  111. .cpuCount(1)
  112. .cpuSpeed(500)
  113. .memory(512)
  114. .guestOSId("11")
  115. .rootDeviceId("0")
  116. .rootDeviceType("NetworkFilesystem")
  117. .jobId("63l")
  118. .jobStatus(0)
  119. .nics(ImmutableSet.of(NIC.builder().id("72").networkId("204").netmask("255.255.255.0").gateway("1.1.1.1")
  120. .IPAddress("1.1.1.5").trafficType(TrafficType.GUEST).guestIPType(GuestIPType.VIRTUAL)
  121. .isDefault(true).build())).hypervisor("XenServer").build();
  122. NodeMetadata node = parser.apply(guest);
  123. assertEquals(
  124. node.toString(),
  125. new NodeMetadataBuilder().id("54").providerId("54").name("i-3-54-VM").group("i-3-54")
  126. .location(ZoneToLocationTest.one).status(Status.PENDING).hostname("i-3-54-VM")
  127. .privateAddresses(ImmutableSet.<String>of())
  128. .publicAddresses(ImmutableSet.<String>of("1.1.1.5"))
  129. .hardware(addHypervisor(ServiceOfferingToHardwareTest.one, "XenServer"))
  130. .imageId(TemplateToImageTest.one.getId())
  131. .operatingSystem(TemplateToImageTest.one.getOperatingSystem()).build().toString());
  132. }
  133. @Test
  134. public void testApplyWhereVirtualMachineWithNoPassword() throws UnknownHostException {
  135. Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
  136. .<Location>of(ZoneToLocationTest.one, ZoneToLocationTest.two));
  137. Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
  138. .<Image>of(TemplateToImageTest.one, TemplateToImageTest.two));
  139. VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier,
  140. CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build(
  141. new CacheLoader<String, Set<IPForwardingRule>>() {
  142. @Override
  143. public Set<IPForwardingRule> load(String arg0) throws Exception {
  144. throw new ResourceNotFoundException("no ip forwarding rule for: " + arg0);
  145. }
  146. }), namingConvention);
  147. // notice if we've already parsed this properly here, we can rely on it.
  148. VirtualMachine guest = Iterables.get(new ListVirtualMachinesResponseTest().expected(), 0);
  149. NodeMetadata node = parser.apply(guest);
  150. assertEquals(
  151. node.toString(),
  152. new NodeMetadataBuilder().id("54").providerId("54").name("i-3-54-VM").group("i-3-54")
  153. .location(ZoneToLocationTest.one).status(Status.PENDING).hostname("i-3-54-VM")
  154. .privateAddresses(ImmutableSet.of("10.1.1.18"))
  155. .hardware(addHypervisor(ServiceOfferingToHardwareTest.one, "XenServer"))
  156. .imageId(TemplateToImageTest.one.getId())
  157. .operatingSystem(TemplateToImageTest.one.getOperatingSystem()).build().toString());
  158. }
  159. protected Hardware addHypervisor(Hardware in, String hypervisor) {
  160. return HardwareBuilder.fromHardware(in).hypervisor(hypervisor).build();
  161. }
  162. }