2022-08-08 14:24:58 +02:00
|
|
|
# Copyright (c) Ansible project
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2021-03-30 12:12:26 +02:00
|
|
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
from ansible.executor.task_result import TaskResult
|
|
|
|
from ansible_collections.community.general.tests.unit.compat import unittest
|
2023-02-12 21:05:08 +01:00
|
|
|
from ansible_collections.community.general.tests.unit.compat.mock import patch, Mock
|
2021-03-30 12:12:26 +02:00
|
|
|
from ansible_collections.community.general.plugins.callback.loganalytics import AzureLogAnalyticsSource
|
|
|
|
|
2024-04-20 09:26:08 +02:00
|
|
|
from datetime import datetime
|
2021-03-30 12:12:26 +02:00
|
|
|
import json
|
2023-10-04 23:23:11 +02:00
|
|
|
import sys
|
2021-03-30 12:12:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestAzureLogAnalytics(unittest.TestCase):
|
|
|
|
@patch('ansible_collections.community.general.plugins.callback.loganalytics.socket')
|
|
|
|
def setUp(self, mock_socket):
|
|
|
|
mock_socket.gethostname.return_value = 'my-host'
|
|
|
|
mock_socket.gethostbyname.return_value = '1.2.3.4'
|
|
|
|
self.loganalytics = AzureLogAnalyticsSource()
|
|
|
|
self.mock_task = Mock('MockTask')
|
|
|
|
self.mock_task._role = 'myrole'
|
|
|
|
self.mock_task._uuid = 'myuuid'
|
|
|
|
self.task_fields = {'args': {}}
|
|
|
|
self.mock_host = Mock('MockHost')
|
|
|
|
self.mock_host.name = 'myhost'
|
|
|
|
|
2023-10-04 23:23:11 +02:00
|
|
|
# Add backward compatibility
|
|
|
|
if sys.version_info < (3, 2):
|
|
|
|
self.assertRegex = self.assertRegexpMatches
|
|
|
|
|
2024-04-20 09:26:08 +02:00
|
|
|
@patch('ansible_collections.community.general.plugins.callback.loganalytics.now')
|
2021-03-30 12:12:26 +02:00
|
|
|
@patch('ansible_collections.community.general.plugins.callback.loganalytics.open_url')
|
2024-04-20 09:26:08 +02:00
|
|
|
def test_overall(self, open_url_mock, mock_now):
|
|
|
|
mock_now.return_value = datetime(2020, 12, 1)
|
2021-03-30 12:12:26 +02:00
|
|
|
result = TaskResult(host=self.mock_host, task=self.mock_task, return_data={}, task_fields=self.task_fields)
|
|
|
|
|
|
|
|
self.loganalytics.send_event(workspace_id='01234567-0123-0123-0123-01234567890a',
|
|
|
|
shared_key='dZD0kCbKl3ehZG6LHFMuhtE0yHiFCmetzFMc2u+roXIUQuatqU924SsAAAAPemhjbGlAemhjbGktTUJQAQIDBA==',
|
|
|
|
state='OK',
|
|
|
|
result=result,
|
|
|
|
runtime=100)
|
|
|
|
|
|
|
|
args, kwargs = open_url_mock.call_args
|
|
|
|
sent_data = json.loads(args[1])
|
|
|
|
|
|
|
|
self.assertEqual(sent_data['event']['timestamp'], 'Tue, 01 Dec 2020 00:00:00 GMT')
|
|
|
|
self.assertEqual(sent_data['event']['host'], 'my-host')
|
|
|
|
self.assertEqual(sent_data['event']['uuid'], 'myuuid')
|
|
|
|
self.assertEqual(args[0], 'https://01234567-0123-0123-0123-01234567890a.ods.opinsights.azure.com/api/logs?api-version=2016-04-01')
|
|
|
|
|
2024-04-20 09:26:08 +02:00
|
|
|
@patch('ansible_collections.community.general.plugins.callback.loganalytics.now')
|
2021-03-30 12:12:26 +02:00
|
|
|
@patch('ansible_collections.community.general.plugins.callback.loganalytics.open_url')
|
2024-04-20 09:26:08 +02:00
|
|
|
def test_auth_headers(self, open_url_mock, mock_now):
|
|
|
|
mock_now.return_value = datetime(2020, 12, 1)
|
2021-03-30 12:12:26 +02:00
|
|
|
result = TaskResult(host=self.mock_host, task=self.mock_task, return_data={}, task_fields=self.task_fields)
|
|
|
|
|
|
|
|
self.loganalytics.send_event(workspace_id='01234567-0123-0123-0123-01234567890a',
|
|
|
|
shared_key='dZD0kCbKl3ehZG6LHFMuhtE0yHiFCmetzFMc2u+roXIUQuatqU924SsAAAAPemhjbGlAemhjbGktTUJQAQIDBA==',
|
|
|
|
state='OK',
|
|
|
|
result=result,
|
|
|
|
runtime=100)
|
|
|
|
|
|
|
|
args, kwargs = open_url_mock.call_args
|
|
|
|
headers = kwargs['headers']
|
|
|
|
|
2023-10-04 23:23:11 +02:00
|
|
|
self.assertRegex(headers['Authorization'], r'^SharedKey 01234567-0123-0123-0123-01234567890a:.*=$')
|
2021-03-30 12:12:26 +02:00
|
|
|
self.assertEqual(headers['Log-Type'], 'ansible_playbook')
|