Skip to content

Commit

Permalink
tests: refactor docker setup for local testing
Browse files Browse the repository at this point in the history
The previous setup for local testing, using docker and tox, had multiple
problems:

  1) Tests ran as root in the container. All files, created by tox in the
     container belonged to root after the test-suite finished. That meant that
     caches and test artifacts, like screenshots or clips, had to be accessed
     using sudo.

  2) Playwright installation steps could not be cached. The playwright
     installation commands, that setup the underlying ubuntu container and
     install the actual browsers, had to run on every run of the test-suite,
     which is expensive.

  3) The test container had only one, unspecified, Python version installed.
     The docker image for the test-suite is based on the Microsofts playwright
     image, which is based some Ubuntu image. So the Python version always was
     the current Python3 version on Ubuntu. This was problematic because the
     version could change on every run and testing with multiple Python
     versions was impossible.

This patch fixes all of these problems by adding a script that runs the test
container as the current user, and updates the playwright docker file to
install all Python versions that Lona supports, using pyenv.

Signed-off-by: Florian Scherf <[email protected]>
  • Loading branch information
fscherf committed May 29, 2023
1 parent dbca56e commit 5fe1fb1
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ jobs:
run: |
pip install -U setuptools
pip install -r tests/REQUIREMENTS.test.txt
python -m playwright install
python -m playwright install-deps
- name: Run Tox
# Run tox using the version of Python in `PATH`
Expand Down
29 changes: 9 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PYTHON_ENV_ROOT=envs
PYTHON_DEV_ENV=$(PYTHON_ENV_ROOT)/$(PYTHON)-dev
PYTHON_PACKAGING_ENV=$(PYTHON_ENV_ROOT)/$(PYTHON)-packaging-env

.PHONY: clean doc dist pytest test ci-test lint isort shell freeze
.PHONY: clean doc dist test ci-test lint isort shell freeze

# development environment #####################################################
$(PYTHON_DEV_ENV): REQUIREMENTS.dev.txt
Expand Down Expand Up @@ -36,28 +36,17 @@ freeze: | $(PYTHON_DEV_ENV)
pip freeze

# tests #######################################################################
pytest: | $(PYTHON_DEV_ENV)
. $(PYTHON_DEV_ENV)/bin/activate && \
rm -rf htmlcov && \
time tox $(args)

ci-test: | $(PYTHON_DEV_ENV)
. $(PYTHON_DEV_ENV)/bin/activate && \
rm -rf htmlcov && \
time JENKINS_URL=1 tox -r $(args)

test:
ARGS=$(args) docker-compose -f tests/docker/docker-compose.yml run lona-tox
./docker-compose run playwright tox $(args)

# linting #####################################################################
lint: | $(PYTHON_DEV_ENV)
. $(PYTHON_DEV_ENV)/bin/activate && \
time tox -e lint $(args)
ci-test:
./docker-compose run playwright tox -e lint,py37,py38,py39,py310,py311 $(args)

# isort #######################################################################
isort: | $(PYTHON_DEV_ENV)
. $(PYTHON_DEV_ENV)/bin/activate && \
tox -e isort $(args)
lint:
./docker-compose run playwright tox -e lint $(args)

isort:
./docker-compose run playwright tox -e isort $(args)

# packaging ###################################################################
dist: | $(PYTHON_PACKAGING_ENV)
Expand Down
7 changes: 7 additions & 0 deletions docker-compose
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

# set the docker user to the local user so test environments and artifacts
# can be access from inside and outside of the test container
export DOCKER_USER=$(id -u):$(id -g)

exec docker-compose $@
25 changes: 25 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3"

networks:
net:
driver: bridge

services:
playwright:
user: ${DOCKER_USER}

build:
context: ./
dockerfile: playwright.Dockerfile

networks:
- net

volumes:
- ./:/app

environment:
- PLAYWRIGHT_BROWSERS_PATH=/ms-playwright

working_dir: "/app"
command: "tox"
30 changes: 30 additions & 0 deletions playwright.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM mcr.microsoft.com/playwright:v1.34.0-jammy

# upgrade ubuntu
RUN apt update && apt upgrade -y

# setup tzdata (required by pyenv dependencies)
RUN apt update && DEBIAN_FRONTEND=noninteractive TZ=Gmt/UTC apt-get -y install tzdata

# install pyenv dependencies
RUN apt update && apt install -y \
git build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev \
libsqlite3-dev curl libncursesw5-dev xz-utils tk-dev libxml2-dev \
libxmlsec1-dev libffi-dev liblzma-dev

# setup pyenv
RUN git clone https://github.com/yyuu/pyenv.git .pyenv
ENV PYENV_ROOT $HOME/.pyenv
ENV PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH

RUN pyenv install 3.7:latest
RUN pyenv install 3.8:latest
RUN pyenv install 3.9:latest
RUN pyenv install 3.10:latest
RUN pyenv install 3.11:latest

RUN pyenv global `pyenv versions --bare`

# setup commandline tools
RUN pip3.10 install --upgrade pip tox
RUN pyenv rehash
22 changes: 0 additions & 22 deletions tests/docker/docker-compose.yml

This file was deleted.

4 changes: 0 additions & 4 deletions tests/docker/playwright.dockerfile

This file was deleted.

6 changes: 2 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
[tox]
skip_missing_interpreters=True
envlist=python
envlist=py310


[tox:jenkins]
envlist=lint,py37,py38,py39,py310,py311


[testenv]
passenv = PLAYWRIGHT_BROWSERS_PATH
ignore_errors=True

deps =
-r tests/REQUIREMENTS.test.txt

commands =
python -m playwright install
python -m playwright install-deps

coverage erase

coverage run -a \
Expand Down

0 comments on commit 5fe1fb1

Please sign in to comment.