diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..2d2ecd68d --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.git/ diff --git a/.gitignore b/.gitignore index eb3ca488f..2d4a2e5f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,11 @@ +*~ .*.swp +/.synapse-base /localhost-* -/server-* +/logs/ /media_store -/.synapse-base +/results.tap +/server-* /synapse /var +\#* diff --git a/docker/Dockerfile b/docker/Dockerfile index 9598a7af7..238445fa5 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,40 +1,31 @@ -FROM ubuntu:14.04 +FROM debian:stretch -ENV PERL5LIB=$HOME/lib/perl5 -ENV PERL_MB_OPT=--install_base=$HOME -ENV PERL_MM_OPT=INSTALL_BASE=$HOME -ENV DEBIAN_FRONTEND=noninteractive +ENV DEBIAN_FRONTEND noninteractive -ADD MyConfig.pm /root/.cpan/CPAN/MyConfig.pm +# Install base dependencies that Python or Go would require +RUN apt-get -qq update +RUN apt-get -qq install -y build-essential perl wget postgresql postgresql-client libpq-dev libssl-dev libz-dev libffi-dev sqlite3 libjpeg-dev libxslt1-dev git locales -RUN apt-get update \ - && apt-get upgrade -y \ - && apt-get install -y \ - build-essential \ - python2.7-dev \ - libffi-dev \ - python-pip \ - python-setuptools \ - sqlite3 \ - libssl-dev \ - python-virtualenv \ - libjpeg-dev \ - libxslt1-dev \ - git \ - postgresql \ - libpq-dev \ - && cd /root \ - && git clone https://github.com/matrix-org/sytest \ - && git clone https://github.com/matrix-org/synapse \ - && pip install --upgrade setuptools \ - && mkdir -p /root/.cpan/CPAN \ - && cd /root/sytest \ - && ./install-deps.pl \ - && cd ../synapse \ - && git checkout develop \ - && python synapse/python_dependencies.py | xargs -n1 pip install \ - && pip install lxml \ - && python setup.py develop --user \ - && python setup.py test +# Set up the locales, as the default Debian image only has C, and PostgreSQL needs the correct locales to make a UTF-8 database +RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \ + dpkg-reconfigure --frontend=noninteractive locales && \ + update-locale LANG=en_US.UTF-8 -WORKDIR /root/sytest +# Set the locales in the environment +ENV LANG en_US.UTF-8 +ENV LANGUAGE en_US:en +ENV LC_ALL en_US.UTF-8 + +# Copy in the sytest dependencies and install them +# The dockerfile context, when run by the buildscript, will actually be the +# repo root, not the docker folder +ADD install-deps.pl ./install-deps.pl +ADD cpanfile ./cpanfile +RUN perl ./install-deps.pl +RUN rm cpanfile install-deps.pl + +# /logs is where we should expect logs to end up +RUN mkdir /logs +# /test is where sytest will live +RUN mkdir /test +WORKDIR /test diff --git a/docker/Dockerfile-synapsepy2 b/docker/Dockerfile-synapsepy2 new file mode 100644 index 000000000..4533ce667 --- /dev/null +++ b/docker/Dockerfile-synapsepy2 @@ -0,0 +1,13 @@ +FROM matrixdotorg/sytest:latest + +RUN apt-get -qq install -y python python-dev python-virtualenv + +ENV PYTHON=python2 + +# /src is where we expect Synapse to be +RUN mkdir /src + +# The dockerfile context, when ran by the buildscript, will actually be the +# repo root, not the docker folder +ADD docker/synapse_sytest.sh /synapse_sytest.sh +ENTRYPOINT /synapse_sytest.sh diff --git a/docker/Dockerfile-synapsepy3 b/docker/Dockerfile-synapsepy3 new file mode 100644 index 000000000..71596a816 --- /dev/null +++ b/docker/Dockerfile-synapsepy3 @@ -0,0 +1,13 @@ +FROM matrixdotorg/sytest:latest + +RUN apt-get -qq install -y python3 python3-dev python3-virtualenv + +ENV PYTHON=python3 + +# /src is where we expect Synapse to be +RUN mkdir /src + +# The dockerfile context, when ran by the buildscript, will actually be the +# repo root, not the docker folder +ADD docker/synapse_sytest.sh /synapse_sytest.sh +ENTRYPOINT /synapse_sytest.sh diff --git a/docker/MyConfig.pm b/docker/MyConfig.pm deleted file mode 100644 index d08a9c68e..000000000 --- a/docker/MyConfig.pm +++ /dev/null @@ -1,72 +0,0 @@ - -$CPAN::Config = { - 'applypatch' => q[], - 'auto_commit' => q[0], - 'build_cache' => q[100], - 'build_dir' => q[/root/.cpan/build], - 'build_dir_reuse' => q[0], - 'build_requires_install_policy' => q[yes], - 'bzip2' => q[/bin/bzip2], - 'cache_metadata' => q[1], - 'check_sigs' => q[0], - 'colorize_output' => q[0], - 'commandnumber_in_prompt' => q[1], - 'connect_to_internet_ok' => q[1], - 'cpan_home' => q[/root/.cpan], - 'curl' => q[], - 'ftp' => q[], - 'ftp_passive' => q[1], - 'ftp_proxy' => q[], - 'getcwd' => q[cwd], - 'gpg' => q[/usr/bin/gpg], - 'gzip' => q[/bin/gzip], - 'halt_on_failure' => q[0], - 'histfile' => q[/root/.cpan/histfile], - 'histsize' => q[100], - 'http_proxy' => q[], - 'inactivity_timeout' => q[0], - 'index_expire' => q[1], - 'inhibit_startup_message' => q[0], - 'keep_source_where' => q[/root/.cpan/sources], - 'load_module_verbosity' => q[none], - 'lynx' => q[], - 'make' => q[], - 'make_arg' => q[], - 'make_install_arg' => q[], - 'make_install_make_command' => q[], - 'makepl_arg' => q[INSTALLDIRS=site], - 'mbuild_arg' => q[], - 'mbuild_install_arg' => q[], - 'mbuild_install_build_command' => q[./Build], - 'mbuildpl_arg' => q[--installdirs site], - 'ncftp' => q[], - 'ncftpget' => q[], - 'no_proxy' => q[*.local, 169.254/16], - 'pager' => q[/usr/bin/less], - 'patch' => q[], - 'perl5lib_verbosity' => q[none], - 'prefer_external_tar' => q[1], - 'prefer_installer' => q[MB], - 'prefs_dir' => q[/root/.cpan/prefs], - 'prerequisites_policy' => q[follow], - 'scan_cache' => q[atstart], - 'shell' => undef, - 'show_unparsable_versions' => q[0], - 'show_upload_date' => q[0], - 'show_zero_versions' => q[0], - 'tar' => q[/bin/tar], - 'tar_verbosity' => q[none], - 'term_is_latin' => q[1], - 'term_ornaments' => q[1], - 'test_report' => q[0], - 'trust_test_report_history' => q[0], - 'unzip' => q[], - 'urllist' => [q[http://cpan.uib.no/], q[http://cpan.mirror.anlx.net/], q[http://cpan.weepeetelecom.be/]], - 'use_sqlite' => q[0], - 'version_timeout' => q[15], - 'wget' => q[], - 'yaml_load_code' => q[0], - 'yaml_module' => q[YAML], -}; -1; -__END__ diff --git a/docker/README.md b/docker/README.md index 2944153ec..6c6951f5c 100644 --- a/docker/README.md +++ b/docker/README.md @@ -1,40 +1,33 @@ -# SyTest Docker +# SyTest Docker Images -Herein lies a Dockerfile for building a functional SyTest test environment. -SyTest and synapse are cloned from the HEAD of their develop branches. You can -run the tests as follows: +These Dockerfiles create containers for running SyTest in various configurations. SyTest is not included in these images, but its dependencies are. -``` -cd /path/to/sytest/docker -docker build -t sytest . -docker run --rm -it sytest bash -``` +Included currently is: -And then at the shell prompt: - -``` -./run-tests.pl -``` +- matrixdotorg/sytest, a base container with SyTest dependencies installed +- matrixdotorg/sytest-synapsepy2, a container which will run SyTest against Synapse on Python 2.7 +- matrixdotorg/sytest-synapsepy3, a container which will run SyTest against Synapse on Python 3.5 -Or other commands as per [the main SyTest -documentation](https://github.com/matrix-org/sytest#running). +## Using the Synapse containers -Alternatively: +Once pulled from Docker Hub, the container can be run on a Synapse checkout: ``` -docker run --rm sytest +$ docker run --rm -it -v /path/to/synapse\:/src -v /path/to/where/you/want/logs\:/logs matrixdotorg/sytest-synapsepy2 ``` -Where `` is `./run-tests.pl` or similar. +This will run on the same branch in SyTest as the Synapse checkout, if possible, but will fall back to using develop. + +If you want to use an existing checkout of SyTest, mount it to `/test` inside the container by adding `-v /path/to/sytest\:/test` to the docker command. +If you want to test against a PostgreSQL database, pass `-e POSTGRES=1` to the docker command. -To use sytest and synapse from the host, so that you can iterate on test -implementation and execute the tests in the container, you can do as follows: +## Building the containers + +The containers are built by executing `build.sh`. You will then have to push them up to Docker Hub: ``` -docker run --rm -it -v /path/to/sytest:/src/sytest -v /path/to/synapse:/src/synapse sytest bash +$ docker push matrixdotorg/sytest +$ docker push matrixdotorg/sytest-synapsepy2 +$ docker push matrixdotorg/sytest-synapsepy3 ``` - -Then at the prompt, `cd /src/sytest` and then you can run `./run-tests.pl` and -iterate developing a new test or modifying an existing test using your -favourite editor on your host. diff --git a/docker/build.sh b/docker/build.sh new file mode 100755 index 000000000..274c7e70f --- /dev/null +++ b/docker/build.sh @@ -0,0 +1,4 @@ +#! /usr/bin/env bash +docker build ../ -f Dockerfile -t matrixdotorg/sytest +docker build ../ -f Dockerfile-synapsepy2 -t matrixdotorg/sytest-synapsepy2 +docker build ../ -f Dockerfile-synapsepy3 -t matrixdotorg/sytest-synapsepy3 diff --git a/docker/synapse_sytest.sh b/docker/synapse_sytest.sh new file mode 100755 index 000000000..e7441ea55 --- /dev/null +++ b/docker/synapse_sytest.sh @@ -0,0 +1,73 @@ +#! /usr/bin/env bash + +set -x + +# Attempt to find a sytest to use. +# If /test/run-tests.pl exists, it means that a SyTest checkout has been mounted into the Docker image. +if [ -e "/test/run-tests.pl" ] +then + # If the user has mounted in a SyTest checkout, use that. We can tell this by files being in the directory. + echo "Using local sytests..." +else + # Otherwise, try and find out what the branch that the Synapse checkout is using. Fall back to develop if it's not a branch. + branch_name="$(git --git-dir=/src/.git symbolic-ref HEAD 2>/dev/null)" || branch_name="develop" + + # Try and fetch the branch + echo "Trying to get same-named sytest branch..." + wget -q https://github.com/matrix-org/sytest/archive/$branch_name.tar.gz -O sytest.tar.gz + + if [ $? -eq 0 ] + then + # The download succeeded, use that. + echo "Using $branch_name!" + else + # Probably a 404, fall back to develop + echo "Using develop instead..." + wget -q https://github.com/matrix-org/sytest/archive/develop.tar.gz -O sytest.tar.gz + fi + + tar --strip-components=1 -xf sytest.tar.gz + +fi + +# PostgreSQL setup +if [ -n "$POSTGRES" ] +then + + export PGDATA=/var/lib/postgresql/data + export PGUSER=postgres + export POSTGRES_DB_1=pg1 + export POSTGRES_DB_2=pg2 + + # Initialise the database files and start the database + su -c '/usr/lib/postgresql/9.6/bin/initdb -E "UTF-8" --lc-collate="en_US.UTF-8" --lc-ctype="en_US.UTF-8" --username=postgres' postgres + su -c '/usr/lib/postgresql/9.6/bin/pg_ctl -w -D /var/lib/postgresql/data start' postgres + + # Use the Jenkins script to write out the configuration for a PostgreSQL using Synapse + jenkins/prep_sytest_for_postgres.sh + + # Make the test databases for the two Synapse servers that will be spun up + su -c 'psql -c "CREATE DATABASE pg1;"' postgres + su -c 'psql -c "CREATE DATABASE pg2;"' postgres + +fi + +# Build the virtualenv, install extra deps that we will need for the tests +$PYTHON -m virtualenv -p $PYTHON /venv/ +/venv/bin/pip install -q --no-cache-dir -e /src/ +/venv/bin/pip install -q --no-cache-dir lxml psycopg2 + +# Make sure all Perl deps are installed -- this is done in the docker build so will only install packages added since the last Docker build +./install-deps.pl + +# Run the tests +./run-tests.pl -I Synapse --python=/venv/bin/python -O tap --all > results.tap + +TEST_STATUS=$? + +# Copy out the logs +cp results.tap /logs/results.tap +cp server-0/homeserver.log /logs/homeserver-0.log +cp server-1/homeserver.log /logs/homeserver-1.log + +exit $TEST_STATUS