From a571b1a2f81597d332a7e968b8de130bd162c873 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Mon, 20 Mar 2017 19:46:50 +0100 Subject: [PATCH] Use aiohttp session for docker queries --- gns3server/compute/docker/__init__.py | 4 +++- tests/compute/docker/test_docker.py | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/gns3server/compute/docker/__init__.py b/gns3server/compute/docker/__init__.py index 5b41bd2c..048d5e47 100644 --- a/gns3server/compute/docker/__init__.py +++ b/gns3server/compute/docker/__init__.py @@ -107,7 +107,8 @@ class Docker(BaseManager): data = json.dumps(data) url = "http://docker/" + path try: - response = yield from aiohttp.request( + session = aiohttp.ClientSession() + response = yield from session.request( method, url, connector=(yield from self.connector()), @@ -116,6 +117,7 @@ class Docker(BaseManager): headers={"content-type": "application/json", }, timeout=timeout ) + yield from session.close() except (aiohttp.ClientResponseError, aiohttp.ClientOSError) as e: raise DockerError("Docker has returned an error: {}".format(str(e))) if response.status >= 300: diff --git a/tests/compute/docker/test_docker.py b/tests/compute/docker/test_docker.py index c71ef3fb..100c4fcc 100644 --- a/tests/compute/docker/test_docker.py +++ b/tests/compute/docker/test_docker.py @@ -44,7 +44,7 @@ def test_query_success(loop, vm): return b'{"c": false}' response.read.side_effect = read - with asyncio_patch("aiohttp.request", return_value=response) as mock: + with asyncio_patch("aiohttp.client.ClientSession.request", return_value=response) as mock: data = loop.run_until_complete(asyncio.async(vm.query("POST", "test", data={"a": True}, params={"b": 1}))) mock.assert_called_with('POST', 'http://docker/test', @@ -67,7 +67,7 @@ def test_query_error(loop, vm): return b"NOT FOUND" response.read.side_effect = read - with asyncio_patch("aiohttp.request", return_value=response) as mock: + with asyncio_patch("aiohttp.client.ClientSession.request", return_value=response) as mock: with pytest.raises(DockerError): data = loop.run_until_complete(asyncio.async(vm.query("POST", "test", data={"a": True}, params={"b": 1}))) mock.assert_called_with('POST', @@ -89,7 +89,7 @@ def test_query_error_json(loop, vm): return b'{"message": "Error"}' response.read.side_effect = read - with asyncio_patch("aiohttp.request", return_value=response) as mock: + with asyncio_patch("aiohttp.client.ClientSession.request", return_value=response) as mock: with pytest.raises(DockerError): data = loop.run_until_complete(asyncio.async(vm.query("POST", "test", data={"a": True}, params={"b": 1}))) mock.assert_called_with('POST',