issue 384: some fixing to AdminLiveTest for cloning vms from admin node - unstable

This commit is contained in:
andreaturli 2011-09-11 21:52:38 +01:00
parent fc52cdbaf8
commit 435253dcb5
2 changed files with 248 additions and 259 deletions

View File

@ -175,8 +175,8 @@ public class VirtualboxAdministrationKickstartLiveTest {
adminDisk = workingDir
+ File.separator
+ System.getProperty("test." + provider + ".clonedDisk",
"disk.vdi");
+ System.getProperty("test." + provider + ".adminDisk",
"admin.vdi");
guestAdditionsDvd = workingDir
+ File.separator
+ System.getProperty("test." + provider + ".guestAdditionsDvd",
@ -457,8 +457,10 @@ public class VirtualboxAdministrationKickstartLiveTest {
runScriptOnNode(guestId,
"mount -o loop /dev/dvd /media/cdrom");
runScriptOnNode(guestId, "sh /media/cdrom/VBoxLinuxAdditions.run");
// for Debian based OS
runScriptOnNode(guestId, "rm /etc/udev/rules.d/70-persistent-net.rules");
//runScriptOnNode(guestId, "apt-get --yes install virtualbox-ose-guest-utils");
//}
}

View File

@ -30,26 +30,26 @@ import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.domain.Credentials;
import org.jclouds.encryption.bouncycastle.config.BouncyCastleCryptoModule;
import org.jclouds.logging.Logger;
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.AfterMethod;
import org.testng.annotations.BeforeClass;
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;
@ -63,214 +63,204 @@ 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.MediumState;
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.inject.Guice;
import com.google.inject.Injector;
@Test(groups = "live", testName = "virtualbox.VirtualboxLiveTest")
public class VirtualboxLiveTest {
protected String provider = "virtualbox";
protected String identity;
protected String credential;
protected URI endpoint;
protected String apiversion;
protected String vmName;
protected String provider = "virtualbox";
protected String identity;
protected String credential;
protected URI endpoint;
protected String apiversion;
protected String vmName;
VirtualBoxManager manager = VirtualBoxManager.createInstance("");
VirtualBoxManager manager = VirtualBoxManager.createInstance("");
protected Injector injector;
protected Predicate<IPSocket> socketTester;
protected SshClient.Factory sshFactory;
protected Injector injector;
protected Predicate<IPSocket> socketTester;
protected SshClient.Factory sshFactory;
protected String osUsername;
protected String osPassword;
protected String controller;
protected String diskFormat;
protected String osUsername;
protected String osPassword;
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 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 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 clonedDiskPath;
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";
// 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 String originalDisk;
private String originalDiskPath;
private String clonedDisk;
private IMedium clonedHd;
private ComputeServiceContext context;
protected int numberOfVirtualMachine;
protected String originalDisk;
private String originalDiskPath;
private String clonedDisk;
private IMedium clonedHd;
private ComputeServiceContext context;
private String hostId = "host";
private String guestId = "guest";
private String majorVersion;
private String minorVersion;
private String apiVersion;
private String hostId = "host";
private String guestId = "guest";
private String majorVersion;
private String minorVersion;
private String apiVersion;
private String adminNodeName;
protected Logger logger() {
return context.utils().loggerFactory().getLogger("jclouds.compute");
}
protected Logger logger() {
return context.utils().loggerFactory().getLogger("jclouds.compute");
}
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.2r73507");
majorVersion = Iterables.get(Splitter.on('r').split(apiVersion), 0);
minorVersion = Iterables.get(Splitter.on('r').split(apiVersion), 1);
}
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.2r73507");
majorVersion = Iterables.get(Splitter.on('r').split(apiVersion), 0);
minorVersion = Iterables.get(Splitter.on('r').split(apiVersion), 1);
}
protected void setupConfigurationProperties() {
// 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.
protected void setupConfigurationProperties() {
// VBOX
settingsFile = null;
osTypeId = System.getProperty("test." + provider + ".osTypeId", "");
vmId = System.getProperty("test." + provider + ".vmId", null);
forceOverwrite = true;
// OS specific information
adminNodeName = System.getProperty("test." + provider + ".adminnodename", "jclouds-virtualbox-kickstart-admin");
vmName = checkNotNull(System.getProperty("test." + provider + ".vmname", "jclouds-virtualbox-node"));
osUsername = System.getProperty("test." + provider + ".osusername", "toor");
osPassword = System.getProperty("test." + provider + ".ospassword", "password");
controller = System.getProperty("test." + provider + ".controller", "IDE Controller");
diskFormat = System.getProperty("test." + provider + ".diskformat", "");
// OS specific information
vmName = checkNotNull(System.getProperty("test." + provider + ".vmname", "jclouds-virtualbox-node"));
osUsername = System.getProperty("test." + provider + ".osusername", "toor");
osPassword = System.getProperty("test." + provider + ".ospassword", "password");
controller = System.getProperty("test." + provider + ".controller", "IDE Controller");
diskFormat = System.getProperty("test." + provider + ".diskformat", "");
workingDir = System.getProperty("user.home")
workingDir = System.getProperty("user.home")
+ File.separator
+ System.getProperty("test." + provider + ".workingDir",
"jclouds-virtualbox-test");
originalDisk = System.getProperty("test." + provider + ".originalDisk", "admin.vdi");
originalDiskPath = workingDir
+ File.separator + originalDisk;
originalDisk = System.getProperty("test." + provider + ".originalDisk", "admin.vdi");
originalDiskPath = workingDir + File.separator + originalDisk;
clonedDisk = System.getProperty("test." + provider + ".clonedDisk", "clone.vdi");
clonedDiskPath = workingDir + File.separator + clonedDisk;
clonedDisk = System.getProperty("test." + provider + ".clonedDisk", "clone.vdi");
clonedDiskPath = workingDir + File.separator + clonedDisk;
numberOfVirtualMachine = Integer.parseInt(checkNotNull(System.getProperty("test." + provider
+ ".numberOfVirtualMachine", "2")));
}
numberOfVirtualMachine = Integer.parseInt(checkNotNull(System.getProperty("test." + provider
+ ".numberOfVirtualMachine", "1")));
}
@BeforeGroups(groups = "live")
protected void setupClient() throws Exception {
context = TestUtils.computeServiceForLocalhost();
socketTester = new RetryablePredicate<IPSocket>(
new InetSocketAddressConnect(), 130, 10, TimeUnit.SECONDS);
setupCredentials();
setupConfigurationProperties();
if (!new InetSocketAddressConnect().apply(new IPSocket(endpoint.getHost(), endpoint.getPort())))
startupVboxWebServer();
}
@BeforeGroups(groups = "live")
protected void setupClient() throws Exception {
context = TestUtils.computeServiceForLocalhost();
socketTester = new RetryablePredicate<IPSocket>(
new InetSocketAddressConnect(), 130, 10, TimeUnit.SECONDS);
setupCredentials();
setupConfigurationProperties();
if (!new InetSocketAddressConnect().apply(new IPSocket(endpoint.getHost(), endpoint.getPort())))
startupVboxWebServer();
}
@BeforeMethod
protected void setupManager() throws RemoteException, MalformedURLException {
manager.connect(endpoint.toASCIIString(), identity, credential);
}
@AfterMethod
protected void disconnectAndClenaupManager() throws RemoteException, MalformedURLException {
manager.disconnect();
manager.cleanup();
}
@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);
@BeforeMethod
protected void setupManager() throws RemoteException, MalformedURLException {
manager.connect(endpoint.toASCIIString(), identity, credential);
}
@AfterMethod
protected void disconnectAndClenaupManager() throws RemoteException, MalformedURLException {
manager.disconnect();
manager.cleanup();
}
@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);
assertEquals(clonedHd.getId().equals(""), false);
}
clonedHd.setType(MediumType.MultiAttach);
assertEquals(clonedHd.getId().equals(""), false);
}
@Test(dependsOnMethods = "testCloneHardDisk")
public void testStartVirtualMachines() {
//IMedium clonedHd = cloneDisk(MediumType.MultiAttach);
for (int i = 1; i < numberOfVirtualMachine + 1; i++) {
createVirtualMachine(i);
}
}
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) {
private void createVirtualMachine(int i) {
String instanceName = vmName + "_" + i;
String instanceName = vmName + "_" + i;
IMachine newVM = manager.getVBox().createMachine(settingsFile, instanceName, osTypeId, vmId, forceOverwrite);
manager.getVBox().registerMachine(newVM);
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;
ISession session = manager.getSessionObject();
IMachine machine = manager.getVBox().findMachine(instanceName);
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;
}
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;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO: lookup translations for 4.1 for the below
// mutable.getNetworkAdapter(new Long(0)).attachToBridgedInterface();
// mutable.getNetworkAdapter(new
// Long(0)).setHostInterface(hostInterface.trim());
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(
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.getNetworkAdapter(new Long(0)).setEnabled(true);
mutable.saveSettings();
// disk
// disk
mutable.addStorageController(controller, StorageBus.IDE);
mutable.saveSettings();
IMedium distroMedium = manager.getVBox().openMedium(
@ -278,85 +268,82 @@ protected void setupCredentials() {
AccessMode.ReadWrite, forceOverwrite);
mutable.attachDevice(controller, 0, 0, DeviceType.HardDisk, distroMedium);
mutable.saveSettings();
session.unlockMachine();
}
session.unlockMachine();
}
private void launchVMProcess(IMachine machine, ISession session) {
IProgress prog = machine.launchVMProcess(session, "gui", "");
prog.waitForCompletion(-1);
session.unlockMachine();
}
protected void checkSSH(IPSocket socket) {
socketTester.apply(socket);
SshClient client = sshFactory.create(socket, new Credentials(osUsername, osPassword));
try {
client.connect();
ExecResponse exec = client.exec("touch /tmp/hello_" + System.currentTimeMillis());
exec = client.exec("echo hello");
System.out.println(exec);
assertEquals(exec.getOutput().trim(), "hello");
} finally {
if (client != null)
client.disconnect();
}
}
@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);
private void launchVMProcess(IMachine machine, ISession session) {
IProgress prog = machine.launchVMProcess(session, "gui", "");
prog.waitForCompletion(-1);
session.unlockMachine();
}
System.out.println("\nLaunching VM named " + machine.getName() + " ...");
launchVMProcess(machine, manager.getSessionObject());
protected void checkSSH(IPSocket socket) {
socketTester.apply(socket);
SshClient client = sshFactory.create(socket, new Credentials(osUsername, osPassword));
try {
client.connect();
ExecResponse exec = client.exec("touch /tmp/hello_" + System.currentTimeMillis());
exec = client.exec("echo hello");
System.out.println(exec);
assertEquals(exec.getOutput().trim(), "hello");
} finally {
if (client != null)
client.disconnect();
}
}
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);
@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("Check SSH for " + instanceName + " ...");
checkSSH(socket);
}
}
System.out.println("\nLaunching VM named " + machine.getName() + " ...");
launchVMProcess(machine, manager.getSessionObject());
@Test(dependsOnMethods = "testSshLogin")
public void testStopVirtualMachine() {
for (int i = 1; i < numberOfVirtualMachine + 1; i++) {
String instanceName = vmName + "_" + i;
IMachine machine = manager.getVBox().findMachine(instanceName);
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);
try {
ISession machineSession = manager.openMachineSession(machine);
IProgress progress = machineSession.getConsole().powerDown();
progress.waitForCompletion(-1);
machineSession.unlockMachine();
System.out.println("Check SSH for " + instanceName + " ...");
checkSSH(socket);
}
}
@Test(dependsOnMethods = "testSshLogin")
public void testStopVirtualMachine() {
for (int i = 1; i < numberOfVirtualMachine + 1; i++) {
String instanceName = vmName + "_" + i;
IMachine machine = manager.getVBox().findMachine(instanceName);
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();
}
}
assertEquals(machine.getState(), MachineState.PoweredOff);
} catch (Exception e) {
e.printStackTrace();
}
}
}
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();
}
}
assertEquals(machine.getState(), MachineState.PoweredOff);
} catch (Exception e) {
e.printStackTrace();
}
}
}
protected ExecResponse runScriptOnNode(String nodeId, String command,
@ -389,8 +376,8 @@ protected void setupCredentials() {
hostId,
vboxwebsrv,
runAsRoot(false).wrapInInitScript(false)
.blockOnPort(endpoint.getPort(), 10)
.blockOnComplete(false)
.nameTask("vboxwebsrv"));
.blockOnPort(endpoint.getPort(), 10)
.blockOnComplete(false)
.nameTask("vboxwebsrv"));
}
}