Skip to content

Commit 5f54ff5

Browse files
committed
add test.sh + gate ci on it
1 parent 2257e94 commit 5f54ff5

4 files changed

Lines changed: 177 additions & 10 deletions

File tree

‎.github/workflows/docker-publish.yml‎

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,21 @@ env:
2121
REGISTRY: ghcr.io
2222
# github.repository as <account>/<repo>
2323
IMAGE_NAME: ${{ github.repository }}
24+
IMAGE_PLATFORMS: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
2425

2526

2627
jobs:
28+
# gate: don't build or publish anything unless the tests pass
29+
test:
30+
runs-on: ubuntu-latest
31+
steps:
32+
- name: Checkout repository
33+
uses: actions/checkout@v5
34+
- name: Run tests
35+
run: ./test.sh
36+
2737
build:
38+
needs: test
2839

2940
runs-on: ubuntu-latest
3041
permissions:
@@ -36,20 +47,25 @@ jobs:
3647

3748
steps:
3849
- name: Checkout repository
39-
uses: actions/checkout@v4
50+
uses: actions/checkout@v5
4051

4152
- name: Install Cosign
4253
uses: sigstore/cosign-installer@v3.5.0
4354

55+
- name: Set up QEMU
56+
uses: docker/setup-qemu-action@v4
57+
with:
58+
platforms: ${{ env.IMAGE_PLATFORMS }}
59+
4460
# Workaround: https://github.com/docker/build-push-action/issues/461
4561
- name: Setup Docker buildx
46-
uses: docker/setup-buildx-action@v3
62+
uses: docker/setup-buildx-action@v4
4763

4864
# Login against a Docker registry except on PR
4965
# https://github.com/docker/login-action
5066
- name: Log into registry ${{ env.REGISTRY }}
5167
if: github.event_name != 'pull_request'
52-
uses: docker/login-action@v3
68+
uses: docker/login-action@v4
5369
with:
5470
registry: ${{ env.REGISTRY }}
5571
username: ${{ github.actor }}
@@ -59,12 +75,16 @@ jobs:
5975
# https://github.com/docker/metadata-action
6076
- name: Extract Docker metadata
6177
id: meta
62-
uses: docker/metadata-action@v5
78+
uses: docker/metadata-action@v6
6379
with:
6480
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
6581

6682
- name: Extract Docker Version Tag
67-
id: docker_version_tag
83+
id: docker_version_tag
84+
env:
85+
# rebuild on code changes (push / manual run); only the nightly
86+
# schedule dedups on the version tag to skip identical rebuilds
87+
FORCE_REBUILD: ${{ github.event_name != 'schedule' && '1' || '' }}
6888
run: |
6989
./get-version.sh $(echo "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:" | tr '[:upper:]' '[:lower:]') | sed 's/^/tag=/g' >> $GITHUB_OUTPUT
7090
@@ -77,12 +97,12 @@ jobs:
7797
- name: Build and push Docker image with tag
7898
if: ${{ !endsWith(steps.docker_version_tag.outputs.tag, 'latest') }}
7999
id: build-and-push-tagged
80-
uses: docker/build-push-action@v5
100+
uses: docker/build-push-action@v7
81101
with:
82102
context: .
83103
push: ${{ github.event_name != 'pull_request' }}
84104
tags: ${{ steps.docker_version_tag.outputs.tag }}
85-
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
105+
platforms: ${{ env.IMAGE_PLATFORMS }}
86106
labels: ${{ steps.meta.outputs.labels }}
87107
cache-from: type=gha
88108
cache-to: type=gha,mode=max
@@ -100,7 +120,7 @@ jobs:
100120

