PageRenderTime 67ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/compute/src/test/java/org/jclouds/compute/BaseTemplateBuilderLiveTest.java

https://github.com/alasdairhodge/jclouds
Java | 304 lines | 235 code | 43 blank | 26 comment | 27 complexity | b0e8952fc5d9f18759f3eb07d4a1307d 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.compute;
  20. import static org.jclouds.compute.util.ComputeServiceUtils.getCores;
  21. import static org.testng.Assert.assertEquals;
  22. import java.io.IOException;
  23. import java.util.Map;
  24. import java.util.Map.Entry;
  25. import java.util.NoSuchElementException;
  26. import java.util.Properties;
  27. import java.util.Set;
  28. import java.util.concurrent.ExecutionException;
  29. import java.util.concurrent.TimeoutException;
  30. import org.jclouds.compute.config.BaseComputeServiceContextModule;
  31. import org.jclouds.compute.domain.Hardware;
  32. import org.jclouds.compute.domain.OsFamily;
  33. import org.jclouds.compute.domain.OsFamilyVersion64Bit;
  34. import org.jclouds.compute.domain.Template;
  35. import org.jclouds.compute.domain.TemplateBuilder;
  36. import org.jclouds.compute.reference.ComputeServiceConstants;
  37. import org.jclouds.domain.Location;
  38. import org.jclouds.domain.LocationScope;
  39. import org.jclouds.domain.LoginCredentials;
  40. import org.jclouds.json.Json;
  41. import org.jclouds.json.config.GsonModule;
  42. import org.jclouds.logging.log4j.config.Log4JLoggingModule;
  43. import org.testng.annotations.AfterTest;
  44. import org.testng.annotations.BeforeClass;
  45. import org.testng.annotations.DataProvider;
  46. import org.testng.annotations.Test;
  47. import com.google.common.base.Predicate;
  48. import com.google.common.base.Predicates;
  49. import com.google.common.base.Splitter;
  50. import com.google.common.collect.ImmutableSet;
  51. import com.google.common.collect.Iterables;
  52. import com.google.common.collect.Sets;
  53. import com.google.inject.Guice;
  54. import com.google.inject.Module;
  55. /**
  56. *
  57. * @author Adrian Cole
  58. */
  59. @Test(groups = "integration,live")
  60. public abstract class BaseTemplateBuilderLiveTest extends BaseVersionedServiceLiveTest {
  61. protected ComputeServiceContext context;
  62. public void testCompareSizes() throws Exception {
  63. Hardware defaultSize = context.getComputeService().templateBuilder().build().getHardware();
  64. Hardware smallest = context.getComputeService().templateBuilder().smallest().build().getHardware();
  65. Hardware fastest = context.getComputeService().templateBuilder().fastest().build().getHardware();
  66. Hardware biggest = context.getComputeService().templateBuilder().biggest().build().getHardware();
  67. System.out.printf("smallest %s%n", smallest);
  68. System.out.printf("fastest %s%n", fastest);
  69. System.out.printf("biggest %s%n", biggest);
  70. assertEquals(defaultSize, smallest);
  71. assert getCores(smallest) <= getCores(fastest) : String.format("%s ! <= %s", smallest, fastest);
  72. assert getCores(biggest) <= getCores(fastest) : String.format("%s ! <= %s", biggest, fastest);
  73. assert biggest.getRam() >= fastest.getRam() : String.format("%s ! >= %s", biggest, fastest);
  74. assert biggest.getRam() >= smallest.getRam() : String.format("%s ! >= %s", biggest, smallest);
  75. assert getCores(fastest) >= getCores(biggest) : String.format("%s ! >= %s", fastest, biggest);
  76. assert getCores(fastest) >= getCores(smallest) : String.format("%s ! >= %s", fastest, smallest);
  77. }
  78. public void testFromTemplate() {
  79. Template defaultTemplate = context.getComputeService().templateBuilder().build();
  80. assertEquals(context.getComputeService().templateBuilder().fromTemplate(defaultTemplate).build().toString(),
  81. defaultTemplate.toString());
  82. }
  83. @BeforeClass
  84. public void setupClient() throws InterruptedException, ExecutionException, TimeoutException, IOException {
  85. setupCredentials();
  86. context = new ComputeServiceContextFactory(setupRestProperties()).createContext(provider,
  87. ImmutableSet.<Module> of(new Log4JLoggingModule()), setupProperties());
  88. }
  89. @DataProvider(name = "osSupported")
  90. public Object[][] osSupported() {
  91. return convertToArray(Sets.filter(provideAllOperatingSystems(),
  92. Predicates.not(defineUnsupportedOperatingSystems())));
  93. }
  94. protected Object[][] convertToArray(Set<OsFamilyVersion64Bit> supportedOperatingSystems) {
  95. Object[][] returnVal = new Object[supportedOperatingSystems.size()][1];
  96. int i = 0;
  97. for (OsFamilyVersion64Bit config : supportedOperatingSystems)
  98. returnVal[i++][0] = config;
  99. return returnVal;
  100. }
  101. protected Predicate<OsFamilyVersion64Bit> defineUnsupportedOperatingSystems() {
  102. return Predicates.alwaysFalse();
  103. }
  104. @DataProvider(name = "osNotSupported")
  105. public Object[][] osNotSupported() {
  106. return convertToArray(Sets.filter(provideAllOperatingSystems(), defineUnsupportedOperatingSystems()));
  107. }
  108. protected Set<OsFamilyVersion64Bit> provideAllOperatingSystems() {
  109. Map<OsFamily, Map<String, String>> map = new BaseComputeServiceContextModule() {
  110. }.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule())
  111. .getInstance(Json.class));
  112. Set<OsFamilyVersion64Bit> supportedOperatingSystems = Sets.newHashSet();
  113. for (Entry<OsFamily, Map<String, String>> osVersions : map.entrySet()) {
  114. for (String version : Sets.newHashSet(osVersions.getValue().values())) {
  115. supportedOperatingSystems.add(new OsFamilyVersion64Bit(osVersions.getKey(), version, false));
  116. supportedOperatingSystems.add(new OsFamilyVersion64Bit(osVersions.getKey(), version, true));
  117. }
  118. }
  119. return supportedOperatingSystems;
  120. }
  121. @Test(dataProvider = "osSupported")
  122. public void testTemplateBuilderCanFind(OsFamilyVersion64Bit matrix) throws InterruptedException {
  123. TemplateBuilder builder = context.getComputeService().templateBuilder().osFamily(matrix.family)
  124. .os64Bit(matrix.is64Bit);
  125. if (!matrix.version.equals(""))
  126. builder.osVersionMatches("^" + matrix.version + "$");
  127. Template template = builder.build();
  128. if (!matrix.version.equals(""))
  129. assertEquals(template.getImage().getOperatingSystem().getVersion(), matrix.version);
  130. assertEquals(template.getImage().getOperatingSystem().is64Bit(), matrix.is64Bit);
  131. assertEquals(template.getImage().getOperatingSystem().getFamily(), matrix.family);
  132. }
  133. @Test(dataProvider = "osNotSupported", expectedExceptions = NoSuchElementException.class)
  134. public void testTemplateBuilderCannotFind(OsFamilyVersion64Bit matrix) throws InterruptedException {
  135. TemplateBuilder builder = context.getComputeService().templateBuilder().osFamily(matrix.family)
  136. .os64Bit(matrix.is64Bit);
  137. if (!matrix.version.equals(""))
  138. builder.osVersionMatches("^" + matrix.version + "$");
  139. builder.build();
  140. }
  141. @Test
  142. public void testTemplateBuilderCanUseImageId() throws Exception {
  143. Template defaultTemplate = context.getComputeService().templateBuilder().build();
  144. context.close();
  145. setupClient();
  146. Template template = context.getComputeService().templateBuilder().imageId(defaultTemplate.getImage().getId())
  147. .locationId(defaultTemplate.getLocation().getId()).build();
  148. assertEquals(template.getImage(), defaultTemplate.getImage());
  149. }
  150. @Test
  151. public void testDefaultTemplateBuilder() throws IOException {
  152. Template defaultTemplate = context.getComputeService().templateBuilder().build();
  153. assert defaultTemplate.getImage().getOperatingSystem().getVersion().matches("1[012].[10][04]") : defaultTemplate
  154. .getImage().getOperatingSystem().getVersion();
  155. assertEquals(defaultTemplate.getImage().getOperatingSystem().is64Bit(), true);
  156. assertEquals(defaultTemplate.getImage().getOperatingSystem().getFamily(), OsFamily.UBUNTU);
  157. assertEquals(getCores(defaultTemplate.getHardware()), 1.0d);
  158. }
  159. protected abstract Set<String> getIso3166Codes();
  160. @Test(groups = { "integration", "live" })
  161. public void testGetAssignableLocations() throws Exception {
  162. assertProvider(context.getProviderSpecificContext());
  163. for (Location location : context.getComputeService().listAssignableLocations()) {
  164. System.err.printf("location %s%n", location);
  165. assert location.getId() != null : location;
  166. assert location != location.getParent() : location;
  167. assert location.getScope() != null : location;
  168. switch (location.getScope()) {
  169. case PROVIDER:
  170. assertProvider(location);
  171. break;
  172. case REGION:
  173. assertProvider(location.getParent());
  174. assert location.getIso3166Codes().size() == 0
  175. || location.getParent().getIso3166Codes().containsAll(location.getIso3166Codes()) : location + " ||"
  176. + location.getParent();
  177. break;
  178. case ZONE:
  179. Location provider = location.getParent().getParent();
  180. // zone can be a direct descendant of provider
  181. if (provider == null)
  182. provider = location.getParent();
  183. assertProvider(provider);
  184. assert location.getIso3166Codes().size() == 0
  185. || location.getParent().getIso3166Codes().containsAll(location.getIso3166Codes()) : location + " ||"
  186. + location.getParent();
  187. break;
  188. case HOST:
  189. Location provider2 = location.getParent().getParent().getParent();
  190. // zone can be a direct descendant of provider
  191. if (provider2 == null)
  192. provider2 = location.getParent().getParent();
  193. assertProvider(provider2);
  194. break;
  195. }
  196. }
  197. }
  198. @Test
  199. public void testTemplateBuilderWithImageIdSpecified() throws IOException {
  200. Template defaultTemplate = context.getComputeService().templateBuilder().build();
  201. ComputeServiceContext context = null;
  202. try {
  203. Properties overrides = setupProperties();
  204. overrides.setProperty("jclouds.image-id", defaultTemplate.getImage().getId());
  205. context = new ComputeServiceContextFactory().createContext(provider,
  206. ImmutableSet.<Module> of(new Log4JLoggingModule()), overrides);
  207. assertEquals(context.getComputeService().templateBuilder().build().toString(), defaultTemplate.toString());
  208. } finally {
  209. if (context != null)
  210. context.close();
  211. }
  212. context = null;
  213. try {
  214. Properties overrides = setupProperties();
  215. overrides.setProperty(provider + ".image-id", defaultTemplate.getImage().getId());
  216. context = new ComputeServiceContextFactory().createContext(provider,
  217. ImmutableSet.<Module> of(new Log4JLoggingModule()), overrides);
  218. assertEquals(context.getComputeService().templateBuilder().build().toString(), defaultTemplate.toString());
  219. } finally {
  220. if (context != null)
  221. context.close();
  222. }
  223. }
  224. @Test
  225. public void testTemplateBuilderWithLoginUserSpecified() throws IOException {
  226. tryOverrideUsingPropertyKey("jclouds");
  227. tryOverrideUsingPropertyKey(provider);
  228. }
  229. protected void tryOverrideUsingPropertyKey(String propertyKey) {
  230. ComputeServiceContext context = null;
  231. try {
  232. Properties overrides = setupProperties();
  233. String login = loginUser != null ? loginUser : "foo:bar";
  234. overrides.setProperty(propertyKey + ".image.login-user", login);
  235. boolean auth = authenticateSudo != null ? Boolean.valueOf(authenticateSudo) : true;
  236. overrides.setProperty(propertyKey + ".image.authenticate-sudo", auth + "");
  237. context = new ComputeServiceContextFactory().createContext(provider,
  238. ImmutableSet.<Module> of(new Log4JLoggingModule()), overrides);
  239. Iterable<String> userPass = Splitter.on(':').split(login);
  240. String user = Iterables.get(userPass, 0);
  241. String pass = Iterables.size(userPass) > 1 ? Iterables.get(userPass, 1) : null;
  242. assertEquals(context.getComputeService().templateBuilder().build().getImage().getDefaultCredentials(),
  243. LoginCredentials.builder().user(user).password(pass).authenticateSudo(auth).build());
  244. } finally {
  245. if (context != null)
  246. context.close();
  247. // Need to clear persisted credentials; otherwise next time a ComputeServiceContext is created
  248. // then it will have these "foo" credentials!
  249. context.credentialStore().clear();
  250. }
  251. }
  252. void assertProvider(Location provider) {
  253. assertEquals(provider.getScope(), LocationScope.PROVIDER);
  254. assertEquals(provider.getParent(), null);
  255. assertEquals(provider.getIso3166Codes(), getIso3166Codes());
  256. }
  257. @AfterTest
  258. protected void cleanup() throws InterruptedException, ExecutionException, TimeoutException {
  259. context.close();
  260. }
  261. }