PageRenderTime 66ms CodeModel.GetById 21ms app.highlight 36ms RepoModel.GetById 5ms app.codeStats 0ms

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

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