Merge pull request #266 from andreaturli/deletemedia

issue 384: improve UnregisterMachineIfExistsAndDeleteItsMedia + Test
This commit is contained in:
Adrian Cole 2011-12-21 18:44:06 -08:00
commit 1ec46fcc29
16 changed files with 371 additions and 260 deletions

View File

@ -19,13 +19,16 @@
package org.jclouds.virtualbox.domain; package org.jclouds.virtualbox.domain;
import com.google.common.base.Objects;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import org.virtualbox_4_1.DeviceType;
import com.google.common.base.Objects;
/** /**
* A representation of a hard disk in a VirtualBox VM. * A representation of a hard disk in a VirtualBox VM.
* <p/> * <p/>
* name is a description to identify the hard disk.
* diskPath is an absolute path to the file that is the location of the storage for the hard disk. * diskPath is an absolute path to the file that is the location of the storage for the hard disk.
* diskFormat is any of the formats supported by ISystemProperties.getMediumFormats() in the VirtualBox API. * diskFormat is any of the formats supported by ISystemProperties.getMediumFormats() in the VirtualBox API.
* This call is platform-dependent so the supported formats differ from host to host. The default format used is VDI. * This call is platform-dependent so the supported formats differ from host to host. The default format used is VDI.
@ -35,17 +38,22 @@ public class HardDisk {
public static final String DEFAULT_DISK_FORMAT = "vdi"; public static final String DEFAULT_DISK_FORMAT = "vdi";
// NB the name is not independent; the IMedium name is chosen based on the last part of diskPath
private final String name;
private final String diskFormat; private final String diskFormat;
private final String diskPath; private final String diskPath;
private final DeviceDetails deviceDetails; private final DeviceDetails deviceDetails;
private final boolean autoDelete;
public HardDisk(DeviceDetails deviceDetails, String diskPath, String diskFormat) { public HardDisk(DeviceDetails deviceDetails, String diskPath, String diskFormat, boolean autoDelete) {
checkNotNull(deviceDetails, "deviceDetails"); checkNotNull(deviceDetails, "deviceDetails");
checkNotNull(diskPath, "diskPath"); checkNotNull(diskPath, "diskPath");
checkNotNull(diskFormat, "diskFormat"); checkNotNull(diskFormat, "diskFormat");
this.diskPath = diskPath; this.diskPath = diskPath;
this.diskFormat = diskFormat; this.diskFormat = diskFormat;
this.deviceDetails = deviceDetails; this.deviceDetails = deviceDetails;
this.name = diskPath.substring(diskPath.lastIndexOf("/") + 1);
this.autoDelete = autoDelete;
} }
public String getDiskPath() { public String getDiskPath() {
@ -60,6 +68,14 @@ public class HardDisk {
return deviceDetails; return deviceDetails;
} }
public String getName() {
return name;
}
public boolean isAutoDelete() {
return autoDelete;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
@ -67,14 +83,15 @@ public class HardDisk {
HardDisk hardDisk = (HardDisk) o; HardDisk hardDisk = (HardDisk) o;
return Objects.equal(deviceDetails, hardDisk.deviceDetails) && return Objects.equal(deviceDetails, hardDisk.deviceDetails) &&
Objects.equal(diskFormat, hardDisk.diskFormat) && Objects.equal(diskFormat, hardDisk.diskFormat) &&
Objects.equal(diskPath, hardDisk.diskPath); Objects.equal(diskPath, hardDisk.diskPath) &&
Objects.equal(name, hardDisk.name);
} }
return false; return false;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hashCode(diskPath, diskFormat, deviceDetails); return Objects.hashCode(diskPath, diskFormat, deviceDetails, name);
} }
@Override @Override
@ -83,6 +100,53 @@ public class HardDisk {
"diskFormat='" + diskFormat + '\'' + "diskFormat='" + diskFormat + '\'' +
", diskPath='" + diskPath + '\'' + ", diskPath='" + diskPath + '\'' +
", deviceDetails=" + deviceDetails + ", deviceDetails=" + deviceDetails +
", name=" + name +
'}'; '}';
} }
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String diskFormat = "vdi";
private String diskPath;
private int controllerPort;
private int deviceSlot;
private DeviceType deviceType = DeviceType.HardDisk;
private boolean autoDelete = false;
public Builder diskFormat(String diskFormat) {
this.diskFormat = diskFormat;
return this;
}
public Builder diskpath(String diskPath) {
this.diskPath = diskPath;
return this;
}
public Builder controllerPort(int controllerPort) {
this.controllerPort = controllerPort;
return this;
}
public Builder deviceSlot(int deviceSlot) {
this.deviceSlot = deviceSlot;
return this;
}
public Builder autoDelete(boolean autoDelete) {
this.autoDelete = autoDelete;
return this;
}
public HardDisk build() {
checkNotNull(diskPath);
checkNotNull(controllerPort);
checkNotNull(deviceSlot);
return new HardDisk(new DeviceDetails(controllerPort, deviceSlot, deviceType), diskPath, diskFormat, autoDelete);
}
}
} }

View File

@ -20,13 +20,24 @@
package org.jclouds.virtualbox.domain; package org.jclouds.virtualbox.domain;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.Image;
import org.jclouds.javax.annotation.Nullable;
import org.virtualbox_4_1.DeviceType; import org.virtualbox_4_1.DeviceType;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.StorageBus; import org.virtualbox_4_1.StorageBus;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
import static org.jclouds.virtualbox.domain.HardDisk.DEFAULT_DISK_FORMAT; import static org.jclouds.virtualbox.domain.HardDisk.DEFAULT_DISK_FORMAT;
/** /**
@ -64,6 +75,12 @@ public class StorageController {
return bus; return bus;
} }
public HardDisk getHardDisk(String diskName) {
final Iterable<HardDisk> hardDisks = filter(getHardDisks(), new HardDiskPredicate(diskName));
return Iterables.getFirst(hardDisks, HardDisk.builder().diskpath("notfound").controllerPort(0).deviceSlot(0).build());
}
public Set<HardDisk> getHardDisks() { public Set<HardDisk> getHardDisks() {
return hardDisks; return hardDisks;
} }
@ -126,13 +143,8 @@ public class StorageController {
return this; return this;
} }
public Builder attachHardDisk(int controllerPort, int deviceSlot, String diskPath) { public Builder attachHardDisk(HardDisk hardDisk) {
hardDisks.add(new HardDisk(new DeviceDetails(controllerPort, deviceSlot, DeviceType.HardDisk), diskPath, DEFAULT_DISK_FORMAT)); hardDisks.add(hardDisk);
return this;
}
public Builder attachHardDisk(int controllerPort, int deviceSlot, String diskPath, String diskFormat) {
hardDisks.add(new HardDisk(new DeviceDetails(controllerPort, deviceSlot, DeviceType.HardDisk), diskPath, diskFormat));
return this; return this;
} }
@ -141,6 +153,19 @@ public class StorageController {
checkNotNull(bus); checkNotNull(bus);
return new StorageController(name, bus, hardDisks, dvds); return new StorageController(name, bus, hardDisks, dvds);
} }
} }
private class HardDiskPredicate implements Predicate<HardDisk> {
private String diskName;
public HardDiskPredicate(String diskName) {
this.diskName = diskName;
}
@Override
public boolean apply(@Nullable HardDisk hardDisk) {
return hardDisk.getName().equals(diskName);
}
};
} }

View File

@ -19,13 +19,18 @@
package org.jclouds.virtualbox.domain; package org.jclouds.virtualbox.domain;
import com.google.common.base.Objects;
import java.util.*;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.virtualbox_4_1.CleanupMode;
import com.google.common.base.Objects;
/** /**
* A description of a Virtual Machine in VirtualBox. * A description of a Virtual Machine in VirtualBox.
@ -39,13 +44,15 @@ public class VmSpec {
private final boolean forceOverwrite; private final boolean forceOverwrite;
private final Map<Long, NatAdapter> natNetworkAdapters; private final Map<Long, NatAdapter> natNetworkAdapters;
private final Set<StorageController> controllers; private final Set<StorageController> controllers;
private final CleanupMode cleanupMode;
public VmSpec(String vmId, String vmName, String osTypeId, long memory, boolean forceOverwrite, Set<StorageController> controllers, Map<Long, NatAdapter> natNetworkAdapters) { public VmSpec(String vmId, String vmName, String osTypeId, long memory, boolean forceOverwrite, Set<StorageController> controllers, Map<Long, NatAdapter> natNetworkAdapters, CleanupMode cleanupMode) {
checkNotNull(vmId, "vmId"); checkNotNull(vmId, "vmId");
checkNotNull(vmName, "vmName"); checkNotNull(vmName, "vmName");
checkArgument(memory > 0, "memory must be > 0"); checkArgument(memory > 0, "memory must be > 0");
checkNotNull(controllers, "controllers"); checkNotNull(controllers, "controllers");
checkNotNull(natNetworkAdapters, "natNetworkAdapters"); checkNotNull(natNetworkAdapters, "natNetworkAdapters");
checkNotNull(cleanupMode, "cleanupMode");
this.vmId = vmId; this.vmId = vmId;
this.vmName = vmName; this.vmName = vmName;
this.osTypeId = osTypeId; this.osTypeId = osTypeId;
@ -53,6 +60,7 @@ public class VmSpec {
this.controllers = controllers; this.controllers = controllers;
this.forceOverwrite = forceOverwrite; this.forceOverwrite = forceOverwrite;
this.natNetworkAdapters = natNetworkAdapters; this.natNetworkAdapters = natNetworkAdapters;
this.cleanupMode = cleanupMode;
} }
public static Builder builder() { public static Builder builder() {
@ -69,6 +77,8 @@ public class VmSpec {
private boolean forceOverwrite; private boolean forceOverwrite;
private Map<Long, NatAdapter> natNetworkAdapters = new HashMap<Long, NatAdapter>(); private Map<Long, NatAdapter> natNetworkAdapters = new HashMap<Long, NatAdapter>();
private long memory; private long memory;
private CleanupMode cleanUpMode;
public Builder controller(StorageController controller) { public Builder controller(StorageController controller) {
controllers.add(controller); controllers.add(controller);
return this; return this;
@ -104,11 +114,16 @@ public class VmSpec {
return this; return this;
} }
public Builder cleanUpMode(CleanupMode cleanupMode) {
this.cleanUpMode = cleanupMode;
return this;
}
public VmSpec build() { public VmSpec build() {
checkNotNull(name, "name"); checkNotNull(name, "name");
checkNotNull(id, "id"); checkNotNull(id, "id");
checkArgument(memory > 0, "Memory must be set"); checkArgument(memory > 0, "Memory must be set");
return new VmSpec(id, name, osTypeId, memory, forceOverwrite, controllers, natNetworkAdapters); return new VmSpec(id, name, osTypeId, memory, forceOverwrite, controllers, natNetworkAdapters, cleanUpMode);
} }
@ -141,6 +156,10 @@ public class VmSpec {
return Collections.unmodifiableMap(natNetworkAdapters); return Collections.unmodifiableMap(natNetworkAdapters);
} }
public CleanupMode getCleanupMode() {
return cleanupMode;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
@ -152,7 +171,8 @@ public class VmSpec {
Objects.equal(memory, other.memory) && Objects.equal(memory, other.memory) &&
Objects.equal(forceOverwrite, other.forceOverwrite) && Objects.equal(forceOverwrite, other.forceOverwrite) &&
Objects.equal(natNetworkAdapters, other.natNetworkAdapters) && Objects.equal(natNetworkAdapters, other.natNetworkAdapters) &&
Objects.equal(controllers, other.controllers); Objects.equal(controllers, other.controllers) &&
Objects.equal(cleanupMode, other.cleanupMode);
} }
return false; return false;
} }
@ -172,6 +192,7 @@ public class VmSpec {
", forceOverwrite=" + forceOverwrite + ", forceOverwrite=" + forceOverwrite +
", natNetworkAdapters=" + natNetworkAdapters + ", natNetworkAdapters=" + natNetworkAdapters +
", controllers=" + controllers + ", controllers=" + controllers +
", cleanupMode=" + cleanupMode +
'}'; '}';
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Licensed to jclouds, Inc. (jclouds) under one or more U * Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file * contributor license agreements. See the NOTICE file
* distributed with this work for additional information * distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file * regarding copyright ownership. jclouds licenses this file
@ -19,16 +19,19 @@
package org.jclouds.virtualbox.functions.admin; package org.jclouds.virtualbox.functions.admin;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collections;
import java.util.List; import java.util.List;
import javax.annotation.Nullable;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Named; import javax.inject.Named;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.virtualbox.domain.ErrorCode; import org.jclouds.virtualbox.domain.ErrorCode;
import org.virtualbox_4_1.CleanupMode; import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IMedium; import org.virtualbox_4_1.IMedium;
import org.virtualbox_4_1.IProgress; import org.virtualbox_4_1.IProgress;
@ -36,57 +39,78 @@ import org.virtualbox_4_1.VBoxException;
import org.virtualbox_4_1.VirtualBoxManager; import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
public class UnregisterMachineIfExistsAndDeleteItsMedia implements Function<String, Void> { public class UnregisterMachineIfExistsAndDeleteItsMedia implements
Function<VmSpec, Void> {
@Resource @Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER) @Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
private VirtualBoxManager manager; private VirtualBoxManager manager;
private CleanupMode mode;
public UnregisterMachineIfExistsAndDeleteItsMedia(VirtualBoxManager manager, CleanupMode mode) { public UnregisterMachineIfExistsAndDeleteItsMedia(VirtualBoxManager manager) {
this.manager = manager; this.manager = manager;
this.mode = mode;
} }
@Override @Override
public Void apply(@Nullable String vmName) { public Void apply(VmSpec vmSpec) {
List<IMedium> mediaToBeDeleted = null; List<IMedium> mediaToBeDeleted = Collections.emptyList();
IMachine machine = null; IMachine machine = null;
try { try {
machine = manager.getVBox().findMachine(vmName); machine = manager.getVBox().findMachine(vmSpec.getVmName());
mediaToBeDeleted = machine.unregister(mode); mediaToBeDeleted = machine.unregister(vmSpec.getCleanupMode());
} catch (VBoxException e) { } catch (VBoxException e) {
ErrorCode errorCode = ErrorCode.valueOf(e); ErrorCode errorCode = ErrorCode.valueOf(e);
switch (errorCode) { switch (errorCode) {
case VBOX_E_OBJECT_NOT_FOUND: case VBOX_E_OBJECT_NOT_FOUND:
logger.debug("Machine %s does not exists, cannot unregister", logger.debug("Machine %s does not exists, cannot unregister",
vmName); vmSpec.getVmName());
break; break;
default: default:
throw e; throw e;
} }
} }
/**
* deletion of all files is currently disabled on Windows/x64 to prevent a List<IMedium> filteredMediaToBeDeleted = Lists.newArrayList(Iterables
* crash .filter(mediaToBeDeleted, new AutoDeleteHardDiskPredicate(vmSpec)));
*/
checkNotNull(filteredMediaToBeDeleted);
if (!filteredMediaToBeDeleted.isEmpty()) {
try { try {
IProgress deletion = machine.delete(mediaToBeDeleted); IProgress deletion = machine.delete(filteredMediaToBeDeleted);
deletion.waitForCompletion(-1); deletion.waitForCompletion(-1);
} catch (Exception e) { } catch (Exception e) {
logger.error(e, "Problem in deleting the media attached to %s", machine.getName()); logger.error(e, "Problem in deleting the media attached to %s",
propagate(e); machine.getName());
Throwables.propagate(e);
}
} }
return null; return null;
} }
protected <T> T propagate(Exception e) { private class AutoDeleteHardDiskPredicate implements Predicate<IMedium> {
Throwables.propagate(e);
assert false; private VmSpec vmSpec;
return null;
public AutoDeleteHardDiskPredicate(VmSpec vmSpec) {
this.vmSpec = vmSpec;
} }
@Override
public boolean apply(IMedium medium) {
for (StorageController controller : vmSpec.getControllers()) {
if (controller.getHardDisk(medium.getName()).isAutoDelete())
return true;
}
return false;
}
};
} }

