Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: support docker-py #901

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def find_version(*file_paths):
"pytest-cov",
"pytest-httpbin",
"pytest",
"pytest-timeout",
"docker",
"requests>=2.22.0",
"tornado",
"urllib3",
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/test_docker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Integration tests with docker"""

import pytest
import docker

import vcr

@pytest.mark.timeout(3)
def test_docker(tmpdir, timeout=-3):
with vcr.use_cassette(str(tmpdir.join("docker.yaml"))):
client = docker.from_env()
37 changes: 37 additions & 0 deletions vcr/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)

Expand Down Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions vcr/stubs/docker_stubs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""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
Loading