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;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.InputStream;
import java.util.Collections;
import java.util.Map;
import javax.inject.Inject;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Throwables;
import com.google.inject.Singleton;
import org.jclouds.compute.ComputeServiceAdapter;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.Template;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.location.suppliers.JustProvider;
import org.jclouds.virtualbox.functions.admin.ImageFromYamlStream;
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 org.virtualbox_4_1.*;
import com.google.common.base.Throwables;
import com.google.inject.Singleton;
import javax.inject.Inject;
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
@ -58,9 +54,11 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
private final VirtualBoxManager manager;
private final JustProvider justProvider;
private Function<IMachine, Image> iMachineToImage;
@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.justProvider = checkNotNull(justProvider, "justProvider");
}
@ -83,9 +81,14 @@ public class VirtualBoxComputeServiceAdapter implements ComputeServiceAdapter<IM
@Override
public Iterable<Image> listImages() {
InputStream is = getClass().getResourceAsStream("/testImages.yaml");
ImageFromYamlStream parser = new ImageFromYamlStream();
return parser.apply(is).asMap().values();
final Predicate<? super IMachine> imagePredicate = new Predicate<IMachine>() {
@Override
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")

View File

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

View File

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

@ -122,9 +122,9 @@ public class IsoToIMachine implements Function<String, IMachine> {
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() {
@Override
public Void apply(IMachine arg0) {
arg0.setMemorySize(1024l);
arg0.saveSettings();
public Void apply(IMachine machine) {
machine.setMemorySize(1024l);
machine.saveSettings();
return null;
}
@ -133,31 +133,15 @@ public class IsoToIMachine implements Function<String, IMachine> {
// IDE Controller
ensureMachineHasIDEControllerNamed(vmName, controllerIDE);
// Distribution medium
ensureMachineHasAttachedDistroMedium(isoName, workingDir, controllerIDE);
// DISK
String adminDiskPath = workingDir + "/" + adminDisk;
if (new File(adminDiskPath).exists()) {
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
final IMedium hd = manager.getVBox().createHardDisk(diskFormat, adminDiskPath);
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>() {
@Override
public Void apply(IMachine arg0) {
arg0.attachDevice(controllerIDE, 0, 1, DeviceType.HardDisk, hd);
arg0.saveSettings();
public Void apply(IMachine machine) {
machine.attachDevice(controllerIDE, 0, 1, DeviceType.HardDisk, hd);
machine.saveSettings();
return null;
}
@ -179,10 +163,10 @@ public class IsoToIMachine implements Function<String, IMachine> {
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() {
@Override
public Void apply(IMachine arg0) {
arg0.getNetworkAdapter(0l).setAttachmentType(NAT);
arg0.getNetworkAdapter(0l).getNatDriver().addRedirect("guestssh", TCP, "127.0.0.1", 2222, "", 22);
arg0.getNetworkAdapter(0l).setEnabled(true);
public Void apply(IMachine machine) {
machine.getNetworkAdapter(0l).setAttachmentType(NAT);
machine.getNetworkAdapter(0l).getNatDriver().addRedirect("guestssh", TCP, "127.0.0.1", 2222, "", 22);
machine.getNetworkAdapter(0l).setEnabled(true);
return null;
}
@ -195,8 +179,8 @@ public class IsoToIMachine implements Function<String, IMachine> {
lockMachineAndApply(manager, Write, vmName, new Function<IMachine, Void>() {
@Override
public Void apply(IMachine arg0) {
arg0.attachDevice(controllerIDE, 1, 1, DeviceType.DVD, guestAdditionsDvdMedium);
public Void apply(IMachine machine) {
machine.attachDevice(controllerIDE, 1, 1, DeviceType.DVD, guestAdditionsDvdMedium);
return null;
}
@ -246,6 +230,22 @@ public class IsoToIMachine implements Function<String, IMachine> {
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) {
lockMachineAndApply(manager, Write, checkNotNull(vmName, "vmName"),
new AddIDEControllerIfNotExists(checkNotNull(controllerIDE, "controllerIDE")));
@ -256,8 +256,8 @@ public class IsoToIMachine implements Function<String, IMachine> {
return lockSessionOnMachineAndApply(manager, type, machineId, new Function<ISession, T>() {
@Override
public T apply(ISession arg0) {
return function.apply(arg0.getMachine());
public T apply(ISession session) {
return function.apply(session.getMachine());
}
@Override

View File

@ -19,16 +19,20 @@
package org.jclouds.virtualbox.compute;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import java.net.URI;
import java.util.Map;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.inject.Guice;
import org.jclouds.compute.config.BaseComputeServiceContextModule;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.Template;
import org.jclouds.compute.reference.ComputeServiceConstants;
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.net.IPSocket;
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.VirtualBoxManager;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import java.net.URI;
import java.util.Map;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
@Test(groups = "live", singleThreaded = true, testName = "VirtualBoxComputeServiceAdapterLiveTest")
public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClientLiveTest {
private VirtualBoxComputeServiceAdapter adapter;
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() {
super.setupClient();
adapter = new VirtualBoxComputeServiceAdapter(getManager(),
new JustProvider(ImmutableSet.<String> of(), provider, URI.create(endpoint)));
final VirtualBoxManager manager = getManager();
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() {
@ -106,6 +117,7 @@ public class VirtualBoxComputeServiceAdapterLiveTest extends BaseVirtualBoxClien
assertFalse(Iterables.isEmpty(profiles));
// check state;
}
@Test
public void testListImages() {
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);
}
}