Relaxed version parsing in IMachineToImage and started on the VirtualBoxComputeServiceAdapterLiveTest

This commit is contained in:
Mattias Holmqvist 2011-09-27 23:05:36 +02:00
parent 215b7c09a3
commit 9112a74f6a
5 changed files with 140 additions and 88 deletions

View File

@ -68,7 +68,7 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
@Override
public Iterable<IMachine> listImages() {
return Collections.emptyList();
return manager.getVBox().getMachines();
}
@Override

View File

@ -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<IMachine, Image> {
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<IMachine, OsFamily> osFamily() {
return new Function<IMachine,OsFamily>() {
@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<String, OsFamily> 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<IMachine, String> osVersion() {
return new Function<IMachine, String>() {
@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);
}
};
}
return new Function<String, OsFamily>() {
@Override
public OsFamily apply(String osDescription) {
if (osDescription.startsWith(UBUNTU)) return OsFamily.UBUNTU;
return OsFamily.UNRECOGNIZED;
}
};
}
private static String parseVersion(String description, String os) {
String noOsName = description.replaceFirst(os,"").trim();
return noOsName.split(" ")[0];
}
/**
* Parses the item description to determine the os version
*
* @return the version, empty if not found
*/
public static Function<String, String> osVersion() {
return new Function<String, String>() {
@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];
}
}

View File

@ -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.
* <p/>
* 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<IMachine> iMachineIterable = adapter.listImages();
for (IMachine iMachine : iMachineIterable) {
Image image = iMachineToImage.apply(iMachine);
System.out.println(image);
}
}
}

View File

@ -174,7 +174,7 @@ public class KickstartTest2 {
}
private void runAll() throws Exception {
context = TestUtils.computeServiceForLocalhost();
context = TestUtils.computeServiceForLocalhostAndGuest();
socketTester = new RetryablePredicate<IPSocket>(
new InetSocketAddressConnect(), 130, 10, TimeUnit.SECONDS);

View File

@ -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<IMachine, String> iMachineStringFunction = IMachineToImage.osVersion();
assertEquals("10.04", iMachineStringFunction.apply(vm));
Function<String, String> 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(), "");
}