Skip to content

Commit

Permalink
Merge pull request #45 from containers/publish-test
Browse files Browse the repository at this point in the history
tests: Add a test for --publish
  • Loading branch information
albertofaria authored Apr 15, 2024
2 parents 2eb1059 + bc24617 commit 93f277f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
24 changes: 11 additions & 13 deletions tests/env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ __elapsed() {
# Usage: __small_log_without_time <color> <format> <args...>
__small_log_without_time() {
# shellcheck disable=SC2059
printf "\033[%sm--- %s\033[0m\n" \
>&2 printf "\033[%sm--- %s\033[0m\n" \
"$1" "$( printf "${@:2}" )"
}

# Usage: __log <color> <format> <args...>
__small_log() {
# shellcheck disable=SC2059
printf "\033[%sm--- [%6.1f] %s\033[0m\n" \
>&2 printf "\033[%sm--- [%6.1f] %s\033[0m\n" \
"$1" "$( __elapsed )" "$( printf "${@:2}" )"
}

Expand All @@ -86,8 +86,8 @@ __big_log() {
text="$( printf "${@:2}" )"
term_cols="$( tput cols 2> /dev/null )" || term_cols=80
sep_len="$(( term_cols - ${#text} - 16 ))"
printf "\033[%sm--- [%6.1f] %s " "$1" "$( __elapsed )" "${text}"
printf '%*s\033[0m\n' "$(( sep_len < 0 ? 0 : sep_len ))" '' | tr ' ' -
>&2 printf "\033[%sm--- [%6.1f] %s " "$1" "$( __elapsed )" "${text}"
>&2 printf '%*s\033[0m\n' "$(( sep_len < 0 ? 0 : sep_len ))" '' | tr ' ' -
}

__log_without_time_and_run() {
Expand Down Expand Up @@ -339,16 +339,11 @@ run)
# shellcheck disable=SC2317
__engine() {
if [[ "$1" == run ]]; then
__command=(
"${engine_cmd[@]}" run
--runtime "$runtime_in_env"
--pull never
--label "$label"
__log_and_run "${engine_cmd[@]}" run \
--runtime "$runtime_in_env" \
--pull never \
--label "$label" \
"${@:2}"
)
__small_log 36 '$ %s' "${__command[*]}"
# shellcheck disable=SC2034
LAST_RUN_ID=$( "${__command[@]}" )
else
__log_and_run "${engine_cmd[@]}" "$@"
fi
Expand Down Expand Up @@ -382,6 +377,9 @@ run)
__skip() {
exit 0
}
__log() {
__small_log 36 \"\$@\"
}
TEMP_DIR=~/$label.temp
UTIL_DIR=~/$label.util
ENGINE=$engine
Expand Down
14 changes: 8 additions & 6 deletions tests/t/hostname.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ for os in fedora coreos; do

# default hostname

__engine run \
--rm --detach \
--name "hostname-$os-default" \
"$image" \
""
id=$(
__engine run \
--rm --detach \
--name "hostname-$os-default" \
"$image" \
""
)

__test() {
__engine exec "hostname-$os-default" --as "$user" \
"set -x && [[ \$( hostname ) == ${LAST_RUN_ID::12} ]]"
"set -x && [[ \$( hostname ) == ${id::12} ]]"
}

__test
Expand Down
26 changes: 26 additions & 0 deletions tests/t/publish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# SPDX-License-Identifier: GPL-2.0-or-later

image="${TEST_IMAGES[fedora]}"
user="${TEST_IMAGES_DEFAULT_USER[fedora]}"

__engine run \
--rm --detach \
--name publish \
--publish 127.0.0.1::8000 \
"$image" \
""

endpoint=$( __engine port publish | tee /dev/stderr | cut -d' ' -f3 )

__engine exec publish --as "$user"

__log 'Ensuring curl fails...'
! curl "$endpoint" 2>/dev/null

__engine exec publish --as "$user" python -m http.server &
trap '__engine stop publish' EXIT

sleep 3

__log 'Ensuring curl succeeds...'
[[ "$( curl "$endpoint" 2>/dev/null | head -1 )" == "<!DOCTYPE HTML>" ]]

0 comments on commit 93f277f

Please sign in to comment.