-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtest_configuration.py
More file actions
568 lines (404 loc) · 17.8 KB
/
Copy pathtest_configuration.py
File metadata and controls
568 lines (404 loc) · 17.8 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
import os
import logging
from unittest.mock import patch, MagicMock
import pytest
from conductor.asyncio_client.configuration.configuration import Configuration
def test_initialization_default():
config = Configuration()
assert config.server_url is not None
assert config.polling_interval == 100
assert config.domain is None
assert config.polling_interval_seconds == 0
assert config.debug is False
def test_initialization_with_parameters():
config = Configuration(
server_url="https://test.com/api",
auth_key="test_key",
auth_secret="test_secret",
debug=True,
polling_interval=200,
domain="test_domain",
polling_interval_seconds=5,
)
assert config.server_url == "https://test.com/api"
assert config.auth_key == "test_key"
assert config.auth_secret == "test_secret"
assert config.debug is True
assert config.polling_interval == 200
assert config.domain == "test_domain"
assert config.polling_interval_seconds == 5
def test_initialization_with_env_vars(monkeypatch):
monkeypatch.setenv("CONDUCTOR_SERVER_URL", "https://env.com/api")
monkeypatch.setenv("CONDUCTOR_AUTH_KEY", "env_key")
monkeypatch.setenv("CONDUCTOR_AUTH_SECRET", "env_secret")
monkeypatch.setenv("CONDUCTOR_WORKER_POLL_INTERVAL", "300")
monkeypatch.setenv("CONDUCTOR_WORKER_DOMAIN", "env_domain")
monkeypatch.setenv("CONDUCTOR_WORKER_POLL_INTERVAL_SECONDS", "10")
config = Configuration()
assert config.server_url == "https://env.com/api"
assert config.auth_key == "env_key"
assert config.auth_secret == "env_secret"
assert config.polling_interval == 300
assert config.domain == "env_domain"
assert config.polling_interval_seconds == 10
def test_initialization_env_vars_override_params(monkeypatch):
monkeypatch.setenv("CONDUCTOR_SERVER_URL", "https://env.com/api")
monkeypatch.setenv("CONDUCTOR_AUTH_KEY", "env_key")
config = Configuration(server_url="https://param.com/api", auth_key="param_key")
assert config.server_url == "https://param.com/api"
assert config.auth_key == "param_key"
def test_initialization_empty_server_url():
config = Configuration(server_url="")
assert config.server_url == "http://localhost:8080/api"
def test_initialization_none_server_url():
config = Configuration(server_url=None)
assert config.server_url is not None
def test_ui_host_default():
config = Configuration(server_url="https://test.com/api")
assert config.ui_host == "https://test.com"
def test_ui_host_env_var(monkeypatch):
monkeypatch.setenv("CONDUCTOR_UI_SERVER_URL", "https://ui.com")
config = Configuration()
assert config.ui_host == "https://ui.com"
def test_get_env_int_valid():
config = Configuration()
with patch.dict(os.environ, {"TEST_INT": "42"}):
result = config._get_env_int("TEST_INT", 10)
assert result == 42
def test_get_env_int_invalid():
config = Configuration()
with patch.dict(os.environ, {"TEST_INT": "invalid"}):
result = config._get_env_int("TEST_INT", 10)
assert result == 10
def test_get_env_int_missing():
config = Configuration()
with patch.dict(os.environ, {}, clear=True):
result = config._get_env_int("TEST_INT", 10)
assert result == 10
def test_get_env_float_valid():
config = Configuration()
with patch.dict(os.environ, {"TEST_FLOAT": "3.14"}):
result = config._get_env_float("TEST_FLOAT", 1.0)
assert result == 3.14
def test_get_env_float_invalid():
config = Configuration()
with patch.dict(os.environ, {"TEST_FLOAT": "invalid"}):
result = config._get_env_float("TEST_FLOAT", 1.0)
assert result == 1.0
def test_get_worker_property_value_task_specific(monkeypatch):
monkeypatch.setenv("CONDUCTOR_WORKER_MYTASK_POLLING_INTERVAL", "500")
config = Configuration()
result = config.get_worker_property_value("polling_interval", "mytask")
assert result == 500.0
def test_get_worker_property_value_global(monkeypatch):
monkeypatch.setenv("CONDUCTOR_WORKER_POLLING_INTERVAL", "600")
config = Configuration()
result = config.get_worker_property_value("polling_interval", "mytask")
assert result == 600.0
def test_get_worker_property_value_default():
config = Configuration()
result = config.get_worker_property_value("polling_interval", "mytask")
assert result == 100
def test_get_worker_property_value_domain():
config = Configuration()
result = config.get_worker_property_value("domain", "mytask")
assert result is None
def test_get_worker_property_value_poll_interval_seconds():
config = Configuration()
result = config.get_worker_property_value("poll_interval_seconds", "mytask")
assert result == 0
def test_convert_property_value_polling_interval():
config = Configuration()
result = config._convert_property_value("polling_interval", "250")
assert result == 250.0
def test_convert_property_value_polling_interval_invalid():
config = Configuration()
result = config._convert_property_value("polling_interval", "invalid")
assert result == 100
def test_convert_property_value_polling_interval_seconds():
config = Configuration()
result = config._convert_property_value("polling_interval_seconds", "5")
assert result == 5.0
def test_convert_property_value_polling_interval_seconds_invalid():
config = Configuration()
result = config._convert_property_value("polling_interval_seconds", "invalid")
assert result == 0
def test_convert_property_value_string():
config = Configuration()
result = config._convert_property_value("domain", "test_domain")
assert result == "test_domain"
def test_set_worker_property():
config = Configuration()
config.set_worker_property("mytask", "polling_interval", 300)
assert config._worker_properties["mytask"]["polling_interval"] == 300
def test_set_worker_property_multiple():
config = Configuration()
config.set_worker_property("mytask", "polling_interval", 300)
config.set_worker_property("mytask", "domain", "test_domain")
assert config._worker_properties["mytask"]["polling_interval"] == 300
assert config._worker_properties["mytask"]["domain"] == "test_domain"
def test_get_worker_property():
config = Configuration()
config.set_worker_property("mytask", "polling_interval", 300)
result = config.get_worker_property("mytask", "polling_interval")
assert result == 300
def test_get_worker_property_not_found():
config = Configuration()
result = config.get_worker_property("mytask", "polling_interval")
assert result is None
def test_get_polling_interval_with_task_type(monkeypatch):
monkeypatch.setenv("CONDUCTOR_WORKER_MYTASK_POLLING_INTERVAL", "400")
config = Configuration()
result = config.get_polling_interval("mytask")
assert result == 400.0
def test_get_polling_interval_default():
config = Configuration()
result = config.get_polling_interval("mytask")
assert result == 100.0
def test_get_domain_with_task_type(monkeypatch):
monkeypatch.setenv("CONDUCTOR_WORKER_MYTASK_DOMAIN", "task_domain")
config = Configuration()
result = config.get_domain("mytask")
assert result == "task_domain"
def test_get_domain_default():
config = Configuration()
result = config.get_domain("mytask")
assert result is None
def test_get_poll_interval_with_task_type(monkeypatch):
monkeypatch.setenv("CONDUCTOR_WORKER_MYTASK_POLLING_INTERVAL", "500")
config = Configuration()
result = config.get_poll_interval("mytask")
assert result == 500
def test_get_poll_interval_default():
config = Configuration()
result = config.get_poll_interval("mytask")
assert result == 100
def test_get_poll_interval_seconds():
config = Configuration()
result = config.get_poll_interval_seconds()
assert result == 0
def test_host_property():
config = Configuration(server_url="https://test.com/api")
assert config.host == "https://test.com/api"
def test_host_setter():
config = Configuration()
config.host = "https://new.com/api"
assert config.host == "https://new.com/api"
def test_debug_property():
config = Configuration(debug=True)
assert config.debug is True
def test_debug_setter():
config = Configuration()
config.debug = True
assert config.debug is True
def test_api_key_property():
config = Configuration()
config.api_key = {"test": "value"}
assert config.api_key == {"test": "value"}
def test_api_key_prefix_property():
config = Configuration()
config.api_key_prefix = {"test": "prefix"}
assert config.api_key_prefix == {"test": "prefix"}
def test_username_property():
config = Configuration()
config.username = "testuser"
assert config.username == "testuser"
def test_password_property():
config = Configuration()
config.password = "testpass"
assert config.password == "testpass"
def test_access_token_property():
config = Configuration()
config.access_token = "testtoken"
assert config.access_token == "testtoken"
def test_verify_ssl_property():
config = Configuration()
config.verify_ssl = False
assert config.verify_ssl is False
def test_ssl_ca_cert_property():
config = Configuration()
config.ssl_ca_cert = "/path/to/cert"
assert config.ssl_ca_cert == "/path/to/cert"
def test_retries_property():
config = Configuration()
config.retries = 5
assert config.retries == 5
def test_logger_format_property():
config = Configuration()
config.logger_format = "%(message)s"
assert config.logger_format == "%(message)s"
def test_log_level_property():
config = Configuration(debug=True)
assert config.log_level == logging.DEBUG
def test_apply_logging_config():
config = Configuration()
config.apply_logging_config()
assert config.is_logger_config_applied is True
def test_apply_logging_config_custom():
config = Configuration()
config.apply_logging_config(log_format="%(message)s", level=logging.ERROR)
assert config.is_logger_config_applied is True
def test_apply_logging_config_already_applied():
config = Configuration()
config.apply_logging_config()
config.apply_logging_config()
assert config.is_logger_config_applied is True
def test_get_logging_formatted_name():
result = Configuration.get_logging_formatted_name("test_logger")
assert result.startswith("[pid:")
assert result.endswith("] test_logger")
def test_ui_host_property():
config = Configuration(server_url="https://test.com/api")
assert config.ui_host == "https://test.com"
def test_getattr_delegation():
config = Configuration()
mock_config = MagicMock()
config._http_config = mock_config
mock_config.test_attr = "test_value"
result = config.test_attr
assert result == "test_value"
def test_getattr_no_http_config():
config = Configuration()
config._http_config = None
with pytest.raises(AttributeError):
_ = config.nonexistent_attr
def test_worker_properties_dict_initialization():
config = Configuration()
assert isinstance(config._worker_properties, dict)
assert len(config._worker_properties) == 0
def test_get_worker_property_value_unknown_property():
config = Configuration()
result = config.get_worker_property_value("unknown_property", "mytask")
assert result is None
def test_get_poll_interval_with_task_type_none_value():
config = Configuration()
with patch.dict(os.environ, {"CONDUCTOR_WORKER_MYTASK_POLLING_INTERVAL": "invalid"}):
result = config.get_poll_interval("mytask")
assert result == 100
def test_host_property_no_http_config():
config = Configuration()
config._http_config = None
config._host = "test_host"
assert config.host == "test_host"
def test_debug_setter_false():
config = Configuration(debug=True)
config.debug = False
assert config.debug is False
def test_proxy_initialization_from_parameter():
config = Configuration(proxy="http://proxy.example.com:8080")
assert config.proxy == "http://proxy.example.com:8080"
assert config._http_config.proxy == "http://proxy.example.com:8080"
def test_proxy_initialization_from_env_var(monkeypatch):
monkeypatch.setenv("CONDUCTOR_PROXY", "http://env-proxy.example.com:3128")
config = Configuration()
assert config.proxy == "http://env-proxy.example.com:3128"
assert config._http_config.proxy == "http://env-proxy.example.com:3128"
def test_proxy_parameter_overrides_env_var(monkeypatch):
monkeypatch.setenv("CONDUCTOR_PROXY", "http://env-proxy.example.com:3128")
config = Configuration(proxy="http://param-proxy.example.com:8080")
assert config.proxy == "http://param-proxy.example.com:8080"
assert config._http_config.proxy == "http://param-proxy.example.com:8080"
def test_proxy_headers_initialization_from_parameter():
headers = {"Authorization": "Bearer token123", "X-Custom": "value"}
config = Configuration(proxy_headers=headers)
assert config.proxy_headers == headers
assert config._http_config.proxy_headers == headers
def test_proxy_headers_initialization_from_env_var_json(monkeypatch):
headers_json = '{"Authorization": "Bearer token123", "X-Custom": "value"}'
monkeypatch.setenv("CONDUCTOR_PROXY_HEADERS", headers_json)
config = Configuration()
expected_headers = {"Authorization": "Bearer token123", "X-Custom": "value"}
assert config.proxy_headers == expected_headers
assert config._http_config.proxy_headers == expected_headers
def test_proxy_headers_initialization_from_env_var_single_header(monkeypatch):
monkeypatch.setenv("CONDUCTOR_PROXY_HEADERS", "Bearer single-token")
config = Configuration()
expected_headers = {"Authorization": "Bearer single-token"}
assert config.proxy_headers == expected_headers
assert config._http_config.proxy_headers == expected_headers
def test_proxy_headers_parameter_overrides_env_var(monkeypatch):
monkeypatch.setenv("CONDUCTOR_PROXY_HEADERS", '{"Authorization": "env-token"}')
param_headers = {"Authorization": "param-token", "X-Custom": "value"}
config = Configuration(proxy_headers=param_headers)
assert config.proxy_headers == param_headers
assert config._http_config.proxy_headers == param_headers
def test_proxy_headers_invalid_json_falls_back_to_single_header(monkeypatch):
monkeypatch.setenv("CONDUCTOR_PROXY_HEADERS", "invalid-json-string")
config = Configuration()
expected_headers = {"Authorization": "invalid-json-string"}
assert config.proxy_headers == expected_headers
assert config._http_config.proxy_headers == expected_headers
def test_proxy_headers_none_when_no_env_var():
config = Configuration()
assert config.proxy_headers is None
assert config._http_config.proxy_headers is None
def test_proxy_none_when_no_env_var():
config = Configuration()
assert config.proxy is None
assert config._http_config.proxy is None
def test_proxy_and_headers_together():
headers = {"Authorization": "Bearer token123"}
config = Configuration(
proxy="http://proxy.example.com:8080",
proxy_headers=headers
)
assert config.proxy == "http://proxy.example.com:8080"
assert config.proxy_headers == headers
assert config._http_config.proxy == "http://proxy.example.com:8080"
assert config._http_config.proxy_headers == headers
def test_proxy_headers_empty_json_parses_correctly(monkeypatch):
monkeypatch.setenv("CONDUCTOR_PROXY_HEADERS", "{}")
config = Configuration()
expected_headers = {}
assert config.proxy_headers == expected_headers
# Empty dict is falsy, so it's not set on HTTP config
assert config._http_config.proxy_headers is None
def test_proxy_headers_env_var_with_none_value(monkeypatch):
monkeypatch.setenv("CONDUCTOR_PROXY_HEADERS", "None")
config = Configuration()
expected_headers = {"Authorization": "None"}
assert config.proxy_headers == expected_headers
assert config._http_config.proxy_headers == expected_headers
def test_proxy_https_url():
config = Configuration(proxy="https://secure-proxy.example.com:8443")
assert config.proxy == "https://secure-proxy.example.com:8443"
assert config._http_config.proxy == "https://secure-proxy.example.com:8443"
def test_proxy_socks_url():
config = Configuration(proxy="socks5://socks-proxy.example.com:1080")
assert config.proxy == "socks5://socks-proxy.example.com:1080"
assert config._http_config.proxy == "socks5://socks-proxy.example.com:1080"
def test_proxy_headers_multiple_headers():
headers = {
"Authorization": "Bearer token123",
"X-API-Key": "api-key-456",
"User-Agent": "CustomAgent/1.0"
}
config = Configuration(proxy_headers=headers)
assert config.proxy_headers == headers
assert config._http_config.proxy_headers == headers
def test_proxy_headers_with_special_characters():
headers = {
"Authorization": "Bearer token with spaces",
"X-Custom-Header": "value-with-special-chars!@#$%"
}
config = Configuration(proxy_headers=headers)
assert config.proxy_headers == headers
assert config._http_config.proxy_headers == headers
def test_get_poll_interval_with_task_type_none():
config = Configuration()
result = config.get_poll_interval("mytask")
assert result == 100
def test_get_poll_interval_task_type_provided_but_value_none():
config = Configuration()
with patch.dict(os.environ, {"CONDUCTOR_WORKER_MYTASK_POLLING_INTERVAL": ""}):
result = config.get_poll_interval("mytask")
assert result == 100
def test_proxy_from_parameter():
proxy_url = "http://proxy.company.com:8080"
config = Configuration(proxy=proxy_url)
assert config.proxy == proxy_url
def test_proxy_from_env(monkeypatch):
proxy_url = "http://proxy.company.com:8080"
monkeypatch.setenv("CONDUCTOR_PROXY", proxy_url)
config = Configuration()
assert config.proxy == proxy_url