windows tests: produce a test script to embed into main.nix

This commit is contained in:
Astro 2019-04-15 01:12:48 +02:00 committed by Sébastien Bourdeauducq
parent 005abc393d
commit d537a96207
4 changed files with 68 additions and 79 deletions

View File

@ -3,50 +3,32 @@
qemuMem ? "2G", qemuMem ? "2G",
testTimeout ? 180, testTimeout ? 180,
artiqPkg ? import ../conda-artiq.nix { inherit pkgs; }, artiqPkg ? import ../conda-artiq.nix { inherit pkgs; },
testCommand ? "python -m unittest discover -v artiq.test",
}: }:
with pkgs; with pkgs;
let let
artiqSrc = <artiqSrc>; escape = builtins.replaceStrings [ "\\" ] [ "\\\\" ];
artiqVersion =
pkgs.runCommand "artiq-version" {
buildInputs = [ pkgs.nix pkgs.git ];
} ''
REV=`git --git-dir ${artiqSrc}/.git rev-parse HEAD`
echo \"5e.`cut -c1-8 <<< $REV`\" > $out
'';
generateTestOkHash =
pkgs.runCommand "generate-test-ok-hash" {
buildInputs = [ pkgs.nix ];
} ''
TMPDIR=`mktemp -d`
cp ${artiqVersion} $TMPDIR/passed
HASH=`nix-hash --type sha256 --base32 $TMPDIR`
echo \"$HASH\" > $out
'';
qemu = import ./qemu.nix { qemu = import ./qemu.nix {
inherit pkgs qemuMem; inherit pkgs qemuMem;
diskImage = "c.img"; diskImage = "c.img";
}; };
ssh = qemu.ssh; # Double-escape because we produce a script from a shell heredoc
ssh = cmd: qemu.ssh (escape cmd);
scp = qemu.scp; scp = qemu.scp;
condaEnv = "artiq"; condaEnv = "artiq";
in
makeTest = name: testCommand: stdenv.mkDerivation {
stdenv.mkDerivation { name = "windows-test-runner";
name = "windows-test-conda-artiq-${name}"; src = ./.;
outputHashAlgo = "sha256";
outputHashMode = "recursive";
outputHash = import generateTestOkHash;
__hydraRetry = false;
phases = [ "buildPhase" ];
buildInputs = qemu.inputs; buildInputs = qemu.inputs;
buildPhase = '' dontBuild = true;
installPhase = ''
mkdir -p $out/bin
cat > $out/bin/run.sh << EOF
# +1 day from last modification of the disk image # +1 day from last modification of the disk image
CLOCK=$(date -Is -d @$(expr $(stat -c %Y ${diskImage}) + 86400)) CLOCK=$(date -Is -d @$(expr $(stat -c %Y ${diskImage}) + 86400))
${qemu.runQemu true [ ${qemu.runQemu true [
@ -68,28 +50,13 @@ let
# Allow tests to run for 2 minutes # Allow tests to run for 2 minutes
${ssh "shutdown -s -t ${toString testTimeout}"} ${ssh "shutdown -s -t ${toString testTimeout}"}
${testCommand} ${ssh "anaconda\\scripts\\activate ${condaEnv} && ${testCommand}"}
# Abort timeouted shutdown # Abort timeouted shutdown
${ssh "shutdown -a"} ${ssh "shutdown -a"}
# Power off immediately # Power off immediately
${ssh "shutdown -p -f"} ${ssh "shutdown -p -f"}
EOF
mkdir $out chmod a+x $out/bin/run.sh
cp ${artiqVersion} $out/passed
''; '';
};
in {
standalone =
makeTest "standalone"
(ssh "anaconda\\scripts\\activate ${condaEnv} && python -m unittest discover -v artiq.test");
kc705 =
makeTest "kc705"
# This weirdly mangled syntax is legit for Windows
(ssh (builtins.replaceStrings [ "\n" ] [ "" ] ''
anaconda\scripts\activate ${condaEnv}
&& set ARTIQ_ROOT=%cd%\anaconda\envs\artiq-env\Lib\site-packages\artiq\examples\kc705_nist_clock
&& set ARTIQ_LOW_LATENCY=1
&& python -m unittest discover -v artiq.test.coredevice
''));
} }

View File

@ -15,12 +15,13 @@ let
sha256 = "1f9icm5rwab6l1f23a70dw0qixzrl62wbglimip82h4zhxlh3jfj"; sha256 = "1f9icm5rwab6l1f23a70dw0qixzrl62wbglimip82h4zhxlh3jfj";
}; };
escape = builtins.replaceStrings [ "\\" ] [ "\\\\" ];
qemu = import ./qemu.nix { qemu = import ./qemu.nix {
inherit pkgs qemuMem; inherit pkgs qemuMem;
diskImage = "c.img"; diskImage = "c.img";
}; };
# Double-escape because we produce a script from a shell heredoc # Double-escape because we produce a script from a shell heredoc
ssh = cmd: qemu.ssh (qemu.escape cmd); ssh = cmd: qemu.ssh (escape cmd);
scp = qemu.scp; scp = qemu.scp;
sshCondaEnv = cmd: ssh "anaconda\\scripts\\activate && ${cmd}"; sshCondaEnv = cmd: ssh "anaconda\\scripts\\activate && ${cmd}";

View File

@ -26,14 +26,13 @@ let
argStr = builtins.concatStringsSep " " (args ++ extraArgs); argStr = builtins.concatStringsSep " " (args ++ extraArgs);
in "qemu-system-x86_64 ${argStr}"; in "qemu-system-x86_64 ${argStr}";
escape = builtins.replaceStrings [ "\\" ] [ "\\\\" ];
sshOpts = "-o StrictHostKeyChecking=accept-new -o UserKnownHostsFile=/tmp/known_hosts"; sshOpts = "-o StrictHostKeyChecking=accept-new -o UserKnownHostsFile=/tmp/known_hosts";
ssh = cmd: '' ssh = cmd: ''
echo ssh windows '${escape cmd}' echo ssh windows '${cmd}'
${sshpass}/bin/sshpass -p${sshPassword} -- \ ${sshpass}/bin/sshpass -p${sshPassword} -- \
${openssh}/bin/ssh -np 2022 ${sshOpts} \ ${openssh}/bin/ssh -np 2022 ${sshOpts} \
${sshUser}@localhost \ ${sshUser}@localhost \
'${escape cmd}' '${cmd}'
''; '';
scp = src: target: '' scp = src: target: ''
echo "Copy ${src} to ${target}" echo "Copy ${src} to ${target}"
@ -44,6 +43,6 @@ let
in in
{ {
inherit qemu-img runQemu escape ssh scp; inherit qemu-img runQemu ssh scp;
inputs = [ qemu openssh sshpass ]; inputs = [ qemu openssh sshpass ];
} }

View File

@ -30,6 +30,11 @@ let
''; '';
artiqpkgs = import "${generatedNix}/default.nix" { inherit pkgs; }; artiqpkgs = import "${generatedNix}/default.nix" { inherit pkgs; };
artiqVersion = import "${generatedNix}/pkgs/artiq-version.nix"; artiqVersion = import "${generatedNix}/pkgs/artiq-version.nix";
windowsRunner = overrides:
import "${generatedNix}/windows" ({
inherit pkgs;
artiqPkg = artiqpkgs.conda-artiq;
} // overrides);
jobs = (builtins.mapAttrs (key: value: pkgs.lib.hydraJob value) artiqpkgs) // { jobs = (builtins.mapAttrs (key: value: pkgs.lib.hydraJob value) artiqpkgs) // {
# This is in the example in the ARTIQ manual - precompile it to speed up # This is in the example in the ARTIQ manual - precompile it to speed up
# installation for users. # installation for users.
@ -46,13 +51,26 @@ in
constituents = builtins.attrValues jobs; constituents = builtins.attrValues jobs;
}; };
windows-no-hardware-tests = pkgs.stdenv.mkDerivation {
name = "windows-no-hardware-tests";
src = ./.;
buildInputs = [ artiqpkgs.conda-artiq ];
phases = [ "buildPhase" ];
buildPhase = ''
${windowsRunner {}}/bin/run.sh
mkdir $out
touch $out/passed
'';
};
# HACK: Abuse fixed-output derivations to escape the sandbox and run the hardware # HACK: Abuse fixed-output derivations to escape the sandbox and run the hardware
# unit tests, all integrated in the Hydra interface. # unit tests, all integrated in the Hydra interface.
# One major downside of this hack is the tests are only run when generateTestOkHash # One major downside of this hack is the tests are only run when generateTestOkHash
# changes, i.e. when the ARTIQ version changes (and not the dependencies). # changes, i.e. when the ARTIQ version changes (and not the dependencies).
# Impure derivations, when they land in Nix/Hydra, should improve the situation. # Impure derivations, when they land in Nix/Hydra, should improve the situation.
kc705-tests = pkgs.stdenv.mkDerivation { extended-tests = pkgs.stdenv.mkDerivation {
name = "kc705-tests"; name = "extended-tests";
outputHashAlgo = "sha256"; outputHashAlgo = "sha256";
outputHashMode = "recursive"; outputHashMode = "recursive";
@ -79,9 +97,13 @@ in
sleep 15 sleep 15
# ping: socket: Operation not permitted # ping: socket: Operation not permitted
#ping kc705-1 -c10 -w30 #ping kc705-1 -c10 -w30
export ARTIQ_ROOT=`python -c "import artiq; print(artiq.__path__[0])"`/examples/kc705_nist_clock export ARTIQ_ROOT=`python -c "import artiq; print(artiq.__path__[0])"`/examples/kc705_nist_clock
export ARTIQ_LOW_LATENCY=1 export ARTIQ_LOW_LATENCY=1
python -m unittest discover -v artiq.test.coredevice python -m unittest discover -v artiq.test.coredevice
${windowsRunner { testCommand = "set ARTIQ_ROOT=%cd%\\anaconda\\envs\\artiq-env\\Lib\\site-packages\\artiq\\examples\\kc705_nist_clock&&set ARTIQ_LOW_LATENCY=1&&python -m unittest discover -v artiq.test.coredevice"; }}/bin/run.sh
mkdir $out mkdir $out
cp ${generatedNix}/pkgs/artiq-version.nix $out/passed cp ${generatedNix}/pkgs/artiq-version.nix $out/passed
''; '';