Skip to content

Commit 340d2ff

Browse files
authored
[Monitor][Ingestion] Add additional tests (Azure#28746)
- Test cases added for aborting an upload via error handler - Test cases added for > 1MB data uploads Signed-off-by: Paul Van Eck <[email protected]>
1 parent ca7e98f commit 340d2ff

File tree

4 files changed

+126
-6
lines changed

4 files changed

+126
-6
lines changed

sdk/monitor/azure-monitor-ingestion/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "python",
44
"TagPrefix": "python/monitor/azure-monitor-ingestion",
5-
"Tag": "python/monitor/azure-monitor-ingestion_70c5ade383"
5+
"Tag": "python/monitor/azure-monitor-ingestion_12276a5674"
66
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
# IN THE SOFTWARE.
2424
#
2525
# --------------------------------------------------------------------------
26+
from datetime import datetime
27+
2628
import pytest
2729

2830
from devtools_testutils.sanitizers import (
@@ -80,3 +82,17 @@ def monitor_info(environment_variables):
8082
"dce": environment_variables.get(ENV_DCE),
8183
"dcr_id": environment_variables.get(ENV_DCR_ID)
8284
}
85+
86+
87+
@pytest.fixture(scope="session")
88+
def large_data():
89+
logs = []
90+
content = "a" * (1024 * 100) # 100 KiB string
91+
92+
# Ensure total size is > 2 MiB data
93+
for i in range(24):
94+
logs.append({
95+
"Time": datetime.now().isoformat(),
96+
"AdditionalContext": content
97+
})
98+
return logs

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import gzip
77
import json
88
import os
9+
from unittest import mock
910
import uuid
1011

1112
import pytest
@@ -43,6 +44,12 @@ def test_send_logs(self, recorded_test, monitor_info):
4344
with client:
4445
client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=LOGS_BODY)
4546

47+
def test_send_logs_large(self, recorded_test, monitor_info, large_data):
48+
client = self.create_client_from_credential(
49+
LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce'])
50+
with client:
51+
client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=large_data)
52+
4653
def test_send_logs_error(self, recorded_test, monitor_info):
4754
client = self.create_client_from_credential(
4855
LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce'])
@@ -92,3 +99,44 @@ def test_send_logs_gzip_file(self, monitor_info):
9299
with open(temp_file, 'rb') as f:
93100
client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=f)
94101
os.remove(temp_file)
102+
103+
104+
def test_abort_error_handler(self, monitor_info):
105+
client = self.create_client_from_credential(
106+
LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce'])
107+
108+
class TestException(Exception):
109+
pass
110+
111+
def on_error(e):
112+
on_error.called = True
113+
if isinstance(e.error, RuntimeError):
114+
raise TestException("Abort")
115+
return
116+
117+
on_error.called = False
118+
119+
with client:
120+
# No exception should be raised
121+
with mock.patch("azure.monitor.ingestion._operations._patch.GeneratedOps.upload",
122+
side_effect=ConnectionError):
123+
client.upload(
124+
rule_id=monitor_info['dcr_id'],
125+
stream_name=monitor_info['stream_name'],
126+
logs=LOGS_BODY,
127+
on_error=on_error)
128+
129+
assert on_error.called
130+
131+
on_error.called = False
132+
# Exception should now be raised since error handler checked for RuntimeError.
133+
with mock.patch("azure.monitor.ingestion._operations._patch.GeneratedOps.upload",
134+
side_effect=RuntimeError):
135+
with pytest.raises(TestException):
136+
client.upload(
137+
rule_id=monitor_info['dcr_id'],
138+
stream_name=monitor_info['stream_name'],
139+
logs=LOGS_BODY,
140+
on_error=on_error)
141+
142+
assert on_error.called

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

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import gzip
77
import os
88
import json
9+
from unittest import mock
910
import uuid
1011

1112
import pytest
@@ -44,7 +45,17 @@ async def test_send_logs_async(self, recorded_test, monitor_info):
4445
LogsIngestionClient, credential, endpoint=monitor_info['dce'])
4546
async with client:
4647
await client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=LOGS_BODY)
47-
credential.close()
48+
await credential.close()
49+
50+
@pytest.mark.asyncio
51+
async def test_send_logs_large(self, recorded_test, monitor_info, large_data):
52+
credential = self.get_credential(LogsIngestionClient, is_async=True)
53+
client = self.create_client_from_credential(
54+
LogsIngestionClient, credential, endpoint=monitor_info['dce'])
55+
async with client:
56+
await client.upload(
57+
rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=large_data)
58+
await credential.close()
4859

