PageRenderTime 39ms CodeModel.GetById 29ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 0ms

/compute/src/test/java/org/jclouds/compute/util/ComputeServiceUtilsTest.java

https://github.com/andreisavu/jclouds
Java | 146 lines | 103 code | 19 blank | 24 comment | 0 complexity | 5b32bc696494ecf87e2b0be0b04a7a29 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.util;
 20
 21import static org.easymock.EasyMock.createMock;
 22import static org.easymock.EasyMock.expect;
 23import static org.easymock.EasyMock.replay;
 24import static org.easymock.EasyMock.verify;
 25import static org.jclouds.compute.util.ComputeServiceUtils.formatStatus;
 26import static org.jclouds.compute.util.ComputeServiceUtils.metadataAndTagsAsCommaDelimitedValue;
 27import static org.jclouds.compute.util.ComputeServiceUtils.metadataAndTagsAsValuesOfEmptyString;
 28import static org.jclouds.compute.util.ComputeServiceUtils.parseVersionOrReturnEmptyString;
 29import static org.testng.Assert.assertEquals;
 30
 31import java.util.Map;
 32
 33import org.jclouds.compute.config.BaseComputeServiceContextModule;
 34import org.jclouds.compute.domain.ComputeMetadataIncludingStatus;
 35import org.jclouds.compute.domain.Image;
 36import org.jclouds.compute.domain.OsFamily;
 37import org.jclouds.compute.options.TemplateOptions;
 38import org.jclouds.compute.reference.ComputeServiceConstants;
 39import org.jclouds.http.HttpRequest;
 40import org.jclouds.json.Json;
 41import org.jclouds.json.config.GsonModule;
 42import org.testng.annotations.Test;
 43
 44import com.google.common.collect.ImmutableMap;
 45import com.google.common.collect.ImmutableSet;
 46import com.google.inject.Guice;
 47
 48/**
 49 * Test the compute utils.
 50 * 
 51 * @author Adrian Cole
 52 * 
 53 */
 54@Test(groups = "unit", testName = "ComputeServiceUtilsTest")
 55public class ComputeServiceUtilsTest {
 56   
 57   @SuppressWarnings("unchecked")
 58   @Test
 59   public void testFormatStatusWithBackendStatus() {
 60      ComputeMetadataIncludingStatus<Image.Status> resource = createMock(ComputeMetadataIncludingStatus.class);
 61      expect(resource.getStatus()).andReturn(Image.Status.PENDING);
 62      expect(resource.getBackendStatus()).andReturn("queued").anyTimes();
 63      replay(resource);
 64      assertEquals(formatStatus(resource), "PENDING[queued]");
 65      verify(resource);
 66   }
 67   
 68   @SuppressWarnings("unchecked")
 69   @Test
 70   public void testFormatStatusWithoutBackendStatus() {
 71      ComputeMetadataIncludingStatus<Image.Status> resource = createMock(ComputeMetadataIncludingStatus.class);
 72      expect(resource.getStatus()).andReturn(Image.Status.PENDING);
 73      expect(resource.getBackendStatus()).andReturn(null).anyTimes();
 74      replay(resource);
 75      assertEquals(formatStatus(resource), "PENDING");
 76      verify(resource);
 77   }
 78   
 79   Map<OsFamily, Map<String, String>> map = new BaseComputeServiceContextModule() {
 80   }.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule())
 81         .getInstance(Json.class));
 82
 83   @Test
 84   public void testMetadataAndTagsAsValuesOfEmptyString() {
 85      TemplateOptions options = TemplateOptions.Builder.tags(ImmutableSet.of("tag")).userMetadata(ImmutableMap.<String, String>of("foo", "bar"));
 86      assertEquals(metadataAndTagsAsValuesOfEmptyString(options), ImmutableMap.<String, String>of("foo", "bar", "tag", ""));
 87   }
 88   
 89   @Test
 90   public void testMetadataAndTagsAsCommaDelimitedValue() {
 91      TemplateOptions options = TemplateOptions.Builder.tags(ImmutableSet.of("tag")).userMetadata(ImmutableMap.<String, String>of("foo", "bar"));
 92      assertEquals(metadataAndTagsAsCommaDelimitedValue(options), ImmutableMap.<String, String>of("foo", "bar", "jclouds_tags", "tag"));
 93   }
 94
 95   @Test
 96   public void testMetadataAndTagsAsValuesOfEmptyStringNoTags() {
 97      TemplateOptions options = TemplateOptions.Builder.userMetadata(ImmutableMap.<String, String>of("foo", "bar"));
 98      assertEquals(metadataAndTagsAsValuesOfEmptyString(options), ImmutableMap.<String, String>of("foo", "bar"));
 99   }
