Merge branch 'refs/heads/functionalize'

This commit is contained in:
Mattias Holmqvist 2011-10-23 11:17:16 +02:00
commit 0cbbdb50ee
8 changed files with 371 additions and 90 deletions

View File

@ -21,30 +21,26 @@
package org.jclouds.virtualbox.compute; package org.jclouds.virtualbox.compute;
import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.base.Function;
import com.google.common.base.Predicate;
import java.io.InputStream; import com.google.common.base.Throwables;
import java.util.Collections; import com.google.inject.Singleton;
import java.util.Map;
import javax.inject.Inject;
import org.jclouds.compute.ComputeServiceAdapter; import org.jclouds.compute.ComputeServiceAdapter;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.Template; import org.jclouds.compute.domain.Template;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.location.suppliers.JustProvider; import org.jclouds.location.suppliers.JustProvider;
import org.jclouds.virtualbox.functions.admin.ImageFromYamlStream; import org.virtualbox_4_1.*;
import org.virtualbox_4_1.CleanupMode;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.SessionState;
import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Throwables; import javax.inject.Inject;
import com.google.inject.Singleton; import java.util.*;
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;
/** /**
* Defines the connection between the * Defines the connection between the
@ -58,9 +54,11 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
private final VirtualBoxManager manager; private final VirtualBoxManager manager;
private final JustProvider justProvider; private final JustProvider justProvider;
private Function<IMachine, Image> iMachineToImage;
@Inject @Inject
public VirtualBoxComputeServiceAdapter(VirtualBoxManager manager, JustProvider justProvider) { public VirtualBoxComputeServiceAdapter(VirtualBoxManager manager, JustProvider justProvider, Function<IMachine, Image> iMachineToImage) {
this.iMachineToImage = iMachineToImage;
this.manager = checkNotNull(manager, "manager"); this.manager = checkNotNull(manager, "manager");
this.justProvider = checkNotNull(justProvider, "justProvider"); this.justProvider = checkNotNull(justProvider, "justProvider");
} }
@ -81,12 +79,17 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
return manager.getVBox().getMachines(); return manager.getVBox().getMachines();
} }
@Override @Override
public Iterable<Image> listImages() { public Iterable<Image> listImages() {
InputStream is = getClass().getResourceAsStream("/testImages.yaml"); final Predicate<? super IMachine> imagePredicate = new Predicate<IMachine>() {
ImageFromYamlStream parser = new ImageFromYamlStream(); @Override
return parser.apply(is).asMap().values(); public boolean apply(@Nullable IMachine iMachine) {
} return iMachine.getName().startsWith(VIRTUALBOX_IMAGE_PREFIX);
}
};
final Iterable<IMachine> imageMachines = filter(manager.getVBox().getMachines(), imagePredicate);
return transform(imageMachines, iMachineToImage);
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override

View File

@ -42,6 +42,7 @@ import org.jclouds.location.Provider;
import org.jclouds.location.suppliers.OnlyLocationOrFirstZone; import org.jclouds.location.suppliers.OnlyLocationOrFirstZone;
import org.jclouds.virtualbox.compute.VirtualBoxComputeServiceAdapter; import org.jclouds.virtualbox.compute.VirtualBoxComputeServiceAdapter;
import org.jclouds.virtualbox.functions.IMachineToHardware; import org.jclouds.virtualbox.functions.IMachineToHardware;
import org.jclouds.virtualbox.functions.IMachineToImage;
import org.jclouds.virtualbox.functions.IMachineToNodeMetadata; import org.jclouds.virtualbox.functions.IMachineToNodeMetadata;
import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.MachineState; import org.virtualbox_4_1.MachineState;
@ -84,14 +85,12 @@ public class VirtualBoxComputeServiceContextModule extends ComputeServiceAdapter
}).to(IMachineToNodeMetadata.class); }).to(IMachineToNodeMetadata.class);
bind(new TypeLiteral<Function<Location, Location>>() { bind(new TypeLiteral<Function<Location, Location>>() {
}).to((Class) IdentityFunction.class); }).to((Class) IdentityFunction.class);
bind(new TypeLiteral<Function<IMachine, Hardware>>() {
}).to(IMachineToHardware.class);
bind(new TypeLiteral<Function<Image, Image>>() { bind(new TypeLiteral<Function<Image, Image>>() {
}).to((Class) IdentityFunction.class); }).to((Class) IdentityFunction.class);
/* bind(new TypeLiteral<Function<IMachine, Hardware>>() {
}).to(IMachineToHardware.class);
bind(new TypeLiteral<Function<IMachine, Image>>() { bind(new TypeLiteral<Function<IMachine, Image>>() {
}).to(IMachineToImage.class); }).to(IMachineToImage.class);
*/
bind(new TypeLiteral<Supplier<Location>>() { bind(new TypeLiteral<Supplier<Location>>() {
}).to(OnlyLocationOrFirstZone.class); }).to(OnlyLocationOrFirstZone.class);
} }