4960
@pytest.mark.asyncio
5061
async def test_send_logs_error(self, recorded_test, monitor_info):
@@ -56,7 +67,7 @@ async def test_send_logs_error(self, recorded_test, monitor_info):
5667
with pytest.raises(HttpResponseError) as ex:
5768
async with client:
5869
await client.upload(rule_id='bad-rule', stream_name=monitor_info['stream_name'], logs=body)
59-
credential.close()
70+
await credential.close()
6071

6172
@pytest.mark.asyncio
6273
async def test_send_logs_error_custom(self, recorded_test, monitor_info):
@@ -76,7 +87,7 @@ async def on_error(e):
7687
await client.upload(
7788
rule_id='bad-rule', stream_name=monitor_info['stream_name'], logs=body, on_error=on_error)
7889
assert on_error.called
79-
credential.close()
90+
await credential.close()
8091

8192
@pytest.mark.asyncio
8293
async def test_send_logs_json_file(self, recorded_test, monitor_info):
@@ -92,7 +103,7 @@ async def test_send_logs_json_file(self, recorded_test, monitor_info):
92103
with open(temp_file, 'r') as f:
93104
await client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=f)
94105
os.remove(temp_file)
95-
credential.close()
106+
await credential.close()
96107

97108
@pytest.mark.asyncio
98109
@pytest.mark.live_test_only("Issues recording binary streams with test-proxy")
@@ -109,4 +120,49 @@ async def test_send_logs_gzip_file(self, monitor_info):
109120
with open(temp_file, 'rb') as f:
110121
await client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=f)
111122
os.remove(temp_file)
112-
credential.close()
123+
await credential.close()
124+
125+
126+
@pytest.mark.asyncio
127+
async def test_abort_error_handler(self, monitor_info):
128+
credential = self.get_credential(LogsIngestionClient, is_async=True)
129+
client = self.create_client_from_credential(
130+
LogsIngestionClient, credential, endpoint=monitor_info['dce'])
131+
body = [{"foo": "bar"}]
132+
133+
class TestException(Exception):
134+
pass
135+
136+
async def on_error(e):
137+
on_error.called = True
138+
if isinstance(e.error, RuntimeError):
139+
raise TestException("Abort")
140+
return
141+
142+
on_error.called = False
143+
144+
async with client:
145+
# No exception should be raised
146+
with mock.patch("azure.monitor.ingestion.aio._operations._patch.GeneratedOps.upload",
147+
side_effect=ConnectionError):
148+
await client.upload(
149+
rule_id=monitor_info['dcr_id'],
150+
stream_name=monitor_info['stream_name'],
151+
logs=LOGS_BODY,
152+
on_error=on_error)
153+
154+
assert on_error.called
155+
156+
on_error.called = False
157+
# Exception should now be raised since error handler checked for RuntimeError.
158+
with mock.patch("azure.monitor.ingestion.aio._operations._patch.GeneratedOps.upload",
159+
side_effect=RuntimeError):
160+
with pytest.raises(TestException):
161+
await client.upload(
162+
rule_id=monitor_info['dcr_id'],
163+
stream_name=monitor_info['stream_name'],
164+
logs=LOGS_BODY,
165+
on_error=on_error)
166+
167+
assert on_error.called
168+
await credential.close()

0 commit comments

Comments
 (0)