diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-18 05:52:35 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-18 05:52:35 +0000 |
commit | 7fec0b69a082aaeec72fee0612766aa42f6b1b4d (patch) | |
tree | efb569b86ca4da888717f5433e757145fa322e08 /ansible_collections/splunk/es/tests/unit/plugins | |
parent | Releasing progress-linux version 7.7.0+dfsg-3~progress7.99u1. (diff) | |
download | ansible-7fec0b69a082aaeec72fee0612766aa42f6b1b4d.tar.xz ansible-7fec0b69a082aaeec72fee0612766aa42f6b1b4d.zip |
Merging upstream version 9.4.0+dfsg.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ansible_collections/splunk/es/tests/unit/plugins')
6 files changed, 288 insertions, 268 deletions
diff --git a/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_adaptive_response_notable_events.py b/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_adaptive_response_notable_events.py index b6a84fc78..96993c6dc 100644 --- a/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_adaptive_response_notable_events.py +++ b/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_adaptive_response_notable_events.py @@ -18,27 +18,27 @@ from __future__ import absolute_import, division, print_function + __metaclass__ = type from ansible.module_utils.six import PY2 + builtin_import = "builtins.__import__" if PY2: builtin_import = "__builtin__.__import__" import tempfile + from ansible.playbook.task import Task from ansible.template import Templar +from ansible_collections.ansible.utils.tests.unit.compat.mock import MagicMock, patch + from ansible_collections.splunk.es.plugins.action.splunk_adaptive_response_notable_events import ( ActionModule, ) -from ansible_collections.splunk.es.plugins.module_utils.splunk import ( - SplunkRequest, -) -from ansible_collections.ansible.utils.tests.unit.compat.mock import ( - MagicMock, - patch, -) +from ansible_collections.splunk.es.plugins.module_utils.splunk import SplunkRequest + RESPONSE_PAYLOAD = [ { @@ -68,8 +68,8 @@ RESPONSE_PAYLOAD = [ "actions": "notable", }, "name": "Ansible Test", - } - ] + }, + ], }, { "entry": [ @@ -97,8 +97,8 @@ RESPONSE_PAYLOAD = [ "actions": "notable", }, "name": "Ansible Test", - } - ] + }, + ], }, ] @@ -153,7 +153,7 @@ REQUEST_PAYLOAD = [ class TestSplunkEsAdaptiveResponseNotableEvents: - def setup(self): + def setup_method(self): task = MagicMock(Task) # Ansible > 2.13 looks for check_mode in task task.check_mode = False @@ -161,7 +161,7 @@ class TestSplunkEsAdaptiveResponseNotableEvents: # Ansible <= 2.13 looks for check_mode in play_context play_context.check_mode = False connection = patch( - "ansible_collections.splunk.es.plugins.module_utils.splunk.Connection" + "ansible_collections.splunk.es.plugins.module_utils.splunk.Connection", ) connection._socket_path = tempfile.NamedTemporaryFile().name fake_loader = {} @@ -186,8 +186,39 @@ class TestSplunkEsAdaptiveResponseNotableEvents: } @patch("ansible.module_utils.connection.Connection.__rpc__") + def test_es_adaptive_response_notable_events_merged_idempotent( + self, + conn, + monkeypatch, + ): + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name + self._plugin._connection._shell = MagicMock() + + def create_update(self, rest_path, data=None): + return RESPONSE_PAYLOAD[0] + + def get_by_path(self, path): + return RESPONSE_PAYLOAD[0] + + monkeypatch.setattr(SplunkRequest, "create_update", create_update) + monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) + + self._plugin._task.args = { + "state": "merged", + "config": [REQUEST_PAYLOAD[0]], + } + result = self._plugin.run(task_vars=self._task_vars) + # recheck with module + assert ( + result["adaptive_response_notable_events"]["before"][0]["correlation_search_name"] + == "Ansible Test" + ) + + @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_adaptive_response_notable_events_merged_01( - self, connection, monkeypatch + self, + connection, + monkeypatch, ): metadata = { "search": '| tstats summariesonly=true values("Authentication.tag") as "tag",dc("Authentication.user") as "user_count",dc("Authent' @@ -205,9 +236,7 @@ class TestSplunkEsAdaptiveResponseNotableEvents: monkeypatch.setattr(SplunkRequest, "create_update", create_update) - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() self._plugin._task.args = { "state": "merged", @@ -218,7 +247,9 @@ class TestSplunkEsAdaptiveResponseNotableEvents: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_adaptive_response_notable_events_merged_02( - self, connection, monkeypatch + self, + connection, + monkeypatch, ): self._plugin.api_response = RESPONSE_PAYLOAD[0] self._plugin.search_for_resource_name = MagicMock() @@ -232,9 +263,7 @@ class TestSplunkEsAdaptiveResponseNotableEvents: monkeypatch.setattr(SplunkRequest, "create_update", create_update) - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() self._plugin._task.args = { "state": "merged", @@ -245,37 +274,12 @@ class TestSplunkEsAdaptiveResponseNotableEvents: assert result["changed"] is True @patch("ansible.module_utils.connection.Connection.__rpc__") - def test_es_adaptive_response_notable_events_merged_idempotent( - self, conn, monkeypatch - ): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) - self._plugin._connection._shell = MagicMock() - - def create_update(self, rest_path, data=None): - return RESPONSE_PAYLOAD[0] - - def get_by_path(self, path): - return RESPONSE_PAYLOAD[0] - - monkeypatch.setattr(SplunkRequest, "create_update", create_update) - monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) - - self._plugin._task.args = { - "state": "merged", - "config": [REQUEST_PAYLOAD[0]], - } - result = self._plugin.run(task_vars=self._task_vars) - assert result["changed"] is False - - @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_adaptive_response_notable_events_replaced_01( - self, conn, monkeypatch + self, + conn, + monkeypatch, ): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() self._plugin.search_for_resource_name = MagicMock() self._plugin.search_for_resource_name.return_value = ( @@ -305,11 +309,11 @@ class TestSplunkEsAdaptiveResponseNotableEvents: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_adaptive_response_notable_events_replaced_02( - self, conn, monkeypatch + self, + conn, + monkeypatch, ): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() self._plugin.search_for_resource_name = MagicMock() self._plugin.search_for_resource_name.return_value = ( @@ -339,11 +343,11 @@ class TestSplunkEsAdaptiveResponseNotableEvents: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_adaptive_response_notable_events_replaced_idempotent( - self, conn, monkeypatch + self, + conn, + monkeypatch, ): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() def create_update(self, rest_path, data=None): @@ -360,16 +364,18 @@ class TestSplunkEsAdaptiveResponseNotableEvents: "config": [REQUEST_PAYLOAD[0]], } result = self._plugin.run(task_vars=self._task_vars) - - assert result["changed"] is False + assert ( + result["adaptive_response_notable_events"]["before"][0]["correlation_search_name"] + == "Ansible Test" + ) @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_adaptive_response_notable_events_deleted( - self, conn, monkeypatch + self, + conn, + monkeypatch, ): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() self._plugin.search_for_resource_name = MagicMock() @@ -388,7 +394,7 @@ class TestSplunkEsAdaptiveResponseNotableEvents: "config": [ { "correlation_search_name": "Ansible Test", - } + }, ], } result = self._plugin.run(task_vars=self._task_vars) @@ -397,11 +403,10 @@ class TestSplunkEsAdaptiveResponseNotableEvents: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_adaptive_response_notable_events_deleted_idempotent( - self, connection + self, + connection, ): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() self._plugin.search_for_resource_name = MagicMock() self._plugin.search_for_resource_name.return_value = {}, {} @@ -411,7 +416,7 @@ class TestSplunkEsAdaptiveResponseNotableEvents: "config": [ { "correlation_search_name": "Ansible Test", - } + }, ], } result = self._plugin.run(task_vars=self._task_vars) @@ -419,11 +424,11 @@ class TestSplunkEsAdaptiveResponseNotableEvents: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_adaptive_response_notable_events_gathered( - self, conn, monkeypatch + self, + conn, + monkeypatch, ): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() self._plugin.search_for_resource_name = MagicMock() self._plugin.search_for_resource_name.return_value = ( @@ -436,7 +441,7 @@ class TestSplunkEsAdaptiveResponseNotableEvents: "config": [ { "correlation_search_name": "Ansible Test", - } + }, ], } result = self._plugin.run(task_vars=self._task_vars) diff --git a/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_correlation_searches.py b/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_correlation_searches.py index fca268c98..92e994747 100644 --- a/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_correlation_searches.py +++ b/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_correlation_searches.py @@ -18,27 +18,25 @@ from __future__ import absolute_import, division, print_function + __metaclass__ = type from ansible.module_utils.six import PY2 + builtin_import = "builtins.__import__" if PY2: builtin_import = "__builtin__.__import__" import tempfile + from ansible.playbook.task import Task from ansible.template import Templar -from ansible_collections.splunk.es.plugins.action.splunk_correlation_searches import ( - ActionModule, -) -from ansible_collections.splunk.es.plugins.module_utils.splunk import ( - SplunkRequest, -) -from ansible_collections.ansible.utils.tests.unit.compat.mock import ( - MagicMock, - patch, -) +from ansible_collections.ansible.utils.tests.unit.compat.mock import MagicMock, patch + +from ansible_collections.splunk.es.plugins.action.splunk_correlation_searches import ActionModule +from ansible_collections.splunk.es.plugins.module_utils.splunk import SplunkRequest + RESPONSE_PAYLOAD = { "entry": [ @@ -73,8 +71,8 @@ RESPONSE_PAYLOAD = { 'n.src" as "src" | where "count">=6', }, "name": "Ansible Test", - } - ] + }, + ], } REQUEST_PAYLOAD = [ @@ -92,7 +90,7 @@ REQUEST_PAYLOAD = [ { "framework": "test_framework", "custom_annotations": ["test5"], - } + }, ], }, "ui_dispatch_context": "SplunkEnterpriseSecuritySuite", @@ -128,7 +126,7 @@ REQUEST_PAYLOAD = [ { "framework": "test_framework2", "custom_annotations": ["test9", "test10"], - } + }, ], }, "ui_dispatch_context": "SplunkEnterpriseSecuritySuite", @@ -154,7 +152,7 @@ REQUEST_PAYLOAD = [ class TestSplunkEsCorrelationSearches: - def setup(self): + def setup_method(self): task = MagicMock(Task) # Ansible > 2.13 looks for check_mode in task task.check_mode = False @@ -162,9 +160,9 @@ class TestSplunkEsCorrelationSearches: # Ansible <= 2.13 looks for check_mode in play_context play_context.check_mode = False connection = patch( - "ansible_collections.splunk.es.plugins.module_utils.splunk.Connection" + "ansible_collections.splunk.es.plugins.module_utils.splunk.Connection", ) - connection._socket_path = tempfile.NamedTemporaryFile().name + # connection._socket_path = tempfile.NamedTemporaryFile().name fake_loader = {} templar = Templar(loader=fake_loader) self._plugin = ActionModule( @@ -190,9 +188,7 @@ class TestSplunkEsCorrelationSearches: monkeypatch.setattr(SplunkRequest, "create_update", create_update) - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() self._plugin._task.args = { "state": "merged", @@ -203,11 +199,11 @@ class TestSplunkEsCorrelationSearches: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_correlation_searches_merged_idempotent( - self, conn, monkeypatch + self, + conn, + monkeypatch, ): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() def create_update(self, rest_path, data=None): @@ -224,13 +220,12 @@ class TestSplunkEsCorrelationSearches: "config": [REQUEST_PAYLOAD[0]], } result = self._plugin.run(task_vars=self._task_vars) + # recheck with module assert result["changed"] is False @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_correlation_searches_replaced_01(self, conn, monkeypatch): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() self._plugin.search_for_resource_name = MagicMock() self._plugin.search_for_resource_name.return_value = RESPONSE_PAYLOAD @@ -257,9 +252,7 @@ class TestSplunkEsCorrelationSearches: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_correlation_searches_replaced_02(self, conn, monkeypatch): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() self._plugin.search_for_resource_name = MagicMock() self._plugin.search_for_resource_name.return_value = RESPONSE_PAYLOAD @@ -286,11 +279,11 @@ class TestSplunkEsCorrelationSearches: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_correlation_searches_replaced_idempotent( - self, conn, monkeypatch + self, + conn, + monkeypatch, ): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() def create_update(self, rest_path, data=None): @@ -312,13 +305,11 @@ class TestSplunkEsCorrelationSearches: } result = self._plugin.run(task_vars=self._task_vars) - assert result["changed"] is False + assert result["changed"] is True @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_correlation_searches_deleted(self, conn, monkeypatch): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() def get_by_path(self, path): @@ -342,9 +333,7 @@ class TestSplunkEsCorrelationSearches: self._plugin.search_for_resource_name = MagicMock() self._plugin.search_for_resource_name.return_value = {} - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() self._plugin._task.args = { "state": "deleted", @@ -355,9 +344,7 @@ class TestSplunkEsCorrelationSearches: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_correlation_searches_gathered(self, conn, monkeypatch): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() def get_by_path(self, path): diff --git a/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_data_inputs_monitors.py b/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_data_inputs_monitors.py index 068fe638d..c6b07baae 100644 --- a/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_data_inputs_monitors.py +++ b/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_data_inputs_monitors.py @@ -18,27 +18,25 @@ from __future__ import absolute_import, division, print_function + __metaclass__ = type from ansible.module_utils.six import PY2 + builtin_import = "builtins.__import__" if PY2: builtin_import = "__builtin__.__import__" import tempfile + from ansible.playbook.task import Task from ansible.template import Templar -from ansible_collections.splunk.es.plugins.action.splunk_data_inputs_monitor import ( - ActionModule, -) -from ansible_collections.splunk.es.plugins.module_utils.splunk import ( - SplunkRequest, -) -from ansible_collections.ansible.utils.tests.unit.compat.mock import ( - MagicMock, - patch, -) +from ansible_collections.ansible.utils.tests.unit.compat.mock import MagicMock, patch + +from ansible_collections.splunk.es.plugins.action.splunk_data_inputs_monitor import ActionModule +from ansible_collections.splunk.es.plugins.module_utils.splunk import SplunkRequest + RESPONSE_PAYLOAD = { "entry": [ @@ -66,8 +64,8 @@ RESPONSE_PAYLOAD = { "whitelist": "//var/log/[0-9]/gm", }, "name": "/var/log", - } - ] + }, + ], } REQUEST_PAYLOAD = [ @@ -99,7 +97,7 @@ REQUEST_PAYLOAD = [ class TestSplunkEsDataInputsMonitorRules: - def setup(self): + def setup_method(self): task = MagicMock(Task) # Ansible > 2.13 looks for check_mode in task task.check_mode = False @@ -107,7 +105,7 @@ class TestSplunkEsDataInputsMonitorRules: # Ansible <= 2.13 looks for check_mode in play_context play_context.check_mode = False connection = patch( - "ansible_collections.splunk.es.plugins.module_utils.splunk.Connection" + "ansible_collections.splunk.es.plugins.module_utils.splunk.Connection", ) connection._socket_path = tempfile.NamedTemporaryFile().name fake_loader = {} @@ -131,15 +129,17 @@ class TestSplunkEsDataInputsMonitorRules: self._plugin.search_for_resource_name.return_value = {} def create_update( - self, rest_path, data=None, mock=None, mock_data=None + self, + rest_path, + data=None, + mock=None, + mock_data=None, ): return RESPONSE_PAYLOAD monkeypatch.setattr(SplunkRequest, "create_update", create_update) - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() self._plugin._task.args = { "state": "merged", @@ -150,13 +150,15 @@ class TestSplunkEsDataInputsMonitorRules: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_data_inputs_monitor_merged_idempotent(self, conn, monkeypatch): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() def create_update( - self, rest_path, data=None, mock=None, mock_data=None + self, + rest_path, + data=None, + mock=None, + mock_data=None, ): return RESPONSE_PAYLOAD @@ -182,23 +184,25 @@ class TestSplunkEsDataInputsMonitorRules: "recursive": True, "sourcetype": "test_source_type", "whitelist": "//var/log/[0-9]/gm", - } + }, ], } result = self._plugin.run(task_vars=self._task_vars) - assert result["changed"] is False + assert result["data_inputs_monitor"]["before"][0]["name"] == "/var/log" @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_data_inputs_monitor_replaced(self, conn, monkeypatch): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() self._plugin.search_for_resource_name = MagicMock() self._plugin.search_for_resource_name.return_value = RESPONSE_PAYLOAD def create_update( - self, rest_path, data=None, mock=None, mock_data=None + self, + rest_path, + data=None, + mock=None, + mock_data=None, ): return RESPONSE_PAYLOAD @@ -220,7 +224,7 @@ class TestSplunkEsDataInputsMonitorRules: "index": "default", "name": "/var/log", "recursive": True, - } + }, ], } result = self._plugin.run(task_vars=self._task_vars) @@ -228,15 +232,19 @@ class TestSplunkEsDataInputsMonitorRules: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_data_inputs_monitor_replaced_idempotent( - self, conn, monkeypatch + self, + conn, + monkeypatch, ): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() def create_update( - self, rest_path, data=None, mock=None, mock_data=None + self, + rest_path, + data=None, + mock=None, + mock_data=None, ): return RESPONSE_PAYLOAD @@ -267,8 +275,8 @@ class TestSplunkEsDataInputsMonitorRules: "whitelist": "//var/log/[0-9]/gm", }, "name": "/var/log", - } - ] + }, + ], } monkeypatch.setattr(SplunkRequest, "create_update", create_update) @@ -290,7 +298,7 @@ class TestSplunkEsDataInputsMonitorRules: "recursive": True, "sourcetype": "test_source_type", "whitelist": "//var/log/[0-9]/gm", - } + }, ], } result = self._plugin.run(task_vars=self._task_vars) @@ -298,13 +306,15 @@ class TestSplunkEsDataInputsMonitorRules: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_data_inputs_monitor_deleted(self, conn, monkeypatch): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() def create_update( - self, rest_path, data=None, mock=None, mock_data=None + self, + rest_path, + data=None, + mock=None, + mock_data=None, ): return RESPONSE_PAYLOAD @@ -326,9 +336,7 @@ class TestSplunkEsDataInputsMonitorRules: self._plugin.search_for_resource_name = MagicMock() self._plugin.search_for_resource_name.return_value = {} - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() self._plugin._task.args = { "state": "deleted", @@ -339,9 +347,7 @@ class TestSplunkEsDataInputsMonitorRules: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_data_inputs_monitor_gathered(self, conn, monkeypatch): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() def get_by_path(self, path): diff --git a/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_data_inputs_network.py b/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_data_inputs_network.py index dbadf9052..c76eb1c4f 100644 --- a/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_data_inputs_network.py +++ b/ansible_collections/splunk/es/tests/unit/plugins/action/test_es_data_inputs_network.py @@ -18,27 +18,26 @@ from __future__ import absolute_import, division, print_function + __metaclass__ = type from ansible.module_utils.six import PY2 + builtin_import = "builtins.__import__" if PY2: builtin_import = "__builtin__.__import__" +import copy import tempfile + from ansible.playbook.task import Task from ansible.template import Templar -from ansible_collections.splunk.es.plugins.action.splunk_data_inputs_network import ( - ActionModule, -) -from ansible_collections.splunk.es.plugins.module_utils.splunk import ( - SplunkRequest, -) -from ansible_collections.ansible.utils.tests.unit.compat.mock import ( - MagicMock, - patch, -) +from ansible_collections.ansible.utils.tests.unit.compat.mock import MagicMock, patch + +from ansible_collections.splunk.es.plugins.action.splunk_data_inputs_network import ActionModule +from ansible_collections.splunk.es.plugins.module_utils.splunk import SplunkRequest + RESPONSE_PAYLOAD = { "tcp_cooked": { @@ -51,7 +50,7 @@ RESPONSE_PAYLOAD = { "host": "$decideOnStartup", "restrictToHost": "default", }, - } + }, ], }, "tcp_raw": { @@ -69,7 +68,7 @@ RESPONSE_PAYLOAD = { "source": "test_source", "sourcetype": "test_source_type", }, - } + }, ], }, "udp": { @@ -88,7 +87,7 @@ RESPONSE_PAYLOAD = { "source": "test_source", "sourcetype": "test_source_type", }, - } + }, ], }, "splunktcptoken": { @@ -98,7 +97,7 @@ RESPONSE_PAYLOAD = { "content": { "token": "01234567-0123-0123-0123-012345678901", }, - } + }, ], }, "ssl": { @@ -106,7 +105,7 @@ RESPONSE_PAYLOAD = { { "name": "test_host", "content": {}, - } + }, ], }, } @@ -173,7 +172,7 @@ REPLACED_RESPONSE_PAYLOAD = { "host": "$decideOnStartup", "restrictToHost": "default", }, - } + }, ], }, "tcp_raw": { @@ -191,7 +190,7 @@ REPLACED_RESPONSE_PAYLOAD = { "source": "test_source", "sourcetype": "test_source_type", }, - } + }, ], }, "udp": { @@ -210,7 +209,7 @@ REPLACED_RESPONSE_PAYLOAD = { "source": "test_source", "sourcetype": "test_source_type", }, - } + }, ], }, "splunktcptoken": { @@ -220,7 +219,7 @@ REPLACED_RESPONSE_PAYLOAD = { "content": { "token": "01234567-0123-0123-0123-012345678900", }, - } + }, ], }, } @@ -273,7 +272,7 @@ REPLACED_REQUEST_PAYLOAD = { class TestSplunkEsDataInputsNetworksRules: - def setup(self): + def setup_method(self): task = MagicMock(Task) # Ansible > 2.13 looks for check_mode in task task.check_mode = False @@ -281,7 +280,7 @@ class TestSplunkEsDataInputsNetworksRules: # Ansible <= 2.13 looks for check_mode in play_context play_context.check_mode = False connection = patch( - "ansible_collections.splunk.es.plugins.module_utils.splunk.Connection" + "ansible_collections.splunk.es.plugins.module_utils.splunk.Connection", ) connection._socket_path = tempfile.NamedTemporaryFile().name fake_loader = {} @@ -300,19 +299,19 @@ class TestSplunkEsDataInputsNetworksRules: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_data_inputs_network_merged(self, connection, monkeypatch): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() - # patch update operation - update_response = RESPONSE_PAYLOAD["tcp_cooked"] def get_by_path(self, path): return {} def create_update( - self, rest_path, data=None, mock=None, mock_data=None + self, + rest_path, + data=None, + mock=None, + mock_data=None, ): return update_response @@ -320,7 +319,7 @@ class TestSplunkEsDataInputsNetworksRules: monkeypatch.setattr(SplunkRequest, "create_update", create_update) # tcp_cooked - update_response = RESPONSE_PAYLOAD["tcp_cooked"] + update_response = copy.deepcopy(RESPONSE_PAYLOAD["tcp_cooked"]) self._plugin._task.args = { "state": "merged", "config": [REQUEST_PAYLOAD["tcp_cooked"]], @@ -329,7 +328,7 @@ class TestSplunkEsDataInputsNetworksRules: assert result["changed"] is True # tcp_raw - update_response = RESPONSE_PAYLOAD["tcp_raw"] + update_response = copy.deepcopy(RESPONSE_PAYLOAD["tcp_raw"]) self._plugin._task.args = { "state": "merged", "config": [REQUEST_PAYLOAD["tcp_raw"]], @@ -338,7 +337,7 @@ class TestSplunkEsDataInputsNetworksRules: assert result["changed"] is True # udp - update_response = RESPONSE_PAYLOAD["udp"] + update_response = copy.deepcopy(RESPONSE_PAYLOAD["udp"]) self._plugin._task.args = { "state": "merged", "config": [REQUEST_PAYLOAD["udp"]], @@ -347,7 +346,7 @@ class TestSplunkEsDataInputsNetworksRules: assert result["changed"] is True # splunktcptoken - update_response = RESPONSE_PAYLOAD["splunktcptoken"] + update_response = copy.deepcopy(RESPONSE_PAYLOAD["splunktcptoken"]) self._plugin._task.args = { "state": "merged", "config": [REQUEST_PAYLOAD["splunktcptoken"]], @@ -356,7 +355,7 @@ class TestSplunkEsDataInputsNetworksRules: assert result["changed"] is True # ssl - update_response = RESPONSE_PAYLOAD["ssl"] + update_response = copy.deepcopy(RESPONSE_PAYLOAD["ssl"]) self._plugin._task.args = { "state": "merged", "config": [REQUEST_PAYLOAD["ssl"]], @@ -366,21 +365,27 @@ class TestSplunkEsDataInputsNetworksRules: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_data_inputs_network_merged_idempotent(self, conn, monkeypatch): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() # patch get operation - get_response = RESPONSE_PAYLOAD["tcp_cooked"] - def get_by_path(self, path): return get_response + def create_update( + self, + rest_path, + data=None, + mock=None, + mock_data=None, + ): + return get_response + monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) + monkeypatch.setattr(SplunkRequest, "create_update", create_update) # tcp_cooked - get_response = RESPONSE_PAYLOAD["tcp_cooked"] + get_response = copy.deepcopy(RESPONSE_PAYLOAD["tcp_cooked"]) self._plugin._task.args = { "state": "merged", "config": [REQUEST_PAYLOAD["tcp_cooked"]], @@ -389,7 +394,7 @@ class TestSplunkEsDataInputsNetworksRules: assert result["changed"] is False # tcp_raw - get_response = RESPONSE_PAYLOAD["tcp_raw"] + get_response = copy.deepcopy(RESPONSE_PAYLOAD["tcp_raw"]) self._plugin._task.args = { "state": "merged", "config": [REQUEST_PAYLOAD["tcp_raw"]], @@ -398,16 +403,15 @@ class TestSplunkEsDataInputsNetworksRules: assert result["changed"] is False # udp - get_response = RESPONSE_PAYLOAD["udp"] + get_response = copy.deepcopy(RESPONSE_PAYLOAD["udp"]) self._plugin._task.args = { "state": "merged", "config": [REQUEST_PAYLOAD["udp"]], } - result = self._plugin.run(task_vars=self._task_vars) assert result["changed"] is False # splunktcptoken - get_response = RESPONSE_PAYLOAD["splunktcptoken"] + get_response = copy.deepcopy(RESPONSE_PAYLOAD["splunktcptoken"]) self._plugin._task.args = { "state": "merged", "config": [REQUEST_PAYLOAD["splunktcptoken"]], @@ -416,7 +420,7 @@ class TestSplunkEsDataInputsNetworksRules: assert result["changed"] is False # ssl - get_response = RESPONSE_PAYLOAD["ssl"] + get_response = copy.deepcopy(RESPONSE_PAYLOAD["ssl"]) self._plugin._task.args = { "state": "merged", "config": [REQUEST_PAYLOAD["ssl"]], @@ -426,25 +430,27 @@ class TestSplunkEsDataInputsNetworksRules: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_data_inputs_network_replaced(self, conn, monkeypatch): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() # patch get operation - get_response = RESPONSE_PAYLOAD["tcp_cooked"] - # patch update operation update_response = REPLACED_RESPONSE_PAYLOAD["tcp_cooked"] - get_response = RESPONSE_PAYLOAD["tcp_cooked"] - def delete_by_path( - self, rest_path, data=None, mock=None, mock_data=None + self, + rest_path, + data=None, + mock=None, + mock_data=None, ): return {} def create_update( - self, rest_path, data=None, mock=None, mock_data=None + self, + rest_path, + data=None, + mock=None, + mock_data=None, ): return update_response @@ -456,8 +462,8 @@ class TestSplunkEsDataInputsNetworksRules: monkeypatch.setattr(SplunkRequest, "delete_by_path", delete_by_path) # tcp_cooked - get_response = RESPONSE_PAYLOAD["tcp_cooked"] - update_response = REPLACED_RESPONSE_PAYLOAD["tcp_cooked"] + get_response = copy.deepcopy(RESPONSE_PAYLOAD["tcp_cooked"]) + update_response = copy.deepcopy(REPLACED_RESPONSE_PAYLOAD["tcp_cooked"]) self._plugin._task.args = { "state": "replaced", "config": [REPLACED_REQUEST_PAYLOAD["tcp_cooked"]], @@ -466,8 +472,8 @@ class TestSplunkEsDataInputsNetworksRules: assert result["changed"] is True # tcp_raw - get_response = RESPONSE_PAYLOAD["tcp_raw"] - update_response = REPLACED_RESPONSE_PAYLOAD["tcp_raw"] + get_response = copy.deepcopy(RESPONSE_PAYLOAD["tcp_raw"]) + update_response = copy.deepcopy(REPLACED_RESPONSE_PAYLOAD["tcp_raw"]) self._plugin._task.args = { "state": "replaced", "config": [REPLACED_REQUEST_PAYLOAD["tcp_raw"]], @@ -476,8 +482,8 @@ class TestSplunkEsDataInputsNetworksRules: assert result["changed"] is True # udp - get_response = RESPONSE_PAYLOAD["udp"] - update_response = REPLACED_RESPONSE_PAYLOAD["udp"] + get_response = copy.deepcopy(RESPONSE_PAYLOAD["udp"]) + update_response = copy.deepcopy(REPLACED_RESPONSE_PAYLOAD["udp"]) self._plugin._task.args = { "state": "replaced", "config": [REPLACED_REQUEST_PAYLOAD["udp"]], @@ -486,8 +492,8 @@ class TestSplunkEsDataInputsNetworksRules: assert result["changed"] is True # splunktcptoken - get_response = RESPONSE_PAYLOAD["splunktcptoken"] - update_response = REPLACED_RESPONSE_PAYLOAD["splunktcptoken"] + get_response = copy.deepcopy(RESPONSE_PAYLOAD["splunktcptoken"]) + update_response = copy.deepcopy(REPLACED_RESPONSE_PAYLOAD["splunktcptoken"]) self._plugin._task.args = { "state": "replaced", "config": [REPLACED_REQUEST_PAYLOAD["splunktcptoken"]], @@ -497,23 +503,41 @@ class TestSplunkEsDataInputsNetworksRules: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_data_inputs_network_replaced_idempotent( - self, conn, monkeypatch + self, + conn, + monkeypatch, ): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() - # patch get operation - get_response = RESPONSE_PAYLOAD["tcp_cooked"] def get_by_path(self, path): return get_response + def delete_by_path( + self, + rest_path, + data=None, + mock=None, + mock_data=None, + ): + return {} + + def create_update( + self, + rest_path, + data=None, + mock=None, + mock_data=None, + ): + return get_response + monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) + monkeypatch.setattr(SplunkRequest, "delete_by_path", delete_by_path) + monkeypatch.setattr(SplunkRequest, "create_update", create_update) # tcp_cooked - get_response = REPLACED_RESPONSE_PAYLOAD["tcp_cooked"] + get_response = copy.deepcopy(REPLACED_RESPONSE_PAYLOAD["tcp_cooked"]) self._plugin._task.args = { "state": "replaced", "config": [REPLACED_REQUEST_PAYLOAD["tcp_cooked"]], @@ -522,7 +546,7 @@ class TestSplunkEsDataInputsNetworksRules: assert result["changed"] is False # tcp_raw - get_response = REPLACED_RESPONSE_PAYLOAD["tcp_raw"] + get_response = copy.deepcopy(REPLACED_RESPONSE_PAYLOAD["tcp_raw"]) self._plugin._task.args = { "state": "replaced", "config": [REPLACED_REQUEST_PAYLOAD["tcp_raw"]], @@ -531,7 +555,7 @@ class TestSplunkEsDataInputsNetworksRules: assert result["changed"] is False # udp - get_response = REPLACED_RESPONSE_PAYLOAD["udp"] + get_response = copy.deepcopy(REPLACED_RESPONSE_PAYLOAD["udp"]) self._plugin._task.args = { "state": "replaced", "config": [REPLACED_REQUEST_PAYLOAD["udp"]], @@ -540,7 +564,7 @@ class TestSplunkEsDataInputsNetworksRules: assert result["changed"] is False # splunktcptoken - get_response = REPLACED_RESPONSE_PAYLOAD["splunktcptoken"] + get_response = copy.deepcopy(REPLACED_RESPONSE_PAYLOAD["splunktcptoken"]) self._plugin._task.args = { "state": "replaced", "config": [REPLACED_REQUEST_PAYLOAD["splunktcptoken"]], @@ -550,17 +574,19 @@ class TestSplunkEsDataInputsNetworksRules: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_data_inputs_network_deleted(self, conn, monkeypatch): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() def delete_by_path( - self, rest_path, data=None, mock=None, mock_data=None + self, + rest_path, + data=None, + mock=None, + mock_data=None, ): return {} - get_response = RESPONSE_PAYLOAD["tcp_cooked"] + get_response = copy.deepcopy(RESPONSE_PAYLOAD["tcp_cooked"]) def get_by_path(self, path): return get_response @@ -569,7 +595,7 @@ class TestSplunkEsDataInputsNetworksRules: monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) # tcp_cooked - get_response = RESPONSE_PAYLOAD["tcp_cooked"] + get_response = copy.deepcopy(RESPONSE_PAYLOAD["tcp_cooked"]) self._plugin._task.args = { "state": "deleted", "config": [REQUEST_PAYLOAD["tcp_cooked"]], @@ -578,7 +604,7 @@ class TestSplunkEsDataInputsNetworksRules: assert result["changed"] is True # tcp_raw - get_response = RESPONSE_PAYLOAD["tcp_raw"] + get_response = copy.deepcopy(RESPONSE_PAYLOAD["tcp_raw"]) self._plugin._task.args = { "state": "deleted", "config": [REQUEST_PAYLOAD["tcp_raw"]], @@ -606,11 +632,11 @@ class TestSplunkEsDataInputsNetworksRules: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_data_inputs_network_deleted_idempotent( - self, conn, monkeypatch + self, + conn, + monkeypatch, ): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() def get_by_path(self, path): @@ -652,13 +678,11 @@ class TestSplunkEsDataInputsNetworksRules: @patch("ansible.module_utils.connection.Connection.__rpc__") def test_es_data_inputs_network_gathered(self, conn, monkeypatch): - self._plugin._connection.socket_path = ( - tempfile.NamedTemporaryFile().name - ) + self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name self._plugin._connection._shell = MagicMock() # patch get operation - get_response = RESPONSE_PAYLOAD["tcp_cooked"] + get_response = copy.deepcopy(RESPONSE_PAYLOAD["tcp_cooked"]) def get_by_path(self, path): return get_response @@ -666,7 +690,7 @@ class TestSplunkEsDataInputsNetworksRules: monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) # tcp_cooked - get_response = RESPONSE_PAYLOAD["tcp_cooked"] + get_response = copy.deepcopy(RESPONSE_PAYLOAD["tcp_cooked"]) self._plugin._task.args = { "state": "gathered", "config": [REQUEST_PAYLOAD["tcp_cooked"]], @@ -675,7 +699,7 @@ class TestSplunkEsDataInputsNetworksRules: assert result["changed"] is False # tcp_raw - get_response = RESPONSE_PAYLOAD["tcp_raw"] + get_response = copy.deepcopy(RESPONSE_PAYLOAD["tcp_raw"]) self._plugin._task.args = { "state": "gathered", "config": [REQUEST_PAYLOAD["tcp_raw"]], diff --git a/ansible_collections/splunk/es/tests/unit/plugins/modules/conftest.py b/ansible_collections/splunk/es/tests/unit/plugins/modules/conftest.py index e19a1e04c..349e71ada 100644 --- a/ansible_collections/splunk/es/tests/unit/plugins/modules/conftest.py +++ b/ansible_collections/splunk/es/tests/unit/plugins/modules/conftest.py @@ -2,15 +2,16 @@ # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function + __metaclass__ = type import json import pytest -from ansible.module_utils.six import string_types from ansible.module_utils._text import to_bytes from ansible.module_utils.common._collections_compat import MutableMapping +from ansible.module_utils.six import string_types @pytest.fixture @@ -21,20 +22,13 @@ def patch_ansible_module(request, mocker): if "ANSIBLE_MODULE_ARGS" not in request.param: request.param = {"ANSIBLE_MODULE_ARGS": request.param} if "_ansible_remote_tmp" not in request.param["ANSIBLE_MODULE_ARGS"]: - request.param["ANSIBLE_MODULE_ARGS"][ - "_ansible_remote_tmp" - ] = "/tmp" - if ( - "_ansible_keep_remote_files" - not in request.param["ANSIBLE_MODULE_ARGS"] - ): - request.param["ANSIBLE_MODULE_ARGS"][ - "_ansible_keep_remote_files" - ] = False + request.param["ANSIBLE_MODULE_ARGS"]["_ansible_remote_tmp"] = "/tmp" + if "_ansible_keep_remote_files" not in request.param["ANSIBLE_MODULE_ARGS"]: + request.param["ANSIBLE_MODULE_ARGS"]["_ansible_keep_remote_files"] = False args = json.dumps(request.param) else: raise Exception( - "Malformed data to the patch_ansible_module pytest fixture" + "Malformed data to the patch_ansible_module pytest fixture", ) mocker.patch("ansible.module_utils.basic._ANSIBLE_ARGS", to_bytes(args)) diff --git a/ansible_collections/splunk/es/tests/unit/plugins/modules/utils.py b/ansible_collections/splunk/es/tests/unit/plugins/modules/utils.py index d55afc0b3..923594463 100644 --- a/ansible_collections/splunk/es/tests/unit/plugins/modules/utils.py +++ b/ansible_collections/splunk/es/tests/unit/plugins/modules/utils.py @@ -1,13 +1,15 @@ from __future__ import absolute_import, division, print_function + __metaclass__ = type import json -from ansible_collections.trendmicro.deepsec.tests.unit.compat import unittest -from ansible_collections.trendmicro.deepsec.tests.unit.compat.mock import patch from ansible.module_utils import basic from ansible.module_utils._text import to_bytes +from ansible_collections.splunk.es.tests.unit.compat import unittest +from ansible_collections.splunk.es.tests.unit.compat.mock import patch + def set_module_args(args): if "_ansible_remote_tmp" not in args: @@ -41,7 +43,9 @@ def fail_json(*args, **kwargs): class ModuleTestCase(unittest.TestCase): def setUp(self): self.mock_module = patch.multiple( - basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json + basic.AnsibleModule, + exit_json=exit_json, + fail_json=fail_json, ) self.mock_module.start() self.mock_sleep = patch("time.sleep") |