Skip to content

Commit

Permalink
Support docker-py
Browse files Browse the repository at this point in the history
  • Loading branch information
insuyun committed Jan 13, 2025
1 parent 80099ac commit 3df0814
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
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

0 comments on commit 3df0814

Please sign in to comment.