issue 384: VirtualboxAdminTest can clone multiple vms from the admin node in linked mode - still issues with bridged network, so right now NAT is used

This commit is contained in:
andreaturli 2011-09-14 00:52:28 +01:00
parent 78b89d10d2
commit 4b62c4a022
2 changed files with 103 additions and 187 deletions

View File

@ -162,7 +162,7 @@ public class VirtualboxAdministrationKickstartLiveTest {
"VBoxGuestAdditions_" + majorVersion + ".iso"); "VBoxGuestAdditions_" + majorVersion + ".iso");
gaIsoUrl = URI.create(System.getProperty("test." + provider gaIsoUrl = URI.create(System.getProperty("test." + provider
+ ".gaIsoUrl", "http://download.virtualbox.org/virtualbox/" + ".gaIsoUrl", "http://download.virtualbox.org/virtualbox/"
+ majorVersion + "/" + gaIsoName)); + majorVersion + "/" + gaIsoName));
distroIsoName = System.getProperty("test." + provider distroIsoName = System.getProperty("test." + provider
+ ".distroIsoName", "ubuntu-11.04-server-i386.iso"); + ".distroIsoName", "ubuntu-11.04-server-i386.iso");
@ -282,9 +282,9 @@ public class VirtualboxAdministrationKickstartLiveTest {
hostId, hostId,
vboxwebsrv, vboxwebsrv,
runAsRoot(false).wrapInInitScript(false) runAsRoot(false).wrapInInitScript(false)
.blockOnPort(endpoint.getPort(), 10) .blockOnPort(endpoint.getPort(), 10)
.blockOnComplete(false) .blockOnComplete(false)
.nameTask("vboxwebsrv")); .nameTask("vboxwebsrv"));
} }
protected boolean isOSX(String id) { protected boolean isOSX(String id) {
@ -296,7 +296,7 @@ public class VirtualboxAdministrationKickstartLiveTest {
return context.getComputeService().getNodeMetadata(id) return context.getComputeService().getNodeMetadata(id)
.getOperatingSystem().getDescription().equals("ubuntu/11.04"); .getOperatingSystem().getDescription().equals("ubuntu/11.04");
} }
@BeforeMethod @BeforeMethod
protected void setupManager() { protected void setupManager() {
manager.connect(endpoint.toASCIIString(), identity, credential); manager.connect(endpoint.toASCIIString(), identity, credential);
@ -304,7 +304,7 @@ public class VirtualboxAdministrationKickstartLiveTest {
@AfterMethod @AfterMethod
protected void disconnectAndClenaupManager() throws RemoteException, protected void disconnectAndClenaupManager() throws RemoteException,
MalformedURLException { MalformedURLException {
manager.disconnect(); manager.disconnect();
manager.cleanup(); manager.cleanup();
} }
@ -354,7 +354,7 @@ public class VirtualboxAdministrationKickstartLiveTest {
machine.lockMachine(session, LockType.Write); machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine(); IMachine mutable = session.getMachine();
mutable.attachDevice(controllerIDE, 0, 0, DeviceType.DVD, distroMedium); mutable.attachDevice(controllerIDE, 0, 0, DeviceType.DVD, distroMedium);
mutable.saveSettings(); // write settings to xml mutable.saveSettings();
session.unlockMachine(); session.unlockMachine();
assertEquals(distroMedium.getId().equals(""), false); assertEquals(distroMedium.getId().equals(""), false);
} }
@ -392,9 +392,9 @@ public class VirtualboxAdministrationKickstartLiveTest {
mutable.getNetworkAdapter(new Long(0)).setAttachmentType( mutable.getNetworkAdapter(new Long(0)).setAttachmentType(
NetworkAttachmentType.NAT); NetworkAttachmentType.NAT);
machine.getNetworkAdapter(new Long(0)) machine.getNetworkAdapter(new Long(0))
.getNatDriver() .getNatDriver()
.addRedirect("guestssh", NATProtocol.TCP, "127.0.0.1", 2222, .addRedirect("guestssh", NATProtocol.TCP, "127.0.0.1", 2222,
"", 22); "", 22);
mutable.getNetworkAdapter(new Long(0)).setEnabled(true); mutable.getNetworkAdapter(new Long(0)).setEnabled(true);
mutable.saveSettings(); mutable.saveSettings();
@ -402,20 +402,20 @@ public class VirtualboxAdministrationKickstartLiveTest {
} }
@Test(dependsOnMethods = "testConfigureNIC") @Test(dependsOnMethods = "testConfigureNIC")
public void testAttachGuestAdditions() { public void testAttachGuestAdditions() {
ISession session = manager.getSessionObject(); ISession session = manager.getSessionObject();
IMachine machine = manager.getVBox().findMachine(vmName); IMachine machine = manager.getVBox().findMachine(vmName);
IMedium distroMedium = manager.getVBox().openMedium(guestAdditionsDvd, DeviceType.DVD,
AccessMode.ReadOnly, forceOverwrite);
machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
mutable.attachDevice(controllerIDE, 1, 1, DeviceType.DVD, distroMedium);
mutable.saveSettings(); // write settings to xml
session.unlockMachine();
assertEquals(distroMedium.getId().equals(""), false);
}
IMedium distroMedium = manager.getVBox().openMedium(guestAdditionsDvd, DeviceType.DVD,
AccessMode.ReadOnly, forceOverwrite);
machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
mutable.attachDevice(controllerIDE, 1, 1, DeviceType.DVD, distroMedium);
mutable.saveSettings(); // write settings to xml
session.unlockMachine();
assertEquals(distroMedium.getId().equals(""), false);
}
@Test(dependsOnMethods = "testAttachGuestAdditions") @Test(dependsOnMethods = "testAttachGuestAdditions")
public void testStartVirtualMachine() throws InterruptedException { public void testStartVirtualMachine() throws InterruptedException {
@ -430,37 +430,35 @@ public class VirtualboxAdministrationKickstartLiveTest {
} }
sendKeyboardSequence(keyboardSequence); sendKeyboardSequence(keyboardSequence);
// test if the sshd on the guest is ready and meanwhile apply AdminAccess
boolean sshDeamonIsRunning = false;
while(!sshDeamonIsRunning) {
try {
AdminAccess.standard().init(new DefaultConfiguration()).render(OsFamily.UNIX);
if(runScriptOnNode(guestId, "id").getExitCode() == 0)
sshDeamonIsRunning = true;
} catch(SshException e) {
System.err.println("connection reset");
}
}
// test if the sshd on the guest is ready and meanwhile apply AdminAccess
boolean sshDeamonIsRunning = false;
while(!sshDeamonIsRunning) {
try {
AdminAccess.standard().init(new DefaultConfiguration()).render(OsFamily.UNIX);
if(runScriptOnNode(guestId, "id").getExitCode() == 0)
sshDeamonIsRunning = true;
} catch(SshException e) {
System.err.println("connection reset");
}
}
} }
@Test(dependsOnMethods = "testStartVirtualMachine") @Test(dependsOnMethods = "testStartVirtualMachine")
public void testConfigureGuestAdditions() { public void testConfigureGuestAdditions() {
// TODO generalize: at the moment we are usign apt-get not the guestadditions.iso attached // TODO generalize: at the moment we are usign apt-get not the guestadditions.iso attached
//if(isUbuntu(guestId)) { //if(isUbuntu(guestId)) {
// Configure your system for building kernel modules by running // Configure your system for building kernel modules by running
runScriptOnNode(guestId, "m-a prepare -i", wrapInInitScript(true)); runScriptOnNode(guestId, "m-a prepare -i", wrapInInitScript(true));
runScriptOnNode(guestId, runScriptOnNode(guestId,
"mount -o loop /dev/dvd /media/cdrom"); "mount -o loop /dev/dvd /media/cdrom");
runScriptOnNode(guestId, "sh /media/cdrom/VBoxLinuxAdditions.run"); ExecResponse response = runScriptOnNode(guestId, "sh /media/cdrom/VBoxLinuxAdditions.run");
response.getExitCode();
// for Debian based OS // for Debian based OS
runScriptOnNode(guestId, "rm /etc/udev/rules.d/70-persistent-net.rules"); runScriptOnNode(guestId, "rm /etc/udev/rules.d/70-persistent-net.rules");
//} //}
} }
@ -484,16 +482,16 @@ public class VirtualboxAdministrationKickstartLiveTest {
while (!machine.getSessionState().equals(SessionState.Unlocked)) { while (!machine.getSessionState().equals(SessionState.Unlocked)) {
try { try {
System.out System.out
.println("waiting for unlocking session - session state: " .println("waiting for unlocking session - session state: "
+ machine.getSessionState()); + machine.getSessionState());
Thread.sleep(1000); Thread.sleep(1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace();
} }
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
e.printStackTrace();
} }
} }
@ -504,7 +502,7 @@ public class VirtualboxAdministrationKickstartLiveTest {
machine.lockMachine(session, LockType.Write); machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine(); IMachine mutable = session.getMachine();
mutable.getNetworkAdapter(new Long(0)).getNatDriver() mutable.getNetworkAdapter(new Long(0)).getNatDriver()
.removeRedirect("guestssh"); .removeRedirect("guestssh");
// detach disk from controller // detach disk from controller
mutable.detachDevice(controllerIDE, 0, 0); mutable.detachDevice(controllerIDE, 0, 0);
mutable.saveSettings(); mutable.saveSettings();
@ -567,41 +565,35 @@ public class VirtualboxAdministrationKickstartLiveTest {
for (String word : converted.split(" ")) { for (String word : converted.split(" ")) {
sb.append("vboxmanage controlvm " + vmName sb.append("vboxmanage controlvm " + vmName
+ " keyboardputscancode " + word + "; "); + " keyboardputscancode " + word + "; ");
if (word.endsWith(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get("<Enter>"))) { if (word.endsWith(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get("<Enter>"))) {
runScriptOnNode(hostId, sb.toString(), runAsRoot(false) runScriptOnNode(hostId, sb.toString(), runAsRoot(false)
.wrapInInitScript(false)); .wrapInInitScript(false));
sb.delete(0, sb.length()-1); sb.delete(0, sb.length()-1);
} }
if (word.endsWith(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get("<Return>"))) { if (word.endsWith(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.get("<Return>"))) {
runScriptOnNode(hostId, sb.toString(), runAsRoot(false) runScriptOnNode(hostId, sb.toString(), runAsRoot(false)
.wrapInInitScript(false)); .wrapInInitScript(false));
sb.delete(0, sb.length()-1); sb.delete(0, sb.length()-1);
} }
} }
} }
} }
private String stringToKeycode(String s) { private String stringToKeycode(String s) {
StringBuilder keycodes = new StringBuilder(); StringBuilder keycodes = new StringBuilder();
//for (String specialButton : KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.keySet()) { if (s.startsWith("<")) {
//System.out.println("specialbutton " + specialButton); String[] specials = s.split("<");
if (s.startsWith("<")) { for (int i = 1; i < specials.length; i++) {
String[] specials = s.split("<"); keycodes.append(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP
for (int i = 1; i < specials.length; i++) { .get("<" + specials[i]) + " ");
keycodes.append(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP }
.get("<" + specials[i]) + " ");
}
System.out.println("keycodes: " + keycodes);
return keycodes.toString(); return keycodes.toString();
} }
int i = 0; int i = 0;
while (i < s.length()) { while (i < s.length()) {

View File

@ -23,10 +23,7 @@ import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot;
import static org.jclouds.compute.options.RunScriptOptions.Builder.wrapInInitScript; import static org.jclouds.compute.options.RunScriptOptions.Builder.wrapInInitScript;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.rmi.RemoteException; import java.rmi.RemoteException;
@ -47,22 +44,16 @@ import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.virtualbox_4_1.AccessMode; import org.virtualbox_4_1.CloneMode;
import org.virtualbox_4_1.CloneOptions; import org.virtualbox_4_1.CloneOptions;
import org.virtualbox_4_1.DeviceType;
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;
import org.virtualbox_4_1.ISession; import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.LockType; import org.virtualbox_4_1.LockType;
import org.virtualbox_4_1.MachineState; import org.virtualbox_4_1.MachineState;
import org.virtualbox_4_1.MediumType;
import org.virtualbox_4_1.NetworkAdapterType;
import org.virtualbox_4_1.NetworkAttachmentType;
import org.virtualbox_4_1.SessionState; import org.virtualbox_4_1.SessionState;
import org.virtualbox_4_1.StorageBus;
import org.virtualbox_4_1.VirtualBoxManager; import org.virtualbox_4_1.VirtualBoxManager;
import org.virtualbox_4_1.jaxws.MediumVariant;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
@ -90,28 +81,16 @@ public class VirtualboxLiveTest {
protected String controller; protected String controller;
protected String diskFormat; protected String diskFormat;
protected String settingsFile; // Fully qualified path where the settings protected String settingsFile;
// file should be created, or NULL for a protected String osTypeId;
// default protected String vmId;
// folder and file based on the name argument (see composeMachineFilename()). protected boolean forceOverwrite;
protected String osTypeId; // Guest OS Type ID.
protected String vmId; // Machine UUID (optional).
protected boolean forceOverwrite; // If true, an existing machine settings
// file will be overwritten.
protected String workingDir; protected String workingDir;
protected String clonedDiskPath; protected String clonedDiskPath;
protected String format = "vdi";
// Create disk If the @a format attribute is empty or null then the default
// storage format specified by ISystemProperties#defaultHardDiskFormat
String format = "vdi";
protected int numberOfVirtualMachine; protected int numberOfVirtualMachine;
protected String originalDisk; protected String originalDisk;
private String originalDiskPath;
private String clonedDisk; private String clonedDisk;
private IMedium clonedHd;
private ComputeServiceContext context; private ComputeServiceContext context;
private String hostId = "host"; private String hostId = "host";
@ -120,6 +99,8 @@ public class VirtualboxLiveTest {
private String minorVersion; private String minorVersion;
private String apiVersion; private String apiVersion;
private String adminNodeName; private String adminNodeName;
private String snapshotDescription;
private String originalDiskPath;
protected Logger logger() { protected Logger logger() {
return context.utils().loggerFactory().getLogger("jclouds.compute"); return context.utils().loggerFactory().getLogger("jclouds.compute");
@ -162,9 +143,9 @@ public class VirtualboxLiveTest {
clonedDisk = System.getProperty("test." + provider + ".clonedDisk", "clone.vdi"); clonedDisk = System.getProperty("test." + provider + ".clonedDisk", "clone.vdi");
clonedDiskPath = workingDir + File.separator + clonedDisk; clonedDiskPath = workingDir + File.separator + clonedDisk;
snapshotDescription = System.getProperty("test." + provider + "snapshotdescription", "jclouds-virtualbox-snaphot");
numberOfVirtualMachine = Integer.parseInt(checkNotNull(System.getProperty("test." + provider numberOfVirtualMachine = Integer.parseInt(checkNotNull(System.getProperty("test." + provider
+ ".numberOfVirtualMachine", "1"))); + ".numberOfVirtualMachine", "3")));
} }
@BeforeGroups(groups = "live") @BeforeGroups(groups = "live")
@ -191,83 +172,52 @@ public class VirtualboxLiveTest {
} }
@Test @Test
public void testCloneHardDisk() { public void testTakeAdminNodeSnapshot() {
IMedium hd = manager.getVBox().openMedium(originalDiskPath, DeviceType.HardDisk, AccessMode.ReadWrite, forceOverwrite); ISession session = manager.getSessionObject();
if (!new File(clonedDiskPath).exists()) { IMachine adminNode = manager.getVBox().findMachine(adminNodeName);
clonedHd = manager.getVBox().createHardDisk(diskFormat, clonedDiskPath); adminNode.lockMachine(session, LockType.Write);
IProgress cloning = hd.cloneTo(clonedHd, new Long( if(adminNode.getCurrentSnapshot() == null || !adminNode.getCurrentSnapshot().getDescription().equals(snapshotDescription)) {
MediumVariant.STANDARD.ordinal()), null); manager.getSessionObject().getConsole().takeSnapshot(adminNode.getId(), snapshotDescription);
cloning.waitForCompletion(-1);
} else
clonedHd = manager.getVBox().openMedium(clonedDiskPath, DeviceType.HardDisk, AccessMode.ReadWrite, forceOverwrite);
clonedHd.setType(MediumType.MultiAttach);
assertEquals(clonedHd.getId().equals(""), false);
}
@Test(dependsOnMethods = "testCloneHardDisk")
public void testStartVirtualMachines() throws InterruptedException {
//IMedium clonedHd = cloneDisk(MediumType.MultiAttach);
List<CloneOptions> options = new ArrayList<CloneOptions>();
options.add(CloneOptions.KeepDiskNames);
for (int i = 1; i < numberOfVirtualMachine + 1; i++) {
createVirtualMachine(i);
} }
} }
private void createVirtualMachine(int i) { @Test(dependsOnMethods = "testTakeAdminNodeSnapshot")
public void testStartAndValidateVirtualMachines() throws InterruptedException {
for (int i = 1; i < numberOfVirtualMachine + 1; i++) {
createAndLaunchVirtualMachine(i);
}
}
private void createAndLaunchVirtualMachine(int i) throws InterruptedException {
String instanceName = vmName + "_" + i; String instanceName = vmName + "_" + i;
IMachine adminNode = manager.getVBox().findMachine(adminNodeName);
IMachine clonedVM = manager.getVBox().createMachine(settingsFile, instanceName, osTypeId, vmId, forceOverwrite);
List<CloneOptions> options = new ArrayList<CloneOptions>();
options.add(CloneOptions.Link);
IProgress progress = adminNode.getCurrentSnapshot().getMachine().cloneTo(clonedVM,CloneMode.MachineState , options);
if(progress.getCompleted())
logger().debug("clone done");
IMachine newVM = manager.getVBox().createMachine(settingsFile, instanceName, osTypeId, vmId, forceOverwrite); manager.getVBox().registerMachine(clonedVM);
manager.getVBox().registerMachine(newVM);
ISession session = manager.getSessionObject();
IMachine machine = manager.getVBox().findMachine(instanceName);
System.out.println("\nLaunching VM named " + clonedVM.getName() + " ...");
machine.lockMachine(session, LockType.Write); launchVMProcess(clonedVM, manager.getSessionObject());
IMachine mutable = session.getMachine(); String ipAddress = null;
// network while (ipAddress == null || ipAddress.equals("")) {
String hostInterface = null; try {
String command = "vboxmanage list bridgedifs"; ipAddress = clonedVM.getGuestPropertyValue("/VirtualBox/GuestInfo/Net/0/V4/IP");
try { Thread.sleep(1000);
Process child = Runtime.getRuntime().exec(command); } catch (InterruptedException e) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(child.getInputStream())); e.printStackTrace();
String line = "";
boolean found = false;
while ((line = bufferedReader.readLine()) != null && !found) {
if (line.split(":")[0].contains("Name")) {
hostInterface = line.split(":")[1];
}
if (line.split(":")[0].contains("Status") && line.split(":")[1].contains("Up")) {
System.out.println("bridge: " + hostInterface.trim());
found = true;
}
} }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
mutable.getNetworkAdapter(new Long(0)).setAttachmentType( System.out.println(ipAddress + " is the IP address of " + clonedVM.getName());
NetworkAttachmentType.Bridged);
mutable.getNetworkAdapter(new Long(0)).setAdapterType(NetworkAdapterType.Am79C973); //TODO
mutable.getNetworkAdapter(new Long(0)).setBridgedInterface(hostInterface.trim()); // IPSocket socket = new IPSocket(ipAddress, 22);
mutable.getNetworkAdapter(new Long(0)).setEnabled(true); // checkSSH(IPSocket socket)
mutable.saveSettings();
// disk
mutable.addStorageController(controller, StorageBus.IDE);
mutable.saveSettings();
IMedium distroMedium = manager.getVBox().openMedium(
clonedDiskPath, DeviceType.HardDisk,
AccessMode.ReadWrite, forceOverwrite);
mutable.attachDevice(controller, 0, 0, DeviceType.HardDisk, distroMedium);
mutable.saveSettings();
session.unlockMachine();
} }
private void launchVMProcess(IMachine machine, ISession session) { private void launchVMProcess(IMachine machine, ISession session) {
@ -291,34 +241,8 @@ public class VirtualboxLiveTest {
} }
} }
@Test(dependsOnMethods = "testStartVirtualMachines") @Test(dependsOnMethods = "testStartAndValidateVirtualMachines")
public void testSshLogin() { public void testStopVirtualMachines() {
String ipAddress = null;
for (int i = 1; i < numberOfVirtualMachine + 1; i++) {
String instanceName = vmName + "_" + i;
IMachine machine = manager.getVBox().findMachine(instanceName);
System.out.println("\nLaunching VM named " + machine.getName() + " ...");
launchVMProcess(machine, manager.getSessionObject());
while (ipAddress == null || ipAddress.equals("")) {
try {
ipAddress = machine.getGuestPropertyValue("/VirtualBox/GuestInfo/Net/0/V4/IP");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("VM " + instanceName + " started with IP " + ipAddress);
IPSocket socket = new IPSocket(ipAddress, 22);
System.out.println("Check SSH for " + instanceName + " ...");
checkSSH(socket);
}
}
@Test(dependsOnMethods = "testSshLogin")
public void testStopVirtualMachine() {
for (int i = 1; i < numberOfVirtualMachine + 1; i++) { for (int i = 1; i < numberOfVirtualMachine + 1; i++) {
String instanceName = vmName + "_" + i; String instanceName = vmName + "_" + i;
IMachine machine = manager.getVBox().findMachine(instanceName); IMachine machine = manager.getVBox().findMachine(instanceName);