diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToHardware.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToHardware.java index 09e592ac4c..fe271a501c 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToHardware.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToHardware.java @@ -25,7 +25,6 @@ import com.google.common.base.Function; import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.HardwareBuilder; 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; @@ -34,22 +33,22 @@ import javax.inject.Inject; public class IMachineToHardware implements Function { - private VirtualBox vbox; + private VirtualBoxManager virtualBoxManager; @Inject - public IMachineToHardware(VirtualBox vbox) { - this.vbox = vbox; + public IMachineToHardware(VirtualBoxManager virtualBoxManager) { + this.virtualBoxManager = virtualBoxManager; } @Override public Hardware apply(@Nullable IMachine vm) { String osTypeId = vm.getOSTypeId(); - IGuestOSType guestOSType = vbox.manager().getVBox().getGuestOSType(osTypeId); + + IGuestOSType guestOSType = virtualBoxManager.getVBox().getGuestOSType(osTypeId); Boolean is64Bit = guestOSType.getIs64Bit(); HardwareBuilder hardwareBuilder = new HardwareBuilder(); hardwareBuilder.ids(vm.getId()); - vm.getSessionPid(); hardwareBuilder.is64Bit(is64Bit); return hardwareBuilder.build(); } diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToIpAddress.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToIpAddress.java index 179dd658c4..4114e6b0f7 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToIpAddress.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IMachineToIpAddress.java @@ -34,6 +34,11 @@ import javax.annotation.Nullable; import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot; +/** + * Get an IP address from an IMachine using arp of the host machine. + * + * @author Mattias Holmqvist, Andrea Turli + */ public class IMachineToIpAddress implements Function { private VirtualBoxManager manager; @@ -67,13 +72,13 @@ public class IMachineToIpAddress implements Function { simplifiedMacAddressOfClonedVM = new StringBuffer(simplifiedMacAddressOfClonedVM).delete(simplifiedMacAddressOfClonedVM.indexOf("0"), simplifiedMacAddressOfClonedVM.indexOf("0") + 1).toString(); } + // TODO: This is both shell-dependent and hard-coded. Needs to be fixed. ExecResponse execResponse = runScriptOnNode(hostId, "for i in {1..254} ; do ping -c 1 -t 1 192.168.2.$i & done", runAsRoot(false).wrapInInitScript(false)); System.out.println(execResponse); String arpLine = runScriptOnNode(hostId, "arp -an | grep " + simplifiedMacAddressOfClonedVM, runAsRoot(false).wrapInInitScript(false)).getOutput(); String ipAddress = arpLine.substring(arpLine.indexOf("(") + 1, arpLine.indexOf(")")); System.out.println("IP address " + ipAddress); - return ipAddress; } @@ -84,8 +89,7 @@ public class IMachineToIpAddress implements Function { protected boolean isOSX(IMachine machine) { String osTypeId = machine.getOSTypeId(); IGuestOSType guestOSType = manager.getVBox().getGuestOSType(osTypeId); - String familyDescription = guestOSType.getFamilyDescription(); - return true; + return guestOSType.getFamilyDescription().equals("Other"); } } diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToHardwareTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToHardwareTest.java new file mode 100644 index 0000000000..d3f86ea718 --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToHardwareTest.java @@ -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); + } + + +} 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 new file mode 100644 index 0000000000..cf0484f034 --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToImageTest.java @@ -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 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); + } + + +} diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadataTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadataTest.java index 1786292150..7318d33c05 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadataTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/IMachineToNodeMetadataTest.java @@ -47,7 +47,7 @@ public class IMachineToNodeMetadataTest { VirtualBox virtualBox = new VirtualBox(); IMachineToNodeMetadata parser = new IMachineToNodeMetadata(); - IMachineToHardware hwParser = new IMachineToHardware(virtualBox); + IMachineToHardware hwParser = new IMachineToHardware(manager); // hwParser.apply()