View File

@ -1,58 +0,0 @@
/**
* 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.domain;
import static org.jclouds.scriptbuilder.domain.Statements.interpret;
import static org.jclouds.virtualbox.domain.Statements.getIpAddress;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import org.jclouds.scriptbuilder.ScriptBuilder;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.ShellToken;
import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import com.google.common.io.Resources;
/**
* @author Andrea Turli
*/
@Test(groups = "unit")
public class GetIPAddressFromGuestAdditionsTest {
ScriptBuilder getIpAddressBuilder = new ScriptBuilder()
.addStatement(getIpAddress("{args}"))
.addStatement(interpret("echo {varl}FOUND_IP_ADDRESS{varr}{lf}"));
public void testUNIX() throws IOException {
assertEquals(getIpAddressBuilder.render(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources
.getResource("test_get_ip_address." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)));
}
// TODO
/*
public void testWINDOWS() throws IOException {
assertEquals(getIpAddressBuilder.render(OsFamily.WINDOWS), CharStreams.toString(Resources.newReaderSupplier(Resources
.getResource("test_get_ip_address." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8)));
}
*/
}

View File

@ -1,53 +0,0 @@
/**
* 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.domain;
import static org.testng.Assert.assertEquals;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.testng.annotations.Test;
/**
* @author Andrea Turli
*/
@Test(groups = "unit")
public class GetIPAddressFromMACTest {
private static final String macAddressWith00 = "0800271A9806";
private static final String unixMacAddressWith00 = "08:00:27:1a:98:06";
private static final String bsdMacAddressWith00 = "8:0:27:1a:98:6";
private static final String macAddressWith0c = "0811271A9806";
private static final String unixMacAddressWith0c = "08:11:27:1a:98:06";
private static final String bsdMacAddressWith0c = "8:11:27:1a:98:6";
public void testGetIPAdressFromMacAddressUnix() {
GetIPAddressFromMAC statement = new GetIPAddressFromMAC(macAddressWith00);
assertEquals(statement.render(OsFamily.UNIX), "MAC="
+ unixMacAddressWith00
+ " && [[ `uname -s` = \"Darwin\" ]] && MAC=" + bsdMacAddressWith00
+ "\n arp -an | grep $MAC\n");
statement = new GetIPAddressFromMAC(macAddressWith0c);
assertEquals(statement.render(OsFamily.UNIX), "MAC="
+ unixMacAddressWith0c
+ " && [[ `uname -s` = \"Darwin\" ]] && MAC=" + bsdMacAddressWith0c
+ "\n arp -an | grep $MAC\n");
}
}

