issue 384: added DetachDistroMedium and RetrieveBridgedInterface + Test

This commit is contained in:
andreaturli 2011-11-25 22:41:20 +00:00
parent 536b8ff790
commit 4214382b8d
3 changed files with 163 additions and 0 deletions

1
.gitignore vendored
View File

@ -11,4 +11,5 @@ bin/
*.eml
*.ipr
*.iws
*.DS_STORE
TAGS

View File

@ -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<IMachine, Void> {
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;
}
}

View File

@ -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);
}
}