Skip to content

Commit 1a2eb78

Browse files
authored
fix(test) - Remove dependency on ~/.aws/config and credentials (#910)
* Remove dependency on .aws/config and credentials
1 parent 33ef7b9 commit 1a2eb78

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

tests/test_project.py

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pathlib import Path
1313
from shutil import copyfile
1414
from unittest import TestCase
15-
from unittest.mock import ANY, MagicMock, call, patch
15+
from unittest.mock import ANY, MagicMock, Mock, call, patch
1616

1717
import pytest
1818
import yaml
@@ -114,6 +114,11 @@ def test_escape_markdown(string):
114114
assert escape_markdown(string) == string
115115

116116

117+
@pytest.fixture
118+
def session():
119+
return Mock(spec_set=["client", "region_name", "get_credentials"])
120+
121+
117122
@pytest.fixture
118123
def project(tmpdir):
119124
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):
476481
),
477482
],
478483
)
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):
480485
project.schema = resource_json(__name__, schema_path)
481486
project.type_name = "AWS::FooBar::Hook"
482487
project.artifact_type = ARTIFACT_TYPE_HOOK
@@ -485,7 +490,17 @@ def test_generate_docs_for_hook(project, tmp_path_factory, schema_path, path):
485490
project.root = tmp_path_factory.mktemp(path)
486491

487492
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
489504
project.generate()
490505
project.generate_docs()
491506
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):
495510

496511
assert docs_dir.is_dir()
497512
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
499516
project.generate()
500517
readme_contents = readme_file.read_text(encoding="utf-8")
501518
assert project.type_name in readme_contents
502519

503520

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):
505522
project.schema = resource_json(
506523
__name__, "data/schema/valid/valid_multityped_property.json"
507524
)
@@ -510,7 +527,9 @@ def test_generate_docs_with_multityped_property(project, tmp_path_factory):
510527
# tmpdir conflicts with other tests, make a unique one
511528
project.root = tmp_path_factory.mktemp("generate_with_docs_type_complex")
512529
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
514533
project.generate()
515534
project.generate_docs()
516535
mock_plugin.generate.assert_called_once_with(project)
@@ -778,7 +797,7 @@ def test_init_resource(project):
778797
assert f.read() == b"\n"
779798

780799

781-
def test_generate_hook_handlers(project, tmpdir):
800+
def test_generate_hook_handlers(project, tmpdir, session):
782801
project.type_name = "Test::Handler::Test"
783802
project.artifact_type = ARTIFACT_TYPE_HOOK
784803
expected_actions = {"preCreateAction", "preDeleteAction"}
@@ -790,7 +809,9 @@ def test_generate_hook_handlers(project, tmpdir):
790809
}
791810
project.root = tmpdir
792811
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
794815
project.generate()
795816

796817
role_path = project.root / "hook-role.yaml"
@@ -820,7 +841,10 @@ def test_generate_hook_handlers_deny_all(project, tmpdir, schema):
820841
project.schema = schema
821842
project.root = tmpdir
822843
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()
824848
project.generate()
825849

826850
role_path = project.root / "hook-role.yaml"
@@ -854,13 +878,17 @@ def test_generate_hook_handlers_deny_all(project, tmpdir, schema):
854878
({"handlers": {"preCreate": {"timeoutInMinutes": 90}, "preDelete": {}}}, 8400),
855879
),
856880
)
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+
):
858884
project.type_name = "Test::Handler::Test"
859885
project.artifact_type = ARTIFACT_TYPE_HOOK
860886
project.schema = schema
861887
project.root = tmpdir
862888
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
864892
project.generate()
865893

866894
role_path = project.root / "hook-role.yaml"
@@ -1400,7 +1428,7 @@ def test_submit_dry_run_hooks(project):
14001428

14011429

14021430
# 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):
14041432
schema = {
14051433
"typeName": "AWS::FOO::BAR",
14061434
"description": "test schema",
@@ -1466,13 +1494,13 @@ def test_submit_dry_run_hooks_with_target_info(project):
14661494
patch_upload = patch.object(project, "_upload", autospec=True)
14671495
patch_path = patch("rpdk.core.project.Path", return_value=zip_path)
14681496
patch_temp = patch("rpdk.core.project.TemporaryFile", autospec=True)
1469-
1497+
patch_session = patch("rpdk.core.boto_helpers.Boto3Session")
14701498
# fmt: off
14711499
# these context managers can't be wrapped by black, but it removes the \
14721500
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:
14741502
mock_plugin.get_plugin_information = MagicMock(return_value=PLUGIN_INFORMATION)
1475-
1503+
mock_session.return_value = session
14761504
project.submit(
14771505
True,
14781506
endpoint_url=None,
@@ -2196,10 +2224,12 @@ def test__get_docs_gettable_atts_good_path():
21962224
assert getatt == [{"name": "Id2", "description": "foo"}]
21972225

21982226

2199-
def test_generate_image_build_config(project):
2227+
def test_generate_image_build_config(project, session):
22002228
project.schema = {}
22012229
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
22032233
project.generate_image_build_config()
22042234
mock_plugin.generate_image_build_config.assert_called_once()
22052235

0 commit comments

Comments
 (0)