#!/usr/bin/env bats # Command line, init, bash version, code quality. load helpers bats_require_minimum_version 1.5.0 setup() { setup_env } @test "--help in English and French" { run "$FENCE" --help [ "$status" -eq 0 ] [[ "$output" == "Usage: docker-fence"* ]] [[ "$output" == *"Exit codes"* ]] run "$FENCE" --lang fr --help [[ "$output" == "Usage : docker-fence"* ]] FENCE_LANG=fr run "$FENCE" -h [[ "$output" == "Usage : docker-fence"* ]] } @test "--version" { run "$FENCE" --version [ "$status" -eq 0 ] [[ "$output" =~ ^docker-fence\ [0-9]+\.[0-9]+\.[0-9]+$ ]] } @test "usage errors: exit code 1" { minimal_conf run fence [ "$status" -eq 1 ] run fence frobnicate [ "$status" -eq 1 ] [[ "$output" == *"invalid usage: frobnicate"* ]] run fence --nope check [ "$status" -eq 1 ] run fence check --force [ "$status" -eq 1 ] [[ "$output" == *"check --force"* ]] run fence check extra [ "$status" -eq 1 ] run fence report --since [ "$status" -eq 1 ] run fence rollback --auto somefile [ "$status" -eq 1 ] } @test "--option=value form" { printf 'admin 192.0.2.10\n' | write_conf run "$FENCE" --config="$CONF" --mode=enforce show-config [ "$status" -eq 0 ] [[ "$output" == *"mode enforce"*"# option"* ]] } @test "--quiet prints errors only, --verbose prints commands" { minimal_conf run --separate-stderr fence -q check [ "$status" -eq 0 ] [ -z "$stderr" ] || [[ "$stderr" != *"configuration valid"* ]] run fence -v apply [[ "$output" == *"running: ufw reload"* ]] } @test "bash too old: exit code 2" { source "$FENCE" run _fence_require_bash 4 4.4.23 [ "$status" -eq 2 ] [[ "$output" == *"bash 5 or later"*"bash 4.4.23"* ]] run _fence_require_bash 5 5.0 [ "$status" -eq 0 ] } @test "init writes a valid template, mode 0644, without overwriting" { run "$FENCE" init [ "$status" -eq 0 ] [ -f "$CONF" ] [ "$(stat -c %a "$CONF")" = 644 ] run fence check [ "$status" -eq 0 ] [[ "$output" == *"mode observe"* ]] printf '# mine\nadmin 192.0.2.10\n' >"$CONF" local before before="$(sum_of "$CONF")" run "$FENCE" init [ "$status" -eq 1 ] [[ "$output" == *"already exists"* ]] [ "$(sum_of "$CONF")" = "$before" ] run "$FENCE" init --force [ "$status" -eq 0 ] [ "$(sum_of "$CONF")" != "$before" ] } @test "init --output: chosen file or standard output" { run "$FENCE" init --output "$BATS_TEST_TMPDIR/sub/dir/my.conf" [ "$status" -eq 0 ] [ -f "$BATS_TEST_TMPDIR/sub/dir/my.conf" ] run --separate-stderr "$FENCE" init --output - [ "$status" -eq 0 ] [[ "${lines[0]}" == "# docker-fence: configuration"* ]] } @test "the template lists every directive with its default value" { source "$REPO/lib/messages.sh" source "$REPO/lib/config.sh" run --separate-stderr "$FENCE" init --output - local k for k in "${FENCE_SETTINGS[@]}"; do [[ "$output" == *"$k ${FENCE_DEFAULTS[$k]}"* ]] || { echo "missing: $k"; return 1; } done for k in admin allow container_to_host trusted_iface; do [[ "$output" == *"# $k "* ]] || { echo "missing: $k"; return 1; } done } @test "the French template is valid too" { "$FENCE" --lang fr init --output "$BATS_TEST_TMPDIR/fr.conf" grep -q 'Réglages' "$BATS_TEST_TMPDIR/fr.conf" run "$FENCE" -c "$BATS_TEST_TMPDIR/fr.conf" check [ "$status" -eq 0 ] } @test "quality: no eval, configuration never sourced" { ! grep -nwE 'eval' "$FENCE" "$REPO"/lib/*.sh # Only the libraries of the tool are loaded with source. run grep -nE '^[[:space:]]*(source|\.)[[:space:]]' "$FENCE" "$REPO"/lib/*.sh [ "$(grep -cvE 'source "\$libdir/[a-z]+\.sh"' <<<"$output")" -eq 0 ] } @test "quality: set -euo pipefail in the tool and the fake binaries" { grep -q 'set -euo pipefail' "$FENCE" local f for f in "$REPO"/tests/stubs/*; do grep -q 'set -euo pipefail' "$f" done } @test "messages: same keys in both languages, no missing key" { source "$REPO/lib/messages.sh" diff <(printf '%s\n' "${!MSG_fr[@]}" | sort) <(printf '%s\n' "${!MSG_en[@]}" | sort) local key local -a used mapfile -t used < <(grep -ohE '(fence_(info|detail|warn|error|fmt)|fence_add_error "[^"]*"|fence_die [^ ]+) "?[a-z][a-z0-9_]+' \ "$FENCE" "$REPO"/lib/*.sh | awk '{print $NF}' | tr -d '"' | sort -u) mapfile -t -O "${#used[@]}" used < <(grep -ohE 'FENCE_VERR=[a-z0-9_]+' "$REPO"/lib/*.sh | cut -d= -f2 | sort -u) for key in "${used[@]}"; do [[ "$key" == d_ ]] && continue [[ -n "${MSG_fr[$key]+x}" ]] || { echo "missing key: $key"; return 1; } done source "$REPO/lib/config.sh" for key in "${FENCE_SETTINGS[@]}"; do [[ -n "${MSG_fr[d_$key]+x}" ]] || { echo "missing description: d_$key"; return 1; } done } @test "messages: no em dash" { ! grep -n $'\xe2\x80\x94' "$REPO/lib/messages.sh" } @test "no address outside documentation ranges in the example and the README" { local f for f in "$REPO/examples/fence.conf" "$REPO/README.md"; do [ -f "$f" ] || continue # Every IPv4 quoted must be RFC 5737, private (Docker) or generic. ! grep -oE '\b[0-9]{1,3}(\.[0-9]{1,3}){3}\b' "$f" \ | grep -vE '^(192\.0\.2|198\.51\.100|203\.0\.113)\.|^(172\.(1[6-9]|2[0-9]|3[01])|10|127)\.|^0\.0\.0\.0$' done }