100   
101   @Test
102   public void testMetadataAndTagsAsCommaDelimitedValueNoTags() {
103      TemplateOptions options = TemplateOptions.Builder.userMetadata(ImmutableMap.<String, String>of("foo", "bar"));
104      assertEquals(metadataAndTagsAsCommaDelimitedValue(options), ImmutableMap.<String, String>of("foo", "bar"));
105   }
106   
107   @Test
108   public void testParseVersionOrReturnEmptyStringUbuntu1004() {
109      assertEquals(parseVersionOrReturnEmptyString(OsFamily.UBUNTU, "Ubuntu 10.04", map), "10.04");
110   }
111
112   @Test
113   public void testParseVersionOrReturnEmptyStringUbuntu1104() {
114      assertEquals(parseVersionOrReturnEmptyString(OsFamily.UBUNTU, "ubuntu 11.04 server (i386)", map), "11.04");
115   }
116
117   @Test
118   public void testExecHttpResponse() {
119      HttpRequest request = HttpRequest.builder()
120                                       .method("GET")
121                                       .endpoint("https://adriancolehappy.s3.amazonaws.com/java/install")
122                                       .addHeader("Host", "adriancolehappy.s3.amazonaws.com")
123                                       .addHeader("Date", "Sun, 12 Sep 2010 08:25:19 GMT")
124                                       .addHeader("Authorization", "AWS 0ASHDJAS82:JASHFDA=").build();
125
126      assertEquals(
127            ComputeServiceUtils.execHttpResponse(request).render(org.jclouds.scriptbuilder.domain.OsFamily.UNIX),
128            "curl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 -X GET -H \"Host: adriancolehappy.s3.amazonaws.com\" -H \"Date: Sun, 12 Sep 2010 08:25:19 GMT\" -H \"Authorization: AWS 0ASHDJAS82:JASHFDA=\" https://adriancolehappy.s3.amazonaws.com/java/install |(bash)\n");
129
130   }
131
132   @Test
133   public void testTarxzpHttpResponse() {
134      HttpRequest request = HttpRequest.builder()
135                                       .method("GET")
136                                       .endpoint("https://adriancolehappy.s3.amazonaws.com/java/install")
137                                       .addHeader("Host", "adriancolehappy.s3.amazonaws.com")
138                                       .addHeader("Date", "Sun, 12 Sep 2010 08:25:19 GMT")
139                                       .addHeader("Authorization", "AWS 0ASHDJAS82:JASHFDA=").build();
140            
141      assertEquals(
142            ComputeServiceUtils.extractTargzIntoDirectory(request, "/stage/").render(
143                  org.jclouds.scriptbuilder.domain.OsFamily.UNIX),
144            "curl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 -X GET -H \"Host: adriancolehappy.s3.amazonaws.com\" -H \"Date: Sun, 12 Sep 2010 08:25:19 GMT\" -H \"Authorization: AWS 0ASHDJAS82:JASHFDA=\" https://adriancolehappy.s3.amazonaws.com/java/install |(mkdir -p /stage/ &&cd /stage/ &&tar -xpzf -)\n");
145   }
146}