diff --git a/sdk/appconfiguration/azure-appconfiguration-provider/assets.json b/sdk/appconfiguration/azure-appconfiguration-provider/assets.json index 712bcd27c378..53b4c344feef 100644 --- a/sdk/appconfiguration/azure-appconfiguration-provider/assets.json +++ b/sdk/appconfiguration/azure-appconfiguration-provider/assets.json @@ -2,5 +2,5 @@ "AssetsRepo": "Azure/azure-sdk-assets", "AssetsRepoPrefixPath": "python", "TagPrefix": "python/appconfiguration/azure-appconfiguration-provider", - "Tag": "python/appconfiguration/azure-appconfiguration-provider_05f22217b9" + "Tag": "python/appconfiguration/azure-appconfiguration-provider_e80b715d7c" } diff --git a/sdk/appconfiguration/azure-appconfiguration-provider/tests/aio/key_vault/test_async_secret_refresh.py b/sdk/appconfiguration/azure-appconfiguration-provider/tests/aio/key_vault/test_async_secret_refresh.py index 0b485c103ee5..fdef46b338fd 100644 --- a/sdk/appconfiguration/azure-appconfiguration-provider/tests/aio/key_vault/test_async_secret_refresh.py +++ b/sdk/appconfiguration/azure-appconfiguration-provider/tests/aio/key_vault/test_async_secret_refresh.py @@ -10,6 +10,7 @@ from devtools_testutils import EnvironmentVariableLoader from devtools_testutils.aio import recorded_by_proxy_async from asynctestcase import AppConfigTestCase +from testcase import create_secret_config_setting from test_constants import ( APPCONFIGURATION_ENDPOINT_STRING, APPCONFIGURATION_KEYVAULT_SECRET_URL, @@ -82,35 +83,30 @@ async def test_secret_refresh_with_updated_values( ): """Test that secrets are refreshed with updated values.""" mock_callback = Mock() - - # Create client with the mock secret resolver - client = await self.create_client( - endpoint=appconfiguration_endpoint_string, - selects={SettingSelector(key_filter="*", label_filter="prod")}, - keyvault_secret_url=appconfiguration_keyvault_secret_url, - keyvault_secret_url2=appconfiguration_keyvault_secret_url2, - on_refresh_success=mock_callback, - refresh_on=[WatchKey("secret", "prod")], - refresh_interval=1, - secret_refresh_interval=1, # Using a short interval for testing - ) - - # Add a key vault reference to the client (this will use mock resolver) + secret_key = f"{self.get_resource_name('test')}-secret" appconfig_client = self.create_appconfig_client(appconfiguration_endpoint_string) - - # Get and modify a key vault reference setting - kv_setting = await appconfig_client.get_configuration_setting(key="secret", label="prod") - assert kv_setting is not None - - # Verify initial value from mock resolver - assert client["secret"] == "Very secret value" - assert kv_setting is not None - assert isinstance(kv_setting, SecretReferenceConfigurationSetting) - # Update the secret_id (which is the value for SecretReferenceConfigurationSetting) - kv_setting.secret_id = appconfiguration_keyvault_secret_url2 - await appconfig_client.set_configuration_setting(kv_setting) - + kv_setting = create_secret_config_setting(secret_key, "prod", appconfiguration_keyvault_secret_url) + secret_created = False try: + await appconfig_client.set_configuration_setting(kv_setting) + secret_created = True + + client = await self.create_client( + endpoint=appconfiguration_endpoint_string, + selects={SettingSelector(key_filter=secret_key, label_filter="prod")}, + keyvault_secret_url=appconfiguration_keyvault_secret_url, + keyvault_secret_url2=appconfiguration_keyvault_secret_url2, + on_refresh_success=mock_callback, + refresh_on=[WatchKey(secret_key, "prod")], + refresh_interval=1, + secret_refresh_interval=1, + ) + + assert client[secret_key] == "Very secret value" + assert isinstance(kv_setting, SecretReferenceConfigurationSetting) + kv_setting.secret_id = appconfiguration_keyvault_secret_url2 + await appconfig_client.set_configuration_setting(kv_setting) + # Expire the refresh timers to simulate time passing client._refresh_timer._next_refresh_time = 0 client._secret_provider.secret_refresh_timer._next_refresh_time = 0 @@ -119,11 +115,11 @@ async def test_secret_refresh_with_updated_values( await client.refresh() # Verify the value was updated - assert client["secret"] == "Very secret value 2" + assert client[secret_key] == "Very secret value 2" assert mock_callback.call_count >= 1 finally: - kv_setting.secret_id = appconfiguration_keyvault_secret_url - await appconfig_client.set_configuration_setting(kv_setting) + if secret_created: + await appconfig_client.delete_configuration_setting(key=secret_key, label="prod") @AppConfigProviderPreparer() @recorded_by_proxy_async diff --git a/sdk/appconfiguration/azure-appconfiguration-provider/tests/aio/test_async_provider_refresh.py b/sdk/appconfiguration/azure-appconfiguration-provider/tests/aio/test_async_provider_refresh.py index 4c95caa94cd9..5d720ec0f158 100644 --- a/sdk/appconfiguration/azure-appconfiguration-provider/tests/aio/test_async_provider_refresh.py +++ b/sdk/appconfiguration/azure-appconfiguration-provider/tests/aio/test_async_provider_refresh.py @@ -17,8 +17,8 @@ APPCONFIGURATION_KEYVAULT_SECRET_URL, FEATURE_MANAGEMENT_KEY, ) -from azure.appconfiguration import ConfigurationSetting -from azure.appconfiguration.provider import WatchKey +from azure.appconfiguration import ConfigurationSetting, FeatureFlagConfigurationSetting +from azure.appconfiguration.provider import SettingSelector, WatchKey AppConfigProviderPreparer = functools.partial( EnvironmentVariableLoader, @@ -39,71 +39,75 @@ class TestAppConfigurationProvider(AppConfigTestCase, unittest.TestCase): @pytest.mark.asyncio async def test_refresh(self, appconfiguration_endpoint_string, appconfiguration_keyvault_secret_url): mock_callback = Mock() - async with await self.create_client( - endpoint=appconfiguration_endpoint_string, - keyvault_secret_url=appconfiguration_keyvault_secret_url, - refresh_on=[WatchKey("refresh_message")], - refresh_interval=1, - on_refresh_success=mock_callback, - feature_flag_enabled=True, - feature_flag_refresh_enabled=True, - ) as client: - assert client["refresh_message"] == "original value" - assert client["my_json"]["key"] == "value" - assert FEATURE_MANAGEMENT_KEY in client - assert has_feature_flag(client, "Alpha") - - appconfig_client = self.create_appconfig_client(appconfiguration_endpoint_string) - - setting = await appconfig_client.get_configuration_setting(key="refresh_message") - setting.value = "updated value" - feature_flag = await appconfig_client.get_configuration_setting(key=".appconfig.featureflag/Alpha") - feature_flag.enabled = True - await appconfig_client.set_configuration_setting(setting) - await appconfig_client.set_configuration_setting(feature_flag) - - # Expire the refresh timers to simulate time passing - client._refresh_timer._next_refresh_time = 0 - client._feature_flag_refresh_timer._next_refresh_time = 0 - - await client.refresh() - assert client["refresh_message"] == "updated value" - assert has_feature_flag(client, "Alpha", True) - assert mock_callback.call_count == 1 - - setting.value = "original value" - feature_flag.enabled = False - await appconfig_client.set_configuration_setting(setting) - await appconfig_client.set_configuration_setting(feature_flag) - - # Expire the refresh timers to simulate time passing - client._refresh_timer._next_refresh_time = 0 - client._feature_flag_refresh_timer._next_refresh_time = 0 - - await client.refresh() - assert client["refresh_message"] == "original value" - assert has_feature_flag(client, "Alpha", False) - assert mock_callback.call_count == 2 - - setting.value = "updated value 2" - feature_flag.enabled = True - await appconfig_client.set_configuration_setting(setting) - await appconfig_client.set_configuration_setting(feature_flag) - - # Not waiting for the refresh interval to pass - await client.refresh() - assert client["refresh_message"] == "original value" - assert has_feature_flag(client, "Alpha", False) - assert mock_callback.call_count == 2 - - setting.value = "original value" - feature_flag.enabled = False + appconfig_client = self.create_appconfig_client(appconfiguration_endpoint_string) + test_prefix = self.get_resource_name("test") + refresh_key = f"{test_prefix}-refresh-message" + feature_id = f"{test_prefix}-alpha" + setting = ConfigurationSetting(key=refresh_key, value="original value") + feature_flag = FeatureFlagConfigurationSetting(feature_id=feature_id, enabled=False) + setting_created = False + feature_flag_created = False + try: await appconfig_client.set_configuration_setting(setting) + setting_created = True await appconfig_client.set_configuration_setting(feature_flag) - - await client.refresh() - assert client["refresh_message"] == "original value" - assert mock_callback.call_count == 2 + feature_flag_created = True + + async with await self.create_client( + endpoint=appconfiguration_endpoint_string, + keyvault_secret_url=appconfiguration_keyvault_secret_url, + selects=[SettingSelector(key_filter=refresh_key)], + refresh_on=[WatchKey(refresh_key)], + refresh_interval=1, + on_refresh_success=mock_callback, + feature_flag_enabled=True, + feature_flag_refresh_enabled=True, + feature_flag_selectors=[SettingSelector(key_filter=feature_id)], + ) as client: + assert client[refresh_key] == "original value" + assert FEATURE_MANAGEMENT_KEY in client + assert has_feature_flag(client, feature_id) + + setting.value = "updated value" + feature_flag.enabled = True + await appconfig_client.set_configuration_setting(setting) + await appconfig_client.set_configuration_setting(feature_flag) + + client._refresh_timer._next_refresh_time = 0 + client._feature_flag_refresh_timer._next_refresh_time = 0 + await client.refresh() + assert client[refresh_key] == "updated value" + assert has_feature_flag(client, feature_id, True) + assert mock_callback.call_count == 1 + + setting.value = "original value" + feature_flag.enabled = False + await appconfig_client.set_configuration_setting(setting) + await appconfig_client.set_configuration_setting(feature_flag) + + client._refresh_timer._next_refresh_time = 0 + client._feature_flag_refresh_timer._next_refresh_time = 0 + await client.refresh() + assert client[refresh_key] == "original value" + assert has_feature_flag(client, feature_id, False) + assert mock_callback.call_count == 2 + + setting.value = "updated value 2" + feature_flag.enabled = True + await appconfig_client.set_configuration_setting(setting) + await appconfig_client.set_configuration_setting(feature_flag) + + await client.refresh() + assert client[refresh_key] == "original value" + assert has_feature_flag(client, feature_id, False) + assert mock_callback.call_count == 2 + finally: + try: + if feature_flag_created: + await appconfig_client.delete_configuration_setting(key=feature_flag.key) + finally: + if setting_created: + await appconfig_client.delete_configuration_setting(key=refresh_key) # method: refresh @AppConfigProviderPreparer() @@ -111,57 +115,51 @@ async def test_refresh(self, appconfiguration_endpoint_string, appconfiguration_ @pytest.mark.skipif(sys.version_info < (3, 8), reason="Python 3.7 does not support AsyncMock") @pytest.mark.asyncio async def test_no_refresh(self, appconfiguration_endpoint_string, appconfiguration_keyvault_secret_url): - appconfig_client = self.create_appconfig_client(appconfiguration_endpoint_string) - - watch_key = ConfigurationSetting(key="watch key", value="0") - await appconfig_client.set_configuration_setting(watch_key) - + test_prefix = self.get_resource_name("test") + refresh_key = f"{test_prefix}-refresh-message" + watch_key_name = f"{test_prefix}-watch-key" + setting = ConfigurationSetting(key=refresh_key, value="original value") + watch_key = ConfigurationSetting(key=watch_key_name, value="0") mock_callback = Mock() - async with await self.create_client( - endpoint=appconfiguration_endpoint_string, - keyvault_secret_url=appconfiguration_keyvault_secret_url, - refresh_on=[WatchKey("watch key")], - refresh_interval=1, - on_refresh_success=mock_callback, - feature_flag_enabled=True, - feature_flag_refresh_enabled=True, - ) as client: - assert client["refresh_message"] == "original value" - assert client["my_json"]["key"] == "value" - assert FEATURE_MANAGEMENT_KEY in client - assert has_feature_flag(client, "Alpha") - - setting = await appconfig_client.get_configuration_setting(key="refresh_message") - setting.value = "updated value" + setting_created = False + watch_key_created = False + try: await appconfig_client.set_configuration_setting(setting) - - # Expire the refresh timers to simulate time passing - client._refresh_timer._next_refresh_time = 0 - client._feature_flag_refresh_timer._next_refresh_time = 0 - - await client.refresh() - # No Change the Watch Key wasn't updated - assert client["refresh_message"] == "original value" - assert has_feature_flag(client, "Alpha", False) - assert mock_callback.call_count == 0 - - watch_key.value = "1" + setting_created = True await appconfig_client.set_configuration_setting(watch_key) - - # Expire the refresh timers to simulate time passing - client._refresh_timer._next_refresh_time = 0 - client._feature_flag_refresh_timer._next_refresh_time = 0 - - await client.refresh() - assert client["refresh_message"] == "updated value" - assert has_feature_flag(client, "Alpha", False) - assert mock_callback.call_count == 1 - - # Reset modified settings - setting.value = "original value" - await appconfig_client.set_configuration_setting(setting) - await appconfig_client.delete_configuration_setting(key="watch key") + watch_key_created = True + + async with await self.create_client( + endpoint=appconfiguration_endpoint_string, + keyvault_secret_url=appconfiguration_keyvault_secret_url, + selects=[SettingSelector(key_filter=refresh_key)], + refresh_on=[WatchKey(watch_key_name)], + refresh_interval=1, + on_refresh_success=mock_callback, + ) as client: + assert client[refresh_key] == "original value" + + setting.value = "updated value" + await appconfig_client.set_configuration_setting(setting) + client._refresh_timer._next_refresh_time = 0 + await client.refresh() + assert client[refresh_key] == "original value" + assert mock_callback.call_count == 0 + + watch_key.value = "1" + await appconfig_client.set_configuration_setting(watch_key) + client._refresh_timer._next_refresh_time = 0 + await client.refresh() + assert client[refresh_key] == "updated value" + assert mock_callback.call_count == 1 + finally: + try: + if watch_key_created: + await appconfig_client.delete_configuration_setting(key=watch_key_name) + finally: + if setting_created: + await appconfig_client.delete_configuration_setting(key=refresh_key) @AppConfigProviderPreparer() @recorded_by_proxy_async @@ -169,37 +167,34 @@ async def test_no_refresh(self, appconfiguration_endpoint_string, appconfigurati @pytest.mark.asyncio async def test_refresh_disabled(self, appconfiguration_endpoint_string, appconfiguration_keyvault_secret_url): mock_callback = AsyncMock() - async with await self.create_client( - endpoint=appconfiguration_endpoint_string, - keyvault_secret_url=appconfiguration_keyvault_secret_url, - refresh_on=[WatchKey("refresh_message")], - refresh_interval=1, - on_refresh_success=mock_callback, - feature_flag_enabled=True, - feature_flag_refresh_enabled=True, - refresh_enabled=False, - ) as client: - assert client["refresh_message"] == "original value" - assert client["my_json"]["key"] == "value" - assert FEATURE_MANAGEMENT_KEY in client - assert has_feature_flag(client, "Alpha") - - appconfig_client = self.create_appconfig_client(appconfiguration_endpoint_string) - - setting = await appconfig_client.get_configuration_setting(key="refresh_message") - setting.value = "updated value" - await appconfig_client.set_configuration_setting(setting) - - # Expire the refresh timers to simulate time passing - client._refresh_timer._next_refresh_time = 0 - - await client.refresh() - # Refresh is disabled, so the value should not change - assert client["refresh_message"] == "original value" - assert mock_callback.call_count == 0 - - setting.value = "original value" + appconfig_client = self.create_appconfig_client(appconfiguration_endpoint_string) + refresh_key = f"{self.get_resource_name('test')}-refresh-message" + setting = ConfigurationSetting(key=refresh_key, value="original value") + setting_created = False + try: await appconfig_client.set_configuration_setting(setting) + setting_created = True + + async with await self.create_client( + endpoint=appconfiguration_endpoint_string, + keyvault_secret_url=appconfiguration_keyvault_secret_url, + selects=[SettingSelector(key_filter=refresh_key)], + refresh_on=[WatchKey(refresh_key)], + refresh_interval=1, + on_refresh_success=mock_callback, + refresh_enabled=False, + ) as client: + assert client[refresh_key] == "original value" + + setting.value = "updated value" + await appconfig_client.set_configuration_setting(setting) + client._refresh_timer._next_refresh_time = 0 + await client.refresh() + assert client[refresh_key] == "original value" + assert mock_callback.call_count == 0 + finally: + if setting_created: + await appconfig_client.delete_configuration_setting(key=refresh_key) except ImportError: pass diff --git a/sdk/appconfiguration/azure-appconfiguration-provider/tests/conftest.py b/sdk/appconfiguration/azure-appconfiguration-provider/tests/conftest.py index 2ee75e620842..15355bfa5141 100644 --- a/sdk/appconfiguration/azure-appconfiguration-provider/tests/conftest.py +++ b/sdk/appconfiguration/azure-appconfiguration-provider/tests/conftest.py @@ -104,16 +104,16 @@ def add_sanitizers(test_proxy): ( os.environ.get( "APPCONFIGURATION_KEYVAULT_SECRET_URL2", - "https://sanitized.vault.azure.net/secrets/fake-secret2/", + "https://sanitized.vault.azure.net/secrets/SecondSecret/", ), - "https://sanitized.vault.azure.net/secrets/fake-secret2/", + "https://sanitized.vault.azure.net/secrets/SecondSecret/", ), ( os.environ.get( "APPCONFIGURATION_KEYVAULT_SECRET_URL", - "https://sanitized.vault.azure.net/secrets/fake-secret/", + "https://sanitized.vault.azure.net/secrets/TestSecret/", ), - "https://sanitized.vault.azure.net/secrets/fake-secret/", + "https://sanitized.vault.azure.net/secrets/TestSecret/", ), ) diff --git a/sdk/appconfiguration/azure-appconfiguration-provider/tests/key_vault/test_secret_refresh.py b/sdk/appconfiguration/azure-appconfiguration-provider/tests/key_vault/test_secret_refresh.py index 4934334ce27d..a60a1b48c995 100644 --- a/sdk/appconfiguration/azure-appconfiguration-provider/tests/key_vault/test_secret_refresh.py +++ b/sdk/appconfiguration/azure-appconfiguration-provider/tests/key_vault/test_secret_refresh.py @@ -8,7 +8,7 @@ import unittest from unittest.mock import Mock, patch from devtools_testutils import EnvironmentVariableLoader, recorded_by_proxy -from testcase import AppConfigTestCase +from testcase import AppConfigTestCase, create_secret_config_setting from test_constants import ( APPCONFIGURATION_ENDPOINT_STRING, APPCONFIGURATION_KEYVAULT_SECRET_URL, @@ -75,35 +75,30 @@ def test_secret_refresh_with_updated_values( ): """Test that secrets are refreshed with updated values.""" mock_callback = Mock() - - # Create client with the mock secret resolver - client = self.create_client( - endpoint=appconfiguration_endpoint_string, - selects={SettingSelector(key_filter="*", label_filter="prod")}, - keyvault_secret_url=appconfiguration_keyvault_secret_url, - keyvault_secret_url2=appconfiguration_keyvault_secret_url2, - on_refresh_success=mock_callback, - refresh_on=[WatchKey("secret", "prod")], - refresh_interval=1, - secret_refresh_interval=1, # Using a short interval for testing - ) - - # Add a key vault reference to the client (this will use mock resolver) + secret_key = f"{self.get_resource_name('test')}-secret" appconfig_client = self.create_appconfig_client(appconfiguration_endpoint_string) - - # Get and modify a key vault reference setting - kv_setting = appconfig_client.get_configuration_setting(key="secret", label="prod") - assert kv_setting is not None - - # Verify initial value from mock resolver - assert client["secret"] == "Very secret value" - assert kv_setting is not None - assert isinstance(kv_setting, SecretReferenceConfigurationSetting) - # Update the secret_id (which is the value for SecretReferenceConfigurationSetting) - kv_setting.secret_id = appconfiguration_keyvault_secret_url2 - appconfig_client.set_configuration_setting(kv_setting) - + kv_setting = create_secret_config_setting(secret_key, "prod", appconfiguration_keyvault_secret_url) + secret_created = False try: + appconfig_client.set_configuration_setting(kv_setting) + secret_created = True + + client = self.create_client( + endpoint=appconfiguration_endpoint_string, + selects={SettingSelector(key_filter=secret_key, label_filter="prod")}, + keyvault_secret_url=appconfiguration_keyvault_secret_url, + keyvault_secret_url2=appconfiguration_keyvault_secret_url2, + on_refresh_success=mock_callback, + refresh_on=[WatchKey(secret_key, "prod")], + refresh_interval=1, + secret_refresh_interval=1, + ) + + assert client[secret_key] == "Very secret value" + assert isinstance(kv_setting, SecretReferenceConfigurationSetting) + kv_setting.secret_id = appconfiguration_keyvault_secret_url2 + appconfig_client.set_configuration_setting(kv_setting) + # Expire the refresh timers to simulate time passing client._refresh_timer._next_refresh_time = 0 client._secret_provider.secret_refresh_timer._next_refresh_time = 0 @@ -112,11 +107,11 @@ def test_secret_refresh_with_updated_values( client.refresh() # Verify the value was updated - assert client["secret"] == "Very secret value 2" + assert client[secret_key] == "Very secret value 2" assert mock_callback.call_count >= 1 finally: - kv_setting.secret_id = appconfiguration_keyvault_secret_url - appconfig_client.set_configuration_setting(kv_setting) + if secret_created: + appconfig_client.delete_configuration_setting(key=secret_key, label="prod") @AppConfigProviderPreparer() @recorded_by_proxy diff --git a/sdk/appconfiguration/azure-appconfiguration-provider/tests/test_constants.py b/sdk/appconfiguration/azure-appconfiguration-provider/tests/test_constants.py index f56583b923e3..e4c29ac19575 100644 --- a/sdk/appconfiguration/azure-appconfiguration-provider/tests/test_constants.py +++ b/sdk/appconfiguration/azure-appconfiguration-provider/tests/test_constants.py @@ -6,5 +6,5 @@ "Secret=lamefakesecretlamefakesecretlamefakesecrett=" # cspell: disable-line ) APPCONFIGURATION_ENDPOINT_STRING = "https://sanitized.azconfig.io" -APPCONFIGURATION_KEYVAULT_SECRET_URL = "https://sanitized.vault.azure.net/secrets/fake-secret/" -APPCONFIGURATION_KEYVAULT_SECRET_URL2 = "https://sanitized.vault.azure.net/secrets/fake-secret2/" +APPCONFIGURATION_KEYVAULT_SECRET_URL = "https://sanitized.vault.azure.net/secrets/TestSecret/" +APPCONFIGURATION_KEYVAULT_SECRET_URL2 = "https://sanitized.vault.azure.net/secrets/SecondSecret/" diff --git a/sdk/appconfiguration/azure-appconfiguration-provider/tests/test_provider_refresh.py b/sdk/appconfiguration/azure-appconfiguration-provider/tests/test_provider_refresh.py index 79dbd0a768ab..7593b4a4756e 100644 --- a/sdk/appconfiguration/azure-appconfiguration-provider/tests/test_provider_refresh.py +++ b/sdk/appconfiguration/azure-appconfiguration-provider/tests/test_provider_refresh.py @@ -13,7 +13,8 @@ APPCONFIGURATION_KEYVAULT_SECRET_URL, FEATURE_MANAGEMENT_KEY, ) -from azure.appconfiguration.provider import WatchKey +from azure.appconfiguration import FeatureFlagConfigurationSetting +from azure.appconfiguration.provider import SettingSelector, WatchKey AppConfigProviderPreparer = functools.partial( EnvironmentVariableLoader, @@ -29,159 +30,154 @@ class TestAppConfigurationProvider(AppConfigTestCase, unittest.TestCase): @recorded_by_proxy def test_refresh(self, appconfiguration_endpoint_string, appconfiguration_keyvault_secret_url): mock_callback = Mock() - client = self.create_client( - endpoint=appconfiguration_endpoint_string, - keyvault_secret_url=appconfiguration_keyvault_secret_url, - refresh_on=[WatchKey("refresh_message")], - refresh_interval=1, - on_refresh_success=mock_callback, - feature_flag_enabled=True, - feature_flag_refresh_enabled=True, - ) - assert client["refresh_message"] == "original value" - assert client["my_json"]["key"] == "value" - assert FEATURE_MANAGEMENT_KEY in client - assert has_feature_flag(client, "Alpha") - appconfig_client = self.create_appconfig_client(appconfiguration_endpoint_string) - - setting = appconfig_client.get_configuration_setting(key="refresh_message") - setting.value = "updated value" - feature_flag = appconfig_client.get_configuration_setting(key=".appconfig.featureflag/Alpha") - feature_flag.enabled = True - appconfig_client.set_configuration_setting(setting) - appconfig_client.set_configuration_setting(feature_flag) - - # Expire the refresh timers to simulate time passing - client._refresh_timer._next_refresh_time = 0 - client._feature_flag_refresh_timer._next_refresh_time = 0 - - client.refresh() - assert client["refresh_message"] == "updated value" - assert has_feature_flag(client, "Alpha", True) - assert mock_callback.call_count == 1 - - setting.value = "original value" - feature_flag.enabled = False - appconfig_client.set_configuration_setting(setting) - appconfig_client.set_configuration_setting(feature_flag) - - # Expire the refresh timers to simulate time passing - client._refresh_timer._next_refresh_time = 0 - client._feature_flag_refresh_timer._next_refresh_time = 0 - - client.refresh() - assert client["refresh_message"] == "original value" - assert has_feature_flag(client, "Alpha", False) - assert mock_callback.call_count == 2 - - setting.value = "updated value 2" - feature_flag.enabled = True - appconfig_client.set_configuration_setting(setting) - appconfig_client.set_configuration_setting(feature_flag) - - # Not waiting for the refresh interval to pass - client.refresh() - assert client["refresh_message"] == "original value" - assert has_feature_flag(client, "Alpha", False) - assert mock_callback.call_count == 2 - - setting.value = "original value" - feature_flag.enabled = False - appconfig_client.set_configuration_setting(setting) - appconfig_client.set_configuration_setting(feature_flag) - - client.refresh() - assert client["refresh_message"] == "original value" - assert mock_callback.call_count == 2 + test_prefix = self.get_resource_name("test") + refresh_key = f"{test_prefix}-refresh-message" + feature_id = f"{test_prefix}-alpha" + setting = ConfigurationSetting(key=refresh_key, value="original value") + feature_flag = FeatureFlagConfigurationSetting(feature_id=feature_id, enabled=False) + setting_created = False + feature_flag_created = False + try: + appconfig_client.set_configuration_setting(setting) + setting_created = True + appconfig_client.set_configuration_setting(feature_flag) + feature_flag_created = True + + client = self.create_client( + endpoint=appconfiguration_endpoint_string, + keyvault_secret_url=appconfiguration_keyvault_secret_url, + selects=[SettingSelector(key_filter=refresh_key)], + refresh_on=[WatchKey(refresh_key)], + refresh_interval=1, + on_refresh_success=mock_callback, + feature_flag_enabled=True, + feature_flag_refresh_enabled=True, + feature_flag_selectors=[SettingSelector(key_filter=feature_id)], + ) + assert client[refresh_key] == "original value" + assert FEATURE_MANAGEMENT_KEY in client + assert has_feature_flag(client, feature_id) + + setting.value = "updated value" + feature_flag.enabled = True + appconfig_client.set_configuration_setting(setting) + appconfig_client.set_configuration_setting(feature_flag) + + client._refresh_timer._next_refresh_time = 0 + client._feature_flag_refresh_timer._next_refresh_time = 0 + client.refresh() + assert client[refresh_key] == "updated value" + assert has_feature_flag(client, feature_id, True) + assert mock_callback.call_count == 1 + + setting.value = "original value" + feature_flag.enabled = False + appconfig_client.set_configuration_setting(setting) + appconfig_client.set_configuration_setting(feature_flag) + + client._refresh_timer._next_refresh_time = 0 + client._feature_flag_refresh_timer._next_refresh_time = 0 + client.refresh() + assert client[refresh_key] == "original value" + assert has_feature_flag(client, feature_id, False) + assert mock_callback.call_count == 2 + + setting.value = "updated value 2" + feature_flag.enabled = True + appconfig_client.set_configuration_setting(setting) + appconfig_client.set_configuration_setting(feature_flag) + + client.refresh() + assert client[refresh_key] == "original value" + assert has_feature_flag(client, feature_id, False) + assert mock_callback.call_count == 2 + finally: + try: + if feature_flag_created: + appconfig_client.delete_configuration_setting(key=feature_flag.key) + finally: + if setting_created: + appconfig_client.delete_configuration_setting(key=refresh_key) @AppConfigProviderPreparer() @recorded_by_proxy def test_no_refresh(self, appconfiguration_endpoint_string, appconfiguration_keyvault_secret_url): - appconfig_client = self.create_appconfig_client(appconfiguration_endpoint_string) - - watch_key = ConfigurationSetting(key="watch key", value="0") - appconfig_client.set_configuration_setting(watch_key) - + test_prefix = self.get_resource_name("test") + refresh_key = f"{test_prefix}-refresh-message" + watch_key_name = f"{test_prefix}-watch-key" + setting = ConfigurationSetting(key=refresh_key, value="original value") + watch_key = ConfigurationSetting(key=watch_key_name, value="0") mock_callback = Mock() - client = self.create_client( - endpoint=appconfiguration_endpoint_string, - keyvault_secret_url=appconfiguration_keyvault_secret_url, - refresh_on=[WatchKey("watch key")], - refresh_interval=1, - on_refresh_success=mock_callback, - feature_flag_enabled=True, - feature_flag_refresh_enabled=True, - ) - assert client["refresh_message"] == "original value" - assert client["my_json"]["key"] == "value" - assert FEATURE_MANAGEMENT_KEY in client - assert has_feature_flag(client, "Alpha") - - setting = appconfig_client.get_configuration_setting(key="refresh_message") - setting.value = "updated value" - appconfig_client.set_configuration_setting(setting) - - # Expire the refresh timers to simulate time passing - client._refresh_timer._next_refresh_time = 0 - client._feature_flag_refresh_timer._next_refresh_time = 0 - - client.refresh() - # No Change the Watch Key wasn't updated - assert client["refresh_message"] == "original value" - assert has_feature_flag(client, "Alpha", False) - assert mock_callback.call_count == 0 - - watch_key.value = "1" - appconfig_client.set_configuration_setting(watch_key) - - # Expire the refresh timers to simulate time passing - client._refresh_timer._next_refresh_time = 0 - client._feature_flag_refresh_timer._next_refresh_time = 0 - - client.refresh() - assert client["refresh_message"] == "updated value" - assert has_feature_flag(client, "Alpha", False) - assert mock_callback.call_count == 1 - - # Reset modified settings - setting.value = "original value" - appconfig_client.set_configuration_setting(setting) - appconfig_client.delete_configuration_setting(key="watch key") + setting_created = False + watch_key_created = False + try: + appconfig_client.set_configuration_setting(setting) + setting_created = True + appconfig_client.set_configuration_setting(watch_key) + watch_key_created = True + + client = self.create_client( + endpoint=appconfiguration_endpoint_string, + keyvault_secret_url=appconfiguration_keyvault_secret_url, + selects=[SettingSelector(key_filter=refresh_key)], + refresh_on=[WatchKey(watch_key_name)], + refresh_interval=1, + on_refresh_success=mock_callback, + ) + assert client[refresh_key] == "original value" + + setting.value = "updated value" + appconfig_client.set_configuration_setting(setting) + client._refresh_timer._next_refresh_time = 0 + client.refresh() + assert client[refresh_key] == "original value" + assert mock_callback.call_count == 0 + + watch_key.value = "1" + appconfig_client.set_configuration_setting(watch_key) + client._refresh_timer._next_refresh_time = 0 + client.refresh() + assert client[refresh_key] == "updated value" + assert mock_callback.call_count == 1 + finally: + try: + if watch_key_created: + appconfig_client.delete_configuration_setting(key=watch_key_name) + finally: + if setting_created: + appconfig_client.delete_configuration_setting(key=refresh_key) @AppConfigProviderPreparer() @recorded_by_proxy def test_empty_refresh(self, appconfiguration_endpoint_string, appconfiguration_keyvault_secret_url): mock_callback = Mock() - client = self.create_client( - endpoint=appconfiguration_endpoint_string, - keyvault_secret_url=appconfiguration_keyvault_secret_url, - refresh_on=[WatchKey("refresh_message")], - refresh_interval=1, - on_refresh_success=mock_callback, - feature_flag_enabled=True, - feature_flag_refresh_enabled=True, - refresh_enabled=False, - ) - assert client["refresh_message"] == "original value" - assert client["my_json"]["key"] == "value" - assert FEATURE_MANAGEMENT_KEY in client - assert has_feature_flag(client, "Alpha") - appconfig_client = self.create_appconfig_client(appconfiguration_endpoint_string) - - setting = appconfig_client.get_configuration_setting(key="refresh_message") - setting.value = "updated value" - appconfig_client.set_configuration_setting(setting) - - # Expire the refresh timers to simulate time passing - client._refresh_timer._next_refresh_time = 0 - - client.refresh() - # Refresh is disabled, so the value should not change - assert client["refresh_message"] == "original value" - assert mock_callback.call_count == 0 - - setting.value = "original value" - appconfig_client.set_configuration_setting(setting) + refresh_key = f"{self.get_resource_name('test')}-refresh-message" + setting = ConfigurationSetting(key=refresh_key, value="original value") + setting_created = False + try: + appconfig_client.set_configuration_setting(setting) + setting_created = True + + client = self.create_client( + endpoint=appconfiguration_endpoint_string, + keyvault_secret_url=appconfiguration_keyvault_secret_url, + selects=[SettingSelector(key_filter=refresh_key)], + refresh_on=[WatchKey(refresh_key)], + refresh_interval=1, + on_refresh_success=mock_callback, + refresh_enabled=False, + ) + assert client[refresh_key] == "original value" + + setting.value = "updated value" + appconfig_client.set_configuration_setting(setting) + client._refresh_timer._next_refresh_time = 0 + client.refresh() + assert client[refresh_key] == "original value" + assert mock_callback.call_count == 0 + finally: + if setting_created: + appconfig_client.delete_configuration_setting(key=refresh_key) diff --git a/sdk/appconfiguration/test-resources.json b/sdk/appconfiguration/test-resources.json index 529337242ddb..dbbbe0b0f361 100644 --- a/sdk/appconfiguration/test-resources.json +++ b/sdk/appconfiguration/test-resources.json @@ -61,7 +61,7 @@ "endpointValue": "[format('https://{0}-{1}{2}', parameters('baseName'), parameters('azConfigPrefix'), parameters('azConfigEndpointSuffix'))]", "azureKeyVaultUrl": "[format('https://{0}{1}/', parameters('baseName'), parameters('keyVaultEndpointSuffix'))]", "azureKeyVaultSecretUrl": "[format('https://{0}{1}/secrets/TestSecret', parameters('baseName'), parameters('keyVaultEndpointSuffix'))]", - "azureKeyVaultSecretUrl2": "[format('https://{0}{1}/secrets/TestSecret2', parameters('baseName'), parameters('keyVaultEndpointSuffix'))]" + "azureKeyVaultSecretUrl2": "[format('https://{0}{1}/secrets/SecondSecret', parameters('baseName'), parameters('keyVaultEndpointSuffix'))]" }, "resources": [ { @@ -127,7 +127,7 @@ }, { "type": "Microsoft.KeyVault/vaults/secrets", - "name": "[concat(parameters('baseName'), '/TestSecret2')]", + "name": "[concat(parameters('baseName'), '/SecondSecret')]", "apiVersion": "2016-10-01", "location": "[parameters('location')]", "dependsOn": [