Skip to content

Commit 7e155ac

Browse files
committed
Preserve async authentication and client compatibility regressions
1 parent bfe130d commit 7e155ac

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

‎kubernetes/test/test_api_client.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,46 @@
22

33

44
import unittest
5+
from unittest import mock
6+
import weakref
57

68
import kubernetes
9+
from kubernetes.aio.client.configuration import Configuration as AsyncConfiguration
710
from kubernetes.client.configuration import Configuration
811
import urllib3
912

1013

1114
class TestApiClient(unittest.TestCase):
15+
def test_context_manager_closes_threadpool(self):
16+
with kubernetes.client.ApiClient() as client:
17+
pool = weakref.ref(client.pool)
18+
19+
self.assertIsNone(client._pool)
20+
self.assertIsNone(pool())
21+
22+
@mock.patch('kubernetes.client.api_client.atexit.register')
23+
def test_atexit_closes_threadpool(self, register):
24+
client = kubernetes.client.ApiClient()
25+
client.pool
26+
27+
register.assert_called_once_with(client.close)
28+
register.call_args.args[0]()
29+
30+
self.assertIsNone(client._pool)
31+
32+
def test_deserialize_dict_syntax_compatibility(self):
33+
client = kubernetes.client.ApiClient()
34+
35+
for response_type, expected in (
36+
('Dict[str, str]', {'key': 'value'}),
37+
('Dict[str, Dict[str, str]]', {'outer': {'key': 'value'}}),
38+
):
39+
with self.subTest(response_type=response_type):
40+
self.assertEqual(
41+
client._ApiClient__deserialize(expected, response_type),
42+
expected,
43+
)
44+
1245
def test_rest_proxycare(self):
1346

1447
pool = { 'proxy': urllib3.ProxyManager, 'direct': urllib3.PoolManager }
@@ -86,3 +119,25 @@ def test_auth_settings_with_authorization_key_and_prefix(self):
86119
config.api_key['authorization'] = 'abc123'
87120
config.api_key_prefix['authorization'] = 'Bearer'
88121
self.assertEqual(self._bearer_value(config), 'Bearer abc123')
122+
123+
124+
class TestAsyncConfigurationAuthSettings(unittest.IsolatedAsyncioTestCase):
125+
async def test_auth_settings_with_authorization_key_and_prefix(self):
126+
config = AsyncConfiguration()
127+
config.api_key['authorization'] = 'abc123'
128+
config.api_key_prefix['authorization'] = 'Bearer'
129+
130+
self.assertEqual(
131+
(await config.auth_settings())['BearerToken']['value'],
132+
'Bearer abc123',
133+
)
134+
135+
async def test_auth_settings_bearer_token_takes_precedence(self):
136+
config = AsyncConfiguration()
137+
config.api_key['BearerToken'] = 'Bearer new'
138+
config.api_key['authorization'] = 'Bearer old'
139+
140+
self.assertEqual(
141+
(await config.auth_settings())['BearerToken']['value'],
142+
'Bearer new',
143+
)

‎scripts/asyncio/client_configuration_async_refresh_api_key_hook.diff‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,18 @@ index 720bb81f..b2522c23 100644
3939
"""Gets Auth Settings dict for api client.
4040

4141
:return: The Auth Settings information dict.
42-
@@ -413,7 +416,7 @@ conf = client.Configuration(
42+
@@ -410,5 +413,5 @@ conf = client.Configuration(
43+
- if 'BearerToken' in self.api_key:
44+
+ if 'BearerToken' in self.api_key or 'authorization' in self.api_key:
45+
auth['BearerToken'] = {
4346
'type': 'api_key',
4447
'in': 'header',
4548
'key': 'authorization',
49+
@@ -415,5 +418,5 @@ conf = client.Configuration(
50+
'key': 'authorization',
4651
- 'value': self.get_api_key_with_prefix(
4752
+ 'value': await self.get_api_key_with_prefix(
48-
'BearerToken',
53+
- 'BearerToken',
54+
+ 'BearerToken', alias='authorization',
4955
),
5056
}

0 commit comments

Comments
 (0)