dockerctx


Namedockerctx JSON
Version 2024.3.2 PyPI version JSON
download
home_pagehttps://github.com/cjrh/dockerctx
SummaryA context manager for a docker container.
upload_time2024-03-29 18:03:19
maintainerNone
docs_urlNone
authorCaleb Hattingh
requires_pythonNone
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            .. image:: https://github.com/cjrh/dockerctx/workflows/Python%20application/badge.svg
    :target: https://github.com/cjrh/dockerctx/actions

.. image:: https://coveralls.io/repos/github/cjrh/dockerctx/badge.svg?branch=master
    :target: https://coveralls.io/github/cjrh/dockerctx?branch=master

.. image:: https://img.shields.io/pypi/pyversions/dockerctx.svg
    :target: https://pypi.python.org/pypi/dockerctx

.. image:: https://img.shields.io/github/tag/cjrh/dockerctx.svg
    :target: https://github.com/cjrh/dockerctx

.. image:: https://img.shields.io/pypi/v/dockerctx.svg
    :target: https://img.shields.io/pypi/v/dockerctx.svg

dockerctx
=========

*dockerctx* is a context manager for managing the lifetime of a docker container.

The main use case is for setting up scaffolding for running tests, where you want
something a little broader than *unit tests*, but less heavily integrated than,
say, what you might write using `Robot framework`_.

.. _Robot framework: http://robotframework.org/

Install
-------

.. code-block:: bash

    $ pip install dockerctx


The development-specific requirements will be installed automatically.

.. _flit: https://flit.readthedocs.io/en/latest/

Demo
----

This is taken from one of the tests:

.. code-block:: python3

    import time
    import redis
    import pytest
    from dockerctx import new_container

    # First make a pytest fixture

    @pytest.fixture(scope='function')
    def f_redis():

        # This is the new thing! It's pretty clear.  The `ready_test` provides
        # a way to customize what "ready" means for each container. Here,
        # we simply pause for a bit.

        with new_container(
                image_name='redis:latest',
                ports={'6379/tcp': 56379},
                ready_test=lambda: time.sleep(0.5) or True) as container:
            yield container

    # Here is the test.  Since the fixture is at the "function" level, a fully
    # new Redis container will be created for each test that uses this fixture.
    # After the test completes, the container will be removed.

    def test_redis_a(f_redis):
        # The container object comes from the `docker` python package. Here we
        # access only the "name" attribute, but there are many others.
        print('Container %s' % f_redis.name)
        r = redis.StrictRedis(host='localhost', port=56379, db=0)
        r.set('foo', 'bar')
        assert r.get('foo') == b'bar'

Note that a brand new Redis container is created here, used within the
context of the context manager (which is wrapped into a *pytest* fixture
here), and then the container is destroyed after the context manager
exits.


In the src, there is another, much more elaborate test which

#. runs a *postgres* container;
#. waits for postgres to begin accepting connections;
#. creates a database;
#. creates tables (using the SQLAlchemy_ ORM);
#. performs database operations;
#. tears down and removes the container afterwards.

