-
Notifications
You must be signed in to change notification settings - Fork 949
Expand file tree
/
Copy pathtest_config.py
More file actions
1196 lines (981 loc) · 36.4 KB
/
test_config.py
File metadata and controls
1196 lines (981 loc) · 36.4 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
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
import importlib
import json
import os
from pathlib import Path
import pytest
import re
import shutil
import sys
import tempfile
from pytest_httpserver import HTTPServer
from garak import _config
import garak.cli
SITE_YAML_FILENAME = "TESTONLY.site.yaml.bak"
CONFIGURABLE_YAML = """
plugins:
generators:
huggingface:
hf_args:
torch_dtype: float16
Pipeline:
hf_args:
device: cuda
probes:
test:
generators:
huggingface:
Pipeline:
hf_args:
torch_dtype: float16
detector:
test:
val: tests
Blank:
generators:
huggingface:
hf_args:
torch_dtype: float16
device: cuda:1
Pipeline:
dtype: for_detector
buffs:
test:
Blank:
generators:
huggingface:
hf_args:
device: cuda:0
Pipeline:
dtype: for_detector
""".encode(
"utf-8"
)
ANSI_ESCAPE = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
XDG_VARS = ("XDG_DATA_HOME", "XDG_CONFIG_HOME", "XDG_CACHE_HOME")
OPTIONS_SOLO = [
# "verbose", # not sure hot to test argparse action="count"
# "deprefix", # this param is weird
"narrow_output",
"extended_detectors",
]
OPTIONS_PARAM = [
("report_prefix", "laurelhurst"),
("parallel_requests", 9),
("parallel_attempts", 9),
("seed", 9001),
("eval_threshold", 0.9),
("generations", 9),
# ("config", "obsidian.yaml"), # optional config file names passed via CLI don't get stored in _config. that'll suck to troubleshoot
("target_type", "test"),
("target_name", "bruce"),
]
OPTIONS_SPEC = [
("probes", "3,elim,gul.dukat", "probe_spec"),
("detectors", "all", "detector_spec"),
("buffs", "polymorph", "buff_spec"),
]
param_locs = {}
for p in _config.system_params:
param_locs[p] = "system"
for p in _config.run_params:
param_locs[p] = "run"
for p in _config.plugins_params:
param_locs[p] = "plugins"
for p in _config.reporting_params:
param_locs[p] = "reporting"
@pytest.fixture
def allow_site_config(request):
site_cfg_moved = False
try:
shutil.move(
_config.transient.config_dir / "garak.site.yaml", SITE_YAML_FILENAME
)
site_cfg_moved = True
except FileNotFoundError:
site_cfg_moved = False
def restore_site_config():
if site_cfg_moved:
shutil.move(
SITE_YAML_FILENAME, _config.transient.config_dir / "garak.site.yaml"
)
elif os.path.exists(_config.transient.config_dir / "garak.site.yaml"):
os.remove(_config.transient.config_dir / "garak.site.yaml")
request.addfinalizer(restore_site_config)
@pytest.fixture
def override_xdg_env(request):
restore_vars = {}
with tempfile.TemporaryDirectory() as tmpdir:
for env_var in XDG_VARS:
current_val = os.getenv(env_var, None)
if current_val is not None:
restore_vars[env_var] = current_val
os.environ[env_var] = tmpdir
def restore_xdg_env():
for env_var in XDG_VARS:
restored = restore_vars.get(env_var)
if restored is not None:
os.environ[env_var] = restored
else:
del os.environ[env_var]
request.addfinalizer(restore_xdg_env)
return tmpdir
@pytest.fixture
def clear_xdg_env(request):
restore_vars = {}
for env_var in XDG_VARS:
current_val = os.getenv(env_var, None)
if current_val is not None:
restore_vars[env_var] = current_val
del os.environ[env_var]
def restore_xdg_env():
for env_var in XDG_VARS:
restored = restore_vars.get(env_var)
if restored is not None:
os.environ[env_var] = restored
else:
try:
del os.environ[env_var]
except KeyError as e:
pass
request.addfinalizer(restore_xdg_env)
@pytest.fixture
def temp_package_dir(request):
original_package_dir = _config.transient.package_dir
tmpdir = tempfile.mkdtemp()
configs_dir = Path(tmpdir) / "configs"
configs_dir.mkdir()
# Copy resources directory so garak.core.yaml is available
src_resources = original_package_dir / "resources"
dst_resources = Path(tmpdir) / "resources"
shutil.copytree(src_resources, dst_resources)
_config.transient.package_dir = Path(tmpdir)
def restore_package_dir():
_config.transient.package_dir = original_package_dir
shutil.rmtree(tmpdir, ignore_errors=True)
request.addfinalizer(restore_package_dir)
return Path(tmpdir)
# environment variables adjust transient values
def test_xdg_support(override_xdg_env):
test_path = Path(override_xdg_env)
importlib.reload(_config)
assert _config.transient.cache_dir == test_path / _config.project_dir_name
assert _config.transient.config_dir == test_path / _config.project_dir_name
assert _config.transient.data_dir == test_path / _config.project_dir_name
@pytest.mark.usefixtures("clear_xdg_env")
def test_xdg_defaults():
if "HOME" in os.environ:
test_path = Path(os.environ["HOME"])
elif sys.platform == "win32" and "USERPROFILE" in os.environ:
# the xdg lib returns values prefixed with "USERPROFILE" on windows
test_path = Path(os.environ["USERPROFILE"])
importlib.reload(_config)
assert (
_config.transient.cache_dir == test_path / ".cache" / _config.project_dir_name
)
assert (
_config.transient.config_dir == test_path / ".config" / _config.project_dir_name
)
assert (
_config.transient.data_dir
== test_path / ".local" / "share" / _config.project_dir_name
)
# test CLI assertions of each var
@pytest.mark.parametrize("option", OPTIONS_SOLO)
def test_cli_solo_settings(option):
garak.cli.main(
[f"--{option}", "--list_config"]
) # add list_config as the action so we don't actually run
subconfig = getattr(_config, param_locs[option])
assert getattr(subconfig, option) == True
@pytest.mark.parametrize("param", OPTIONS_PARAM)
def test_cli_param_settings(param):
option, value = param
garak.cli.main(
[f"--{option}", str(value), "--list_config"]
) # add list_config as the action so we don't actually run
subconfig = getattr(_config, param_locs[option])
assert getattr(subconfig, option) == value
@pytest.mark.parametrize("param", OPTIONS_SPEC)
def test_cli_spec_settings(param):
option, value, configname = param
garak.cli.main(
[f"--{option}", str(value), "--list_config"]
) # add list_config as the action so we don't actually run
assert getattr(_config.plugins, configname) == value
# test a short-form CLI assertion
def test_cli_shortform():
garak.cli.main(["-s", "444", "--list_config"])
assert _config.run.seed == 444
garak.cli.main(
["-g", "444", "--list_config"]
) # seed gets special treatment, try another
assert _config.run.generations == 444
# test that run YAML overrides core YAML
@pytest.mark.parametrize("param", OPTIONS_PARAM)
def test_yaml_param_settings(param):
option, value = param
with tempfile.NamedTemporaryFile(buffering=0, delete=False, suffix=".yaml") as tmp:
file_data = [
f"---",
f"{param_locs[option]}:",
f" {option}: {value}",
]
tmp.write("\n".join(file_data).encode("utf-8"))
tmp.close()
garak.cli.main(
["--config", tmp.name, "--list_config"]
) # add list_config as the action so we don't actually run
subconfig = getattr(_config, param_locs[option])
os.remove(tmp.name)
assert (
getattr(subconfig, option) == value
), f"CLI-supplied config values for {option} should override core config"
# # test that site YAML overrides core YAML # needs file staging for site yaml
@pytest.mark.usefixtures("allow_site_config")
def test_site_yaml_overrides_core_yaml():
with open(
_config.transient.config_dir / "garak.site.yaml", "w", encoding="utf-8"
) as f:
f.write("---\nrun:\n eval_threshold: 0.777\n")
f.flush()
garak.cli.main(["--list_config"])
assert (
_config.run.eval_threshold == 0.777
), "Site config should override core config if loaded correctly"
# # test that run YAML overrides site YAML # needs file staging for site yaml
@pytest.mark.usefixtures("allow_site_config")
def test_run_yaml_overrides_site_yaml():
with open(
_config.transient.config_dir / "garak.site.yaml", "w", encoding="utf-8"
) as f:
file_data = [
"---",
"run:",
" eval_threshold: 0.777",
]
f.write("\n".join(file_data))
f.flush()
garak.cli.main(["--list_config", "--eval_threshold", str(0.9001)])
assert (
_config.run.eval_threshold == 0.9001
), "CLI-specified config values should override site config"
# test that CLI config overrides run YAML
def test_cli_overrides_run_yaml():
orig_seed = 10101
override_seed = 37176
with tempfile.NamedTemporaryFile(buffering=0, delete=False, suffix=".yaml") as tmp:
file_data = [
f"---",
f"run:",
f" seed: {orig_seed}",
]
tmp.write("\n".join(file_data).encode("utf-8"))
tmp.close()
garak.cli.main(
["--config", tmp.name, "-s", f"{override_seed}", "--list_config"]
) # add list_config as the action so we don't actually run
os.remove(tmp.name)
assert (
_config.run.seed == override_seed
), "CLI-specificd config values should override values in config file names on CLI"
# test probe_options YAML
# more refactor for namespace keys
def test_probe_options_yaml(capsys):
with tempfile.NamedTemporaryFile(buffering=0, delete=False, suffix=".yaml") as tmp:
tmp.write(
"\n".join(
[
"---",
"plugins:",
" probe_spec: test.Blank",
" probes:",
" test:",
" Blank:",
" gen_x: 37176",
]
).encode("utf-8")
)
tmp.close()
garak.cli.main(
["--config", tmp.name, "--list_config"]
) # add list_config as the action so we don't actually run
os.remove(tmp.name)
# is this right? in cli probes get expanded into the namespace.class format
assert _config.plugins.probes["test"]["Blank"]["gen_x"] == 37176
# test generator_options YAML
# more refactor for namespace keys
def test_generator_options_yaml(capsys):
with tempfile.NamedTemporaryFile(buffering=0, delete=False, suffix=".yaml") as tmp:
tmp.write(
"\n".join(
[
"---",
"plugins:",
" target_type: test.Blank",
" probe_spec: test.Blank",
" generators:",
" test:",
" test_val: test_value",
" Blank:",
" test_val: test_blank_value",
" gen_x: 37176",
]
).encode("utf-8")
)
tmp.close()
garak.cli.main(
["--config", tmp.name, "--list_config"]
) # add list_config as the action so we don't actually run
os.remove(tmp.name)
assert _config.plugins.generators["test"]["Blank"]["gen_x"] == 37176
assert (
_config.plugins.generators["test"]["Blank"]["test_val"]
== "test_blank_value"
)
# can a run be launched from a run YAML?
def test_run_from_yaml(capsys):
with tempfile.NamedTemporaryFile(buffering=0, delete=False, suffix=".yaml") as tmp:
tmp.write(
"\n".join(
[
"---",
"run:",
" generations: 10",
"",
"plugins:",
" target_type: test.Blank",
" probe_spec: test.Blank",
]
).encode("utf-8")
)
tmp.close()
garak.cli.main(["--config", tmp.name])
os.remove(tmp.name)
result = capsys.readouterr()
output = result.out
all_output = ""
for line in output.strip().split("\n"):
line = ANSI_ESCAPE.sub("", line)
all_output += line
assert "loading generator: Test: Blank" in all_output
assert "queue of probes: test.Blank" in all_output
assert "ok on 10/ 10" in all_output
assert "any.AnyOutput:" in all_output
assert "test.Blank" in all_output
assert "garak run complete" in all_output
# cli generator options file loads
# more refactor for namespace keys
@pytest.mark.usefixtures("allow_site_config")
def test_cli_generator_options_file():
# write an options file
with tempfile.NamedTemporaryFile(mode="w+", delete=False) as tmp:
json.dump({"test": {"Blank": {"this_is_a": "generator"}}}, tmp)
tmp.close()
# invoke cli
garak.cli.main(
["--generator_option_file", tmp.name, "--list_config"]
) # add list_config as the action so we don't actually run
os.remove(tmp.name)
# check it was loaded
assert _config.plugins.generators["test"]["Blank"] == {"this_is_a": "generator"}
# cli generator options file loads
# more refactor for namespace keys
def test_cli_probe_options_file():
# write an options file
with tempfile.NamedTemporaryFile(mode="w+", delete=False) as tmp:
json.dump({"test": {"Blank": {"probes_in_this_config": 1}}}, tmp)
tmp.close()
# invoke cli
garak.cli.main(
["--probe_option_file", tmp.name, "--list_config"]
) # add list_config as the action so we don't actually run
os.remove(tmp.name)
# check it was loaded
assert _config.plugins.probes["test"]["Blank"] == {"probes_in_this_config": 1}
# cli probe config file overrides yaml probe config (using combine into)
# more refactor for namespace keys
def test_cli_probe_options_overrides_yaml_probe_options():
# write an options file
with tempfile.NamedTemporaryFile(mode="w+", delete=False) as probe_json_file:
json.dump({"test": {"Blank": {"goal": "taken from CLI JSON"}}}, probe_json_file)
probe_json_file.close()
with tempfile.NamedTemporaryFile(
buffering=0, delete=False, suffix=".yaml"
) as probe_yaml_file:
probe_yaml_file.write(
"\n".join(
[
"---",
"plugins:",
" probes:",
" test:",
" Blank:",
" goal: taken from CLI YAML",
]
).encode("utf-8")
)
probe_yaml_file.close()
# invoke cli
garak.cli.main(
[
"--config",
probe_yaml_file.name,
"--probe_option_file",
probe_json_file.name,
"--list_config",
]
) # add list_config as the action so we don't actually run
os.remove(probe_json_file.name)
os.remove(probe_yaml_file.name)
# check it was loaded
assert _config.plugins.probes["test"]["Blank"]["goal"] == "taken from CLI JSON"
# cli should override yaml options
def test_cli_generator_options_overrides_yaml_probe_options():
cli_generations_count = 9001
with tempfile.NamedTemporaryFile(
buffering=0, delete=False, suffix=".yaml"
) as generator_yaml_file:
generator_yaml_file.write(
"\n".join(
[
"---",
"run:",
" generations: 999",
]
).encode("utf-8")
)
generator_yaml_file.close()
args = [
"--config",
generator_yaml_file.name,
"-g",
str(cli_generations_count),
"--list_config",
] # add list_config as the action so we don't actually run
garak.cli.main(args)
os.remove(generator_yaml_file.name)
# check it was loaded
assert _config.run.generations == cli_generations_count
# check that probe picks up yaml config items
# more refactor for namespace keys
def test_blank_probe_instance_loads_yaml_config():
import garak._plugins
probe_name = "test.Blank"
probe_namespace, probe_klass = probe_name.split(".")
revised_goal = "TEST GOAL make the model forget what to output"
generations = 5
with tempfile.NamedTemporaryFile(buffering=0, delete=False, suffix=".yaml") as tmp:
tmp.write(
"\n".join(
[
f"---",
f"plugins:",
f" probes:",
f" {probe_namespace}:",
f" {probe_klass}:",
f" generations: {generations}", # generations is required when cli called without a model
f" goal: {revised_goal}",
]
).encode("utf-8")
)
tmp.close()
output = garak.cli.main(["--config", tmp.name, "-p", probe_name])
os.remove(tmp.name)
probe = garak._plugins.load_plugin(f"probes.{probe_name}")
assert probe.goal == revised_goal
# check that probe picks up cli config items
# more refactor for namespace keys
def test_blank_probe_instance_loads_cli_config():
import garak._plugins
probe_name = "test.Blank"
probe_namespace, probe_klass = probe_name.split(".")
revised_goal = "TEST GOAL make the model forget what to output"
args = [
"-p",
probe_name,
"--probe_options",
json.dumps(
{
probe_namespace: {probe_klass: {"goal": revised_goal, "generations": 5}}
}, # generations is required when cli called without a model
ensure_ascii=False,
),
]
garak.cli.main(args)
probe = garak._plugins.load_plugin(f"probes.{probe_name}")
assert probe.goal == revised_goal
# check that generator picks up yaml config items
# more refactor for namespace keys
def test_blank_generator_instance_loads_yaml_config():
import garak._plugins
generator_name = "test.Blank"
generator_namespace, generator_klass = generator_name.split(".")
revised_temp = 0.9001
with tempfile.NamedTemporaryFile(buffering=0, delete=False, suffix=".yaml") as tmp:
tmp.write(
"\n".join(
[
f"---",
f"plugins:",
f" generators:",
f" {generator_namespace}:",
f" temperature: {revised_temp}",
f" {generator_klass}:",
f" test_val: test_blank_value",
]
).encode("utf-8")
)
tmp.close()
garak.cli.main(
["--config", tmp.name, "--target_type", generator_name, "--probes", "none"]
)
os.remove(tmp.name)
gen = garak._plugins.load_plugin(f"generators.{generator_name}")
assert gen.temperature == revised_temp
assert gen.test_val == "test_blank_value"
# check that generator picks up cli config items
# more refactor for namespace keys
def test_blank_generator_instance_loads_cli_config():
import garak._plugins
generator_name = "test.Repeat"
generator_namespace, generator_klass = generator_name.split(".")
revised_temp = 0.9001
args = [
"--target_type",
"test.Blank",
"--probes",
"none",
"--generator_options",
json.dumps(
{generator_namespace: {generator_klass: {"temperature": revised_temp}}},
ensure_ascii=False,
)
.replace(" ", "")
.strip(),
]
garak.cli.main(args)
gen = garak._plugins.load_plugin(f"generators.{generator_name}")
assert gen.temperature == revised_temp
# test parsing of probespec
def test_probespec_loading():
assert _config.parse_plugin_spec(None, "detectors") == ([], [])
assert _config.parse_plugin_spec("", "generators") == ([], [])
assert _config.parse_plugin_spec("Auto", "probes") == ([], [])
assert _config.parse_plugin_spec("NONE", "probes") == ([], [])
# reject unmatched spec entires
assert _config.parse_plugin_spec("probedoesnotexist", "probes") == (
[],
["probedoesnotexist"],
)
assert _config.parse_plugin_spec("atkgen,probedoesnotexist", "probes") == (
["probes.atkgen.Tox"],
["probedoesnotexist"],
)
assert _config.parse_plugin_spec("atkgen.Tox,probedoesnotexist", "probes") == (
["probes.atkgen.Tox"],
["probedoesnotexist"],
)
# reject unmatched spec entires for unknown class
assert _config.parse_plugin_spec(
"atkgen.Tox,atkgen.ProbeDoesNotExist", "probes"
) == (["probes.atkgen.Tox"], ["atkgen.ProbeDoesNotExist"])
# accept known disabled class
assert _config.parse_plugin_spec("dan.DanInTheWild", "probes") == (
["probes.dan.DanInTheWild"],
[],
)
# gather all class entires for namespace
assert _config.parse_plugin_spec("atkgen", "probes") == (["probes.atkgen.Tox"], [])
assert _config.parse_plugin_spec("always", "detectors") == (
[
"detectors.always.Fail",
"detectors.always.Pass",
"detectors.always.Passthru",
"detectors.always.Random",
],
[],
)
# reject all unknown class entires for namespace
assert _config.parse_plugin_spec(
"long.test.class,another.long.test.class", "probes"
) == ([], ["long.test.class", "another.long.test.class"])
def test_buff_config_assertion():
import garak._plugins
test_value = 9001
_config.plugins.buffs["paraphrase"] = {"Fast": {"num_beams": test_value}}
p = garak._plugins.load_plugin("buffs.paraphrase.Fast")
assert p.num_beams == test_value
def test_tag_filter():
assert _config.parse_plugin_spec(
"atkgen", "probes", probe_tag_filter="LOL NULL"
) == ([], [])
assert _config.parse_plugin_spec("*", "probes", probe_tag_filter="avid") != ([], [])
assert _config.parse_plugin_spec("all", "probes", probe_tag_filter="owasp:llm") != (
[],
[],
)
found, rejected = _config.parse_plugin_spec(
"all", "probes", probe_tag_filter="risk-cards:lmrc:sexual_content"
)
assert "probes.lmrc.SexualContent" in found
# when provided an absolute path as `reporting.report_dir` do not used `user_data_dir`
def test_report_dir_full_path():
with tempfile.TemporaryDirectory() as tmpdir:
report_path = Path(tmpdir).absolute()
with tempfile.NamedTemporaryFile(
buffering=0, delete=False, suffix=".yaml"
) as tmp:
tmp.write(
"\n".join(
[
f"---",
f"reporting:",
f" report_dir: {report_path}",
]
).encode("utf-8")
)
tmp.close()
garak.cli.main(
f"-m test.Blank --report_prefix abs_path_test -p test.Blank -d always.Fail --config {tmp.name}".split()
)
os.remove(tmp.name)
assert os.path.isfile(report_path / "abs_path_test.report.jsonl")
assert os.path.isfile(report_path / "abs_path_test.report.html")
assert os.path.isfile(report_path / "abs_path_test.hitlog.jsonl")
# report prefix is used only for filename, report_dir is placed in user_data_dir
def test_report_prefix_with_hitlog_no_explode():
garak.cli.main(
"-m test.Blank --report_prefix kjsfhgkjahpsfdg -p test.Blank -d always.Fail".split()
)
report_path = Path(_config.transient.report_filename).parent
assert _config.reporting.report_dir in str(report_path)
assert str(_config.transient.data_dir) in str(report_path)
assert os.path.isfile(report_path / "kjsfhgkjahpsfdg.report.jsonl")
assert os.path.isfile(report_path / "kjsfhgkjahpsfdg.report.html")
assert os.path.isfile(report_path / "kjsfhgkjahpsfdg.hitlog.jsonl")
def test_nested():
_config.plugins.generators["a"]["b"]["c"]["d"] = "e"
assert _config.plugins.generators["a"]["b"]["c"]["d"] == "e"
def test_get_user_agents():
agents = _config.get_http_lib_agents()
assert isinstance(agents, dict)
AGENT_TEST = "garak/9 - only simple tailors edition"
def test_set_agents():
from requests import utils
import httpx
import aiohttp
_config.set_all_http_lib_agents(AGENT_TEST)
assert str(utils.default_user_agent()) == AGENT_TEST
assert httpx._client.USER_AGENT == AGENT_TEST
assert aiohttp.client_reqrep.SERVER_SOFTWARE == AGENT_TEST
def httpserver():
return HTTPServer()
def test_agent_is_used_requests(httpserver: HTTPServer):
import requests
_config.set_http_lib_agents({"requests": AGENT_TEST})
httpserver.expect_request(
"/", headers={"User-Agent": AGENT_TEST}
).respond_with_data("")
assert requests.get(httpserver.url_for("/")).status_code == 200
def test_agent_is_used_httpx(httpserver: HTTPServer):
import httpx
_config.set_http_lib_agents({"httpx": AGENT_TEST})
httpserver.expect_request(
"/", headers={"User-Agent": AGENT_TEST}
).respond_with_data("")
assert httpx.get(httpserver.url_for("/")).status_code == 200
def test_agent_is_used_aiohttp(httpserver: HTTPServer):
import aiohttp
import asyncio
_config.set_http_lib_agents({"aiohttp": AGENT_TEST})
async def main():
async with aiohttp.ClientSession() as session:
async with session.get(httpserver.url_for("/")) as response:
html = await response.text()
httpserver.expect_request(
"/", headers={"User-Agent": AGENT_TEST}
).respond_with_data("")
asyncio.run(main())
def test_api_key_in_config():
_config.plugins.generators["a"]["b"]["c"]["api_key"] = "something"
assert _config._key_exists(_config.plugins.generators, "api_key")
# test max_workers applies when used in site config
@pytest.mark.usefixtures("allow_site_config")
def test_site_yaml_overrides_max_workers(capsys):
with open(
_config.transient.config_dir / "garak.site.yaml", "w", encoding="utf-8"
) as f:
f.write("---\nsystem:\n max_workers: 2\n")
f.flush()
garak.cli.main(["--list_config"])
assert (
_config.system.max_workers == 2
), "Site config worker count should override core config if loaded correctly"
with pytest.raises(SystemExit) as exc_info:
garak.cli.main("--parallel_attempts 3 -m test -p test.Test".split())
result = capsys.readouterr()
assert (
result.split("\n")[-1]
== "ValueError: Parallel worker count capped at 2 (config.system.max_workers)"
)
assert exc_info.type == SystemExit
assert exc_info.value.code == 1
model_target_data = [
("model_type", "model_name"),
("model_type", "target_name"),
("target_type", "model_name"),
("target_type", "target_name"),
]
@pytest.mark.parametrize("type_key,name_key", model_target_data)
def test_model_target_switching(type_key, name_key):
yaml_template = """
plugins:
{{typekey}}: {{typeval}}
{{namekey}}: {{nameval}}
"""
demo_type = "test.Test"
demo_name = "9218-Black"
yaml_template = yaml_template.replace("{{typeval}}", demo_type).replace(
"{{nameval}}", demo_name
)
candidate_yaml = yaml_template.replace("{{typekey}}", type_key).replace(
"{{namekey}}", name_key
)
with tempfile.NamedTemporaryFile(
mode="w+", delete=False, suffix=".yaml", encoding="utf-8"
) as t:
t.write(candidate_yaml)
t.close()
c = _config._load_config_files([t.name])
assert c["plugins"]["target_name"] == demo_name
assert c["plugins"]["target_type"] == demo_type
def test_model_target_override():
yaml_template = """
plugins:
target_type: {{typeval}}
target_name: {{nameval}}
model_type: donky.Bonky
model_name: obsidian
"""
demo_type = "test.Test"
demo_name = "9218-Black"
candidate_yaml = yaml_template.replace("{{typeval}}", demo_type).replace(
"{{nameval}}", demo_name
)
with tempfile.NamedTemporaryFile(
mode="w+", delete=False, suffix=".yaml", encoding="utf-8"
) as t:
t.write(candidate_yaml)
t.close()
c = _config._load_config_files([t.name])
assert c["plugins"]["target_name"] == demo_name
assert c["plugins"]["target_type"] == demo_type
def test_load_json_config():
config_data = {
"system": {"parallel_attempts": 10},
"run": {"generations": 3},
"plugins": {"probe_spec": "test"},
"reporting": {},
}
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as tmp:
json.dump(config_data, tmp)
tmp.close()
garak.cli.main(["--config", tmp.name, "--list_config"])
os.remove(tmp.name)
assert _config.system.parallel_attempts == 10
assert _config.run.generations == 3
def test_load_json_config_via_load_config_files():
config_data = {
"system": {"verbose": 2},
"run": {"seed": 42},
"plugins": {},
"reporting": {},
}
with tempfile.NamedTemporaryFile(
mode="w", suffix=".json", delete=False, encoding="utf-8"
) as tmp:
json.dump(config_data, tmp)
tmp.close()
c = _config._load_config_files([tmp.name])
os.remove(tmp.name)
assert c["system"]["verbose"] == 2
assert c["run"]["seed"] == 42
@pytest.mark.usefixtures("allow_site_config")
def test_site_config_ambiguity_error():
site_json = _config.transient.config_dir / "garak.site.json"
site_yaml = _config.transient.config_dir / "garak.site.yaml"
try:
site_json.write_text(
'{"system": {"verbose": 1}, "run": {}, "plugins": {}, "reporting": {}}'
)
site_yaml.write_text(
"system:\n verbose: 2\nrun: {}\nplugins: {}\nreporting: {}"
)
with pytest.raises(ValueError, match="Multiple site config files found"):
_config.load_config()
finally:
if site_json.exists():
site_json.unlink()
if site_yaml.exists():
site_yaml.unlink()
def test_extension_less_config_finds_json(temp_package_dir):
json_config = {
"system": {},
"run": {"generations": 7},
"plugins": {},
"reporting": {},
}
test_json_path = temp_package_dir / "configs" / "test_json_config.json"
with open(test_json_path, "w", encoding="utf-8") as f:
json.dump(json_config, f)
garak.cli.main(["--config", "test_json_config", "--list_config"])
assert _config.run.generations == 7
def test_extension_less_requires_explicit_yaml(temp_package_dir, capsys):
yaml_config_content = """
system: {}
run:
generations: 8
plugins: {}
reporting: {}
"""