Reorganize tests

* Split out test helper functions into tools/test_functions.sh
  * bin/_blackbox_common_test.sh: Unit-tests for functions.
  * blackbox_postdeploy: Use cp_permissions instead of chmod --reference
This commit is contained in:
tlimoncelli@stackexchange.com
2015-07-24 09:01:00 -04:00
parent 8956be47a3
commit aee22fc99d
5 changed files with 180 additions and 109 deletions

View File

@@ -380,6 +380,23 @@ function md5sum_file() {
esac
}
function cp_permissions() {
# Copy the perms of $1 onto $2 .. end.
case $(uname -s) in
Darwin )
chmod $( stat -f '%p' "$1" ) "${@:2}"
;;
Linux | CYGWIN* )
chmod --reference "$1" "${@:2}"
;;
* )
echo 'ERROR: Unknown OS. Exiting.'
exit 1
;;
esac
}
#
# Abstract the difference between git and hg:
#

23
bin/_blackbox_common_test.sh Executable file
View File

@@ -0,0 +1,23 @@
#!/usr/bin/env bash
#
# _blackbox_common_test.sh -- Unit tests of functions from _blackbox_common.sh
#
set -e
. "${0%/*}/_blackbox_common.sh"
. tools/test_functions.sh
PHASE 'Test cp-permissions: TestA'
touch TestA TestB TestC TestD
chmod 0347 TestA
chmod 0700 TestB
chmod 0070 TestC
chmod 0070 TestD
cp_permissions TestA TestB TestC
assert_file_perm '--wxr--rwx' TestA
assert_file_perm '--wxr--rwx' TestB
assert_file_perm '--wxr--rwx' TestC
assert_file_perm '----rwx---' TestD
echo '========== DONE.'

View File

@@ -30,7 +30,7 @@ echo '========== Decrypting new/changed files: START'
while IFS= read <&99 -r unencrypted_file; do
encrypted_file=$(get_encrypted_filename "$unencrypted_file")
decrypt_file_overwrite "$encrypted_file" "$unencrypted_file"
chmod --reference "$encrypted_file" "$unencrypted_file"
cp_permissions "$encrypted_file" "$unencrypted_file"
if [[ ! -z "$FILE_GROUP" ]]; then
chmod g+r "$unencrypted_file"
chgrp "$FILE_GROUP" "$unencrypted_file"

View File

@@ -3,117 +3,15 @@
blackbox_home=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/../bin
export PATH="${blackbox_home}:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/local/bin"
. _stack_lib.sh
set -e
. _stack_lib.sh
. tools/test_functions.sh
function PHASE() {
echo '********************'
echo '********************'
echo '*********' """$@"""
echo '********************'
echo '********************'
}
PHASE 'UNIT TESTS'
function md5sum_file() {
# Portably generate the MD5 hash of file $1.
case $(uname -s) in
Darwin )
md5 -r "$1" | awk '{ print $1 }'
;;
Linux )
md5sum "$1" | awk '{ print $1 }'
;;
CYGWIN* )
md5sum "$1" | awk '{ print $1 }'
;;
* )
echo 'ERROR: Unknown OS. Exiting.'
exit 1
;;
esac
}
_blackbox_common_test.sh
function assert_file_missing() {
if [[ -e "$1" ]]; then
echo "ASSERT FAILED: ${1} should not exist."
exit 1
fi
}
function assert_file_exists() {
if [[ ! -e "$1" ]]; then
echo "ASSERT FAILED: ${1} should exist."
echo "PWD=$(/bin/pwd -P)"
#echo "LS START"
#ls -la
#echo "LS END"
exit 1
fi
}
function assert_file_md5hash() {
local file="$1"
local wanted="$2"
assert_file_exists "$file"
local found
found=$(md5sum_file "$file")
if [[ "$wanted" != "$found" ]]; then
echo "ASSERT FAILED: $file hash wanted=$wanted found=$found"
exit 1
fi
}
function assert_file_group() {
local file="$1"
local wanted="$2"
local found
assert_file_exists "$file"
case $(uname -s) in
Darwin|FreeBSD )
found=$(stat -f '%Sg' "$file")
;;
Linux )
found=$(stat -c '%G' "$file")
;;
CYGWIN* )
echo "ASSERT_FILE_GROUP: Running on Cygwin. Not being tested."
return 0
;;
* )
echo 'ERROR: Unknown OS. Exiting.'
exit 1
;;
esac
if [[ "$wanted" != "$found" ]]; then
echo "ASSERT FAILED: $file chgrp wanted=$wanted found=$found"
exit 1
fi
}
function assert_line_not_exists() {
local target="$1"
local file="$2"
assert_file_exists "$file"
if grep -F -x -s -q >/dev/null "$target" "$file" ; then
echo "ASSERT FAILED: line '$target' should not exist in file $file"
echo "==== file contents: START $file"
cat "$file"
echo "==== file contents: END $file"
exit 1
fi
}
function assert_line_exists() {
local target="$1"
local file="$2"
assert_file_exists "$file"
if ! grep -F -x -s -q >/dev/null "$target" "$file" ; then
echo "ASSERT FAILED: line '$target' should exist in file $file"
echo "==== file contents: START $file"
cat "$file"
echo "==== file contents: END $file"
exit 1
fi
}
PHASE 'SYSTEM TESTS'
make_tempdir test_repository
cd "$test_repository"
@@ -143,7 +41,6 @@ function become_bob() {
git config --global user.email bob@example.com
}
PHASE 'Alice creates a repo. She creates secret.txt.'
become_alice

