Skip to content

Commit

Permalink
Add Makefile & Docker for running tests locally
Browse files Browse the repository at this point in the history
* Add Makefile with targets:
  * For a single or all tets
  * For CI, local, or docker
* Add Dockerfile and docker-compose configuration
  • Loading branch information
costas-basdekis committed Nov 1, 2020
1 parent 12e8861 commit 3ce8667
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 10 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/test-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,4 @@ jobs:

- name: Run ${{ matrix.test-platform }} Tests
run: |
# Inline tests script
chmod +x buildroot/bin/*
chmod +x buildroot/tests/*
export PATH=./buildroot/bin/:./buildroot/tests/:${PATH}
run_tests . ${{ matrix.test-platform }}
make tests-single-ci TEST_TARGET=${{ matrix.test-platform }}
54 changes: 54 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
help:
@echo "Tasks for local development:"
@echo "* tests-single-ci: Run a single test from inside the CI"
@echo "* tests-single-local: Run a single test locally"
@echo "* tests-single-local-docker: Run a single test locally, using docker-compose"
@echo "* tests-all-local: Run all tests locally"
@echo "* tests-all-local-docker: Run all tests locally, using docker-compose"
@echo "* setup-local-docker: Setup local docker-compose"
@echo ""
@echo "Options for testing:"
@echo " TEST_TARGET Set when running tests-single-*, to select the"
@echo " test. If you set it to ALL it will run all "
@echo " tests, but some of them are broken: use "
@echo " tests-all-* instead to run only the ones that "
@echo " run on GitHub CI"
@echo " VERBOSE_PLATFORMIO If you want the full PIO output, set any value"
@echo " GIT_RESET_HARD Used by CI: reset all local changes. WARNING:"
@echo " THIS WILL UNDO ANY CHANGES YOU'VE MADE!"
.PHONY: help

tests-single-ci:
export GIT_RESET_HARD=true
$(MAKE) tests-single-local TEST_TARGET=$(TEST_TARGET)
.PHONY: tests-single-ci

tests-single-local:
@if ! test -n "$(TEST_TARGET)" ; then echo "***ERROR*** Set TEST_TARGET=<your-module> or use make tests-all-local" ; return 1; fi
chmod +x buildroot/bin/*
chmod +x buildroot/tests/*
export PATH=./buildroot/bin/:./buildroot/tests/:${PATH} \
&& export VERBOSE_PLATFORMIO=$(VERBOSE_PLATFORMIO) \
&& run_tests . $(TEST_TARGET)
.PHONY: tests-single-local

tests-single-local-docker:
@if ! test -n "$(TEST_TARGET)" ; then echo "***ERROR*** Set TEST_TARGET=<your-module> or use make tests-all-local-docker" ; return 1; fi
docker-compose run --rm marlin $(MAKE) tests-single-local TEST_TARGET=$(TEST_TARGET) VERBOSE_PLATFORMIO=$(VERBOSE_PLATFORMIO) GIT_RESET_HARD=$(GIT_RESET_HARD)
.PHONY: tests-single-local-docker

tests-all-local:
chmod +x buildroot/bin/*
chmod +x buildroot/tests/*
export PATH=./buildroot/bin/:./buildroot/tests/:${PATH} \
&& export VERBOSE_PLATFORMIO=$(VERBOSE_PLATFORMIO) \
&& for TEST_TARGET in $$(./get_test_targets.py) ; do echo "Running tests for $$TEST_TARGET" ; run_tests . $$TEST_TARGET ; done
.PHONY: tests-all-local

tests-all-local-docker:
docker-compose run --rm marlin $(MAKE) tests-all-local VERBOSE_PLATFORMIO=$(VERBOSE_PLATFORMIO) GIT_RESET_HARD=$(GIT_RESET_HARD)
.PHONY: tests-all-local-docker

setup-local-docker:
docker-compose build
.PHONY: setup-local-docker
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ Proposed patches should be submitted as a Pull Request against the ([bugfix-2.0.
- This branch is for fixing bugs and integrating any new features for the duration of the Marlin 2.0.x life-cycle.
- Follow the [Coding Standards](https://marlinfw.org/docs/development/coding_standards.html) to gain points with the maintainers.
- Please submit your questions and concerns to the [Issue Queue](https://github.com/MarlinFirmware/Marlin/issues).
- If you make significant changes, try to run tests locally if you can
- It's optional: running all the tests on Windows might take a long time, and they will run anyway on GitHub
- If you're running the tests on Linux, or on WSL with the code on a Linux volume, the speed is much faster
- You can use `make tests-all-local`/`make tests-single-local TEST_TARGET=...`
- If you prefer Docker you can use `make tests-all-local-docker`/`make tests-all-local-docker TEST_TARGET=...`

### [RepRap.org Wiki Page](https://reprap.org/wiki/Marlin)

Expand Down
26 changes: 21 additions & 5 deletions buildroot/tests/run_tests
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ set -e

exec_test () {
printf "\n\033[0;32m[Test $2] \033[0m$3...\n"
if platformio run --project-dir $1 -e $2 --silent; then
if [[ -z "$VERBOSE_PLATFORMIO" ]] ; then
silent="--silent"
else
silent=""
fi
if platformio run --project-dir $1 -e $2 $silent; then
printf "\033[0;32mPassed\033[0m\n"
return 0
else
git reset --hard HEAD
if [[ -n $GIT_RESET_HARD ]]; then
git reset --hard HEAD
else
restore_configs
fi
printf "\033[0;31mFailed!\033[0m\n"
return 1
fi
Expand All @@ -30,12 +39,19 @@ if [[ $2 = "ALL" ]]; then
testenv=$(basename $f | cut -d"-" -f1)
printf "Running \033[0;32m$f\033[0m Tests\n"
exec_test $1 "$testenv --target clean" "Setup Build Environment"
$f $1 $testenv
git reset --hard HEAD
if [[ $GIT_RESET_HARD == "true" ]]; then
git reset --hard HEAD
else
restore_configs
fi
done
else
exec_test $1 "$2 --target clean" "Setup Build Environment"
$2-tests $1 $2
git reset --hard HEAD
if [[ $GIT_RESET_HARD == "true" ]]; then
git reset --hard HEAD
else
restore_configs
fi
fi
printf "\033[0;32mAll tests completed successfully\033[0m\n"
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "3.8"
services:
# The main image: this doesn't run any particular command, but is mainly used
# for running tests locally
marlin:
image: marlin-dev
build:
dockerfile: Dockerfile
context: docker
working_dir: /code
volumes:
- .:/code
- platformio-cache:/root/.platformio

volumes:
# This volume holds installed libraries for PlatformIO. If this is deleted you
# will have to download all the dependencies again, which can be a very slow
# process
platformio-cache:
7 changes: 7 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:3.9.0-buster

RUN pip install -U https://github.com/platformio/platformio-core/archive/develop.zip
RUN platformio update
# To get the test platforms
RUN pip install PyYaml
#ENV PATH /code/buildroot/bin/:/code/buildroot/tests/:${PATH}
12 changes: 12 additions & 0 deletions get_test_targets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python
"""
Extract the builds used in Github CI, so that we can run them locally
"""
import yaml


with open('.github/workflows/test-builds.yml') as f:
github_configuration = yaml.safe_load(f)
test_platforms = github_configuration\
['jobs']['test_builds']['strategy']['matrix']['test-platform']
print(' '.join(test_platforms))

0 comments on commit 3ce8667

Please sign in to comment.