| 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 unittest |
| 9 |
from datetime import datetime |
| 10 |
from unittest.mock import Mock, patch |
| 11 |
|
| 12 |
import pytest |
| 13 |
from ansible.executor.task_result import TaskResult |
| 14 |
from ansible.release import __version__ as ansible_release |
| 15 |
|
| 16 |
from ansible_collections.community.general.plugins.callback.loganalytics import AzureLogAnalyticsSource |
| 17 |
|
| 18 |
if tuple(int(x) for x in ansible_release.split(".")[:2]) >= (2, 21): |
| 19 |
# https://github.com/ansible/ansible/issues/86761 |
| 20 |
pytest.skip("Temporarily skipping callback tests for ansible-core >= 2.21", allow_module_level=True) |
| 21 |
|
| 22 |
|
| 23 |
class TestAzureLogAnalytics(unittest.TestCase): |
| 24 |
@patch("ansible_collections.community.general.plugins.callback.loganalytics.socket") |
| 25 |
def setUp(self, mock_socket): |
| 26 |
mock_socket.gethostname.return_value = "my-host" |
| 27 |
mock_socket.gethostbyname.return_value = "1.2.3.4" |
| 28 |
self.loganalytics = AzureLogAnalyticsSource() |
| 29 |
self.mock_task = Mock("MockTask") |
| 30 |
self.mock_task._role = "myrole" |
| 31 |
self.mock_task._uuid = "myuuid" |
| 32 |
self.task_fields = {"args": {}} |
| 33 |
self.mock_host = Mock("MockHost") |
| 34 |
self.mock_host.name = "myhost" |
| 35 |
|
| 36 |
@patch("ansible_collections.community.general.plugins.callback.loganalytics.now") |
| 37 |
@patch("ansible_collections.community.general.plugins.callback.loganalytics.open_url") |
| 38 |
def test_overall(self, open_url_mock, mock_now): |
| 39 |
mock_now.return_value = datetime(2020, 12, 1) |
| 40 |
result = TaskResult(host=self.mock_host, task=self.mock_task, return_data={}, task_fields=self.task_fields) |
| 41 |
|
| 42 |
self.loganalytics.send_event( |
| 43 |
workspace_id="01234567-0123-0123-0123-01234567890a", |
| 44 |
shared_key="dZD0kCbKl3ehZG6LHFMuhtE0yHiFCmetzFMc2u+roXIUQuatqU924SsAAAAPemhjbGlAemhjbGktTUJQAQIDBA==", |
| 45 |
state="OK", |
| 46 |
result=result, |
| 47 |
runtime=100, |
| 48 |
) |
| 49 |
|
| 50 |
args, kwargs = open_url_mock.call_args |
| 51 |
sent_data = json.loads(args[1]) |
| 52 |
|
| 53 |
self.assertEqual(sent_data["event"]["timestamp"], "Tue, 01 Dec 2020 00:00:00 GMT") |
| 54 |
self.assertEqual(sent_data["event"]["host"], "my-host") |
| 55 |
self.assertEqual(sent_data["event"]["uuid"], "myuuid") |
| 56 |
self.assertEqual( |
| 57 |
args[0], |
| 58 |
"https://01234567-0123-0123-0123-01234567890a.ods.opinsights.azure.com/api/logs?api-version=2016-04-01", |
| 59 |
) |
| 60 |
|
| 61 |
@patch("ansible_collections.community.general.plugins.callback.loganalytics.now") |
| 62 |
@patch("ansible_collections.community.general.plugins.callback.loganalytics.open_url") |
| 63 |
def test_auth_headers(self, open_url_mock, mock_now): |
| 64 |
mock_now.return_value = datetime(2020, 12, 1) |
| 65 |
result = TaskResult(host=self.mock_host, task=self.mock_task, return_data={}, task_fields=self.task_fields) |
| 66 |
|
| 67 |
self.loganalytics.send_event( |
| 68 |
workspace_id="01234567-0123-0123-0123-01234567890a", |
| 69 |
shared_key="dZD0kCbKl3ehZG6LHFMuhtE0yHiFCmetzFMc2u+roXIUQuatqU924SsAAAAPemhjbGlAemhjbGktTUJQAQIDBA==", |
| 70 |
state="OK", |
| 71 |
result=result, |
| 72 |
runtime=100, |
| 73 |
) |
| 74 |
|
| 75 |
args, kwargs = open_url_mock.call_args |
| 76 |
headers = kwargs["headers"] |
| 77 |
|
| 78 |
self.assertRegex(headers["Authorization"], r"^SharedKey 01234567-0123-0123-0123-01234567890a:.*=$") |
| 79 |
self.assertEqual(headers["Log-Type"], "ansible_playbook") |