134
tools/test_functions.sh Executable file
View File

@@ -0,0 +1,134 @@
#!/usr/bin/env bash
function PHASE() {
echo '********************'
echo '********************'
echo '*********' """$@"""
echo '********************'
echo '********************'
}
function md5sum_file() {
# Portably generate the MD5 hash of file $1.
case $(uname -s) in
Darwin )
md5 -r "$1" | awk '{ print $1 }'
;;
Linux )
md5sum "$1" | awk '{ print $1 }'
;;
CYGWIN* )
md5sum "$1" | awk '{ print $1 }'
;;
* )
echo 'ERROR: Unknown OS. Exiting.'
exit 1
;;
esac
}
function assert_file_missing() {
if [[ -e "$1" ]]; then
echo "ASSERT FAILED: ${1} should not exist."
exit 1
fi
}
function assert_file_exists() {
if [[ ! -e "$1" ]]; then
echo "ASSERT FAILED: ${1} should exist."
echo "PWD=$(/bin/pwd -P)"
#echo "LS START"
#ls -la
#echo "LS END"
exit 1
fi
}
function assert_file_md5hash() {
local file="$1"
local wanted="$2"
assert_file_exists "$file"
local found
found=$(md5sum_file "$file")
if [[ "$wanted" != "$found" ]]; then
echo "ASSERT FAILED: $file hash wanted=$wanted found=$found"
exit 1
fi
}
function assert_file_group() {
local file="$1"
local wanted="$2"
local found
assert_file_exists "$file"
case $(uname -s) in
Darwin|FreeBSD )
found=$(stat -f '%Sg' "$file")
;;
Linux )
found=$(stat -c '%G' "$file")
;;
CYGWIN* )
echo "ASSERT_FILE_GROUP: Running on Cygwin. Not being tested."
return 0
;;
* )
echo 'ERROR: Unknown OS. Exiting.'
exit 1
;;
esac
if [[ "$wanted" != "$found" ]]; then
echo "ASSERT FAILED: $file chgrp wanted=$wanted found=$found"
exit 1
fi
}
function assert_file_perm() {
local wanted="$1"
local file="$2"
local found
assert_file_exists "$file"
case $(uname -s) in
Darwin|FreeBSD )
found=$(stat -f '%Sp' "$file")
;;
# NB(tlim): CYGWIN hasn't been tested. It might be more like Darwin.
Linux | CYGWIN* )
found=$(stat -c '%A' "$file")
;;
* )
echo 'ERROR: Unknown OS. Exiting.'
exit 1
;;
esac
if [[ "$wanted" != "$found" ]]; then
echo "ASSERT FAILED: $file chgrp wanted=$wanted found=$found"
exit 1
fi
}
function assert_line_not_exists() {
local target="$1"
local file="$2"
assert_file_exists "$file"
if grep -F -x -s -q >/dev/null "$target" "$file" ; then
echo "ASSERT FAILED: line '$target' should not exist in file $file"
echo "==== file contents: START $file"
cat "$file"
echo "==== file contents: END $file"
exit 1
fi
}
function assert_line_exists() {
local target="$1"
local file="$2"
assert_file_exists "$file"
if ! grep -F -x -s -q >/dev/null "$target" "$file" ; then
echo "ASSERT FAILED: line '$target' should exist in file $file"
echo "==== file contents: START $file"
cat "$file"
echo "==== file contents: END $file"
exit 1
fi
}