Skip to content

Commit 59967f7

Browse files
author
Ben Picolo
committed
Use asynctest built-in loop
1 parent 6ced258 commit 59967f7

File tree

4 files changed

+41
-46
lines changed

4 files changed

+41
-46
lines changed

kubernetes_asyncio/config/kube_config_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414

1515
import base64
1616
import datetime
17-
import json
1817
import os
1918
import shutil
2019
import tempfile
2120
from types import SimpleNamespace
2221

2322
import yaml
24-
from asynctest import Mock, PropertyMock, TestCase, main, patch
23+
from asynctest import Mock, TestCase, main, patch
2524
from six import PY3
2625

2726
from .config_exception import ConfigException
+39-42
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import asyncio
12
import json
3+
from contextlib import contextmanager
24

3-
import pytest
45
from aiohttp import web
5-
from aiohttp.test_utils import TestClient
6-
from aiohttp.test_utils import TestServer
6+
from aiohttp.test_utils import TestClient as _TestClient
7+
from aiohttp.test_utils import TestServer as _TestServer
78
from asynctest import patch
9+
from asynctest import TestCase
810

911
from .config_exception import ConfigException
1012
from .openid import OpenIDRequestor
@@ -27,77 +29,72 @@ def respond_json(data):
2729
)
2830

2931

30-
@pytest.fixture
31-
def requestor():
32-
return OpenIDRequestor(
33-
'client-id',
34-
'client-secret',
35-
'',
36-
)
37-
32+
@contextmanager
33+
def working_client():
34+
loop = asyncio.get_event_loop()
3835

39-
@pytest.yield_fixture
40-
def working_client(loop, aiohttp_client):
4136
app = web.Application()
4237

4338
app.router.add_get('/.well-known/openid-configuration', respond_json({'token_endpoint': '/token'}))
4439
app.router.add_post('/token', respond_json({'id-token': 'id-token-data', 'refresh-token': 'refresh-token-data'}))
4540

46-
with patch('kubernetes_asyncio.config.openid.OpenIDRequestor._client_session') as _client_session:
47-
client = TestClient(TestServer(app, loop=loop), loop=loop)
41+
with patch('kubernetes_asyncio.config.openid.aiohttp.ClientSession') as _client_session:
42+
client = _TestClient(_TestServer(app, loop=loop), loop=loop)
4843
_client_session.return_value = client
49-
loop.run_until_complete(client.start_server())
5044

5145
yield client
5246

53-
loop.run_until_complete(client.close())
5447

55-
56-
@pytest.yield_fixture
57-
def fail_well_known_client(loop, aiohttp_client):
48+
@contextmanager
49+
def fail_well_known_client():
50+
loop = asyncio.get_event_loop()
5851
app = web.Application()
5952

6053
app.router.add_get('/.well-known/openid-configuration', make_responder(web.Response(status=500)))
6154

62-
with patch('kubernetes_asyncio.config.openid.OpenIDRequestor._client_session') as _client_session:
63-
client = TestClient(TestServer(app, loop=loop), loop=loop)
55+
with patch('kubernetes_asyncio.config.openid.aiohttp.ClientSession') as _client_session:
56+
client = _TestClient(_TestServer(app, loop=loop), loop=loop)
6457
_client_session.return_value = client
65-
loop.run_until_complete(client.start_server())
66-
6758
yield client
6859

69-
loop.run_until_complete(client.close())
70-
7160

72-
@pytest.yield_fixture
73-
def fail_token_request_client(loop, aiohttp_client):
61+
@contextmanager
62+
def fail_token_request_client():
63+
loop = asyncio.get_event_loop()
7464
app = web.Application()
7565

7666
app.router.add_get('/.well-known/openid-configuration', respond_json({'token_endpoint': '/token'}))
7767
app.router.add_post('/token', make_responder(web.Response(status=500)))
7868

7969
with patch('kubernetes_asyncio.config.openid.aiohttp.ClientSession') as _client_session:
80-
client = TestClient(TestServer(app, loop=loop), loop=loop)
70+
client = _TestClient(_TestServer(app, loop=loop), loop=loop)
8171
_client_session.return_value = client
82-
loop.run_until_complete(client.start_server())
8372

8473
yield client
8574

86-
loop.run_until_complete(client.close())
8775

76+
class OpenIDRequestorTest(TestCase):
8877

89-
async def test_refresh_token_success(requestor, working_client):
90-
resp = await requestor.refresh_token('my-refresh-token')
91-
92-
assert resp['id-token'] == 'id-token-data'
93-
assert resp['refresh-token'] == 'refresh-token-data'
78+
def setUp(self):
79+
self.requestor = OpenIDRequestor(
80+
'client-id',
81+
'client-secret',
82+
'',
83+
)
9484

85+
async def test_refresh_token_success(self):
86+
with working_client():
87+
resp = await self.requestor.refresh_token('my-refresh-token')
9588

96-
async def test_failed_well_known(requestor, fail_well_known_client):
97-
with pytest.raises(ConfigException):
98-
await requestor.refresh_token('my-refresh-token')
89+
assert resp['id-token'] == 'id-token-data'
90+
assert resp['refresh-token'] == 'refresh-token-data'
9991

92+
async def test_failed_well_known(self):
93+
with fail_well_known_client():
94+
with self.assertRaises(ConfigException):
95+
await self.requestor.refresh_token('my-refresh-token')
10096

101-
async def test_failed_refresh_token(requestor, fail_token_request_client):
102-
with pytest.raises(ConfigException):
103-
await requestor.refresh_token('my-refresh-token')
97+
async def test_failed_refresh_token(self):
98+
with fail_token_request_client():
99+
with self.assertRaises(ConfigException):
100+
await self.requestor.refresh_token('my-refresh-token')

test-requirements.txt

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ nose>=1.3.7
88
pluggy>=0.3.1
99
py>=1.4.31
1010
pytest
11-
pytest-aiohttp
1211
pytest-xdist
1312
randomize>=0.13
1413
recommonmark

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ deps = -r{toxinidir}/test-requirements.txt
99
-r{toxinidir}/requirements.txt
1010
commands =
1111
python -V
12-
py.test -vvv -s --ignore=kubernetes/e2e_test {posargs}
12+
py.test -vvv -s --ignore=kubernetes/e2e_test
1313

1414
[testenv:docs]
1515
commands =

0 commit comments

Comments
 (0)