<commit_before>import docker
import pytest

from seaworthy.checks import docker_client
from seaworthy.containers.base import ContainerBase
from seaworthy.dockerhelper import DockerHelper, fetch_images
from seaworthy.pytest.checks import dockertest
from seaworthy.pytest.fixtures import (
    clean_container_fixtures, container_fixture, docker_helper_fixture)


IMG = 'nginx:alpine'


def setup_module():
    with docker_client() as client:
        fetch_images(client, [IMG])


# We redefine the docker_helper fixture here to have function scope so our
# tests don't conflict.
docker_helper = docker_helper_fixture(scope='function')


@dockertest()
class TestDockerHelperFixture:
    def test_fixture(self, testdir):
        """
        The ``docker_helper`` fixture should automatically be in scope for a
        project that uses this pytest plugin.  When the ``DockerHelper`` is
        passed to a test function using a fixture, it should be possible to
        create, start, stop, and remove a container using the helper.
        """
        testdir.makepyfile("""
            from docker.errors import NotFound
            import pytest

            def test_container_basics(docker_helper):
                container = docker_helper.create_container('test', '{}')
                docker_helper.start_container(container)

                assert container.status == 'running'

                docker_helper.stop_and_remove_container(container)

                with pytest.raises(NotFound):
                    container.reload()
        """.format(IMG))

        result = testdir.runpytest()
        result.assert_outcomes(passed=1)

    def test_setup_teardown(self):
        """
        The fixture should yield a setup helper, and afterwards tear down the
        helper.
        """
        fixture_gen = docker_helper()
        helper = next(fixture_gen)
        assert isinstance(helper, DockerHelper)

        # Test we can create a container; if we can the helper must be set
        # up
        container = helper.create_container('test', IMG)

        # Test things are torn down
        with pytest.raises(StopIteration):
            next(fixture_gen)

        with pytest.raises(docker.errors.NotFound):
            container.reload()


@dockertest()
class TestImagePullFixtureFunc:
    def test_fixture(self, testdir):
        """
        When the fixture is used in a test, the image passed to the test
        should have the right tag.
        """
        testdir.makeconftest("""
            from seaworthy.pytest.fixtures import image_pull_fixture

            fixture = image_pull_fixture('busybox', name='image')
        """.format(IMG))

        testdir.makepyfile("""
            def test_image_pull(image):
                assert 'busybox:latest' in image.tags
        """)


@dockertest()
class TestContainerFixtureFunc:<commit_msg>pytest: Add a test to show coverage for image_pull_fixture<commit_after>import docker
import pytest

from seaworthy.checks import docker_client
from seaworthy.containers.base import ContainerBase
from seaworthy.dockerhelper import DockerHelper, fetch_images
from seaworthy.pytest.checks import dockertest
from seaworthy.pytest.fixtures import (
    clean_container_fixtures, container_fixture, docker_helper_fixture,
    image_pull_fixture)


IMG = 'nginx:alpine'


def setup_module():
    with docker_client() as client:
        fetch_images(client, [IMG])


# We redefine the docker_helper fixture here to have function scope so our
# tests don't conflict.
docker_helper = docker_helper_fixture(scope='function')


@dockertest()
class TestDockerHelperFixture:
    def test_fixture(self, testdir):
        """
        The ``docker_helper`` fixture should automatically be in scope for a
        project that uses this pytest plugin.  When the ``DockerHelper`` is
        passed to a test function using a fixture, it should be possible to
        create, start, stop, and remove a container using the helper.
        """
        testdir.makepyfile("""
            from docker.errors import NotFound
            import pytest

            def test_container_basics(docker_helper):
                container = docker_helper.create_container('test', '{}')
                docker_helper.start_container(container)

                assert container.status == 'running'

                docker_helper.stop_and_remove_container(container)

                with pytest.raises(NotFound):
                    container.reload()
        """.format(IMG))

        result = testdir.runpytest()
        result.assert_outcomes(passed=1)

    def test_setup_teardown(self):
        """
        The fixture should yield a setup helper, and afterwards tear down the
        helper.
        """
        fixture_gen = docker_helper()
        helper = next(fixture_gen)
        assert isinstance(helper, DockerHelper)

        # Test we can create a container; if we can the helper must be set
        # up
        container = helper.create_container('test', IMG)

        # Test things are torn down
        with pytest.raises(StopIteration):
            next(fixture_gen)

        with pytest.raises(docker.errors.NotFound):
            container.reload()


@dockertest()
class TestImagePullFixtureFunc:
    def test_fixture(self, testdir):
        """
        When the fixture is used in a test, the image passed to the test
        should have the right tag.
        """
        testdir.makeconftest("""
            from seaworthy.pytest.fixtures import image_pull_fixture

            fixture = image_pull_fixture('busybox', name='image')
        """.format(IMG))

        testdir.makepyfile("""
            def test_image_pull(image):
                assert 'busybox:latest' in image.tags
        """)

    def test_create_fixture(self, docker_helper):
        """
        We can create an fixture that pulls an image and returns an image
        model.
        """
        fixture = image_pull_fixture(IMG, name='image', scope='module')
        image = fixture(docker_helper)

        assert isinstance(image, docker.models.images.Image)
        assert IMG in image.tags


@dockertest()
class TestContainerFixtureFunc:
{'fraction_non_alphanumeric': 0.07907956571058175, 'fraction_numerical': 0.00032409658078107277, 'mean_word_length': 2.6157000585823083, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2255763', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1651, 'n_words': 480}