File: test_logs_ingestion_async.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (66 lines) | stat: -rw-r--r-- 2,806 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE.txt in the project root for
# license information.
# -------------------------------------------------------------------------
import pytest

from azure.core.exceptions import HttpResponseError
from azure.monitor.ingestion.aio import LogsIngestionClient
from devtools_testutils import AzureRecordedTestCase


class TestLogsIngestionClientAsync(AzureRecordedTestCase):

    @pytest.mark.asyncio
    async def test_send_logs_async(self, recorded_test, monitor_info):
        client = self.create_client_from_credential(
            LogsIngestionClient, self.get_credential(LogsIngestionClient, is_async=True), endpoint=monitor_info['dce'])
        async with client:
            body = [
                {
                    "Time": "2021-12-08T23:51:14.1104269Z",
                    "Computer": "Computer1",
                    "AdditionalContext": {
                        "testContextKey": 3,
                        "CounterName": "AppMetric1"
                    }
                },
                {
                    "Time": "2021-12-08T23:51:14.1104269Z",
                    "Computer": "Computer2",
                    "AdditionalContext": {
                        "testContextKey": 2,
                        "CounterName": "AppMetric1"
                    }
                }
            ]
            await client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=body)

    @pytest.mark.asyncio
    async def test_send_logs_error(self, recorded_test, monitor_info):
        client = self.create_client_from_credential(
            LogsIngestionClient, self.get_credential(LogsIngestionClient, is_async=True), endpoint=monitor_info['dce'])
        body = [{"foo": "bar"}]

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

    @pytest.mark.asyncio
    async def test_send_logs_error_custom(self, recorded_test, monitor_info):
        client = self.create_client_from_credential(
            LogsIngestionClient, self.get_credential(LogsIngestionClient, is_async=True), endpoint=monitor_info['dce'])
        body = [{"foo": "bar"}]

        async def on_error(error, logs):
            on_error.called = True
            assert isinstance(error, HttpResponseError)
            assert logs == body

        on_error.called = False

        async with client:
            await client.upload(
                rule_id='bad-rule', stream_name=monitor_info['stream_name'], logs=body, on_error=on_error)
        assert on_error.called