PageRenderTime 79ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/jclouds/jclouds
Java | 158 lines | 122 code | 16 blank | 20 comment | 1 complexity | 0f70cc0a3aaa4cfb9ad7344d3ac05302 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.softlayer.compute.functions;
  18. import static org.testng.Assert.assertEquals;
  19. import static org.testng.Assert.assertNotNull;
  20. import static org.testng.Assert.assertTrue;
  21. import java.util.Set;
  22. import org.jclouds.compute.domain.NodeMetadata;
  23. import org.jclouds.compute.domain.OsFamily;
  24. import org.jclouds.compute.functions.GroupNamingConvention;
  25. import org.jclouds.domain.Location;
  26. import org.jclouds.domain.LocationBuilder;
  27. import org.jclouds.domain.LocationScope;
  28. import org.jclouds.softlayer.domain.Datacenter;
  29. import org.jclouds.softlayer.domain.OperatingSystem;
  30. import org.jclouds.softlayer.domain.Password;
  31. import org.jclouds.softlayer.domain.PowerState;
  32. import org.jclouds.softlayer.domain.SoftwareDescription;
  33. import org.jclouds.softlayer.domain.SoftwareLicense;
  34. import org.jclouds.softlayer.domain.VirtualGuest;
  35. import org.testng.annotations.Test;
  36. import com.google.common.base.Supplier;
  37. import com.google.common.base.Suppliers;
  38. import com.google.common.collect.ImmutableSet;
  39. import com.google.common.collect.Iterables;
  40. import com.google.common.collect.Sets;
  41. import com.google.inject.Guice;
  42. /**
  43. * Tests the function that transforms SoftLayer VirtualGuest to NodeMetadata.
  44. */
  45. @Test(groups = "unit", testName = "VirtualGuestToNodeMetadataTest")
  46. public class VirtualGuestToNodeMetadataTest {
  47. VirtualGuestToImage virtualGuestToImage = Guice.createInjector().getInstance(VirtualGuestToImage.class);
  48. VirtualGuestToHardware virtualGuestToHardware = Guice.createInjector().getInstance(VirtualGuestToHardware.class);
  49. GroupNamingConvention.Factory namingConvention = Guice.createInjector().getInstance(GroupNamingConvention.Factory.class);
  50. Location location = new LocationBuilder().id("test")
  51. .description("example")
  52. .scope(LocationScope.ZONE)
  53. .build();
  54. Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet.of(location));
  55. @Test
  56. public void testVirtualGuestToNodeMetadata() {
  57. VirtualGuest virtualGuest = createVirtualGuest();
  58. NodeMetadata nodeMetadata = new VirtualGuestToNodeMetadata(locationSupplier, namingConvention,
  59. virtualGuestToImage, virtualGuestToHardware).apply(virtualGuest);
  60. assertNotNull(nodeMetadata);
  61. assertEquals(nodeMetadata.getName(), virtualGuest.getHostname());
  62. assertNotNull(nodeMetadata.getLocation());
  63. assertEquals(nodeMetadata.getLocation().getId(), location.getId());
  64. assertEquals(nodeMetadata.getHostname(), virtualGuest.getFullyQualifiedDomainName());
  65. assertEquals(nodeMetadata.getHardware().getRam(), virtualGuest.getMaxMemory());
  66. assertTrue(nodeMetadata.getHardware().getProcessors().size() == 1);
  67. assertEquals(Iterables.get(nodeMetadata.getHardware().getProcessors(), 0).getCores(), (double) virtualGuest.getStartCpus());
  68. assertEquals(nodeMetadata.getOperatingSystem().getFamily(), OsFamily.UBUNTU);
  69. assertEquals(nodeMetadata.getOperatingSystem().getVersion(), "12.04");
  70. assertEquals(nodeMetadata.getOperatingSystem().is64Bit(), true);
  71. }
  72. private VirtualGuest createVirtualGuest() {
  73. return VirtualGuest.builder()
  74. .domain("example.com")
  75. .hostname("host1")
  76. .fullyQualifiedDomainName("host1.example.com")
  77. .id(1301396)
  78. .maxMemory(1024)
  79. .startCpus(1)
  80. .localDiskFlag(true)
  81. .operatingSystem(OperatingSystem.builder().id("UBUNTU_LATEST")
  82. .operatingSystemReferenceCode("UBUNTU_LATEST")
  83. .softwareLicense(SoftwareLicense.builder()
  84. .softwareDescription(SoftwareDescription.builder()
  85. .version("12.04-64 Minimal for CCI")
  86. .referenceCode("UBUNTU_12_64")
  87. .longDescription("Ubuntu Linux 12.04 LTS Precise Pangolin - Minimal Install (64 bit)")
  88. .build())
  89. .build())
  90. .build())
  91. .datacenter(Datacenter.builder().name("test").build())
  92. .powerState(PowerState.builder().keyName(VirtualGuest.State.RUNNING).build())
  93. .build();
  94. }
  95. @Test(expectedExceptions = { IllegalStateException.class })
  96. public void testGetBestPasswordNone() {
  97. Set<Password> passwords = Sets.newLinkedHashSet();
  98. VirtualGuestToNodeMetadata f = new VirtualGuestToNodeMetadata(locationSupplier, namingConvention,
  99. virtualGuestToImage, virtualGuestToHardware);
  100. f.getBestPassword(passwords, null);
  101. }
  102. @Test
  103. public void testGetBestPasswordOneRoot() {
  104. Set<Password> passwords = Sets.newLinkedHashSet();
  105. passwords.add(new Password(1, "root", "pass"));
  106. VirtualGuestToNodeMetadata f = new VirtualGuestToNodeMetadata(locationSupplier, namingConvention,
  107. virtualGuestToImage, virtualGuestToHardware);
  108. Password best = f.getBestPassword(passwords, null);
  109. assertEquals(best.getUsername(), "root");
  110. }
  111. @Test
  112. public void testGetBestPasswordOneNonRoot() {
  113. Set<Password> passwords = Sets.newLinkedHashSet();
  114. passwords.add(new Password(1, "nonroot", "word"));
  115. VirtualGuestToNodeMetadata f = new VirtualGuestToNodeMetadata(locationSupplier, namingConvention,
  116. virtualGuestToImage, virtualGuestToHardware);
  117. Password best = f.getBestPassword(passwords, null);
  118. assertEquals(best.getUsername(), "nonroot");
  119. }
  120. @Test
  121. public void testGetBestPasswordTwoDifferent() {
  122. Set<Password> passwords = Sets.newLinkedHashSet();
  123. passwords.add(new Password(1, "nonroot", "word"));
  124. passwords.add(new Password(2, "root", "pass"));
  125. VirtualGuestToNodeMetadata f = new VirtualGuestToNodeMetadata(locationSupplier, namingConvention,
  126. virtualGuestToImage, virtualGuestToHardware);
  127. Password best = f.getBestPassword(passwords, null);
  128. assertEquals(best.getUsername(), "root");
  129. }
  130. @Test
  131. public void testGetBestPasswordTwoSame() {
  132. Set<Password> passwords = Sets.newLinkedHashSet();
  133. passwords.add(new Password(1, "root", "word"));
  134. passwords.add(new Password(2, "root", "pass"));
  135. VirtualGuestToNodeMetadata f = new VirtualGuestToNodeMetadata(locationSupplier, namingConvention,
  136. virtualGuestToImage, virtualGuestToHardware);
  137. Password best = f.getBestPassword(passwords, null);
  138. assertEquals(best.getUsername(), "root");
  139. // should take the first
  140. assertEquals(best.getPassword(), "word");
  141. }
  142. }