From cbd8e4cb09f4d7b968eaabd087690d2bf9caa9e6 Mon Sep 17 00:00:00 2001 From: redxef Date: Wed, 7 Jun 2023 23:54:52 +0200 Subject: [PATCH] Fix docker tests to use actual volumes. --- tarback/docker.sh | 4 ---- tests/common.sh | 27 ++++++++++++++++++++++++++- tests/test-002-docker.sh | 32 +++++++++++++++++--------------- tests/test-003-ssh-docker.sh | 32 +++++++++++++++++--------------- 4 files changed, 60 insertions(+), 35 deletions(-) diff --git a/tarback/docker.sh b/tarback/docker.sh index b6c7fc4..f277d56 100644 --- a/tarback/docker.sh +++ b/tarback/docker.sh @@ -1,9 +1,5 @@ #!/bin/sh _tarback_plugin_docker_cmd='docker run -i --rm --workdir=/mnt --volume="$1":/mnt/"$1" alpine ' -TARBACK_TAR_CREATE_COMMAND="$_tarback_plugin_docker_cmd "'tar c -C "/mnt/$1" .' -TARBACK_TAR_EXTRACT_COMMAND="$_tarback_plugin_docker_cmd "'tar x -C "/mnt/$1"' - -# Can set file to same as directory command since a docker volume is always a directory TARBACK_TAR_CREATE_COMMAND_FILE="$_tarback_plugin_docker_cmd "'tar c -C "/mnt/$1" .' TARBACK_TAR_EXTRACT_COMMAND_FILE="$_tarback_plugin_docker_cmd "'tar x -C "/mnt/$1"' diff --git a/tests/common.sh b/tests/common.sh index f7801f8..56c734a 100755 --- a/tests/common.sh +++ b/tests/common.sh @@ -5,9 +5,34 @@ create_directories() { mkdir -p "$workdir/source" mkdir -p "$workdir/dest" mkdir -p "$workdir/restore" + + if [ $# -eq 1 ]; then + filename_len=$1 + else + filename_len=255 + fi # generate a filename with every possible byte in it to test # escaping, the only character not included is `/` since that is # illegal for filenames (obviously) - echo 'Hello World!' > "$workdir/source/$(for i in $(seq 0 255); do printf '%x' "$i" | xxd -r -p; done | tr -d '/')" + echo 'Hello World!' > "$workdir/source/$(for i in $(seq 0 $filename_len); do printf '%x' "$i" | xxd -r -p; done | tr -d '/')" echo "$workdir" } + +create_docker_volume() { + volumename="tarback-docker-volume-test-$(shuf -i 1000-9999 -n 1)" + # use shorter name because alpine runs into a filename length limit + if [ $# -eq 2 ]; then + workdir="$1" + filename="$2" + docker run --rm -i -v "$volumename:/mnt" alpine tee "/mnt/$filename" < "$workdir/source/$filename" 1>/dev/null + fi + echo "$volumename" +} + +extract_docker_volume() { + volumename="$1" + workdir="$2" + filename="$3" + #docker run --rm -v "$volumename:/mnt" alpine find / + docker run --rm -v "$volumename:/mnt" alpine cat "/mnt/$filename" > "$workdir/restore/$filename" +} diff --git a/tests/test-002-docker.sh b/tests/test-002-docker.sh index e69c38e..9f7b5cb 100755 --- a/tests/test-002-docker.sh +++ b/tests/test-002-docker.sh @@ -5,25 +5,27 @@ set -eu . ./tests/common.sh # archive whole directory -workdir="$(create_directories)" -./tarback.sh -p docker create "$workdir/source" "$workdir/dest/source.tar.xz" -./tarback.sh -p docker restore "$workdir/dest/source.tar.xz" "$workdir/restore" +workdir="$(create_directories 127)" +filename="$(basename "$(find "$workdir/source" -type f)")" +docker_volume="$(create_docker_volume "$workdir" "$filename")" +./tarback.sh -p docker create "$docker_volume" "$workdir/dest/source.tar.xz" +docker volume rm "$docker_volume" 2>/dev/null 1>&2 +docker_volume="$(create_docker_volume)" +./tarback.sh -p docker restore "$workdir/dest/source.tar.xz" "$docker_volume" +extract_docker_volume "$docker_volume" "$workdir" "$filename" diff "$workdir/source" "$workdir/restore" +docker volume rm "$docker_volume" 2>/dev/null 1>&2 rm -rf "$workdir" -# archive single file -workdir="$(create_directories)" +workdir="$(create_directories 127)" filename="$(basename "$(find "$workdir/source" -type f)")" -./tarback.sh -p docker create "$workdir/source/$filename" "$workdir/dest/source.tar.xz" -./tarback.sh -p docker restore "$workdir/dest/source.tar.xz" "$workdir/restore/$filename" +docker_volume="$(create_docker_volume "$workdir" "$filename")" +TARBACK_SPLIT='split -b 10 -' ./tarback.sh -p docker create "$docker_volume" "$workdir/dest/source.tar.xz" +docker volume rm "$docker_volume" 2>/dev/null 1>&2 +docker_volume="$(create_docker_volume)" +TARBACK_SPLIT='split -b 10 -' ./tarback.sh -p docker restore "$workdir/dest/source.tar.xz" "$docker_volume" +extract_docker_volume "$docker_volume" "$workdir" "$filename" diff "$workdir/source" "$workdir/restore" +docker volume rm "$docker_volume" 2>/dev/null 1>&2 rm -rf "$workdir" -# split archive -workdir="$(create_directories)" -filename="$(basename "$(find "$workdir/source" -type f)")" -TARBACK_SPLIT="split -b 10 -" ./tarback.sh -p docker create "$workdir/source/$filename" "$workdir/dest/source.tar.xz" -TARBACK_SPLIT="split -b 10 -" ./tarback.sh -p docker restore "$workdir/dest/source.tar.xz" "$workdir/restore/$filename" -diff "$workdir/source" "$workdir/restore" -[ "$(ls -1 "$workdir/dest/"*.part* | wc -l)" -gt 1 ] -rm -rf "$workdir" diff --git a/tests/test-003-ssh-docker.sh b/tests/test-003-ssh-docker.sh index aae6ba4..f735438 100755 --- a/tests/test-003-ssh-docker.sh +++ b/tests/test-003-ssh-docker.sh @@ -7,25 +7,27 @@ set -eu export TARBACK_REMOTE='ssh localhost' # archive whole directory -workdir="$(create_directories)" -./tarback.sh -p ssh,docker create "$workdir/source" "$workdir/dest/source.tar.xz" -./tarback.sh -p ssh,docker restore "$workdir/dest/source.tar.xz" "$workdir/restore" +workdir="$(create_directories 127)" +filename="$(basename "$(find "$workdir/source" -type f)")" +docker_volume="$(create_docker_volume "$workdir" "$filename")" +./tarback.sh -p ssh,docker create "$docker_volume" "$workdir/dest/source.tar.xz" +docker volume rm "$docker_volume" 2>/dev/null 1>&2 +docker_volume="$(create_docker_volume)" +./tarback.sh -p ssh,docker restore "$workdir/dest/source.tar.xz" "$docker_volume" +extract_docker_volume "$docker_volume" "$workdir" "$filename" diff "$workdir/source" "$workdir/restore" +docker volume rm "$docker_volume" 2>/dev/null 1>&2 rm -rf "$workdir" -# archive single file -workdir="$(create_directories)" +workdir="$(create_directories 127)" filename="$(basename "$(find "$workdir/source" -type f)")" -./tarback.sh -p ssh,docker create "$workdir/source/$filename" "$workdir/dest/source.tar.xz" -./tarback.sh -p ssh,docker restore "$workdir/dest/source.tar.xz" "$workdir/restore/$filename" +docker_volume="$(create_docker_volume "$workdir" "$filename")" +TARBACK_SPLIT='split -b 10 -' ./tarback.sh -p ssh,docker create "$docker_volume" "$workdir/dest/source.tar.xz" +docker volume rm "$docker_volume" 2>/dev/null 1>&2 +docker_volume="$(create_docker_volume)" +TARBACK_SPLIT='split -b 10 -' ./tarback.sh -p ssh,docker restore "$workdir/dest/source.tar.xz" "$docker_volume" +extract_docker_volume "$docker_volume" "$workdir" "$filename" diff "$workdir/source" "$workdir/restore" +docker volume rm "$docker_volume" 2>/dev/null 1>&2 rm -rf "$workdir" -# split archive -workdir="$(create_directories)" -filename="$(basename "$(find "$workdir/source" -type f)")" -TARBACK_SPLIT="split -b 10 -" ./tarback.sh -p ssh,docker create "$workdir/source/$filename" "$workdir/dest/source.tar.xz" -TARBACK_SPLIT="split -b 10 -" ./tarback.sh -p ssh,docker restore "$workdir/dest/source.tar.xz" "$workdir/restore/$filename" -diff "$workdir/source" "$workdir/restore" -[ "$(ls -1 "$workdir/dest/"*.part* | wc -l)" -gt 1 ] -rm -rf "$workdir"