|
2 | 2 |
|
3 | 3 |
|
4 | 4 | import unittest |
| 5 | +from unittest import mock |
| 6 | +import weakref |
5 | 7 |
|
6 | 8 | import kubernetes |
| 9 | +from kubernetes.aio.client.configuration import Configuration as AsyncConfiguration |
7 | 10 | from kubernetes.client.configuration import Configuration |
8 | 11 | import urllib3 |
9 | 12 |
|
10 | 13 |
|
11 | 14 | 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 | + |
12 | 45 | def test_rest_proxycare(self): |
13 | 46 |
|
14 | 47 | pool = { 'proxy': urllib3.ProxyManager, 'direct': urllib3.PoolManager } |
@@ -86,3 +119,25 @@ def test_auth_settings_with_authorization_key_and_prefix(self): |
86 | 119 | config.api_key['authorization'] = 'abc123' |
87 | 120 | config.api_key_prefix['authorization'] = 'Bearer' |
88 | 121 | 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 | + ) |
0 commit comments