-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
1479 lines (1296 loc) · 47.2 KB
/
test_cli.py
File metadata and controls
1479 lines (1296 loc) · 47.2 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
import base64
from unittest.mock import patch
import boto3
import yaml
from botocore.stub import Stubber
from typer.testing import CliRunner
from envars.cli import app
runner = CliRunner()
def create_envars_file(tmp_path, content=""):
file_path = tmp_path / "envars.yml"
file_path.write_text(content)
return str(file_path)
def read_yaml_file(file_path):
with open(file_path) as f:
return yaml.safe_load(f)
def test_init_command(tmp_path):
file_path = tmp_path / "envars.yml"
result = runner.invoke(
app,
[
"--file",
str(file_path),
"init",
"--app",
"MyApp",
"--env",
"dev,prod",
"--loc",
"aws:123,gcp:456",
"--description-mandatory",
],
)
assert result.exit_code == 0
assert "Successfully initialized" in result.stdout
data = read_yaml_file(file_path)
assert data["configuration"]["app"] == "MyApp"
assert data["configuration"]["environments"] == ["dev", "prod"]
assert {"aws": "123"} in data["configuration"]["locations"]
assert {"gcp": "456"} in data["configuration"]["locations"]
assert data["configuration"]["description_mandatory"] is True
# Test with default value (False)
file_path_default = tmp_path / "envars_default.yml"
result = runner.invoke(
app,
[
"--file",
str(file_path_default),
"init",
"--app",
"MyApp",
"--env",
"dev,prod",
"--loc",
"aws:123,gcp:456",
],
)
assert result.exit_code == 0
data = read_yaml_file(file_path_default)
assert data["configuration"]["description_mandatory"] is False
def test_init_command_no_locations(tmp_path):
file_path = tmp_path / "envars_no_loc.yml"
result = runner.invoke(
app,
[
"--file",
str(file_path),
"init",
"--app",
"MyApp",
"--env",
"dev,prod",
],
)
assert result.exit_code == 0
assert "Successfully initialized" in result.stdout
data = read_yaml_file(file_path)
assert data["configuration"]["app"] == "MyApp"
assert data["configuration"]["environments"] == ["dev", "prod"]
assert "locations" not in data["configuration"]
assert data["configuration"]["description_mandatory"] is False
def test_add_default_variable(tmp_path):
file_path = create_envars_file(tmp_path)
result = runner.invoke(app, ["--file", file_path, "add", "MY_VAR=my_value"])
assert result.exit_code == 0
assert "Successfully added/updated MY_VAR in" in result.stdout
data = read_yaml_file(file_path)
assert data["environment_variables"]["MY_VAR"]["default"] == "my_value"
def test_add_env_variable(tmp_path):
initial_content = """
configuration:
environments:
- dev
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "add", "MY_VAR=dev_value", "--env", "dev"])
assert result.exit_code == 0
assert "Successfully added/updated MY_VAR in" in result.stdout
data = read_yaml_file(file_path)
assert data["environment_variables"]["MY_VAR"]["dev"] == "dev_value"
def test_add_loc_variable(tmp_path):
initial_content = """
configuration:
locations:
- my_loc: "loc123"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "add", "MY_VAR=loc_value", "--loc", "my_loc"])
assert result.exit_code == 0
assert "Successfully added/updated MY_VAR in" in result.stdout
data = read_yaml_file(file_path)
assert data["environment_variables"]["MY_VAR"]["my_loc"] == "loc_value"
def test_add_specific_variable(tmp_path):
initial_content = """
configuration:
environments:
- dev
locations:
- my_loc: "loc123"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(
app, ["--file", file_path, "add", "MY_VAR=specific_value", "--env", "dev", "--loc", "my_loc"]
)
assert result.exit_code == 0
assert "Successfully added/updated MY_VAR in" in result.stdout
data = read_yaml_file(file_path)
assert data["environment_variables"]["MY_VAR"]["dev"]["my_loc"] == "specific_value"
def test_add_secret_variable(tmp_path):
initial_content = """
configuration:
app: MyApp
kms_key: "arn:aws:kms:us-east-1:123456789012:key/mrk-12345"
environments:
- dev
locations:
- my_loc: "loc123"
"""
file_path = create_envars_file(tmp_path, initial_content)
kms_client = boto3.client("kms", region_name="us-east-1")
with Stubber(kms_client) as stubber:
encrypted_value = base64.b64encode(b"encrypted_value").decode("utf-8")
stubber.add_response(
"encrypt",
{"CiphertextBlob": b"encrypted_value"},
{
"KeyId": "arn:aws:kms:us-east-1:123456789012:key/mrk-12345",
"Plaintext": b"super_secret_value",
"EncryptionContext": {"app": "MyApp", "env": "dev", "location": "my_loc"},
},
)
with patch("boto3.client", return_value=kms_client):
result = runner.invoke(
app,
[
"--file",
file_path,
"add",
"MY_SECRET=super_secret_value",
"--env",
"dev",
"--loc",
"my_loc",
"--secret",
],
)
assert result.exit_code == 0
assert "Successfully added/updated MY_SECRET in" in result.stdout
stubber.assert_no_pending_responses()
with open(file_path) as f:
content = f.read()
assert "!secret" in content
assert encrypted_value in content
def test_update_existing_variable(tmp_path):
initial_content = """
environment_variables:
MY_VAR:
default: "old_value"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "add", "MY_VAR=new_value"])
assert result.exit_code == 0
assert "Successfully added/updated MY_VAR in" in result.stdout
data = read_yaml_file(file_path)
assert data["environment_variables"]["MY_VAR"]["default"] == "new_value"
def test_add_variable_invalid_format(tmp_path):
file_path = create_envars_file(tmp_path)
result = runner.invoke(app, ["--file", file_path, "add", "MY_VAR_no_equal_sign"])
assert result.exit_code == 1
assert "Invalid variable assignment format" in result.stderr
def test_add_variable_non_existent_location(tmp_path):
initial_content = """
configuration:
locations:
- my_loc: "loc123"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "add", "MY_VAR=value", "--loc", "non_existent_loc"])
assert result.exit_code == 1
assert "Location 'non_existent_loc' not found" in result.stderr
def test_add_variable_with_loc_but_no_locations_configured(tmp_path):
file_path = create_envars_file(tmp_path)
result = runner.invoke(app, ["--file", file_path, "add", "MY_VAR=value", "--loc", "some_loc"])
assert result.exit_code == 1
assert "locations' are not configured" in result.stderr
def test_add_variable_non_existent_environment_for_specific(tmp_path):
initial_content = """
configuration:
locations:
- my_loc: "loc123"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(
app, ["--file", file_path, "add", "MY_VAR=value", "--env", "non_existent_env", "--loc", "my_loc"]
)
assert (
result.exit_code == 0
) # Should add the variable, but the environment won't be recognized by get_variable_value
assert "Successfully added/updated MY_VAR in" in result.stdout
data = read_yaml_file(file_path)
assert data["environment_variables"]["MY_VAR"]["non_existent_env"]["my_loc"] == "value"
def test_print_decrypt_secret(tmp_path):
encrypted_string = base64.b64encode(b"some_encrypted_bytes").decode("utf-8")
initial_content = f"""
configuration:
app: MyApp
kms_key: "arn:aws:kms:us-east-1:123456789012:key/mrk-12345"
environments:
- dev
locations:
- my_loc: "loc123"
environment_variables:
MY_SECRET:
dev:
my_loc: !secret {encrypted_string}
"""
file_path = create_envars_file(tmp_path, initial_content)
kms_client = boto3.client("kms", region_name="us-east-1")
with Stubber(kms_client) as stubber:
stubber.add_response(
"decrypt",
{"Plaintext": b"decrypted_value"},
{
"CiphertextBlob": b"some_encrypted_bytes",
"EncryptionContext": {"app": "MyApp", "env": "dev", "location": "my_loc"},
},
)
with patch("boto3.client", return_value=kms_client):
result = runner.invoke(
app,
[
"--file",
file_path,
"output",
"--env",
"dev",
"--loc",
"my_loc",
],
)
assert result.exit_code == 0, result.stderr
assert "MY_SECRET=decrypted_value" in result.stdout
stubber.assert_no_pending_responses()
@patch("os.execvpe")
def test_exec_command_with_envars_env_var(mock_execvpe, tmp_path):
initial_content = """
configuration:
environments:
- dev
locations:
- my_loc: "loc123"
environment_variables:
MY_VAR:
default: "default_value"
dev:
my_loc: "dev_loc_value"
"""
file_path = create_envars_file(tmp_path, initial_content)
with patch.dict("os.environ", {"ENVARS_ENV": "dev"}):
result = runner.invoke(
app,
[
"--file",
file_path,
"exec",
"--loc",
"my_loc",
"sh",
"-c",
"echo $MY_VAR",
],
)
assert result.exit_code == 0
# Assert that execvpe was called with the correct command and environment
mock_execvpe.assert_called_once()
call_args = mock_execvpe.call_args[0]
assert call_args[0] == "sh"
assert call_args[1] == [
"sh",
"-c",
"echo $MY_VAR",
]
assert "MY_VAR" in call_args[2]
assert call_args[2]["MY_VAR"] == "dev_loc_value"
@patch("os.execvpe")
def test_exec_command_greedy(mock_execvpe, tmp_path):
initial_content = """
configuration:
environments:
- dev
locations:
- my_loc: "loc123"
environment_variables:
MY_VAR:
default: "default_value"
dev:
my_loc: "dev_loc_value"
"""
file_path = create_envars_file(tmp_path, initial_content)
# The command to execute is `sh -c 'echo $MY_VAR'`.
# This will print the value of the environment variable MY_VAR.
result = runner.invoke(
app,
[
"--file",
file_path,
"exec",
"--env",
"dev",
"--loc",
"my_loc",
"sh",
"-c",
"echo $MY_VAR",
],
)
assert result.exit_code == 0
# Assert that execvpe was called with the correct command and environment
mock_execvpe.assert_called_once()
call_args = mock_execvpe.call_args[0]
assert call_args[0] == "sh"
assert call_args[1] == [
"sh",
"-c",
"echo $MY_VAR",
]
assert "MY_VAR" in call_args[2]
assert call_args[2]["MY_VAR"] == "dev_loc_value"
# Test with a command that has its own flags
mock_execvpe.reset_mock()
result = runner.invoke(
app,
[
"--file",
file_path,
"exec",
"--env",
"dev",
"--loc",
"my_loc",
"sh",
"-c",
'echo "var=$MY_VAR, args=$@"',
"--",
"--my-flag",
"my-value",
],
)
assert result.exit_code == 0
mock_execvpe.assert_called_once()
call_args = mock_execvpe.call_args[0]
assert call_args[0] == "sh"
assert call_args[1] == [
"sh",
"-c",
'echo "var=$MY_VAR, args=$@"',
"--my-flag",
"my-value",
]
assert "MY_VAR" in call_args[2]
assert call_args[2]["MY_VAR"] == "dev_loc_value"
def test_print_with_env_and_loc(tmp_path):
initial_content = """
configuration:
environments:
- dev
locations:
- my_loc: "loc123"
environment_variables:
MY_VAR:
default: "default_value"
dev:
my_loc: "dev_loc_value"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "output", "--env", "dev", "--loc", "my_loc"])
assert result.exit_code == 0
assert "MY_VAR=dev_loc_value" in result.stdout
# Test with ENVARS_ENV environment variable
with patch.dict("os.environ", {"ENVARS_ENV": "dev"}):
result = runner.invoke(app, ["--file", file_path, "output", "--loc", "my_loc"])
assert result.exit_code == 0
assert "MY_VAR=dev_loc_value" in result.stdout
def test_tree_command(tmp_path):
initial_content = """
configuration:
environments:
- dev
locations:
- my_loc: "loc123"
environment_variables:
MY_VAR:
default: "default_value"
dev:
my_loc: "dev_loc_value"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "tree"])
assert result.exit_code == 0
assert "Envars Configuration" in result.stdout
assert "MY_VAR" in result.stdout
def test_print_invalid_env(tmp_path):
initial_content = """
configuration:
environments:
- dev
locations:
- my_loc: "loc123"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "output", "--env", "prod", "--loc", "my_loc"])
assert result.exit_code == 1
assert "Environment 'prod' not found" in result.stderr
def test_print_invalid_loc(tmp_path):
initial_content = """
configuration:
environments:
- dev
locations:
- my_loc: "loc123"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "output", "--env", "dev", "--loc", "other_loc"])
assert result.exit_code == 1
assert "Location 'other_loc' not found" in result.stderr
def test_output_invalid_loc_no_locations_configured(tmp_path):
initial_content = """
configuration:
environments:
- dev
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "output", "--env", "dev", "--loc", "other_loc"])
assert result.exit_code == 1
assert "'locations' are not configured" in result.stderr
def test_exec_invalid_env(tmp_path):
initial_content = """
configuration:
environments:
- dev
locations:
- my_loc: "loc123"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "exec", "--env", "prod", "--loc", "my_loc", "echo", "hello"])
assert result.exit_code == 1
assert "Environment 'prod' not found" in result.stderr
def test_exec_invalid_loc(tmp_path):
initial_content = """
configuration:
environments:
- dev
locations:
- my_loc: "loc123"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "exec", "--env", "dev", "--loc", "other_loc", "echo", "hello"])
assert result.exit_code == 1
assert "Location 'other_loc' not found" in result.stderr
def test_output_yaml_command(tmp_path):
encrypted_string = base64.b64encode(b"some_encrypted_bytes").decode("utf-8")
initial_content = f"""
configuration:
app: MyApp
kms_key: "arn:aws:kms:us-east-1:123456789012:key/mrk-12345"
environments:
- dev
locations:
- my_loc: "loc123"
environment_variables:
MY_VAR:
default: "default_value"
dev:
my_loc: "dev_loc_value"
MY_SECRET:
dev:
my_loc: !secret {encrypted_string}
"""
file_path = create_envars_file(tmp_path, initial_content)
kms_client = boto3.client("kms", region_name="us-east-1")
with Stubber(kms_client) as stubber:
stubber.add_response(
"decrypt",
{"Plaintext": b"decrypted_value"},
{
"CiphertextBlob": b"some_encrypted_bytes",
"EncryptionContext": {"app": "MyApp", "env": "dev", "location": "my_loc"},
},
)
with patch("boto3.client", return_value=kms_client):
result = runner.invoke(
app, ["--file", file_path, "output", "--format", "yaml", "--env", "dev", "--loc", "my_loc"]
)
assert result.exit_code == 0
expected_yaml = """
envars:
MY_VAR: dev_loc_value
MY_SECRET: decrypted_value
"""
output_dict = yaml.safe_load(result.stdout)
expected_dict = yaml.safe_load(expected_yaml)
assert output_dict == expected_dict
stubber.assert_no_pending_responses()
def test_output_json_command(tmp_path):
initial_content = """
configuration:
environments:
- dev
locations:
- my_loc: "loc123"
environment_variables:
MY_VAR:
default: "default_value"
dev:
my_loc: "dev_loc_value"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "output", "--format", "json", "--env", "dev", "--loc", "my_loc"])
assert result.exit_code == 0
import json
output_dict = json.loads(result.stdout)
assert output_dict == {"envars": {"MY_VAR": "dev_loc_value"}}
@patch("google.auth.default", return_value=(None, None))
@patch("subprocess.run")
def test_set_systemd_env_command(mock_run, mock_google_auth, tmp_path):
mock_run.return_value.stdout = ""
initial_content = """
configuration:
environments:
- dev
locations:
- my_loc: "loc123"
environment_variables:
MY_VAR:
default: "default_value"
dev:
my_loc: "dev_loc_value"
ANOTHER_VAR:
default: "another_value"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "set-systemd-env", "--env", "dev", "--loc", "my_loc"])
assert result.exit_code == 0
assert "Successfully set systemd environment variables" in result.stdout
mock_run.assert_called_once()
call_args = mock_run.call_args[0]
assert call_args[0] == [
"systemctl",
"--user",
"set-environment",
"MY_VAR=dev_loc_value",
"ANOTHER_VAR=another_value",
]
def test_validate_command_success(tmp_path):
initial_content = """
configuration:
environments:
- dev
locations:
- my_loc: "loc123"
environment_variables:
MY_VAR:
description: "A test variable"
dev:
my_loc: "dev_loc_value"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "validate"])
assert result.exit_code == 0
assert "Validation successful!" in result.stdout
def test_validate_command_missing_variable_definition(tmp_path):
# The `load_from_yaml` will load it, but `validate` should catch it.
file_path = tmp_path / "invalid_vars.yml"
with open(file_path, "w") as f:
f.write(
"""
environment_variables:
MY_VAR:
description: "This is fine"
# ANOTHER_VAR is not defined here, but has values below
ANOTHER_VAR:
default: "another_value"
"""
)
# To make this test work, we need to create a manager that has the inconsistency.
# We'll add a value for a variable that is not in the manager's `variables` dict.
from src.envars.models import VariableManager, VariableValue
manager = VariableManager()
manager.add_variable_value(VariableValue(variable_name="UNDEFINED_VAR", value="some_value", scope_type="DEFAULT"))
with patch("envars.cli.load_from_yaml", return_value=manager):
result = runner.invoke(app, ["--file", str(file_path), "validate"])
assert result.exit_code == 1
assert "Variable 'UNDEFINED_VAR' has values but is not defined as a top-level variable." in result.stderr.replace(
"\n", ""
)
def test_add_variable_lowercase(tmp_path):
file_path = create_envars_file(tmp_path)
result = runner.invoke(app, ["--file", file_path, "add", "my_var=my_value"])
assert result.exit_code == 1
assert "Variable names must be uppercase." in result.stderr
def test_validate_command_lowercase_variable(tmp_path):
initial_content = """
environment_variables:
my_var:
default: "my_value"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "validate"])
assert result.exit_code == 1
assert "Variable name 'my_var' must be uppercase." in result.stderr
def test_load_from_yaml_lowercase_variable(tmp_path):
initial_content = """
environment_variables:
my_var:
default: "my_value"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "output"])
assert result.exit_code == 1
assert "Variable name 'my_var' must be uppercase." in result.stderr
def test_add_variable_description_mandatory(tmp_path):
initial_content = """
configuration:
description_mandatory: true
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "add", "MY_VAR=my_value"])
assert result.exit_code == 1
assert "Description is mandatory for new variable 'MY_VAR'." in result.stderr
result = runner.invoke(app, ["--file", file_path, "add", "MY_VAR=my_value", "--description", "My description"])
assert result.exit_code == 0
def test_validate_command_description_mandatory(tmp_path):
initial_content = """
configuration:
description_mandatory: true
environment_variables:
MY_VAR:
default: "my_value"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "validate"])
assert result.exit_code == 1
assert "Variable 'MY_VAR' is missing a description." in result.stderr
def test_config_command(tmp_path):
initial_content = """
configuration:
app: MyApp
kms_key: "old-kms-key"
description_mandatory: false
environments:
- dev
locations:
- my_loc: "loc123"
"""
file_path = create_envars_file(tmp_path, initial_content)
# Test updating kms_key
result = runner.invoke(app, ["--file", file_path, "config", "--kms-key", "new-kms-key"])
assert result.exit_code == 0
data = read_yaml_file(file_path)
assert data["configuration"]["kms_key"] == "new-kms-key"
# Test adding an environment
result = runner.invoke(app, ["--file", file_path, "config", "--add-env", "prod"])
assert result.exit_code == 0
data = read_yaml_file(file_path)
assert "prod" in data["configuration"]["environments"]
# Test removing an environment
result = runner.invoke(app, ["--file", file_path, "config", "--remove-env", "dev"])
assert result.exit_code == 0
data = read_yaml_file(file_path)
assert "dev" not in data["configuration"]["environments"]
# Test adding a location
result = runner.invoke(app, ["--file", file_path, "config", "--add-loc", "new_loc:loc456"])
assert result.exit_code == 0
data = read_yaml_file(file_path)
assert {"new_loc": "loc456"} in data["configuration"]["locations"]
# Test removing a location
result = runner.invoke(app, ["--file", file_path, "config", "--remove-loc", "my_loc"])
assert result.exit_code == 0
data = read_yaml_file(file_path)
assert {"my_loc": "loc123"} not in data["configuration"]["locations"]
# Test updating description_mandatory
result = runner.invoke(app, ["--file", file_path, "config", "--description-mandatory"])
assert result.exit_code == 0
data = read_yaml_file(file_path)
assert data["configuration"]["description_mandatory"] is True
result = runner.invoke(app, ["--file", file_path, "config", "--no-description-mandatory"])
assert result.exit_code == 0
data = read_yaml_file(file_path)
assert data["configuration"]["description_mandatory"] is False
def test_add_sensitive_variable_no_flag(tmp_path):
file_path = create_envars_file(tmp_path)
result = runner.invoke(app, ["--file", file_path, "add", "MY_PASSWORD=my_value"])
assert result.exit_code == 1
assert "Variable 'MY_PASSWORD' may be sensitive." in result.stderr
def test_add_sensitive_variable_no_secret_flag(tmp_path):
file_path = create_envars_file(tmp_path)
result = runner.invoke(app, ["--file", file_path, "add", "MY_PASSWORD=my_value", "--no-secret"])
assert result.exit_code == 0
data = read_yaml_file(file_path)
assert data["environment_variables"]["MY_PASSWORD"]["default"] == "my_value"
def test_add_default_secret_fails(tmp_path):
initial_content = """
configuration:
kms_key: "some-key"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "add", "MY_SECRET=my_value", "--secret"])
assert result.exit_code == 1
assert "Secrets must be scoped to an environment and/or location." in result.stderr
def test_validate_default_secret_fails(tmp_path):
initial_content = """
configuration:
kms_key: "some-key"
environment_variables:
MY_SECRET:
default: !secret "my_value"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "validate"])
assert result.exit_code == 1
assert "Variable 'MY_SECRET' is a secret and cannot have a default value." in result.stderr
def test_validate_default_secret_fails_with_ignore_flag(tmp_path):
initial_content = """
configuration:
kms_key: "some-key"
environment_variables:
MY_SECRET:
default: !secret "my_value"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "validate", "--ignore-default-secrets"])
assert result.exit_code == 0
assert "Validation successful!" in result.stdout
def test_variable_templating_with_jinja(tmp_path):
initial_content = """
configuration:
environments:
- dev
locations:
- my_loc: "loc123"
environment_variables:
DOMAIN:
default: "example.com"
HOSTNAME:
default: "my-app.{{ DOMAIN }}"
DATABASE_URL:
default: "postgres://user:pass@{{ HOSTNAME }}:5432/db"
DEFAULT_PORT:
default: "5432"
PORT:
default: "{{ env.get('PORT', DEFAULT_PORT) }}"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "output", "--format", "yaml", "--env", "dev", "--loc", "my_loc"])
assert result.exit_code == 0
output_dict = yaml.safe_load(result.stdout)
assert output_dict["envars"]["HOSTNAME"] == "my-app.example.com"
assert output_dict["envars"]["DATABASE_URL"] == "postgres://user:pass@my-app.example.com:5432/db"
assert output_dict["envars"]["PORT"] == "5432"
@patch("envars.main.GCPSecretManager")
@patch("envars.main.SSMParameterStore")
def test_variable_from_parameter_store(mock_ssm_store, mock_gcp_secret_manager, tmp_path):
mock_ssm_instance = mock_ssm_store.return_value
mock_ssm_instance.get_parameter.return_value = "ssm_value"
initial_content = """
configuration:
kms_key: "arn:aws:kms:us-east-1:123456789012:key/mrk-12345"
environments:
- dev
locations:
- my_loc: "loc123"
environment_variables:
MY_VAR:
default: "parameter_store:/my/parameter"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "output", "--format", "yaml", "--env", "dev", "--loc", "my_loc"])
assert result.exit_code == 0
output_dict = yaml.safe_load(result.stdout)
assert output_dict["envars"]["MY_VAR"] == "ssm_value"
mock_ssm_instance.get_parameter.assert_called_once_with("/my/parameter")
@patch("envars.main.GCPSecretManager")
def test_variable_from_gcp_secret_manager(mock_gcp_secret_manager, tmp_path):
mock_gcp_instance = mock_gcp_secret_manager.return_value
mock_gcp_instance.access_secret_version.return_value = "gcp_secret_value"
initial_content = """
configuration:
kms_key: "projects/my-gcp-project/locations/us-central1/keyRings/my-key-ring/cryptoKeys/my-key"
environments:
- dev
locations:
- my_loc: "loc123"
environment_variables:
MY_VAR:
default: "gcp_secret_manager:projects/my-project/secrets/my-secret/versions/latest"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "output", "--format", "yaml", "--env", "dev", "--loc", "my_loc"])
assert result.exit_code == 0
output_dict = yaml.safe_load(result.stdout)
assert output_dict["envars"]["MY_VAR"] == "gcp_secret_value"
mock_gcp_instance.access_secret_version.assert_called_once_with(
"projects/my-project/secrets/my-secret/versions/latest"
)
def test_add_mismatched_remote_variable(tmp_path):
initial_content = """
configuration:
kms_key: "arn:aws:kms:us-east-1:123456789012:key/mrk-12345"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(
app,
[
"--file",
file_path,
"add",
"MY_VAR=gcp_secret_manager:projects/my-project/secrets/my-secret/versions/latest",
],
)
assert result.exit_code == 1
assert "Cannot use 'gcp_secret_manager:' with an AWS KMS key." in result.stderr
def test_validate_mismatched_remote_variable(tmp_path):
initial_content = """
configuration:
kms_key: "arn:aws:kms:us-east-1:123456789012:key/mrk-12345"
environment_variables:
MY_VAR:
default: "gcp_secret_manager:projects/my-project/secrets/my-secret/versions/latest"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "validate"])
assert result.exit_code == 1
assert "Variable 'MY_VAR' uses 'gcp_secret_manager:' with an AWS KMS key." in result.stderr
def test_validate_mismatched_remote_variable_cf(tmp_path):
initial_content = """
configuration:
kms_key: "projects/my-gcp-project/locations/us-central1/keyRings/my-key-ring/cryptoKeys/my-key"
environment_variables:
MY_VAR:
default: "cloudformation_export:my-export"
"""
file_path = create_envars_file(tmp_path, initial_content)
result = runner.invoke(app, ["--file", file_path, "validate"])
assert result.exit_code == 1
assert "uses 'parameter_store:' or 'cloudformation_export:' with a GCP KMS key." in result.stderr.replace("\n", "")
def test_load_from_yaml_invalid_structure(tmp_path):
initial_content = """
configuration:
environments: