From b59219622625ab8841005edaf289d2a10683e00c Mon Sep 17 00:00:00 2001 From: Mattias Holmqvist Date: Sun, 23 Oct 2011 00:38:14 +0200 Subject: [PATCH] Reverted listImages back from Yaml to regular Vbox. --- .../VirtualBoxComputeServiceAdapter.java | 51 +++++----- ...VirtualBoxComputeServiceContextModule.java | 7 +- ...rtualBoxComputeServiceAdapterLiveTest.java | 40 +++++--- .../VirtualBoxComputeServiceAdapterTest.java | 95 +++++++++++++++++++ 4 files changed, 151 insertions(+), 42 deletions(-) create mode 100644 sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterTest.java diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java index f543290ae0..d4f8d32d87 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapter.java @@ -21,30 +21,26 @@ package org.jclouds.virtualbox.compute; -import static com.google.common.base.Preconditions.checkNotNull; - -import java.io.InputStream; -import java.util.Collections; -import java.util.Map; - -import javax.inject.Inject; - +import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.common.base.Throwables; +import com.google.inject.Singleton; import org.jclouds.compute.ComputeServiceAdapter; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Template; import org.jclouds.domain.Credentials; import org.jclouds.domain.Location; +import org.jclouds.javax.annotation.Nullable; import org.jclouds.location.suppliers.JustProvider; -import org.jclouds.virtualbox.functions.admin.ImageFromYamlStream; -import org.virtualbox_4_1.CleanupMode; -import org.virtualbox_4_1.IMachine; -import org.virtualbox_4_1.IProgress; -import org.virtualbox_4_1.ISession; -import org.virtualbox_4_1.SessionState; -import org.virtualbox_4_1.VirtualBoxManager; +import org.virtualbox_4_1.*; -import com.google.common.base.Throwables; -import com.google.inject.Singleton; +import javax.inject.Inject; +import java.util.*; + +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.collect.Iterables.filter; +import static com.google.common.collect.Iterables.transform; +import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX; /** * Defines the connection between the @@ -58,9 +54,11 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter iMachineToImage; @Inject - public VirtualBoxComputeServiceAdapter(VirtualBoxManager manager, JustProvider justProvider) { + public VirtualBoxComputeServiceAdapter(VirtualBoxManager manager, JustProvider justProvider, Function iMachineToImage) { + this.iMachineToImage = iMachineToImage; this.manager = checkNotNull(manager, "manager"); this.justProvider = checkNotNull(justProvider, "justProvider"); } @@ -81,12 +79,17 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter listImages() { - InputStream is = getClass().getResourceAsStream("/testImages.yaml"); - ImageFromYamlStream parser = new ImageFromYamlStream(); - return parser.apply(is).asMap().values(); - } + @Override + public Iterable listImages() { + final Predicate imagePredicate = new Predicate() { + @Override + public boolean apply(@Nullable IMachine iMachine) { + return iMachine.getName().startsWith(VIRTUALBOX_IMAGE_PREFIX); + } + }; + final Iterable imageMachines = filter(manager.getVBox().getMachines(), imagePredicate); + return transform(imageMachines, iMachineToImage); + } @SuppressWarnings("unchecked") @Override diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java index 0bc7c65c8b..fc7c1e76f1 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/config/VirtualBoxComputeServiceContextModule.java @@ -42,6 +42,7 @@ import org.jclouds.location.Provider; import org.jclouds.location.suppliers.OnlyLocationOrFirstZone; import org.jclouds.virtualbox.compute.VirtualBoxComputeServiceAdapter; import org.jclouds.virtualbox.functions.IMachineToHardware; +import org.jclouds.virtualbox.functions.IMachineToImage; import org.jclouds.virtualbox.functions.IMachineToNodeMetadata; import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.MachineState; @@ -84,14 +85,12 @@ public class VirtualBoxComputeServiceContextModule extends ComputeServiceAdapter }).to(IMachineToNodeMetadata.class); bind(new TypeLiteral>() { }).to((Class) IdentityFunction.class); - bind(new TypeLiteral>() { - }).to(IMachineToHardware.class); bind(new TypeLiteral>() { }).to((Class) IdentityFunction.class); - /* + bind(new TypeLiteral>() { + }).to(IMachineToHardware.class); bind(new TypeLiteral>() { }).to(IMachineToImage.class); - */ bind(new TypeLiteral>() { }).to(OnlyLocationOrFirstZone.class); } diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterLiveTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterLiveTest.java index 1054a8254f..d40a899382 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterLiveTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterLiveTest.java @@ -19,16 +19,20 @@ package org.jclouds.virtualbox.compute; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; - -import java.net.URI; -import java.util.Map; - +import com.google.common.base.Function; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.common.collect.Maps; +import com.google.inject.Guice; +import org.jclouds.compute.config.BaseComputeServiceContextModule; import org.jclouds.compute.domain.ExecResponse; import org.jclouds.compute.domain.Image; +import org.jclouds.compute.domain.OsFamily; import org.jclouds.compute.domain.Template; +import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.domain.Credentials; +import org.jclouds.json.Json; +import org.jclouds.json.config.GsonModule; import org.jclouds.location.suppliers.JustProvider; import org.jclouds.net.IPSocket; import org.jclouds.ssh.SshClient; @@ -40,21 +44,28 @@ import org.testng.annotations.Test; import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.VirtualBoxManager; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Iterables; -import com.google.common.collect.Maps; +import java.net.URI; +import java.util.Map; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; @Test(groups = "live", singleThreaded = true, testName = "VirtualBoxComputeServiceAdapterLiveTest") public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClientLiveTest { private VirtualBoxComputeServiceAdapter adapter; private IMachine machine; + private final Map> osVersionMap = new BaseComputeServiceContextModule() { + }.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule()) + .getInstance(Json.class)); - @BeforeGroups(groups = { "live" }) + @BeforeGroups(groups = {"live"}) public void setupClient() { super.setupClient(); - adapter = new VirtualBoxComputeServiceAdapter(getManager(), - new JustProvider(ImmutableSet. of(), provider, URI.create(endpoint))); + final VirtualBoxManager manager = getManager(); + Function iMachineToImage = new IMachineToImage(manager, osVersionMap); + adapter = new VirtualBoxComputeServiceAdapter(manager, + new JustProvider(ImmutableSet.of(), provider, URI.create(endpoint)), iMachineToImage); } protected VirtualBoxManager getManager() { @@ -78,7 +89,7 @@ public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClien // check other things, like cpu correct, mem correct, image/os is correct // (as possible) assert credentialStore.containsKey("node#" + machine.getId()) : "credentials to log into machine not found " - + machine; + + machine; // TODO: what's the IP address? // assert InetAddresses.isInetAddress(machine.getPrimaryBackendIpAddress()) : machine; doConnectViaSsh(machine, credentialStore.get("node#" + machine.getId())); @@ -86,7 +97,7 @@ public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClien protected void doConnectViaSsh(IMachine machine, Credentials creds) { SshClient ssh = context.utils().sshFactory() - .create(new IPSocket("//TODO", 22), creds); + .create(new IPSocket("//TODO", 22), creds); try { ssh.connect(); ExecResponse hello = ssh.exec("echo hello"); @@ -106,6 +117,7 @@ public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClien assertFalse(Iterables.isEmpty(profiles)); // check state; } + @Test public void testListImages() { Iterable iMageIterable = adapter.listImages(); diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterTest.java new file mode 100644 index 0000000000..d82f356f08 --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterTest.java @@ -0,0 +1,95 @@ +/* + * * + * * Licensed to jclouds, Inc. (jclouds) under one or more + * * contributor license agreements. See the NOTICE file + * * distributed with this work for additional information + * * regarding copyright ownership. jclouds licenses this file + * * to you under the Apache License, Version 2.0 (the + * * "License"); you may not use this file except in compliance + * * with the License. You may obtain a copy of the License at + * * + * * http://www.apache.org/licenses/LICENSE-2.0 + * * + * * Unless required by applicable law or agreed to in writing, + * * software distributed under the License is distributed on an + * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * * KIND, either express or implied. See the License for the + * * specific language governing permissions and limitations + * * under the License. + * + */ + +package org.jclouds.virtualbox.compute; + +import com.google.common.base.Function; +import com.google.common.collect.Iterators; +import com.google.inject.Guice; +import org.easymock.EasyMock; +import org.jclouds.compute.config.BaseComputeServiceContextModule; +import org.jclouds.compute.domain.Image; +import org.jclouds.compute.domain.OsFamily; +import org.jclouds.compute.reference.ComputeServiceConstants; +import org.jclouds.json.Json; +import org.jclouds.json.config.GsonModule; +import org.jclouds.location.suppliers.JustProvider; +import org.jclouds.virtualbox.functions.IMachineToImage; +import org.testng.annotations.Test; +import org.virtualbox_4_1.IGuestOSType; +import org.virtualbox_4_1.IMachine; +import org.virtualbox_4_1.IVirtualBox; +import org.virtualbox_4_1.VirtualBoxManager; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import static org.easymock.classextension.EasyMock.expect; +import static org.easymock.classextension.EasyMock.createNiceMock; +import static org.easymock.classextension.EasyMock.replay; +import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX; +import static org.testng.Assert.assertEquals; + +@Test(groups = "unit") +public class VirtualBoxComputeServiceAdapterTest { + + Map> osMap = new BaseComputeServiceContextModule() { + }.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule()) + .getInstance(Json.class)); + + @Test + public void testListImages() throws Exception { + + VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class); + JustProvider justProvider = createNiceMock(JustProvider.class); + IVirtualBox vBox = createNiceMock(IVirtualBox.class); + IGuestOSType osType = createNiceMock(IGuestOSType.class); + + List machines = new ArrayList(); + IMachine imageMachine = createNiceMock(IMachine.class); + IMachine clonedMachine = createNiceMock(IMachine.class); + machines.add(imageMachine); + machines.add(clonedMachine); + + expect(clonedMachine.getName()).andReturn("My Linux Node"); + expect(clonedMachine.getDescription()).andReturn("My Linux Node"); + expect(imageMachine.getName()).andReturn(VIRTUALBOX_IMAGE_PREFIX + "ubuntu-10.04"); + expect(imageMachine.getDescription()).andReturn(VIRTUALBOX_IMAGE_PREFIX + "ubuntu-10.04"); + + expect(manager.getVBox()).andReturn(vBox).anyTimes(); + expect(vBox.getMachines()).andReturn(machines).anyTimes(); + expect(vBox.getGuestOSType(EasyMock.anyObject())).andReturn(osType).anyTimes(); + expect(osType.getDescription()).andReturn("Ubuntu 10.04").anyTimes(); + expect(osType.getIs64Bit()).andReturn(true).anyTimes(); + + replay(manager, justProvider, vBox, clonedMachine, imageMachine, osType); + + Function iMachineToImage = new IMachineToImage(manager, osMap); + VirtualBoxComputeServiceAdapter adapter = new VirtualBoxComputeServiceAdapter(manager, justProvider, iMachineToImage); + + Iterator iterator = adapter.listImages().iterator(); + Image image = Iterators.getOnlyElement(iterator); + assertEquals(image.getDescription(), VIRTUALBOX_IMAGE_PREFIX + "ubuntu-10.04"); + + } +}