Add docker-fence v1 with tests and docs
This commit is contained in:
commit
305cb562f8
46 files changed
+5473
No files matched your search
@@ -0,0 +1,238 @@
|
||||
#!/usr/bin/env bats
|
||||
# install.sh: installation and removal under a temporary prefix.
|
||||
|
||||
load helpers
|
||||
|
||||
bats_require_minimum_version 1.5.0
|
||||
|
||||
INSTALLER=""
|
||||
|
||||
setup() {
|
||||
setup_env
|
||||
INSTALLER="$REPO/install.sh"
|
||||
PREFIX="$BATS_TEST_TMPDIR/prefix"
|
||||
}
|
||||
|
||||
need_root() {
|
||||
[ "$EUID" -eq 0 ] || skip "install.sh needs root"
|
||||
}
|
||||
|
||||
# State of the real configuration file, read only: checksum, or "absent".
|
||||
config_state() {
|
||||
if [ -e /etc/docker-fence/fence.conf ]; then
|
||||
sum_of /etc/docker-fence/fence.conf
|
||||
else
|
||||
echo absent
|
||||
fi
|
||||
}
|
||||
|
||||
@test "install.sh refuses to run without root" {
|
||||
if [ "$EUID" -eq 0 ]; then
|
||||
command -v setpriv >/dev/null || skip "setpriv not found"
|
||||
run setpriv --reuid=65534 --regid=65534 --clear-groups bash "$INSTALLER" --prefix "$PREFIX"
|
||||
else
|
||||
run bash "$INSTALLER" --prefix "$PREFIX"
|
||||
fi
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"must be run as root"* ]]
|
||||
[ ! -e "$PREFIX" ]
|
||||
}
|
||||
|
||||
@test "install.sh installs the tool and the libraries, and prints them" {
|
||||
need_root
|
||||
run bash "$INSTALLER" --prefix "$PREFIX"
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$(stat -c %a "$PREFIX/lib/docker-fence/docker-fence")" = 755 ]
|
||||
local f
|
||||
for f in "$REPO"/lib/*.sh; do
|
||||
cmp "$f" "$PREFIX/lib/docker-fence/lib/$(basename "$f")"
|
||||
[ "$(stat -c %a "$PREFIX/lib/docker-fence/lib/$(basename "$f")")" = 644 ]
|
||||
[[ "$output" == *"installed $PREFIX/lib/docker-fence/lib/$(basename "$f")"* ]]
|
||||
done
|
||||
[ "$(readlink "$PREFIX/bin/docker-fence")" = "$PREFIX/lib/docker-fence/docker-fence" ]
|
||||
[[ "$output" == *"installed $PREFIX/lib/docker-fence/docker-fence"* ]]
|
||||
[[ "$output" == *"linked $PREFIX/bin/docker-fence -> $PREFIX/lib/docker-fence/docker-fence"* ]]
|
||||
# The installed command works through the symlink.
|
||||
run "$PREFIX/bin/docker-fence" --version
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == "docker-fence "* ]]
|
||||
run "$PREFIX/bin/docker-fence" -c "$REPO/examples/fence.conf" --help
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "install.sh twice gives the same result and removes stale libraries" {
|
||||
need_root
|
||||
bash "$INSTALLER" --prefix "$PREFIX"
|
||||
touch "$PREFIX/lib/docker-fence/lib/old.sh"
|
||||
run bash "$INSTALLER" --prefix "$PREFIX"
|
||||
[ "$status" -eq 0 ]
|
||||
[ ! -e "$PREFIX/lib/docker-fence/lib/old.sh" ]
|
||||
[[ "$output" == *"removed stale $PREFIX/lib/docker-fence/lib/old.sh"* ]]
|
||||
diff <(cd "$REPO/lib" && ls) <(cd "$PREFIX/lib/docker-fence/lib" && ls)
|
||||
}
|
||||
|
||||
@test "install.sh never touches /etc/docker-fence/fence.conf" {
|
||||
need_root
|
||||
! grep -nE '(>|rm|cp|mv|install|ln|chmod|chown|mkdir)[^#]*\$CONFIG' "$INSTALLER"
|
||||
local before
|
||||
before="$(config_state)"
|
||||
bash "$INSTALLER" --prefix "$PREFIX"
|
||||
[ "$(config_state)" = "$before" ]
|
||||
bash "$INSTALLER" --prefix "$PREFIX" --uninstall
|
||||
[ "$(config_state)" = "$before" ]
|
||||
}
|
||||
|
||||
@test "install.sh --uninstall removes what was installed, nothing else" {
|
||||
need_root
|
||||
bash "$INSTALLER" --prefix "$PREFIX"
|
||||
touch "$PREFIX/bin/other-tool"
|
||||
run bash "$INSTALLER" --prefix "$PREFIX" --uninstall
|
||||
[ "$status" -eq 0 ]
|
||||
[ ! -e "$PREFIX/bin/docker-fence" ]
|
||||
[ ! -e "$PREFIX/lib/docker-fence" ]
|
||||
[ -e "$PREFIX/bin/other-tool" ]
|
||||
[[ "$output" == *"removed $PREFIX/bin/docker-fence"* ]]
|
||||
[[ "$output" == *"removed $PREFIX/lib/docker-fence/lib/config.sh"* ]]
|
||||
[[ "$output" == *"removed $PREFIX/lib/docker-fence"* ]]
|
||||
}
|
||||
|
||||
@test "install.sh --uninstall when nothing is installed" {
|
||||
need_root
|
||||
run bash "$INSTALLER" --prefix "$PREFIX" --uninstall
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"nothing to remove in $PREFIX"* ]]
|
||||
}
|
||||
|
||||
@test "install.sh keeps a docker-fence command it did not install" {
|
||||
need_root
|
||||
mkdir -p "$PREFIX/bin"
|
||||
printf '#!/bin/sh\n' >"$PREFIX/bin/docker-fence"
|
||||
run bash "$INSTALLER" --prefix "$PREFIX"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"is not a symlink"* ]]
|
||||
run bash "$INSTALLER" --prefix "$PREFIX" --uninstall
|
||||
[ "$status" -eq 0 ]
|
||||
[ -f "$PREFIX/bin/docker-fence" ]
|
||||
[[ "$output" == *"kept $PREFIX/bin/docker-fence"* ]]
|
||||
|
||||
rm "$PREFIX/bin/docker-fence"
|
||||
ln -s /usr/bin/true "$PREFIX/bin/docker-fence"
|
||||
run bash "$INSTALLER" --prefix "$PREFIX" --uninstall
|
||||
[ -L "$PREFIX/bin/docker-fence" ]
|
||||
[[ "$output" == *"points elsewhere"* ]]
|
||||
}
|
||||
|
||||
@test "install.sh options: --help, invalid prefix, unknown option" {
|
||||
run bash "$INSTALLER" --help
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == "Usage: install.sh"* ]]
|
||||
run bash "$INSTALLER" --prefix relative/dir
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"absolute path"* ]]
|
||||
run bash "$INSTALLER" --prefix '/opt/a b'
|
||||
[ "$status" -eq 1 ]
|
||||
run bash "$INSTALLER" --prefix /
|
||||
[ "$status" -eq 1 ]
|
||||
run bash "$INSTALLER" --prefix
|
||||
[ "$status" -eq 1 ]
|
||||
run bash "$INSTALLER" --frobnicate
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"unknown option: --frobnicate"* ]]
|
||||
}
|
||||
|
||||
@test "install.sh --prefix=DIR form" {
|
||||
need_root
|
||||
run bash "$INSTALLER" --prefix="$PREFIX"
|
||||
[ "$status" -eq 0 ]
|
||||
[ -L "$PREFIX/bin/docker-fence" ]
|
||||
}
|
||||
|
||||
@test "install.sh lists exactly the files of the tool" {
|
||||
local listed actual
|
||||
listed="$(sed -n '/^readonly FILES=(/,/^)/p' "$INSTALLER" | sed '1d;$d' | tr -d ' ' | sort)"
|
||||
actual="$( (cd "$REPO" && printf '%s\n' docker-fence lib/*.sh) | sort)"
|
||||
[ "$listed" = "$actual" ]
|
||||
}
|
||||
|
||||
@test "install.sh writes no firewall rule and explains the next steps" {
|
||||
need_root
|
||||
: >"$STUB_LOG"
|
||||
run bash "$INSTALLER" --prefix "$PREFIX"
|
||||
[ "$status" -eq 0 ]
|
||||
# The fake ufw, iptables, systemd-run and systemctl are first in PATH.
|
||||
! grep -qE '^(ufw|iptables|systemd-run|systemctl) ' "$STUB_LOG"
|
||||
[ ! -s "$KERNEL" ]
|
||||
[[ "$output" == *"No firewall rule was written."* ]]
|
||||
[[ "$output" == *"/etc/docker-fence/fence.conf"* ]]
|
||||
[[ "$output" == *" init "* || "$output" == *"already exists"* ]]
|
||||
[[ "$output" == *" check "* ]]
|
||||
[[ "$output" == *" apply "* ]]
|
||||
[[ "$output" == *" confirm "* ]]
|
||||
[[ "$output" == *" rules "* ]]
|
||||
}
|
||||
|
||||
@test "install.sh alone downloads the files (curl, no git)" {
|
||||
need_root
|
||||
command -v curl >/dev/null || skip "curl not found"
|
||||
mkdir -p "$BATS_TEST_TMPDIR/alone"
|
||||
cp "$INSTALLER" "$BATS_TEST_TMPDIR/alone/install.sh"
|
||||
run bash "$BATS_TEST_TMPDIR/alone/install.sh" --prefix "$PREFIX" --source-url "file://$REPO"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"downloading docker-fence from file://$REPO"* ]]
|
||||
cmp "$REPO/docker-fence" "$PREFIX/lib/docker-fence/docker-fence"
|
||||
local f
|
||||
for f in "$REPO"/lib/*.sh; do
|
||||
cmp "$f" "$PREFIX/lib/docker-fence/lib/$(basename "$f")"
|
||||
done
|
||||
run "$PREFIX/bin/docker-fence" --version
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "curl ... | sudo bash: install.sh read from a pipe" {
|
||||
need_root
|
||||
command -v curl >/dev/null || skip "curl not found"
|
||||
run bash -c 'bash -s -- --prefix "$1" --source-url "file://$2" <"$3"' _ "$PREFIX" "$REPO" "$INSTALLER"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"downloading docker-fence"* ]]
|
||||
[ -L "$PREFIX/bin/docker-fence" ]
|
||||
"$PREFIX/bin/docker-fence" --version
|
||||
}
|
||||
|
||||
@test "install.sh refuses a download that is not the expected script" {
|
||||
need_root
|
||||
command -v curl >/dev/null || skip "curl not found"
|
||||
local bad="$BATS_TEST_TMPDIR/bad"
|
||||
mkdir -p "$bad/lib"
|
||||
cp "$REPO"/lib/*.sh "$bad/lib/"
|
||||
printf '<!doctype html><title>Making sure you are not a bot</title>\n' >"$bad/docker-fence"
|
||||
run bash -s -- --prefix "$PREFIX" --source-url "file://$bad" <"$INSTALLER"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"unexpected content for docker-fence"* ]]
|
||||
[ ! -e "$PREFIX" ]
|
||||
|
||||
cp "$REPO/docker-fence" "$bad/docker-fence"
|
||||
rm "$bad/lib/listing.sh"
|
||||
run bash -s -- --prefix "$PREFIX" --source-url "file://$bad" <"$INSTALLER"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"download failed"*"lib/listing.sh"* ]]
|
||||
[ ! -e "$PREFIX" ]
|
||||
}
|
||||
|
||||
@test "install.sh refuses invalid source URL and ref names" {
|
||||
run bash "$INSTALLER" --source-url http://example.invalid/x
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"https:// or file:// URL"* ]]
|
||||
run bash "$INSTALLER" --tag 'v1;id'
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"invalid branch or tag name"* ]]
|
||||
run bash "$INSTALLER" --branch
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "install.sh --uninstall says the firewall is not changed" {
|
||||
need_root
|
||||
bash "$INSTALLER" --prefix "$PREFIX"
|
||||
run bash "$INSTALLER" --prefix "$PREFIX" --uninstall
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"No firewall rule was changed"* ]]
|
||||
}
|
||||
Reference in new issue
Block a user