#Use dagger as service utility for integration tests (outside dagger pipeline)

1 messages · Page 1 of 1 (latest)

ebon lance
#

Hi all, I was making a proof of concept to use dagger as a service provider for integration tests in python with pytest library.

So the workflow I tried is to define a fixture that creates a postgres container exposing the port. Then within a test I can successfully get the container's endpoint, but failing to connect to it as I'm not running the tests from a dagger pipeline and can't bind the service.

What do you suggest for connecting to the postgres container ?

Sample code here:

@pytest.fixture(scope="session")
async def postgres_service():
    async with dagger.Connection() as client:
        postgres = (
            dagger_client.container()
            .from_("postgres")
            .with_env_variable("POSTGRES_USER", "postgres")
            .with_env_variable("POSTGRES_PASSWORD", "postgres")
            .with_env_variable("POSTGRES_DB", "postgres")
            .with_exposed_port(5432)
        )
        return await postgres.endpoint()

@pytest.mark.asyncio
async def test_query(postgres_service):
    service_endpoint = await postgres_service
    host = service_endpoint.split(":")[0]
    ## host is k1hh5md0ptupo, so the fixture works fine
    db_url = f"dbname=postgres user=postgres password=postgres host={host} port=5432"
    conn = connect_db(db_url)
    cur = conn.cursor()
    cur.execute("SELECT 'Hello, World!'")
    result = cur.fetchall()
    cur.close()
    conn.close()
    assert result == [("Hello, World!",)]
snow parrot
#

Hi @ebon lance , host networking (both host-to-container or h2c, and the reverse c2h) is a feature that is still in development: https://github.com/dagger/dagger/pull/5557

I think your options are:

  1. Wait for that feature to drop. It’s nearly finished, so hopefully a matter of weeks to finalize testing and release

  2. Run the tests in Dagger - in which case the connecting problem goes away, but I understand that’s not always an option.

Sorry about that

GitHub

This PR combines a few efforts that have been piling up throughout our chaotic Zenith adventures; sorry for the huge PR:

Switch services to run via the gateway interface instead of via Solve() (pr...

ebon lance
#

Ok, thats fine. Thanks for taking the time to answer. I actually think that we should be able to choose. Maybe run the tests outside dagger when working locally but use dagger in the CI server.

A workaround is to have some env variable or something that allows me to choose if I want to run with dagger or not and if I'm not in dagger, then I could run postgres service using docker or anything else.

snow parrot
#

Soon 🙂