corrected os family parsing for virtual box

This commit is contained in:
Adrian Cole 2011-10-22 17:53:10 +02:00
parent 7d35ec47ee
commit 8194e84c30
8 changed files with 132 additions and 142 deletions

View File

@ -42,7 +42,6 @@ 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;
@ -99,7 +98,7 @@ public class VirtualBoxComputeServiceContextModule extends ComputeServiceAdapter
@Override
protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) {
return template.osFamily(OsFamily.UBUNTU).os64Bit(false).osVersionMatches("11.04-server");
return template.osFamily(OsFamily.UBUNTU).osVersionMatches("11.04");
}
@VisibleForTesting

View File

@ -23,7 +23,7 @@
package org.jclouds.virtualbox.domain;
import static org.jclouds.virtualbox.functions.IMachineToImage.osFamily;
import static org.jclouds.compute.util.ComputeServiceUtils.parseOsFamilyOrUnrecognized;
import java.util.List;
import java.util.Map;
@ -141,8 +141,8 @@ public class YamlImage {
public Image apply(YamlImage arg0) {
if (arg0 == null)
return null;
OsFamily family = parseOsFamilyOrUnrecognized(arg0.os_family);
OsFamily family = osFamily().apply(arg0.os_family);
OperatingSystem operatingSystem = OperatingSystem.builder()
.description(arg0.os_description)
.family(family)

View File

@ -21,7 +21,15 @@
package org.jclouds.virtualbox.functions;
import com.google.common.base.Function;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.compute.util.ComputeServiceUtils.parseOsFamilyOrUnrecognized;
import static org.jclouds.compute.util.ComputeServiceUtils.parseVersionOrReturnEmptyString;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.ImageBuilder;
import org.jclouds.compute.domain.OperatingSystem;
@ -31,77 +39,33 @@ import org.virtualbox_4_1.IGuestOSType;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.VirtualBoxManager;
import javax.inject.Inject;
import com.google.common.base.Function;
@Singleton
public class IMachineToImage implements Function<IMachine, Image> {
private static final String UBUNTU = "Ubuntu";
private VirtualBoxManager virtualboxManager;
private final VirtualBoxManager virtualboxManager;
private final Map<OsFamily, Map<String, String>> osVersionMap;
@Inject
public IMachineToImage(VirtualBoxManager virtualboxManager) {
this.virtualboxManager = virtualboxManager;
public IMachineToImage(VirtualBoxManager virtualboxManager, Map<OsFamily, Map<String, String>> osVersionMap) {
this.virtualboxManager = checkNotNull(virtualboxManager, "virtualboxManager");
this.osVersionMap = checkNotNull(osVersionMap, "osVersionMap");
}
@Override
public Image apply(@Nullable IMachine from) {
if (from == null)
return null;
IGuestOSType guestOSType = virtualboxManager.getVBox().getGuestOSType(from.getOSTypeId());
OsFamily family = parseOsFamilyOrUnrecognized(guestOSType.getDescription());
String version = parseVersionOrReturnEmptyString(family, guestOSType.getDescription(),
osVersionMap);
OperatingSystem os = OperatingSystem.builder().description(guestOSType.getDescription()).family(family)
.version(version).is64Bit(guestOSType.getIs64Bit()).build();
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<String, OsFamily> osFamily() {
return new Function<String, OsFamily>() {
@Override
public OsFamily apply(String osDescription) {
if (osDescription.startsWith("linux")) return OsFamily.LINUX;
if (osDescription.startsWith("Ubuntu")) return OsFamily.LINUX;
return OsFamily.UNRECOGNIZED;
}
};
}
/**
* 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.LINUX))
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

@ -21,7 +21,7 @@ import com.google.common.collect.ImmutableSet;
* @author Andrea Turli
*/
@Test(groups = "live")
@Test(groups = "live", testName = "VirtualBoxTemplateBuilderLiveTest")
public class VirtualBoxTemplateBuilderLiveTest extends BaseTemplateBuilderLiveTest {
public VirtualBoxTemplateBuilderLiveTest() {
@ -36,14 +36,7 @@ public class VirtualBoxTemplateBuilderLiveTest extends BaseTemplateBuilderLiveTe
public boolean apply(OsFamilyVersion64Bit input) {
switch (input.family) {
case UBUNTU:
return !(input.version.startsWith("11") || input.version.equals("8.04")) && input.is64Bit;
case DEBIAN:
return !(input.version.equals("6.0")) && input.is64Bit;
case CENTOS:
return !(input.version.matches("5.[023]") || input.version.equals("8.04")) && input.is64Bit;
case WINDOWS:
return input.version.equals("2008 SP2") || input.version.equals("")
|| (input.version.equals("2008 R2") && input.is64Bit);
return input.version.equals("11.04") && !input.is64Bit;
default:
return false;
}
@ -54,8 +47,9 @@ public class VirtualBoxTemplateBuilderLiveTest extends BaseTemplateBuilderLiveTe
@Test
public void testTemplateBuilder() {
System.out.println(this.context.getComputeService().listImages());
Template defaultTemplate = this.context.getComputeService().templateBuilder().build();
assertEquals(defaultTemplate.getImage().getOperatingSystem().is64Bit(), true);
assertEquals(defaultTemplate.getImage().getOperatingSystem().is64Bit(), false);
assertEquals(defaultTemplate.getImage().getOperatingSystem().getVersion(), "10.04");
assertEquals(defaultTemplate.getImage().getOperatingSystem().getFamily(), OsFamily.UBUNTU);
assertEquals(getCores(defaultTemplate.getHardware()), 1.0d);

View File

@ -21,16 +21,6 @@
package org.jclouds.virtualbox.functions;
import com.google.common.base.Function;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.OsFamily;
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 static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createNiceMock;
@ -38,14 +28,34 @@ import static org.easymock.classextension.EasyMock.replay;
import static org.testng.Assert.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
import java.util.Map;
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.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 com.google.inject.Guice;
@Test(groups = "unit")
public class IMachineToImageTest {
Map<OsFamily, Map<String, String>> map = new BaseComputeServiceContextModule() {
}.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule())
.getInstance(Json.class));
@Test
public void testConvert() throws Exception {
VirtualBoxManager vbm = createNiceMock(VirtualBoxManager.class);
IVirtualBox vBox= createNiceMock(IVirtualBox.class);
IVirtualBox vBox = createNiceMock(IVirtualBox.class);
IMachine vm = createNiceMock(IMachine.class);
IGuestOSType guestOsType = createNiceMock(IGuestOSType.class);
String linuxDescription = "Ubuntu 10.04";
@ -59,25 +69,46 @@ public class IMachineToImageTest {
replay(vbm, vBox, vm, guestOsType);
IMachineToImage fn = new IMachineToImage(vbm);
IMachineToImage fn = new IMachineToImage(vbm, map);
Image image = fn.apply(vm);
assertEquals(image.getDescription(), "my-ubuntu-machine");
assertEquals(image.getOperatingSystem().getDescription(), linuxDescription);
assertTrue(image.getOperatingSystem().is64Bit());
assertEquals(image.getOperatingSystem().getFamily(), OsFamily.LINUX);
assertEquals(image.getOperatingSystem().getFamily(), OsFamily.UBUNTU);
assertEquals(image.getOperatingSystem().getVersion(), "10.04");
}
@Test
public void testOsVersion() throws Exception {
public void testConvert1() throws Exception {
String osDescription = "Ubuntu 10.04";
VirtualBoxManager vbm = createNiceMock(VirtualBoxManager.class);
IVirtualBox vBox = createNiceMock(IVirtualBox.class);
IMachine vm = createNiceMock(IMachine.class);
IGuestOSType guestOsType = createNiceMock(IGuestOSType.class);
String guestOsDescription = "ubuntu 11.04 server (i386)";
String vmDescription = "ubuntu-11.04-server-i386";
expect(vbm.getVBox()).andReturn(vBox).anyTimes();
Function<String, String> iMachineStringFunction = IMachineToImage.osVersion();
assertEquals("10.04", iMachineStringFunction.apply(osDescription));
expect(vm.getOSTypeId()).andReturn("os-type").anyTimes();
expect(vBox.getGuestOSType(eq("os-type"))).andReturn(guestOsType);
expect(vm.getDescription()).andReturn(vmDescription).anyTimes();
expect(guestOsType.getDescription()).andReturn(guestOsDescription).anyTimes();
expect(guestOsType.getIs64Bit()).andReturn(true);
replay(vbm, vBox, vm, guestOsType);
IMachineToImage fn = new IMachineToImage(vbm, map);
Image image = fn.apply(vm);
assertEquals(image.getDescription(), vmDescription);
assertEquals(image.getOperatingSystem().getDescription(), guestOsDescription);
assertTrue(image.getOperatingSystem().is64Bit());
assertEquals(image.getOperatingSystem().getFamily(), OsFamily.UBUNTU);
assertEquals(image.getOperatingSystem().getVersion(), "11.04");
}
@ -85,7 +116,7 @@ public class IMachineToImageTest {
public void testUnparseableOsString() throws Exception {
VirtualBoxManager vbm = createNiceMock(VirtualBoxManager.class);
IVirtualBox vBox= createNiceMock(IVirtualBox.class);
IVirtualBox vBox = createNiceMock(IVirtualBox.class);
IMachine vm = createNiceMock(IMachine.class);
IGuestOSType guestOsType = createNiceMock(IGuestOSType.class);
@ -100,12 +131,11 @@ public class IMachineToImageTest {
replay(vbm, vBox, vm, guestOsType);
Image image = new IMachineToImage(vbm).apply(vm);
Image image = new IMachineToImage(vbm, map).apply(vm);
assertEquals(image.getOperatingSystem().getDescription(), "SomeOtherOs 2.04");
assertEquals(image.getOperatingSystem().getVersion(), "");
}
}

View File

@ -30,22 +30,32 @@ import static com.google.common.collect.Iterables.any;
import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest;
import static org.testng.Assert.assertTrue;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import java.util.Set;
import org.jclouds.compute.ComputeServiceContext;
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.domain.Credentials;
import org.jclouds.json.Json;
import org.jclouds.json.config.GsonModule;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.VirtualBoxManager;
import com.google.inject.Guice;
@Test(groups = "live", singleThreaded = true, testName = "IsoToIMachineLiveTest")
public class IsoToIMachineLiveTest extends BaseVirtualBoxClientLiveTest {
Map<OsFamily, Map<String, String>> map = new BaseComputeServiceContextModule() {
}.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule())
.getInstance(Json.class));
private String settingsFile = null;
private boolean forceOverwrite = true;
private String vmId = "jclouds-image-iso-1";
@ -83,7 +93,7 @@ public class IsoToIMachineLiveTest extends BaseVirtualBoxClientLiveTest {
guestId,
new Credentials("toor", "password")).apply("ubuntu-11.04-server-i386.iso");
IMachineToImage iMachineToImage = new IMachineToImage(manager);
IMachineToImage iMachineToImage = new IMachineToImage(manager, map);
Image newImage = iMachineToImage.apply(imageMachine);
//TODO add the description to the cache of the images or serialize to YAML the image desc
Set<? extends Image> images = context.getComputeService().listImages();

View File

@ -8,54 +8,47 @@ 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.virtualbox.BaseVirtualBoxClientLiveTest;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
/**
*
* 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.
*
*
* @author Andrea Turli
*/
*
* 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.
*
*
* @author Andrea Turli
*/
@Test(groups = "unit")
public class ImageFromYamlStreamTest {
public static final Image TEST1 = new ImageBuilder()
.id("myTestId")
.name("ubuntu-11.04-server-i386")
.description("ubuntu 11.04 server (i386)")
.operatingSystem(OperatingSystem.builder()
.description("ubuntu")
.family(OsFamily.LINUX)
.version("11.04")
.build())
.build();
@Test
public void testNodesParse() throws Exception {
InputStream is = getClass().getResourceAsStream("/testImages.yaml");
ImageFromYamlStream parser = new ImageFromYamlStream();
assertEquals(parser.apply(is).asMap(), ImmutableMap.of(TEST1.getId(), TEST1));
is.close();
}
public static final Image TEST1 = new ImageBuilder()
.id("myTestId")
.name("ubuntu-11.04-server-i386")
.description("ubuntu 11.04 server (i386)")
.operatingSystem(
OperatingSystem.builder().description("ubuntu").family(OsFamily.UBUNTU).version("11.04").build())
.build();
@Test
public void testNodesParse() throws Exception {
InputStream is = getClass().getResourceAsStream("/testImages.yaml");
ImageFromYamlStream parser = new ImageFromYamlStream();
assertEquals(parser.apply(is).asMap(), ImmutableMap.of(TEST1.getId(), TEST1));
is.close();
}
}

View File

@ -3,7 +3,7 @@ images:
name: ubuntu-11.04-server-i386
description: ubuntu 11.04 server (i386)
os_arch: x86
os_family: linux
os_family: ubuntu
os_description: ubuntu
os_version: 11.04
iso: http://releases.ubuntu.com/11.04/ubuntu-11.04-server-i386.iso