forked from lightspeed-core/lightspeed-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_10000_lines.py
More file actions
10000 lines (9540 loc) · 364 KB
/
Copy pathpython_10000_lines.py
File metadata and controls
10000 lines (9540 loc) · 364 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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Unit tests for functions defined in src/configuration.py."""
# pylint: disable=too-many-lines
from collections.abc import Generator
from pathlib import Path
from typing import Any
import pytest
from pydantic import ValidationError
import constants
from cache.in_memory_cache import InMemoryCache
from cache.sqlite_cache import SQLiteCache
from configuration import AppConfig, LogicError
from models.config import CustomProfile, ModelContextProtocolServer
from utils.checks import InvalidConfigurationError
# pylint: disable=broad-exception-caught,protected-access
@pytest.fixture(autouse=True)
def _reset_app_config_between_tests() -> Generator:
# ensure clean state before each test
"""
Reset AppConfig singleton internal state before and after a test to avoid test contamination.
Attempts to set AppConfig()._configuration to None and
AppConfig()._quota_limiters to an empty list, ignoring any exceptions, then
yields control to the test and repeats the cleanup after the test.
"""
try:
AppConfig()._configuration = None # type: ignore[attr-defined]
AppConfig()._quota_limiters = [] # type: ignore[attr-defined]
except Exception:
pass
yield
# ensure clean state after each test
try:
AppConfig()._configuration = None # type: ignore[attr-defined]
AppConfig()._quota_limiters = [] # type: ignore[attr-defined]
except Exception:
pass
def test_default_configuration() -> None:
"""Test that configuration attributes are not accessible for uninitialized app."""
cfg = AppConfig()
assert cfg is not None
# configuration is not loaded
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
# try to read property
_ = cfg.configuration # pylint: disable=pointless-statement
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
# try to read property
_ = cfg.service_configuration # pylint: disable=pointless-statement
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
# try to read property
_ = cfg.llama_stack_configuration # pylint: disable=pointless-statement
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
# try to read property
_ = (
cfg.user_data_collection_configuration
) # pylint: disable=pointless-statement
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
# try to read property
_ = cfg.mcp_servers # pylint: disable=pointless-statement
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
# try to read property
_ = cfg.token_usage_history # pylint: disable=pointless-statement
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
# try to read property
_ = cfg.azure_entra_id # pylint: disable=pointless-statement
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
# try to read property
_ = cfg.splunk # pylint: disable=pointless-statement
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
# try to read property
_ = cfg.deployment_environment # pylint: disable=pointless-statement
def test_configuration_is_singleton() -> None:
"""Test that configuration is singleton."""
cfg1 = AppConfig()
cfg2 = AppConfig()
assert cfg1 == cfg2
def test_init_from_dict() -> None:
"""Test the configuration initialization from dictionary with config values."""
config_dict: dict[str, Any] = {
"name": "foo",
"service": {
"host": "localhost",
"port": 8080,
"auth_enabled": False,
},
"llama_stack": {
"api_key": "xyzzy",
"url": "http://x.y.com:1234",
"use_as_library_client": False,
},
"user_data_collection": {
"feedback_enabled": False,
},
"mcp_servers": [],
"customization": None,
"authentication": {
"module": "noop",
},
"a2a_state": {
"sqlite": None,
"postgres": None,
},
"splunk": {
"enabled": False,
"url": "foo.bar.baz",
"index": "index",
"source": "source",
"timeout": 10,
"verify_ssl": False,
},
"deployment_environment": "foo",
}
cfg = AppConfig()
cfg.init_from_dict(config_dict)
# check for all subsections
assert cfg.configuration is not None
assert cfg.llama_stack_configuration is not None
assert cfg.service_configuration is not None
assert cfg.user_data_collection_configuration is not None
# check for configuration subsection
assert cfg.configuration.name == "foo"
# check for llama_stack_configuration subsection
assert cfg.llama_stack_configuration.api_key is not None
assert cfg.llama_stack_configuration.api_key.get_secret_value() == "xyzzy"
assert str(cfg.llama_stack_configuration.url) == "http://x.y.com:1234/"
assert cfg.llama_stack_configuration.use_as_library_client is False
# check for service_configuration subsection
assert cfg.service_configuration.host == "localhost"
assert cfg.service_configuration.port == 8080
assert cfg.service_configuration.auth_enabled is False
assert cfg.service_configuration.workers == 1
assert cfg.service_configuration.color_log is True
assert cfg.service_configuration.access_log is True
# check for user data collection subsection
assert cfg.user_data_collection_configuration.feedback_enabled is False
# check authentication_configuration
assert cfg.authentication_configuration is not None
assert cfg.authentication_configuration.module == "noop"
# check authorization configuration - default value
assert cfg.authorization_configuration is not None
# check database configuration
assert cfg.database_configuration is not None
# check inference configuration
assert cfg.inference is not None
# check conversation cache
assert cfg.conversation_cache_configuration is not None
# check a2a state
assert cfg.a2a_state is not None
assert cfg.a2a_state.sqlite is None
assert cfg.a2a_state.postgres is None
# check Splunk
assert cfg.splunk is not None
assert cfg.splunk.enabled is False
assert cfg.splunk.url == "foo.bar.baz"
assert cfg.splunk.index == "index"
assert cfg.splunk.source == "source"
assert cfg.splunk.timeout == 10
assert cfg.splunk.verify_ssl is False
# check deployment_environment
assert cfg.deployment_environment is not None
# check token usage history
assert cfg.token_usage_history is None
def test_init_from_dict_with_mcp_servers() -> None:
"""Test initialization with MCP servers configuration."""
config_dict = {
"name": "foo",
"service": {
"host": "localhost",
"port": 8080,
"auth_enabled": False,
"workers": 1,
"color_log": True,
"access_log": True,
},
"llama_stack": {
"api_key": "xyzzy",
"url": "http://x.y.com:1234",
"use_as_library_client": False,
},
"user_data_collection": {
"feedback_enabled": False,
},
"mcp_servers": [
{
"name": "server1",
"url": "http://localhost:8080",
},
{
"name": "server2",
"provider_id": "custom-provider",
"url": "https://api.example.com",
},
],
"customization": None,
}
cfg = AppConfig()
cfg.init_from_dict(config_dict)
assert len(cfg.mcp_servers) == 2
assert cfg.mcp_servers[0].name == "server1"
assert cfg.mcp_servers[0].provider_id == "model-context-protocol"
assert cfg.mcp_servers[0].url == "http://localhost:8080"
assert cfg.mcp_servers[1].name == "server2"
assert cfg.mcp_servers[1].provider_id == "custom-provider"
assert cfg.mcp_servers[1].url == "https://api.example.com"
def test_init_from_dict_with_authorization_configuration() -> None:
"""Test initialization with authorization configuration configuration.
Verify AppConfig initializes authorization configuration when an empty
`authorization` block is provided.
Initializes the singleton AppConfig from a dict that includes an empty
`authorization` section and asserts that `authorization_configuration` is
not None.
"""
config_dict = {
"name": "foo",
"service": {
"host": "localhost",
"port": 8080,
"auth_enabled": False,
"workers": 1,
"color_log": True,
"access_log": True,
},
"llama_stack": {
"api_key": "xyzzy",
"url": "http://x.y.com:1234",
"use_as_library_client": False,
},
"user_data_collection": {
"feedback_enabled": False,
},
"authorization": {},
"customization": None,
}
cfg = AppConfig()
cfg.init_from_dict(config_dict)
assert cfg.authorization_configuration is not None
def test_load_proper_configuration(tmpdir: Path) -> None:
"""Test loading proper configuration from YAML file.
Verify that a valid YAML configuration file loads and populates key AppConfig sections.
Writes a YAML configuration to a temporary file, loads it with
AppConfig.load_configuration, and asserts that `configuration`,
`llama_stack_configuration`, `service_configuration`, and
`user_data_collection_configuration` are populated.
"""
cfg_filename = tmpdir / "config.yaml"
with open(cfg_filename, "w", encoding="utf-8") as fout:
fout.write("""
name: foo bar baz
service:
host: localhost
port: 8080
auth_enabled: false
workers: 1
color_log: true
access_log: true
llama_stack:
use_as_library_client: false
url: http://localhost:8321
api_key: xyzzy
user_data_collection:
feedback_enabled: false
mcp_servers: []
""")
cfg = AppConfig()
cfg.load_configuration(str(cfg_filename))
assert cfg.configuration is not None
assert cfg.llama_stack_configuration is not None
assert cfg.service_configuration is not None
assert cfg.user_data_collection_configuration is not None
def test_load_configuration_with_mcp_servers(tmpdir: Path) -> None:
"""Test loading configuration from YAML file with MCP servers."""
cfg_filename = tmpdir / "config.yaml"
with open(cfg_filename, "w", encoding="utf-8") as fout:
fout.write("""
name: test service
service:
host: localhost
port: 8080
auth_enabled: false
workers: 1
color_log: true
access_log: true
llama_stack:
use_as_library_client: false
url: http://localhost:8321
api_key: test-key
user_data_collection:
feedback_enabled: false
mcp_servers:
- name: filesystem-server
url: http://localhost:3000
- name: git-server
provider_id: custom-git-provider
url: https://git.example.com/mcp
""")
cfg = AppConfig()
cfg.load_configuration(str(cfg_filename))
assert len(cfg.mcp_servers) == 2
assert cfg.mcp_servers[0].name == "filesystem-server"
assert cfg.mcp_servers[0].provider_id == "model-context-protocol"
assert cfg.mcp_servers[0].url == "http://localhost:3000"
assert cfg.mcp_servers[1].name == "git-server"
assert cfg.mcp_servers[1].provider_id == "custom-git-provider"
assert cfg.mcp_servers[1].url == "https://git.example.com/mcp"
def test_mcp_servers_property_empty() -> None:
"""Test mcp_servers property returns empty list when no servers configured."""
config_dict: dict[str, Any] = {
"name": "test",
"service": {
"host": "localhost",
"port": 8080,
"auth_enabled": False,
"workers": 1,
"color_log": True,
"access_log": True,
},
"llama_stack": {
"api_key": "test-key",
"url": "http://localhost:8321",
"use_as_library_client": False,
},
"user_data_collection": {
"feedback_enabled": False,
},
"mcp_servers": [],
"customization": None,
}
cfg = AppConfig()
cfg.init_from_dict(config_dict)
servers = cfg.mcp_servers
assert isinstance(servers, list)
assert len(servers) == 0
def test_mcp_servers_property_with_servers() -> None:
"""Test mcp_servers property returns correct list of ModelContextProtocolServer objects."""
config_dict = {
"name": "test",
"service": {
"host": "localhost",
"port": 8080,
"auth_enabled": False,
"workers": 1,
"color_log": True,
"access_log": True,
},
"llama_stack": {
"api_key": "test-key",
"url": "http://localhost:8321",
"use_as_library_client": False,
},
"user_data_collection": {
"feedback_enabled": False,
},
"mcp_servers": [
{
"name": "test-server",
"url": "http://localhost:8080",
},
],
"customization": None,
}
cfg = AppConfig()
cfg.init_from_dict(config_dict)
servers = cfg.mcp_servers
assert isinstance(servers, list)
assert len(servers) == 1
assert isinstance(servers[0], ModelContextProtocolServer)
assert servers[0].name == "test-server"
assert servers[0].url == "http://localhost:8080"
def test_configuration_not_loaded() -> None:
"""Test that accessing configuration before loading raises an error."""
cfg = AppConfig()
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
c = cfg.configuration
assert c is not None
def test_service_configuration_not_loaded() -> None:
"""Test that accessing service_configuration before loading raises an error."""
cfg = AppConfig()
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
c = cfg.service_configuration
assert c is not None
def test_llama_stack_configuration_not_loaded() -> None:
"""Test that accessing llama_stack_configuration before loading raises an error."""
cfg = AppConfig()
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
c = cfg.llama_stack_configuration
assert c is not None
def test_user_data_collection_configuration_not_loaded() -> None:
"""Test that accessing user_data_collection_configuration before loading raises an error."""
cfg = AppConfig()
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
c = cfg.user_data_collection_configuration
assert c is not None
def test_mcp_servers_not_loaded() -> None:
"""Test that accessing mcp_servers before loading raises an error."""
cfg = AppConfig()
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
c = cfg.mcp_servers
assert c is not None
def test_authentication_configuration_not_loaded() -> None:
"""Test that accessing authentication_configuration before loading raises an error."""
cfg = AppConfig()
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
c = cfg.authentication_configuration
assert c is not None
def test_customization_not_loaded() -> None:
"""Test that accessing customization before loading raises an error."""
cfg = AppConfig()
with pytest.raises(LogicError, match="logic error: configuration is not loaded"):
c = cfg.customization
assert c is not None
def test_load_configuration_with_customization_system_prompt_path(tmpdir: Path) -> None:
"""Test loading configuration from YAML file with customization."""
system_prompt_filename = tmpdir / "system_prompt.txt"
with open(system_prompt_filename, "w", encoding="utf-8") as fout:
fout.write("this is system prompt")
cfg_filename = tmpdir / "config.yaml"
with open(cfg_filename, "w", encoding="utf-8") as fout:
fout.write(f"""
name: test service
service:
host: localhost
port: 8080
auth_enabled: false
workers: 1
color_log: true
access_log: true
llama_stack:
use_as_library_client: false
url: http://localhost:8321
api_key: test-key
user_data_collection:
feedback_enabled: false
mcp_servers:
- name: filesystem-server
url: http://localhost:3000
- name: git-server
provider_id: custom-git-provider
url: https://git.example.com/mcp
customization:
disable_query_system_prompt: true
system_prompt_path: {system_prompt_filename}
""")
cfg = AppConfig()
cfg.load_configuration(str(cfg_filename))
assert cfg.customization is not None
assert cfg.customization.system_prompt is not None
assert cfg.customization.system_prompt == "this is system prompt"
def test_load_configuration_with_customization_system_prompt(tmpdir: Path) -> None:
"""Test loading configuration from YAML file with system_prompt in the customization."""
cfg_filename = tmpdir / "config.yaml"
with open(cfg_filename, "w", encoding="utf-8") as fout:
fout.write("""
name: test service
service:
host: localhost
port: 8080
auth_enabled: false
workers: 1
color_log: true
access_log: true
llama_stack:
use_as_library_client: false
url: http://localhost:8321
api_key: test-key
user_data_collection:
feedback_enabled: false
mcp_servers:
- name: filesystem-server
url: http://localhost:3000
- name: git-server
provider_id: custom-git-provider
url: https://git.example.com/mcp
customization:
system_prompt: |-
this is system prompt in the customization section
""")
cfg = AppConfig()
cfg.load_configuration(str(cfg_filename))
assert cfg.customization is not None
assert cfg.customization.system_prompt is not None
assert (
cfg.customization.system_prompt.strip()
== "this is system prompt in the customization section"
)
def test_configuration_with_profile_customization(tmpdir: Path) -> None:
"""Test loading configuration from YAML file with a custom profile."""
expected_profile = CustomProfile(path="tests/profiles/test/profile.py")
expected_prompts = expected_profile.get_prompts()
cfg_filename = tmpdir / "config.yaml"
with open(cfg_filename, "w", encoding="utf-8") as fout:
fout.write("""
name: test service
service:
host: localhost
port: 8080
auth_enabled: false
workers: 1
color_log: true
access_log: true
llama_stack:
use_as_library_client: false
url: http://localhost:8321
api_key: test-key
user_data_collection:
feedback_enabled: false
customization:
profile_path: tests/profiles/test/profile.py
""")
cfg = AppConfig()
cfg.load_configuration(str(cfg_filename))
assert (
cfg.customization is not None and cfg.customization.custom_profile is not None
)
fetched_prompts = cfg.customization.custom_profile.get_prompts()
assert fetched_prompts is not None and fetched_prompts.get(
"default"
) == expected_prompts.get("default")
def test_configuration_with_all_customizations(tmpdir: Path) -> None:
"""Test loading configuration from YAML file with a custom profile, prompt and prompt path."""
expected_profile = CustomProfile(path="tests/profiles/test/profile.py")
expected_prompts = expected_profile.get_prompts()
system_prompt_filename = tmpdir / "system_prompt.txt"
with open(system_prompt_filename, "w", encoding="utf-8") as fout:
fout.write("this is system prompt")
cfg_filename = tmpdir / "config.yaml"
with open(cfg_filename, "w", encoding="utf-8") as fout:
fout.write(f"""
name: test service
service:
host: localhost
port: 8080
auth_enabled: false
workers: 1
color_log: true
access_log: true
llama_stack:
use_as_library_client: false
url: http://localhost:8321
api_key: test-key
user_data_collection:
feedback_enabled: false
customization:
profile_path: tests/profiles/test/profile.py
system_prompt: custom prompt
system_prompt_path: {system_prompt_filename}
""")
cfg = AppConfig()
cfg.load_configuration(str(cfg_filename))
assert (
cfg.customization is not None and cfg.customization.custom_profile is not None
)
fetched_prompts = cfg.customization.custom_profile.get_prompts()
assert fetched_prompts is not None and fetched_prompts.get(
"default"
) == expected_prompts.get("default")
def test_configuration_with_sqlite_conversation_cache(tmpdir: Path) -> None:
"""Test loading configuration from YAML file with conversation cache configuration."""
cfg_filename = tmpdir / "config.yaml"
with open(cfg_filename, "w", encoding="utf-8") as fout:
fout.write("""
name: test service
service:
host: localhost
port: 8080
auth_enabled: false
workers: 1
color_log: true
access_log: true
llama_stack:
use_as_library_client: false
url: http://localhost:8321
api_key: test-key
user_data_collection:
feedback_enabled: false
conversation_cache:
type: "sqlite"
sqlite:
db_path: ":memory:"
""")
cfg = AppConfig()
cfg.load_configuration(str(cfg_filename))
assert cfg.conversation_cache_configuration is not None
assert cfg.conversation_cache_configuration.type == "sqlite"
assert cfg.conversation_cache_configuration.sqlite is not None
assert cfg.conversation_cache_configuration.postgres is None
assert cfg.conversation_cache_configuration.memory is None
assert cfg.conversation_cache is not None
assert isinstance(cfg.conversation_cache, SQLiteCache)
def test_configuration_with_in_memory_conversation_cache(tmpdir: Path) -> None:
"""Test loading configuration from YAML file with conversation cache configuration."""
cfg_filename = tmpdir / "config.yaml"
with open(cfg_filename, "w", encoding="utf-8") as fout:
fout.write("""
name: test service
service:
host: localhost
port: 8080
auth_enabled: false
workers: 1
color_log: true
access_log: true
llama_stack:
use_as_library_client: false
url: http://localhost:8321
api_key: test-key
user_data_collection:
feedback_enabled: false
conversation_cache:
type: "memory"
memory:
max_entries: 42
""")
cfg = AppConfig()
cfg.load_configuration(str(cfg_filename))
assert cfg.conversation_cache_configuration is not None
assert cfg.conversation_cache_configuration.type == "memory"
assert cfg.conversation_cache_configuration.sqlite is None
assert cfg.conversation_cache_configuration.postgres is None
assert cfg.conversation_cache_configuration.memory is not None
assert cfg.conversation_cache is not None
assert isinstance(cfg.conversation_cache, InMemoryCache)
def test_configuration_with_quota_handlers_no_storage(tmpdir: Path) -> None:
"""Test loading configuration from YAML file with quota handlers configuration."""
cfg_filename = tmpdir / "config.yaml"
with open(cfg_filename, "w", encoding="utf-8") as fout:
fout.write("""
name: test service
service:
host: localhost
port: 8080
auth_enabled: false
workers: 1
color_log: true
access_log: true
llama_stack:
use_as_library_client: false
url: http://localhost:8321
api_key: test-key
user_data_collection:
feedback_enabled: false
quota_handlers:
limiters:
- name: user_monthly_limits
type: user_limiter
initial_quota: 10
quota_increase: 10
period: "2 seconds"
- name: cluster_monthly_limits
type: cluster_limiter
initial_quota: 100
quota_increase: 10
period: "10 seconds"
scheduler:
# scheduler ticks in seconds
period: 1
""")
cfg = AppConfig()
cfg.load_configuration(str(cfg_filename))
assert cfg.quota_handlers_configuration is not None
assert cfg.quota_handlers_configuration.sqlite is None
assert cfg.quota_handlers_configuration.postgres is None
assert cfg.quota_handlers_configuration.limiters is not None
assert cfg.quota_handlers_configuration.scheduler is not None
# check the quota limiters configuration
assert len(cfg.quota_limiters) == 0
# check the scheduler configuration
assert cfg.quota_handlers_configuration.scheduler.period == 1
def test_configuration_with_token_history_no_storage(tmpdir: Path) -> None:
"""Test loading configuration from YAML file with quota handlers configuration."""
cfg_filename = tmpdir / "config.yaml"
with open(cfg_filename, "w", encoding="utf-8") as fout:
fout.write("""
name: test service
service:
host: localhost
port: 8080
auth_enabled: false
workers: 1
color_log: true
access_log: true
llama_stack:
use_as_library_client: false
url: http://localhost:8321
api_key: test-key
user_data_collection:
feedback_enabled: false
quota_handlers:
scheduler:
# scheduler ticks in seconds
period: 1
enable_token_history: true
""")
cfg = AppConfig()
cfg.load_configuration(str(cfg_filename))
assert cfg.quota_handlers_configuration is not None
assert cfg.quota_handlers_configuration.sqlite is None
assert cfg.quota_handlers_configuration.postgres is None
assert cfg.quota_handlers_configuration.scheduler is not None
# check the token usage history
assert cfg.token_usage_history is not None
def test_configuration_with_quota_handlers(tmpdir: Path) -> None:
"""Test loading configuration from YAML file with quota handlers configuration."""
cfg_filename = tmpdir / "config.yaml"
with open(cfg_filename, "w", encoding="utf-8") as fout:
fout.write("""
name: test service
service:
host: localhost
port: 8080
auth_enabled: false
workers: 1
color_log: true
access_log: true
llama_stack:
use_as_library_client: false
url: http://localhost:8321
api_key: test-key
user_data_collection:
feedback_enabled: false
quota_handlers:
sqlite:
db_path: ":memory:"
limiters:
- name: user_monthly_limits
type: user_limiter
initial_quota: 10
quota_increase: 10
period: "2 seconds"
- name: cluster_monthly_limits
type: cluster_limiter
initial_quota: 100
quota_increase: 10
period: "10 seconds"
scheduler:
# scheduler ticks in seconds
period: 1
""")
cfg = AppConfig()
cfg.load_configuration(str(cfg_filename))
assert cfg.quota_handlers_configuration is not None
assert cfg.quota_handlers_configuration.sqlite is not None
assert cfg.quota_handlers_configuration.postgres is None
assert cfg.quota_handlers_configuration.limiters is not None
assert cfg.quota_handlers_configuration.scheduler is not None
# check the storage
assert cfg.quota_handlers_configuration.sqlite.db_path == ":memory:"
# check the quota limiters configuration
assert len(cfg.quota_limiters) == 2
assert (
str(cfg.quota_limiters[0])
== "UserQuotaLimiter: initial quota: 10 increase by: 10"
)
assert (
str(cfg.quota_limiters[1])
== "ClusterQuotaLimiter: initial quota: 100 increase by: 10"
)
# check the scheduler configuration
assert cfg.quota_handlers_configuration.scheduler.period == 1
def test_load_configuration_with_azure_entra_id(tmpdir: Path) -> None:
"""Return Azure Entra ID configuration when provided in configuration."""
cfg_filename = tmpdir / "config.yaml"
with open(cfg_filename, "w", encoding="utf-8") as fout:
fout.write("""
name: test service
service:
host: localhost
port: 8080
auth_enabled: false
workers: 1
color_log: true
access_log: true
llama_stack:
api_key: test-key
url: http://localhost:8321
use_as_library_client: false
user_data_collection:
feedback_enabled: false
azure_entra_id:
tenant_id: tenant
client_id: client
client_secret: secret
""")
cfg = AppConfig()
cfg.load_configuration(str(cfg_filename))
azure_conf = cfg.azure_entra_id
assert azure_conf is not None
assert azure_conf.tenant_id.get_secret_value() == "tenant"
assert azure_conf.client_id.get_secret_value() == "client"
assert azure_conf.client_secret.get_secret_value() == "secret"
def test_load_configuration_with_incomplete_azure_entra_id_raises(tmpdir: Path) -> None:
"""Raise error if Azure Entra ID block is incomplete in configuration."""
cfg_filename = tmpdir / "config.yaml"
with open(cfg_filename, "w", encoding="utf-8") as fout:
fout.write("""
name: test service
service:
host: localhost
port: 8080
auth_enabled: false
workers: 1
color_log: true
access_log: true
llama_stack:
api_key: test-key
url: http://localhost:8321
use_as_library_client: false
user_data_collection:
feedback_enabled: false
azure_entra_id:
tenant_id: tenant
client_id: client
""")
cfg = AppConfig()
with pytest.raises(ValidationError):
cfg.load_configuration(str(cfg_filename))
def test_rag_id_mapping_excludes_solr_when_okp_not_configured(
minimal_config: AppConfig,
) -> None:
"""Test that rag_id_mapping does not include OKP/Solr when OKP is not in rag config."""
assert minimal_config.rag_id_mapping == {}
def test_rag_id_mapping_includes_solr_when_okp_in_inline() -> None:
"""Test that rag_id_mapping includes OKP/Solr mapping when OKP is in rag.inline."""
cfg = AppConfig()
cfg.init_from_dict(
{
"name": "test",
"service": {"host": "localhost", "port": 8080},
"llama_stack": {
"api_key": "k",
"url": "http://test.com:1234",
"use_as_library_client": False,
},
"user_data_collection": {},
"authentication": {"module": "noop"},
"rag": {"inline": [constants.OKP_RAG_ID]},
}
)
assert constants.SOLR_DEFAULT_VECTOR_STORE_ID in cfg.rag_id_mapping
assert (
cfg.rag_id_mapping[constants.SOLR_DEFAULT_VECTOR_STORE_ID]
== constants.OKP_RAG_ID
)
def test_rag_id_mapping_includes_solr_when_okp_in_tool() -> None:
"""Test that rag_id_mapping includes OKP/Solr mapping when OKP is in rag.tool."""
cfg = AppConfig()
cfg.init_from_dict(
{
"name": "test",
"service": {"host": "localhost", "port": 8080},
"llama_stack": {
"api_key": "k",
"url": "http://test.com:1234",
"use_as_library_client": False,
},
"user_data_collection": {},
"authentication": {"module": "noop"},
"rag": {"tool": [constants.OKP_RAG_ID]},
}
)
assert constants.SOLR_DEFAULT_VECTOR_STORE_ID in cfg.rag_id_mapping
assert (
cfg.rag_id_mapping[constants.SOLR_DEFAULT_VECTOR_STORE_ID]
== constants.OKP_RAG_ID
)
def test_rag_id_mapping_with_byok(tmp_path: Path) -> None:
"""Test that rag_id_mapping builds correct mapping from BYOK config."""
db_file = tmp_path / "test.db"
db_file.touch()
cfg = AppConfig()
cfg.init_from_dict(