PageRenderTime 38ms CodeModel.GetById 28ms app.highlight 8ms RepoModel.GetById 0ms app.codeStats 0ms

/apis/cloudstack/src/test/java/org/jclouds/cloudstack/compute/functions/VirtualMachineToNodeMetadataTest.java

https://github.com/richardcloudsoft/legacy-jclouds
Java | 192 lines | 139 code | 30 blank | 23 comment | 0 complexity | 9546bc616a3720b3f2819f75e5d9fce1 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.cloudstack.compute.functions;
 20
 21import static org.testng.Assert.assertEquals;
 22
 23import java.net.UnknownHostException;
 24import java.util.Set;
 25
 26import org.jclouds.cloudstack.domain.GuestIPType;
 27import org.jclouds.cloudstack.domain.IPForwardingRule;
 28import org.jclouds.cloudstack.domain.NIC;
 29import org.jclouds.cloudstack.domain.TrafficType;
 30import org.jclouds.cloudstack.domain.VirtualMachine;
 31import org.jclouds.cloudstack.parse.ListVirtualMachinesResponseTest;
 32import org.jclouds.compute.domain.Hardware;
 33import org.jclouds.compute.domain.HardwareBuilder;
 34import org.jclouds.compute.domain.Image;
 35import org.jclouds.compute.domain.NodeMetadata;
 36import org.jclouds.compute.domain.NodeMetadata.Status;
 37import org.jclouds.compute.domain.NodeMetadataBuilder;
 38import org.jclouds.compute.functions.GroupNamingConvention;
 39import org.jclouds.date.internal.SimpleDateFormatDateService;
 40import org.jclouds.domain.Location;
 41import org.jclouds.rest.ResourceNotFoundException;
 42import org.testng.annotations.Test;
 43
 44import com.google.common.base.Supplier;
 45import com.google.common.base.Suppliers;
 46import com.google.common.cache.CacheBuilder;
 47import com.google.common.cache.CacheLoader;
 48import com.google.common.collect.ImmutableSet;
 49import com.google.common.collect.Iterables;
 50import com.google.inject.Guice;
 51
 52/**
 53 * @author Adrian Cole, Andrei Savu
 54 */
 55@Test(groups = "unit", testName = "VirtualMachineToNodeMetadataTest")
 56public class VirtualMachineToNodeMetadataTest {
 57
 58   GroupNamingConvention.Factory namingConvention = Guice.createInjector().getInstance(GroupNamingConvention.Factory.class);
 59
 60   @Test
 61   public void testApplyWhereVirtualMachineWithIPForwardingRule() throws UnknownHostException {
 62
 63      Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
 64         .<Location>of(ZoneToLocationTest.one, ZoneToLocationTest.two));
 65
 66      Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
 67         .<Image>of(TemplateToImageTest.one, TemplateToImageTest.two));
 68      VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier,
 69            CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build(
 70                  new CacheLoader<String, Set<IPForwardingRule>>() {
 71                     @Override
 72                     public Set<IPForwardingRule> load(String arg0) throws Exception {
 73                        return ImmutableSet.of(IPForwardingRule.builder().id("1234l").IPAddress("1.1.1.1").build());
 74                     }
 75                  }), namingConvention);
 76
 77      // notice if we've already parsed this properly here, we can rely on it.
 78      VirtualMachine guest = Iterables.get(new ListVirtualMachinesResponseTest().expected(), 0);
 79
 80      NodeMetadata node = parser.apply(guest);
 81
 82      assertEquals(
 83            node.toString(),
 84            new NodeMetadataBuilder().id("54").providerId("54").name("i-3-54-VM").group("i-3-54")
 85                  .location(ZoneToLocationTest.one).status(Status.PENDING).hostname("i-3-54-VM")
 86                  .privateAddresses(ImmutableSet.of("10.1.1.18")).publicAddresses(ImmutableSet.of("1.1.1.1"))
 87                  .hardware(addHypervisor(ServiceOfferingToHardwareTest.one, "XenServer"))
 88                  .imageId(TemplateToImageTest.one.getId())
 89                  .operatingSystem(TemplateToImageTest.one.getOperatingSystem()).build().toString());
 90
 91   }
 92
 93   @Test
 94   public void testApplyWhereVirtualMachineHasNoIpForwardingRuleAndAPublicIP() throws UnknownHostException {
 95
 96      Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
 97         .<Location>of(ZoneToLocationTest.one, ZoneToLocationTest.two));
 98
 99      Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
