Skip to main content

Uses docker to spawn containers for services required during tests

Project description

Uses docker to spawn containers for services required during tests

Build Status

Install

This project is available on pypi, so you can use pip to install it:

pip install docker-services

Or include it as a dependency on setup.py or a requirements.txt file, whatever you prefer.

Use it

1. Configure your project services

You need to start listing all services that your project depends on and leverage on docker-services to maintain the lifecycle of those services during test runs.

This must happen using either the docker_services config option on any pytest .cfg/.ini file or on a .services.yaml file, the value for that option requires a yaml structure where top level members are the service names and their values can be either an empty value, image name or a or an object.

Due to limitations of ini files, full support for yaml is not available on .cfg/.ini files, if you need complex config then usage of the .services.yaml file is encouraged.

This is a basic example for a project that depends on a postgres service:

[pytest]
docker_services=postgres:

This is the same:

[pytest]
docker_services=
    postgres:

And this:

[pytest]
docker_services=
    postgres: postgres

And this too (on .services.yaml):

postgres:
    image: postgres

And guess what?... this too!

database:
    name: postgres
    image: postgres

If the projects depends different services, list all of them:

[pytest]
docker_services=
    postgres:
    redis:

When image name is not specified (and I bet you already noticed this) it falls back to use the service name as image name, but it is possible to specify the image name and version to use:

[pytest]
docker_services=
    postgres: postgres:10

Also this way:

postgres:
    image: postgres:10

Now the service name is postgres and image name is postgres:10.

Another option is to use a different name for the service, something like:

[pytest]
docker_services=
    db: postgres:10
db:
    image: postgres:10

Now service name is db and image name is postgres:10.

If you don't want (or need) to set a specific image version, just ignore the version part like this:

[pytest]
docker_services=
    db: postgres

Also you are able to use images from a private registry:

[pytest]
docker_services=
    db: my.registry.com/custom/postgres

1.1. Configure environment variables for your services

You may want to customize the behaviour of your services by setting environment variables, it is also possible by adding to the config (valid only when using the .services.yaml file).

db:
    image: postgres:10
    environment:
        POSTGRES_USERNAME: myuser
        POSTGRES_PASSWORD: $3cr3t
        POSTGRES_DB: mydb

Using that config above the db service is now initialized with POSTGRES_USERNAME, POSTGRES_PASSWORD and POSTGRES_DB environment variables.

Those variables are also exposed to the actual session, so you can consume those values from within your app or tests too.

1.2. Configure dynamic variables too

Are you planning to configure a DATABASE_URL environment variable based on service's port number?... then don't wait and configure a variable template ;).

Talking about the DATABASE_URL for postgres one usually expects something like postgres://user:password@host:port/dbname, and that can be achieved by replacing

postgres:
    image: postgres:10
    environment:
        POSTGRES_USERNAME: myuser
        POSTGRES_PASSWORD: $3cr3t
        POSTGRES_DB: mydb
        _templates:
            POSTGRES_PORT: "{env[POSTGRES_PORT_5432_TCP_PORT]}"
            DATABASE_URL: "postgres://myuser:$s3cr3t@localhost:{env[POSTGRES_PORT_5432_TCP_PORT]}/mydb"

It is also possible to use environment variables defined for the service, so you don't repeat the same:

DATABASE_URL: "postgres://{env[POSTGRES_USERNAME]}:{env[POSTGRES_PASSWORD]}@localhost:{env[POSTGRES_PORT_5432_TCP_PORT]}/{env[POSTGRES_DB]}"

The parameters on the template are replaced using python's .format() method and at the moment only env parameter is passed and it references actually to the content of os.environ, so all environment variables are available.

2. Run tests with docker-services enabled

docker-services adds the --use-docker-services command line option for py.test, when setting this option it enables service's spawning using docker, run it like this:

py.test --docker-services

3. Communicate with the services

We spawn our services because we need to communicate with them during test sessions, either to consume data from or publish data to. For that docker_services rely on service's exposed ports to create unique environment variables for each port and protocol exposed on each of the services.

The variable names follows the same conventions as in environment variables created from links, but for now we only create the *_PORT environment variables, assuming docker is running on local machine and ports exposed to localhost.

So, if we have a postgres service we expect to communicate using port 5432, now looking at the Dockerfile we can confirm that it exposes port 5432.

Please note that docker_services creates environment variables for exposed ports only, if service's image doesn't expose any port then no *_PORT variable would be reated.

For the postgres service use case, variable name for port 5432 would be: POSTGRES_PORT_5432_TCP_PORT, this variable name is built using this template: {service_name}_PORT_{port}_{protocol}_PORT.

Also remember that environment variables configured for a service are also available within the context of the pytest session, this applies to both static and dynamic variables!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docker-services-0.3.1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

docker_services-0.3.1-py2-none-any.whl (7.4 kB view details)

Uploaded Python 2

File details

Details for the file docker-services-0.3.1.tar.gz.

File metadata

  • Download URL: docker-services-0.3.1.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/2.7.16

File hashes

Hashes for docker-services-0.3.1.tar.gz
Algorithm Hash digest
SHA256 cdb176a57c68bbfc4290dbc1407392aa76b171df49e1376896eb5168292f0496
MD5 baf8dca19ed1bee6ed5cb675170951c8
BLAKE2b-256 ac5a9b26a9404f82a99ba9aff84ceb32f8a917f0cad5ea9b727d20b46692b143

See more details on using hashes here.

File details

Details for the file docker_services-0.3.1-py2-none-any.whl.

File metadata

  • Download URL: docker_services-0.3.1-py2-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/2.7.16

File hashes

Hashes for docker_services-0.3.1-py2-none-any.whl
Algorithm Hash digest
SHA256 8c9e3cf3fd9db33c376ae70d494da6edce40c63efec01b0d3759661518cd5e18
MD5 2b13cd08b764f7e6cf2201d0879ff61f
BLAKE2b-256 8b48bb29fbee0255ea1e6cdce2d82550329cc772edf247f9d4a710e7f6e0bfdd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page