View File

@ -19,12 +19,13 @@
package org.jclouds.virtualbox.domain; package org.jclouds.virtualbox.domain;
import org.testng.annotations.Test;
import org.virtualbox_4_1.StorageBus;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.StorageBus;
public class VmSpecTest { public class VmSpecTest {
@Test @Test
@ -68,6 +69,7 @@ public class VmSpecTest {
.name("My VM") .name("My VM")
.osTypeId("Ubuntu") .osTypeId("Ubuntu")
.memoryMB(1024) .memoryMB(1024)
.cleanUpMode(CleanupMode.Full)
.natNetworkAdapter( .natNetworkAdapter(
0, 0,
NatAdapter.builder().tcpRedirectRule("localhost", 2222, "", 22).build()) NatAdapter.builder().tcpRedirectRule("localhost", 2222, "", 22).build())
@ -75,6 +77,8 @@ public class VmSpecTest {
.controller( .controller(
StorageController.builder().name("Controller") StorageController.builder().name("Controller")
.bus(StorageBus.IDE) .bus(StorageBus.IDE)
.attachHardDisk(0, 0, "/tmp/tempdisk.vdi").build()); .attachHardDisk(HardDisk.builder().diskpath("/tmp/tempdisk.vdi")
.controllerPort(0).deviceSlot(0).build())
.build());
} }
} }

