From 394bb1b6025e89572cd210176066c4f2b05c5e50 Mon Sep 17 00:00:00 2001 From: Mattias Holmqvist Date: Sun, 23 Oct 2011 23:59:54 +0200 Subject: [PATCH] Added function ApplyMemoryToMachine for virtualbox. commit e52df20b9010ebac266e2b593628ea1876a578f9 Author: Mattias Holmqvist Date: Sun Oct 23 23:59:25 2011 +0200 Added function ApplyMemoryToMachine for virtualbox. commit a43e189b306fddbdc5ee5930355f20d90d148854 Merge: 3f6de26 041360e Author: Mattias Holmqvist Date: Sun Oct 23 23:26:42 2011 +0200 Merge branch 'master' of https://github.com/jclouds/jclouds into functionalize Conflicts: sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/AttachNATRedirectRuleToMachine.java commit 3f6de265d7342676fdeab99badd31f31cb43aadc Author: Mattias Holmqvist Date: Sun Oct 23 23:23:49 2011 +0200 Added AttachNATRedirectRuleToMachine to virtualbox. --- .../functions/ApplyMemoryToMachine.java | 44 ++++++++++++ .../virtualbox/functions/IsoToIMachine.java | 28 ++++---- .../functions/ApplyMemoryToMachineTest.java | 68 +++++++++++++++++++ 3 files changed, 125 insertions(+), 15 deletions(-) create mode 100644 sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/ApplyMemoryToMachine.java create mode 100644 sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/ApplyMemoryToMachineTest.java diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/ApplyMemoryToMachine.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/ApplyMemoryToMachine.java new file mode 100644 index 0000000000..dc60249a94 --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/ApplyMemoryToMachine.java @@ -0,0 +1,44 @@ +/* + * 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.virtualbox_4_1.IMachine; + +import javax.annotation.Nullable; + +/** + * @author Mattias Holmqvist + */ +public class ApplyMemoryToMachine implements Function { + + private long memorySize; + + public ApplyMemoryToMachine(long memorySize) { + this.memorySize = memorySize; + } + + @Override + public Object apply(@Nullable IMachine machine) { + machine.setMemorySize(memorySize); + machine.saveSettings(); + return null; + } +} diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java index 9dfe060bcb..e5d33fc08d 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/IsoToIMachine.java @@ -105,16 +105,7 @@ public class IsoToIMachine implements Function { String workingDir = System.getProperty(VIRTUALBOX_WORKINGDIR, defaultWorkingDir); // Change RAM - lockMachineAndApply(manager, Write, vmName, new Function() { - - @Override - public Void apply(IMachine machine) { - machine.setMemorySize(1024l); - machine.saveSettings(); - return null; - } - - }); + ensureMachineHasMemory(vmName, 1024l); // IDE Controller ensureMachineHasIDEControllerNamed(vmName, controllerIDE); @@ -128,12 +119,8 @@ public class IsoToIMachine implements Function { new File(adminDiskPath).delete(); } - // Create hard disk - IMedium hardDisk = new CreateMediumIfNotAlreadyExists(manager, diskFormat, true).apply(adminDiskPath); - // Attach hard disk to machine - lockMachineAndApply(manager, Write, vmName, - new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager)); + ensureMachineHasHardDiskAttached(vmName, adminDiskPath); // NAT ensureNATNetworkingIsAppliedToMachine(vmName); @@ -197,6 +184,17 @@ public class IsoToIMachine implements Function { return vm; } + private void ensureMachineHasHardDiskAttached(String vmName, String path) { + // Create hard disk + IMedium hardDisk = new CreateMediumIfNotAlreadyExists(manager, diskFormat, true).apply(path); + lockMachineAndApply(manager, Write, vmName, + new AttachHardDiskToMachineIfNotAlreadyAttached(controllerIDE, hardDisk, manager)); + } + + private void ensureMachineHasMemory(String vmName, final long memorySize) { + lockMachineAndApply(manager, Write, vmName, new ApplyMemoryToMachine(memorySize)); + } + private void ensureNATNetworkingIsAppliedToMachine(String vmName) { lockMachineAndApply(manager, Write, vmName, new AttachNATRedirectRuleToMachine(0l)); } diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/ApplyMemoryToMachineTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/ApplyMemoryToMachineTest.java new file mode 100644 index 0000000000..fee8274493 --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/ApplyMemoryToMachineTest.java @@ -0,0 +1,68 @@ +/* + * 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.testng.annotations.Test; +import org.virtualbox_4_1.IMachine; +import org.virtualbox_4_1.VBoxException; + +import static org.easymock.classextension.EasyMock.*; + +/** + * @author Mattias Holmqvist + */ +@Test(groups = "unit", testName = "ApplyMemoryToMachineTest") +public class ApplyMemoryToMachineTest { + + @Test + public void testSetRAMSizeSuccessful() throws Exception { + long memorySize = 1024l; + IMachine machine = createMock(IMachine.class); + + machine.setMemorySize(memorySize); + machine.saveSettings(); + + replay(machine); + + new ApplyMemoryToMachine(memorySize).apply(machine); + + verify(machine); + } + + @Test(expectedExceptions = VBoxException.class) + public void testRethrowInvalidRamSizeError() throws Exception { + // Mainly here for documentation purposes + final String error = "VirtualBox error: Invalid RAM size: " + + "3567587327 MB (must be in range [4, 2097152] MB) (0x80070057)"; + + long memorySize = 1024l; + IMachine machine = createMock(IMachine.class); + + VBoxException invalidRamSizeException = new VBoxException(createNiceMock(Throwable.class), error); + machine.setMemorySize(memorySize); + expectLastCall().andThrow(invalidRamSizeException); + machine.saveSettings(); + + replay(machine); + + new ApplyMemoryToMachine(memorySize).apply(machine); + } + +}