Skip to content

Commit

Permalink
Reuse sandbox and add tests (#85)
Browse files Browse the repository at this point in the history
* Reuse sandbox and add tests

* explain a comment better
  • Loading branch information
angelhof authored Jun 29, 2023
1 parent 6059bac commit 0b4c678
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 3 deletions.
59 changes: 59 additions & 0 deletions test/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,62 @@ test_touch_and_rm_D_flag_commit()
[ ! -f file.txt.gz ]
}

test_reuse_sandbox()
{
local try_workspace=$1
cp $RESOURCE_DIR/file.txt.gz "$try_workspace/"
cd "$try_workspace/"

## Set up expected output
echo 'test' >expected2.txt
echo 'test2' >>expected2.txt
touch expected3.out

try_example_dir=$(mktemp -d)
"$try" -D $try_example_dir "touch file_1.txt; echo test > file_2.txt; rm file.txt.gz"
"$try" -D $try_example_dir "rm file_1.txt; echo test2 >> file_2.txt; touch file.txt.gz"
$try commit $try_example_dir

[ ! -f file_1.txt ] &&
diff -q expected2.txt file_2.txt &&
diff -q expected3.out file.txt.gz
}

test_reuse_problematic_sandbox()
{
local try_workspace=$1
cp $RESOURCE_DIR/file.txt.gz "$try_workspace/"
cd "$try_workspace/"

## Set up expected output
echo 'test' >expected2.txt
echo 'test2' >>expected2.txt
touch expected3.out

try_example_dir=$(mktemp -d)
"$try" -D $try_example_dir "touch file_1.txt; echo test > file_2.txt; rm file.txt.gz"

## KK 2023-06-29 This test is meant to modify the sandbox directory in an illegal way,
## at the moment, this modification will be caught as illegal by `try`,
## but it doesn't seem to both overlayfs at all.
## TODO: Extend this with more problematic overlayfs modifications.
touch "$try_example_dir/temproot/bin/foo"
! "$try" -D $try_example_dir "rm file_1.txt; echo test2 >> file_2.txt; touch file.txt.gz"
}

test_non_existent_sandbox()
{
local try_workspace=$1
cp $RESOURCE_DIR/file.txt.gz "$try_workspace/"
cd "$try_workspace/"

try_example_dir="non-existent"
! "$try" -D $try_example_dir "touch file_1.txt" 2>/dev/null &&
! "$try" summary $try_example_dir 2>/dev/null &&
! "$try" commit $try_example_dir 2>/dev/null &&
! "$try" explore $try_example_dir 2>/dev/null
}

test_pipeline()
{
local try_workspace=$1
Expand Down Expand Up @@ -266,6 +322,9 @@ if [ "$#" -eq 0 ]; then
run_test test_untar_D_flag_commit
run_test test_touch_and_rm_no_flag
run_test test_touch_and_rm_D_flag_commit
run_test test_reuse_sandbox
run_test test_reuse_problematic_sandbox
run_test test_non_existent_sandbox
run_test test_pipeline
run_test test_cmd_sbst_and_var
run_test test_summary
Expand Down
58 changes: 55 additions & 3 deletions try
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,24 @@ TRY_VERSION="0.1.0"
try() {
START_DIR="$PWD"

[ "$SANDBOX_DIR" ] || SANDBOX_DIR=$(mktemp -d)
if [ "$SANDBOX_DIR" ]
then
## If the name of a sandbox is given then we need to exit prematurely if its directory doesn't exist
[ ! -d "$SANDBOX_DIR" ] && { printf "%s: could not find directory $SANDBOX_DIR\n" "$(basename $0)" >&2; exit 2; }
else
## Create a new sandbox if one was not given
SANDBOX_DIR=$(mktemp -d)
fi

## If the sandbox is not valid we exit early
if ! sandbox_valid_or_empty "$SANDBOX_DIR"
then
printf "%s: given sandbox $SANDBOX_DIR is invalid\n" "$(basename $0)" >&2
exit 1
fi

## Make any directories that don't already exist, this is OK to do here
## because we have already checked if it valid.
export SANDBOX_DIR
mkdir -p "$SANDBOX_DIR/upperdir" "$SANDBOX_DIR/workdir" "$SANDBOX_DIR/temproot"

Expand All @@ -32,7 +49,7 @@ try() {
do
## Only make the directory if the original is a directory too
if [ -d "$top_dir" ]; then
mkdir "$SANDBOX_DIR"/upperdir/"$top_dir" "$SANDBOX_DIR"/workdir"/$top_dir" "$SANDBOX_DIR"/temproot/"$top_dir"
mkdir -p "$SANDBOX_DIR"/upperdir/"$top_dir" "$SANDBOX_DIR"/workdir"/$top_dir" "$SANDBOX_DIR"/temproot/"$top_dir"
fi
done

Expand Down Expand Up @@ -140,7 +157,7 @@ summary() {
exit 2
elif ! [ -d "$SANDBOX_DIR/upperdir" ]
then
printf "%s: could not find directory $SANDBOX_DIR\n" "$(basename $0)" >&2
printf "%s: could not find directory $SANDBOX_DIR/upperdir\n" "$(basename $0)" >&2
exit 1
fi

Expand Down Expand Up @@ -178,6 +195,16 @@ EOF
################################################################################

commit() {
if ! [ -d "$SANDBOX_DIR" ]
then
printf "%s: could not find directory $SANDBOX_DIR\n" "$(basename $0)" >&2
exit 2
elif ! [ -d "$SANDBOX_DIR/upperdir" ]
then
printf "%s: could not find directory $SANDBOX_DIR/upperdir\n" "$(basename $0)" >&2
exit 1
fi

changed_files=$(find_upperdir_changes "$SANDBOX_DIR")
summary_output=$(process_changes "$SANDBOX_DIR" "$changed_files")

Expand Down Expand Up @@ -263,6 +290,31 @@ $changed_files
EOF
}

## Returns 0 if a sandbox is empty (fresh for use)
## or if it already exists, and is well-formed
sandbox_valid_or_empty() {
sandbox_dir="$1"
if [ ! -d "$sandbox_dir/upperdir" ] && [ ! -d "$sandbox_dir/workdir" ] && [ ! -d "$sandbox_dir/temproot" ]
then
## No sandbox directory exists so we can happily return
return 0
fi

## The sandbox already exists so we now need to check if it is valid
## Validity requirements:
## - no file exists in the temproot tree, i.e., all directories are empty
##
## TODO: Make this validity assertion tighter
## KK 2023-06-28 workdir seems to be non-empty after a single use, is that expected?
if [ ! -z "$(find "$sandbox_dir/temproot" -depth -not -type d)" ]
then
return 1
fi

return 0
}



################################################################################
# Argument parsing
Expand Down

0 comments on commit 0b4c678

Please sign in to comment.