def create_alert_config(self, name): alert_config_name = self.create_random_name(name) if is_live(): self.variables["alert_config_name"] = alert_config_name alert_config = self.client.create_alert_configuration( name=self.variables["alert_config_name"], cross_metrics_operator="AND", metric_alert_configurations=[ MetricAlertConfiguration( detection_configuration_id=self.variables["detection_config_id"], alert_scope=MetricAnomalyAlertScope( scope_type="TopN", top_n_group_in_scope=TopNGroupScope( top=5, period=10, min_top_count=9 ) ), alert_conditions=MetricAnomalyAlertConditions( metric_boundary_condition=MetricBoundaryCondition( direction="Both", companion_metric_id=self.variables["data_feed_metric_id"], lower=1.0, upper=5.0 ) ) ), MetricAlertConfiguration( detection_configuration_id=self.variables["detection_config_id"], alert_scope=MetricAnomalyAlertScope( scope_type="SeriesGroup", series_group_in_scope={'region': 'Beijing'} ), alert_conditions=MetricAnomalyAlertConditions( severity_condition=SeverityCondition( min_alert_severity="Low", max_alert_severity="High" ) ) ), MetricAlertConfiguration( detection_configuration_id=self.variables["detection_config_id"], alert_scope=MetricAnomalyAlertScope( scope_type="WholeSeries" ), alert_conditions=MetricAnomalyAlertConditions( severity_condition=SeverityCondition( min_alert_severity="Low", max_alert_severity="High" ) ) ) ], hook_ids=[] ) if is_live(): self.variables["alert_config_id"] = alert_config.id return alert_config
def __init__(self, **kwargs) -> None: self.azure_keyvault_url = "https://vaultname.vault.azure.net" if is_live(): self.azure_keyvault_url = os.environ["AZURE_KEYVAULT_URL"] self.is_logging_enabled = kwargs.pop("logging_enable", True) if is_live(): os.environ["AZURE_TENANT_ID"] = os.environ["KEYVAULT_TENANT_ID"] os.environ["AZURE_CLIENT_ID"] = os.environ["KEYVAULT_CLIENT_ID"] os.environ["AZURE_CLIENT_SECRET"] = os.environ[ "KEYVAULT_CLIENT_SECRET"]
async def create_email_hook(self, name): email_hook_name = self.create_random_name(name) if is_live(): self.variables["email_hook_name"] = email_hook_name email_hook = await self.client.create_hook(hook=EmailNotificationHook( name=self.variables["email_hook_name"], emails_to_alert=["*****@*****.**"], description="my email hook", external_link="external link")) if is_live(): self.variables["email_hook_id"] = email_hook.id return email_hook
async def create_detection_config(self, name): detection_config_name = self.create_random_name(name) if is_live(): self.variables["detection_config_name"] = detection_config_name detection_config = await self.client.create_detection_configuration( name=self.variables["detection_config_name"], metric_id=self.variables["data_feed_metric_id"], description="My test metric anomaly detection configuration", whole_series_detection_condition=MetricDetectionCondition( condition_operator="AND", smart_detection_condition=SmartDetectionCondition( sensitivity=50, anomaly_detector_direction="Both", suppress_condition=SuppressCondition(min_number=5, min_ratio=5)), hard_threshold_condition=HardThresholdCondition( anomaly_detector_direction="Both", suppress_condition=SuppressCondition(min_number=5, min_ratio=5), lower_bound=0, upper_bound=100), change_threshold_condition=ChangeThresholdCondition( change_percentage=50, shift_point=30, within_range=True, anomaly_detector_direction="Both", suppress_condition=SuppressCondition(min_number=2, min_ratio=2))), series_detection_conditions=[ MetricSingleSeriesDetectionCondition( series_key={ "region": "Beijing", "category": "Shoes Handbags & Sunglasses" }, smart_detection_condition=SmartDetectionCondition( anomaly_detector_direction="Both", sensitivity=63, suppress_condition=SuppressCondition(min_number=1, min_ratio=100))) ], series_group_detection_conditions=[ MetricSeriesGroupDetectionCondition( series_group_key={"region": "Beijing"}, smart_detection_condition=SmartDetectionCondition( anomaly_detector_direction="Both", sensitivity=63, suppress_condition=SuppressCondition(min_number=1, min_ratio=100))) ]) if is_live(): self.variables["detection_config_id"] = detection_config.id return detection_config
def create_data_feed(self, name): name = self.create_random_name(name) if is_live(): self.variables["data_feed_name"] = name data_feed = self.client.create_data_feed( name=self.variables["data_feed_name"], source=SqlServerDataFeedSource( connection_string=os.getenv("METRICS_ADVISOR_SQL_SERVER_CONNECTION_STRING", "metrics_advisor_sql_server_connection_string"), query="select * from adsample2 where Timestamp = @StartTime" ), granularity=DataFeedGranularity( granularity_type="Daily", ), schema=DataFeedSchema( metrics=[ DataFeedMetric(name="cost", description="the cost"), DataFeedMetric(name="revenue", description="the revenue") ], dimensions=[ DataFeedDimension(name="category"), DataFeedDimension(name="region") ], timestamp_column="Timestamp" ), ingestion_settings=DataFeedIngestionSettings( ingestion_begin_time=datetime.datetime(2019, 10, 1), data_source_request_concurrency=0, ingestion_retry_delay=-1, ingestion_start_offset=-1, stop_retry_after=-1, ), admins=["*****@*****.**"], data_feed_description="my first data feed", missing_data_point_fill_settings=DataFeedMissingDataPointFillSettings( fill_type="SmartFilling" ), rollup_settings=DataFeedRollupSettings( rollup_type="NoRollup", rollup_method="None", ), viewers=["viewers"], access_mode="Private", action_link_template="action link template" ) if is_live(): self.variables["data_feed_id"] = data_feed.id self.variables["data_feed_metric_id"] = data_feed.metric_ids['cost'] return data_feed
def pytest_runtest_setup(item): is_live_only_test_marked = bool( [mark for mark in item.iter_markers(name="live_test_only")]) if is_live_only_test_marked: from devtools_testutils import is_live if not is_live(): pytest.skip("live test only") is_playback_test_marked = bool( [mark for mark in item.iter_markers(name="playback_test_only")]) if is_playback_test_marked: from devtools_testutils import is_live if is_live() and os.environ.get('AZURE_SKIP_LIVE_RECORDING', '').lower() == 'true': pytest.skip("playback test only")
async def create_web_hook(self, name): web_hook_name = self.create_random_name(name) if is_live(): self.variables["web_hook_name"] = web_hook_name web_hook = await self.client.create_hook( hook=WebNotificationHook(name=self.variables["web_hook_name"], endpoint="https://httpbin.org/post", description="my web hook", external_link="external link", username="******", password="******")) if is_live(): self.variables["web_hook_id"] = web_hook.id return web_hook
def load_registry(): if not is_live(): return authority = get_authority(os.environ.get("CONTAINERREGISTRY_ENDPOINT")) repos = [ "library/hello-world", "library/alpine", "library/busybox", ] tags = [ [ "library/hello-world:latest", "library/hello-world:v1", "library/hello-world:v2", "library/hello-world:v3", "library/hello-world:v4", ], ["library/alpine"], ["library/busybox"], ] for repo, tag in zip(repos, tags): try: import_image(authority, repo, tag) except Exception as e: print(e)
async def create_resources(self, **kwargs): if not is_live(): return kwargs try: if self.data_feed: self.data_feed = await self.create_data_feed("datafeed") if self.detection_config: self.detection_config = await self.create_detection_config( "detectionconfig") if self.alert_config: self.alert_config = await self.create_alert_config( "alertconfig") if self.email_hook: self.email_hook = await self.create_email_hook("emailhook") if self.web_hook: self.web_hook = await self.create_web_hook("web_hook") except Exception as e: try: await self.client.delete_data_feed( self.variables["data_feed_id"]) except KeyError: pass raise e
def add_sanitizers(test_proxy): add_remove_header_sanitizer(headers="Ocp-Apim-Subscription-Key") add_remove_header_sanitizer(headers="Retry-After") add_general_regex_sanitizer( value="fakeendpoint", regex="(?<=\\/\\/)[a-z-]+(?=\\.cognitiveservices\\.azure\\.com)" ) add_general_regex_sanitizer( regex="(?<=\\/\\/)[a-z]+(?=(?:|-secondary)\\.(?:table|blob|queue)\\.core\\.windows\\.net)", value="fakeendpoint", ) add_oauth_response_sanitizer() # run tests yield # Dogfood env uses a static storage account so we clean up the blob resources # This is unnecessary for AzureCloud where each storage account is deleted at the end of testing if is_live() and os.getenv("TRANSLATION_ENVIRONMENT") == "Dogfood": client = BlobServiceClient( "https://" + os.getenv("TRANSLATION_DOCUMENT_STORAGE_NAME") + ".blob.core.windows.net/", os.getenv("TRANSLATION_DOCUMENT_STORAGE_KEY") ) for container in client.list_containers(): client.delete_container(container)
def _tear_down(self): if is_live(): self._delete_all_tables(self.ts) self.test_tables = [] if self.ts._cosmos_endpoint: self.sleep(SLEEP_DELAY) self.ts.close()
async def _preparer_wrapper(test_class, credential, **kwargs): self.create_test_client(credential) await self.create_resources(**kwargs) if is_live(): await fn(test_class, self.client, variables=self.variables) else: await fn(test_class, self.client)
async def wrapper(*args, **kwargs): key = kwargs.pop("tables_primary_storage_account_key") name = kwargs.pop("tables_storage_account_name") key = AzureNamedKeyCredential(key=key, name=name) kwargs["tables_primary_storage_account_key"] = key kwargs["tables_storage_account_name"] = name trimmed_kwargs = {k: v for k, v in kwargs.items()} trim_kwargs_from_test_function(func, trimmed_kwargs) EXPONENTIAL_BACKOFF = 1.5 RETRY_COUNT = 0 try: return await func(*args, **trimmed_kwargs) except HttpResponseError as exc: if exc.status_code != 429: raise print("Retrying: {} {}".format(RETRY_COUNT, EXPONENTIAL_BACKOFF)) while RETRY_COUNT < 6: if is_live(): time.sleep(EXPONENTIAL_BACKOFF) try: return await func(*args, **trimmed_kwargs) except HttpResponseError as exc: print("Retrying: {} {}".format(RETRY_COUNT, EXPONENTIAL_BACKOFF)) EXPONENTIAL_BACKOFF **= 2 RETRY_COUNT += 1 if exc.status_code != 429 or RETRY_COUNT >= 6: raise
def async_create_token_credential(): # type: () -> AsyncFakeTokenCredential or DefaultAzureCredential from devtools_testutils import is_live if not is_live(): from .async_fake_token_credential import AsyncFakeTokenCredential return AsyncFakeTokenCredential() from azure.identity.aio import DefaultAzureCredential return DefaultAzureCredential()
async def _tear_down(self): if is_live(): async for table in self.ts.list_tables(): await self.ts.delete_table(table.name) if self.ts._cosmos_endpoint: self.sleep(SLEEP_DELAY) self.test_tables = [] await self.ts.close()
def create_token_credential(): # type: () -> FakeTokenCredential or DefaultAzureCredential from devtools_testutils import is_live if not is_live(): from .fake_token_credential import FakeTokenCredential return FakeTokenCredential() from azure.identity import DefaultAzureCredential return DefaultAzureCredential()
def patch_sleep(): def immediate_return(_): return if not is_live(): with mock.patch("time.sleep", immediate_return): yield else: yield
def patch_async_sleep(): async def immediate_return(_): return if not is_live(): with mock.patch("asyncio.sleep", immediate_return): yield else: yield
def test_create_user_from_managed_identity(self, connection_string): endpoint, access_key = parse_connection_str(connection_string) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() identity_client = CommunicationIdentityClient(endpoint, credential) user = identity_client.create_user() assert user.identifier is not None
async def test_delete_user_from_managed_identity(self, communication_connection_string): endpoint, access_key = parse_connection_str(communication_connection_string) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() identity_client = CommunicationIdentityClient(endpoint, credential) async with identity_client: user = await identity_client.create_user() await identity_client.delete_user(user) assert user.properties.get('id') is not None
async def test_get_token_from_managed_identity(self, communication_connection_string): endpoint, access_key = parse_connection_str(communication_connection_string) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() identity_client = CommunicationIdentityClient(endpoint, credential) async with identity_client: user = await identity_client.create_user() token_response = await identity_client.get_token(user, scopes=[CommunicationTokenScope.CHAT]) assert user.properties.get('id') is not None assert token_response.token is not None
def test_issue_token_from_managed_identity(self, connection_string): endpoint, access_key = parse_connection_str(connection_string) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() identity_client = CommunicationIdentityClient(endpoint, credential) user = identity_client.create_user() token_response = identity_client.issue_token(user, scopes=["chat"]) assert user.identifier is not None assert token_response.token is not None
def test_create_user_from_managed_identity(self, communication_livetest_dynamic_connection_string): endpoint, access_key = parse_connection_str(communication_livetest_dynamic_connection_string) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() identity_client = CommunicationIdentityClient( endpoint, credential, http_logging_policy=get_http_logging_policy() ) user = identity_client.create_user() assert user.properties.get('id') is not None
def test_send_sms_from_managed_identity(self): endpoint, access_key = parse_connection_str(self.connection_str) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() sms_client = SmsClient(endpoint, credential) # calling send() with sms values sms_responses = sms_client.send(from_=self.phone_number, to=[self.phone_number], message="Hello World via SMS") assert len(sms_responses) == 1 self.verify_successful_sms_response(sms_responses[0])
def test_get_token_for_teams_user_from_managed_identity( self, communication_livetest_dynamic_connection_string): if (self.skip_get_token_for_teams_user_test()): return endpoint, access_key = parse_connection_str( communication_livetest_dynamic_connection_string) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() identity_client = CommunicationIdentityClient( endpoint, credential, http_logging_policy=get_http_logging_policy()) add_token = self.generate_teams_user_aad_token() token_response = identity_client.get_token_for_teams_user(add_token) assert token_response.token is not None
async def test_example_secret_list_operations(self, client, **kwargs): if not is_live(): set_custom_default_matcher(excluded_headers="Authorization") secret_client = client async with secret_client: for i in range(7): secret_name = self.get_resource_name("secret{}".format(i)) await secret_client.set_secret(secret_name, "value{}".format(i)) # [START list_secrets] # gets a list of secrets in the vault secrets = secret_client.list_properties_of_secrets() async for secret in secrets: # the list doesn't include values or versions of the secrets print(secret.id) print(secret.name) print(secret.enabled) # [END list_secrets] # [START list_properties_of_secret_versions] # gets a list of all versions of a secret secret_versions = secret_client.list_properties_of_secret_versions( "secret-name") async for secret in secret_versions: # the list doesn't include the versions' values print(secret.id) print(secret.enabled) print(secret.updated_on) # [END list_properties_of_secret_versions] # [START list_deleted_secrets] # gets a list of deleted secrets (requires soft-delete enabled for the vault) deleted_secrets = secret_client.list_deleted_secrets() async for secret in deleted_secrets: # the list doesn't include values or versions of the deleted secrets print(secret.id) print(secret.name) print(secret.scheduled_purge_date) print(secret.recovery_id) print(secret.deleted_date)
async def test_send_sms_async_from_managed_identity(self): endpoint, access_key = parse_connection_str(self.connection_str) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() sms_client = SmsClient(endpoint, credential) print(sms_client) async with sms_client: # calling send() with sms values sms_response = await sms_client.send( from_phone_number=PhoneNumber(self.phone_number), to_phone_numbers=[PhoneNumber(self.phone_number)], message="Hello World via SMS", send_sms_options=SendSmsOptions(enable_delivery_report=True) ) # optional property assert sms_response.message_id is not None
def test_revoke_tokens_from_managed_identity(self, communication_livetest_dynamic_connection_string): endpoint, access_key = parse_connection_str(communication_livetest_dynamic_connection_string) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() identity_client = CommunicationIdentityClient( endpoint, credential, http_logging_policy=get_http_logging_policy() ) user = identity_client.create_user() token_response = identity_client.get_token(user, scopes=[CommunicationTokenScope.CHAT]) identity_client.revoke_tokens(user) assert user.properties.get('id') is not None assert token_response.token is not None
async def test_send_sms_async_from_managed_identity(self): endpoint, access_key = parse_connection_str(self.connection_str) from devtools_testutils import is_live if not is_live(): credential = FakeTokenCredential() else: credential = DefaultAzureCredential() sms_client = SmsClient(endpoint, credential) async with sms_client: # calling send() with sms values sms_responses = await sms_client.send( from_=self.phone_number, to=[self.phone_number], message="Hello World via SMS", enable_delivery_report=True, # optional property tag="custom-tag") # optional property assert len(sms_responses) is 1 for sms_response in sms_responses: self.verify_sms_response(sms_response)
def load_registry(): if not is_live(): return repos = [ "library/hello-world", "library/alpine", "library/busybox", ] tags = [ [ "library/hello-world:latest", "library/hello-world:v1", "library/hello-world:v2", "library/hello-world:v3", "library/hello-world:v4", ], ["library/alpine"], ["library/busybox"], ] for repo, tag in zip(repos, tags): try: import_image(repo, tag) except Exception as e: print(e)