From 4214382b8dacf26f5d8026daabf5822fd60d03a3 Mon Sep 17 00:00:00 2001 From: andreaturli Date: Fri, 25 Nov 2011 22:41:20 +0000 Subject: [PATCH] issue 384: added DetachDistroMedium and RetrieveBridgedInterface + Test --- .gitignore | 1 + .../DetachDistroMediumToMachine.java | 58 ++++++++++ .../DetachDistroMediumToMachineTest.java | 104 ++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/DetachDistroMediumToMachine.java create mode 100644 sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/DetachDistroMediumToMachineTest.java diff --git a/.gitignore b/.gitignore index a695d7202d..e1c23c8b9a 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ bin/ *.eml *.ipr *.iws +*.DS_STORE TAGS diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/DetachDistroMediumToMachine.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/DetachDistroMediumToMachine.java new file mode 100644 index 0000000000..0d141c6106 --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/DetachDistroMediumToMachine.java @@ -0,0 +1,58 @@ +/** + * 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 javax.annotation.Nullable; + +import org.virtualbox_4_1.IMachine; +import org.virtualbox_4_1.VBoxException; + +import com.google.common.base.Function; + +/** + * @author Andrea Turli + */ +public class DetachDistroMediumToMachine implements Function { + + private final String controller; + + public DetachDistroMediumToMachine(String controller) { + this.controller = controller; + } + + @Override + public Void apply(@Nullable IMachine machine) { + try { + int controllerPort = 0; + int device = 0; + machine.detachDevice(controller, controllerPort, device); + machine.saveSettings(); + } catch (VBoxException e) { + if (!alreadyDetached(e)) + throw e; + } + return null; + } + + private boolean alreadyDetached(VBoxException e) { + return e.getMessage().indexOf("is already detached from port") != -1; + } + +} \ No newline at end of file diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/DetachDistroMediumToMachineTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/DetachDistroMediumToMachineTest.java new file mode 100644 index 0000000000..8bf0658981 --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/functions/DetachDistroMediumToMachineTest.java @@ -0,0 +1,104 @@ +/** + * 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 static org.easymock.EasyMock.expectLastCall; +import static org.easymock.classextension.EasyMock.createMock; +import static org.easymock.classextension.EasyMock.createNiceMock; +import static org.easymock.classextension.EasyMock.replay; +import static org.easymock.classextension.EasyMock.verify; + +import org.testng.annotations.Test; +import org.virtualbox_4_1.IMachine; +import org.virtualbox_4_1.VBoxException; + +/** + * @author Andrea Turli + */ +@Test(groups = "unit", testName = "DetachDistroMediumToMachineTest") +public class DetachDistroMediumToMachineTest { + + @Test + public void testDetachDistroMedium() throws Exception { + + String controller = "IDE Controller"; + IMachine machine = createMock(IMachine.class); + + machine.saveSettings(); + machine.detachDevice(controller, 0, 0); + + replay(machine); + + new DetachDistroMediumToMachine(controller).apply(machine); + + verify(machine); + + } + + @Test + public void testAcceptAlreadyDetachedDistroMedium() throws Exception { + + String controller = "IDE Controller"; + + IMachine machine = createNiceMock(IMachine.class); + + final StringBuilder errorBuilder = new StringBuilder(); + errorBuilder.append("VirtualBox error: "); + errorBuilder.append("Medium '/Users/johndoe/jclouds-virtualbox-test/ubuntu-11.04-server-i386.iso' "); + errorBuilder.append("is already detached from port 0, device 0 of controller 'IDE Controller' "); + errorBuilder.append("of this virtual machine (0x80BB000C)"); + String isoAlreadyAttachedException = errorBuilder.toString(); + + VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), + isoAlreadyAttachedException); + machine.detachDevice(controller, 0, 0); + expectLastCall().andThrow(isoAttachedException); + + replay(machine); + + new DetachDistroMediumToMachine(controller).apply(machine); + + verify(machine); + + } + + @Test(expectedExceptions = VBoxException.class) + public void testFailOnOtherVBoxErrors() throws Exception { + + String controller = "IDE Controller"; + + IMachine machine = createNiceMock(IMachine.class); + + final StringBuilder errorBuilder = new StringBuilder(); + errorBuilder.append("VirtualBox error: "); + errorBuilder.append("Some other VBox error"); + String isoAlreadyAttachedException = errorBuilder.toString(); + + VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), + isoAlreadyAttachedException); + machine.detachDevice(controller, 0, 0); + expectLastCall().andThrow(isoAttachedException); + + replay(machine); + + new DetachDistroMediumToMachine(controller).apply(machine); + + } + +}