Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor “tests/default.nix” file #235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 55 additions & 24 deletions tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down