From 9112a74f6a19c774a2bf6fddb92e40d90457eff8 Mon Sep 17 00:00:00 2001 From: Mattias Holmqvist Date: Tue, 27 Sep 2011 23:05:36 +0200 Subject: [PATCH] Relaxed version parsing in IMachineToImage and started on the VirtualBoxComputeServiceAdapterLiveTest --- .../VirtualBoxComputeServiceAdapter.java | 2 +- .../virtualbox/functions/IMachineToImage.java | 126 +++++++++--------- ...rtualBoxComputeServiceAdapterLiveTest.java | 59 ++++++++ .../virtualbox/experiment/KickstartTest2.java | 2 +- .../functions/IMachineToImageTest.java | 39 +++--- 5 files changed, 140 insertions(+), 88 deletions(-) create mode 100644 sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterLiveTest.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 65527223be..5f4748a204 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 @@ -68,7 +68,7 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter listImages() { - return Collections.emptyList(); + return manager.getVBox().getMachines(); } @Override diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToImage.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToImage.java index a8d7be3fba..b0d4d3f5f4 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToImage.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToImage.java @@ -21,89 +21,87 @@ package org.jclouds.virtualbox.functions; -import java.util.NoSuchElementException; - -import javax.inject.Inject; - +import com.google.common.base.Function; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.ImageBuilder; import org.jclouds.compute.domain.OperatingSystem; import org.jclouds.compute.domain.OsFamily; import org.jclouds.javax.annotation.Nullable; -import org.jclouds.virtualbox.VirtualBox; import org.virtualbox_4_1.IGuestOSType; import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.VirtualBoxManager; -import com.google.common.base.Function; +import javax.inject.Inject; public class IMachineToImage implements Function { - private static final String UBUNTU = "Ubuntu Linux"; + private static final String UBUNTU = "Ubuntu"; - private VirtualBoxManager virtualboxManager; + private VirtualBoxManager virtualboxManager; - @Inject - public IMachineToImage(VirtualBoxManager virtualboxManager) { - this.virtualboxManager = virtualboxManager; - } + @Inject + public IMachineToImage(VirtualBoxManager virtualboxManager) { + this.virtualboxManager = virtualboxManager; + } - @Override - public Image apply(@Nullable IMachine from) { + @Override + public Image apply(@Nullable IMachine from) { - Boolean is64Bit = virtualboxManager.getVBox().getGuestOSType(from.getOSTypeId()).getIs64Bit(); + IGuestOSType guestOSType = virtualboxManager.getVBox().getGuestOSType(from.getOSTypeId()); - //Somehow this method gets called with the correct product item. - OsFamily family = osFamily().apply(from); - OperatingSystem os = OperatingSystem.builder() - .description(from.getDescription()) - .family(family) - .version(osVersion().apply(from)) - .is64Bit(is64Bit) - .build(); + //Somehow this method gets called with the correct product item. + OsFamily family = osFamily().apply(guestOSType.getDescription()); + OperatingSystem os = OperatingSystem.builder() + .description(guestOSType.getDescription()) + .family(family) + .version(osVersion().apply(guestOSType.getDescription())) + .is64Bit(guestOSType.getIs64Bit()) + .build(); - return new ImageBuilder() - .id("" + from.getId()) - .description(from.getDescription()) - .operatingSystem(os) - .build(); - } + return new ImageBuilder() + .id("" + from.getId()) + .description(from.getDescription()) + .operatingSystem(os) + .build(); + } - /** - * Parses the item description to determine the OSFamily - * @return the @see OsFamily or OsFamily.UNRECOGNIZED - */ - public static Function osFamily() { - return new Function() { - @Override - public OsFamily apply(IMachine iMachine) { - final String description = iMachine.getDescription(); - if ( description.startsWith(UBUNTU) ) return OsFamily.UBUNTU; - return OsFamily.UNRECOGNIZED; - } - }; - } + /** + * Parses the item description to determine the OSFamily + * + * @return the @see OsFamily or OsFamily.UNRECOGNIZED + */ + public static Function osFamily() { - /** - * Parses the item description to determine the os version - * @return the version - * @throws java.util.NoSuchElementException if the version cannot be determined - */ - public static Function osVersion() { - return new Function() { - @Override - public String apply(IMachine iMachine) { - final String description = iMachine.getDescription(); - OsFamily family = osFamily().apply(iMachine); - if(family.equals(OsFamily.UBUNTU)) return parseVersion(description, UBUNTU); - else throw new NoSuchElementException("No os parseVersion for item:" + iMachine); - } - }; - } - - private static String parseVersion(String description, String os) { - String noOsName = description.replaceFirst(os,"").trim(); - return noOsName.split(" ")[0]; - } + return new Function() { + @Override + public OsFamily apply(String osDescription) { + if (osDescription.startsWith(UBUNTU)) return OsFamily.UBUNTU; + return OsFamily.UNRECOGNIZED; + } + }; + } + + /** + * Parses the item description to determine the os version + * + * @return the version, empty if not found + */ + public static Function osVersion() { + return new Function() { + @Override + public String apply(String osDescription) { + OsFamily family = osFamily().apply(osDescription); + if (family.equals(OsFamily.UBUNTU)) + return parseVersion(osDescription, UBUNTU); + else + return ""; + } + }; + } + + private static String parseVersion(String description, String os) { + String noOsName = description.replaceFirst(os, "").trim(); + return noOsName.split(" ")[0]; + } } 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 new file mode 100644 index 0000000000..9b06911288 --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/compute/VirtualBoxComputeServiceAdapterLiveTest.java @@ -0,0 +1,59 @@ +/* + * * + * * 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 org.jclouds.compute.domain.Image; +import org.jclouds.virtualbox.functions.IMachineToImage; +import org.testng.annotations.Test; +import org.virtualbox_4_1.IMachine; +import org.virtualbox_4_1.VirtualBoxManager; + +import java.net.URI; + +/** + * Tests basic functionality of the VirtualBoxComputeServiceAdapter. + *

+ * Note that you need vboxwebsrv running for these tests to work. + */ +@Test(groups = "live") +public class VirtualBoxComputeServiceAdapterLiveTest { + + + @Test + public void testGetImages() throws Exception { + + VirtualBoxManager virtualBoxManager = VirtualBoxManager.createInstance(""); + + URI endpoint = new URI("http://localhost:18083"); + virtualBoxManager.connect(endpoint.toASCIIString(), "admin", "123456"); + + VirtualBoxComputeServiceAdapter adapter = new VirtualBoxComputeServiceAdapter(virtualBoxManager); + IMachineToImage iMachineToImage = new IMachineToImage(virtualBoxManager); + Iterable iMachineIterable = adapter.listImages(); + + for (IMachine iMachine : iMachineIterable) { + Image image = iMachineToImage.apply(iMachine); + System.out.println(image); + } + } +} \ No newline at end of file diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/experiment/KickstartTest2.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/experiment/KickstartTest2.java index 08bbe9d7bd..602e530afa 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/experiment/KickstartTest2.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/experiment/KickstartTest2.java @@ -174,7 +174,7 @@ public class KickstartTest2 { } private void runAll() throws Exception { - context = TestUtils.computeServiceForLocalhost(); + context = TestUtils.computeServiceForLocalhostAndGuest(); socketTester = new RetryablePredicate( new InetSocketAddressConnect(), 130, 10, TimeUnit.SECONDS); diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToImageTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToImageTest.java index cf0484f034..e96d305aa1 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToImageTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToImageTest.java @@ -31,8 +31,6 @@ import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IVirtualBox; import org.virtualbox_4_1.VirtualBoxManager; -import java.util.NoSuchElementException; - import static org.easymock.EasyMock.eq; import static org.easymock.EasyMock.expect; import static org.easymock.classextension.EasyMock.createNiceMock; @@ -50,13 +48,13 @@ public class IMachineToImageTest { IVirtualBox vBox= createNiceMock(IVirtualBox.class); IMachine vm = createNiceMock(IMachine.class); IGuestOSType guestOsType = createNiceMock(IGuestOSType.class); - + String linuxDescription = "Ubuntu 10.04"; expect(vbm.getVBox()).andReturn(vBox).anyTimes(); - String linuxDescription = "Ubuntu Linux 10.04"; expect(vm.getOSTypeId()).andReturn("os-type").anyTimes(); - expect(vm.getDescription()).andReturn(linuxDescription).anyTimes(); expect(vBox.getGuestOSType(eq("os-type"))).andReturn(guestOsType); + expect(vm.getDescription()).andReturn("my-ubuntu-machine").anyTimes(); + expect(guestOsType.getDescription()).andReturn(linuxDescription).anyTimes(); expect(guestOsType.getIs64Bit()).andReturn(true); replay(vbm, vBox, vm, guestOsType); @@ -65,7 +63,7 @@ public class IMachineToImageTest { Image image = fn.apply(vm); - assertEquals(image.getDescription(), linuxDescription); + assertEquals(image.getDescription(), "my-ubuntu-machine"); assertEquals(image.getOperatingSystem().getDescription(), linuxDescription); assertTrue(image.getOperatingSystem().is64Bit()); assertEquals(image.getOperatingSystem().getFamily(), OsFamily.UBUNTU); @@ -75,22 +73,15 @@ public class IMachineToImageTest { @Test public void testOsVersion() throws Exception { - VirtualBoxManager vbm = createNiceMock(VirtualBoxManager.class); - IVirtualBox vBox= createNiceMock(IVirtualBox.class); - IMachine vm = createNiceMock(IMachine.class); - expect(vbm.getVBox()).andReturn(vBox).anyTimes(); + String osDescription = "Ubuntu 10.04"; - expect(vm.getDescription()).andReturn("Ubuntu Linux 10.04").anyTimes(); - - replay(vm); - - Function iMachineStringFunction = IMachineToImage.osVersion(); - assertEquals("10.04", iMachineStringFunction.apply(vm)); + Function iMachineStringFunction = IMachineToImage.osVersion(); + assertEquals("10.04", iMachineStringFunction.apply(osDescription)); } - @Test(expectedExceptions = NoSuchElementException.class) + @Test public void testUnparseableOsString() throws Exception { VirtualBoxManager vbm = createNiceMock(VirtualBoxManager.class); @@ -100,16 +91,20 @@ public class IMachineToImageTest { expect(vbm.getVBox()).andReturn(vBox).anyTimes(); - String linuxDescription = "SomeOtherOs 2.04"; + String unknownOsDescription = "SomeOtherOs 2.04"; expect(vm.getOSTypeId()).andReturn("os-type").anyTimes(); - expect(vm.getDescription()).andReturn(linuxDescription).anyTimes(); - - expect(vBox.getGuestOSType(eq("os-type"))).andReturn(guestOsType); + expect(vm.getDescription()).andReturn("my-unknown-machine").anyTimes(); + expect(guestOsType.getDescription()).andReturn(unknownOsDescription).anyTimes(); expect(guestOsType.getIs64Bit()).andReturn(true); + expect(vBox.getGuestOSType(eq("os-type"))).andReturn(guestOsType); replay(vbm, vBox, vm, guestOsType); - new IMachineToImage(vbm).apply(vm); + Image image = new IMachineToImage(vbm).apply(vm); + + assertEquals(image.getOperatingSystem().getDescription(), "SomeOtherOs 2.04"); + assertEquals(image.getOperatingSystem().getVersion(), ""); + }