Skip to content

Commit 08a9cb8

Browse files
authored
Merge pull request #2 from devloberto/feature/command-try_docker
add `try_docker` function for docker image discovery
2 parents 33d9c0e + 6c9d818 commit 08a9cb8

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
DEFAULT_PATH=tests
2+
BASHUNIT_SHOW_HEADER=true
3+
BASHUNIT_HEADER_ASCII_ART=true

compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ services:
88
- shell-dev-tools:debian-trixie
99
volumes:
1010
- $PWD:/home/dev/shell-dev-tools
11-
- /var/run/docker.sock:/var/run/docker.sock:ro
11+
- /var/run/docker.sock:/var/run/docker.sock
1212

src/docker.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
11
dockerll() {
22
docker ps -a --format "table {{ .Names }}\t{{ .Status }}\t{{ .Networks }}\t{{ .Ports }}" $@
33
}
4+
5+
try_docker() {
6+
local docker_run="docker run"
7+
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>"
8+
if [[ $1 == '-h' || $1 == '--help' ]]; then
9+
echo -e $help
10+
return 0
11+
fi
12+
if [[ $# < 2 ]]; then
13+
echo -e "\n\terror: too few arguments \n\n$help"
14+
return 1
15+
fi
16+
container_name=$1
17+
image=$2
18+
entrypoint=${3:-/bin/bash}
19+
command="${@:4}"
20+
echo "$docker_run -it --rm --name $container_name --entrypoint=$entrypoint $image $command"
21+
if [[ "$UNIT_TESTING" == "true" ]]; then
22+
detach="-d" # testing: no interactive tty
23+
else
24+
it="-it" # the dev wants to immediately discover the container
25+
rm="--rm"
26+
fi
27+
docker run $detach $it $rm --name $container_name --entrypoint=$entrypoint $image $command
28+
}

tests/docker_test.sh

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,33 @@ test_dockerll_filtered_by_name() {
3333
# output shell-dev-tools container
3434
local second_line=$(echo "$output" | sed -n '2p')
3535
assert_contains "shell-dev-tools " "$second_line"
36-
assert_matches " Up [0-9]+ (seconds|minutes) " "$second_line"
36+
assert_matches " Up [a-zA-Z0-9 ]+ (seconds?|minutes?|hours?) " "$second_line"
3737
assert_contains " shell-dev-tools_default" "$second_line"
3838
}
39+
40+
TEST_CONTAINER_NAME=test-shell-dev-tools-unit-try_docker
41+
42+
test_try_docker() {
43+
# source_file="$(current_dir)/../src/docker.sh"
44+
# tmp_log_file=temp_file
45+
46+
# nohup source $source_file && TESTING=true try_docker "$TEST_CONTAINER_NAME" shell-dev-tools:debian-trixie >"$tmp_log_file" 2>&1 &
47+
output=$(UNIT_TESTING=true try_docker "$TEST_CONTAINER_NAME" shell-dev-tools:debian-trixie tail -f /dev/null)
48+
assert_contains "docker run -it --rm --name $TEST_CONTAINER_NAME --entrypoint=tail shell-dev-tools:debian-trixie -f /dev/null" "$output"
49+
50+
sleep 1
51+
docker ps --format '{{.Names}}' | grep -q "^$TEST_CONTAINER_NAME\$"
52+
assert_successful_code # assert the TEST_CONTAINER is running
53+
54+
output=$(docker exec "$TEST_CONTAINER_NAME" cat /etc/issue)
55+
assert_equals "Debian GNU/Linux 13 \n \l" "$output"
56+
57+
# cp "$tmp_log_file" /tmp/test-shell-dev-tools.log
58+
}
59+
60+
tear_down_after_script() {
61+
# stop and remove test_container if it exists
62+
if docker ps -a --format '{{.Names}}' | grep -q "^$TEST_CONTAINER_NAME\$"; then
63+
docker rm -f "$TEST_CONTAINER_NAME" >/dev/null 2>&1
64+
fi
65+
}

0 commit comments

Comments
 (0)