Skip to content

Commit

Permalink
initial refactor to start testing doBackup directly, and cloudTar sep…
Browse files Browse the repository at this point in the history
…arately
  • Loading branch information
TrentonAdams committed Jun 1, 2021
1 parent 20577d4 commit a75a73d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 33 deletions.
21 changes: 10 additions & 11 deletions cloud-tar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,6 @@ function exitWith() {
exit 99
}

function encrypt() {
# only encrypt if recipient given
if [[ -z "${gpg_recipients}" ]]; then
cat
else
recipients=();
for email in "${gpg_recipients[@]}"; do recipients+=(-r "${email}"); done
gpg "${recipients[@]}" --encrypt
fi
}

function verifyRequiredCommands() {
command -v split >/dev/null || { exitWith "split command not installed"; }
command -v aws >/dev/null || { exitWith "aws cli not installed"; }
Expand All @@ -67,6 +56,16 @@ function verifyRequiredCommands() {
command -v mktemp >/dev/null || { exitWith "mktemp not installed"; }
}

# test parseCommands mock parseCommands
# output mode
# stub everything else and ignore
# test mode backup
# test that parseBackupsArgs was called
# test that doBackup was called
# test mode restore
# test that parseRestoreArgs was called
# test that doRestore was called
# test aws was called when skip_s3 != true
function cloudTar() {
skip_s3="true"
my_args=$(parseCommands "$@")
Expand Down
11 changes: 11 additions & 0 deletions components/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ function notifyLargeBackup() {
fi
}

function encrypt() {
# only encrypt if recipient given
if [[ -z "${gpg_recipients}" ]]; then
cat
else
recipients=();
for email in "${gpg_recipients[@]}"; do recipients+=(-r "${email}"); done
gpg "${recipients[@]}" --encrypt
fi
}

function doBackup() {
[[ -d "${source_folder}" ]] || { exitWith "missing source folder ${source_folder}"; }
[[ -d "${destination_folder}" ]] || { exitWith "missing backup folder ${destination_folder}" ; }
Expand Down
8 changes: 4 additions & 4 deletions components/parse-restore-args.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ function parseRestoreArgs() {
case $1 in
-s | --source)
shift
printf "source_folder='%s'\n" "$1"
printf "export source_folder='%s';\n" "$1"
;;
-d | --destination)
shift
printf "destination_folder+=('%s')\n" "$1"
printf "export destination_folder+=('%s');\n" "$1"
;;
-n | --name)
shift
printf "backup_name='%s'\n" "$1"
printf "export backup_name='%s';\n" "$1"
;;
?*)
printf "args+=('%s')\n" "$1"
printf "export args+=('%s');\n" "$1"
;;
*)
break
Expand Down
65 changes: 47 additions & 18 deletions test/cloud-tar.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ source ./cloud-tar.sh
mkdir -p files backup
for i in {1..10}; do echo "file${i}" > "files/file-${i}"; done
# act
run cloudTar backup \
export source_folder=./files/
export destination_folder=backup/
export backup_name=test-backup
run doBackup \
-s ./files/ \
-d backup/ \
-n test-backup;
Expand All @@ -37,7 +40,11 @@ source ./cloud-tar.sh
mkdir -p files backup
for i in {1..10}; do echo "file${i}" > "files/file-${i}"; done
# act
run cloudTar backup \
export source_folder=./files/
export destination_folder=backup/
export backup_name=test-backup
export gpg_recipients=("${recipient}")
run doBackup \
-r "${recipient}" \
-s ./files/ \
-d backup/ \
Expand All @@ -64,7 +71,10 @@ source ./cloud-tar.sh
mkdir -p files backup
for i in {1..10}; do echo "file${i}" > "files/file-${i}"; done
# act
run cloudTar backup \
export source_folder=./files/
export destination_folder=backup/
export backup_name=test-backup
run doBackup \
-s ./files/ \
-d backup/ \
-n test-backup;
Expand All @@ -73,7 +83,7 @@ source ./cloud-tar.sh
assert [ -f backup/test-backup.0.backupaa ]

