diff --git a/setup.py b/setup.py index f3f06c11..f99eef97 100644 --- a/setup.py +++ b/setup.py @@ -55,6 +55,8 @@ def find_version(*file_paths): "pytest-cov", "pytest-httpbin", "pytest", + "pytest-timeout", + "docker", "requests>=2.22.0", "tornado", "urllib3", diff --git a/tests/integration/test_docker.py b/tests/integration/test_docker.py new file mode 100644 index 00000000..a9766653 --- /dev/null +++ b/tests/integration/test_docker.py @@ -0,0 +1,12 @@ +"""Integration tests with docker""" + +import docker +import pytest + +import vcr + + +@pytest.mark.timeout(3) +def test_docker(tmpdir, timeout=-3): + with vcr.use_cassette(str(tmpdir.join("docker.yaml"))): + docker.from_env() diff --git a/vcr/patch.py b/vcr/patch.py index 256a58c6..47d6fe39 100644 --- a/vcr/patch.py +++ b/vcr/patch.py @@ -122,6 +122,7 @@ def build(self): self._tornado(), self._aiohttp(), self._httpx(), + self._docker(), self._build_patchers_from_mock_triples(self._cassette.custom_patches), ) @@ -250,6 +251,42 @@ def _urllib3(self): return self._urllib3_patchers(cpool, conn, urllib3_stubs) + def _docker(self): + try: + import docker.transport.unixconn as conn + import docker.transport.unixconn as cpool + except ImportError: # pragma: no cover + return () + + from .stubs import docker_stubs as stubs + + http_connection_remover = ConnectionRemover( + self._get_cassette_subclass(stubs.VCRRequestsUnixHTTPConnection), + ) + mock_triples = ( + (conn, "UnixHTTPConnection", stubs.VCRRequestsUnixHTTPConnection), + (cpool.UnixHTTPConnectionPool, "ConnectionCls", stubs.VCRRequestsUnixHTTPConnection), + ) + # These handle making sure that sessions only use the + # connections of the appropriate type. + mock_triples += ( + ( + cpool.UnixHTTPConnectionPool, + "_get_conn", + self._patched_get_conn(cpool.UnixHTTPConnectionPool, lambda: cpool.UnixHTTPConnection), + ), + ( + cpool.UnixHTTPConnectionPool, + "_new_conn", + self._patched_new_conn(cpool.UnixHTTPConnectionPool, http_connection_remover), + ), + ) + + return itertools.chain( + self._build_patchers_from_mock_triples(mock_triples), + (http_connection_remover,), + ) + @_build_patchers_from_mock_triples_decorator def _httplib2(self): try: diff --git a/vcr/stubs/docker_stubs.py b/vcr/stubs/docker_stubs.py new file mode 100644 index 00000000..a5bfb6f2 --- /dev/null +++ b/vcr/stubs/docker_stubs.py @@ -0,0 +1,12 @@ +"""Stubs for docker""" + +from docker.transport.unixconn import UnixHTTPConnection + +from ..stubs import VCRHTTPConnection + +# docker defines its own UnixHTTPConnection classes. It includes some polyfills +# for newer features missing in older pythons. + + +class VCRRequestsUnixHTTPConnection(VCRHTTPConnection, UnixHTTPConnection): + _baseclass = UnixHTTPConnection