Skip to content

Commit

Permalink
Merge pull request #5959 from flouthoc/integrate-experiment
Browse files Browse the repository at this point in the history
Use tmpfs for integration tests
  • Loading branch information
openshift-merge-bot[bot] authored Feb 19, 2025
2 parents 5dc3c23 + c87fd8e commit 3d14858
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 19 deletions.
10 changes: 8 additions & 2 deletions imagebuildah/stage_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,13 @@ func (s *StageExecutor) performCopy(excludes []string, copies ...imagebuilder.Co
data = strings.TrimPrefix(data, "\n")
// add breakline when heredoc ends for docker compat
data = data + "\n"
tmpFile, err := os.Create(filepath.Join(parse.GetTempDir(), path.Base(filepath.ToSlash(file.Name))))
// Create seperate subdir for this file.
tmpDir, err := os.MkdirTemp(parse.GetTempDir(), "buildah-heredoc")
if err != nil {
return fmt.Errorf("unable to create tmp dir for heredoc run %q: %w", parse.GetTempDir(), err)
}
defer os.RemoveAll(tmpDir)
tmpFile, err := os.Create(filepath.Join(tmpDir, path.Base(filepath.ToSlash(file.Name))))
if err != nil {
return fmt.Errorf("unable to create tmp file for COPY instruction at %q: %w", parse.GetTempDir(), err)
}
Expand All @@ -442,7 +448,7 @@ func (s *StageExecutor) performCopy(excludes []string, copies ...imagebuilder.Co
tmpFile.Close()
return fmt.Errorf("unable to write contents of heredoc file at %q: %w", tmpFile.Name(), err)
}
copySources = append(copySources, filepath.Base(tmpFile.Name()))
copySources = append(copySources, filepath.Join(filepath.Base(tmpDir), filepath.Base(tmpFile.Name())))
tmpFile.Close()
}
contextDir = parse.GetTempDir()
Expand Down
7 changes: 4 additions & 3 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -539,13 +539,14 @@ _EOF
}

@test "bud build with heredoc content" {
_prefetch quay.io/fedora/python-311
_prefetch alpine
run_buildah build -t heredoc $WITH_POLICY_JSON -f $BUDFILES/heredoc/Containerfile .
expect_output --substring "print first line from heredoc"
expect_output --substring "print second line from heredoc"
expect_output --substring "Heredoc writing first file"
expect_output --substring "some text of first file"
expect_output --substring "file2 from python"
expect_output --substring "file passed to program"
expect_output --substring "and it contains multiple"
expect_output --substring "(your index page goes here)"
expect_output --substring "(robots content)"
expect_output --substring "(humans content)"
Expand All @@ -560,7 +561,7 @@ _EOF
# verify that build output contains summary of heredoc content
expect_output --substring 'RUN <<EOF \(echo "print first line from heredoc"...)'
expect_output --substring 'RUN <<EOF \(echo "Heredoc writing first file" >> /file1...)'
expect_output --substring 'RUN python3 <<EOF \(with open\("/file2", "w") as f:...)'
expect_output --substring 'RUN cat <<EOF \(file passed to program...)'
expect_output --substring 'ADD <<EOF /index.html \(\(your index page goes here))'
expect_output --substring 'COPY <<robots.txt <<humans.txt /test/ \(\(robots content)) \(\(humans content))'
}
Expand Down
11 changes: 5 additions & 6 deletions tests/bud/heredoc/Containerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM quay.io/fedora/python-311
FROM alpine

USER root
WORKDIR /
Expand All @@ -15,13 +15,12 @@ EOF

RUN cat file1

RUN python3 <<EOF
with open("/file2", "w") as f:
print("file2 from python", file=f)
RUN cat <<EOF
file passed to program
and it contains multiple
lines
EOF

RUN cat file2

ADD <<EOF /index.html
(your index page goes here)
EOF
Expand Down
43 changes: 43 additions & 0 deletions tests/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,49 @@ function rm() {
run_unshared rm "$@"
}

#################
# run_with_log # Logs command before running it
#################
#
function run_with_log() {
local expected_rc=0
local retry=1
local cmd="$*"
case "$1" in
[0-9]) expected_rc=$1; shift;;
[1-9][0-9]) expected_rc=$1; shift;;
[12][0-9][0-9]) expected_rc=$1; shift;;
'?') expected_rc= ; shift;; # ignore exit code
--retry) retry=3; shift;; # retry with sleep of 1 sec
esac
while [ $retry -gt 0 ]; do
retry=$(( retry - 1 ))
echo "$_LOG_PROMPT $cmd"
run "$@"
echo "$output"
if [ "$status" -ne 0 ]; then
echo -n "[ rc=$status ";
if [ -n "$expected_rc" ]; then
if [ "$status" -eq "$expected_rc" ]; then
echo -n "(expected) ";
else
echo -n "(** EXPECTED $expected_rc **) ";
fi
fi
echo "]"
fi
if [ -n "$expected_rc" ]; then
if [ "$status" -eq "$expected_rc" ]; then
return
elif [ $retry -gt 0 ]; then
echo "[ RETRYING ]" >&2
sleep 1
else
die "exit code is $status; expected $expected_rc"
fi
fi
done
}

#################
# run_buildah # Invoke buildah, with timeout, using BATS 'run'
Expand Down
16 changes: 11 additions & 5 deletions tests/mkcw.bats
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ function mkcw_check_image() {

# Decrypt, mount, and take a look around.
uuid=$(cryptsetup luksUUID "$mountpoint"/disk.img)
cryptsetup luksOpen --key-file "$TEST_SCRATCH_DIR"/key "$mountpoint"/disk.img "$uuid"
run_with_log cryptsetup luksOpen --key-file "$TEST_SCRATCH_DIR"/key "$mountpoint"/disk.img "$uuid"
mkdir -p "$TEST_SCRATCH_DIR"/mount
mount /dev/mapper/"$uuid" "$TEST_SCRATCH_DIR"/mount
run_with_log mount /dev/mapper/"$uuid" "$TEST_SCRATCH_DIR"/mount
# Should have a not-empty config file with parts of an image's config.
test -s "$TEST_SCRATCH_DIR"/mount/.krun_config.json
# Should have a /tmp directory, at least.
Expand All @@ -42,9 +42,15 @@ function mkcw_check_image() {
fi

# Clean up.
umount "$TEST_SCRATCH_DIR"/mount
cryptsetup luksClose "$uuid"
buildah umount "$ctrID"
run_with_log umount -f "$TEST_SCRATCH_DIR"/mount
# `Retry` if `luksClose` fails with defaults of `run_with_log` because
# when unmounting the filesystem mounted on the device /dev/mapper/"$uuid"
# without `retry` somehow we end up in a state where mount is still being
# used by the kernel because when we do `lsof /dev/mapper/"$uuid"` it
# shows nothing but `dmsetup info -c $uuid` shows the device is still
# under use. Adding `--retry` in between somehow fixes this.
run_with_log --retry cryptsetup luksClose "$uuid"
run_buildah umount "$ctrID"
}

@test "mkcw-convert" {
Expand Down
5 changes: 2 additions & 3 deletions tests/test_runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ set -e

cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"

# Default to using /var/tmp for test space, since it's more likely to support
# labels than /tmp, which is often on tmpfs.
export TMPDIR=${TMPDIR:-/var/tmp}
# Default to using /tmp for test space.
export TMPDIR=${TMPDIR:-/tmp}

function execute() {
>&2 echo "++ $@"
Expand Down

0 comments on commit 3d14858

Please sign in to comment.