12
12
from pathlib import Path
13
13
from shutil import copyfile
14
14
from unittest import TestCase
15
- from unittest .mock import ANY , MagicMock , call , patch
15
+ from unittest .mock import ANY , MagicMock , Mock , call , patch
16
16
17
17
import pytest
18
18
import yaml
@@ -114,6 +114,11 @@ def test_escape_markdown(string):
114
114
assert escape_markdown (string ) == string
115
115
116
116
117
+ @pytest .fixture
118
+ def session ():
119
+ return Mock (spec_set = ["client" , "region_name" , "get_credentials" ])
120
+
121
+
117
122
@pytest .fixture
118
123
def project (tmpdir ):
119
124
unique_dir = "" .join (random .choices (string .ascii_uppercase , k = 12 ))
@@ -476,7 +481,7 @@ def test_generate_with_docs(project, tmp_path_factory, schema_path, path):
476
481
),
477
482
],
478
483
)
479
- def test_generate_docs_for_hook (project , tmp_path_factory , schema_path , path ):
484
+ def test_generate_docs_for_hook (project , tmp_path_factory , session , schema_path , path ):
480
485
project .schema = resource_json (__name__ , schema_path )
481
486
project .type_name = "AWS::FooBar::Hook"
482
487
project .artifact_type = ARTIFACT_TYPE_HOOK
@@ -485,7 +490,17 @@ def test_generate_docs_for_hook(project, tmp_path_factory, schema_path, path):
485
490
project .root = tmp_path_factory .mktemp (path )
486
491
487
492
mock_plugin = MagicMock (spec = ["generate" ])
488
- with patch .object (project , "_plugin" , mock_plugin ):
493
+ patch_session = patch ("rpdk.core.boto_helpers.Boto3Session" )
494
+
495
+ mock_cfn_client = MagicMock (spec = ["describe_type" ])
496
+ with patch .object (project , "_plugin" , mock_plugin ), patch_session as mock_session :
497
+ mock_cfn_client .describe_type .return_value = {
498
+ "Schema" : {},
499
+ "Type" : "" ,
500
+ "ProvisioningType" : "" ,
501
+ }
502
+ session .client .side_effect = [mock_cfn_client , MagicMock ()]
503
+ mock_session .return_value = session
489
504
project .generate ()
490
505
project .generate_docs ()
491
506
mock_plugin .generate .assert_called_once_with (project )
@@ -495,13 +510,15 @@ def test_generate_docs_for_hook(project, tmp_path_factory, schema_path, path):
495
510
496
511
assert docs_dir .is_dir ()
497
512
assert readme_file .is_file ()
498
- with patch .object (project , "_plugin" , mock_plugin ):
513
+ with patch .object (project , "_plugin" , mock_plugin ), patch_session as mock_session :
514
+ session .client .side_effect = [mock_cfn_client , MagicMock ()]
515
+ mock_session .return_value = session
499
516
project .generate ()
500
517
readme_contents = readme_file .read_text (encoding = "utf-8" )
501
518
assert project .type_name in readme_contents
502
519
503
520
504
- def test_generate_docs_with_multityped_property (project , tmp_path_factory ):
521
+ def test_generate_docs_with_multityped_property (project , tmp_path_factory , session ):
505
522
project .schema = resource_json (
506
523
__name__ , "data/schema/valid/valid_multityped_property.json"
507
524
)
@@ -510,7 +527,9 @@ def test_generate_docs_with_multityped_property(project, tmp_path_factory):
510
527
# tmpdir conflicts with other tests, make a unique one
511
528
project .root = tmp_path_factory .mktemp ("generate_with_docs_type_complex" )
512
529
mock_plugin = MagicMock (spec = ["generate" ])
513
- with patch .object (project , "_plugin" , mock_plugin ):
530
+ patch_session = patch ("rpdk.core.boto_helpers.Boto3Session" )
531
+ with patch .object (project , "_plugin" , mock_plugin ), patch_session as mock_session :
532
+ mock_session .return_value = session
514
533
project .generate ()
515
534
project .generate_docs ()
516
535
mock_plugin .generate .assert_called_once_with (project )
@@ -778,7 +797,7 @@ def test_init_resource(project):
778
797
assert f .read () == b"\n "
779
798
780
799
781
- def test_generate_hook_handlers (project , tmpdir ):
800
+ def test_generate_hook_handlers (project , tmpdir , session ):
782
801
project .type_name = "Test::Handler::Test"
783
802
project .artifact_type = ARTIFACT_TYPE_HOOK
784
803
expected_actions = {"preCreateAction" , "preDeleteAction" }
@@ -790,7 +809,9 @@ def test_generate_hook_handlers(project, tmpdir):
790
809
}
791
810
project .root = tmpdir
792
811
mock_plugin = MagicMock (spec = ["generate" ])
793
- with patch .object (project , "_plugin" , mock_plugin ):
812
+ patch_session = patch_session = patch ("rpdk.core.boto_helpers.Boto3Session" )
813
+ with patch .object (project , "_plugin" , mock_plugin ), patch_session as mock_session :
814
+ mock_session .return_value = session
794
815
project .generate ()
795
816
796
817
role_path = project .root / "hook-role.yaml"
@@ -820,7 +841,10 @@ def test_generate_hook_handlers_deny_all(project, tmpdir, schema):
820
841
project .schema = schema
821
842
project .root = tmpdir
822
843
mock_plugin = MagicMock (spec = ["generate" ])
823
- with patch .object (project , "_plugin" , mock_plugin ):
844
+ with patch .object (project , "_plugin" , mock_plugin ), patch (
845
+ "rpdk.core.boto_helpers.Boto3Session"
846
+ ) as session :
847
+ session .return_value = session ()
824
848
project .generate ()
825
849
826
850
role_path = project .root / "hook-role.yaml"
@@ -854,13 +878,17 @@ def test_generate_hook_handlers_deny_all(project, tmpdir, schema):
854
878
({"handlers" : {"preCreate" : {"timeoutInMinutes" : 90 }, "preDelete" : {}}}, 8400 ),
855
879
),
856
880
)
857
- def test_generate__hook_handlers_role_session_timeout (project , tmpdir , schema , result ):
881
+ def test_generate__hook_handlers_role_session_timeout (
882
+ project , tmpdir , schema , result , session
883
+ ):
858
884
project .type_name = "Test::Handler::Test"
859
885
project .artifact_type = ARTIFACT_TYPE_HOOK
860
886
project .schema = schema
861
887
project .root = tmpdir
862
888
mock_plugin = MagicMock (spec = ["generate" ])
863
- with patch .object (project , "_plugin" , mock_plugin ):
889
+ patch_session = patch ("rpdk.core.boto_helpers.Boto3Session" )
890
+ with patch .object (project , "_plugin" , mock_plugin ), patch_session as mock_session :
891
+ mock_session .return_value = session
864
892
project .generate ()
865
893
866
894
role_path = project .root / "hook-role.yaml"
@@ -1400,7 +1428,7 @@ def test_submit_dry_run_hooks(project):
1400
1428
1401
1429
1402
1430
# pylint: disable=too-many-arguments, too-many-locals, too-many-statements
1403
- def test_submit_dry_run_hooks_with_target_info (project ):
1431
+ def test_submit_dry_run_hooks_with_target_info (project , session ):
1404
1432
schema = {
1405
1433
"typeName" : "AWS::FOO::BAR" ,
1406
1434
"description" : "test schema" ,
@@ -1466,13 +1494,13 @@ def test_submit_dry_run_hooks_with_target_info(project):
1466
1494
patch_upload = patch .object (project , "_upload" , autospec = True )
1467
1495
patch_path = patch ("rpdk.core.project.Path" , return_value = zip_path )
1468
1496
patch_temp = patch ("rpdk.core.project.TemporaryFile" , autospec = True )
1469
-
1497
+ patch_session = patch ( "rpdk.core.boto_helpers.Boto3Session" )
1470
1498
# fmt: off
1471
1499
# these context managers can't be wrapped by black, but it removes the \
1472
1500
with patch_plugin as mock_plugin , patch_path as mock_path , \
1473
- patch_temp as mock_temp , patch_upload as mock_upload :
1501
+ patch_temp as mock_temp , patch_upload as mock_upload , patch_session as mock_session :
1474
1502
mock_plugin .get_plugin_information = MagicMock (return_value = PLUGIN_INFORMATION )
1475
-
1503
+ mock_session . return_value = session
1476
1504
project .submit (
1477
1505
True ,
1478
1506
endpoint_url = None ,
@@ -2196,10 +2224,12 @@ def test__get_docs_gettable_atts_good_path():
2196
2224
assert getatt == [{"name" : "Id2" , "description" : "foo" }]
2197
2225
2198
2226
2199
- def test_generate_image_build_config (project ):
2227
+ def test_generate_image_build_config (project , session ):
2200
2228
project .schema = {}
2201
2229
mock_plugin = MagicMock (spec = ["generate_image_build_config" ])
2202
- with patch .object (project , "_plugin" , mock_plugin ):
2230
+ patch_session = patch ("rpdk.core.boto_helpers.Boto3Session" )
2231
+ with patch .object (project , "_plugin" , mock_plugin ), patch_session as mock_session :
2232
+ mock_session .return_value = session
2203
2233
project .generate_image_build_config ()
2204
2234
mock_plugin .generate_image_build_config .assert_called_once ()
2205
2235
0 commit comments