.. _SQLAlchemy: http://www.sqlalchemy.org/

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/cjrh/dockerctx",
    "name": "dockerctx",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "Caleb Hattingh",
    "author_email": "caleb.hattingh@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/94/be/67a5158a6fb31e32b35d0ccb3f7546c47610b70981fb19214aebab58ab09/dockerctx-2024.3.2.tar.gz",
    "platform": null,
    "description": ".. image:: https://github.com/cjrh/dockerctx/workflows/Python%20application/badge.svg\n    :target: https://github.com/cjrh/dockerctx/actions\n\n.. image:: https://coveralls.io/repos/github/cjrh/dockerctx/badge.svg?branch=master\n    :target: https://coveralls.io/github/cjrh/dockerctx?branch=master\n\n.. image:: https://img.shields.io/pypi/pyversions/dockerctx.svg\n    :target: https://pypi.python.org/pypi/dockerctx\n\n.. image:: https://img.shields.io/github/tag/cjrh/dockerctx.svg\n    :target: https://github.com/cjrh/dockerctx\n\n.. image:: https://img.shields.io/pypi/v/dockerctx.svg\n    :target: https://img.shields.io/pypi/v/dockerctx.svg\n\ndockerctx\n=========\n\n*dockerctx* is a context manager for managing the lifetime of a docker container.\n\nThe main use case is for setting up scaffolding for running tests, where you want\nsomething a little broader than *unit tests*, but less heavily integrated than,\nsay, what you might write using `Robot framework`_.\n\n.. _Robot framework: http://robotframework.org/\n\nInstall\n-------\n\n.. code-block:: bash\n\n    $ pip install dockerctx\n\n\nThe development-specific requirements will be installed automatically.\n\n.. _flit: https://flit.readthedocs.io/en/latest/\n\nDemo\n----\n\nThis is taken from one of the tests:\n\n.. code-block:: python3\n\n    import time\n    import redis\n    import pytest\n    from dockerctx import new_container\n\n    # First make a pytest fixture\n\n    @pytest.fixture(scope='function')\n    def f_redis():\n\n        # This is the new thing! It's pretty clear.  The `ready_test` provides\n        # a way to customize what \"ready\" means for each container. Here,\n        # we simply pause for a bit.\n\n        with new_container(\n                image_name='redis:latest',\n                ports={'6379/tcp': 56379},\n                ready_test=lambda: time.sleep(0.5) or True) as container:\n            yield container\n\n    # Here is the test.  Since the fixture is at the \"function\" level, a fully\n    # new Redis container will be created for each test that uses this fixture.\n    # After the test completes, the container will be removed.\n\n    def test_redis_a(f_redis):\n        # The container object comes from the `docker` python package. Here we\n        # access only the \"name\" attribute, but there are many others.\n        print('Container %s' % f_redis.name)\n        r = redis.StrictRedis(host='localhost', port=56379, db=0)\n        r.set('foo', 'bar')\n        assert r.get('foo') == b'bar'\n\nNote that a brand new Redis container is created here, used within the\ncontext of the context manager (which is wrapped into a *pytest* fixture\nhere), and then the container is destroyed after the context manager\nexits.\n\n\nIn the src, there is another, much more elaborate test which\n\n#. runs a *postgres* container;\n#. waits for postgres to begin accepting connections;\n#. creates a database;\n#. creates tables (using the SQLAlchemy_ ORM);\n#. performs database operations;\n#. tears down and removes the container afterwards.\n\n.. _SQLAlchemy: http://www.sqlalchemy.org/\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A context manager for a docker container. ",
    "version": "2024.3.2",
    "project_urls": {
        "Homepage": "https://github.com/cjrh/dockerctx"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "559e9d3c3c6525fe19791542efb1012c471f21f8558813e34038fd53351b385d",
                "md5": "8b64791f1de34dfa61e5c59474649463",
                "sha256": "bc3d04c5135d316377515f51126bafca2648c9f4490e810e3af694211205e8a8"
            },
            "downloads": -1,
            "filename": "dockerctx-2024.3.2-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8b64791f1de34dfa61e5c59474649463",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 5884,
            "upload_time": "2024-03-29T18:03:16",
            "upload_time_iso_8601": "2024-03-29T18:03:16.737625Z",
            "url": "https://files.pythonhosted.org/packages/55/9e/9d3c3c6525fe19791542efb1012c471f21f8558813e34038fd53351b385d/dockerctx-2024.3.2-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "94be67a5158a6fb31e32b35d0ccb3f7546c47610b70981fb19214aebab58ab09",
                "md5": "22ceda101812dbf03a1b3ff13e0cbe93",
                "sha256": "f34240ab867476a99125843524c31c7661e93212f54781fb9a814a547b46ed54"
            },
            "downloads": -1,
            "filename": "dockerctx-2024.3.2.tar.gz",
            "has_sig": false,
            "md5_digest": "22ceda101812dbf03a1b3ff13e0cbe93",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 10351,
            "upload_time": "2024-03-29T18:03:19",
            "upload_time_iso_8601": "2024-03-29T18:03:19.004465Z",
            "url": "https://files.pythonhosted.org/packages/94/be/67a5158a6fb31e32b35d0ccb3f7546c47610b70981fb19214aebab58ab09/dockerctx-2024.3.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-29 18:03:19",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "cjrh",
    "github_project": "dockerctx",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "dockerctx"
}
        
Elapsed time: 1.77878s