| 1 |
# Copyright (c) Ansible project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import json |
| 8 |
import time |
| 9 |
import unittest |
| 10 |
import unittest.mock |
| 11 |
import urllib |
| 12 |
|
| 13 |
import pytest |
| 14 |
from ansible.executor.task_result import TaskResult |
| 15 |
from ansible.release import __version__ as ansible_release |
| 16 |
|
| 17 |
from ansible_collections.community.general.plugins.callback.loganalytics_ingestion import ( |
| 18 |
AzureLogAnalyticsIngestionSource, |
| 19 |
) |
| 20 |
|
| 21 |
if tuple(int(x) for x in ansible_release.split(".")[:2]) >= (2, 21): |
| 22 |
# https://github.com/ansible/ansible/issues/86761 |
| 23 |
pytest.skip("Temporarily skipping callback tests for ansible-core >= 2.21", allow_module_level=True) |
| 24 |
|
| 25 |
|
| 26 |
class TestAzureLogAnalyticsIngestion(unittest.TestCase): |
| 27 |
dce_url = "https://fake.dce_url.ansible.com" |
| 28 |
dcr_id = "fake-dcr-id" |
| 29 |
client_id = "fake-client_id" |
| 30 |
client_secret = "fake-client-secret" |
| 31 |
tenant_id = "fake-tenant-id" |
| 32 |
stream_name = "fake-stream-name" |
| 33 |
fake_access_token = None |
| 34 |
|
| 35 |
def setUp(self): |
| 36 |
self.fake_access_token = json.dumps( |
| 37 |
{"expires_in": time.time() + 3600, "access_token": "fake_access_token"} |
| 38 |
).encode("utf-8") |
| 39 |
|
| 40 |
@unittest.mock.patch( |
| 41 |
"ansible_collections.community.general.plugins.callback.loganalytics_ingestion.open_url", autospec=True |
| 42 |
) |
| 43 |
@unittest.mock.patch("ansible.executor.task_result.TaskResult") |
| 44 |
def test_sending_data(self, task_result_mock, open_url_mock): |
| 45 |
""" |
| 46 |
Tests sending data by verifying that the expected POST requests are submitted to the expected hosts. |
| 47 |
""" |
| 48 |
# The data returned from 'open_url' is only ever read during authentication. |
| 49 |
open_url_mock.return_value.read.return_value = self.fake_access_token |
| 50 |
|
| 51 |
# TODO: How to set plugin default arguments? |
| 52 |
# I tried instantiating the 'CallbackModule' but all it ever did was complain that 'client_id' wasn't defined. |
| 53 |
self.loganalytics = AzureLogAnalyticsIngestionSource( |
| 54 |
self.dce_url, |
| 55 |
self.dcr_id, |
| 56 |
3, |
| 57 |
True, |
| 58 |
self.client_id, |
| 59 |
self.client_secret, |
| 60 |
self.tenant_id, |
| 61 |
self.stream_name, |
| 62 |
False, |
| 63 |
False, |
| 64 |
2, |
| 65 |
"community.general.loganalytics_ingestion", |
| 66 |
) |
| 67 |
|
| 68 |
assert open_url_mock.call_count == 1 |
| 69 |
url = urllib.parse.urlparse(open_url_mock.call_args_list[0][0][0]) |
| 70 |
assert url.netloc == "login.microsoftonline.com" |
| 71 |
|
| 72 |
results = ["foo", "bar", "biz"] |
| 73 |
for i, result in enumerate(results, start=1): |
| 74 |
host_mock = unittest.mock.Mock("host_mock") |
| 75 |
host_mock.name = "fake-name" |
| 76 |
task_mock = unittest.mock.Mock("task_mock") |
| 77 |
task_mock._role = "fake-role" |
| 78 |
task_mock.get_name = lambda r=result: r |
| 79 |
|
| 80 |
task_result = TaskResult( |
| 81 |
host=host_mock, task=task_mock, return_data={}, task_fields={"action": "fake-action", "args": {}} |
| 82 |
) |
| 83 |
self.loganalytics.send_to_loganalytics("fake-playbook", task_result, "OK") |
| 84 |
|
| 85 |
assert open_url_mock.call_count == 1 + i |
| 86 |
|
| 87 |
url = urllib.parse.urlparse(open_url_mock.call_args_list[i][0][0]) |
| 88 |
assert url.scheme + "://" + url.netloc == self.dce_url |
| 89 |
|
| 90 |
assert json.loads(open_url_mock.call_args_list[i].kwargs.get("data"))[0].get("TaskName") == result |