issue 384: first implementation of install_guest_additions Statement + Test

This commit is contained in:
Andrea Turli 2011-12-14 14:05:05 +00:00 committed by Adrian Cole
parent bd14c88a5d
commit b67827f864
9 changed files with 121 additions and 62 deletions

View File

@ -0,0 +1,49 @@
package org.jclouds.virtualbox.domain;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.scriptbuilder.domain.Statements.call;
import static org.jclouds.scriptbuilder.domain.Statements.exec;
import java.util.Collections;
import org.jclouds.scriptbuilder.ScriptBuilder;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.Statement;
public class InstallGuestAdditions implements Statement {
private final String vboxVersion;
private final String mountPoint;
public InstallGuestAdditions(String vboxVersion) {
this(vboxVersion, "/mnt");
}
public InstallGuestAdditions(String vboxVersion, String mountPoint) {
this.vboxVersion = checkNotNull(vboxVersion, "vboxVersion");
this.mountPoint = checkNotNull(mountPoint, "mountPoint");
}
@Override
public Iterable<String> functionDependencies(OsFamily family) {
return Collections.emptyList();
}
@Override
public String render(OsFamily family) {
checkNotNull(family, "family");
if (family == OsFamily.WINDOWS)
throw new UnsupportedOperationException("windows not yet implemented");
String vboxGuestAdditionsIso = "VBoxGuestAdditions_" + vboxVersion + ".iso";
ScriptBuilder scriptBuilder = new ScriptBuilder()
.addStatement(exec("{cd} {fs}tmp"))
.addStatement(exec("wget http://download.virtualbox.org/virtualbox/" + vboxVersion + "/" + vboxGuestAdditionsIso))
.addStatement(exec(String.format("mount -o loop %s %s", vboxGuestAdditionsIso, mountPoint)))
.addStatement(call("installGuestAdditions"))
.addStatement(exec(String.format("sh %s%s", mountPoint, "/VBoxLinuxAdditions.run")))
.addStatement(exec(String.format("umount %s", mountPoint)));
return scriptBuilder.render(family);
}
}

View File

@ -18,6 +18,7 @@
*/
package org.jclouds.virtualbox.domain;
import org.jclouds.scriptbuilder.domain.AppendFile;
import org.jclouds.scriptbuilder.domain.Call;
import org.jclouds.scriptbuilder.domain.Statement;

View File

@ -1,7 +1,7 @@
function exportIpAddressFromVmNamed {
unset FOUND_IP_ADDRESS;
[ $# -eq 1 ] || {
abort "installGuestAdditions requires virtual machine name parameter"
abort "exportIpAddressFromVmNamed requires virtual machine name parameter"
return 1
}
local VMNAME="$0"; shift

View File

@ -0,0 +1,14 @@
function installGuestAdditions {
unset OSNAME;
[ $# -eq 1 ] || {
abort "installGuestAdditions requires virtual machine name parameter"
return 1
}
local OSNAME=`lsb_release -d -s | cut -d ' ' -f 1`; shift
if [ $OSNAME = 'Ubuntu' ]
then
echo "OS Name is Ubuntu"
`apt-get install build-essential module-assistant && m-a prepare -i`
fi
return 0
}

View File

@ -47,5 +47,4 @@ public class ExportIpAddressForVMNamedTest {
assertEquals(exportIpAddressForVMNamedBuilder.render(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources
.getResource("test_export_ip_address_from_vm_named." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)));
}
}

View File

@ -1,58 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.virtualbox.domain;
import static org.jclouds.scriptbuilder.domain.Statements.interpret;
import static org.jclouds.virtualbox.domain.Statements.getIpAddress;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import org.jclouds.scriptbuilder.ScriptBuilder;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.ShellToken;
import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import com.google.common.io.Resources;
/**
* @author Andrea Turli
*/
@Test(groups = "unit")
public class GetIPAddressFromGuestAdditionsTest {
ScriptBuilder getIpAddressBuilder = new ScriptBuilder()
.addStatement(getIpAddress("{args}"))
.addStatement(interpret("echo {varl}FOUND_IP_ADDRESS{varr}{lf}"));
public void testUNIX() throws IOException {
assertEquals(getIpAddressBuilder.render(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources
.getResource("test_get_ip_address." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)));
}
// TODO
/*
public void testWINDOWS() throws IOException {
assertEquals(getIpAddressBuilder.render(OsFamily.WINDOWS), CharStreams.toString(Resources.newReaderSupplier(Resources
.getResource("test_get_ip_address." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8)));
}
*/
}

View File

@ -0,0 +1,23 @@
package org.jclouds.virtualbox.domain;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.ShellToken;
import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import com.google.common.io.Resources;
@Test(groups = "unit")
public class InstallGuestAdditionsTest {
@Test
public void testUnix() throws IOException {
InstallGuestAdditions statement = new InstallGuestAdditions("4.1.6");
assertEquals(statement.render(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources
.getResource("test_install_guest_additions." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)));
}
}

View File

@ -10,7 +10,7 @@ function abort {
function exportIpAddressFromVmNamed {
unset FOUND_IP_ADDRESS;
[ $# -eq 1 ] || {
abort "installGuestAdditions requires virtual machine name parameter"
abort "exportIpAddressFromVmNamed requires virtual machine name parameter"
return 1
}
local VMNAME="$0"; shift

View File

@ -0,0 +1,31 @@
#!/bin/bash
set +u
shopt -s xpg_echo
shopt -s expand_aliases
unset PATH JAVA_HOME LD_LIBRARY_PATH
function abort {
echo "aborting: $@" 1>&2
exit 1
}
function installGuestAdditions {
unset OSNAME;
[ $# -eq 1 ] || {
abort "installGuestAdditions requires virtual machine name parameter"
return 1
}
local OSNAME=`lsb_release -d -s | cut -d ' ' -f 1`; shift
if [ $OSNAME = 'Ubuntu' ]
then
echo "OS Name is Ubuntu"
`apt-get install build-essential module-assistant && m-a prepare -i`
fi
return 0
}
export PATH=/usr/ucb/bin:/bin:/sbin:/usr/bin:/usr/sbin
cd /tmp
wget http://download.virtualbox.org/virtualbox/4.1.6/VBoxGuestAdditions_4.1.6.iso
mount -o loop VBoxGuestAdditions_4.1.6.iso /mnt
installGuestAdditions || exit 1
sh /mnt/VBoxLinuxAdditions.run
umount /mnt
exit 0