2
0
genodepkgs/tests/driver-hw.nix
Emery Hemingway d37bff8d16 Derive QEMU memory for tests from init configuration
Boot QEMU tests with the minimum required memory. This value can be
derived from the test configuration rather than set manually.
2020-02-19 13:26:15 +01:00

177 lines
5.5 KiB
Nix

# SPDX-License-Identifier: CC0-1.0
{ system, apps, testPkgs, hostPkgs, lib, depot }:
let
testDriver = with hostPkgs;
stdenv.mkDerivation {
name = "hw-genode-test-driver";
preferLocalBuild = true;
buildInputs = [ makeWrapper expect ];
dontUnpack = true;
installPhase = ''
install -Dm555 ${./hw-test-driver.exp} $out/bin/genode-test-driver
wrapProgram $out/bin/genode-test-driver \
--prefix PATH : "${lib.makeBinPath [ expect coreutils ]}"
'';
};
runTests = driver:
hostPkgs.stdenv.mkDerivation {
name = "hw-" + driver.testName;
preferLocalBuild = true;
buildCommand = ''
mkdir -p $out/nix-support
${driver}/bin/genode-test-driver | tee $out/log
touch $out/nix-support
echo "report testlog $out log" >> $out/nix-support/hydra-build-products
'';
};
defaultScript = ''run_genode_until {child "init" exited with exit value 0} 60'';
mkTest = { name ? "unamed", testScript ? defaultScript, testConfig, testInputs ? [ ]
, env ? { }, ... # TODO: remove ...
}@t:
let
manifest = lib.mergeManifests (with testPkgs;
[ base-hw-pc genode.os sotest-producer ]
++ testInputs);
env' = {
DHALL_PRELUDE = "${testPkgs.dhallPrelude}/package.dhall";
DHALL_GENODE = "${testPkgs.dhallGenode}/package.dhall";
MANIFEST = manifest;
} // env;
image = apps.hw-image.function env'
"${./driver-hw-config.dhall} ${testConfig}";
baseSetup = ''
##
# Wait for a specific output of a already running spawned proce
#
proc wait_for_output { wait_for_re timeout_value running_spawn_id } {
global output
if {$wait_for_re == "forever"} {
set timeout -1
interact {
\003 {
send_user "Expect: 'interact' received 'strg+c' and was cancelled\n";
exit
}
-i $running_spawn_id
}
} else {
set timeout $timeout_value
}
expect {
-i $running_spawn_id -re $wait_for_re { }
eof { puts stderr "Error: Spawned process died unexpectedly"; exit -1 }
timeout { puts stderr "Error: Test execution timed out"; exit -1 }
}
set output $expect_out(buffer)
}
proc run_genode_until {{wait_for_re forever} {timeout_value 0} {running_spawn_id -1}} {
#
# If a running_spawn_id is specified, wait for the expected output
#
if {$running_spawn_id != -1} {
wait_for_output $wait_for_re $timeout_value $running_spawn_id
return
}
global env
global spawn_id
spawn ${hostPkgs.qemu_test}/bin/qemu-system-x86_64 \
-machine q35 -serial mon:stdio -nographic \
-m size=[expr ($env(TEST_RAM) / 1048576) + 16] \
-kernel "${testPkgs.bender}" \
-initrd "${image}"
wait_for_output $wait_for_re $timeout_value $spawn_id
}
# TODO: not in TCL
global env
set out $env(out)
'';
driver = with hostPkgs;
buildPackages.runCommand "genode-test-driver-${name}" {
buildInputs = [ makeWrapper expect ];
inherit baseSetup testConfig testScript;
preferLocalBuild = true;
testName = name;
DHALL_GENODE = "${testPkgs.dhallGenode}/package.dhall";
MANIFEST = manifest;
} ''
mkdir -p $out/bin
echo "$testScript" > $out/test-script
echo "$baseSetup" > $out/base-setup
eval $(${apps.dhall-to-bash.program} --declare "TEST_RAM" <<< "${./boot-ram.dhall} ${testConfig}")
ln -s ${testDriver}/bin/genode-test-driver $out/bin/
wrapProgram $out/bin/genode-test-driver \
--set testScript "$testScript" \
--set baseSetup "$baseSetup" \
--set TEST_RAM $TEST_RAM \
'';
passMeta = drv:
drv
// lib.optionalAttrs (t ? meta) { meta = (drv.meta or { }) // t.meta; };
test = passMeta (runTests driver);
in test // {
inherit driver image test manifest;
config = hostPkgs.runCommand (name + ".dhall") env' ''
export XDG_CACHE_HOME=''${TMPDIR:-/tmp}
${apps.dhall.program} <<< \
"(${./driver-hw-config.dhall} ${testConfig})" > $out
'';
iso = apps.hw-iso.function env'
"(${./driver-hw-config.dhall} ${testConfig})";
xml = hostPkgs.runCommand (name + ".config") env' ''
export XDG_CACHE_HOME=''${TMPDIR:-/tmp}
${apps.render-init.program} \
"(${./driver-hw-config.dhall} ${testConfig}).config" > $out'';
sotest = hostPkgs.runCommand "hw-${name}-sotest" env' ''
cp "${testPkgs.bender}" bender
cp ${image} image.elf
mkdir -p $out/nix-support
${hostPkgs.zip}/bin/zip "$out/binaries.zip" \
bender image.elf
${apps.dhall-to-yaml.program} < ${
./sotest_hw_config.dhall
} > "$out/sotest_config.yaml"
echo file zip $out/binaries.zip >> "$out/nix-support/hydra-build-products"
echo file config $out/sotest_config.yaml >> "$out/nix-support/hydra-build-products"
'';
};
in {
callTest = path: args:
(import path ({
testEnv = {
inherit mkTest lib;
isLinux = false;
isNova = true;
};
pkgs = testPkgs;
inherit depot hostPkgs;
} // args));
}