View File

@ -19,13 +19,6 @@
package org.jclouds.virtualbox.functions; package org.jclouds.virtualbox.functions;
import com.google.common.collect.Iterables;
import org.jclouds.virtualbox.domain.DeviceDetails;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.StorageController;
import org.testng.annotations.Test;
import org.virtualbox_4_1.*;
import static com.google.common.collect.Iterables.getOnlyElement; import static com.google.common.collect.Iterables.getOnlyElement;
import static org.easymock.EasyMock.anyLong; import static org.easymock.EasyMock.anyLong;
import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expect;
@ -34,7 +27,19 @@ import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.createNiceMock; import static org.easymock.classextension.EasyMock.createNiceMock;
import static org.easymock.classextension.EasyMock.replay; import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify; import static org.easymock.classextension.EasyMock.verify;
import static org.virtualbox_4_1.DeviceType.HardDisk;
import org.jclouds.virtualbox.domain.DeviceDetails;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.StorageController;
import org.testng.annotations.Test;
import org.virtualbox_4_1.DeviceType;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IMedium;
import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.IVirtualBox;
import org.virtualbox_4_1.StorageBus;
import org.virtualbox_4_1.VBoxException;
import org.virtualbox_4_1.VirtualBoxManager;
/** /**
* @author Mattias Holmqvist * @author Mattias Holmqvist
@ -47,9 +52,10 @@ public class AttachMediumToMachineIfNotAlreadyAttachedTest {
String controllerName = "IDE Controller"; String controllerName = "IDE Controller";
String diskPath = "/Users/johndoe/jclouds-virtualbox-images/admin.vdi"; String diskPath = "/Users/johndoe/jclouds-virtualbox-images/admin.vdi";
String diskName = "admin";
String diskFormat = "vdi"; String diskFormat = "vdi";
int controllerPort = 0; int controllerPort = 0;
int device = 1; int deviceSlot = 1;
VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class); VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class);
IMachine machine = createMock(IMachine.class); IMachine machine = createMock(IMachine.class);
@ -61,14 +67,15 @@ public class AttachMediumToMachineIfNotAlreadyAttachedTest {
expect(vBox.createHardDisk(diskFormat, diskPath)).andReturn(hardDisk); expect(vBox.createHardDisk(diskFormat, diskPath)).andReturn(hardDisk);
expect(hardDisk.createBaseStorage(anyLong(), anyLong())).andReturn(progress); expect(hardDisk.createBaseStorage(anyLong(), anyLong())).andReturn(progress);
machine.attachDevice(controllerName, controllerPort, device, HardDisk, hardDisk); machine.attachDevice(controllerName, controllerPort, deviceSlot, DeviceType.HardDisk, hardDisk);
machine.saveSettings(); machine.saveSettings();
replay(manager, machine, vBox, hardDisk); replay(manager, machine, vBox, hardDisk);
StorageController controller = StorageController.builder() StorageController controller = StorageController.builder()
.name(controllerName) .name(controllerName)
.bus(StorageBus.IDE) .bus(StorageBus.IDE)
.attachHardDisk(controllerPort, device, diskPath) .attachHardDisk(HardDisk.builder().diskpath(diskPath)
.controllerPort(controllerPort).deviceSlot(deviceSlot).build())
.build(); .build();
@ -100,7 +107,7 @@ public class AttachMediumToMachineIfNotAlreadyAttachedTest {
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class),
isoAlreadyAttachedException); isoAlreadyAttachedException);
machine.attachDevice(controllerName, controllerPort, deviceSlot, HardDisk, hardDisk); machine.attachDevice(controllerName, controllerPort, deviceSlot, DeviceType.HardDisk, hardDisk);
expectLastCall().andThrow(isoAttachedException); expectLastCall().andThrow(isoAttachedException);
replay(manager, machine, vBox, hardDisk); replay(manager, machine, vBox, hardDisk);
@ -108,7 +115,8 @@ public class AttachMediumToMachineIfNotAlreadyAttachedTest {
StorageController controller = StorageController.builder() StorageController controller = StorageController.builder()
.name(controllerName) .name(controllerName)
.bus(StorageBus.IDE) .bus(StorageBus.IDE)
.attachHardDisk(controllerPort, deviceSlot, "/Users/mattias/jclouds-virtualbox-test/testadmin.vdi") .attachHardDisk(HardDisk.builder().diskpath("/Users/mattias/jclouds-virtualbox-test/testadmin.vdi")
.controllerPort(controllerPort).deviceSlot(deviceSlot).build())
.build(); .build();
DeviceDetails deviceDetails = getOnlyElement(controller.getHardDisks()).getDeviceDetails(); DeviceDetails deviceDetails = getOnlyElement(controller.getHardDisks()).getDeviceDetails();
@ -137,7 +145,7 @@ public class AttachMediumToMachineIfNotAlreadyAttachedTest {
VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class), VBoxException isoAttachedException = new VBoxException(createNiceMock(Throwable.class),
isoAlreadyAttachedException); isoAlreadyAttachedException);
machine.attachDevice(controllerName, controllerPort, deviceSlot, HardDisk, hardDisk); machine.attachDevice(controllerName, controllerPort, deviceSlot, DeviceType.HardDisk, hardDisk);
expectLastCall().andThrow(isoAttachedException); expectLastCall().andThrow(isoAttachedException);
replay(manager, machine, vBox, hardDisk); replay(manager, machine, vBox, hardDisk);
@ -146,7 +154,8 @@ public class AttachMediumToMachineIfNotAlreadyAttachedTest {
StorageController controller = StorageController.builder() StorageController controller = StorageController.builder()
.name(controllerName) .name(controllerName)
.bus(StorageBus.IDE) .bus(StorageBus.IDE)
.attachHardDisk(controllerPort, deviceSlot, "/Users/mattias/jclouds-virtualbox-test/testadmin.vdi") .attachHardDisk(HardDisk.builder().diskpath("/Users/mattias/jclouds-virtualbox-test/testadmin.vdi")
.controllerPort(controllerPort).deviceSlot(deviceSlot).build())
.build(); .build();
DeviceDetails deviceDetails = getOnlyElement(controller.getHardDisks()).getDeviceDetails(); DeviceDetails deviceDetails = getOnlyElement(controller.getHardDisks()).getDeviceDetails();

View File

@ -19,25 +19,31 @@
package org.jclouds.virtualbox.functions; package org.jclouds.virtualbox.functions;
import com.google.common.base.Predicate; import static org.jclouds.virtualbox.domain.ExecutionType.HEADLESS;
import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest;
import static org.testng.Assert.assertEquals;
import static org.virtualbox_4_1.NetworkAttachmentType.Bridged;
import java.util.concurrent.TimeUnit;
import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.net.IPSocket; import org.jclouds.net.IPSocket;
import org.jclouds.predicates.InetSocketAddressConnect; import org.jclouds.predicates.InetSocketAddressConnect;
import org.jclouds.predicates.RetryablePredicate; import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.StorageController; import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec; import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.util.PropertyUtils; import org.jclouds.virtualbox.util.PropertyUtils;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.virtualbox_4_1.*; import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.StorageBus;
import org.virtualbox_4_1.VirtualBoxManager;
import java.util.concurrent.TimeUnit; import com.google.common.base.Predicate;
import static org.jclouds.virtualbox.domain.ExecutionType.HEADLESS;
import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest;
import static org.testng.Assert.assertEquals;
import static org.virtualbox_4_1.NetworkAttachmentType.Bridged;
/** /**
* @author Andrea Turli * @author Andrea Turli
@ -57,7 +63,6 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba
private String vmName = "jclouds-image-virtualbox-iso-to-machine-test"; private String vmName = "jclouds-image-virtualbox-iso-to-machine-test";
private String cloneName = vmName + "_clone"; private String cloneName = vmName + "_clone";
private String isoName = "ubuntu-11.04-server-i386.iso";
@Test @Test
public void testCloneMachineFromAnotherMachine() throws Exception { public void testCloneMachineFromAnotherMachine() throws Exception {
@ -86,9 +91,12 @@ public class CloneAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends Ba
String workingDir = PropertyUtils.getWorkingDirFromProperty(); String workingDir = PropertyUtils.getWorkingDirFromProperty();
StorageController ideController = StorageController.builder().name(controllerIDE).bus(StorageBus.IDE) StorageController ideController = StorageController.builder().name(controllerIDE).bus(StorageBus.IDE)
.attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso") .attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso")
.attachHardDisk(0, 1, workingDir + "/testadmin.vdi") .attachHardDisk(HardDisk.builder().diskpath(workingDir + "/testadmin.vdi")
.controllerPort(0).deviceSlot(1).build())
.attachISO(1, 1, workingDir + "/VBoxGuestAdditions_4.1.2.iso").build(); .attachISO(1, 1, workingDir + "/VBoxGuestAdditions_4.1.2.iso").build();
VmSpec vmSpecification = VmSpec.builder().id(vmId).name(vmName).osTypeId(osTypeId) VmSpec vmSpecification = VmSpec.builder().id(vmId).name(vmName).osTypeId(osTypeId)
.memoryMB(512)
.cleanUpMode(CleanupMode.Full)
.controller(ideController) .controller(ideController)
.forceOverwrite(true).build(); .forceOverwrite(true).build();
return new CreateAndInstallVm(manager, guestId, localHostContext, hostId, socketTester, return new CreateAndInstallVm(manager, guestId, localHostContext, hostId, socketTester,

View File

@ -44,6 +44,7 @@ import org.jclouds.net.IPSocket;
import org.jclouds.predicates.InetSocketAddressConnect; import org.jclouds.predicates.InetSocketAddressConnect;
import org.jclouds.predicates.RetryablePredicate; import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.NatAdapter; import org.jclouds.virtualbox.domain.NatAdapter;
import org.jclouds.virtualbox.domain.StorageController; import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec; import org.jclouds.virtualbox.domain.VmSpec;
@ -75,14 +76,29 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
private String ideControllerName = "IDE Controller"; private String ideControllerName = "IDE Controller";
private String guestId = "guest"; private String guestId = "guest";
private String hostId = "host"; private String hostId = "host";
private String vmName = "jclouds-image-virtualbox-iso-to-machine-test"; private String vmName = "jclouds-image-virtualbox-iso-to-machine-test";
private StorageController ideController;
private VmSpec vmSpecification;
@BeforeGroups(groups = {"live"}) @BeforeGroups(groups = {"live"})
public void setUp() throws Exception { public void setUp() throws Exception {
identity = "toor"; identity = "toor";
credential = "password"; credential = "password";
new UnregisterMachineIfExistsAndDeleteItsMedia(manager, CleanupMode.Full).apply(vmName);
String workingDir = PropertyUtils.getWorkingDirFromProperty();
HardDisk hardDisk = HardDisk.builder().diskpath(workingDir + "/testadmin.vdi").autoDelete(true)
.controllerPort(0).deviceSlot(1).build();
ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE)
.attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso")
.attachHardDisk(hardDisk)
.attachISO(1, 1, workingDir + "/VBoxGuestAdditions_4.1.2.iso").build();
vmSpecification = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).osTypeId(osTypeId)
.controller(ideController)
.forceOverwrite(true)
.cleanUpMode(CleanupMode.Full)
.natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build()).build();
new UnregisterMachineIfExistsAndDeleteItsMedia(manager).apply(vmSpecification);
} }
public void testCreateImageMachineFromIso() throws Exception { public void testCreateImageMachineFromIso() throws Exception {
@ -92,15 +108,6 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
"localhost", new Credentials("toor", "password")); "localhost", new Credentials("toor", "password"));
Predicate<IPSocket> socketTester = new RetryablePredicate<IPSocket>(new InetSocketAddressConnect(), 10, 1, TimeUnit.SECONDS); Predicate<IPSocket> socketTester = new RetryablePredicate<IPSocket>(new InetSocketAddressConnect(), 10, 1, TimeUnit.SECONDS);
String workingDir = PropertyUtils.getWorkingDirFromProperty();
StorageController ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE)
.attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso")
.attachHardDisk(0, 1, workingDir + "/testadmin.vdi")
.attachISO(1, 1, workingDir + "/VBoxGuestAdditions_4.1.2.iso").build();
VmSpec vmSpecification = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).osTypeId(osTypeId)
.controller(ideController)
.forceOverwrite(true)
.natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build()).build();
IMachine imageMachine = new CreateAndInstallVm(manager, guestId, localHostContext, hostId, IMachine imageMachine = new CreateAndInstallVm(manager, guestId, localHostContext, hostId,
socketTester, "127.0.0.1", 8080, HEADLESS) socketTester, "127.0.0.1", 8080, HEADLESS)
.apply(vmSpecification); .apply(vmSpecification);
@ -111,9 +118,7 @@ public class CreateAndInstallVmLiveTest extends BaseVirtualBoxClientLiveTest {
// YAML the image desc // YAML the image desc
Set<? extends Image> images = context.getComputeService().listImages(); Set<? extends Image> images = context.getComputeService().listImages();
Iterable<String> imageIds = transform(images, extractId()); Iterable<String> imageIds = transform(images, extractId());
assertTrue(any(imageIds, equalTo(newImage.getId()))); assertTrue(any(imageIds, equalTo(newImage.getId())));
} }
private Function<Image, String> extractId() { private Function<Image, String> extractId() {

View File

@ -24,11 +24,16 @@ import static org.testng.Assert.fail;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.ErrorCode; import org.jclouds.virtualbox.domain.ErrorCode;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec; import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExistsAndDeleteItsMedia; import org.jclouds.virtualbox.functions.admin.UnregisterMachineIfExistsAndDeleteItsMedia;
import org.jclouds.virtualbox.util.PropertyUtils;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode; import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.StorageBus;
import org.virtualbox_4_1.VBoxException; import org.virtualbox_4_1.VBoxException;
/** /**
@ -36,24 +41,41 @@ import org.virtualbox_4_1.VBoxException;
*/ */
public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClientLiveTest { public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClientLiveTest {
private String ideControllerName;
private CleanupMode mode;
private StorageController ideController;
@BeforeMethod
public void setUp() {
ideControllerName = "IDE Controller";
mode = CleanupMode.Full;
String workingDir = PropertyUtils.getWorkingDirFromProperty();
ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE)
.attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso")
.attachHardDisk(HardDisk.builder().diskpath(workingDir + "/testadmin.vdi")
.controllerPort(0).deviceSlot(1).build())
.attachISO(1, 1, workingDir + "/VBoxGuestAdditions_4.1.2.iso").build();
}
@Test @Test
public void testCreateNewMachine() throws Exception { public void testCreateNewMachine() throws Exception {
String vmName = "jclouds-test-create-1-node"; String vmName = "jclouds-test-create-1-node";
new UnregisterMachineIfExistsAndDeleteItsMedia(manager, CleanupMode.Full).apply(vmName); VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName).memoryMB(512).controller(ideController).cleanUpMode(mode)
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName)
.osTypeId("Debian").forceOverwrite(true).build(); .osTypeId("Debian").forceOverwrite(true).build();
new UnregisterMachineIfExistsAndDeleteItsMedia(manager).apply(launchSpecification);
IMachine debianNode = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification); IMachine debianNode = new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification);
IMachine machine = manager.getVBox().findMachine(vmName); IMachine machine = manager.getVBox().findMachine(vmName);
assertEquals(debianNode.getName(), machine.getName()); assertEquals(debianNode.getName(), machine.getName());
new UnregisterMachineIfExistsAndDeleteItsMedia(manager, CleanupMode.Full).apply(vmName); new UnregisterMachineIfExistsAndDeleteItsMedia(manager).apply(launchSpecification);
} }
@Test @Test
public void testCreateNewMachineWithBadOsType() throws Exception { public void testCreateNewMachineWithBadOsType() throws Exception {
String vmName = "jclouds-test-create-2-node"; String vmName = "jclouds-test-create-2-node";
new UnregisterMachineIfExistsAndDeleteItsMedia(manager, CleanupMode.Full).apply(vmName); VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName).memoryMB(512).controller(ideController).cleanUpMode(mode)
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName)
.osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build(); .osTypeId("SomeWeirdUnknownOs").forceOverwrite(true).build();
new UnregisterMachineIfExistsAndDeleteItsMedia(manager).apply(launchSpecification);
try { try {
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification); new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification);
fail(); fail();

