Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use preparers for monitor #19307

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions sdk/monitor/azure-monitor-query/tests/preparers.py
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions sdk/monitor/azure-monitor-query/tests/test_logs_single_query.py
Original file line number Diff line number Diff line change
@@ -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
141 changes: 141 additions & 0 deletions sdk/monitor/test-resources.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
2 changes: 0 additions & 2 deletions sdk/monitor/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'