Starting on unit tests for functions in virtualbox.

This commit is contained in:
Mattias Holmqvist 2011-09-27 20:50:30 +02:00
parent 22fa34fdb5
commit 91d42c4d4a
4 changed files with 189 additions and 7 deletions

View File

@ -25,7 +25,6 @@ import com.google.common.base.Function;
import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.HardwareBuilder; import org.jclouds.compute.domain.HardwareBuilder;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
import org.jclouds.virtualbox.VirtualBox;
import org.virtualbox_4_1.IGuestOSType; import org.virtualbox_4_1.IGuestOSType;
import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.VirtualBoxManager; import org.virtualbox_4_1.VirtualBoxManager;
@ -34,22 +33,22 @@ import javax.inject.Inject;
public class IMachineToHardware implements Function<IMachine, Hardware> { public class IMachineToHardware implements Function<IMachine, Hardware> {
private VirtualBox vbox; private VirtualBoxManager virtualBoxManager;
@Inject @Inject
public IMachineToHardware(VirtualBox vbox) { public IMachineToHardware(VirtualBoxManager virtualBoxManager) {
this.vbox = vbox; this.virtualBoxManager = virtualBoxManager;
} }
@Override @Override
public Hardware apply(@Nullable IMachine vm) { public Hardware apply(@Nullable IMachine vm) {
String osTypeId = vm.getOSTypeId(); String osTypeId = vm.getOSTypeId();
IGuestOSType guestOSType = vbox.manager().getVBox().getGuestOSType(osTypeId);
IGuestOSType guestOSType = virtualBoxManager.getVBox().getGuestOSType(osTypeId);
Boolean is64Bit = guestOSType.getIs64Bit(); Boolean is64Bit = guestOSType.getIs64Bit();
HardwareBuilder hardwareBuilder = new HardwareBuilder(); HardwareBuilder hardwareBuilder = new HardwareBuilder();
hardwareBuilder.ids(vm.getId()); hardwareBuilder.ids(vm.getId());
vm.getSessionPid();
hardwareBuilder.is64Bit(is64Bit); hardwareBuilder.is64Bit(is64Bit);
return hardwareBuilder.build(); return hardwareBuilder.build();
} }

View File

@ -0,0 +1,67 @@
/*
* *
* * 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.functions;
import org.jclouds.compute.domain.Hardware;
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;
import static org.easymock.classextension.EasyMock.replay;
import static org.testng.Assert.assertEquals;
@Test(groups = "unit")
public class IMachineToHardwareTest {
@Test
public void testConvert() throws Exception {
VirtualBoxManager vbm = createNiceMock(VirtualBoxManager.class);
IVirtualBox vBox = createNiceMock(IVirtualBox.class);
IMachine vm = createNiceMock(IMachine.class);
IGuestOSType guestOsType = createNiceMock(IGuestOSType.class);
String linuxDescription = "Ubuntu Linux 10.04";
String machineId = "hw-machineId";
expect(vm.getOSTypeId()).andReturn("os-type").anyTimes();
expect(vm.getDescription()).andReturn(linuxDescription).anyTimes();
expect(vBox.getGuestOSType(eq("os-type"))).andReturn(guestOsType);
expect(vbm.getVBox()).andReturn(vBox);
expect(guestOsType.getIs64Bit()).andReturn(true);
expect(vm.getId()).andReturn(machineId);
replay(vbm, vBox, vm, guestOsType);
Hardware hardware = new IMachineToHardware(vbm).apply(vm);
assertEquals(hardware.getId(), machineId);
assertEquals(hardware.getProviderId(), machineId);
}
}

View File

@ -0,0 +1,116 @@
/*
* *
* * 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.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 java.util.NoSuchElementException;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createNiceMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.testng.Assert.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
@Test(groups = "unit")
public class IMachineToImageTest {
@Test
public void testConvert() throws Exception {
VirtualBoxManager vbm = createNiceMock(VirtualBoxManager.class);
IVirtualBox vBox= createNiceMock(IVirtualBox.class);
IMachine vm = createNiceMock(IMachine.class);
IGuestOSType guestOsType = createNiceMock(IGuestOSType.class);
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(guestOsType.getIs64Bit()).andReturn(true);
replay(vbm, vBox, vm, guestOsType);
IMachineToImage fn = new IMachineToImage(vbm);
Image image = fn.apply(vm);
assertEquals(image.getDescription(), linuxDescription);
assertEquals(image.getOperatingSystem().getDescription(), linuxDescription);
assertTrue(image.getOperatingSystem().is64Bit());
assertEquals(image.getOperatingSystem().getFamily(), OsFamily.UBUNTU);
assertEquals(image.getOperatingSystem().getVersion(), "10.04");
}
@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();
expect(vm.getDescription()).andReturn("Ubuntu Linux 10.04").anyTimes();
replay(vm);
Function<IMachine, String> iMachineStringFunction = IMachineToImage.osVersion();
assertEquals("10.04", iMachineStringFunction.apply(vm));
}
@Test(expectedExceptions = NoSuchElementException.class)
public void testUnparseableOsString() throws Exception {
VirtualBoxManager vbm = createNiceMock(VirtualBoxManager.class);
IVirtualBox vBox= createNiceMock(IVirtualBox.class);
IMachine vm = createNiceMock(IMachine.class);
IGuestOSType guestOsType = createNiceMock(IGuestOSType.class);
expect(vbm.getVBox()).andReturn(vBox).anyTimes();
String linuxDescription = "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(guestOsType.getIs64Bit()).andReturn(true);
replay(vbm, vBox, vm, guestOsType);
new IMachineToImage(vbm).apply(vm);
}
}

View File

@ -47,7 +47,7 @@ public class IMachineToNodeMetadataTest {
VirtualBox virtualBox = new VirtualBox(); VirtualBox virtualBox = new VirtualBox();
IMachineToNodeMetadata parser = new IMachineToNodeMetadata(); IMachineToNodeMetadata parser = new IMachineToNodeMetadata();
IMachineToHardware hwParser = new IMachineToHardware(virtualBox); IMachineToHardware hwParser = new IMachineToHardware(manager);
// hwParser.apply() // hwParser.apply()