Skip to content

Commit ff9b657

Browse files
authored
chore: Change FeatureToggleAppConfigConfigProvider to accept AppConfig client, so SAM-T can send a monitored client. (#2480)
1 parent 5730edd commit ff9b657

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

samtranslator/feature_toggle/feature_toggle.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,17 @@ class FeatureToggleAppConfigConfigProvider(FeatureToggleConfigProvider):
129129
"""Feature toggle config provider which loads config from AppConfig."""
130130

131131
@cw_timer(prefix="External", name="AppConfig")
132-
def __init__(self, application_id, environment_id, configuration_profile_id):
132+
def __init__(self, application_id, environment_id, configuration_profile_id, app_config_client=None):
133133
FeatureToggleConfigProvider.__init__(self)
134134
try:
135135
LOG.info("Loading feature toggle config from AppConfig...")
136136
# Lambda function has 120 seconds limit
137137
# (5 + 5) * 2, 20 seconds maximum timeout duration
138138
# In case of high latency from AppConfig, we can always fall back to use an empty config and continue transform
139139
client_config = Config(connect_timeout=5, read_timeout=5, retries={"total_max_attempts": 2})
140-
self.app_config_client = boto3.client("appconfig", config=client_config)
140+
self.app_config_client = (
141+
boto3.client("appconfig", config=client_config) if not app_config_client else app_config_client
142+
)
141143
response = self.app_config_client.get_configuration(
142144
Application=application_id,
143145
Environment=environment_id,

tests/feature_toggle/test_feature_toggle.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,49 @@ def setUp(self):
105105
]
106106
)
107107
@patch("samtranslator.feature_toggle.feature_toggle.boto3")
108+
@patch("samtranslator.feature_toggle.feature_toggle.Config")
108109
def test_feature_toggle_with_appconfig_provider(
109-
self, feature_name, stage, region, account_id, expected, boto3_mock
110+
self, feature_name, stage, region, account_id, expected, config_mock, boto3_mock
110111
):
111112
boto3_mock.client.return_value = self.app_config_mock
113+
config_object_mock = Mock()
114+
config_mock.return_value = config_object_mock
112115
feature_toggle_config_provider = FeatureToggleAppConfigConfigProvider(
113116
"test_app_id", "test_env_id", "test_conf_id"
114117
)
115118
feature_toggle = FeatureToggle(
116119
feature_toggle_config_provider, stage=stage, region=region, account_id=account_id
117120
)
121+
boto3_mock.client.assert_called_once_with("appconfig", config=config_object_mock)
122+
self.assertEqual(feature_toggle.is_enabled(feature_name), expected)
123+
124+
@parameterized.expand(
125+
[
126+
param("feature-1", "beta", "default", "123456789123", False),
127+
param("feature-1", "beta", "us-west-2", "123456789123", True),
128+
param("feature-2", "beta", "us-west-2", "123456789123", False), # because feature is missing
129+
param("feature-1", "beta", "ap-south-1", "123456789124", False), # because default is used
130+
param("feature-1", "alpha", "us-east-1", "123456789123", False), # non-exist stage
131+
param("feature-1", "beta", "us-east-1", "123456789100", True),
132+
param("feature-1", "beta", "us-east-1", "123456789123", False),
133+
# any None for stage, region and account_id returns False
134+
param("feature-1", None, None, None, False),
135+
param("feature-1", "beta", None, None, False),
136+
param("feature-1", "beta", "us-west-2", None, False),
137+
param("feature-1", "beta", None, "123456789123", False),
138+
]
139+
)
140+
@patch("samtranslator.feature_toggle.feature_toggle.boto3")
141+
def test_feature_toggle_with_appconfig_provider_and_app_config_client(
142+
self, feature_name, stage, region, account_id, expected, boto3_mock
143+
):
144+
feature_toggle_config_provider = FeatureToggleAppConfigConfigProvider(
145+
"test_app_id", "test_env_id", "test_conf_id", self.app_config_mock
146+
)
147+
feature_toggle = FeatureToggle(
148+
feature_toggle_config_provider, stage=stage, region=region, account_id=account_id
149+
)
150+
boto3_mock.client.assert_not_called()
118151
self.assertEqual(feature_toggle.is_enabled(feature_name), expected)
119152

120153

0 commit comments

Comments
 (0)