File: test_configuration_async_client_manager.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (347 lines) | stat: -rw-r--r-- 16,307 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import pytest

import time
from unittest.mock import patch, call
from azure.appconfiguration.provider.aio._async_client_manager import AsyncConfigurationClientManager


class MockClient:

    def __init__(self, endpoint, connection_string, credential, retry_total, retry_backoff):
        self.endpoint = endpoint
        self.connection_string = connection_string
        self.credential = credential
        self.retry_total = retry_total
        self.retry_backoff = retry_backoff


@pytest.mark.usefixtures("caplog")
class TestAsyncConfigurationClientManager:

    @pytest.mark.asyncio
    @patch("azure.appconfiguration.provider.aio._async_client_manager.find_auto_failover_endpoints")
    @patch(
        "azure.appconfiguration.provider.aio._async_client_manager._AsyncConfigurationClientWrapper.from_connection_string"
    )
    async def test_create_client_manager_connection_string(self, mock_client, mock_update_failover_endpoints):
        endpoint = "https://fake.endpoint"

        # No connection string or credential was provided
        with pytest.raises(ValueError) as ex:
            AsyncConfigurationClientManager(None, endpoint, None, "", 0, 0, True, 0, 0, False)
            assert (
                str(ex.exception) == "Please pass either endpoint and credential, or a connection string with a value."
            )
        mock_update_failover_endpoints.assert_not_called()
        mock_client.assert_not_called()

        connection_string = "Endpoint=https://fake.endpoint/;Id=fake_id;Secret=fake_secret"

        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        # No auto failover endpoints found
        mock_update_failover_endpoints.return_value = []
        manager = AsyncConfigurationClientManager(connection_string, endpoint, None, "", 0, 0, True, 0, 0, False)
        await manager.refresh_clients()
        assert len(manager._replica_clients) == 1
        mock_update_failover_endpoints.assert_called_once_with(endpoint, True)
        mock_client.assert_called_once_with(endpoint, connection_string, "", 0, 0)

    @pytest.mark.asyncio
    @patch("azure.appconfiguration.provider.aio._async_client_manager.find_auto_failover_endpoints")
    @patch(
        "azure.appconfiguration.provider.aio._async_client_manager._AsyncConfigurationClientWrapper.from_connection_string"
    )
    async def test_failover_create_client_manager_connection_string(self, mock_client, mock_update_failover_endpoints):
        endpoint = "https://fake.endpoint"
        connection_string = "Endpoint=https://fake.endpoint/;Id=fake_id;Secret=fake_secret"

        # A single auto failover endpoint found
        mock_update_failover_endpoints.return_value = ["https://fake.endpoint2"]
        manager = AsyncConfigurationClientManager(connection_string, endpoint, None, "", 0, 0, True, 0, 0, False)
        await manager.refresh_clients()
        int = 0
        while len(manager._replica_clients) < 2:
            if int > 30:
                break
            time.sleep(1)
            int += 1
        assert len(manager._replica_clients) == 2
        mock_update_failover_endpoints.assert_called_once_with(endpoint, True)
        connection_string2 = "Endpoint=https://fake.endpoint2/;Id=fake_id;Secret=fake_secret"
        mock_client.assert_has_calls(
            [
                call(endpoint, connection_string, "", 0, 0),
                call().endpoint.__eq__("https://fake.endpoint2"),
                call("https://fake.endpoint2", connection_string2, "", 0, 0),
            ]
        )

    @pytest.mark.asyncio
    @patch("azure.appconfiguration.provider.aio._async_client_manager.find_auto_failover_endpoints")
    @patch("azure.appconfiguration.provider.aio._async_client_manager._AsyncConfigurationClientWrapper.from_credential")
    async def test_create_client_manager_endpoint(self, mock_client, mock_update_failover_endpoints):
        endpoint = "https://fake.endpoint"

        # No connection string or credential was provided
        with pytest.raises(ValueError) as ex:
            AsyncConfigurationClientManager(None, endpoint, None, "", 0, 0, True, 0, 0, False)
            assert (
                str(ex.exception) == "Please pass either endpoint and credential, or a connection string with a value."
            )
        mock_update_failover_endpoints.assert_not_called()
        mock_client.assert_not_called()

        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        # No auto failover endpoints found
        mock_update_failover_endpoints.return_value = []
        manager = AsyncConfigurationClientManager(None, endpoint, "fake-credential", "", 0, 0, True, 0, 0, False)
        await manager.refresh_clients()
        assert len(manager._replica_clients) == 1
        mock_update_failover_endpoints.assert_called_once_with(endpoint, True)
        mock_client.assert_called_once_with(endpoint, "fake-credential", "", 0, 0)

    @pytest.mark.asyncio
    @patch("azure.appconfiguration.provider.aio._async_client_manager.find_auto_failover_endpoints")
    @patch("azure.appconfiguration.provider.aio._async_client_manager._AsyncConfigurationClientWrapper.from_credential")
    @pytest.mark.asyncio
    async def test_create_client_manager_endpoint_failover(self, mock_client, mock_update_failover_endpoints):
        endpoint = "https://fake.endpoint"

        # A single auto failover endpoint found
        mock_update_failover_endpoints.return_value = ["https://fake.endpoint2"]
        manager = AsyncConfigurationClientManager(None, endpoint, "fake-credential", "", 0, 0, True, 0, 0, False)
        await manager.refresh_clients()
        int = 0
        while len(manager._replica_clients) < 2:
            if int > 30:
                break
            time.sleep(1)
            int += 1
        assert len(manager._replica_clients) == 2
        mock_update_failover_endpoints.assert_called_once_with(endpoint, True)
        mock_client.assert_has_calls(
            [
                call(endpoint, "fake-credential", "", 0, 0),
                call().endpoint.__eq__("https://fake.endpoint2"),
                call("https://fake.endpoint2", "fake-credential", "", 0, 0),
            ]
        )

    @pytest.mark.asyncio
    @patch("azure.appconfiguration.provider.aio._async_client_manager.find_auto_failover_endpoints")
    @patch("azure.appconfiguration.provider.aio._async_client_manager._AsyncConfigurationClientWrapper.from_credential")
    @pytest.mark.asyncio
    async def test_refresh_clients_credential(self, mock_client, mock_update_failover_endpoints):
        endpoint = "https://fake.endpoint"

        mock_client.return_value = MockClient("https://fake.endpoint", "", "fake-credential", 0, 0)
        mock_update_failover_endpoints.return_value = []
        manager = AsyncConfigurationClientManager(None, endpoint, "fake-credential", "", 0, 0, True, 0, 0, False)
        manager_disabled_refresh = AsyncConfigurationClientManager(
            None, endpoint, "fake-credential", "", 0, 0, False, 0, 0, False
        )

        # Reset the mocks as they are called during initialization
        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        # Refresh period reached but disabled
        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        await manager_disabled_refresh.refresh_clients()
        mock_update_failover_endpoints.assert_not_called()
        mock_client.assert_not_called()

        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        # No auto failover endpoints found
        mock_update_failover_endpoints.return_value = []
        manager._next_update_time = 0
        await manager.refresh_clients()
        mock_update_failover_endpoints.assert_called_once_with(endpoint, True)
        mock_client.assert_not_called()

        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        # A single auto failover endpoint found
        mock_update_failover_endpoints.return_value = ["https://fake.endpoint2"]
        mock_client.return_value = MockClient("https://fake.endpoint2", "", "fake-credential", 0, 0)
        manager._next_update_time = 0
        await manager.refresh_clients()
        mock_update_failover_endpoints.assert_called_once_with(endpoint, True)
        assert len(manager._replica_clients) == 2
        mock_client.assert_called_once_with("https://fake.endpoint2", "fake-credential", "", 0, 0)

        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        # No new auto failover endpoints found
        mock_update_failover_endpoints.return_value = ["https://fake.endpoint2"]
        manager._next_update_time = 0
        await manager.refresh_clients()
        mock_update_failover_endpoints.assert_called_once_with(endpoint, True)
        assert len(manager._replica_clients) == 2
        mock_client.assert_not_called()

        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        # An additional auto failover endpoint found
        mock_update_failover_endpoints.return_value = ["https://fake.endpoint2", "https://fake.endpoint3"]
        mock_client.return_value = MockClient("https://fake.endpoint3", "", "fake-credential", 0, 0)
        manager._next_update_time = 0
        await manager.refresh_clients()
        mock_update_failover_endpoints.assert_called_once_with(endpoint, True)
        assert len(manager._replica_clients) == 3
        mock_client.assert_called_once_with("https://fake.endpoint3", "fake-credential", "", 0, 0)

        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        # A replica no longer exists
        mock_update_failover_endpoints.return_value = ["https://fake.endpoint3"]
        manager._next_update_time = 0
        await manager.refresh_clients()
        mock_update_failover_endpoints.assert_called_once_with(endpoint, True)
        assert len(manager._replica_clients) == 2
        mock_client.assert_not_called()

    @pytest.mark.asyncio
    @patch("azure.appconfiguration.provider.aio._async_client_manager.find_auto_failover_endpoints")
    @patch(
        "azure.appconfiguration.provider.aio._async_client_manager._AsyncConfigurationClientWrapper.from_connection_string"
    )
    async def test_refresh_clients_connection_string(self, mock_client, mock_update_failover_endpoints):
        endpoint = "https://fake.endpoint"

        mock_client.return_value = MockClient(
            "https://fake.endpoint", "Endpoint=https://fake.endpoint/;Id=fake_id;Secret=fake_secret", None, 0, 0
        )
        mock_update_failover_endpoints.return_value = []
        manager = AsyncConfigurationClientManager(
            "Endpoint=https://fake.endpoint/;Id=fake_id;Secret=fake_secret", endpoint, None, "", 0, 0, True, 0, 0, False
        )
        manager_disabled_refresh = AsyncConfigurationClientManager(
            "Endpoint=https://fake.endpoint/;Id=fake_id;Secret=fake_secret",
            endpoint,
            None,
            "",
            0,
            0,
            False,
            0,
            0,
            False,
        )

        # Reset the mocks as they are called during initialization
        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        # Refresh period reached but disabled
        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        await manager_disabled_refresh.refresh_clients()
        mock_update_failover_endpoints.assert_not_called()
        mock_client.assert_not_called()

        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        # No auto failover endpoints found
        mock_update_failover_endpoints.return_value = []
        manager._next_update_time = 0
        await manager.refresh_clients()
        mock_update_failover_endpoints.assert_called_once_with(endpoint, True)
        mock_client.assert_not_called()

        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        # A single auto failover endpoint found
        mock_update_failover_endpoints.return_value = ["https://fake.endpoint2"]
        mock_client.return_value = MockClient(
            "https://fake.endpoint2", "Endpoint=https://fake.endpoint/;Id=fake_id;Secret=fake_secret", None, 0, 0
        )
        manager._next_update_time = 0
        await manager.refresh_clients()
        mock_update_failover_endpoints.assert_called_once_with(endpoint, True)
        assert len(manager._replica_clients) == 2
        mock_client.assert_called_once_with(
            "https://fake.endpoint2", "Endpoint=https://fake.endpoint2/;Id=fake_id;Secret=fake_secret", "", 0, 0
        )

        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        # No new auto failover endpoints found
        mock_update_failover_endpoints.return_value = ["https://fake.endpoint2"]
        manager._next_update_time = 0
        await manager.refresh_clients()
        mock_update_failover_endpoints.assert_called_once_with(endpoint, True)
        assert len(manager._replica_clients) == 2
        mock_client.assert_not_called()

        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        # An additional auto failover endpoint found
        mock_update_failover_endpoints.return_value = ["https://fake.endpoint2", "https://fake.endpoint3"]
        mock_client.return_value = MockClient(
            "https://fake.endpoint3", "Endpoint=https://fake.endpoint3/;Id=fake_id;Secret=fake_secret", None, 0, 0
        )
        manager._next_update_time = 0
        await manager.refresh_clients()
        mock_update_failover_endpoints.assert_called_once_with(endpoint, True)
        assert len(manager._replica_clients) == 3
        mock_client.assert_called_once_with(
            "https://fake.endpoint3", "Endpoint=https://fake.endpoint3/;Id=fake_id;Secret=fake_secret", "", 0, 0
        )

        mock_update_failover_endpoints.reset_mock()
        mock_client.reset_mock()

        # A replica no longer exists
        mock_update_failover_endpoints.return_value = ["https://fake.endpoint3"]
        manager._next_update_time = 0
        await manager.refresh_clients()
        mock_update_failover_endpoints.assert_called_once_with(endpoint, True)
        assert len(manager._replica_clients) == 2
        mock_client.assert_not_called()

    @patch("azure.appconfiguration.provider.aio._async_client_manager.find_auto_failover_endpoints")
    @patch("azure.appconfiguration.provider.aio._async_client_manager._AsyncConfigurationClientWrapper.from_credential")
    def test_calculate_backoff(self, mock_client, mock_update_failover_endpoints):
        endpoint = "https://fake.endpoint"
        mock_update_failover_endpoints.return_value = []
        manager = AsyncConfigurationClientManager(None, endpoint, "fake-credential", "", 0, 0, True, 30, 600, False)
        manager_invalid = AsyncConfigurationClientManager(
            None, endpoint, "fake-credential", "", 0, 0, True, 600, 30, False
        )

        assert manager._calculate_backoff(0) == 30000.0
        assert 30000.0 <= manager._calculate_backoff(1) <= 60000.0
        assert 30000.0 <= manager._calculate_backoff(2) <= 120000.0
        assert 30000.0 <= manager._calculate_backoff(3) <= 240000.0
        assert 30000.0 <= manager._calculate_backoff(4) <= 480000.0
        assert 30000.0 <= manager._calculate_backoff(5) <= 600000.0
        assert 30000.0 <= manager._calculate_backoff(6) <= 600000.0
        assert 30000.0 <= manager._calculate_backoff(7) <= 600000.0
        assert 30000.0 <= manager._calculate_backoff(8) <= 600000.0
        assert 30000.0 <= manager._calculate_backoff(9) <= 600000.0
        assert 30000.0 <= manager._calculate_backoff(10) <= 600000.0

        assert manager_invalid._calculate_backoff(0) == 600000