touch "files/file-11"
run cloudTar backup \
run doBackup \
-s ./files/ \
-d backup/ \
-n test-backup;
Expand Down Expand Up @@ -111,13 +121,16 @@ source ./cloud-tar.sh
mkdir -p files backup
for i in {1..10}; do echo "file${i}" > "files/file-${i}"; done
# act
run cloudTar backup \
export source_folder=./files/
export destination_folder=backup/
export backup_name=test-backup
run doBackup \
-s ./files/ \
-d backup/ \
-n test-backup;

rm -f "files/file-10"
run cloudTar backup \
run doBackup \
-s ./files/ \
-d backup/ \
-n test-backup;
Expand All @@ -128,7 +141,7 @@ source ./cloud-tar.sh
sleep 1

rm -f "files/file-9"
run cloudTar backup \
run doBackup \
-s ./files/ \
-d backup/ \
-n test-backup;
Expand Down Expand Up @@ -191,7 +204,11 @@ source ./cloud-tar.sh
mkdir -p files backup/sub-folder
for i in {1..10}; do echo "file${i}" > "files/file-${i}"; done
# act
run cloudTar backup \
export source_folder=./files/
export destination_folder=backup/
export backup_name=test-backup
export backup_sub_folder=sub-folder
run doBackup \
-s ./files/ \
-d backup/ \
--sub-folder sub-folder \
Expand All @@ -216,7 +233,10 @@ source ./cloud-tar.sh
mkdir -p files backup
for i in {1..10}; do echo "file${i}" > "files/file-${i}"; done
# act
run cloudTar backup \
export source_folder=./files/
export destination_folder=backup/
export backup_name=test-backup
run doBackup \
-s ./files/ \
-d backup/ \
-n test-backup;
Expand Down Expand Up @@ -244,18 +264,22 @@ source ./cloud-tar.sh
for i in {1..10}; do echo "file${i}" > "files/file-${i}"; done

# act
run cloudTar backup \
-s ./files/ \
-d backup/ \
-r ${recipient} \
-n test-backup
export source_folder=./files/
export destination_folder=backup/
export backup_name=test-backup
export gpg_recipients=("${recipient}")

assert [ -f ./backup/test-backup.*.backupaa ]
run doBackup

assert [ -f ./backup/test-backup.*.backupaa ]
unset source_folder destination_folder backup_name gpg_recipients
# unset destination_folder
run cloudTar restore \
-s backup \
-d restore \
-n test-backup;
ls -ltr
# assert_output --partial "Hello"
assert [ -d restore ];
for i in {1..10}; do
assert_output --partial "./files/file-${i}";
Expand All @@ -270,28 +294,33 @@ source ./cloud-tar.sh
mkdir -p files backup
for i in {1..10}; do echo "file${i}" > "files/file-${i}"; done
# act
run cloudTar backup \
export source_folder=./files/
export destination_folder=backup/
export backup_name=test-backup

run doBackup \
-s ./files/ \
-d backup/ \
-n test-backup;

assert [ -f ./backup/test-backup.*.backupaa ]

rm -f "files/file-10"
run cloudTar backup \
run doBackup \
-s ./files/ \
-d backup/ \
-n test-backup;
assert [ $(ls -1 ./backup/test-backup.*.backupaa | wc -l) -eq 2 ]

rm -f "files/file-9"
run cloudTar backup \
run doBackup \
-s ./files/ \
-d backup/ \
-n test-backup;
assert [ $(ls -1 ./backup/test-backup.*.backupaa | wc -l) -eq 3 ]

# assert
unset source_folder destination_folder backup_name gpg_recipients
run cloudTar restore \
-s backup \
-d restore \
Expand Down

0 comments on commit a75a73d

Please sign in to comment.