diff --git a/sdk/monitor/azure-monitor-query/tests/preparers.py b/sdk/monitor/azure-monitor-query/tests/preparers.py new file mode 100644 index 000000000000..d862a1b98da4 --- /dev/null +++ b/sdk/monitor/azure-monitor-query/tests/preparers.py @@ -0,0 +1,50 @@ + +import functools +import inspect + +from azure.identity import DefaultAzureCredential +from devtools_testutils import PowerShellPreparer, is_live + +LogsPreparer = functools.partial( + PowerShellPreparer, + "azure", + workspace_id="fake_log_workspace_id", + secondary_workspace_id="fake_sec_workspace_id", + workspace_key="fake_log_workspace_key", + secondary_workspace_key="fake_sec_workspace_key" +) + +def trim_kwargs_from_test_function(fn, kwargs): + # the next function is the actual test function. the kwargs need to be trimmed so + # that parameters which are not required will not be passed to it. + if not getattr(fn, "__is_preparer", False): + try: + args, _, kw, _, _, _, _ = inspect.getfullargspec(fn) + except AttributeError: + args, _, kw, _ = inspect.getargspec(fn) # pylint: disable=deprecated-method + if kw is None: + args = set(args) + for key in [k for k in kwargs if k not in args]: + del kwargs[key] + +def logs_decorator(func, **kwargs): + @LogsPreparer() + def wrapper(*args, **kwargs): + workspace_id = kwargs.pop("workspace_id") + secondary_workspace_id = kwargs.pop("secondary_workspace_id") + secondary_workspace_key = kwargs.pop("secondary_workspace_key") + workspace_key = kwargs.pop("workspace_key") + key = DefaultAzureCredential() + + kwargs["workspace_id"] = workspace_id + kwargs["secondary_workspace_id"] = secondary_workspace_id + kwargs["credential"] = key + kwargs["workspace_key"] = workspace_key + kwargs["secondary_workspace_key"] = secondary_workspace_key + + trimmed_kwargs = {k: v for k, v in kwargs.items()} + trim_kwargs_from_test_function(func, trimmed_kwargs) + + func(*args, **trimmed_kwargs) + + return wrapper \ No newline at end of file diff --git a/sdk/monitor/azure-monitor-query/tests/test_logs_single_query.py b/sdk/monitor/azure-monitor-query/tests/test_logs_single_query.py new file mode 100644 index 000000000000..d48e4ad958d2 --- /dev/null +++ b/sdk/monitor/azure-monitor-query/tests/test_logs_single_query.py @@ -0,0 +1,34 @@ +# coding: utf-8 + +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +import pytest + +import os +import sys + +from devtools_testutils import AzureTestCase + +from azure.core.exceptions import HttpResponseError +from azure.monitor.query import LogsQueryClient, LogsQueryRequest +from preparers import logs_decorator + + +class LogsSingleQueryTest(AzureTestCase): + @pytest.mark.live_test_only + @logs_decorator + def test_logs_send_single_query(self, workspace_id, secondary_workspace_id, credential, workspace_key, secondary_workspace_key): + client = LogsQueryClient(credential) + query = """AppRequests | + where TimeGenerated > ago(12h) | + summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""" + + # returns LogsQueryResults + response = client.query(workspace_id, query) + + assert response is not None + assert response.tables is not None diff --git a/sdk/monitor/test-resources.json b/sdk/monitor/test-resources.json new file mode 100644 index 000000000000..0f0e47aa3f3b --- /dev/null +++ b/sdk/monitor/test-resources.json @@ -0,0 +1,141 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "baseName": { + "type": "string", + "defaultValue": "[resourceGroup().name]", + "metadata": { + "description": "The base resource name." + } + }, + "location": { + "type": "string", + "defaultValue": "[resourceGroup().location]", + "metadata": { + "description": "The location of the resource. By default, this is the same as the resource group." + } + }, + "testApplicationOid": { + "type": "string", + "metadata": { + "description": "The client OID to grant access to test resources." + } + }, + "testApplicationId": { + "type": "string", + "metadata": { + "description": "The application client ID used to run tests." + } + } + }, + "variables": { + "logReaderRoleId": "73c42c96-874c-492b-b04d-ab87d138a893", + "metricPublisherRoleId": "3913510d-42f4-4e42-8a64-420c390055eb", + "workspaceName": "[concat(parameters('baseName'), '-logs')]", + "secondaryWorkspaceName": "[concat(parameters('baseName'), '-logs2')]" + }, + "resources": [ + { + "name": "[parameters('baseName')]", + "type": "Microsoft.Insights/components", + "location": "[parameters('location')]", + "apiVersion": "2015-05-01", + "properties": { + "Application_Type": "other" + } + }, + { + "type": "Microsoft.OperationalInsights/workspaces", + "name": "[variables('workspaceName')]", + "apiVersion": "2020-08-01", + "location": "[parameters('location')]", + "properties": { + "sku": { + "name": "PerGB2018" + }, + "retentionInDays": 30, + "features": { + "searchVersion": 1, + "legacy": 0, + "enableLogAccessUsingOnlyResourcePermissions": "true" + } + } + }, + { + "type": "Microsoft.OperationalInsights/workspaces", + "name": "[variables('secondaryWorkspaceName')]", + "apiVersion": "2020-08-01", + "location": "[parameters('location')]", + "properties": { + "sku": { + "name": "PerGB2018" + }, + "retentionInDays": 30, + "features": { + "searchVersion": 1, + "legacy": 0, + "enableLogAccessUsingOnlyResourcePermissions": "true" + } + } + }, + { + "type": "Microsoft.Authorization/roleAssignments", + "apiVersion": "2019-04-01-preview", + "name": "[guid(resourceGroup().id, parameters('testApplicationOid'), variables('logReaderRoleId'))]", + "properties": { + "roleDefinitionId": "[resourceId('Microsoft.Authorization/roleDefinitions', variables('logReaderRoleId'))]", + "principalId": "[parameters('testApplicationOid')]", + "scope": "[resourceGroup().id]" + } + }, + { + "type": "Microsoft.Authorization/roleAssignments", + "apiVersion": "2019-04-01-preview", + "name": "[guid(resourceGroup().id, parameters('testApplicationOid'), variables('metricPublisherRoleId'))]", + "properties": { + "roleDefinitionId": "[resourceId('Microsoft.Authorization/roleDefinitions', variables('metricPublisherRoleId'))]", + "principalId": "[parameters('testApplicationOid')]", + "scope": "[resourceGroup().id]" + } + } + ], + "outputs": { + "CONNECTION_STRING": { + "value": "[reference(resourceId('Microsoft.Insights/components', parameters('baseName')), '2015-05-01').ConnectionString]", + "type": "string" + }, + "APPLICATION_ID": { + "value": "[reference(resourceId('Microsoft.Insights/components', parameters('baseName')), '2015-05-01').AppId]", + "type": "string" + }, + "WORKSPACE_ID": { + "value": "[reference(resourceId('Microsoft.OperationalInsights/workspaces', variables('workspaceName')), '2020-10-01').customerId]", + "type": "string" + }, + "SECONDARY_WORKSPACE_ID": { + "value": "[reference(resourceId('Microsoft.OperationalInsights/workspaces', variables('secondaryWorkspaceName')), '2020-10-01').customerId]", + "type": "string" + }, + "WORKSPACE_KEY": { + "value": "[listKeys(resourceId('Microsoft.OperationalInsights/workspaces', variables('workspaceName')), '2020-10-01').primarySharedKey]", + "type": "string" + }, + "SECONDARY_WORKSPACE_KEY": { + "value": "[listKeys(resourceId('Microsoft.OperationalInsights/workspaces', variables('secondaryWorkspaceName')), '2020-10-01').primarySharedKey]", + "type": "string" + }, + "METRICS_RESOURCE_ID": { + "value": "[resourceId('Microsoft.OperationalInsights/workspaces', variables('workspaceName'))]", + "type": "string" + }, + "METRICS_RESOURCE_NAMESPACE": { + "value": "Microsoft.OperationalInsights/workspaces", + "type": "string" + }, + "LOGS_ENDPOINT": { + "value": "https://api.loganalytics.io", + "type": "string" + } + } +} \ No newline at end of file diff --git a/sdk/monitor/tests.yml b/sdk/monitor/tests.yml index b4e25640e0f7..9a7b2f28fa61 100644 --- a/sdk/monitor/tests.yml +++ b/sdk/monitor/tests.yml @@ -11,6 +11,4 @@ stages: AZURE_TENANT_ID: $(aad-azure-sdk-test-tenant-id) AZURE_CLIENT_ID: $(aad-azure-sdk-test-client-id) AZURE_CLIENT_SECRET: $(aad-azure-sdk-test-client-secret) - LOG_WORKSPACE_ID: $(azure-monitor-query-log-workspace) - METRICS_RESOURCE_URI: $(azure-monitor-query-metrics-uri) AZURE_TEST_RUN_LIVE: 'true'