From 2c604ed1693ab7aafb392fc5e4d2fb8d85731126 Mon Sep 17 00:00:00 2001 From: Viacheslav Lotsmanov Date: Fri, 21 May 2021 03:09:30 +0300 Subject: [PATCH] =?UTF-8?q?Refactor=20=E2=80=9Ctests/default.nix=E2=80=9D?= =?UTF-8?q?=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/default.nix | 79 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 24 deletions(-) diff --git a/tests/default.nix b/tests/default.nix index 2e4a9c2b0..148fab0e8 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -5,59 +5,90 @@ let packs = (import ../.).packs; + # “e” stands for “executable”. + # Attribute name is executable name. + # Attribute value is an escaped full path in Nix store to an executable. + # For instance { dc = "'/nix/store/…-bc-…/bin/dc'"; } + e = builtins.mapAttrs (n: v: pkgs.lib.escapeShellArg "${v}/bin/${n}") { + cat = pkgs.coreutils; + cp = pkgs.coreutils; + id = pkgs.coreutils; + ln = pkgs.coreutils; + sleep = pkgs.coreutils; + timeout = pkgs.coreutils; + + dc = pkgs.bc; + grep = pkgs.gnugrep; + tar = pkgs.gnutar; + xargs = pkgs.findutils; + + pgrep = pkgs.procps; + ps = pkgs.procps; + }; + + guardDependencies = + builtins.concatStringsSep "\n" (map (executable: '' + if ! [[ -r ${executable} ]] || ! [[ -e ${executable} ]]; then + >&2 printf 'Executable "%s" is not found!\n' ${executable} + exit 1 + fi + '') (builtins.attrValues e)); + smokeTest = pack: pkgs.runCommandLocal "smoketest" { server = pack.server; world = ./testdata/SmokeTest.tar.gz; props = ./testdata/server.properties; - buildInputs = with pkgs; [ jre8 rsync procps psmisc tmux ]; + buildInputs = with pkgs; [ jre8 rsync tmux ]; # Enable web access __noChroot = 1; } '' - set -e + set -Eeuo pipefail || exit - ln -s $server server - tar xvzf $world - cat $props > server.properties + # Guard dependencies first + ${guardDependencies} + + ${e.ln} -s -- "$server" server + ${e.tar} -xvz --file="$world" + ${e.cat} -- "$props" > server.properties echo -n server-port= >> server.properties - id -u | ${pkgs.bc}/bin/dc -e '? 1000 % 30000 + p' >> server.properties - cat server.properties + ${e.id} -u | ${e.dc} -e '? 1000 % 30000 + p' >> server.properties + ${e.cat} server.properties echo 'eula=true' > eula.txt export SKIP_TMUX=1 export KILL_PROMETHEUS=1 - echo | timeout -s 9 900 bash server/start.sh -Dfml.queryResult=confirm & - sleep 3 + echo | ${e.timeout} -s 9 900 bash server/start.sh -Dfml.queryResult=confirm & + ${e.sleep} 3 terminate() { + set -Eeuo pipefail || exit echo 'Exiting.' - set +e - pgrep -s 0 | grep -v "^$$$" | xargs kill - set -e + ${e.pgrep} -s 0 | (${e.grep} -v "^$$$" || true) | ${e.xargs} kill echo 'Exited.' } time=0 while true; do - grep 'Task Failed Successfully' logs/latest.log && { + if ${e.grep} 'Task Failed Successfully' logs/latest.log; then echo 'Smoke test successful. Exiting.' terminate - cp logs/latest.log $out + ${e.cp} logs/latest.log -- "$out" exit 0 - } - ps -u $UID | grep -q java || { - echo 'Java process unexpectedly terminated' - exit 1 - } - time=$(($time + 1)) - if [[ $time -gt 1000 ]]; then - echo 'Exiting with timeout' - terminate + fi + if ! ${e.ps} -u "$UID" | ${e.grep} -q java; then + >&2 echo 'Java process unexpectedly terminated' + exit 1 + fi + time=$(( time + 1 )) + if (( time > 1000 )); then + >&2 echo 'Exiting with timeout' + >&2 terminate exit 1 fi - sleep 1 + ${e.sleep} 1 done ''; in