101121
- name: Build Docker Latest Tag
102122
if: ${{ !endsWith(steps.docker_version_tag.outputs.tag, 'latest') }}
103-
id: docker_latest_tag
123+
id: docker_latest_tag
104124
run: |
105125
echo "tag=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" | tr '[:upper:]' '[:lower:]' >> $GITHUB_OUTPUT
106126
@@ -109,12 +129,12 @@ jobs:
109129
- name: Build and push Docker image as latest
110130
if: ${{ !endsWith(steps.docker_version_tag.outputs.tag, 'latest') }}
111131
id: build-and-push-latest
112-
uses: docker/build-push-action@v5
132+
uses: docker/build-push-action@v7
113133
with:
114134
context: .
115135
push: ${{ github.event_name != 'pull_request' }}
116136
tags: ${{ steps.docker_latest_tag.outputs.tag }}
117-
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6
137+
platforms: ${{ env.IMAGE_PLATFORMS }}
118138
labels: ${{ steps.meta.outputs.labels }}
119139
cache-from: type=gha
120140
cache-to: type=gha,mode=max

‎.github/workflows/test.yml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout repository
12+
uses: actions/checkout@v5
13+
14+
- name: Run test suite
15+
run: ./test.sh

‎get-version.sh‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ export ALPINE_VERSION=$(docker run --rm -t get-version cat /etc/alpine-release |
66
[ -z "$ALPINE_VERSION" ] && exit 1
77

88
export IMGTAG=$(echo "$1""a$ALPINE_VERSION-f$FAUXMO_VERSION")
9+
# FORCE_REBUILD (set by the workflow for push / manual runs) rebuilds even if
10+
# the versioned image already exists, so code/config changes get republished.
11+
# the nightly schedule leaves it unset and keeps deduping on the version tag.
12+
if [ -n "$FORCE_REBUILD" ]; then
13+
echo "$IMGTAG"
14+
exit 0
15+
fi
16+
917
export IMAGE_EXISTS=$(docker pull "$IMGTAG" 2>/dev/null >/dev/null; echo $?)
1018

1119
# return latest, if container is already available :)

‎test.sh‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/bin/sh
2+
# automated smoke test for the fauxmo container
3+
# builds the image, starts it with a single CommandLinePlugin device and asserts
4+
# fauxmo actually comes up and serves the WeMo setup.xml for that device.
5+
#
6+
# this test is the guard that lets us keep 'FROM alpine' (unpinned/latest) in the
7+
# Dockerfile: if a future alpine/python/fauxmo bump breaks startup or the HTTP
8+
# emulation, this fails instead of silently publishing a broken image.
9+
set -eu
10+
11+
IMAGE=fauxmo-test
12+
NAME=fauxmo-test-run
13+
DEVICE_PORT=49915
14+
DEVICE_NAME="test device"
15+
16+
FAILED=0
17+
fail() {
18+
echo "FAIL: $*" >&2
19+
FAILED=1
20+
}
21+
22+
cleanup() {
23+
echo ">> cleanup: removing container $NAME"
24+
docker rm -f "$NAME" >/dev/null 2>&1 || true
25+
}
26+
trap cleanup EXIT INT TERM
27+
28+
echo ">> building image $IMAGE"
29+
docker build -t "$IMAGE" .
30+
31+
echo ">> (re)starting container $NAME"
32+
docker rm -f "$NAME" >/dev/null 2>&1 || true
33+
# minimal one-device config via env: a CommandLinePlugin device on DEVICE_PORT.
34+
# the on/off/state commands are no-ops (true) - we only care that fauxmo starts
35+
# and serves the WeMo HTTP emulation for the device.
36+
docker run -d --name "$NAME" \
37+
-e FAUXMO_PLUGIN_COMMANDLINE_DEVICE_1_test="{ \"name\": \"$DEVICE_NAME\", \"port\": $DEVICE_PORT, \"on_cmd\": \"true\", \"off_cmd\": \"true\", \"state_cmd\": \"true\" }" \
38+
"$IMAGE"
39+
40+
echo ">> waiting for fauxmo to start (up to ~40s)"
41+
READY=0
42+
i=0
43+
while [ "$i" -lt 20 ]; do
44+
if ! docker ps --format '{{.Names}}' | grep -q "^${NAME}$"; then
45+
echo "!! container is not running anymore, dumping logs:" >&2
46+
docker logs "$NAME" >&2 2>&1 || true
47+
fail "container exited during startup"
48+
break
49+
fi
50+
# fauxmo up AND the device port is listening
51+
if docker exec "$NAME" sh -c 'ps aux | grep -q "[f]auxmo"' 2>/dev/null \
52+
&& docker exec "$NAME" sh -c "netstat -ltn 2>/dev/null | grep -q ':$DEVICE_PORT '" 2>/dev/null; then
53+
READY=1
54+
break
55+
fi
56+
i=$((i + 1))
57+
sleep 2
58+
done
59+
60+
if [ "$READY" -ne 1 ] && [ "$FAILED" -eq 0 ]; then
61+
echo "!! fauxmo did not come up in time, dumping logs:" >&2
62+
docker logs "$NAME" >&2 2>&1 || true
63+
fail "timed out waiting for fauxmo / device port"
64+
fi
65+
66+
# only run the deeper assertions if the container is still up
67+
if docker ps --format '{{.Names}}' | grep -q "^${NAME}$"; then
68+
69+
echo ">> assert: container is running"
70+
docker ps --format '{{.Names}}' | grep -q "^${NAME}$" \
71+
&& echo "ok - container running" || fail "container not running"
72+
73+
echo ">> assert: config.json was generated with our device"
74+
if docker exec "$NAME" grep -q "$DEVICE_NAME" /etc/fauxmo/config.json; then
75+
echo "ok - /etc/fauxmo/config.json contains the device"
76+
else
77+
fail "generated config.json is missing the device"
78+
fi
79+
80+
echo ">> assert: fauxmo python process present"
81+
if docker exec "$NAME" sh -c 'ps aux | grep -q "[f]auxmo"'; then
82+
echo "ok - fauxmo process running"
83+
else
84+
fail "fauxmo process not found"
85+
fi
86+
87+
echo ">> assert: device HTTP port $DEVICE_PORT is listening"
88+
if docker exec "$NAME" sh -c "netstat -ltn 2>/dev/null | grep -q ':$DEVICE_PORT '"; then
89+
echo "ok - port $DEVICE_PORT listening"
90+
else
91+
fail "device port $DEVICE_PORT not listening"
92+
fi
93+
94+
# fauxmo binds to the container's resolved interface IP ("ip_address": "auto"),
95+
# not 127.0.0.1, so probe the actual bound address from inside the container.
96+
echo ">> assert: device serves WeMo setup.xml over HTTP"
97+
SETUP=$(docker exec "$NAME" sh -c "curl -s -i \"http://\$(hostname -i):$DEVICE_PORT/setup.xml\"" 2>/dev/null || true)
98+
if echo "$SETUP" | grep -q '200 OK' \
99+
&& echo "$SETUP" | grep -q 'urn:Belkin:device:controllee' \
100+
&& echo "$SETUP" | grep -q "$DEVICE_NAME"; then
101+
echo "ok - setup.xml served (WeMo/Belkin emulation responding for '$DEVICE_NAME')"
102+
else
103+
echo "!! unexpected setup.xml response:" >&2
104+
echo "$SETUP" >&2
105+
fail "device did not serve a valid WeMo setup.xml"
106+
fi
107+
108+
echo ">> assert: fauxmo identifies itself as Fauxmo in HTTP headers"
109+
if echo "$SETUP" | grep -qi 'X-User-Agent: Fauxmo'; then
110+
echo "ok - X-User-Agent: Fauxmo header present"
111+
else
112+
fail "missing 'X-User-Agent: Fauxmo' header"
113+
fi
114+
115+
fi
116+
117+
echo
118+
if [ "$FAILED" -eq 0 ]; then
119+
echo "ALL TESTS PASSED"
120+
exit 0
121+
else
122+
echo "SOME TESTS FAILED"
123+
exit 1
124+
fi

0 commit comments

Comments
 (0)