Skip to content

Commit

Permalink
fix: Remove the bin/ dir that was created as a side-effect of grab.
Browse files Browse the repository at this point in the history
'stacker grab' was creating a 'bin/' dir in the current working
directory.  This fixes that and adds a test to ensure that neither
grab nor build have side effects.

Fixes #566.

Signed-off-by: Scott Moser <smoser@brickies.net>
  • Loading branch information
smoser committed Jan 9, 2024
1 parent 2152d8d commit a30061d
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 6 deletions.
11 changes: 5 additions & 6 deletions pkg/stacker/grab.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package stacker
import (
"fmt"
"io/fs"
"os"
"path"
"path/filepath"

Expand All @@ -20,24 +19,24 @@ func Grab(sc types.StackerConfig, storage types.Storage, name string, source str
}
defer c.Close()

err = c.BindMount(targetDir, types.InternalStackerDir, "")
err = SetupBuildContainerConfig(sc, storage, c, types.InternalStackerDir, name)
if err != nil {
return err
}
defer os.Remove(path.Join(sc.RootFSDir, name, "rootfs", "stacker"))

err = SetupBuildContainerConfig(sc, storage, c, types.InternalStackerDir, name)
idestdir := filepath.Join(types.InternalStackerDir, "grab")
err = c.BindMount(targetDir, idestdir, "")
if err != nil {
return err
}

bcmd := []string{filepath.Join(types.InternalStackerDir, types.BinStacker), "internal-go"}

iDestName := filepath.Join(types.InternalStackerDir, path.Base(source))
iDestName := filepath.Join(idestdir, path.Base(source))
if idest == "" || source[len(source)-1:] != "/" {
err = c.Execute(append(bcmd, "cp", source, iDestName), nil)
} else {
err = c.Execute(append(bcmd, "cp", source, types.InternalStackerDir+"/"), nil)
err = c.Execute(append(bcmd, "cp", source, idestdir+"/"), nil)
}
if err != nil {
return err
Expand Down
49 changes: 49 additions & 0 deletions test/grab.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
load helpers

function setup() {
stacker_setup
}

function teardown() {
cleanup
}

# do a build and a grab in an empty directory and
# verify that no unexpected files are created.
@test "grab has no side-effects" {
cat > stacker.yaml <<EOF
layer1:
from:
type: oci
url: $BUSYBOX_OCI
imports:
- myfile.txt
run: |
cp /stacker/imports/myfile.txt /my-file
EOF
startdir="$PWD"
wkdir="$PWD/work-dir"
bdir="$PWD/build-dir"
grabdir="$PWD/grab-dir"
mkdir "$wkdir" "$grabdir" "$bdir"
give_user_ownership "$wkdir" "$grabdir" "$bdir"

echo "hello world" > myfile.txt
expected_sha=$(sha myfile.txt)

cd "$bdir"
stacker "--work-dir=$wkdir" build "--stacker-file=$startdir/stacker.yaml"
dir_is_empty . ||
test_error "build dir had unexpected files: $_RET_EXTRA"

cd "$grabdir"
stacker "--work-dir=$wkdir" grab layer1:/my-file
[ -f my-file ]
found_sha=$(sha my-file)
[ "${expected_sha}" = "${found_sha}" ]

dir_has_only . my-file ||
test_error "grab produced extra files." \
"missing=${_RET_MISSING} extra=${_RET_EXTRA}"
}

61 changes: 61 additions & 0 deletions test/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ function stacker_setup() {
chown -R $SUDO_USER:$SUDO_USER .
}

function give_user_ownership() {
if [ "$PRIVILEGE_LEVEL" = "priv" ]; then
return
fi
if [ -z "$SUDO_UID" ]; then
echo "PRIVILEGE_LEVEL=$PRIVILEGE_LEVEL but empty SUDO_USER"
exit 1
fi
chown -R "$SUDO_USER:$SUDO_USER" "$@"
}

function cleanup() {
cd "$ROOT_DIR/test"
umount_under "$TEST_TMPDIR"
Expand Down Expand Up @@ -225,6 +236,56 @@ function _skopeo() {
HOME="$home" "$SKOPEO" "$@"
}

function dir_has_only() {
local d="$1" oifs="$IFS" unexpected="" f=""
shift
_RET_MISSING=""
_RET_EXTRA=""
unexpected=$(
shopt -s nullglob;
IFS="/"; allexp="/$*/"; IFS="$oifs"
# allexp gets /item/item2/ for all items in args
x=""
cd "$d" || {
echo "dir_has_only could not 'cd $d' from $PWD" 1>&2;
exit 1;
}
for found in * .*; do
[ "$found" = "." ] || [ "$found" = ".." ] && continue
[ "${allexp#*/$found/}" != "$allexp" ] && continue
x="$x $found"
done
echo "$x"
) || return 1
_RET_EXTRA="${unexpected# }"
for f in "$@"; do
[ -e "$d/$f" -o -L "$d/$f" ] && continue
_RET_MISSING="${_RET_MISSING} $f"
done
_RET_MISSING="${_RET_MISSING# }"
[ -z "$_RET_MISSING" -a -z "${_RET_EXTRA}" ]
return
}

function dir_is_empty() {
dir_has_only "$1"
}

# log a failure with ERROR:
# allows more descriptive error:
# [ -f file ] || test_error "missing 'file'"
# compared to just
# [ -f file ]
function test_error() {
local m=""
echo "ERROR: $1"
shift
for m in "$@"; do
echo " $m"
done
return 1
}

function test_copy_buffer_size() {
local buffer_size=$1
local file_type=$2
Expand Down

0 comments on commit a30061d

Please sign in to comment.