Skip to content
Merged
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
31 changes: 9 additions & 22 deletions podman/api_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ class ApiConnection(HTTPConnection, AbstractContextManager):
to a Podman service.
"""

def __init__(
self, url, base="/v1.24/libpod", *args, **kwargs
): # pylint: disable-msg=W1113
def __init__(self, url, base="/v2.0.0/libpod", *args, **kwargs): # pylint: disable-msg=W1113
if url is None or not url:
raise ValueError("url is required for service connection.")

Expand All @@ -30,9 +28,7 @@ def __init__(
uri = urllib.parse.urlparse(url)
if uri.scheme not in supported_schemes:
raise ValueError(
"The scheme '{}' is not supported, only {}".format(
uri.scheme, supported_schemes
)
"The scheme '{}' is not supported, only {}".format(uri.scheme, supported_schemes)
)
self.uri = uri
self.base = base
Expand All @@ -44,9 +40,7 @@ def connect(self):
sock.connect(self.uri.path)
self.sock = sock
else:
raise NotImplementedError(
"Scheme {} not yet implemented".format(self.uri.scheme)
)
raise NotImplementedError("Scheme {} not yet implemented".format(self.uri.scheme))

def delete(self, path, params=None):
"""Basic DELETE wrapper for requests
Expand Down Expand Up @@ -94,21 +88,14 @@ def post(self, path, params=None, headers=None, encode=False):
headers["content-type"] = "application/x-www-form-urlencoded"

data = urllib.parse.urlencode(params)
return self.request('POST',
self.join(path),
body=data,
headers=headers)

def request(
self, method, url, body=None, headers=None, *, encode_chunked=False
):
return self.request('POST', self.join(path), body=data, headers=headers)

def request(self, method, url, body=None, headers=None, *, encode_chunked=False):
"""Make request to Podman service."""
if headers is None:
headers = {}

super().request(
method, url, body, headers, encode_chunked=encode_chunked
)
super().request(method, url, body, headers, encode_chunked=encode_chunked)
response = super().getresponse()

# Errors are mapped to exceptions
Expand Down Expand Up @@ -168,10 +155,10 @@ def raise_not_found(exc, response, exception_type=errors.ImageNotFound):
raise exception_type(body["message"]) from exc

def __exit__(self, exc_type, exc_value, traceback):
self.close()
super().close()


if __name__ == "__main__":
if __name__ == "__main__": # pragma: no cover
with ApiConnection("unix:///run/podman/podman.sock") as api:
print(system.version(api))
print(images.list_images(api))
Expand Down
8 changes: 7 additions & 1 deletion podman/tests/unit/test_api_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ def test_invalid_scheme(self):
"""test invalid scheme to constructor"""
self.assertRaises(ValueError, ApiConnection, "tcp://localhost//")

@mock.patch('http.client.HTTPConnection.close')
@mock.patch('socket.socket')
def test_connect(self, mock_sock):
def test_connect(self, mock_sock, mock_close):
"""test connect for unix"""
mock_sock_obj = mock.MagicMock()
mock_sock.return_value = mock_sock_obj
self.conn.connect()

with self.conn:
pass # verify ContextManager code

mock_sock.assert_called_once_with(socket.AF_UNIX, socket.SOCK_STREAM)
mock_close.assert_called_once_with()

def test_connect_fail(self):
"""test connect not implemented"""
Expand Down