Skip to content

Commit 62d3e2d

Browse files
authored
[Monitor] Enable sovereign cloud testing (#35209)
This refactors some of the monitor ingestion/query library tests to allow for sovereign cloud testing. China cloud and Government cloud testing are also enabled on the weekly pipelines. Signed-off-by: Paul Van Eck <[email protected]>
1 parent b64cfdd commit 62d3e2d

File tree

6 files changed

+95
-27
lines changed

6 files changed

+95
-27
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See LICENSE.txt in the project root for
4+
# license information.
5+
# -------------------------------------------------------------------------
6+
import os
7+
8+
from devtools_testutils import AzureRecordedTestCase
9+
10+
11+
ENV_MONITOR_ENVIRONMENT = "MONITOR_ENVIRONMENT"
12+
AUDIENCE_MAP = {
13+
"AzureCloud": "https://monitor.azure.com",
14+
"AzureChinaCloud": "https://monitor.azure.cn",
15+
"AzureUSGovernment": "https://monitor.azure.us"
16+
}
17+
18+
19+
class LogsIngestionClientTestCase(AzureRecordedTestCase):
20+
21+
def get_client(self, client_class, credential, **kwargs):
22+
23+
environment = os.getenv(ENV_MONITOR_ENVIRONMENT)
24+
if environment:
25+
audience = AUDIENCE_MAP.get(environment)
26+
kwargs["credential_scopes"] = [f"{audience}/.default"]
27+
28+
return self.create_client_from_credential(client_class, credential, **kwargs)

sdk/monitor/azure-monitor-ingestion/tests/test_logs_ingestion.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
from azure.core.exceptions import HttpResponseError
1515
from azure.monitor.ingestion import LogsIngestionClient
16-
from devtools_testutils import AzureRecordedTestCase
16+
17+
from base_testcase import LogsIngestionClientTestCase
1718

1819

1920
LOGS_BODY = [
@@ -36,30 +37,30 @@
3637
]
3738

3839

39-
class TestLogsIngestionClient(AzureRecordedTestCase):
40+
class TestLogsIngestionClient(LogsIngestionClientTestCase):
4041

4142
def test_send_logs(self, recorded_test, monitor_info):
42-
client = self.create_client_from_credential(
43+
client = self.get_client(
4344
LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce'])
4445
with client:
4546
client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=LOGS_BODY)
4647

4748
def test_send_logs_large(self, recorded_test, monitor_info, large_data):
48-
client = self.create_client_from_credential(
49+
client = self.get_client(
4950
LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce'])
5051
with client:
5152
client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=large_data)
5253

5354
def test_send_logs_error(self, recorded_test, monitor_info):
54-
client = self.create_client_from_credential(
55+
client = self.get_client(
5556
LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce'])
5657
body = [{"foo": "bar"}]
5758

5859
with pytest.raises(HttpResponseError) as ex:
5960
client.upload(rule_id='bad-rule', stream_name=monitor_info['stream_name'], logs=body)
6061

6162
def test_send_logs_error_custom(self, recorded_test, monitor_info):
62-
client = self.create_client_from_credential(
63+
client = self.get_client(
6364
LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce'])
6465
body = [{"foo": "bar"}]
6566

@@ -75,7 +76,7 @@ def on_error(e):
7576
assert on_error.called
7677

7778
def test_send_logs_json_file(self, recorded_test, monitor_info):
78-
client = self.create_client_from_credential(
79+
client = self.get_client(
7980
LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce'])
8081

8182
temp_file = str(uuid.uuid4()) + '.json'
@@ -89,7 +90,7 @@ def test_send_logs_json_file(self, recorded_test, monitor_info):
8990

9091
@pytest.mark.live_test_only("Issues recording binary streams with test-proxy")
9192
def test_send_logs_gzip_file(self, monitor_info):
92-
client = self.create_client_from_credential(
93+
client = self.get_client(
9394
LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce'])
9495

9596
temp_file = str(uuid.uuid4()) + '.json.gz'
@@ -101,7 +102,7 @@ def test_send_logs_gzip_file(self, monitor_info):
101102
os.remove(temp_file)
102103

103104
def test_abort_error_handler(self, monitor_info):
104-
client = self.create_client_from_credential(
105+
client = self.get_client(
105106
LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce'])
106107

107108
class TestException(Exception):
@@ -142,7 +143,7 @@ def on_error(e):
142143

143144
@pytest.mark.parametrize("logs", ['[{"foo": "bar"}]', "foo", {"foo": "bar"}, None])
144145
def test_invalid_logs_format(self, monitor_info, logs):
145-
client = self.create_client_from_credential(
146+
client = self.get_client(
146147
LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce'])
147148

148149
with pytest.raises(ValueError):

sdk/monitor/azure-monitor-ingestion/tests/test_logs_ingestion_async.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
from azure.core.exceptions import HttpResponseError
1515
from azure.monitor.ingestion.aio import LogsIngestionClient
16-
from devtools_testutils import AzureRecordedTestCase
16+
17+
from base_testcase import LogsIngestionClientTestCase
1718

1819

1920
LOGS_BODY = [
@@ -36,12 +37,12 @@
3637
]
3738

3839

39-
class TestLogsIngestionClientAsync(AzureRecordedTestCase):
40+
class TestLogsIngestionClientAsync(LogsIngestionClientTestCase):
4041

4142
@pytest.mark.asyncio
4243
async def test_send_logs_async(self, recorded_test, monitor_info):
4344
credential = self.get_credential(LogsIngestionClient, is_async=True)
44-
client = self.create_client_from_credential(
45+
client = self.get_client(
4546
LogsIngestionClient, credential, endpoint=monitor_info['dce'])
4647
async with client:
4748
await client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=LOGS_BODY)
@@ -50,7 +51,7 @@ async def test_send_logs_async(self, recorded_test, monitor_info):
5051
@pytest.mark.asyncio
5152
async def test_send_logs_large(self, recorded_test, monitor_info, large_data):
5253
credential = self.get_credential(LogsIngestionClient, is_async=True)
53-
client = self.create_client_from_credential(
54+
client = self.get_client(
5455
LogsIngestionClient, credential, endpoint=monitor_info['dce'])
5556
async with client:
5657
await client.upload(
@@ -60,7 +61,7 @@ async def test_send_logs_large(self, recorded_test, monitor_info, large_data):
6061
@pytest.mark.asyncio
6162
async def test_send_logs_error(self, recorded_test, monitor_info):
6263
credential = self.get_credential(LogsIngestionClient, is_async=True)
63-
client = self.create_client_from_credential(
64+
client = self.get_client(
6465
LogsIngestionClient, credential, endpoint=monitor_info['dce'])
6566
body = [{"foo": "bar"}]
6667

@@ -72,7 +73,7 @@ async def test_send_logs_error(self, recorded_test, monitor_info):
7273
@pytest.mark.asyncio
7374
async def test_send_logs_error_custom(self, recorded_test, monitor_info):
7475
credential = self.get_credential(LogsIngestionClient, is_async=True)
75-
client = self.create_client_from_credential(
76+
client = self.get_client(
7677
LogsIngestionClient, credential, endpoint=monitor_info['dce'])
7778
body = [{"foo": "bar"}]
7879

@@ -92,7 +93,7 @@ async def on_error(e):
9293
@pytest.mark.asyncio
9394
async def test_send_logs_json_file(self, recorded_test, monitor_info):
9495
credential = self.get_credential(LogsIngestionClient, is_async=True)
95-
client = self.create_client_from_credential(
96+
client = self.get_client(
9697
LogsIngestionClient, credential, endpoint=monitor_info['dce'])
9798

9899
temp_file = str(uuid.uuid4()) + '.json'
@@ -109,7 +110,7 @@ async def test_send_logs_json_file(self, recorded_test, monitor_info):
109110
@pytest.mark.live_test_only("Issues recording binary streams with test-proxy")
110111
async def test_send_logs_gzip_file(self, monitor_info):
111112
credential = self.get_credential(LogsIngestionClient, is_async=True)
112-
client = self.create_client_from_credential(
113+
client = self.get_client(
113114
LogsIngestionClient, credential, endpoint=monitor_info['dce'])
114115

115116
temp_file = str(uuid.uuid4()) + '.json.gz'
@@ -125,7 +126,7 @@ async def test_send_logs_gzip_file(self, monitor_info):
125126
@pytest.mark.asyncio
126127
async def test_abort_error_handler(self, monitor_info):
127128
credential = self.get_credential(LogsIngestionClient, is_async=True)
128-
client = self.create_client_from_credential(
129+
client = self.get_client(
129130
LogsIngestionClient, credential, endpoint=monitor_info['dce'])
130131

131132
class TestException(Exception):
@@ -169,7 +170,7 @@ async def on_error(e):
169170
@pytest.mark.parametrize("logs", ['[{"foo": "bar"}]', "foo", {"foo": "bar"}, None])
170171
async def test_invalid_logs_format(self, monitor_info, logs):
171172
credential = self.get_credential(LogsIngestionClient, is_async=True)
172-
client = self.create_client_from_credential(LogsIngestionClient, credential, endpoint=monitor_info['dce'])
173+
client = self.get_client(LogsIngestionClient, credential, endpoint=monitor_info['dce'])
173174

174175
async with client:
175176
with pytest.raises(ValueError):

sdk/monitor/azure-monitor-query/tests/base_testcase.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@
1818
"AzureUSGovernment": "https://api.loganalytics.us/v1"
1919
}
2020

21+
METRICS_CLIENT_ENVIRONMENT_AUDIENCE_MAP = {
22+
"AzureCloud": "https://metrics.monitor.azure.com",
23+
"AzureChinaCloud": "https://metrics.monitor.azure.cn",
24+
"AzureUSGovernment": "https:/metrics.monitor.azure.us"
25+
}
26+
27+
TLD_MAP = {
28+
"AzureCloud": "com",
29+
"AzureChinaCloud": "cn",
30+
"AzureUSGovernment": "us"
31+
}
32+
2133

2234
class AzureMonitorQueryLogsTestCase(AzureRecordedTestCase):
2335

@@ -47,9 +59,15 @@ class MetricsClientTestCase(AzureRecordedTestCase):
4759

4860
def get_client(self, client_class, credential, endpoint = None):
4961

62+
environment = os.getenv(ENV_MONITOR_ENVIRONMENT)
5063
kwargs = {}
64+
tld = "com"
65+
if environment:
66+
kwargs["audience"] = METRICS_CLIENT_ENVIRONMENT_AUDIENCE_MAP.get(environment)
67+
tld = TLD_MAP.get(environment, "com")
68+
5169
if not endpoint:
5270
region = os.getenv(ENV_MONITOR_LOCATION) or "westus2"
53-
kwargs["endpoint"] = f"https://{region}.metrics.monitor.azure.com"
71+
kwargs["endpoint"] = f"https://{region}.metrics.monitor.azure.{tld}"
5472

5573
return self.create_client_from_credential(client_class, credential, **kwargs)

sdk/monitor/test-resources-post.ps1

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ $clientSecret = $DeploymentOutputs['MONITOR_CLIENT_SECRET']
1414
$dcrImmutableId = $DeploymentOutputs['AZURE_MONITOR_DCR_ID']
1515
$dceEndpoint = $DeploymentOutputs['AZURE_MONITOR_DCE']
1616
$streamName = $DeploymentOutputs['AZURE_MONITOR_STREAM_NAME']
17+
$environment = $DeploymentOutputs['MONITOR_ENVIRONMENT']
18+
$authorityHost = $DeploymentOutputs['AZURE_AUTHORITY_HOST']
1719

1820
##################
1921
### Step 0: Wait for role assignment to propagate
@@ -24,10 +26,19 @@ Start-Sleep -s 180
2426
##################
2527
### Step 1: Obtain a bearer token used later to authenticate against the DCE.
2628
##################
27-
$scope= [System.Web.HttpUtility]::UrlEncode("https://monitor.azure.com//.default")
29+
# Audience Mappings
30+
$audienceMappings = @{
31+
"AzureCloud" = "https://monitor.azure.com";
32+
"AzureUSGovernment" = "https://monitor.azure.us";
33+
"AzureChinaCloud" = "https://monitor.azure.cn";
34+
}
35+
36+
$audience = $audienceMappings[$environment]
37+
38+
$scope= [System.Web.HttpUtility]::UrlEncode("$audience/.default")
2839
$body = "client_id=$clientId&scope=$scope&client_secret=$clientSecret&grant_type=client_credentials";
2940
$headers = @{"Content-Type"="application/x-www-form-urlencoded"};
30-
$uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
41+
$uri = "$authorityHost/$tenantId/oauth2/v2.0/token"
3142
$bearerToken = (Invoke-RestMethod -Uri $uri -Method "Post" -Body $body -Headers $headers).access_token
3243

3344
##################
@@ -68,12 +79,12 @@ $staticData = @"
6879
##################
6980
$body = $staticData;
7081
$headers = @{"Authorization"="Bearer $bearerToken";"Content-Type"="application/json"};
71-
$uri = "$dceEndpoint/dataCollectionRules/$dcrImmutableId/streams/${streamName}?api-version=2021-11-01-preview"
72-
$uri2 = "$dceEndpoint/dataCollectionRules/$dcrImmutableId/streams/${streamName}2?api-version=2021-11-01-preview"
82+
$uri = "$dceEndpoint/dataCollectionRules/$dcrImmutableId/streams/${streamName}?api-version=2023-01-01"
83+
$uri2 = "$dceEndpoint/dataCollectionRules/$dcrImmutableId/streams/${streamName}2?api-version=2023-01-01"
7384

7485
Write-Host "Sending sample data..."
75-
Invoke-RestMethod -Uri $uri -Method "Post" -Body $body -Headers $headers -TimeoutSec 20 -MaximumRetryCount 3
76-
Invoke-RestMethod -Uri $uri2 -Method "Post" -Body $body -Headers $headers -TimeoutSec 20 -MaximumRetryCount 3
86+
Invoke-RestMethod -Uri $uri -Method "Post" -Body $body -Headers $headers -TimeoutSec 40 -MaximumRetryCount 3
87+
Invoke-RestMethod -Uri $uri2 -Method "Post" -Body $body -Headers $headers -TimeoutSec 40 -MaximumRetryCount 3
7788

7889
##################
7990
### Step 4: Sleep to allow time for data to reflect in the workspace tables.

sdk/monitor/tests.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ extends:
66
ServiceDirectory: monitor
77
TestTimeoutInMinutes: 300
88
BuildTargetingString: azure-monitor-*
9+
SupportedClouds: 'Public,UsGov,China'
10+
CloudConfig:
11+
Public:
12+
SubscriptionConfiguration: $(sub-config-azure-cloud-test-resources)
13+
UsGov:
14+
SubscriptionConfiguration: $(sub-config-gov-test-resources)
15+
China:
16+
SubscriptionConfiguration: $(sub-config-cn-test-resources)
17+
Location: chinanorth3
918
EnvVars:
1019
AZURE_SUBSCRIPTION_ID: $(MONITOR_SUBSCRIPTION_ID)
1120
AZURE_TENANT_ID: $(MONITOR_TENANT_ID)

0 commit comments

Comments
 (0)