View File

@ -39,10 +39,10 @@ public class AddIDEControllerIfNotExists implements Function<IMachine, Void> {
} }
@Override @Override
public Void apply(IMachine arg0) { public Void apply(IMachine machine) {
try { try {
arg0.addStorageController(controllerName, StorageBus.IDE); machine.addStorageController(controllerName, StorageBus.IDE);
arg0.saveSettings(); machine.saveSettings();
} catch (VBoxException e) { } catch (VBoxException e) {
if (e.getMessage().indexOf("already exists") == -1) if (e.getMessage().indexOf("already exists") == -1)
throw e; throw e;

View File

@ -0,0 +1,63 @@
/*
* *
* * 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.DeviceType;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IMedium;
import org.virtualbox_4_1.VBoxException;
import javax.annotation.Nullable;
/**
* @author Mattias Holmqvist
*/
public class AttachDistroMediumToMachine implements Function<IMachine, Void> {
private final String controllerIDE;
private final IMedium distroMedium;
public AttachDistroMediumToMachine(String controllerIDE, IMedium distroMedium) {
this.controllerIDE = controllerIDE;
this.distroMedium = distroMedium;
}
@Override
public Void apply(@Nullable IMachine machine) {
try {
int controllerPort = 0;
int device = 0;
machine.attachDevice(controllerIDE, controllerPort, device, DeviceType.DVD, distroMedium);
machine.saveSettings();
} catch (VBoxException e) {
if (!alreadyAttached(e))
throw e;
}
return null;
}
private boolean alreadyAttached(VBoxException e) {
return e.getMessage().indexOf("is already attached to port") != -1;
}
}

View File

@ -87,8 +87,8 @@ public class IsoToIMachine implements Function<String, IMachine> {
@Inject @Inject
public IsoToIMachine(VirtualBoxManager manager, String adminDisk, String diskFormat, String settingsFile, public IsoToIMachine(VirtualBoxManager manager, String adminDisk, String diskFormat, String settingsFile,
String vmName, String osTypeId, String vmId, boolean forceOverwrite, String controllerIDE, String vmName, String osTypeId, String vmId, boolean forceOverwrite, String controllerIDE,
ComputeServiceContext context, String hostId, String guestId, Credentials credentials) { ComputeServiceContext context, String hostId, String guestId, Credentials credentials) {
super(); super();
this.manager = manager; this.manager = manager;
this.adminDisk = adminDisk; this.adminDisk = adminDisk;
@ -122,9 +122,9 @@ public class IsoToIMachine implements Function<String, IMachine> {
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() { lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() {
@Override @Override
public Void apply(IMachine arg0) { public Void apply(IMachine machine) {
arg0.setMemorySize(1024l); machine.setMemorySize(1024l);
arg0.saveSettings(); machine.saveSettings();
return null; return null;
} }
@ -133,31 +133,15 @@ public class IsoToIMachine implements Function<String, IMachine> {
// IDE Controller // IDE Controller
ensureMachineHasIDEControllerNamed(vmName, controllerIDE); ensureMachineHasIDEControllerNamed(vmName, controllerIDE);
// Distribution medium
ensureMachineHasAttachedDistroMedium(isoName, workingDir, controllerIDE);
// DISK // DISK
String adminDiskPath = workingDir + "/" + adminDisk; String adminDiskPath = workingDir + "/" + adminDisk;
if (new File(adminDiskPath).exists()) { if (new File(adminDiskPath).exists()) {
new File(adminDiskPath).delete(); new File(adminDiskPath).delete();
} }
final IMedium distroMedium = manager.getVBox().openMedium(workingDir + "/" + isoName, DVD, ReadOnly,
forceOverwrite);
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() {
@Override
public Void apply(IMachine arg0) {
try {
arg0.attachDevice(controllerIDE, 0, 0, DeviceType.DVD, distroMedium);
arg0.saveSettings();
} catch (VBoxException e) {
if (e.getMessage().indexOf("is already attached to port") == -1)
throw e;
}
return null;
}
});
// Create and attach hard disk // Create and attach hard disk
final IMedium hd = manager.getVBox().createHardDisk(diskFormat, adminDiskPath); final IMedium hd = manager.getVBox().createHardDisk(diskFormat, adminDiskPath);
long size = 4L * 1024L * 1024L * 1024L - 4L; long size = 4L * 1024L * 1024L * 1024L - 4L;
@ -167,9 +151,9 @@ public class IsoToIMachine implements Function<String, IMachine> {
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() { lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() {
@Override @Override
public Void apply(IMachine arg0) { public Void apply(IMachine machine) {
arg0.attachDevice(controllerIDE, 0, 1, DeviceType.HardDisk, hd); machine.attachDevice(controllerIDE, 0, 1, DeviceType.HardDisk, hd);
arg0.saveSettings(); machine.saveSettings();
return null; return null;
} }
@ -179,10 +163,10 @@ public class IsoToIMachine implements Function<String, IMachine> {
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() { lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() {
@Override @Override
public Void apply(IMachine arg0) { public Void apply(IMachine machine) {
arg0.getNetworkAdapter(0l).setAttachmentType(NAT); machine.getNetworkAdapter(0l).setAttachmentType(NAT);
arg0.getNetworkAdapter(0l).getNatDriver().addRedirect("guestssh", TCP, "127.0.0.1", 2222, "", 22); machine.getNetworkAdapter(0l).getNatDriver().addRedirect("guestssh", TCP, "127.0.0.1", 2222, "", 22);
arg0.getNetworkAdapter(0l).setEnabled(true); machine.getNetworkAdapter(0l).setEnabled(true);
return null; return null;
} }
@ -190,13 +174,13 @@ public class IsoToIMachine implements Function<String, IMachine> {
String guestAdditionsDvd = workingDir + "/VBoxGuestAdditions_4.1.2.iso"; String guestAdditionsDvd = workingDir + "/VBoxGuestAdditions_4.1.2.iso";
final IMedium guestAdditionsDvdMedium = manager.getVBox().openMedium(guestAdditionsDvd, DeviceType.DVD, final IMedium guestAdditionsDvdMedium = manager.getVBox().openMedium(guestAdditionsDvd, DeviceType.DVD,
AccessMode.ReadOnly, forceOverwrite); AccessMode.ReadOnly, forceOverwrite);
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() { lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() {
@Override @Override
public Void apply(IMachine arg0) { public Void apply(IMachine machine) {
arg0.attachDevice(controllerIDE, 1, 1, DeviceType.DVD, guestAdditionsDvdMedium); machine.attachDevice(controllerIDE, 1, 1, DeviceType.DVD, guestAdditionsDvdMedium);
return null; return null;
} }
@ -246,18 +230,34 @@ public class IsoToIMachine implements Function<String, IMachine> {
return vm; return vm;
} }
private void ensureMachineHasAttachedDistroMedium(String isoName, String workingDir, String controllerIDE) {
final String pathToIsoFile = checkFileExists(workingDir + "/" + isoName);
final IMedium distroMedium = manager.getVBox().openMedium(pathToIsoFile, DVD, ReadOnly, forceOverwrite);
lockMachineAndApply(manager, Write, vmName,
new AttachDistroMediumToMachine(
checkNotNull(controllerIDE, "controllerIDE"),
checkNotNull(distroMedium, "distroMedium")));
}
public static String checkFileExists(String filePath) {
if (new File(filePath).exists()) {
return filePath;
}
throw new IllegalStateException("File " + filePath + " does not exist.");
}
public void ensureMachineHasIDEControllerNamed(String vmName, String controllerIDE) { public void ensureMachineHasIDEControllerNamed(String vmName, String controllerIDE) {
lockMachineAndApply(manager, Write, checkNotNull(vmName, "vmName"), lockMachineAndApply(manager, Write, checkNotNull(vmName, "vmName"),
new AddIDEControllerIfNotExists(checkNotNull(controllerIDE, "controllerIDE"))); new AddIDEControllerIfNotExists(checkNotNull(controllerIDE, "controllerIDE")));
} }
public static <T> T lockMachineAndApply(VirtualBoxManager manager, final LockType type, final String machineId, public static <T> T lockMachineAndApply(VirtualBoxManager manager, final LockType type, final String machineId,
final Function<IMachine, T> function) { final Function<IMachine, T> function) {
return lockSessionOnMachineAndApply(manager, type, machineId, new Function<ISession, T>() { return lockSessionOnMachineAndApply(manager, type, machineId, new Function<ISession, T>() {
@Override @Override
public T apply(ISession arg0) { public T apply(ISession session) {
return function.apply(arg0.getMachine()); return function.apply(session.getMachine());
} }
@Override @Override
@ -270,7 +270,7 @@ public class IsoToIMachine implements Function<String, IMachine> {
} }
public static <T> T lockSessionOnMachineAndApply(VirtualBoxManager manager, LockType type, String machineId, public static <T> T lockSessionOnMachineAndApply(VirtualBoxManager manager, LockType type, String machineId,
Function<ISession, T> function) { Function<ISession, T> function) {
try { try {
ISession session = manager.getSessionObject(); ISession session = manager.getSessionObject();
IMachine immutableMachine = manager.getVBox().findMachine(machineId); IMachine immutableMachine = manager.getVBox().findMachine(machineId);
@ -282,17 +282,17 @@ public class IsoToIMachine implements Function<String, IMachine> {
} }
} catch (VBoxException e) { } catch (VBoxException e) {
throw new RuntimeException(String.format("error applying %s to %s with %s lock: %s", function, machineId, throw new RuntimeException(String.format("error applying %s to %s with %s lock: %s", function, machineId,
type, e.getMessage()), e); type, e.getMessage()), e);
} }
} }
private String defaultInstallSequence() { private String defaultInstallSequence() {
return "<Esc><Esc><Enter> " return "<Esc><Esc><Enter> "
+ "/install/vmlinuz noapic preseed/url=http://10.0.2.2:8080/src/test/resources/preseed.cfg " + "/install/vmlinuz noapic preseed/url=http://10.0.2.2:8080/src/test/resources/preseed.cfg "
+ "debian-installer=en_US auto locale=en_US kbd-chooser/method=us " + "hostname=" + vmName + " " + "debian-installer=en_US auto locale=en_US kbd-chooser/method=us " + "hostname=" + vmName + " "
+ "fb=false debconf/frontend=noninteractive " + "fb=false debconf/frontend=noninteractive "
+ "keyboard-configuration/layout=USA keyboard-configuration/variant=USA console-setup/ask_detect=false " + "keyboard-configuration/layout=USA keyboard-configuration/variant=USA console-setup/ask_detect=false "
+ "initrd=/install/initrd.gz -- <Enter>"; + "initrd=/install/initrd.gz -- <Enter>";
} }
private void sendKeyboardSequence(String keyboardSequence) { private void sendKeyboardSequence(String keyboardSequence) {

View File

@ -19,16 +19,20 @@
package org.jclouds.virtualbox.compute; package org.jclouds.virtualbox.compute;
import static org.testng.Assert.assertEquals; import com.google.common.base.Function;
import static org.testng.Assert.assertFalse; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import java.net.URI; import com.google.common.collect.Maps;
import java.util.Map; import com.google.inject.Guice;
import org.jclouds.compute.config.BaseComputeServiceContextModule;
import org.jclouds.compute.domain.ExecResponse; import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.Template; import org.jclouds.compute.domain.Template;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.json.Json;
import org.jclouds.json.config.GsonModule;
import org.jclouds.location.suppliers.JustProvider; import org.jclouds.location.suppliers.JustProvider;
import org.jclouds.net.IPSocket; import org.jclouds.net.IPSocket;
import org.jclouds.ssh.SshClient; import org.jclouds.ssh.SshClient;
@ -40,21 +44,28 @@ import org.testng.annotations.Test;
import org.virtualbox_4_1.IMachine; import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.VirtualBoxManager; import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.collect.ImmutableSet; import java.net.URI;
import com.google.common.collect.Iterables; import java.util.Map;
import com.google.common.collect.Maps;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
@Test(groups = "live", singleThreaded = true, testName = "VirtualBoxComputeServiceAdapterLiveTest") @Test(groups = "live", singleThreaded = true, testName = "VirtualBoxComputeServiceAdapterLiveTest")
public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClientLiveTest { public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClientLiveTest {
private VirtualBoxComputeServiceAdapter adapter; private VirtualBoxComputeServiceAdapter adapter;
private IMachine machine; private IMachine machine;
private final Map<OsFamily, Map<String, String>> osVersionMap = new BaseComputeServiceContextModule() {
}.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule())
.getInstance(Json.class));
@BeforeGroups(groups = { "live" }) @BeforeGroups(groups = {"live"})
public void setupClient() { public void setupClient() {
super.setupClient(); super.setupClient();
adapter = new VirtualBoxComputeServiceAdapter(getManager(), final VirtualBoxManager manager = getManager();
new JustProvider(ImmutableSet.<String> of(), provider, URI.create(endpoint))); Function<IMachine, Image> iMachineToImage = new IMachineToImage(manager, osVersionMap);
adapter = new VirtualBoxComputeServiceAdapter(manager,
new JustProvider(ImmutableSet.<String>of(), provider, URI.create(endpoint)), iMachineToImage);
} }
protected VirtualBoxManager getManager() { protected VirtualBoxManager getManager() {
@ -78,7 +89,7 @@ public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClien
// check other things, like cpu correct, mem correct, image/os is correct // check other things, like cpu correct, mem correct, image/os is correct
// (as possible) // (as possible)
assert credentialStore.containsKey("node#" + machine.getId()) : "credentials to log into machine not found " assert credentialStore.containsKey("node#" + machine.getId()) : "credentials to log into machine not found "
+ machine; + machine;
// TODO: what's the IP address? // TODO: what's the IP address?
// assert InetAddresses.isInetAddress(machine.getPrimaryBackendIpAddress()) : machine; // assert InetAddresses.isInetAddress(machine.getPrimaryBackendIpAddress()) : machine;
doConnectViaSsh(machine, credentialStore.get("node#" + machine.getId())); doConnectViaSsh(machine, credentialStore.get("node#" + machine.getId()));
@ -86,7 +97,7 @@ public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClien
protected void doConnectViaSsh(IMachine machine, Credentials creds) { protected void doConnectViaSsh(IMachine machine, Credentials creds) {
SshClient ssh = context.utils().sshFactory() SshClient ssh = context.utils().sshFactory()
.create(new IPSocket("//TODO", 22), creds); .create(new IPSocket("//TODO", 22), creds);
try { try {
ssh.connect(); ssh.connect();
ExecResponse hello = ssh.exec("echo hello"); ExecResponse hello = ssh.exec("echo hello");
@ -106,6 +117,7 @@ public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClien
assertFalse(Iterables.isEmpty(profiles)); assertFalse(Iterables.isEmpty(profiles));
// check state; // check state;
} }
@Test @Test
public void testListImages() { public void testListImages() {
Iterable<Image> iMageIterable = adapter.listImages(); Iterable<Image> iMageIterable = adapter.listImages();

View File

@ -0,0 +1,95 @@
/*
* *
* * 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.compute;
import com.google.common.base.Function;
import com.google.common.collect.Iterators;
import com.google.inject.Guice;
import org.easymock.EasyMock;
import org.jclouds.compute.config.BaseComputeServiceContextModule;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.json.Json;
import org.jclouds.json.config.GsonModule;
import org.jclouds.location.suppliers.JustProvider;
import org.jclouds.virtualbox.functions.IMachineToImage;
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.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static org.easymock.classextension.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createNiceMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_IMAGE_PREFIX;
import static org.testng.Assert.assertEquals;
@Test(groups = "unit")
public class VirtualBoxComputeServiceAdapterTest {
Map<OsFamily, Map<String, String>> osMap = new BaseComputeServiceContextModule() {
}.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule())
.getInstance(Json.class));
@Test
public void testListImages() throws Exception {
VirtualBoxManager manager = createNiceMock(VirtualBoxManager.class);
JustProvider justProvider = createNiceMock(JustProvider.class);
IVirtualBox vBox = createNiceMock(IVirtualBox.class);
IGuestOSType osType = createNiceMock(IGuestOSType.class);
List<IMachine> machines = new ArrayList<IMachine>();
IMachine imageMachine = createNiceMock(IMachine.class);
IMachine clonedMachine = createNiceMock(IMachine.class);
machines.add(imageMachine);
machines.add(clonedMachine);
expect(clonedMachine.getName()).andReturn("My Linux Node");
expect(clonedMachine.getDescription()).andReturn("My Linux Node");
expect(imageMachine.getName()).andReturn(VIRTUALBOX_IMAGE_PREFIX + "ubuntu-10.04");
expect(imageMachine.getDescription()).andReturn(VIRTUALBOX_IMAGE_PREFIX + "ubuntu-10.04");
expect(manager.getVBox()).andReturn(vBox).anyTimes();
expect(vBox.getMachines()).andReturn(machines).anyTimes();
expect(vBox.getGuestOSType(EasyMock.<String>anyObject())).andReturn(osType).anyTimes();
expect(osType.getDescription()).andReturn("Ubuntu 10.04").anyTimes();
expect(osType.getIs64Bit()).andReturn(true).anyTimes();
replay(manager, justProvider, vBox, clonedMachine, imageMachine, osType);
Function<IMachine, Image> iMachineToImage = new IMachineToImage(manager, osMap);
VirtualBoxComputeServiceAdapter adapter = new VirtualBoxComputeServiceAdapter(manager, justProvider, iMachineToImage);
Iterator<Image> iterator = adapter.listImages().iterator();
Image image = Iterators.getOnlyElement(iterator);
assertEquals(image.getDescription(), VIRTUALBOX_IMAGE_PREFIX + "ubuntu-10.04");
}
}

View File

@ -0,0 +1,109 @@
/*
* *
* * 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.DeviceType;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IMedium;
import org.virtualbox_4_1.VBoxException;
import static org.easymock.classextension.EasyMock.*;
/**
* @author Mattias Holmqvist
*/
@Test(groups = "unit", testName = "AttachDistroMediumToMachineTest")
public class AttachDistroMediumToMachineTest {
@Test
public void testAttachDistroMedium() throws Exception {
String controllerIDE = "IDE Controller";
IMedium distroMedium = createNiceMock(IMedium.class);
IMachine machine = createMock(IMachine.class);
machine.saveSettings();
machine.attachDevice(controllerIDE, 0, 0, DeviceType.DVD, distroMedium);
replay(machine, distroMedium);
new AttachDistroMediumToMachine(controllerIDE, distroMedium).apply(machine);
verify(machine);
}
@Test
public void testAcceptAlreadyAttachedDistroMedium() throws Exception {
String controllerIDE = "IDE Controller";
IMedium distroMedium = createNiceMock(IMedium.class);
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 attached to 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.attachDevice(controllerIDE, 0, 0, DeviceType.DVD, distroMedium);
expectLastCall().andThrow(isoAttachedException);
replay(machine, distroMedium);
new AttachDistroMediumToMachine(controllerIDE, distroMedium).apply(machine);
verify(machine);
}
@Test(expectedExceptions = VBoxException.class)
public void testFailOnOtherVBoxErrors() throws Exception {
String controllerIDE = "IDE Controller";
IMedium distroMedium = createNiceMock(IMedium.class);
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.attachDevice(controllerIDE, 0, 0, DeviceType.DVD, distroMedium);
expectLastCall().andThrow(isoAttachedException);
replay(machine, distroMedium);
new AttachDistroMediumToMachine(controllerIDE, distroMedium).apply(machine);
}
}