From 95e491049da9a93b4beb6a2bf16949d7a3695309 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Thu, 3 Jun 2021 10:07:46 -0700 Subject: [PATCH] Add tests --- .../monitor/query/_metrics_query_client.py | 11 ++-- .../query/aio/_metrics_query_client_async.py | 12 ++-- .../azure-monitor-query/dev_requirements.txt | 1 + .../samples/sample_metric_definitions.py | 20 ++++++ .../samples/sample_metrics_query_client.py | 3 +- .../tests/test_logs_client.py | 62 +++++++++++++++++++ .../tests/test_metrics_client.py | 36 +++++++++++ .../azure-monitor-query/tests/test_monitor.py | 6 -- sdk/monitor/tests.yml | 13 ++++ 9 files changed, 142 insertions(+), 22 deletions(-) create mode 100644 sdk/monitor/azure-monitor-query/samples/sample_metric_definitions.py create mode 100644 sdk/monitor/azure-monitor-query/tests/test_logs_client.py create mode 100644 sdk/monitor/azure-monitor-query/tests/test_metrics_client.py delete mode 100644 sdk/monitor/azure-monitor-query/tests/test_monitor.py diff --git a/sdk/monitor/azure-monitor-query/azure/monitor/query/_metrics_query_client.py b/sdk/monitor/azure-monitor-query/azure/monitor/query/_metrics_query_client.py index ca74e5ffbf9a..0a3745e1db93 100644 --- a/sdk/monitor/azure-monitor-query/azure/monitor/query/_metrics_query_client.py +++ b/sdk/monitor/azure-monitor-query/azure/monitor/query/_metrics_query_client.py @@ -102,22 +102,19 @@ def list_metric_namespaces(self, resource_uri, **kwargs): """ return self._namespace_op.list(resource_uri, **kwargs) - def list_metric_definitions(self, resource_uri, metricnamespace=None, **kwargs): + def list_metric_definitions(self, resource_uri, metric_namespace=None, **kwargs): # type: (str, str, Any) -> ItemPaged[MetricDefinition] """Lists the metric definitions for the resource. :param resource_uri: The identifier of the resource. :type resource_uri: str - :param metricnamespace: Metric namespace to query metric definitions for. - :type metricnamespace: str + :param metric_namespace: Metric namespace to query metric definitions for. + :type metric_namespace: str :return: An iterator like instance of either MetricDefinitionCollection or the result of cls(response) :rtype: ~azure.core.paging.ItemPaged[~azure.monitor.query.MetricDefinition] :raises: ~azure.core.exceptions.HttpResponseError """ - kwargs.setdefault("metricnamespace", metricnamespace) - return MetricDefinition._from_generated( # pylint: disable=protected-access - self._namespace_op.list(resource_uri, **kwargs) - ) + return self._definitions_op.list(resource_uri, metric_namespace, **kwargs) def close(self): # type: () -> None diff --git a/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_metrics_query_client_async.py b/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_metrics_query_client_async.py index 006df1c8cc07..794561cbaa1b 100644 --- a/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_metrics_query_client_async.py +++ b/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_metrics_query_client_async.py @@ -100,21 +100,19 @@ async def list_metric_namespaces(self, resource_uri, **kwargs): """ return await self._namespace_op.list(resource_uri, **kwargs) - async def list_metric_definitions(self, resource_uri, **kwargs): - # type: (str, Any) -> ItemPaged[MetricDefinition] + async def list_metric_definitions(self, resource_uri, metric_namespace=None, **kwargs): + # type: (str, str, Any) -> ItemPaged[MetricDefinition] """Lists the metric definitions for the resource. :param resource_uri: The identifier of the resource. :type resource_uri: str - :keyword metricnamespace: Metric namespace to query metric definitions for. - :paramtype metricnamespace: str + :param metric_namespace: Metric namespace to query metric definitions for. + :type metric_namespace: str :return: An iterator like instance of either MetricDefinitionCollection or the result of cls(response) :rtype: ~azure.core.paging.ItemPaged[~azure.monitor.query.MetricDefinition] :raises: ~azure.core.exceptions.HttpResponseError """ - return await MetricDefinition._from_generated( # pylint: disable=protected-access - self._namespace_op.list(resource_uri, **kwargs) - ) + return await self._definitions_op.list(resource_uri, metric_namespace, **kwargs) async def __aenter__(self) -> "MetricsClient": await self._client.__aenter__() diff --git a/sdk/monitor/azure-monitor-query/dev_requirements.txt b/sdk/monitor/azure-monitor-query/dev_requirements.txt index 9846116107f7..fdddf49adce6 100644 --- a/sdk/monitor/azure-monitor-query/dev_requirements.txt +++ b/sdk/monitor/azure-monitor-query/dev_requirements.txt @@ -2,4 +2,5 @@ -e ../../../tools/azure-sdk-tools -e ../../core/azure-core -e ../../identity/azure-identity +azure-mgmt-loganalytics aiohttp>=3.0; python_version >= '3.5' diff --git a/sdk/monitor/azure-monitor-query/samples/sample_metric_definitions.py b/sdk/monitor/azure-monitor-query/samples/sample_metric_definitions.py new file mode 100644 index 000000000000..5edf82628405 --- /dev/null +++ b/sdk/monitor/azure-monitor-query/samples/sample_metric_definitions.py @@ -0,0 +1,20 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import os +from azure.monitor.query import MetricsClient +from azure.identity import ClientSecretCredential + +credential = ClientSecretCredential( + client_id = os.environ['AZURE_CLIENT_ID'], + client_secret = os.environ['AZURE_CLIENT_SECRET'], + tenant_id = os.environ['AZURE_TENANT_ID'] + ) + +client = MetricsClient(credential) + +metrics_uri = os.environ['METRICS_RESOURCE_URI'] +response = client.list_metric_definitions(metrics_uri, metric_namespace='microsoft.eventgrid/topics') + +for item in response: + pass diff --git a/sdk/monitor/azure-monitor-query/samples/sample_metrics_query_client.py b/sdk/monitor/azure-monitor-query/samples/sample_metrics_query_client.py index f785d29a08cc..0246556d7e23 100644 --- a/sdk/monitor/azure-monitor-query/samples/sample_metrics_query_client.py +++ b/sdk/monitor/azure-monitor-query/samples/sample_metrics_query_client.py @@ -16,8 +16,7 @@ client = MetricsClient(credential) -# metrics_uri = os.environ['METRICS_RESOURCE_URI'] -metrics_uri = "/subscriptions/faa080af-c1d8-40ad-9cce-e1a450ca5b57/resourceGroups/sabhyrav-resourcegroup/providers/Microsoft.EventGrid/topics/rakshith-cloud" +metrics_uri = os.environ['METRICS_RESOURCE_URI'] response = client.query( metrics_uri, metricnames=["PublishSuccessCount"], diff --git a/sdk/monitor/azure-monitor-query/tests/test_logs_client.py b/sdk/monitor/azure-monitor-query/tests/test_logs_client.py new file mode 100644 index 000000000000..18b55d79942f --- /dev/null +++ b/sdk/monitor/azure-monitor-query/tests/test_logs_client.py @@ -0,0 +1,62 @@ +import pytest +import os +from azure.identity import ClientSecretCredential +from azure.core.exceptions import HttpResponseError +from azure.monitor.query import LogsClient, LogsQueryRequest + +def _credential(): + credential = ClientSecretCredential( + client_id = os.environ['AZURE_CLIENT_ID'], + client_secret = os.environ['AZURE_CLIENT_SECRET'], + tenant_id = os.environ['AZURE_TENANT_ID'] + ) + return credential + +def test_logs_auth(): + credential = _credential() + client = LogsClient(credential) + query = """AppRequests | + where TimeGenerated > ago(12h) | + summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""" + + # returns LogsQueryResults + response = client.query(os.environ['LOG_WORKSPACE_ID'], query) + + assert response is not None + assert response.tables is not None + +def test_logs_server_timeout(): + client = LogsClient(_credential()) + + with pytest.raises(HttpResponseError) as e: + response = client.query( + os.environ['LOG_WORKSPACE_ID'], + "range x from 1 to 10000000000 step 1 | count", + server_timeout=1, + ) + assert e.message.contains('Gateway timeout') + +def test_logs_batch_query(): + client = LogsClient(_credential()) + + requests = [ + LogsQueryRequest( + query="AzureActivity | summarize count()", + timespan="PT1H", + workspace= os.environ['LOG_WORKSPACE_ID'] + ), + LogsQueryRequest( + query= """AppRequests | take 10 | + summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""", + timespan="PT1H", + workspace= os.environ['LOG_WORKSPACE_ID'] + ), + LogsQueryRequest( + query= "AppRequests | take 2", + workspace= os.environ['LOG_WORKSPACE_ID'] + ), + ] + response = client.batch_query(requests) + + assert len(response.responses) == 3 + diff --git a/sdk/monitor/azure-monitor-query/tests/test_metrics_client.py b/sdk/monitor/azure-monitor-query/tests/test_metrics_client.py new file mode 100644 index 000000000000..d0e06c822663 --- /dev/null +++ b/sdk/monitor/azure-monitor-query/tests/test_metrics_client.py @@ -0,0 +1,36 @@ +import pytest +import os +from azure.identity import ClientSecretCredential +from azure.core.exceptions import HttpResponseError +from azure.monitor.query import MetricsClient + +def _credential(): + credential = ClientSecretCredential( + client_id = os.environ['AZURE_CLIENT_ID'], + client_secret = os.environ['AZURE_CLIENT_SECRET'], + tenant_id = os.environ['AZURE_TENANT_ID'] + ) + return credential + +def test_metrics_auth(): + credential = _credential() + client = MetricsClient(credential) + # returns LogsQueryResults + response = client.query(os.environ['METRICS_RESOURCE_URI'], metricnames=["PublishSuccessCount"], timespan='P2D') + + assert response is not None + assert response.metrics is not None + +def test_metrics_namespaces(): + client = MetricsClient(_credential()) + + response = client.list_metric_namespaces(os.environ['METRICS_RESOURCE_URI']) + + assert response is not None + +def test_metrics_definitions(): + client = MetricsClient(_credential()) + + response = client.list_metric_definitions(os.environ['METRICS_RESOURCE_URI'], metric_namespace='microsoft.eventgrid/topics') + + assert response is not None diff --git a/sdk/monitor/azure-monitor-query/tests/test_monitor.py b/sdk/monitor/azure-monitor-query/tests/test_monitor.py deleted file mode 100644 index c222e973a8b2..000000000000 --- a/sdk/monitor/azure-monitor-query/tests/test_monitor.py +++ /dev/null @@ -1,6 +0,0 @@ -import pytest - -def test_placeholder(): - ## just a placeholder test to bypass CI exit code 5 - assert 1 == 1 - \ No newline at end of file diff --git a/sdk/monitor/tests.yml b/sdk/monitor/tests.yml index 6db7facfe0e7..c7fd14b50116 100644 --- a/sdk/monitor/tests.yml +++ b/sdk/monitor/tests.yml @@ -12,3 +12,16 @@ stages: AZURE_CLIENT_ID: $(aad-azure-sdk-test-client-id) AZURE_CLIENT_SECRET: $(aad-azure-sdk-test-client-secret) AZURE_TEST_RUN_LIVE: 'true' + - template: /eng/pipelines/templates/stages/archetype-sdk-tests.yml + parameters: + ServiceDirectory: monitor + TestTimeoutInMinutes: 300 + BuildTargetingString: azure-monitor-query + EnvVars: + AZURE_SUBSCRIPTION_ID: $(azure-subscription-id) + 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'