Skip to content

Commit f8853c4

Browse files
committed
feat: Add back BATS for testing images
BATS was initially added to this repository in #802, but was then removed in #1339. This adds it back, and hooks it up to Github Actions. This also fixes #1583, which happened due to a bug in the "Build image" step: the build context was set to the root project directory, which meant the `COPY docker-entrypoint.sh /usr/local/bin/` instruction was copying the base `docker-entrypoint.sh` file into the Docker image instead of the one in the variant directory. Changing the context to the variant directory solves that.
1 parent cbbf60d commit f8853c4

File tree

4 files changed

+49
-21
lines changed

4 files changed

+49
-21
lines changed

.github/workflows/build-test.yml

+9-21
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ jobs:
5555
strategy:
5656
fail-fast: false
5757
matrix: ${{ fromJson(needs.gen-matrix.outputs.matrix) }}
58+
env:
59+
NODE_VERSION: ${{ matrix.version }}
60+
IMAGE_TAG: node:${{ matrix.version }}-${{ matrix.variant }}
5861

5962
steps:
6063
- name: Get short node version
@@ -66,31 +69,16 @@ jobs:
6669

6770
- name: Checkout
6871
uses: actions/checkout@v2
72+
with:
73+
submodules: true
6974

7075
- name: Build image
7176
uses: docker/build-push-action@v2
7277
with:
7378
push: false
7479
load: true
75-
context: .
76-
file: ./${{ steps.short-version.outputs.result }}/${{ matrix.variant }}/Dockerfile
77-
tags: node:${{ matrix.version }}-${{ matrix.variant }}
78-
79-
- name: Test for node version
80-
run: |
81-
image_node_version=$(docker run --rm node:${{ matrix.version }}-${{ matrix.variant }} node --print "process.versions.node")
82-
echo "Expected: \"${{ matrix.version }}\", Got: \"${image_node_version}\""
83-
[ "${image_node_version}" == "${{ matrix.version }}" ]
84-
85-
- name: Verify entrypoint runs regular, non-executable files with node
86-
run: |
87-
tmp_file=$(mktemp)
88-
echo 'console.log("success")' > "${tmp_file}"
89-
output=$(docker run --rm -v "${tmp_file}:/app/index.js" node:${{ matrix.version }}-${{ matrix.variant }} app/index.js)
90-
[ "${output}" = 'success' ]
91-
92-
- name: Test for npm
93-
run: docker run --rm node:${{ matrix.version }}-${{ matrix.variant }} npm --version
80+
context: ./${{ steps.short-version.outputs.result }}/${{ matrix.variant }}
81+
tags: ${{ env.IMAGE_TAG }}
9482

95-
- name: Test for yarn
96-
run: docker run --rm node:${{ matrix.version }}-${{ matrix.variant }} yarn --version
83+
- name: Run tests with BATS
84+
run: test/bats/bin/bats --verbose-run test/docker-image.bats

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "test/bats"]
2+
path = test/bats
3+
url = https://github.com/bats-core/bats-core.git

test/bats

Submodule bats added at 99d64eb

test/docker-image.bats

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Docs: https://bats-core.readthedocs.io/en/stable/writing-tests.html
2+
3+
setup() {
4+
tmp_file=$(mktemp)
5+
echo 'console.log("success")' > "${tmp_file}"
6+
}
7+
8+
@test "Test for node version" {
9+
run -0 docker run --rm "${IMAGE_TAG}" node --print "process.versions.node"
10+
[ "$output" = "${NODE_VERSION}" ]
11+
}
12+
13+
@test "Test for node version, without directly invoking node" {
14+
run -0 docker run --rm "${IMAGE_TAG}" --print "process.versions.node"
15+
[ "$output" = "${NODE_VERSION}" ]
16+
}
17+
18+
@test "Test for npm" {
19+
run -0 docker run --rm "${IMAGE_TAG}" npm --version
20+
[ -n "$output" ]
21+
}
22+
23+
@test "Test for yarn" {
24+
run -0 docker run --rm "${IMAGE_TAG}" yarn --version
25+
[ -n "$output" ]
26+
}
27+
28+
@test "Verify entrypoint runs relative path pointing to regular, non-executable file with node" {
29+
run -0 docker run --rm -v "${tmp_file}:/index.js" "${IMAGE_TAG}" index.js
30+
[ "$output" = 'success' ]
31+
}
32+
33+
@test "Verify entrypoint runs absolute path pointing to regular, non-executable file with node" {
34+
run -0 docker run --rm -v "${tmp_file}:/index.js" "${IMAGE_TAG}" /index.js
35+
[ "$output" = 'success' ]
36+
}

0 commit comments

Comments
 (0)