View File

@ -31,6 +31,7 @@ import static org.easymock.classextension.EasyMock.verify;
import org.easymock.EasyMock; import org.easymock.EasyMock;
import org.jclouds.virtualbox.domain.VmSpec; import org.jclouds.virtualbox.domain.VmSpec;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IVirtualBox; import org.virtualbox_4_1.IVirtualBox;
import org.virtualbox_4_1.VBoxException; import org.virtualbox_4_1.VBoxException;
@ -49,7 +50,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
IVirtualBox vBox = createMock(IVirtualBox.class); IVirtualBox vBox = createMock(IVirtualBox.class);
String vmName = "jclouds-image-my-ubuntu-image"; String vmName = "jclouds-image-my-ubuntu-image";
VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName).osTypeId("").memoryMB(1024).build(); VmSpec launchSpecification = VmSpec.builder().id(vmName).name(vmName).osTypeId("").memoryMB(1024).cleanUpMode(CleanupMode.Full).build();
IMachine createdMachine = createMock(IMachine.class); IMachine createdMachine = createMock(IMachine.class);
@ -90,7 +91,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
replay(manager, vBox); replay(manager, vBox);
VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").memoryMB(1024).build(); VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").memoryMB(1024).cleanUpMode(CleanupMode.Full).build();
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification); new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification);
} }
@ -111,7 +112,7 @@ public class CreateAndRegisterMachineFromIsoIfNotAlreadyExistsTest {
replay(manager, vBox); replay(manager, vBox);
VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").memoryMB(1024).build(); VmSpec launchSpecification = VmSpec.builder().id("").name(vmName).osTypeId("").cleanUpMode(CleanupMode.Full).memoryMB(1024).build();
new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification); new CreateAndRegisterMachineFromIsoIfNotAlreadyExists(manager).apply(launchSpecification);
} }

View File

@ -19,20 +19,19 @@
package org.jclouds.virtualbox.functions; package org.jclouds.virtualbox.functions;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.io.File;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest; import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.DeviceDetails;
import org.jclouds.virtualbox.domain.ErrorCode; import org.jclouds.virtualbox.domain.ErrorCode;
import org.jclouds.virtualbox.domain.HardDisk; import org.jclouds.virtualbox.domain.HardDisk;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.virtualbox_4_1.DeviceType; import org.virtualbox_4_1.DeviceType;
import org.virtualbox_4_1.VBoxException; import org.virtualbox_4_1.VBoxException;
import java.io.File;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
/** /**
* @author Mattias Holmqvist * @author Mattias Holmqvist
*/ */
@ -41,7 +40,7 @@ public class CreateMediumIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClient
@Test @Test
public void testCreateMedium() throws Exception { public void testCreateMedium() throws Exception {
String path = System.getProperty("user.home") + "/jclouds-virtualbox-test/test-medium-1.vdi"; String path = System.getProperty("user.home") + "/jclouds-virtualbox-test/test-medium-1.vdi";
HardDisk hardDisk = new HardDisk(new DeviceDetails(0, 0, DeviceType.HardDisk), path, "vdi"); HardDisk hardDisk = HardDisk.builder().diskpath(path).controllerPort(0).deviceSlot(0).build();
new CreateMediumIfNotAlreadyExists(manager, true).apply(hardDisk); new CreateMediumIfNotAlreadyExists(manager, true).apply(hardDisk);
manager.getVBox().findMedium(path, DeviceType.HardDisk); manager.getVBox().findMedium(path, DeviceType.HardDisk);
assertFileCanBeDeleted(path); assertFileCanBeDeleted(path);
@ -50,7 +49,7 @@ public class CreateMediumIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClient
@Test @Test
public void testCreateMediumFailWhenUsingNonFullyQualifiedPath() throws Exception { public void testCreateMediumFailWhenUsingNonFullyQualifiedPath() throws Exception {
String path = "test-medium-2.vdi"; String path = "test-medium-2.vdi";
HardDisk hardDisk = new HardDisk(new DeviceDetails(0, 0, DeviceType.HardDisk), path, "vdi"); HardDisk hardDisk = HardDisk.builder().diskpath(path).controllerPort(0).deviceSlot(0).build();
try { try {
new CreateMediumIfNotAlreadyExists(manager, true).apply(hardDisk); new CreateMediumIfNotAlreadyExists(manager, true).apply(hardDisk);
fail(); fail();
@ -63,7 +62,7 @@ public class CreateMediumIfNotAlreadyExistsLiveTest extends BaseVirtualBoxClient
@Test @Test
public void testCreateSameMediumTwiceWhenUsingOverwrite() throws Exception { public void testCreateSameMediumTwiceWhenUsingOverwrite() throws Exception {
String path = System.getProperty("user.home") + "/jclouds-virtualbox-test/test-medium-3.vdi"; String path = System.getProperty("user.home") + "/jclouds-virtualbox-test/test-medium-3.vdi";
HardDisk hardDisk = new HardDisk(new DeviceDetails(0, 0, DeviceType.HardDisk), path, "vdi"); HardDisk hardDisk = HardDisk.builder().diskpath(path).controllerPort(0).deviceSlot(0).build();
new CreateMediumIfNotAlreadyExists(manager, true).apply(hardDisk); new CreateMediumIfNotAlreadyExists(manager, true).apply(hardDisk);
new CreateMediumIfNotAlreadyExists(manager, true).apply(hardDisk); new CreateMediumIfNotAlreadyExists(manager, true).apply(hardDisk);
manager.getVBox().findMedium(path, DeviceType.HardDisk); manager.getVBox().findMedium(path, DeviceType.HardDisk);

View File

@ -19,7 +19,6 @@
package org.jclouds.virtualbox.functions; package org.jclouds.virtualbox.functions;
import static com.google.common.collect.Iterables.getOnlyElement;
import static org.easymock.EasyMock.anyLong; import static org.easymock.EasyMock.anyLong;
import static org.easymock.EasyMock.eq; import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expect;
@ -29,7 +28,6 @@ import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify; import static org.easymock.classextension.EasyMock.verify;
import static org.testng.Assert.assertNotSame; import static org.testng.Assert.assertNotSame;
import org.jclouds.virtualbox.domain.DeviceDetails;
import org.jclouds.virtualbox.domain.HardDisk; import org.jclouds.virtualbox.domain.HardDisk;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -48,11 +46,14 @@ public class CreateMediumIfNotAlreadyExistsTest {
private String adminDiskPath; private String adminDiskPath;
private String diskFormat; private String diskFormat;
private String diskName;
@BeforeMethod @BeforeMethod
public void setUp() throws Exception { public void setUp() throws Exception {
adminDiskPath = "/Users/johndoe/jclouds-virtualbox-images/admin.vdi"; adminDiskPath = "/Users/johndoe/jclouds-virtualbox-images/admin.vdi";
diskFormat = "vdi"; diskFormat = "vdi";
diskName = "diskName";
} }
@Test @Test
@ -158,7 +159,7 @@ public class CreateMediumIfNotAlreadyExistsTest {
} }
private HardDisk createTestHardDisk() { private HardDisk createTestHardDisk() {
return new HardDisk(new DeviceDetails(0, 0, DeviceType.HardDisk), adminDiskPath, diskFormat); return HardDisk.builder().diskpath(adminDiskPath).controllerPort(0).deviceSlot(0).build();
} }
} }

View File

@ -19,28 +19,58 @@
package org.jclouds.virtualbox.functions.admin; package org.jclouds.virtualbox.functions.admin;
import org.testng.annotations.Test; import static org.easymock.EasyMock.expect;
import org.virtualbox_4_1.*; 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 java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static org.easymock.classextension.EasyMock.createMock; import org.jclouds.virtualbox.domain.HardDisk;
import static org.easymock.classextension.EasyMock.expect; import org.jclouds.virtualbox.domain.NatAdapter;
import static org.easymock.classextension.EasyMock.*; import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.util.PropertyUtils;
import org.testng.annotations.Test;
import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IMedium;
import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.IVirtualBox;
import org.virtualbox_4_1.StorageBus;
import org.virtualbox_4_1.VirtualBoxManager;
@Test(groups = "unit", testName = "UnregisterMachineIfExistsTest") @Test(groups = "unit", testName = "UnregisterMachineIfExistsTest")
public class UnregisterMachineIfExistsAndDeleteItsMediaTest { public class UnregisterMachineIfExistsAndDeleteItsMediaTest {
private String ideControllerName = "IDE Controller";
private CleanupMode mode = CleanupMode.Full;
private String vmName = "jclouds-image-example-machine-to-be-destroyed";
private String vmId = "jclouds-image-iso-unregister";
private String osTypeId = "";
@Test @Test
public void testUnregisterExistingMachine() throws Exception { public void testUnregisterExistingMachine() throws Exception {
VirtualBoxManager manager = createMock(VirtualBoxManager.class); VirtualBoxManager manager = createMock(VirtualBoxManager.class);
IVirtualBox vBox = createMock(IVirtualBox.class); IVirtualBox vBox = createMock(IVirtualBox.class);
IMachine registeredMachine = createMock(IMachine.class); IMachine registeredMachine = createMock(IMachine.class);
List<IMedium> mediums = Collections.emptyList(); IProgress progress = createNiceMock(IProgress.class);
List<IMedium> media = new ArrayList<IMedium>();
List<IMedium> mediums = Collections.unmodifiableList(media);
CleanupMode mode = CleanupMode.Full; String workingDir = PropertyUtils.getWorkingDirFromProperty();
String vmName = "jclouds-image-example-machine"; StorageController ideController = StorageController.builder().name(ideControllerName).bus(StorageBus.IDE)
.attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso")
.attachHardDisk(HardDisk.builder().diskpath(workingDir + "/testadmin.vdi").controllerPort(0).deviceSlot(1).build())
.attachISO(1, 1, workingDir + "/VBoxGuestAdditions_4.1.2.iso").build();
VmSpec vmSpecification = VmSpec.builder().id(vmId).name(vmName).memoryMB(512).osTypeId(osTypeId)
.controller(ideController)
.forceOverwrite(true)
.cleanUpMode(CleanupMode.Full)
.natNetworkAdapter(0, NatAdapter.builder().tcpRedirectRule("127.0.0.1", 2222, "", 22).build()).build();
expect(manager.getVBox()).andReturn(vBox).anyTimes(); expect(manager.getVBox()).andReturn(vBox).anyTimes();
expect(vBox.findMachine(vmName)).andReturn(registeredMachine); expect(vBox.findMachine(vmName)).andReturn(registeredMachine);
@ -48,9 +78,12 @@ public class UnregisterMachineIfExistsAndDeleteItsMediaTest {
expect(registeredMachine.unregister(mode)).andReturn(mediums); expect(registeredMachine.unregister(mode)).andReturn(mediums);
expectLastCall().anyTimes(); expectLastCall().anyTimes();
replay(manager, vBox, registeredMachine); expect(registeredMachine.delete(mediums)).andReturn(progress);
expectLastCall().anyTimes();
new UnregisterMachineIfExistsAndDeleteItsMedia(manager, mode).apply(vmName); replay(manager, vBox, registeredMachine, progress);
new UnregisterMachineIfExistsAndDeleteItsMedia(manager).apply(vmSpecification);
} }
} }

View File

@ -1,24 +1,5 @@
package org.jclouds.virtualbox.predicates; package org.jclouds.virtualbox.predicates;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.domain.Credentials;
import org.jclouds.net.IPSocket;
import org.jclouds.predicates.InetSocketAddressConnect;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.ExecutionType;
import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.functions.CreateAndInstallVm;
import org.jclouds.virtualbox.functions.LaunchMachineIfNotAlreadyRunning;
import org.jclouds.virtualbox.util.PropertyUtils;
import org.testng.annotations.Test;
import org.virtualbox_4_1.*;
import java.util.concurrent.TimeUnit;
import static org.jclouds.virtualbox.domain.ExecutionType.HEADLESS; import static org.jclouds.virtualbox.domain.ExecutionType.HEADLESS;
import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest; import static org.jclouds.virtualbox.experiment.TestUtils.computeServiceForLocalhostAndGuest;
import static org.jclouds.virtualbox.util.MachineUtils.applyForMachine; import static org.jclouds.virtualbox.util.MachineUtils.applyForMachine;
@ -26,6 +7,31 @@ import static org.jclouds.virtualbox.util.MachineUtils.lockSessionOnMachineAndAp
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import static org.virtualbox_4_1.LockType.Shared; import static org.virtualbox_4_1.LockType.Shared;
import java.util.concurrent.TimeUnit;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.domain.Credentials;
import org.jclouds.net.IPSocket;
import org.jclouds.predicates.InetSocketAddressConnect;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.virtualbox.BaseVirtualBoxClientLiveTest;
import org.jclouds.virtualbox.domain.ExecutionType;
import org.jclouds.virtualbox.domain.HardDisk;
import org.jclouds.virtualbox.domain.StorageController;
import org.jclouds.virtualbox.domain.VmSpec;
import org.jclouds.virtualbox.functions.CreateAndInstallVm;
import org.jclouds.virtualbox.functions.LaunchMachineIfNotAlreadyRunning;
import org.jclouds.virtualbox.util.PropertyUtils;
import org.testng.annotations.Test;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.StorageBus;
import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
@Test(groups = "live", singleThreaded = true, testName = "SshAvailableLiveTest") @Test(groups = "live", singleThreaded = true, testName = "SshAvailableLiveTest")
public class SshAvailableLiveTest extends BaseVirtualBoxClientLiveTest { public class SshAvailableLiveTest extends BaseVirtualBoxClientLiveTest {
@ -64,13 +70,13 @@ public class SshAvailableLiveTest extends BaseVirtualBoxClientLiveTest {
Predicate<IPSocket> socketTester = new RetryablePredicate<IPSocket>( Predicate<IPSocket> socketTester = new RetryablePredicate<IPSocket>(
new InetSocketAddressConnect(), 10, 1, TimeUnit.SECONDS); new InetSocketAddressConnect(), 10, 1, TimeUnit.SECONDS);
String vmId = "jclouds-image-iso-2"; String vmId = "jclouds-image-iso-2";
String isoName = "ubuntu-11.04-server-i386.iso";
String workingDir = PropertyUtils.getWorkingDirFromProperty(); String workingDir = PropertyUtils.getWorkingDirFromProperty();
StorageController ideController = StorageController.builder().name("IDE Controller").bus(StorageBus.IDE) StorageController ideController = StorageController.builder().name("IDE Controller").bus(StorageBus.IDE)
.attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso") .attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso")
.attachHardDisk(0, 1, workingDir + "/testadmin.vdi").build(); .attachHardDisk(HardDisk.builder().diskpath(workingDir + "/testadmin.vdi").controllerPort(0).deviceSlot(1).build()).build();
VmSpec vmSpecification = VmSpec.builder().id(vmId).name(vmName).osTypeId("") VmSpec vmSpecification = VmSpec.builder().id(vmId).name(vmName).osTypeId("")
.memoryMB(512)
.controller(ideController) .controller(ideController)
.forceOverwrite(true).build(); .forceOverwrite(true).build();