def fixtures(tmpdir):
    """Initializes an in-memory Sqlite database with data in tests/fixtures"""
    db._connect()

    yield ti.load_fixtures(tmpdir)

    db.database_proxy.close()
Exemplo n.º 2
0
def db(network, images):
    """Set up the database and create the tables for alpenhorn.

    Also connect peewee to this database, so we can query its state."""

    from alpenhorn import db

    print("Creating the database...")

    # Create the database container
    db_container = client.containers.run(
        "mysql:5.7",
        name="db",
        detach=True,
        network_mode=network,
        ports={"3306/tcp": 63306},
        environment={"MYSQL_ALLOW_EMPTY_PASSWORD": "******"},
    )

    # Wait until the MySQL instance is properly up
    client.containers.run(
        "alpenhorn",
        remove=True,
        detach=False,
        network=network,
        command=
        "bash -c 'while ! mysqladmin ping -h db --silent; do sleep 3; done'",
    )

    # Create the database
    client.containers.run(
        "alpenhorn",
        remove=True,
        detach=False,
        network=network,
        command="mysql -h db -e 'CREATE DATABASE alpenhorn_db'",
    )

    print("Creating the tables...")

    # Initialise alpenhorn
    client.containers.run(
        "alpenhorn",
        remove=True,
        detach=False,
        network=network,
        command="alpenhorn init",
    )

    # Connect our peewee models to the database
    db._connect(url="mysql://[email protected]:63306/alpenhorn_db")

    yield db_container

    # Take down the peewee connection
    db.database_proxy.close()

    print("Cleaning up db container...")
    _stop_or_kill(db_container)
    db_container.remove()
Exemplo n.º 3
0
def fixtures(tmpdir):
    """Initializes an in-memory Sqlite database with data in tests/fixtures"""
    db._connect()

    fixtures = ti.load_fixtures(tmpdir)

    # create a valid ALPENHORN_NODE
    node_file = fixtures["root"].join("ALPENHORN_NODE")
    node_file.write("x")
    assert node_file.check()

    yield fixtures

    db.database_proxy.close()
Exemplo n.º 4
0
def fixtures(tmpdir):
    db._connect()

    # the database connection will fail to execute a statement every other time
    db.database_proxy.obj.__class__ = type(
        "FailingRetryableDatabase",
        (db.RetryOperationalError, FailingSqliteDatabase),
        {},
    )
    db.database_proxy.obj.fail_count = 0
    db.database_proxy.obj.fail = False

    yield ti.load_fixtures(tmpdir)

    assert db.database_proxy.obj.fail_count > 0
    db.database_proxy.close()
Exemplo n.º 5
0
def fixtures():

    from alpenhorn import db

    db._connect()

    db.database_proxy.create_tables([
        acquisition.AcqType,
        acquisition.FileType,
        acquisition.ArchiveAcq,
        acquisition.ArchiveFile,
        generic.GenericAcqInfo,
        generic.GenericFileInfo,
    ])

    yield

    # cleanup
    db.database_proxy.close()