Commit ab2d750d authored by Philippe Pepiot's avatar Philippe Pepiot
Browse files

test: migrations

For each dumps in the test/dump repository, restore the dump, upgrade
cubicweb-saem-ref to the given revision (--upgrade-revision) and run a
parent 6b832097
......@@ -87,6 +87,18 @@ Note that you can add any parameters that testinfra (pytest) accept, for
instance ``./ test centos6 -- -k idempotence --pdb``.
Testing migration
Add dumps generated by ``cubicweb-ctl db-dump`` in the ``test/dumps`` directory, then::
# To test upgrade against latest public changeset of
./ test centos7 -- test/ --upgrade-revision master
# To test against any revision from
./ test centos7 -- test/ --upgrade-revision REV
.. _saem_ref:
.. _supervisor:
.. _docker:
......@@ -77,9 +77,12 @@ def test(ctx, image):
tag = get_tag(image, True)
if not image_exists(tag):
ctx.invoke(build, image=image, salt=True)
postgres_tag = get_tag("postgres", False)
if not image_exists(tag):
ctx.invoke(build, image="postgres", salt=False)
import pytest
return pytest.main(["--docker-image", tag] + ctx.args)
return pytest.main(["--docker-image", tag, "--postgres-image", postgres_tag] + ctx.args)
@cli.command(help="Run a container and spawn an interactive shell inside")
......@@ -5,7 +5,7 @@ import time
def TestinfraBackend(request):
def TestinfraBackend(pytestconfig, request):
# Override the TestinfraBackend fixture,
# all testinfra fixtures (i.e. modules) depend on it.
......@@ -13,11 +13,25 @@ def TestinfraBackend(request):
if "centos7" in request.param:
# Systemd require privileged container
if getattr(request.function, "use_postgres"):
postgres_id = subprocess.check_output([
"docker", "run", "-d", pytestconfig.getoption('postgres_image'),
cmd.extend(["--link", "{0}:postgres".format(postgres_id)])
postgres_id = None
if getattr(request.function, "docker_addopts"):
docker_id = subprocess.check_output(cmd).strip()
def teardown():
subprocess.check_output(["docker", "rm", "-f", docker_id])
if postgres_id is not None:
subprocess.check_output(["docker", "rm", "-f", postgres_id])
# Destroy the container at the end of the fixture life
......@@ -30,6 +44,12 @@ def pytest_addoption(parser):
"--docker-image", action="store", dest="docker_image",
help="docker image(s) to test")
"--postgres-image", action="store", dest="postgres_image",
help="postgres image to use in postgres tests")
"--upgrade-revision", action="store",
help="List of hg revision to test against (use 'master' for latest public changeset)")
def pytest_generate_tests(metafunc):
......@@ -50,6 +70,11 @@ def pytest_generate_tests(metafunc):
"TestinfraBackend", images.split(","), indirect=True, scope=scope)
if 'saem_ref_upgrade_revision' in metafunc.fixturenames:
if not metafunc.config.option.upgrade_revision:
metafunc.parametrize('saem_ref_upgrade_revision', [metafunc.config.option.upgrade_revision])
FROM postgres:9.4
RUN apt-get -qqy update && apt-get -qqy install postgresql-plpython-9.4
# coding: utf-8
import os
import pytest
DUMPS_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), "dumps")
DUMPS = [fname for fname in os.listdir(DUMPS_DIR) if fname.endswith(".tar.gz")]
@pytest.mark.parametrize("dump", DUMPS)
@pytest.mark.docker_addopts("-v", "{0}:/dumps".format(DUMPS_DIR))
def test_migration(Salt, Command, dump, saem_ref_upgrade_revision):
run = Command.check_output
db_host = Salt("environ.get", "POSTGRES_PORT_5432_TCP_ADDR")
db_port = Salt("environ.get", "POSTGRES_PORT_5432_TCP_PORT")
config = "/home/saemref/etc/cubicweb.d/saemref/sources"
run("sed -ri %s %s", "s@^db-driver=.*$@db-driver=postgres@", config)
run("sed -ri %s %s", "s@^db-user=.*$@db-user=postgres@", config)
run("sed -ri %s %s", "s@^db-name=.*$@db-name=saemref@", config)
run("sed -ri %s %s", "s@^db-host=.*$@db-host={0}@".format(db_host), config)
run("sed -ri %s %s", "s@^db-port=.*$@db-port={0}@".format(db_port), config)
run("createdb -h {0} -p {1} -U postgres saemref".format(db_host, db_port))
run("yum install -y mercurial python-pip")
# Weird bug with backports.ssl_match_hostname
run("pip install -I setuptools")
if saem_ref_upgrade_revision == "master":
pkg = "hg+"
pkg = "hg+{0}#egg=cubicweb-saem-ref".format(
run("pip install %s", pkg)
run("su - saemref -c 'CW_MODE=user cubicweb-ctl db-restore saemref /dumps/{0}'".format(dump))
out = Command("su - saemref -c 'CW_MODE=user cubicweb-ctl upgrade -v 0 --force --backup-db=n saemref'")
# upgrade exit 0 even if a migration failed
assert out.rc == 0
assert "-> instance migrated." in out.stdout, "STDOUT:\n\n%s\n\nSTDERR:\n\n%s" % (out.stdout, out.stderr)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment