Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/callback/test_splunk.py

raw
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.splunk import SplunkHTTPCollectorSource
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 TestSplunkClient(unittest.TestCase):
24 @patch("ansible_collections.community.general.plugins.callback.splunk.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.splunk = SplunkHTTPCollectorSource()
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.splunk.now")
37 @patch("ansible_collections.community.general.plugins.callback.splunk.open_url")
38 def test_timestamp_with_milliseconds(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.splunk.send_event(
43 url="endpoint",
44 authtoken="token",
45 validate_certs=False,
46 include_milliseconds=True,
47 batch="abcefghi-1234-5678-9012-abcdefghijkl",
48 state="OK",
49 result=result,
50 runtime=100,
51 )
52
53 args, kwargs = open_url_mock.call_args
54 sent_data = json.loads(args[1])
55
56 self.assertEqual(sent_data["event"]["timestamp"], "2020-12-01 00:00:00.000000 +0000")
57 self.assertEqual(sent_data["event"]["host"], "my-host")
58 self.assertEqual(sent_data["event"]["ip_address"], "1.2.3.4")
59
60 @patch("ansible_collections.community.general.plugins.callback.splunk.now")
61 @patch("ansible_collections.community.general.plugins.callback.splunk.open_url")
62 def test_timestamp_without_milliseconds(self, open_url_mock, mock_now):
63 mock_now.return_value = datetime(2020, 12, 1)
64 result = TaskResult(host=self.mock_host, task=self.mock_task, return_data={}, task_fields=self.task_fields)
65
66 self.splunk.send_event(
67 url="endpoint",
68 authtoken="token",
69 validate_certs=False,
70 include_milliseconds=False,
71 batch="abcefghi-1234-5678-9012-abcdefghijkl",
72 state="OK",
73 result=result,
74 runtime=100,
75 )
76
77 args, kwargs = open_url_mock.call_args
78 sent_data = json.loads(args[1])
79
80 self.assertEqual(sent_data["event"]["timestamp"], "2020-12-01 00:00:00 +0000")
81 self.assertEqual(sent_data["event"]["host"], "my-host")
82 self.assertEqual(sent_data["event"]["ip_address"], "1.2.3.4")