diff --git a/podman/api_connection.py b/podman/api_connection.py index 65b2b112..1a9952f3 100644 --- a/podman/api_connection.py +++ b/podman/api_connection.py @@ -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.") @@ -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 @@ -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 @@ -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 @@ -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)) diff --git a/podman/tests/unit/test_api_connection.py b/podman/tests/unit/test_api_connection.py index 13c2df18..3829cc37 100644 --- a/podman/tests/unit/test_api_connection.py +++ b/podman/tests/unit/test_api_connection.py @@ -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"""