100         .<Image>of(TemplateToImageTest.one, TemplateToImageTest.two));
101
102      VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier,
103            CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build(
104                  new CacheLoader<String, Set<IPForwardingRule>>() {
105                     @Override
106                     public Set<IPForwardingRule> load(String arg0) throws Exception {
107                        return ImmutableSet.of();
108                     }
109                  }), namingConvention);
110
111      VirtualMachine guest =VirtualMachine.builder()
112         .id("54")
113         .name("i-3-54-VM")
114         .displayName("i-3-54-VM")
115         .account("adrian")
116         .domainId("1")
117         .domain("ROOT")
118         .created(new SimpleDateFormatDateService().iso8601SecondsDateParse("2011-02-16T14:28:37-0800"))
119         .state(VirtualMachine.State.STARTING)
120         .isHAEnabled(false)
121         .zoneId("1")
122         .zoneName("San Jose 1")
123         .templateId("2")
124         .templateName("CentOS 5.3(64-bit) no GUI (XenServer)")
125         .templateDisplayText("CentOS 5.3(64-bit) no GUI (XenServer)")
126         .passwordEnabled(false)
127         .serviceOfferingId("1")
128         .serviceOfferingName("Small Instance")
129         .cpuCount(1)
130         .cpuSpeed(500)
131         .memory(512)
132         .guestOSId("11")
133         .rootDeviceId("0")
134         .rootDeviceType("NetworkFilesystem")
135         .jobId("63l")
136         .jobStatus(0)
137         .nics(ImmutableSet.of(NIC.builder().id("72").networkId("204").netmask("255.255.255.0").gateway("1.1.1.1")
138            .IPAddress("1.1.1.5").trafficType(TrafficType.GUEST).guestIPType(GuestIPType.VIRTUAL)
139            .isDefault(true).build())).hypervisor("XenServer").build();
140
141      NodeMetadata node = parser.apply(guest);
142
143      assertEquals(
144         node.toString(),
145         new NodeMetadataBuilder().id("54").providerId("54").name("i-3-54-VM").group("i-3-54")
146            .location(ZoneToLocationTest.one).status(Status.PENDING).hostname("i-3-54-VM")
147            .privateAddresses(ImmutableSet.<String>of())
148            .publicAddresses(ImmutableSet.<String>of("1.1.1.5"))
149            .hardware(addHypervisor(ServiceOfferingToHardwareTest.one, "XenServer"))
150            .imageId(TemplateToImageTest.one.getId())
151            .operatingSystem(TemplateToImageTest.one.getOperatingSystem()).build().toString());
152   }
153
154   @Test
155   public void testApplyWhereVirtualMachineWithNoPassword() throws UnknownHostException {
156
157      Supplier<Set<? extends Location>> locationSupplier = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
158         .<Location>of(ZoneToLocationTest.one, ZoneToLocationTest.two));
159
160      Supplier<Set<? extends Image>> imageSupplier = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
161         .<Image>of(TemplateToImageTest.one, TemplateToImageTest.two));
162      VirtualMachineToNodeMetadata parser = new VirtualMachineToNodeMetadata(locationSupplier, imageSupplier,
163            CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build(
164                  new CacheLoader<String, Set<IPForwardingRule>>() {
165
166                     @Override
167                     public Set<IPForwardingRule> load(String arg0) throws Exception {
168                        throw new ResourceNotFoundException("no ip forwarding rule for: " + arg0);
169                     }
170
171                  }), namingConvention);
172
173      // notice if we've already parsed this properly here, we can rely on it.
174      VirtualMachine guest = Iterables.get(new ListVirtualMachinesResponseTest().expected(), 0);
175
176      NodeMetadata node = parser.apply(guest);
177
178      assertEquals(
179            node.toString(),
180            new NodeMetadataBuilder().id("54").providerId("54").name("i-3-54-VM").group("i-3-54")
181                  .location(ZoneToLocationTest.one).status(Status.PENDING).hostname("i-3-54-VM")
182                  .privateAddresses(ImmutableSet.of("10.1.1.18"))
183                  .hardware(addHypervisor(ServiceOfferingToHardwareTest.one, "XenServer"))
184                  .imageId(TemplateToImageTest.one.getId())
185                  .operatingSystem(TemplateToImageTest.one.getOperatingSystem()).build().toString());
186   }
187
188   protected Hardware addHypervisor(Hardware in, String hypervisor) {
189      return HardwareBuilder.fromHardware(in).hypervisor(hypervisor).build();
190   }
191
192}