Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
DEFAULT_PATH=tests
BASHUNIT_SHOW_HEADER=true
BASHUNIT_HEADER_ASCII_ART=true
2 changes: 1 addition & 1 deletion compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ services:
- shell-dev-tools:debian-trixie
volumes:
- $PWD:/home/dev/shell-dev-tools
- /var/run/docker.sock:/var/run/docker.sock:ro
- /var/run/docker.sock:/var/run/docker.sock

25 changes: 25 additions & 0 deletions src/docker.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
dockerll() {
docker ps -a --format "table {{ .Names }}\t{{ .Status }}\t{{ .Networks }}\t{{ .Ports }}" $@
}

try_docker() {
local docker_run="docker run"
local help="\nusage\t\ttry_docker <container_name> <image> [command]\n\ndefault entrypoint is /bin/bash:\n$docker_run -it --rm --name <container_name> --entrypoint=<entrypoint> <image> <command>"
if [[ $1 == '-h' || $1 == '--help' ]]; then
echo -e $help
return 0
fi
if [[ $# < 2 ]]; then
echo -e "\n\terror: too few arguments \n\n$help"
return 1
fi
container_name=$1
image=$2
entrypoint=${3:-/bin/bash}
command="${@:4}"
echo "$docker_run -it --rm --name $container_name --entrypoint=$entrypoint $image $command"
if [[ "$UNIT_TESTING" == "true" ]]; then
detach="-d" # testing: no interactive tty
else
it="-it" # the dev wants to immediately discover the container
rm="--rm"
fi
docker run $detach $it $rm --name $container_name --entrypoint=$entrypoint $image $command
}
29 changes: 28 additions & 1 deletion tests/docker_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,33 @@ test_dockerll_filtered_by_name() {
# output shell-dev-tools container
local second_line=$(echo "$output" | sed -n '2p')
assert_contains "shell-dev-tools " "$second_line"
assert_matches " Up [0-9]+ (seconds|minutes) " "$second_line"
assert_matches " Up [a-zA-Z0-9 ]+ (seconds?|minutes?|hours?) " "$second_line"
assert_contains " shell-dev-tools_default" "$second_line"
}

TEST_CONTAINER_NAME=test-shell-dev-tools-unit-try_docker

test_try_docker() {
# source_file="$(current_dir)/../src/docker.sh"
# tmp_log_file=temp_file

# nohup source $source_file && TESTING=true try_docker "$TEST_CONTAINER_NAME" shell-dev-tools:debian-trixie >"$tmp_log_file" 2>&1 &
output=$(UNIT_TESTING=true try_docker "$TEST_CONTAINER_NAME" shell-dev-tools:debian-trixie tail -f /dev/null)
assert_contains "docker run -it --rm --name $TEST_CONTAINER_NAME --entrypoint=tail shell-dev-tools:debian-trixie -f /dev/null" "$output"

sleep 1
docker ps --format '{{.Names}}' | grep -q "^$TEST_CONTAINER_NAME\$"
assert_successful_code # assert the TEST_CONTAINER is running

output=$(docker exec "$TEST_CONTAINER_NAME" cat /etc/issue)
assert_equals "Debian GNU/Linux 13 \n \l" "$output"

# cp "$tmp_log_file" /tmp/test-shell-dev-tools.log
}

tear_down_after_script() {
# stop and remove test_container if it exists
if docker ps -a --format '{{.Names}}' | grep -q "^$TEST_CONTAINER_NAME\$"; then
docker rm -f "$TEST_CONTAINER_NAME" >/dev/null 2>&1
fi
}