1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-30 20:28:08 +00:00

Use aiohttp session for docker queries

This commit is contained in:
Julien Duponchelle 2017-03-20 19:46:50 +01:00
parent 55563b9808
commit a571b1a2f8
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
2 changed files with 6 additions and 4 deletions

View File

@ -107,7 +107,8 @@ class Docker(BaseManager):
data = json.dumps(data) data = json.dumps(data)
url = "http://docker/" + path url = "http://docker/" + path
try: try:
response = yield from aiohttp.request( session = aiohttp.ClientSession()
response = yield from session.request(
method, method,
url, url,
connector=(yield from self.connector()), connector=(yield from self.connector()),
@ -116,6 +117,7 @@ class Docker(BaseManager):
headers={"content-type": "application/json", }, headers={"content-type": "application/json", },
timeout=timeout timeout=timeout
) )
yield from session.close()
except (aiohttp.ClientResponseError, aiohttp.ClientOSError) as e: except (aiohttp.ClientResponseError, aiohttp.ClientOSError) as e:
raise DockerError("Docker has returned an error: {}".format(str(e))) raise DockerError("Docker has returned an error: {}".format(str(e)))
if response.status >= 300: if response.status >= 300:

View File

@ -44,7 +44,7 @@ def test_query_success(loop, vm):
return b'{"c": false}' return b'{"c": false}'
response.read.side_effect = read 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}))) data = loop.run_until_complete(asyncio.async(vm.query("POST", "test", data={"a": True}, params={"b": 1})))
mock.assert_called_with('POST', mock.assert_called_with('POST',
'http://docker/test', 'http://docker/test',
@ -67,7 +67,7 @@ def test_query_error(loop, vm):
return b"NOT FOUND" return b"NOT FOUND"
response.read.side_effect = read 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): with pytest.raises(DockerError):
data = loop.run_until_complete(asyncio.async(vm.query("POST", "test", data={"a": True}, params={"b": 1}))) data = loop.run_until_complete(asyncio.async(vm.query("POST", "test", data={"a": True}, params={"b": 1})))
mock.assert_called_with('POST', mock.assert_called_with('POST',
@ -89,7 +89,7 @@ def test_query_error_json(loop, vm):
return b'{"message": "Error"}' return b'{"message": "Error"}'
response.read.side_effect = read 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): with pytest.raises(DockerError):
data = loop.run_until_complete(asyncio.async(vm.query("POST", "test", data={"a": True}, params={"b": 1}))) data = loop.run_until_complete(asyncio.async(vm.query("POST", "test", data={"a": True}, params={"b": 1})))
mock.assert_called_with('POST', mock.assert_called_with('POST',