PageRenderTime 186ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

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