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
migration.
parent 6b832097
.cache
test/*_salted.Dockerfile
test/dumps/*.tar.gz
......@@ -87,6 +87,18 @@ Note that you can add any parameters that testinfra (pytest) accept, for
instance ``./run-test.py 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 http://hg.logilab.org/master/cubes/saem_ref
./run-tests.py test centos7 -- test/test_migration.py --upgrade-revision master
# To test against any revision from http://hg.logilab.org/review/cubes/saem_ref
./run-tests.py test centos7 -- test/test_migration.py --upgrade-revision REV
.. _saem_ref: https://www.cubicweb.org/project/cubicweb-saem_ref
.. _supervisor: http://supervisord.org
.. _docker: https://www.docker.com/
......
......@@ -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
@pytest.fixture
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
cmd.append("--privileged")
if getattr(request.function, "use_postgres"):
postgres_id = subprocess.check_output([
"docker", "run", "-d", pytestconfig.getoption('postgres_image'),
]).strip()
cmd.extend(["--link", "{0}:postgres".format(postgres_id)])
else:
postgres_id = None
if getattr(request.function, "docker_addopts"):
cmd.extend(list(request.function.docker_addopts.args))
cmd.append(request.param)
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
request.addfinalizer(teardown)
......@@ -30,6 +44,12 @@ def pytest_addoption(parser):
parser.addoption(
"--docker-image", action="store", dest="docker_image",
help="docker image(s) to test")
parser.addoption(
"--postgres-image", action="store", dest="postgres_image",
help="postgres image to use in postgres tests")
parser.addoption(
"--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):
metafunc.parametrize(
"TestinfraBackend", images.split(","), indirect=True, scope=scope)
if 'saem_ref_upgrade_revision' in metafunc.fixturenames:
if not metafunc.config.option.upgrade_revision:
pytest.skip()
else:
metafunc.parametrize('saem_ref_upgrade_revision', [metafunc.config.option.upgrade_revision])
@pytest.fixture
......
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))
@pytest.mark.use_postgres
@pytest.mark.destructive
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+http://hg.logilab.org/master/cubes/saem_ref@default#egg=cubicweb-saem-ref"
else:
pkg = "hg+http://hg.logilab.org/review/cubes/saem_ref@{0}#egg=cubicweb-saem-ref".format(
saem_ref_upgrade_revision)
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