| 1 |
# Copyright 2026 Red Hat Inc. |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
|
| 19 |
""" |
| 20 |
Unit tests for the splunk_notes_info action plugin. |
| 21 |
|
| 22 |
These tests verify that the splunk_notes_info action plugin works correctly for: |
| 23 |
- Querying notes from findings, investigations, and response plan tasks |
| 24 |
- Querying a specific note by note_id |
| 25 |
- Querying all notes with limit parameter |
| 26 |
- Handling not found scenarios (404 and MC_0050 errors) |
| 27 |
|
| 28 |
The tests use mocking to simulate Splunk API responses. |
| 29 |
""" |
| 30 |
|
| 31 |
import copy |
| 32 |
import tempfile |
| 33 |
|
| 34 |
from unittest.mock import MagicMock, patch |
| 35 |
|
| 36 |
from ansible.playbook.task import Task |
| 37 |
from ansible.template import Templar |
| 38 |
|
| 39 |
from ansible_collections.splunk.es.plugins.action.splunk_notes_info import ActionModule |
| 40 |
from ansible_collections.splunk.es.plugins.module_utils.notes import validate_target_params |
| 41 |
from ansible_collections.splunk.es.plugins.module_utils.splunk import SplunkRequest |
| 42 |
|
| 43 |
|
| 44 |
def _get_msg_str(result: dict) -> str: |
| 45 |
"""Get message from result as a lowercase string. |
| 46 |
|
| 47 |
Handles both string and list message formats that Ansible can return. |
| 48 |
|
| 49 |
Args: |
| 50 |
result: The result dictionary from module execution. |
| 51 |
|
| 52 |
Returns: |
| 53 |
The message as a lowercase string. |
| 54 |
""" |
| 55 |
msg = result.get("msg", "") |
| 56 |
if isinstance(msg, list): |
| 57 |
return " ".join(str(m) for m in msg).lower() |
| 58 |
return str(msg).lower() |
| 59 |
|
| 60 |
|
| 61 |
# Test data |
| 62 |
FINDING_REF_ID = "2008e99d-af14-4fec-89da-b9b17a81820a@@notable@@time1768225865" |
| 63 |
INVESTIGATION_UUID = "590afa9c-23d5-4377-b909-cd2cfa1bc0f1" |
| 64 |
RESPONSE_PLAN_UUID = "b9ef7dce-6dcd-4900-b5d5-982fc194554a" |
| 65 |
PHASE_UUID = "phase-001-uuid" |
| 66 |
TASK_UUID = "task-001-uuid" |
| 67 |
NOTE_UUID_1 = "note-abc123" |
| 68 |
NOTE_UUID_2 = "note-def456" |
| 69 |
|
| 70 |
# Note API responses (as returned by API in "items" array) |
| 71 |
NOTES_API_RESPONSE = { |
| 72 |
"items": [ |
| 73 |
{ |
| 74 |
"id": NOTE_UUID_1, |
| 75 |
"content": "First note content.", |
| 76 |
}, |
| 77 |
{ |
| 78 |
"id": NOTE_UUID_2, |
| 79 |
"content": "Second note content.", |
| 80 |
}, |
| 81 |
], |
| 82 |
"offset": 0, |
| 83 |
"limit": 100, |
| 84 |
"total": 2, |
| 85 |
} |
| 86 |
|
| 87 |
SINGLE_NOTE_API_RESPONSE = { |
| 88 |
"id": NOTE_UUID_1, |
| 89 |
"content": "First note content.", |
| 90 |
} |
| 91 |
|
| 92 |
EMPTY_NOTES_RESPONSE = { |
| 93 |
"items": [], |
| 94 |
"offset": 0, |
| 95 |
"limit": 100, |
| 96 |
"total": 0, |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
class TestEsNotesInfo: |
| 101 |
"""Test class for the splunk_notes_info action plugin. |
| 102 |
|
| 103 |
The splunk_notes_info module is a "read-only" info module that queries |
| 104 |
Splunk for notes without making changes. It should always return |
| 105 |
changed=False. |
| 106 |
|
| 107 |
Query modes: |
| 108 |
1. All notes: Returns all notes for the target (with optional limit) |
| 109 |
2. By note_id: Returns a specific note |
| 110 |
""" |
| 111 |
|
| 112 |
def setup_method(self): |
| 113 |
"""Set up test fixtures before each test method. |
| 114 |
|
| 115 |
Creates mock Ansible components needed to test the action plugin. |
| 116 |
""" |
| 117 |
# Create a mock Task object |
| 118 |
task = MagicMock(Task) |
| 119 |
task.check_mode = False |
| 120 |
|
| 121 |
# Create mock play context |
| 122 |
play_context = MagicMock() |
| 123 |
play_context.check_mode = False |
| 124 |
|
| 125 |
# Create a mock connection |
| 126 |
connection = patch( |
| 127 |
"ansible_collections.splunk.es.plugins.module_utils.splunk.Connection", |
| 128 |
) |
| 129 |
|
| 130 |
# Ansible's template engine |
| 131 |
fake_loader = {} |
| 132 |
templar = Templar(loader=fake_loader) |
| 133 |
|
| 134 |
# Create the action plugin instance |
| 135 |
self._plugin = ActionModule( |
| 136 |
task=task, |
| 137 |
connection=connection, |
| 138 |
play_context=play_context, |
| 139 |
loader=fake_loader, |
| 140 |
templar=templar, |
| 141 |
shared_loader_obj=None, |
| 142 |
) |
| 143 |
|
| 144 |
# Set required task attributes |
| 145 |
self._plugin._task.action = "splunk_notes_info" |
| 146 |
self._plugin._task.async_val = False |
| 147 |
|
| 148 |
# Task variables |
| 149 |
self._task_vars = {} |
| 150 |
|
| 151 |
# Finding Notes Tests |
| 152 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 153 |
def test_finding_notes_all(self, connection, monkeypatch): |
| 154 |
"""Test querying all notes from a finding.""" |
| 155 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 156 |
self._plugin._connection._shell = MagicMock() |
| 157 |
|
| 158 |
def get_by_path(self, path, query_params=None): |
| 159 |
return copy.deepcopy(NOTES_API_RESPONSE) |
| 160 |
|
| 161 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 162 |
|
| 163 |
self._plugin._task.args = { |
| 164 |
"target_type": "finding", |
| 165 |
"finding_ref_id": FINDING_REF_ID, |
| 166 |
} |
| 167 |
|
| 168 |
result = self._plugin.run(task_vars=self._task_vars) |
| 169 |
|
| 170 |
assert result["changed"] is False |
| 171 |
assert result.get("failed") is not True |
| 172 |
assert "notes" in result |
| 173 |
assert len(result["notes"]) == 2 |
| 174 |
assert result["notes"][0]["note_id"] == NOTE_UUID_1 |
| 175 |
assert result["notes"][1]["note_id"] == NOTE_UUID_2 |
| 176 |
|
| 177 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 178 |
def test_finding_notes_by_id(self, connection, monkeypatch): |
| 179 |
"""Test querying a specific note from a finding by note_id.""" |
| 180 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 181 |
self._plugin._connection._shell = MagicMock() |
| 182 |
|
| 183 |
def get_by_path(self, path, query_params=None): |
| 184 |
# Finding/investigation use filtered lookup, so return all notes |
| 185 |
return copy.deepcopy(NOTES_API_RESPONSE) |
| 186 |
|
| 187 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 188 |
|
| 189 |
self._plugin._task.args = { |
| 190 |
"target_type": "finding", |
| 191 |
"finding_ref_id": FINDING_REF_ID, |
| 192 |
"note_id": NOTE_UUID_1, |
| 193 |
} |
| 194 |
|
| 195 |
result = self._plugin.run(task_vars=self._task_vars) |
| 196 |
|
| 197 |
assert result["changed"] is False |
| 198 |
assert result.get("failed") is not True |
| 199 |
assert len(result["notes"]) == 1 |
| 200 |
assert result["notes"][0]["note_id"] == NOTE_UUID_1 |
| 201 |
|
| 202 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 203 |
def test_finding_notes_notable_time_extracted(self, connection, monkeypatch): |
| 204 |
"""Test that notable_time is extracted from finding_ref_id for API query.""" |
| 205 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 206 |
self._plugin._connection._shell = MagicMock() |
| 207 |
|
| 208 |
captured_params = [] |
| 209 |
|
| 210 |
def get_by_path(self, path, query_params=None): |
| 211 |
captured_params.append(query_params) |
| 212 |
return copy.deepcopy(NOTES_API_RESPONSE) |
| 213 |
|
| 214 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 215 |
|
| 216 |
self._plugin._task.args = { |
| 217 |
"target_type": "finding", |
| 218 |
"finding_ref_id": FINDING_REF_ID, |
| 219 |
} |
| 220 |
|
| 221 |
result = self._plugin.run(task_vars=self._task_vars) |
| 222 |
|
| 223 |
assert result["changed"] is False |
| 224 |
# Verify notable_time was extracted and passed to API |
| 225 |
assert len(captured_params) > 0 |
| 226 |
assert "notable_time" in captured_params[0] |
| 227 |
assert captured_params[0]["notable_time"] == "1768225865" |
| 228 |
|
| 229 |
# Investigation Notes Tests |
| 230 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 231 |
def test_investigation_notes_all(self, connection, monkeypatch): |
| 232 |
"""Test querying all notes from an investigation.""" |
| 233 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 234 |
self._plugin._connection._shell = MagicMock() |
| 235 |
|
| 236 |
def get_by_path(self, path, query_params=None): |
| 237 |
return copy.deepcopy(NOTES_API_RESPONSE) |
| 238 |
|
| 239 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 240 |
|
| 241 |
self._plugin._task.args = { |
| 242 |
"target_type": "investigation", |
| 243 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 244 |
} |
| 245 |
|
| 246 |
result = self._plugin.run(task_vars=self._task_vars) |
| 247 |
|
| 248 |
assert result["changed"] is False |
| 249 |
assert result.get("failed") is not True |
| 250 |
assert len(result["notes"]) == 2 |
| 251 |
|
| 252 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 253 |
def test_investigation_notes_by_id(self, connection, monkeypatch): |
| 254 |
"""Test querying a specific note from an investigation by note_id.""" |
| 255 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 256 |
self._plugin._connection._shell = MagicMock() |
| 257 |
|
| 258 |
def get_by_path(self, path, query_params=None): |
| 259 |
return copy.deepcopy(NOTES_API_RESPONSE) |
| 260 |
|
| 261 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 262 |
|
| 263 |
self._plugin._task.args = { |
| 264 |
"target_type": "investigation", |
| 265 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 266 |
"note_id": NOTE_UUID_2, |
| 267 |
} |
| 268 |
|
| 269 |
result = self._plugin.run(task_vars=self._task_vars) |
| 270 |
|
| 271 |
assert result["changed"] is False |
| 272 |
assert len(result["notes"]) == 1 |
| 273 |
assert result["notes"][0]["note_id"] == NOTE_UUID_2 |
| 274 |
|
| 275 |
# Response Plan Task Notes Tests |
| 276 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 277 |
def test_task_notes_all(self, connection, monkeypatch): |
| 278 |
"""Test querying all notes from a response plan task.""" |
| 279 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 280 |
self._plugin._connection._shell = MagicMock() |
| 281 |
|
| 282 |
def get_by_path(self, path, query_params=None): |
| 283 |
return copy.deepcopy(NOTES_API_RESPONSE) |
| 284 |
|
| 285 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 286 |
|
| 287 |
self._plugin._task.args = { |
| 288 |
"target_type": "response_plan_task", |
| 289 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 290 |
"response_plan_id": RESPONSE_PLAN_UUID, |
| 291 |
"phase_id": PHASE_UUID, |
| 292 |
"task_id": TASK_UUID, |
| 293 |
} |
| 294 |
|
| 295 |
result = self._plugin.run(task_vars=self._task_vars) |
| 296 |
|
| 297 |
assert result["changed"] is False |
| 298 |
assert result.get("failed") is not True |
| 299 |
assert len(result["notes"]) == 2 |
| 300 |
|
| 301 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 302 |
def test_task_notes_by_id_direct_lookup(self, connection, monkeypatch): |
| 303 |
"""Test querying a specific task note by ID uses direct API lookup.""" |
| 304 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 305 |
self._plugin._connection._shell = MagicMock() |
| 306 |
|
| 307 |
captured_paths = [] |
| 308 |
|
| 309 |
def get_by_path(self, path, query_params=None): |
| 310 |
captured_paths.append(path) |
| 311 |
# Direct lookup returns single note dict, not wrapped in items |
| 312 |
return copy.deepcopy(SINGLE_NOTE_API_RESPONSE) |
| 313 |
|
| 314 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 315 |
|
| 316 |
self._plugin._task.args = { |
| 317 |
"target_type": "response_plan_task", |
| 318 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 319 |
"response_plan_id": RESPONSE_PLAN_UUID, |
| 320 |
"phase_id": PHASE_UUID, |
| 321 |
"task_id": TASK_UUID, |
| 322 |
"note_id": NOTE_UUID_1, |
| 323 |
} |
| 324 |
|
| 325 |
result = self._plugin.run(task_vars=self._task_vars) |
| 326 |
|
| 327 |
assert result["changed"] is False |
| 328 |
assert len(result["notes"]) == 1 |
| 329 |
# Verify direct note path was used (contains note_id in path) |
| 330 |
assert len(captured_paths) == 1 |
| 331 |
assert NOTE_UUID_1 in captured_paths[0] |
| 332 |
|
| 333 |
# Limit Parameter Tests |
| 334 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 335 |
def test_limit_parameter_default(self, connection, monkeypatch): |
| 336 |
"""Test that default limit (100) is used when not specified.""" |
| 337 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 338 |
self._plugin._connection._shell = MagicMock() |
| 339 |
|
| 340 |
captured_params = [] |
| 341 |
|
| 342 |
def get_by_path(self, path, query_params=None): |
| 343 |
captured_params.append(query_params) |
| 344 |
return copy.deepcopy(NOTES_API_RESPONSE) |
| 345 |
|
| 346 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 347 |
|
| 348 |
self._plugin._task.args = { |
| 349 |
"target_type": "investigation", |
| 350 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 351 |
} |
| 352 |
|
| 353 |
result = self._plugin.run(task_vars=self._task_vars) |
| 354 |
|
| 355 |
assert result["changed"] is False |
| 356 |
assert len(captured_params) > 0 |
| 357 |
assert captured_params[0]["limit"] == 100 |
| 358 |
|
| 359 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 360 |
def test_limit_parameter_custom(self, connection, monkeypatch): |
| 361 |
"""Test that custom limit is passed to API.""" |
| 362 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 363 |
self._plugin._connection._shell = MagicMock() |
| 364 |
|
| 365 |
captured_params = [] |
| 366 |
|
| 367 |
def get_by_path(self, path, query_params=None): |
| 368 |
captured_params.append(query_params) |
| 369 |
return copy.deepcopy(NOTES_API_RESPONSE) |
| 370 |
|
| 371 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 372 |
|
| 373 |
self._plugin._task.args = { |
| 374 |
"target_type": "investigation", |
| 375 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 376 |
"limit": 10, |
| 377 |
} |
| 378 |
|
| 379 |
result = self._plugin.run(task_vars=self._task_vars) |
| 380 |
|
| 381 |
assert result["changed"] is False |
| 382 |
assert len(captured_params) > 0 |
| 383 |
assert captured_params[0]["limit"] == 10 |
| 384 |
|
| 385 |
# Empty Results Tests |
| 386 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 387 |
def test_empty_notes_response(self, connection, monkeypatch): |
| 388 |
"""Test handling of empty notes response.""" |
| 389 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 390 |
self._plugin._connection._shell = MagicMock() |
| 391 |
|
| 392 |
def get_by_path(self, path, query_params=None): |
| 393 |
return copy.deepcopy(EMPTY_NOTES_RESPONSE) |
| 394 |
|
| 395 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 396 |
|
| 397 |
self._plugin._task.args = { |
| 398 |
"target_type": "investigation", |
| 399 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 400 |
} |
| 401 |
|
| 402 |
result = self._plugin.run(task_vars=self._task_vars) |
| 403 |
|
| 404 |
assert result["changed"] is False |
| 405 |
assert result.get("failed") is not True |
| 406 |
assert result["notes"] == [] |
| 407 |
|
| 408 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 409 |
def test_note_by_id_not_found(self, connection, monkeypatch): |
| 410 |
"""Test querying a non-existent note by ID returns empty list.""" |
| 411 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 412 |
self._plugin._connection._shell = MagicMock() |
| 413 |
|
| 414 |
def get_by_path(self, path, query_params=None): |
| 415 |
return copy.deepcopy(NOTES_API_RESPONSE) |
| 416 |
|
| 417 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 418 |
|
| 419 |
self._plugin._task.args = { |
| 420 |
"target_type": "investigation", |
| 421 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 422 |
"note_id": "non-existent-note-id", |
| 423 |
} |
| 424 |
|
| 425 |
result = self._plugin.run(task_vars=self._task_vars) |
| 426 |
|
| 427 |
assert result["changed"] is False |
| 428 |
assert result.get("failed") is not True |
| 429 |
assert result["notes"] == [] |
| 430 |
|
| 431 |
# Error Handling Tests |
| 432 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 433 |
def test_handles_404_error(self, connection, monkeypatch): |
| 434 |
"""Test graceful handling of 404 errors.""" |
| 435 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 436 |
self._plugin._connection._shell = MagicMock() |
| 437 |
|
| 438 |
def get_by_path(self, path, query_params=None): |
| 439 |
raise Exception("HTTP Error 404: Not Found") |
| 440 |
|
| 441 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 442 |
|
| 443 |
self._plugin._task.args = { |
| 444 |
"target_type": "investigation", |
| 445 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 446 |
} |
| 447 |
|
| 448 |
result = self._plugin.run(task_vars=self._task_vars) |
| 449 |
|
| 450 |
# Should return empty list, not fail |
| 451 |
assert result["changed"] is False |
| 452 |
assert result.get("failed") is not True |
| 453 |
assert result["notes"] == [] |
| 454 |
|
| 455 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 456 |
def test_handles_mc_0050_error(self, connection, monkeypatch): |
| 457 |
"""Test graceful handling of MC_0050 (internal server error for missing resource).""" |
| 458 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 459 |
self._plugin._connection._shell = MagicMock() |
| 460 |
|
| 461 |
def get_by_path(self, path, query_params=None): |
| 462 |
raise Exception( |
| 463 |
"Splunk httpapi returned error 500 with message " |
| 464 |
"{'code': 'MC_0050', 'message': 'Internal server error'}", |
| 465 |
) |
| 466 |
|
| 467 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 468 |
|
| 469 |
self._plugin._task.args = { |
| 470 |
"target_type": "response_plan_task", |
| 471 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 472 |
"response_plan_id": RESPONSE_PLAN_UUID, |
| 473 |
"phase_id": PHASE_UUID, |
| 474 |
"task_id": TASK_UUID, |
| 475 |
"note_id": "deleted-note-id", |
| 476 |
} |
| 477 |
|
| 478 |
result = self._plugin.run(task_vars=self._task_vars) |
| 479 |
|
| 480 |
# Should return empty list, not fail |
| 481 |
assert result["changed"] is False |
| 482 |
assert result.get("failed") is not True |
| 483 |
assert result["notes"] == [] |
| 484 |
|
| 485 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 486 |
def test_handles_other_errors(self, connection, monkeypatch): |
| 487 |
"""Test that other errors properly fail the module.""" |
| 488 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 489 |
self._plugin._connection._shell = MagicMock() |
| 490 |
|
| 491 |
def get_by_path(self, path, query_params=None): |
| 492 |
raise Exception("Connection timeout error") |
| 493 |
|
| 494 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 495 |
|
| 496 |
self._plugin._task.args = { |
| 497 |
"target_type": "investigation", |
| 498 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 499 |
} |
| 500 |
|
| 501 |
try: |
| 502 |
self._plugin.run(task_vars=self._task_vars) |
| 503 |
assert False, "Should have raised an exception" |
| 504 |
except Exception as e: |
| 505 |
assert "Connection timeout" in str(e) or "Failed to query" in str(e) |
| 506 |
|
| 507 |
# Validation Tests |
| 508 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 509 |
def test_missing_target_type(self, connection): |
| 510 |
"""Test that missing target_type returns an error.""" |
| 511 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 512 |
self._plugin._connection._shell = MagicMock() |
| 513 |
|
| 514 |
self._plugin._task.args = { |
| 515 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 516 |
} |
| 517 |
|
| 518 |
result = self._plugin.run(task_vars=self._task_vars) |
| 519 |
|
| 520 |
assert result["failed"] is True |
| 521 |
|
| 522 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 523 |
def test_missing_finding_ref_id_for_finding(self, connection): |
| 524 |
"""Test that missing finding_ref_id for finding target returns an error.""" |
| 525 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 526 |
self._plugin._connection._shell = MagicMock() |
| 527 |
|
| 528 |
self._plugin._task.args = { |
| 529 |
"target_type": "finding", |
| 530 |
} |
| 531 |
|
| 532 |
result = self._plugin.run(task_vars=self._task_vars) |
| 533 |
|
| 534 |
assert result["failed"] is True |
| 535 |
assert "finding_ref_id" in _get_msg_str(result) |
| 536 |
|
| 537 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 538 |
def test_missing_investigation_ref_id_for_investigation(self, connection): |
| 539 |
"""Test that missing investigation_ref_id for investigation target returns an error.""" |
| 540 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 541 |
self._plugin._connection._shell = MagicMock() |
| 542 |
|
| 543 |
self._plugin._task.args = { |
| 544 |
"target_type": "investigation", |
| 545 |
} |
| 546 |
|
| 547 |
result = self._plugin.run(task_vars=self._task_vars) |
| 548 |
|
| 549 |
assert result["failed"] is True |
| 550 |
assert "investigation_ref_id" in _get_msg_str(result) |
| 551 |
|
| 552 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 553 |
def test_missing_params_for_response_plan_task(self, connection): |
| 554 |
"""Test that missing parameters for response_plan_task target returns an error.""" |
| 555 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 556 |
self._plugin._connection._shell = MagicMock() |
| 557 |
|
| 558 |
self._plugin._task.args = { |
| 559 |
"target_type": "response_plan_task", |
| 560 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 561 |
# Missing response_plan_id, phase_id, task_id |
| 562 |
} |
| 563 |
|
| 564 |
result = self._plugin.run(task_vars=self._task_vars) |
| 565 |
|
| 566 |
assert result["failed"] is True |
| 567 |
assert "response_plan_id" in _get_msg_str(result) or "phase_id" in _get_msg_str(result) |
| 568 |
|
| 569 |
# Custom API Path Tests |
| 570 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 571 |
def test_custom_api_path(self, connection, monkeypatch): |
| 572 |
"""Test that custom API path parameters are used.""" |
| 573 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 574 |
self._plugin._connection._shell = MagicMock() |
| 575 |
|
| 576 |
captured_paths = [] |
| 577 |
|
| 578 |
def get_by_path(self, path, query_params=None): |
| 579 |
captured_paths.append(path) |
| 580 |
return copy.deepcopy(NOTES_API_RESPONSE) |
| 581 |
|
| 582 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 583 |
|
| 584 |
self._plugin._task.args = { |
| 585 |
"target_type": "investigation", |
| 586 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 587 |
"api_namespace": "customNS", |
| 588 |
"api_user": "customuser", |
| 589 |
"api_app": "CustomApp", |
| 590 |
} |
| 591 |
|
| 592 |
result = self._plugin.run(task_vars=self._task_vars) |
| 593 |
|
| 594 |
assert result["changed"] is False |
| 595 |
assert result.get("failed") is not True |
| 596 |
assert len(captured_paths) > 0 |
| 597 |
assert "customNS" in captured_paths[0] |
| 598 |
assert "customuser" in captured_paths[0] |
| 599 |
assert "CustomApp" in captured_paths[0] |
| 600 |
|
| 601 |
# Always Changed=False Tests |
| 602 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 603 |
def test_always_changed_false(self, connection, monkeypatch): |
| 604 |
"""Verify that info module always returns changed=False. |
| 605 |
|
| 606 |
Info modules are read-only and should never report changes. |
| 607 |
""" |
| 608 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 609 |
self._plugin._connection._shell = MagicMock() |
| 610 |
|
| 611 |
def get_by_path(self, path, query_params=None): |
| 612 |
return copy.deepcopy(NOTES_API_RESPONSE) |
| 613 |
|
| 614 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 615 |
|
| 616 |
# Test with various query types |
| 617 |
test_cases = [ |
| 618 |
{ |
| 619 |
"target_type": "investigation", |
| 620 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 621 |
}, |
| 622 |
{ |
| 623 |
"target_type": "investigation", |
| 624 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 625 |
"note_id": NOTE_UUID_1, |
| 626 |
}, |
| 627 |
{ |
| 628 |
"target_type": "finding", |
| 629 |
"finding_ref_id": FINDING_REF_ID, |
| 630 |
}, |
| 631 |
{ |
| 632 |
"target_type": "investigation", |
| 633 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 634 |
"limit": 10, |
| 635 |
}, |
| 636 |
] |
| 637 |
|
| 638 |
for args in test_cases: |
| 639 |
self._plugin._task.args = args |
| 640 |
result = self._plugin.run(task_vars=self._task_vars) |
| 641 |
assert result["changed"] is False, f"Expected changed=False for args: {args}" |
| 642 |
|
| 643 |
# Field Mapping Tests |
| 644 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 645 |
def test_field_mapping(self, connection, monkeypatch): |
| 646 |
"""Test that API fields are correctly mapped to module format.""" |
| 647 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 648 |
self._plugin._connection._shell = MagicMock() |
| 649 |
|
| 650 |
def get_by_path(self, path, query_params=None): |
| 651 |
return copy.deepcopy(NOTES_API_RESPONSE) |
| 652 |
|
| 653 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 654 |
|
| 655 |
self._plugin._task.args = { |
| 656 |
"target_type": "investigation", |
| 657 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 658 |
} |
| 659 |
|
| 660 |
result = self._plugin.run(task_vars=self._task_vars) |
| 661 |
|
| 662 |
note = result["notes"][0] |
| 663 |
|
| 664 |
# Verify field mapping (id -> note_id) |
| 665 |
assert "note_id" in note |
| 666 |
assert "content" in note |
| 667 |
assert note["note_id"] == NOTE_UUID_1 |
| 668 |
assert note["content"] == "First note content." |
| 669 |
|
| 670 |
# Consistency Tests |
| 671 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 672 |
def test_returns_list(self, connection, monkeypatch): |
| 673 |
"""Test that notes are always returned as a list.""" |
| 674 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 675 |
self._plugin._connection._shell = MagicMock() |
| 676 |
|
| 677 |
def get_by_path(self, path, query_params=None): |
| 678 |
return copy.deepcopy(NOTES_API_RESPONSE) |
| 679 |
|
| 680 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 681 |
|
| 682 |
self._plugin._task.args = { |
| 683 |
"target_type": "investigation", |
| 684 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 685 |
} |
| 686 |
|
| 687 |
result = self._plugin.run(task_vars=self._task_vars) |
| 688 |
|
| 689 |
assert isinstance(result["notes"], list) |
| 690 |
|
| 691 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 692 |
def test_single_note_returns_list(self, connection, monkeypatch): |
| 693 |
"""Test that even single note query returns a list.""" |
| 694 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 695 |
self._plugin._connection._shell = MagicMock() |
| 696 |
|
| 697 |
def get_by_path(self, path, query_params=None): |
| 698 |
return copy.deepcopy(NOTES_API_RESPONSE) |
| 699 |
|
| 700 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 701 |
|
| 702 |
self._plugin._task.args = { |
| 703 |
"target_type": "investigation", |
| 704 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 705 |
"note_id": NOTE_UUID_1, |
| 706 |
} |
| 707 |
|
| 708 |
result = self._plugin.run(task_vars=self._task_vars) |
| 709 |
|
| 710 |
assert isinstance(result["notes"], list) |
| 711 |
assert len(result["notes"]) == 1 |
| 712 |
|
| 713 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 714 |
def test_empty_returns_list(self, connection, monkeypatch): |
| 715 |
"""Test that empty results are returned as an empty list.""" |
| 716 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 717 |
self._plugin._connection._shell = MagicMock() |
| 718 |
|
| 719 |
def get_by_path(self, path, query_params=None): |
| 720 |
return copy.deepcopy(EMPTY_NOTES_RESPONSE) |
| 721 |
|
| 722 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 723 |
|
| 724 |
self._plugin._task.args = { |
| 725 |
"target_type": "investigation", |
| 726 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 727 |
} |
| 728 |
|
| 729 |
result = self._plugin.run(task_vars=self._task_vars) |
| 730 |
|
| 731 |
assert isinstance(result["notes"], list) |
| 732 |
assert len(result["notes"]) == 0 |
| 733 |
|
| 734 |
|
| 735 |
class TestEsNotesInfoHelperMethods: |
| 736 |
"""Tests for the helper methods in the splunk_notes_info action plugin.""" |
| 737 |
|
| 738 |
def setup_method(self): |
| 739 |
"""Set up test fixtures before each test method.""" |
| 740 |
task = MagicMock(Task) |
| 741 |
task.check_mode = False |
| 742 |
|
| 743 |
play_context = MagicMock() |
| 744 |
connection = patch( |
| 745 |
"ansible_collections.splunk.es.plugins.module_utils.splunk.Connection", |
| 746 |
) |
| 747 |
|
| 748 |
fake_loader = {} |
| 749 |
templar = Templar(loader=fake_loader) |
| 750 |
|
| 751 |
self._plugin = ActionModule( |
| 752 |
task=task, |
| 753 |
connection=connection, |
| 754 |
play_context=play_context, |
| 755 |
loader=fake_loader, |
| 756 |
templar=templar, |
| 757 |
shared_loader_obj=None, |
| 758 |
) |
| 759 |
|
| 760 |
self._plugin._task.action = "splunk_notes_info" |
| 761 |
self._plugin._task.async_val = False |
| 762 |
|
| 763 |
def test_validate_target_params_finding_valid(self): |
| 764 |
"""Test validation passes for finding with finding_ref_id.""" |
| 765 |
args = {"finding_ref_id": FINDING_REF_ID} |
| 766 |
|
| 767 |
result = validate_target_params("finding", args) |
| 768 |
|
| 769 |
assert result is None |
| 770 |
|
| 771 |
def test_validate_target_params_finding_missing(self): |
| 772 |
"""Test validation fails for finding without finding_ref_id.""" |
| 773 |
args = {} |
| 774 |
|
| 775 |
result = validate_target_params("finding", args) |
| 776 |
|
| 777 |
assert result is not None |
| 778 |
assert "finding_ref_id" in result |
| 779 |
|
| 780 |
def test_validate_target_params_investigation_valid(self): |
| 781 |
"""Test validation passes for investigation with investigation_ref_id.""" |
| 782 |
args = {"investigation_ref_id": INVESTIGATION_UUID} |
| 783 |
|
| 784 |
result = validate_target_params("investigation", args) |
| 785 |
|
| 786 |
assert result is None |
| 787 |
|
| 788 |
def test_validate_target_params_investigation_missing(self): |
| 789 |
"""Test validation fails for investigation without investigation_ref_id.""" |
| 790 |
args = {} |
| 791 |
|
| 792 |
result = validate_target_params("investigation", args) |
| 793 |
|
| 794 |
assert result is not None |
| 795 |
assert "investigation_ref_id" in result |
| 796 |
|
| 797 |
def test_validate_target_params_response_plan_task_valid(self): |
| 798 |
"""Test validation passes for response_plan_task with all required params.""" |
| 799 |
args = { |
| 800 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 801 |
"response_plan_id": RESPONSE_PLAN_UUID, |
| 802 |
"phase_id": PHASE_UUID, |
| 803 |
"task_id": TASK_UUID, |
| 804 |
} |
| 805 |
|
| 806 |
result = validate_target_params("response_plan_task", args) |
| 807 |
|
| 808 |
assert result is None |
| 809 |
|
| 810 |
def test_validate_target_params_response_plan_task_missing(self): |
| 811 |
"""Test validation fails for response_plan_task with missing params.""" |
| 812 |
args = {"investigation_ref_id": INVESTIGATION_UUID} |
| 813 |
|
| 814 |
result = validate_target_params("response_plan_task", args) |
| 815 |
|
| 816 |
assert result is not None |
| 817 |
assert "response_plan_id" in result |