Skip to content

Instantly share code, notes, and snippets.

@erikarvstedt
Last active August 31, 2018 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikarvstedt/d37ba23e336fbcf30f54daafde3085ed to your computer and use it in GitHub Desktop.
Save erikarvstedt/d37ba23e336fbcf30f54daafde3085ed to your computer and use it in GitHub Desktop.
#! /usr/bin/env bash
set -euo pipefail
shopt -s nullglob
#--------------------------------------------------------------------
nixpkgs=/path/to/nixpkgs
containerName=test-pl
containerAddressOnHost=10.50.200.1;
containerAddress=10.50.200.2;
paperlessServer=$containerAddress:28981
#--------------------------------------------------------------------
if [[ $PATH != *paperlessTestEnv* ]]; then
nix-build --out-link /tmp/paperlessTestEnv -E '
with (import <nixpkgs> {});
buildEnv {
name = "paperlessTestEnv";
paths = [ extra-container jq imagemagick ];
}
' > /dev/null
export PATH=/tmp/paperlessTestEnv/bin${PATH:+:}$PATH
fi
containerDir=/var/lib/containers/$containerName
paperlessDir=$containerDir/var/lib/paperless
consumeDir=$paperlessDir/consume
cleanup() {
sudo extra-container destroy $containerName
}
trap "cleanup" EXIT
reportError() {
echo "Error on line $1"
}
trap 'reportError $LINENO' ERR
testMatches() {
actual="$1"
expected="$2"
if [[ $actual != $expected ]]; then
echo
echo 'Pattern does not match'
echo 'Expected:'
echo "$expected"
echo
echo 'Actual:'
echo "$actual"
echo
return 1
fi
}
doWait() {
durationSecs=$1
condition="$2"
SECONDS=0
while true; do
eval "$condition" && return 0
elapsed=$SECONDS
(( $elapsed >= $durationSecs )) && return 1
sleep 0.1
done
}
makeTestDoc() {
sudo convert -size 400x40 xc:white -font "DejaVu-Sans" -pointsize 20 -fill black -annotate +5+20 \
"hello world 16-10-2005" "$1/doc.png"
}
makeContainer() {
# echo "Container config"; cat <<EOF
NIX_PATH=nixpkgs=$nixpkgs sudo extra-container create --start <<EOF
{
containers.$containerName = {
privateNetwork = true;
hostAddress = "$containerAddressOnHost";
localAddress = "$containerAddress";
config = $(cat);
};
}
EOF
}
remakeContainer() {
sudo extra-container destroy $containerName
makeContainer
}
runInContainer() {
sudo nixos-container run $containerName -- "$@"
}
#--------------------------------------------------------------------
echo "Test basic consuming"
remakeContainer <<'EOF'
{
services.paperless = {
enable = true;
address = "0.0.0.0";
};
}
EOF
doWait 5 'curl -s $paperlessServer'
(($(curl -s $paperlessServer/api/documents/ | jq .count) == 0 ))
makeTestDoc $consumeDir
doWait 5 '(($(curl -s $paperlessServer/api/documents/ | jq .count) == 1 ))'
makeTestDoc $consumeDir
doWait 5 '(($(curl -s $paperlessServer/api/documents/ | jq .count) == 2 ))'
#--------------------------------------------------------------------
echo "Test restarting and auto migrations"
getNumExecutedMigrations() {
runInContainer journalctl -u paperless-consumer | grep "Running migrations" | wc -l
}
getServerStopCount() {
runInContainer journalctl -u paperless-server | grep "Stopped Paperless" | wc -l
}
# The server has never been stopped
(( $(getServerStopCount) == 0 ))
# One initial migration should have run
(( $(getNumExecutedMigrations) == 1 ))
runInContainer systemctl restart paperless-consumer
# Restarting paperless with the package source unchanged should trigger no migration
(( $(getNumExecutedMigrations) == 1 ))
# Restarting the consumer should have stopped (and restarted) the server
(( $(getServerStopCount) == 1 ))
makeContainer <<'EOF'
{ pkgs, ... }: {
services.paperless = {
enable = true;
address = "0.0.0.0";
package = let
dummySrc = pkgs.runCommand "dummySrc" {} ''
ln -s ${pkgs.paperless.src} $out
'';
in
(pkgs.paperless.override { enableTests = false; })
.overrideAttrs (_: { src = dummySrc; });
};
}
EOF
# Paperless was restarted with `dummySrc` as the new package source which should trigger a migration
(( $(getNumExecutedMigrations) == 2 ))
#--------------------------------------------------------------------
echo "Test encryption with passphrase file"
remakeContainer <<'EOF'
{ config, ... }: {
environment.etc.passphrase = {
text = "mypasswd";
user = config.services.paperless.user;
mode = "0600";
};
services.paperless = {
enable = true;
address = "0.0.0.0";
extraConfig = {
PAPERLESS_PASSPHRASE = "$(< /etc/passphrase)";
};
};
}
EOF
# Check that the passphrase env var is set
consumerPID=$(out=$(runInContainer systemctl show paperless-consumer -p MainPID) && echo ${out/MainPID=})
runInContainer cat /proc/$consumerPID/environ | grep -q PAPERLESS_PASSPHRASE=mypasswd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment