Skip to content

Commit 9cee47e

Browse files
committed
Add shell test harness and CI
Introduce a bats suite for the two shell scripts, run in CI alongside shellcheck. Neither existed before, so every open shell bug in the tracker sits in code with no test and no lint covering it. The runner image is built FROM the action image on purpose: the scripts need GNU xargs from findutils, and against the busybox xargs in a plain alpine image the env_file tests fail for reasons unrelated to the code. Tests drive a fake docker CLI from fixtures, so no Swarm is needed. A test documenting an open bug calls skip with a link to the issue and asserts the desired behaviour, so fixing the bug means deleting the skip line rather than writing a new test. Issues 13, 19 and 21 each have one. scripts/stack-wait.sh is now maintained as a fork rather than tracking upstream, and its header says so. All shellcheck findings are resolved; where word splitting is intentional the line carries an explanatory disable instead of a change. One behaviour change: an unknown flag is now rejected instead of being silently ignored. Also adds a dependabot config covering both the workflows and the Dockerfile base image, and bumps actions/checkout to v7. Refs #13, #19, #21
1 parent f941c3e commit 9cee47e

20 files changed

Lines changed: 701 additions & 9 deletions

‎.dockerignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ Dockerfile
55
docs
66
Makefile
77
README.md
8+
Dockerfile.test
9+
tests

‎.github/dependabot.yml‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
version: 2
2+
updates:
3+
4+
- package-ecosystem: "github-actions"
5+
directory: "/"
6+
schedule:
7+
# Check for updates to GitHub Actions every week
8+
interval: "weekly"
9+
# No `cooldown` here: it is not supported for the github-actions
10+
# ecosystem, so the weekly interval is the only pacing available.
11+
groups:
12+
# One pull request for all action bumps, so CI validates them together.
13+
github-actions:
14+
patterns:
15+
- "*"
16+
# Must already exist in the repository, otherwise it is silently ignored.
17+
labels:
18+
- "dependencies"
19+
20+
- package-ecosystem: "docker"
21+
# Keeps the pinned base image in Dockerfile current.
22+
directory: "/"
23+
schedule:
24+
interval: "weekly"
25+
cooldown:
26+
# Supported for the docker ecosystem: ignore releases younger than this.
27+
default-days: 7
28+
labels:
29+
- "dependencies"

‎.github/workflows/ci.yml‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
pull_request:
8+
workflow_dispatch:
9+
10+
jobs:
11+
12+
lint:
13+
name: Shellcheck
14+
runs-on: ubuntu-latest
15+
steps:
16+
17+
- name: Checkout
18+
uses: actions/checkout@v7
19+
20+
- name: Lint shell scripts
21+
run: make lint
22+
23+
test:
24+
name: Bats
25+
runs-on: ubuntu-latest
26+
steps:
27+
28+
- name: Checkout
29+
uses: actions/checkout@v7
30+
31+
# Builds the action image, then the test runner on top of it, so the
32+
# suite runs against the same environment the action ships. This also
33+
# means a broken Dockerfile fails here instead of at release time.
34+
- name: Run test suite
35+
run: make test

‎.github/workflows/release.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
steps:
1818

1919
- name: Checkout
20-
uses: actions/checkout@v5
20+
uses: actions/checkout@v7
2121

2222
- name: Docker meta
2323
id: meta

‎Dockerfile.test‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Test runner image.
2+
#
3+
# It is built FROM the action image on purpose: the scripts depend on what that
4+
# image provides (bash, and GNU xargs from findutils rather than the busybox
5+
# one), so the suite has to run in the same environment the action ships.
6+
ARG BASE_IMAGE=ghcr.io/kitconcept/docker-stack-deploy:test
7+
FROM ${BASE_IMAGE}
8+
9+
RUN apk add --no-cache bats
10+
11+
WORKDIR /code
12+
13+
ENTRYPOINT [ "bats" ]
14+
CMD [ "tests/" ]

‎Makefile‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ MAKEFLAGS+=--no-builtin-rules
1111
BASE_NAME=docker-stack-deploy
1212
IMAGE_NAME=ghcr.io/kitconcept/$(BASE_NAME)
1313

14+
# Images used by the test suite, never pushed.
15+
TEST_IMAGE=$(IMAGE_NAME):test
16+
TEST_RUNNER_IMAGE=$(IMAGE_NAME):test-runner
17+
SHELLCHECK_IMAGE=koalaman/shellcheck:stable
18+
19+
# Everything shellcheck should look at.
20+
SHELL_SOURCES=scripts/docker-entrypoint.sh scripts/stack-wait.sh tests/helpers/bin/docker
21+
1422
# We like colors
1523
# From: https://coderwall.com/p/izxssa/colored-makefile-for-golang-projects
1624
RED=`tput setaf 1`
@@ -36,6 +44,19 @@ build-image: # Build Docker Image
3644
@echo "Building $(IMAGE_NAME)"
3745
docker build . -t $(IMAGE_NAME)
3846

47+
.PHONY: lint
48+
lint: # Lint the shell scripts with shellcheck
49+
docker run --rm -v "$(PWD)":/code -w /code $(SHELLCHECK_IMAGE) $(SHELL_SOURCES)
50+
51+
.PHONY: build-test-image
52+
build-test-image: # Build the bats test runner image
53+
docker build . -t $(TEST_IMAGE)
54+
docker build . -f Dockerfile.test --build-arg BASE_IMAGE=$(TEST_IMAGE) -t $(TEST_RUNNER_IMAGE)
55+
56+
.PHONY: test
57+
test: build-test-image # Run the bats test suite
58+
docker run --rm -v "$(PWD)":/code -w /code $(TEST_RUNNER_IMAGE) tests/
59+
3960
create-tag: # Create a new tag using git
4061
@test -n "$(VERSION)"
4162
if git show-ref --tags v$(VERSION) --quiet; \

‎README.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,29 @@ Please **DO NOT** commit to version branches directly. Even for the smallest and
201201

202202
**ALWAYS** open a pull request and ask somebody else to merge your code. **NEVER** merge it yourself.
203203

204+
### Development
205+
206+
Both commands need Docker, and nothing else — there is no local toolchain to install.
207+
208+
```shell
209+
make lint # shellcheck the shell scripts
210+
make test # run the bats suite
211+
```
212+
213+
`make test` builds the action image and then builds the test runner on top of
214+
it, so the suite runs in the same environment the action ships. That matters:
215+
the scripts rely on GNU `xargs` from `findutils`, and against the busybox
216+
`xargs` in a plain Alpine image the tests fail for reasons that have nothing to
217+
do with the code.
218+
219+
Tests live in `tests/` and use a fake `docker` CLI (`tests/helpers/bin/docker`)
220+
that replays a scenario from `tests/fixtures/*.services`, so no Swarm is
221+
needed. Each fixture describes one poll of the wait loop per section.
222+
223+
Tests that document a currently open bug call `skip` with a link to the issue.
224+
They are written to assert the *desired* behaviour, so fixing the bug means
225+
deleting the `skip` line rather than writing a new test.
226+
204227

205228
## Credits
206229

‎scripts/docker-entrypoint.sh‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,20 @@ check_deploy() {
7676
scale_after() {
7777
if [[ -n "$SCALE_AFTER" ]]; then
7878
echo "Scaling services: $SCALE_AFTER"
79+
# Unquoted on purpose: SCALE_AFTER may hold several "service=n" pairs,
80+
# and `docker service scale` expects them as separate arguments.
81+
# shellcheck disable=SC2086
7982
docker service scale $SCALE_AFTER
8083
fi
8184
}
8285

86+
# Everything above is a function definition; everything below is the deploy
87+
# flow. Sourcing this script (as the test suite does) stops here, so the
88+
# functions can be exercised individually without running a deploy.
89+
if [ "${BASH_SOURCE[0]}" != "${0}" ]; then
90+
return 0
91+
fi
92+
8393
[ -z ${DEBUG+x} ] && export DEBUG="0"
8494

8595
# ADDITIONAL ENV VARIABLES

‎scripts/stack-wait.sh‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#!/bin/sh
22

3-
# By: Brandon Mitchell <public@bmitch.net>
3+
# Originally by: Brandon Mitchell <public@bmitch.net>
44
# License: MIT
5-
# Source repo: https://github.com/sudo-bmitch/docker-stack-wait
5+
# Upstream repo: https://github.com/sudo-bmitch/docker-stack-wait
6+
#
7+
# Forked into this repository and maintained here. Changes are not
8+
# automatically taken from upstream; see tests/stack-wait.bats for the
9+
# behaviour this fork is expected to preserve.
610

711
set -e
812
trap "{ exit 1; }" TERM INT
@@ -14,7 +18,7 @@ opt_t=3600
1418
start_epoc=$(date +%s)
1519

1620
usage() {
17-
echo "$(basename $0) [opts] stack_name"
21+
echo "$(basename "$0") [opts] stack_name"
1822
echo " -f filter: only wait for services matching filter, may be passed multiple"
1923
echo " times, see docker stack services for the filter syntax"
2024
echo " -h: this help message"
@@ -32,7 +36,7 @@ check_timeout() {
3236
# next sleep completes
3337
if [ "$opt_t" -gt 0 ]; then
3438
cur_epoc=$(date +%s)
35-
cutoff_epoc=$(expr ${start_epoc} + $opt_t - $opt_s)
39+
cutoff_epoc=$((start_epoc + opt_t - opt_s))
3640
if [ "$cur_epoc" -gt "$cutoff_epoc" ]; then
3741
echo "Error: Timeout exceeded"
3842
print_service_logs
@@ -46,8 +50,12 @@ get_service_ids() {
4650
for name in $opt_n; do
4751
service_list="${service_list:+${service_list} }${stack_name}_${name}"
4852
done
53+
# Unquoted on purpose: service_list is a space-separated list of names.
54+
# shellcheck disable=SC2086
4955
docker service inspect --format '{{.ID}}' ${service_list}
5056
else
57+
# Unquoted on purpose: opt_f accumulates repeated "-f filter" pairs.
58+
# shellcheck disable=SC2086
5159
docker stack services ${opt_f} -q "${stack_name}"
5260
fi
5361
}
@@ -57,16 +65,20 @@ service_state() {
5765
# strip any invalid chars from service name for caching state
5866
service_safe=$(echo "$service" | sed 's/[^A-Za-z0-9_]/_/g')
5967
state=$2
68+
# Unquoted on purpose in both evals: service_safe is the sanitised service
69+
# name spliced into a variable name, not a value.
70+
# shellcheck disable=SC2086
6071
if eval [ \"\$cache_${service_safe}\" != \"\$state\" ]; then
6172
echo "Service $service state: $state"
73+
# shellcheck disable=SC2086
6274
eval cache_${service_safe}=\"\$state\"
6375
fi
6476
}
6577
print_service_logs() {
6678
if [ "$opt_p" != "0" ]; then
6779
service_ids=$(get_service_ids)
6880
for service_id in ${service_ids}; do
69-
docker service logs --tail $opt_p "$service_id"
81+
docker service logs --tail "$opt_p" "$service_id"
7082
done
7183
fi
7284
}
@@ -80,11 +92,12 @@ while getopts 'f:hn:p:rs:t:' opt; do
8092
r) opt_r=1;;
8193
s) opt_s="$OPTARG";;
8294
t) opt_t="$OPTARG";;
95+
*) usage;;
8396
esac
8497
done
85-
shift $(expr $OPTIND - 1)
98+
shift $((OPTIND - 1))
8699

87-
if [ $# -ne 1 -o "$opt_h" = "1" -o "$opt_s" -le "0" ]; then
100+
if [ $# -ne 1 ] || [ "$opt_h" = "1" ] || [ "$opt_s" -le "0" ]; then
88101
usage
89102
fi
90103

@@ -145,7 +158,7 @@ while [ "$stack_done" != "1" ]; do
145158
if [ "$service_done" = "2" ]; then
146159
# error condition
147160
stack_done=2
148-
elif [ "$service_done" = "0" -a "$stack_done" = "1" ]; then
161+
elif [ "$service_done" = "0" ] && [ "$stack_done" = "1" ]; then
149162
# only go to an updating state if not in an error state
150163
stack_done=0
151164
fi

0 commit comments

Comments
 (0)