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");
gaIsoUrl = URI.create(System.getProperty("test." + provider
+ ".gaIsoUrl", "http://download.virtualbox.org/virtualbox/"
+ majorVersion + "/" + gaIsoName));
+ majorVersion + "/" + gaIsoName));
distroIsoName = System.getProperty("test." + provider
+ ".distroIsoName", "ubuntu-11.04-server-i386.iso");
@ -282,9 +282,9 @@ public class VirtualboxAdministrationKickstartLiveTest {
hostId,
vboxwebsrv,
runAsRoot(false).wrapInInitScript(false)
.blockOnPort(endpoint.getPort(), 10)
.blockOnComplete(false)
.nameTask("vboxwebsrv"));
.blockOnPort(endpoint.getPort(), 10)
.blockOnComplete(false)
.nameTask("vboxwebsrv"));
}
protected boolean isOSX(String id) {
@ -304,7 +304,7 @@ public class VirtualboxAdministrationKickstartLiveTest {
@AfterMethod
protected void disconnectAndClenaupManager() throws RemoteException,
MalformedURLException {
MalformedURLException {
manager.disconnect();
manager.cleanup();
}
@ -354,7 +354,7 @@ public class VirtualboxAdministrationKickstartLiveTest {
machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
mutable.attachDevice(controllerIDE, 0, 0, DeviceType.DVD, distroMedium);
mutable.saveSettings(); // write settings to xml
mutable.saveSettings();
session.unlockMachine();
assertEquals(distroMedium.getId().equals(""), false);
}
@ -392,9 +392,9 @@ public class VirtualboxAdministrationKickstartLiveTest {
mutable.getNetworkAdapter(new Long(0)).setAttachmentType(
NetworkAttachmentType.NAT);
machine.getNetworkAdapter(new Long(0))
.getNatDriver()
.addRedirect("guestssh", NATProtocol.TCP, "127.0.0.1", 2222,
"", 22);
.getNatDriver()
.addRedirect("guestssh", NATProtocol.TCP, "127.0.0.1", 2222,
"", 22);
mutable.getNetworkAdapter(new Long(0)).setEnabled(true);
mutable.saveSettings();
@ -402,19 +402,19 @@ public class VirtualboxAdministrationKickstartLiveTest {
}
@Test(dependsOnMethods = "testConfigureNIC")
public void testAttachGuestAdditions() {
ISession session = manager.getSessionObject();
IMachine machine = manager.getVBox().findMachine(vmName);
public void testAttachGuestAdditions() {
ISession session = manager.getSessionObject();
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")
@ -431,32 +431,30 @@ public class VirtualboxAdministrationKickstartLiveTest {
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")
public void testConfigureGuestAdditions() {
// TODO generalize: at the moment we are usign apt-get not the guestadditions.iso attached
//if(isUbuntu(guestId)) {
// Configure your system for building kernel modules by running
runScriptOnNode(guestId, "m-a prepare -i", wrapInInitScript(true));
// Configure your system for building kernel modules by running
runScriptOnNode(guestId, "m-a prepare -i", wrapInInitScript(true));
runScriptOnNode(guestId,
"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
runScriptOnNode(guestId, "rm /etc/udev/rules.d/70-persistent-net.rules");
@ -484,16 +482,16 @@ public class VirtualboxAdministrationKickstartLiveTest {
while (!machine.getSessionState().equals(SessionState.Unlocked)) {
try {
System.out
.println("waiting for unlocking session - session state: "
+ machine.getSessionState());
.println("waiting for unlocking session - session state: "
+ machine.getSessionState());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
e.printStackTrace();
}
}
@ -504,7 +502,7 @@ public class VirtualboxAdministrationKickstartLiveTest {
machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
mutable.getNetworkAdapter(new Long(0)).getNatDriver()
.removeRedirect("guestssh");
.removeRedirect("guestssh");
// detach disk from controller
mutable.detachDevice(controllerIDE, 0, 0);
mutable.saveSettings();
@ -586,19 +584,13 @@ public class VirtualboxAdministrationKickstartLiveTest {
}
private String stringToKeycode(String s) {
StringBuilder keycodes = new StringBuilder();
//for (String specialButton : KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP.keySet()) {
//System.out.println("specialbutton " + specialButton);
if (s.startsWith("<")) {
String[] specials = s.split("<");
for (int i = 1; i < specials.length; i++) {
keycodes.append(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP
.get("<" + specials[i]) + " ");
}
System.out.println("keycodes: " + keycodes);
if (s.startsWith("<")) {
String[] specials = s.split("<");
for (int i = 1; i < specials.length; i++) {
keycodes.append(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP
.get("<" + specials[i]) + " ");
}
return keycodes.toString();
}

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