renamed tests that require virtualbox running to Live suffix

This commit is contained in:
Adrian Cole 2011-07-27 02:44:10 -07:00
parent 9c67e6b63c
commit 9fd467d030
2 changed files with 0 additions and 1025 deletions

View File

@ -1,519 +0,0 @@
package org.jclouds.virtualbox.experiment;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.propagate;
import static org.jclouds.compute.options.RunScriptOptions.Builder.blockOnPort;
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.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.rmi.RemoteException;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.logging.Logger;
import org.jclouds.net.IPSocket;
import org.jclouds.predicates.InetSocketAddressConnect;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.virtualbox.experiment.settings.KeyboardScancodes;
import org.testng.annotations.AfterClass;
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.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.IStorageController;
import org.virtualbox_4_1.LockType;
import org.virtualbox_4_1.MachineState;
import org.virtualbox_4_1.NATProtocol;
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;
import com.google.common.collect.Iterables;
import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables;
@Test(groups = "live", testName = "virtualbox.VirtualboxAdministrationKickstartTest")
public class VirtualboxAdministrationKickstartTest {
protected String provider = "virtualbox";
protected String identity;
protected String credential;
protected URI endpoint;
protected String apiVersion;
protected String vmName;
VirtualBoxManager manager = VirtualBoxManager.createInstance("");
protected Predicate<IPSocket> socketTester;
protected String settingsFile; // Fully qualified path where the settings
protected String osTypeId; // Guest OS Type ID.
protected String vmId; // Machine UUID (optional).
protected boolean forceOverwrite;
protected String diskFormat;
protected String workingDir;
protected String originalDisk;
protected String clonedDisk;
protected String guestAdditionsDvd;
private URI gaIsoUrl;
private String gaIsoName;
private URI distroIsoUrl;
private String distroIsoName;
private String controllerIDE;
private String controllerSATA;
private String keyboardSequence;
private String vdiName;
private String preseedUrl;
protected Server server = null;
private ComputeServiceContext context;
private String hostId = "host";
private String guestId = "guest";
private String majorVersion;
private String minorVersion;
protected void setupCredentials() {
identity = System.getProperty("test." + provider + ".identity", "administrator");
credential = System.getProperty("test." + provider + ".credential", "12345");
endpoint = URI.create(System.getProperty("test." + provider + ".endpoint", "http://localhost:18083/"));
apiVersion = System.getProperty("test." + provider + ".apiversion", "4.1.0r73009");
majorVersion = Iterables.get(Splitter.on('r').split(apiVersion),0);
minorVersion = Iterables.get(Splitter.on('r').split(apiVersion),1);
}
protected Logger logger() {
return context.utils().loggerFactory().getLogger("jclouds.compute");
}
protected void setupConfigurationProperties() {
controllerIDE = System.getProperty("test." + provider + ".controllerIde", "IDE Controller");
controllerSATA = System.getProperty("test." + provider + ".controllerSata", "SATA Controller");
diskFormat = System.getProperty("test." + provider + ".diskformat", "");
// VBOX
settingsFile = null;
osTypeId = System.getProperty("test." + provider + ".osTypeId", "");
vmId = System.getProperty("test." + provider + ".vmId", null);
forceOverwrite = true;
vmName = System.getProperty("test." + provider + ".vmname", "jclouds-virtualbox-kickstart-admin");
workingDir = System.getProperty("user.home")
+ File.separator + System.getProperty("test." + provider + ".workingDir", "jclouds-virtualbox-test");
if (new File(workingDir).mkdir())
;
vdiName = System.getProperty("test." + provider + ".vdiName", "centos-5.2-x86.7z");
gaIsoName = System.getProperty("test." + provider + ".gaIsoName", "VBoxGuestAdditions_"+majorVersion+"-update-"+minorVersion+".iso");
gaIsoUrl = URI.create(System.getProperty("test." + provider + ".gaIsoUrl", "http://download.virtualbox.org/virtualbox/"+majorVersion+"/VBoxGuestAdditions_"+majorVersion+"-update-"+minorVersion+".iso"));
distroIsoName = System.getProperty("test." + provider
+ ".distroIsoName", "ubuntu-11.04-server-i386.iso");
distroIsoUrl = URI.create(System
.getProperty("test." + provider + ".distroIsoUrl",
"http://releases.ubuntu.com/11.04/ubuntu-11.04-server-i386.iso"));
originalDisk = workingDir + File.separator + "VDI" + File.separator + System.getProperty("test." + provider + ".originalDisk",
"centos-5.2-x86.vdi");
clonedDisk = workingDir + File.separator + System.getProperty("test." + provider + ".clonedDisk",
"template.vdi");
guestAdditionsDvd = workingDir + File.separator + System.getProperty("test." + provider + ".guestAdditionsDvd",
"VBoxGuestAdditions_"+majorVersion+"-update-"+minorVersion+".iso");
preseedUrl = System.getProperty("test." + provider + ".preseedurl",
"http://dl.dropbox.com/u/693111/preseed.cfg");
keyboardSequence = System.getProperty("test." + provider + ".keyboardSequence",
"<Esc> <Esc> <Enter> "
+ "/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
+ " "
+ "fb=false debconf/frontend=noninteractive "
+ "keyboard-configuration/layout=USA keyboard-configuration/variant=USA console-setup/ask_detect=false "
+ "initrd=/install/initrd.gz -- <Enter>");
}
@BeforeGroups(groups = "live")
protected void setupClient() throws Exception {
context = TestUtils.computeServiceForLocalhost();
socketTester = new RetryablePredicate<IPSocket>(new InetSocketAddressConnect(), 130, 10,
TimeUnit.SECONDS);
setupCredentials();
setupConfigurationProperties();
downloadFileUnlessPresent(distroIsoUrl, workingDir, distroIsoName);
downloadFileUnlessPresent(gaIsoUrl, workingDir, gaIsoName);
installVbox();
checkVboxVersionExpected();
if (!new InetSocketAddressConnect().apply(new IPSocket(endpoint.getHost(), endpoint.getPort())))
startupVboxWebServer();
configureJettyServer();
}
private void configureJettyServer() throws Exception {
Server server = new Server(8080);
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setDirectoriesListed(true);
resource_handler.setWelcomeFiles(new String[] { "index.html" });
resource_handler.setResourceBase(".");
logger().info("serving " + resource_handler.getBaseResource());
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resource_handler,
new DefaultHandler() });
server.setHandler(handlers);
server.start();
}
void installVbox() throws IOException, InterruptedException {
if (runScriptOnNode(hostId, "VBoxManage -version").getExitCode() != 0) {
logger().debug("installing virtualbox");
if (isOSX(hostId))
;// TODO
else
runScriptOnNode(hostId, "apt-get --yes install virtualbox-ose");
// TODO other platforms
}
}
void checkVboxVersionExpected() throws IOException, InterruptedException {
logger().debug("checking virtualbox version");
assertEquals(runScriptOnNode(hostId, "VBoxManage -version").getOutput().trim(), apiVersion);
}
/**
*
* @param command
* absolute path to command. For ubuntu 10.04: /usr/bin/vboxwebsrv
* @throws IOException
* @throws InterruptedException
*/
void startupVboxWebServer() {
logger().debug("disabling password access");
runScriptOnNode(hostId, "VBoxManage setproperty websrvauthlibrary null");
logger().debug("starting vboxwebsrv");
String vboxwebsrv = "vboxwebsrv -t 5 -v";
if (isOSX(hostId))
vboxwebsrv = "cd /Applications/VirtualBox.app/Contents/MacOS/&&" + vboxwebsrv;
// allow jclouds to background the process, this is why we don't specify
// -b; logs will go corresponding to task name in this case under /tmp/vboxwebsrv
runScriptOnNode(hostId, vboxwebsrv,
blockOnPort(endpoint.getPort(), 10).blockOnComplete(false).nameTask("vboxwebsrv"));
}
protected boolean isOSX(String id) {
return context.getComputeService().getNodeMetadata(hostId).getOperatingSystem().getDescription().equals("Mac OS X");
}
@BeforeMethod
protected void setupManager() {
manager.connect(endpoint.toASCIIString(), identity, credential);
}
@AfterMethod
protected void disconnectAndClenaupManager() throws RemoteException,
MalformedURLException {
manager.disconnect();
manager.cleanup();
}
public void testCreateVirtualMachine() {
IMachine newVM = manager.getVBox().createMachine(settingsFile, vmName,
osTypeId, vmId, forceOverwrite);
manager.getVBox().registerMachine(newVM);
assertEquals(newVM.getName(), vmName);
}
@Test(dependsOnMethods = "testCreateVirtualMachine")
public void testChangeRAM() {
Long memorySize = new Long(1024);
ISession session = manager.getSessionObject();
IMachine machine = manager.getVBox().findMachine(vmName);
machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
mutable.setMemorySize(memorySize);
mutable.saveSettings();
session.unlockMachine();
assertEquals(manager.getVBox().findMachine(vmName).getMemorySize(),
memorySize);
}
@Test(dependsOnMethods = "testChangeRAM")
public void testCreateScsiController() {
ISession session = manager.getSessionObject();
IMachine machine = manager.getVBox().findMachine(vmName);
machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
mutable.addStorageController(controllerSATA, StorageBus.SATA);
mutable.saveSettings();
session.unlockMachine();
assertEquals(manager.getVBox().findMachine(vmName)
.getStorageControllers().size(), 2);
}
@Test(dependsOnMethods = "testCreateScsiController")
public void testCreateAndAttachHardDisk() {
IMedium hd = null;
if (!new File(clonedDisk).exists()) {
hd = manager.getVBox().createHardDisk(diskFormat, clonedDisk);
long size = 2 * 1024 * 1024 * 1024 - 1;
hd.createBaseStorage(new Long(size), new Long(
MediumVariant.VMDK_SPLIT_2_G.ordinal()));
} else
hd = manager.getVBox().openMedium(clonedDisk, DeviceType.HardDisk,
AccessMode.ReadWrite, forceOverwrite);
ISession session = manager.getSessionObject();
IMachine machine = manager.getVBox().findMachine(vmName);
machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
mutable.attachDevice(controllerSATA, 0, 0, DeviceType.HardDisk, hd);
mutable.saveSettings(); // write settings to xml
session.unlockMachine();
assertEquals(hd.getId().equals(""), false);
}
@Test(dependsOnMethods = "testCreateAndAttachHardDisk")
public void testConfigureNIC() {
ISession session = manager.getSessionObject();
IMachine machine = manager.getVBox().findMachine(vmName);
machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
// network BRIDGED to access HTTP server
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;
}
}
// NAT
// mutable.getNetworkAdapter(new Long(0)).attachToNAT(); TODO: this no longer exists!
mutable.getNetworkAdapter(new Long(0)).setNATNetwork("");
machine.getNetworkAdapter(new Long(0))
.getNatDriver()
.addRedirect("guestssh", NATProtocol.TCP, "127.0.0.1",
2222, "", 22);
mutable.getNetworkAdapter(new Long(0)).setEnabled(true);
mutable.saveSettings();
session.unlockMachine();
} catch (IOException e) {
propagate(e);
}
}
@Test(dependsOnMethods = "testConfigureNIC")
public void testStartVirtualMachine() throws InterruptedException {
IMachine machine = manager.getVBox().findMachine(vmName);
ISession session = manager.getSessionObject();
launchVMProcess(machine, session);
assertEquals(machine.getState(), MachineState.Running);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
propagate(e);
}
sendKeyboardSequence(keyboardSequence);
}
@Test(dependsOnMethods = "testStartVirtualMachine")
public void testConfigureGuestAdditions() {
// Configure your system for building kernel modules by running
runScriptOnNode(guestId, "m-a prepare -i");
runScriptOnNode(guestId, "mount -o loop /usr/share/virtualbox/VBoxGuestAdditions.iso /mnt");
runScriptOnNode(guestId, "/mnt/VBoxLinuxAdditions.run");
}
@Test(dependsOnMethods = "testConfigureGuestAdditions")
public void testStopVirtualMachine() {
IMachine machine = manager.getVBox().findMachine(vmName);
powerDownMachine(machine);
assertEquals(machine.getState(), MachineState.PoweredOff);
}
/**
* @param machine
*/
private void powerDownMachine(IMachine machine) {
try {
ISession machineSession = manager.openMachineSession(machine);
IProgress progress = machineSession.getConsole().powerDown();
progress.waitForCompletion(-1);
machineSession.unlockMachine();
while (!machine.getSessionState().equals(SessionState.Unlocked)) {
try {
System.out.println("waiting for unlocking session - session state: "
+ machine.getSessionState());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test(dependsOnMethods = "testStopVirtualMachine")
public void cleanUp() throws IOException {
ISession session = manager.getSessionObject();
IMachine machine = manager.getVBox().findMachine(vmName);
machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
mutable.getNetworkAdapter(new Long(0)).getNatDriver()
.removeRedirect("guestssh");
// detach disk from controller
mutable.detachDevice(controllerIDE, 0, 0);
mutable.saveSettings();
session.unlockMachine();
for (IStorageController storageController : machine
.getStorageControllers()) {
if (storageController.getName().equals(controllerSATA)) {
session = manager.getSessionObject();
machine.lockMachine(session, LockType.Write);
mutable = session.getMachine();
mutable.detachDevice(storageController.getName(), 1, 1);
mutable.saveSettings();
session.unlockMachine();
}
}
}
@AfterClass
void stopVboxWebServer() throws IOException {
runScriptOnNode(guestId, "pidof vboxwebsrv | xargs kill");
}
protected ExecResponse runScriptOnNode(String nodeId, String command, RunScriptOptions options) {
ExecResponse toReturn = context.getComputeService().runScriptOnNode(nodeId, command, options);
assert toReturn.getExitCode() == 0: toReturn;
return toReturn;
}
protected ExecResponse runScriptOnNode(String nodeId, String command) {
return runScriptOnNode(nodeId, command, wrapInInitScript(false));
}
private File downloadFileUnlessPresent(URI sourceURL, String destinationDir,
String filename) throws Exception {
File iso = new File(destinationDir, filename);
if (!iso.exists()) {
InputStream is = context.utils().http().get(sourceURL);
checkNotNull(is, "%s not found", sourceURL);
try {
ByteStreams.copy(is, new FileOutputStream(iso));
} finally {
Closeables.closeQuietly(is);
}
}
return iso;
}
private void sendKeyboardSequence(String keyboardSequence) throws InterruptedException {
String[] sequenceSplited = keyboardSequence.split(" ");
for (String word : sequenceSplited) {
String converted = stringToKeycode(word);
for (String string : converted.split(" ")) {
runScriptOnNode(hostId , "VBoxManage controlvm " + vmName + " keyboardputscancode "
+ string);
if (converted
.contains(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP
.get("<Return>")))
Thread.sleep(180);
}
}
}
private String stringToKeycode(String s) {
StringBuilder keycodes = new StringBuilder();
for (String specialButton : KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP
.keySet()) {
if (s.startsWith(specialButton)) {
keycodes.append(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP
.get(specialButton));
return keycodes.toString();
}
}
int i = 0;
while (i < s.length()) {
String digit = s.substring(i, i + 1);
String hex = KeyboardScancodes.NORMAL_KEYBOARD_BUTTON_MAP
.get(digit);
keycodes.append(hex + " ");
i++;
}
keycodes.append(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP
.get("<Spacebar>") + " ");
return keycodes.toString();
}
/**
*
* @param machine
* @param session
*/
private void launchVMProcess(IMachine machine, ISession session) {
IProgress prog = machine.launchVMProcess(session, "gui", "");
prog.waitForCompletion(-1);
session.unlockMachine();
}
}

View File

@ -1,506 +0,0 @@
package org.jclouds.virtualbox.experiment;
import static org.testng.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.concurrent.TimeUnit;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.domain.Credentials;
import org.jclouds.encryption.bouncycastle.config.BouncyCastleCryptoModule;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.net.IPSocket;
import org.jclouds.predicates.InetSocketAddressConnect;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.ssh.SshClient;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.annotations.AfterClass;
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.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.IStorageController;
import org.virtualbox_4_1.LockType;
import org.virtualbox_4_1.MachineState;
import org.virtualbox_4_1.NATProtocol;
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.io.Files;
import com.google.common.io.InputSupplier;
import com.google.inject.Guice;
import com.google.inject.Injector;
@Test(groups = "live", testName = "virtualbox.VirtualboxAdministrationTest")
public class VirtualboxAdministrationTest {
protected String provider = "virtualbox";
protected String identity;
protected String credential;
protected String endpoint;
protected String apiversion;
protected String vmName;
VirtualBoxManager manager = VirtualBoxManager.createInstance("");
protected Injector injector;
protected Predicate<IPSocket> socketTester;
protected SshClient.Factory sshFactory;
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 osUsername;
protected String osPassword;
protected String controller;
protected String diskFormat;
protected String workingDir;
protected String originalDisk;
protected String clonedDisk;
protected String guestAdditionsDvdName;
private String vdiUrl;
private String gaIsoUrl;
private String vboxwebsrvStartCommand;
// private Process pr;
private String vdiName;
private String gaIsoName;
private String admin_pwd;
private String hostUsername;
private String hostPassword;
private String install7zip;
private String run7zip;
private String installVboxOse;
/**
*
*
*
* @param workingDir
* @param vdiUrl
* @param proxy Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", 5865));
* @return
* @throws Exception
*/
private File downloadFile(String sourceURL, String destinationDir, String vboxGuestAdditionsName, Proxy proxy) throws Exception {
String absolutePathName = destinationDir + File.separator + vboxGuestAdditionsName;
File iso = new File(absolutePathName);
final URL isoURL = new URL(sourceURL);
final HttpURLConnection uc = (HttpURLConnection) isoURL.openConnection(); // isoURL.openConnection(proxy);
uc.connect();
if(!iso.exists()) {
System.out.println("Start download " + sourceURL + " to " + absolutePathName);
Files.copy(new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return uc.getInputStream();
}
}, iso);
}
return iso;
}
protected void setupCredentials() {
identity = System.getProperty("test." + provider + ".identity", "administrator");
credential = System.getProperty("test." + provider + ".credential", "12345");
endpoint = System.getProperty("test." + provider + ".endpoint", "http://localhost:18083/");
apiversion = System.getProperty("test." + provider + ".apiversion");
}
protected void setupConfigurationProperties() {
admin_pwd = System.getProperty("test." + provider + ".admin_pwd", "password");
// OS
osUsername = System.getProperty("test." + provider + ".osusername", "root");
osPassword = System.getProperty("test." + provider + ".ospassword", "toortoor");
controller = System.getProperty("test." + provider + ".controller", "IDE Controller");
// Create disk If the @a format attribute is empty or null then the default storage format specified by ISystemProperties#defaultHardDiskFormat
diskFormat = System.getProperty("test." + provider + ".diskformat", "");
//VBOX
settingsFile = null; // 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()).
osTypeId = System.getProperty("test." + provider + ".osTypeId", ""); // Guest OS Type ID.
vmId = System.getProperty("test." + provider + ".vmId", null); // Machine UUID (optional).
forceOverwrite = true; // If true, an existing machine settings file will be overwritten.
vmName = System.getProperty("test." + provider + ".vmname", "jclouds-virtualbox-admin");
workingDir = System.getProperty("user.home") + File.separator + System.getProperty("test." + provider + ".workingDir", "jclouds-virtualbox-test") ;
if(new File(workingDir).mkdir());
vdiName = System.getProperty("test." + provider + ".vdiName", "centos-5.2-x86.7z");
vdiUrl = System.getProperty("test." + provider + ".vdiUrl", "http://leaseweb.dl.sourceforge.net/project/virtualboximage/CentOS/5.2/centos-5.2-x86.7z");
gaIsoName = System.getProperty("test." + provider + ".gaIsoName", "VBoxGuestAdditions_4.0.2-update-69551.iso");
gaIsoUrl = System.getProperty("test." + provider + ".gaIsoUrl", "http://download.virtualbox.org/virtualbox/4.0.2/VBoxGuestAdditions_4.0.2-update-69551.iso");
vboxwebsrvStartCommand = System.getProperty("test." + provider + ".vboxwebsrvStartCommand","/usr/bin/vboxwebsrv");
originalDisk = workingDir + File.separator + "VDI" +File.separator + System.getProperty("test." + provider + ".originalDisk", "centos-5.2-x86.vdi");
clonedDisk = workingDir + File.separator + System.getProperty("test." + provider + ".clonedDisk", "template.vdi");
guestAdditionsDvdName = workingDir + File.separator + System.getProperty("test." + provider + ".guestAdditionsDvdName", "VBoxGuestAdditions_4.0.2-update-69551.iso");
install7zip = System.getProperty("test." + provider + ".install7zip", "sudo -S apt-get --yes install p7zip");
run7zip = System.getProperty("test." + provider + ".run7zip", "p7zip -d ");
installVboxOse = System.getProperty("test." + provider + ".installvboxose", "sudo -S apt-get --yes install virtualbox-ose");
if(!new File(originalDisk).exists()) {
IPSocket socket = new IPSocket("127.0.0.1", 22);
socketTester.apply(socket);
SshClient client = sshFactory.create(socket, new Credentials(hostUsername, hostPassword));
try {
File vdi7z = downloadFile(vdiUrl, workingDir, vdiName, null);
client.connect();
ExecResponse exec = client.exec("echo " + admin_pwd + " | " + install7zip + "; cd " + workingDir + "; " + run7zip + vdi7z.getName());
System.out.println(exec);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (client != null)
client.disconnect();
}
}
if(!new File(guestAdditionsDvdName).exists()) {
try {
File gaIso = downloadFile(gaIsoUrl, workingDir, gaIsoName, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@BeforeGroups(groups = "live")
protected void setupClient() throws IOException, InterruptedException {
hostUsername = System.getProperty("test." + provider + ".hostusername", "toor");
hostPassword = System.getProperty("test." + provider + ".hostpassword", "password");
injector = Guice.createInjector(new SshjSshClientModule(),
new SLF4JLoggingModule(), new BouncyCastleCryptoModule());
sshFactory = injector.getInstance(SshClient.Factory.class);
socketTester = new RetryablePredicate<IPSocket>(
new InetSocketAddressConnect(), 3600, 1, TimeUnit.SECONDS);
injector.injectMembers(socketTester);
setupCredentials();
setupConfigurationProperties();
installVbox();
// startup vbox web server
startupVboxWebServer(vboxwebsrvStartCommand);
}
private void installVbox() throws IOException, InterruptedException {
IPSocket socket = new IPSocket("127.0.0.1", 22);
socketTester.apply(socket);
SshClient client = sshFactory.create(socket, new Credentials(hostUsername, hostPassword));
try {
client.connect();
ExecResponse exec = client.exec("echo " + hostPassword + " | " + installVboxOse);
System.out.println(exec);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (client != null)
client.disconnect();
}
}
/**
*
* @param command absolute path to command. For ubuntu 10.04: /usr/bin/vboxwebsrv
* @throws IOException
* @throws InterruptedException
*/
private void startupVboxWebServer(String command) throws IOException, InterruptedException {
// Disable login credential: $
//rt.exec("VBoxManage setproperty websrvauthlibrary null");
IPSocket socket = new IPSocket("127.0.0.1", 22);
socketTester.apply(socket);
SshClient client = sshFactory.create(socket, new Credentials(hostUsername, hostPassword));
try {
client.connect();
ExecResponse exec = client.exec(command + " --timeout 50000 -b");
System.out.println(exec.getOutput());
System.out.println(exec);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (client != null)
client.disconnect();
}
}
@BeforeMethod
protected void setupManager() {
manager.connect(endpoint, identity, credential);
}
@AfterMethod
protected void disconnectAndClenaupManager() throws RemoteException,
MalformedURLException {
manager.disconnect();
manager.cleanup();
}
public void testCreateVirtualMachine() {
IMachine newVM = manager.getVBox().createMachine(settingsFile, vmName,
osTypeId, vmId, forceOverwrite);
manager.getVBox().registerMachine(newVM);
assertEquals(newVM.getName(), vmName);
}
@Test(dependsOnMethods = "testCreateVirtualMachine")
public void testChangeRAM() {
Long memorySize = new Long(2048);
ISession session = manager.getSessionObject();
IMachine machine = manager.getVBox().findMachine(vmName);
machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
mutable.setMemorySize(memorySize);
mutable.saveSettings();
session.unlockMachine();
assertEquals(manager.getVBox().findMachine(vmName)
.getMemorySize(), memorySize);
}
@Test(dependsOnMethods = "testChangeRAM")
public void testCreateDiskController() {
ISession session = manager.getSessionObject();
IMachine machine = manager.getVBox().findMachine(vmName);
machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
mutable.addStorageController(controller, StorageBus.IDE);
mutable.saveSettings();
session.unlockMachine();
assertEquals(manager.getVBox().findMachine(vmName)
.getStorageControllers().size(), 1);
}
@Test(dependsOnMethods = "testCreateDiskController")
public void testCloneAndAttachHardDisk() {
IMedium hd = manager.getVBox().openMedium(originalDisk,
DeviceType.HardDisk, AccessMode.ReadOnly, forceOverwrite);
IMedium clonedHd = null;
if(!new File(clonedDisk).exists()) {
clonedHd = manager.getVBox().createHardDisk(diskFormat, clonedDisk);
IProgress cloning = hd.cloneTo(clonedHd, new Long(MediumVariant.VMDK_SPLIT_2_G.ordinal()), null);
cloning.waitForCompletion(-1);
} else
clonedHd = manager.getVBox().openMedium(clonedDisk,
DeviceType.HardDisk, AccessMode.ReadOnly, forceOverwrite);
ISession session = manager.getSessionObject();
IMachine machine = manager.getVBox().findMachine(vmName);
machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
mutable.attachDevice(controller, 0, 0, DeviceType.HardDisk, clonedHd);
mutable.saveSettings(); // write settings to xml
session.unlockMachine();
assertEquals(hd.getId().equals(""), false);
}
@Test(dependsOnMethods = "testCloneAndAttachHardDisk")
public void testConfigureNIC() {
ISession session = manager.getSessionObject();
IMachine machine = manager.getVBox().findMachine(vmName);
machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
/*
* NAT
*/
// mutable.getNetworkAdapter(new Long(0)).attachToNAT(); not in 4.1
mutable.getNetworkAdapter(new Long(0)).setNATNetwork("");
mutable.getNetworkAdapter(new Long(0)).setEnabled(true);
mutable.saveSettings();
session.unlockMachine();
machine.lockMachine(session, LockType.Write);
mutable = session.getMachine();
machine.getNetworkAdapter(new Long(0))
.getNatDriver()
.addRedirect("guestssh", NATProtocol.TCP, "127.0.0.1", 2222, "", 22);
mutable.saveSettings();
session.unlockMachine();
}
@Test(dependsOnMethods = "testConfigureNIC")
public void testAttachGuestAdditions() {
ISession session = manager.getSessionObject();
IMachine machine = manager.getVBox().findMachine(vmName);
IMedium guestAdditionsDVD = manager.getVBox().openMedium(guestAdditionsDvdName,
DeviceType.DVD, AccessMode.ReadOnly, forceOverwrite);
for (IStorageController storageController : machine
.getStorageControllers()) {
// for DVD we choose IDE
if (storageController.getName().equals(controller)) {
machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
// IDE secondary slave [1:1]
mutable.attachDevice(storageController.getName(),
new Integer(1), new Integer(1), DeviceType.DVD,
guestAdditionsDVD);
mutable.saveSettings();
session.unlockMachine();
}
}
}
@Test(dependsOnMethods = "testAttachGuestAdditions")
public void testStartVirtualMachine() {
IMachine machine = manager.getVBox().findMachine(vmName);
ISession session = manager.getSessionObject();
launchVMProcess(machine, session);
assertEquals(machine.getState(), MachineState.Running);
}
/**
*
* @param machine
* @param session
*/
private void launchVMProcess(IMachine machine, ISession session) {
IProgress prog = machine.launchVMProcess(session, "gui", "");
prog.waitForCompletion(-1);
session.unlockMachine();
}
@Test(dependsOnMethods = "testStartVirtualMachine")
public void testInstallGuestAdditionsThroughNATPortForwarding() {
IPSocket socket = new IPSocket("127.0.0.1", 2222);
socketTester.apply(socket);
SshClient client = sshFactory.create(socket, new Credentials(osUsername, osPassword));
try {
client.connect();
ExecResponse exec = client.exec("yum install gcc kernel kernel-devel -y");
System.out.println(exec);
} finally {
if (client != null)
client.disconnect();
}
//manually restart
IMachine machine = manager.getVBox().findMachine(vmName);
powerDownMachine(machine);
launchVMProcess(machine, manager.getSessionObject());
socketTester.apply(socket);
client = sshFactory.create(socket, new Credentials(osUsername, osPassword));
try {
client.connect();
ExecResponse exec = client.exec("mkdir -p /media/cdrom; mount /dev/cdrom /media/cdrom; sh /media/cdrom/VBoxLinuxAdditions.run --nox11 force");
System.out.println(exec);
exec = client.exec("echo '/usr/sbin/VBoxService' >> /etc/rc.d/rc.local");
System.out.println(exec);
} finally {
if (client != null)
client.disconnect();
}
}
@Test(dependsOnMethods = "testInstallGuestAdditionsThroughNATPortForwarding")
public void testStopVirtualMachine() {
IMachine machine = manager.getVBox().findMachine(vmName);
powerDownMachine(machine);
assertEquals(machine.getState(), MachineState.PoweredOff);
}
/**
* @param machine
*/
private void powerDownMachine(IMachine machine) {
try {
ISession machineSession = manager.openMachineSession(machine);
IProgress progress = machineSession.getConsole().powerDown();
progress.waitForCompletion(-1);
machineSession.unlockMachine();
while(!machine.getSessionState().equals(SessionState.Unlocked)){
try {
System.out.println("waiting for unlocking session - session state: " + machine.getSessionState());
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test(dependsOnMethods = "testStopVirtualMachine")
public void cleanUp() throws IOException {
ISession session = manager.getSessionObject();
IMachine machine = manager.getVBox().findMachine(vmName);
machine.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
mutable.getNetworkAdapter(new Long(0)).getNatDriver().removeRedirect("guestssh");
// detach disk from controller
mutable.detachDevice(controller, 0, 0);
mutable.saveSettings();
session.unlockMachine();
for (IStorageController storageController : machine
.getStorageControllers()) {
if (storageController.getName().equals(controller)) {
session = manager.getSessionObject();
machine.lockMachine(session, LockType.Write);
mutable = session.getMachine();
mutable.detachDevice(storageController.getName(), 1, 1);
mutable.saveSettings();
session.unlockMachine();
}
}
}
@AfterClass
void stopVboxWebServer() throws IOException{
// stop vbox web server
IPSocket socket = new IPSocket("127.0.0.1", 22);
socketTester.apply(socket);
SshClient client = sshFactory.create(socket, new Credentials(hostUsername, hostPassword));
try {
client.connect();
ExecResponse exec = client.exec("pidof vboxwebsrv | xargs kill");
System.out.println(exec.getOutput());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (client != null)
client.disconnect();
} }
}