Skip to content

Commit

Permalink
Merge pull request #746 from centerofci/integ_test_playwright_python
Browse files Browse the repository at this point in the history
E2E Integration test environment setup
  • Loading branch information
pavish committed Nov 16, 2021
2 parents fbff78e + e5bfc70 commit 222d4e1
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 7 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/run-e2e-integ-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Run E2E integration tests
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
# We only want to run on external PRs, since internal PRs are covered by "push"
# This prevents this from running twice on internal PRs
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
steps:
- uses: actions/checkout@v2

# In this step, this action saves a list of existing images,
# the cache is created without them in the post run.
# It also restores the cache if it exists.
- uses: satackey/action-docker-layer-caching@v0.0.11
# Ignore the failure of a step and avoid terminating the job.
continue-on-error: true
with:
key: mathesar-docker-cache-integ-tests-{hash}
restore-keys: |
mathesar-docker-cache-e2e-tests-
- name: Copy env file
run: cp .env.example .env

- name: Use integ test dockerfile
run: rm Dockerfile && mv Dockerfile.integ-tests Dockerfile

# The code is checked out under uid 1001 - reset this to 1000 for the
# container to run tests successfully
- name: Fix permissions
run: sudo chown -R 1000:1000 .

- name: Build the stack
run: docker-compose up --build -d

- name: Sleep for 60 seconds
run: sleep 60s
shell: bash

# TODO: This needs to be handled inside the tests
- name: Run migrations
run: docker exec mathesar_service python manage.py migrate

- name: Run type installation
run: docker exec mathesar_service python install.py --skip-confirm

- name: Run integ tests with pytest
run: docker exec mathesar_service pytest --browser chromium --browser webkit --browser firefox integration_tests/

- uses: actions/upload-artifact@v2
if: ${{ failure() || success() }}
with:
name: Recordings
path: ${{ github.workspace }}/videos/
2 changes: 1 addition & 1 deletion .github/workflows/run-pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ jobs:
run: docker-compose up --build -d

- name: Run tests with pytest
run: docker exec mathesar_service pytest
run: docker exec mathesar_service pytest mathesar/ db/
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
FROM python:3.9-buster

# Add mathesar user
ENV PYTHONUNBUFFERED=1
ENV DOCKERIZE_VERSION v0.6.1

Expand Down
131 changes: 131 additions & 0 deletions Dockerfile.integ-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
FROM buildpack-deps:focal

ARG DEBIAN_FRONTEND=noninteractive

# Install python
# Some of this is ported from docker file for python-3.9:
# https://github.com/docker-library/python/blob/33751272d8171cece37c59180c049ab77cf9c837/3.9/buster/Dockerfile

# ensure local python is preferred over distribution python
ENV PATH /usr/local/bin:$PATH

# http://bugs.python.org/issue19846
# > At the moment, setting "LANG=C" on a Linux system *fundamentally breaks Python 3*, and that's not OK.
ENV LANG C.UTF-8

# Download node14 source
RUN curl -fsSL https://deb.nodesource.com/setup_14.x | bash -

# extra dependencies (over what buildpack-deps already includes)
RUN apt-get update && apt-get install -y --no-install-recommends \
libbluetooth-dev \
tk-dev \
uuid-dev \
sudo \
nodejs \
&& rm -rf /var/lib/apt/lists/*

ENV GPG_KEY E3FF2839C048B25C084DEBE9B26995E310250568
ENV PYTHON_VERSION 3.9.8

RUN set -ex \
\
&& wget -O python.tar.xz "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz" \
&& wget -O python.tar.xz.asc "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz.asc" \
&& export GNUPGHOME="$(mktemp -d)" \
&& gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys "$GPG_KEY" \
&& gpg --batch --verify python.tar.xz.asc python.tar.xz \
&& { command -v gpgconf > /dev/null && gpgconf --kill all || :; } \
&& rm -rf "$GNUPGHOME" python.tar.xz.asc \
&& mkdir -p /usr/src/python \
&& tar -xJC /usr/src/python --strip-components=1 -f python.tar.xz \
&& rm python.tar.xz \
\
&& cd /usr/src/python \
&& gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)" \
&& ./configure \
--build="$gnuArch" \
--enable-loadable-sqlite-extensions \
--enable-optimizations \
--enable-option-checking=fatal \
--enable-shared \
--with-system-expat \
--with-system-ffi \
--without-ensurepip \
&& make -j "$(nproc)" \
&& make install \
&& rm -rf /usr/src/python \
\
&& find /usr/local -depth \
\( \
\( -type d -a \( -name test -o -name tests -o -name idle_test \) \) \
-o \( -type f -a \( -name '*.pyc' -o -name '*.pyo' -o -name '*.a' \) \) \
\) -exec rm -rf '{}' + \
\
&& ldconfig \
\
&& python3 --version

# make some useful symlinks that are expected to exist
RUN cd /usr/local/bin \
&& ln -s idle3 idle \
&& ln -s pydoc3 pydoc \
&& ln -s python3 python \
&& ln -s python3-config python-config

# if this is called "PIP_VERSION", pip explodes with "ValueError: invalid truth value '<VERSION>'"
ENV PYTHON_PIP_VERSION 21.2.4
# https://github.com/docker-library/python/issues/365
ENV PYTHON_SETUPTOOLS_VERSION 57.5.0
# https://github.com/pypa/get-pip
ENV PYTHON_GET_PIP_URL https://github.com/pypa/get-pip/raw/d781367b97acf0ece7e9e304bf281e99b618bf10/public/get-pip.py
ENV PYTHON_GET_PIP_SHA256 01249aa3e58ffb3e1686b7141b4e9aac4d398ef4ac3012ed9dff8dd9f685ffe0

RUN set -ex; \
\
wget -O get-pip.py "$PYTHON_GET_PIP_URL"; \
echo "$PYTHON_GET_PIP_SHA256 *get-pip.py" | sha256sum --check --strict -; \
\
python get-pip.py \
--disable-pip-version-check \
--no-cache-dir \
"pip==$PYTHON_PIP_VERSION" \
"setuptools==$PYTHON_SETUPTOOLS_VERSION" \
; \
pip --version; \
\
find /usr/local -depth \
\( \
\( -type d -a \( -name test -o -name tests -o -name idle_test \) \) \
-o \
\( -type f -a \( -name '*.pyc' -o -name '*.pyo' \) \) \
\) -exec rm -rf '{}' +; \
rm -f get-pip.py

ENV PYTHONUNBUFFERED=1
ENV DOCKERIZE_VERSION v0.6.1

# Install dockerize
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz

RUN pip install playwright==1.16.1
RUN pip install pytest-playwright==0.2.2
RUN playwright install
RUN playwright install-deps

# Change work directory
WORKDIR /code/

COPY requirements.txt .
COPY requirements-dev.txt .

RUN pip install -r requirements.txt --force-reinstall sqlalchemy-filters
RUN pip install -r requirements-dev.txt
COPY . .

RUN sudo npm install -g npm-force-resolutions
RUN cd mathesar_ui && npm install --unsafe-perm && npm run build

EXPOSE 8000 3000 6006
37 changes: 37 additions & 0 deletions LICENSES/python-dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Component: python3.9 docker image source file
Copyright: 2014 Docker, Inc
License: MIT
License Text: https://github.com/docker-library/python/blob/33751272d8171cece37c59180c049ab77cf9c837/LICENSE
Derivative files:
/Dockerfile.integ-tests

=================================
BEGIN of license
=================================

The MIT License (MIT)

Copyright (c) 2014 Docker, Inc.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

=================================
END of license
=================================
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ We are currently in early development and hope to release an alpha version by la
- [Frontend](#frontend)
- [Linting](#linting)
- [Running tests](#running-tests)
- [E2E integration tests](#e2e-integration-tests)
- [Setting up the environment for E2E tests](#setting-up-the-environment-for-e2e-tests)
- [Running E2E tests](#running-e2e-tests)
- [Opening a shell in the container](#opening-a-shell-in-the-container)
- [Troubleshooting](#troubleshooting)
- [License](#license)
Expand Down Expand Up @@ -85,14 +88,39 @@ If you'd like to run tests before pushing, here's how you do it:

Backend tests:
```
docker exec mathesar_service pytest
docker exec mathesar_service pytest mathesar/ db/
```

Frontend tests:
```
docker exec mathesar_service bash -c "cd mathesar_ui && npm test"
```

### E2E integration tests

#### Setting up the environment for E2E tests

Running E2E integration tests requires a separate docker setup. The dockerfile for it can be found [here](Dockerfile.integ-tests).

Inorder to make use of it, you can change line 18 in docker-compose.yml:

```
# change this
dockerfile: Dockerfile
# to this
dockerfile: Dockerfile.integ-tests
```

If you are working on the frontend and would like to run integration tests regularly, you could make use of this for your default development environment setup.

#### Running E2E tests

The E2E tests require the server to be up and running. You can run the tests by executing the following command:
```
docker exec mathesar_service pytest integration_tests/
```

### Opening a shell in the container

If you need to do some work on the container that's running the code, here's how you access it:
Expand Down
5 changes: 4 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ services:
volumes:
- ./.volumes:/var/lib/postgresql/data
service:
build: .
build:
context: .
# Change this to Dockerfile.integ-tests to be able to run e2e integ tests
dockerfile: Dockerfile
container_name: mathesar_service
environment:
- MODE=${MODE-PRODUCTION}
Expand Down
2 changes: 1 addition & 1 deletion install.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def install_on_db_with_key(database_key, skip_confirm):
host = DATABASES[database_key]["HOST"]
db_name = DATABASES[database_key]["NAME"]
port = DATABASES[database_key]["PORT"]
print("Installing Mathesar DB on preexisting PostgreSQL instance...")
print(f"Installing Mathesar DB {db_name} on preexisting PostgreSQL instance at host {host}...")
if skip_confirm is False:
confirmation = input(
f"Mathesar will be installed on DB {db_name} at host {host}."
Expand Down
6 changes: 6 additions & 0 deletions integration_tests/test_base_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def test_base_page(browser):
context = browser.new_context(record_video_dir='videos/')
page = context.new_page()
page.goto('http://localhost:8000')
assert page.inner_text('h1') == 'Welcome to Mathesar!'
context.close()
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ env =
D:DEBUG=False
D:SECRET_KEY=hdC7qKjaFXNBjJ4heMMlOMrP-6j1-OvZpPf87DAXyaw
D:DJANGO_DATABASE_KEY=default
D:DJANGO_DATABASE_URL=postgres://mathesar:mathesar@db:5432/mathesar_django
D:MATHESAR_DATABASES=(mathesar_db_test|postgres://mathesar:mathesar@db:5432/mathesar_db_test)
D:DJANGO_DATABASE_URL=postgres://mathesar:mathesar@mathesar_db:5432/mathesar_django
D:MATHESAR_DATABASES=(mathesar_db_test|postgres://mathesar:mathesar@mathesar_db:5432/mathesar_db_test)
TEST=True
MODE=PRODUCTION

0 comments on commit 222d4e1

Please sign in to comment.