Skip to content

Commit a3ad1ca

Browse files
weltekialexellis
authored andcommitted
Fix tests silently skipped by Docker BuildKit
Docker BuildKit (default since Docker 23.0) optimizes builds by skipping stages that are not referenced by the final target. The test stage (FROM build AS test) was not a dependency of the ship stage, so BuildKit skipped it entirely, meaning tests never ran during faas-cli build. Remove the separate test stage and move the test commands into the build stage which the ship stage depends on, ensuring tests are always executed when enabled. Change TEST_ENABLED default from true to false so tests are opt-in via --build-arg TEST_ENABLED=true. Signed-off-by: Han Verstraete <han@openfaas.com>
1 parent ece3b3b commit a3ad1ca

File tree

9 files changed

+22
-19
lines changed

9 files changed

+22
-19
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,17 +385,27 @@ def handle(req):
385385

386386

387387
## Testing
388-
The `python3` templates will run `pytest` using `tox` during the `faas-cli build`. There are several options for controlling this.
388+
The `python3` templates support running `pytest` using `tox` during `faas-cli build`. Testing is disabled by default and must be explicitly enabled.
389389

390-
### Disabling testing
391-
The template exposes the build arg `TEST_ENABLED`. You can completely disable testing during build by passing the following flag to the CLI
390+
### Enabling testing
391+
The template exposes the build arg `TEST_ENABLED`. You can enable testing during build by passing the following flag to the CLI
392392

393393
```sh
394-
--build-arg 'TEST_ENABLED=false'
394+
--build-arg 'TEST_ENABLED=true'
395395
```
396396

397397
You can also set it permanently in your stack.yaml, see the [YAML reference in the docs](https://docs.openfaas.com/reference/yaml/#function-build-args-build-args).
398398

399+
```yaml
400+
functions:
401+
fn:
402+
lang: python3-http
403+
handler: ./fn
404+
image: fn:latest
405+
build_args:
406+
TEST_ENABLED: "true"
407+
```
408+
399409
### Changing the test configuration
400410
The template creates a default `tox.ini` file, modifying this file can completely control what happens during the test. You can change the test command, for example switching to `nose`. See the [tox docs](https://tox.readthedocs.io/en/latest/index.html) for more details and examples.
401411

template/python3-flask-debian/Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ USER root
4747

4848
COPY --chown=app:app function/ .
4949

50-
FROM build AS test
5150
ARG TEST_COMMAND=tox
52-
ARG TEST_ENABLED=true
51+
ARG TEST_ENABLED=false
5352
RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping tests" || eval "$TEST_COMMAND"
5453

55-
5654
FROM build AS ship
5755
WORKDIR /home/app/
5856

template/python3-flask-debian/function/handler_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Test your handler here
44

5-
# To disable testing, you can set the build_arg `TEST_ENABLED=false` on the CLI or in your stack.yml
5+
# To enable testing, you can set the build_arg `TEST_ENABLED=true` on the CLI or in your stack.yml
66
# https://docs.openfaas.com/reference/yaml/#function-build-args-build-args
77

88
def test_handle():

template/python3-flask/Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ USER root
4545
COPY --chown=app:app function/ .
4646

4747

48-
FROM build AS test
4948
ARG TEST_COMMAND=tox
50-
ARG TEST_ENABLED=true
49+
ARG TEST_ENABLED=false
5150
RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping tests" || eval "$TEST_COMMAND"
5251

5352
FROM build AS ship

template/python3-flask/function/handler_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Test your handler here
44

5-
# To disable testing, you can set the build_arg `TEST_ENABLED=false` on the CLI or in your stack.yml
5+
# To enable testing, you can set the build_arg `TEST_ENABLED=true` on the CLI or in your stack.yml
66
# https://docs.openfaas.com/reference/yaml/#function-build-args-build-args
77

88
def test_handle():

template/python3-http-debian/Dockerfile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,10 @@ RUN pip install --no-cache-dir --user -r requirements.txt
4242
USER root
4343
COPY --chown=app:app function/ .
4444

45-
FROM build AS test
46-
4745
ARG TEST_COMMAND=tox
48-
ARG TEST_ENABLED=true
46+
ARG TEST_ENABLED=false
4947
RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping tests" || eval "$TEST_COMMAND"
5048

51-
5249
FROM build AS ship
5350
WORKDIR /home/app/
5451

template/python3-http-debian/function/handler_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Test your handler here
44

5-
# To disable testing, you can set the build_arg `TEST_ENABLED=false` on the CLI or in your stack.yml
5+
# To enable testing, you can set the build_arg `TEST_ENABLED=true` on the CLI or in your stack.yml
66
# https://docs.openfaas.com/reference/yaml/#function-build-args-build-args
77

88
def test_handle():

template/python3-http/Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ RUN pip install --no-cache-dir --user -r requirements.txt
4141
USER root
4242
COPY --chown=app:app function/ .
4343

44-
FROM build AS test
4544
ARG TEST_COMMAND=tox
46-
ARG TEST_ENABLED=true
45+
ARG TEST_ENABLED=false
4746
RUN [ "$TEST_ENABLED" = "false" ] && echo "skipping tests" || eval "$TEST_COMMAND"
4847

4948
FROM build AS ship

template/python3-http/function/handler_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Test your handler here
44

5-
# To disable testing, you can set the build_arg `TEST_ENABLED=false` on the CLI or in your stack.yml
5+
# To enable testing, you can set the build_arg `TEST_ENABLED=true` on the CLI or in your stack.yml
66
# https://docs.openfaas.com/reference/yaml/#function-build-args-build-args
77

88
def test_handle():

0 commit comments

Comments
 (0)