| 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 action plugin and module utilities. |
| 21 |
""" |
| 22 |
|
| 23 |
|
| 24 |
import copy |
| 25 |
import tempfile |
| 26 |
|
| 27 |
from unittest.mock import MagicMock, patch |
| 28 |
|
| 29 |
from ansible.playbook.task import Task |
| 30 |
from ansible.template import Templar |
| 31 |
|
| 32 |
from ansible_collections.splunk.es.plugins.action.splunk_notes import ActionModule |
| 33 |
from ansible_collections.splunk.es.plugins.module_utils.notes import ( |
| 34 |
build_note_api_path, |
| 35 |
build_notes_api_path, |
| 36 |
build_task_note_api_path, |
| 37 |
build_task_notes_api_path, |
| 38 |
map_note_from_api, |
| 39 |
map_note_to_api, |
| 40 |
validate_target_params, |
| 41 |
) |
| 42 |
from ansible_collections.splunk.es.plugins.module_utils.splunk import SplunkRequest |
| 43 |
|
| 44 |
|
| 45 |
def _get_msg_str(result: dict) -> str: |
| 46 |
"""Get message from result as a lowercase string. |
| 47 |
|
| 48 |
Handles both string and list message formats that Ansible can return. |
| 49 |
|
| 50 |
Args: |
| 51 |
result: The result dictionary from module execution. |
| 52 |
|
| 53 |
Returns: |
| 54 |
The message as a lowercase string. |
| 55 |
""" |
| 56 |
msg = result.get("msg", "") |
| 57 |
if isinstance(msg, list): |
| 58 |
return " ".join(str(m) for m in msg).lower() |
| 59 |
return str(msg).lower() |
| 60 |
|
| 61 |
|
| 62 |
# Test data |
| 63 |
FINDING_REF_ID = "2008e99d-af14-4fec-89da-b9b17a81820a@@notable@@time1768225865" |
| 64 |
INVESTIGATION_UUID = "590afa9c-23d5-4377-b909-cd2cfa1bc0f1" |
| 65 |
RESPONSE_PLAN_UUID = "b9ef7dce-6dcd-4900-b5d5-982fc194554a" |
| 66 |
PHASE_UUID = "phase-001-uuid" |
| 67 |
TASK_UUID = "task-001-uuid" |
| 68 |
NOTE_UUID = "note-abc123" |
| 69 |
|
| 70 |
# Note API response |
| 71 |
NOTE_RESPONSE = { |
| 72 |
"id": NOTE_UUID, |
| 73 |
"content": "This is the note content.", |
| 74 |
} |
| 75 |
|
| 76 |
NOTE_RESPONSE_MINIMAL = { |
| 77 |
"id": NOTE_UUID, |
| 78 |
"content": "Minimal note content.", |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
class TestNotesModuleUtils: |
| 83 |
"""Tests for the notes module utility functions.""" |
| 84 |
|
| 85 |
# Path Builder Tests |
| 86 |
def test_build_notes_api_path_defaults(self): |
| 87 |
"""Test building notes path with default values.""" |
| 88 |
result = build_notes_api_path(INVESTIGATION_UUID) |
| 89 |
|
| 90 |
expected = ( |
| 91 |
f"servicesNS/nobody/missioncontrol/public/v2/investigations/{INVESTIGATION_UUID}/notes" |
| 92 |
) |
| 93 |
assert result == expected |
| 94 |
|
| 95 |
def test_build_notes_api_path_custom(self): |
| 96 |
"""Test building notes path with custom values.""" |
| 97 |
result = build_notes_api_path( |
| 98 |
investigation_id=INVESTIGATION_UUID, |
| 99 |
namespace="customNS", |
| 100 |
user="customuser", |
| 101 |
app="CustomApp", |
| 102 |
) |
| 103 |
|
| 104 |
expected = ( |
| 105 |
f"customNS/customuser/CustomApp/public/v2/investigations/{INVESTIGATION_UUID}/notes" |
| 106 |
) |
| 107 |
assert result == expected |
| 108 |
|
| 109 |
def test_build_note_api_path_defaults(self): |
| 110 |
"""Test building specific note path with default values.""" |
| 111 |
result = build_note_api_path(INVESTIGATION_UUID, NOTE_UUID) |
| 112 |
|
| 113 |
expected = f"servicesNS/nobody/missioncontrol/public/v2/investigations/{INVESTIGATION_UUID}/notes/{NOTE_UUID}" |
| 114 |
assert result == expected |
| 115 |
|
| 116 |
def test_build_note_api_path_custom(self): |
| 117 |
"""Test building specific note path with custom values.""" |
| 118 |
result = build_note_api_path( |
| 119 |
investigation_id=INVESTIGATION_UUID, |
| 120 |
note_id=NOTE_UUID, |
| 121 |
namespace="customNS", |
| 122 |
user="customuser", |
| 123 |
app="CustomApp", |
| 124 |
) |
| 125 |
|
| 126 |
expected = f"customNS/customuser/CustomApp/public/v2/investigations/{INVESTIGATION_UUID}/notes/{NOTE_UUID}" |
| 127 |
assert result == expected |
| 128 |
|
| 129 |
def test_build_task_notes_api_path_defaults(self): |
| 130 |
"""Test building task notes path with default values.""" |
| 131 |
result = build_task_notes_api_path( |
| 132 |
investigation_id=INVESTIGATION_UUID, |
| 133 |
response_plan_id=RESPONSE_PLAN_UUID, |
| 134 |
phase_id=PHASE_UUID, |
| 135 |
task_id=TASK_UUID, |
| 136 |
) |
| 137 |
|
| 138 |
expected = ( |
| 139 |
f"servicesNS/nobody/missioncontrol/public/v2/investigations/{INVESTIGATION_UUID}" |
| 140 |
f"/responseplans/{RESPONSE_PLAN_UUID}/phase/{PHASE_UUID}/tasks/{TASK_UUID}/notes" |
| 141 |
) |
| 142 |
assert result == expected |
| 143 |
|
| 144 |
def test_build_task_notes_api_path_custom(self): |
| 145 |
"""Test building task notes path with custom values.""" |
| 146 |
result = build_task_notes_api_path( |
| 147 |
investigation_id=INVESTIGATION_UUID, |
| 148 |
response_plan_id=RESPONSE_PLAN_UUID, |
| 149 |
phase_id=PHASE_UUID, |
| 150 |
task_id=TASK_UUID, |
| 151 |
namespace="customNS", |
| 152 |
user="customuser", |
| 153 |
app="CustomApp", |
| 154 |
) |
| 155 |
|
| 156 |
expected = ( |
| 157 |
f"customNS/customuser/CustomApp/public/v2/investigations/{INVESTIGATION_UUID}" |
| 158 |
f"/responseplans/{RESPONSE_PLAN_UUID}/phase/{PHASE_UUID}/tasks/{TASK_UUID}/notes" |
| 159 |
) |
| 160 |
assert result == expected |
| 161 |
|
| 162 |
def test_build_task_note_api_path_defaults(self): |
| 163 |
"""Test building specific task note path with default values.""" |
| 164 |
result = build_task_note_api_path( |
| 165 |
investigation_id=INVESTIGATION_UUID, |
| 166 |
response_plan_id=RESPONSE_PLAN_UUID, |
| 167 |
phase_id=PHASE_UUID, |
| 168 |
task_id=TASK_UUID, |
| 169 |
note_id=NOTE_UUID, |
| 170 |
) |
| 171 |
|
| 172 |
expected = ( |
| 173 |
f"servicesNS/nobody/missioncontrol/public/v2/investigations/{INVESTIGATION_UUID}" |
| 174 |
f"/responseplans/{RESPONSE_PLAN_UUID}/phase/{PHASE_UUID}/tasks/{TASK_UUID}/notes/{NOTE_UUID}" |
| 175 |
) |
| 176 |
assert result == expected |
| 177 |
|
| 178 |
# Mapping Tests |
| 179 |
def test_map_note_from_api_full(self): |
| 180 |
"""Test mapping a full note response from API.""" |
| 181 |
result = map_note_from_api(copy.deepcopy(NOTE_RESPONSE)) |
| 182 |
|
| 183 |
assert result["note_id"] == NOTE_UUID |
| 184 |
assert result["content"] == "This is the note content." |
| 185 |
|
| 186 |
def test_map_note_from_api_minimal(self): |
| 187 |
"""Test mapping a minimal note response from API.""" |
| 188 |
result = map_note_from_api(copy.deepcopy(NOTE_RESPONSE_MINIMAL)) |
| 189 |
|
| 190 |
assert result["note_id"] == NOTE_UUID |
| 191 |
assert result["content"] == "Minimal note content." |
| 192 |
|
| 193 |
def test_map_note_from_api_empty(self): |
| 194 |
"""Test mapping an empty note response from API.""" |
| 195 |
result = map_note_from_api({}) |
| 196 |
|
| 197 |
assert result["note_id"] == "" |
| 198 |
assert result["content"] == "" |
| 199 |
|
| 200 |
def test_map_note_to_api(self): |
| 201 |
"""Test mapping a note to API payload.""" |
| 202 |
note = {"content": "Test content"} |
| 203 |
|
| 204 |
result = map_note_to_api(note) |
| 205 |
|
| 206 |
assert result["content"] == "Test content" |
| 207 |
|
| 208 |
|
| 209 |
class TestEsNotesActionPlugin: |
| 210 |
"""Test class for the splunk_notes action plugin.""" |
| 211 |
|
| 212 |
def setup_method(self): |
| 213 |
"""Set up test fixtures before each test method.""" |
| 214 |
# Create a mock Task object |
| 215 |
task = MagicMock(Task) |
| 216 |
task.check_mode = False |
| 217 |
|
| 218 |
# Create mock play context |
| 219 |
play_context = MagicMock() |
| 220 |
play_context.check_mode = False |
| 221 |
|
| 222 |
# Create a mock connection |
| 223 |
connection = patch( |
| 224 |
"ansible_collections.splunk.es.plugins.module_utils.splunk.Connection", |
| 225 |
) |
| 226 |
|
| 227 |
# Ansible's template engine |
| 228 |
fake_loader = {} |
| 229 |
templar = Templar(loader=fake_loader) |
| 230 |
|
| 231 |
# Create the action plugin instance |
| 232 |
self._plugin = ActionModule( |
| 233 |
task=task, |
| 234 |
connection=connection, |
| 235 |
play_context=play_context, |
| 236 |
loader=fake_loader, |
| 237 |
templar=templar, |
| 238 |
shared_loader_obj=None, |
| 239 |
) |
| 240 |
|
| 241 |
# Set required task attributes |
| 242 |
self._plugin._task.action = "splunk_notes" |
| 243 |
self._plugin._task.async_val = False |
| 244 |
|
| 245 |
# Task variables |
| 246 |
self._task_vars = {} |
| 247 |
|
| 248 |
# Finding Notes Tests |
| 249 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 250 |
def test_create_finding_note_success(self, connection, monkeypatch): |
| 251 |
"""Test successful creation of a finding note.""" |
| 252 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 253 |
self._plugin._connection._shell = MagicMock() |
| 254 |
|
| 255 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 256 |
return copy.deepcopy(NOTE_RESPONSE) |
| 257 |
|
| 258 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 259 |
|
| 260 |
self._plugin._task.args = { |
| 261 |
"target_type": "finding", |
| 262 |
"finding_ref_id": FINDING_REF_ID, |
| 263 |
"content": "This is a test note.", |
| 264 |
"state": "present", |
| 265 |
} |
| 266 |
|
| 267 |
result = self._plugin.run(task_vars=self._task_vars) |
| 268 |
|
| 269 |
assert result["changed"] is True |
| 270 |
assert result.get("failed") is not True |
| 271 |
assert "note" in result |
| 272 |
assert result["note"]["after"]["note_id"] == NOTE_UUID |
| 273 |
assert "created" in _get_msg_str(result) |
| 274 |
|
| 275 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 276 |
def test_create_finding_note_minimal(self, connection, monkeypatch): |
| 277 |
"""Test creating a finding note with minimal parameters (only content).""" |
| 278 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 279 |
self._plugin._connection._shell = MagicMock() |
| 280 |
|
| 281 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 282 |
return copy.deepcopy(NOTE_RESPONSE_MINIMAL) |
| 283 |
|
| 284 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 285 |
|
| 286 |
self._plugin._task.args = { |
| 287 |
"target_type": "finding", |
| 288 |
"finding_ref_id": FINDING_REF_ID, |
| 289 |
"content": "Minimal note content.", |
| 290 |
"state": "present", |
| 291 |
} |
| 292 |
|
| 293 |
result = self._plugin.run(task_vars=self._task_vars) |
| 294 |
|
| 295 |
assert result["changed"] is True |
| 296 |
assert result.get("failed") is not True |
| 297 |
|
| 298 |
# Investigation Notes Tests |
| 299 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 300 |
def test_create_investigation_note_success(self, connection, monkeypatch): |
| 301 |
"""Test successful creation of an investigation note.""" |
| 302 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 303 |
self._plugin._connection._shell = MagicMock() |
| 304 |
|
| 305 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 306 |
return copy.deepcopy(NOTE_RESPONSE) |
| 307 |
|
| 308 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 309 |
|
| 310 |
self._plugin._task.args = { |
| 311 |
"target_type": "investigation", |
| 312 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 313 |
"content": "Investigation note content.", |
| 314 |
"state": "present", |
| 315 |
} |
| 316 |
|
| 317 |
result = self._plugin.run(task_vars=self._task_vars) |
| 318 |
|
| 319 |
assert result["changed"] is True |
| 320 |
assert result.get("failed") is not True |
| 321 |
assert "created" in _get_msg_str(result) |
| 322 |
|
| 323 |
# Response Plan Task Notes Tests |
| 324 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 325 |
def test_create_task_note_success(self, connection, monkeypatch): |
| 326 |
"""Test successful creation of a response plan task note.""" |
| 327 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 328 |
self._plugin._connection._shell = MagicMock() |
| 329 |
|
| 330 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 331 |
return copy.deepcopy(NOTE_RESPONSE) |
| 332 |
|
| 333 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 334 |
|
| 335 |
self._plugin._task.args = { |
| 336 |
"target_type": "response_plan_task", |
| 337 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 338 |
"response_plan_id": RESPONSE_PLAN_UUID, |
| 339 |
"phase_id": PHASE_UUID, |
| 340 |
"task_id": TASK_UUID, |
| 341 |
"content": "Task note content.", |
| 342 |
"state": "present", |
| 343 |
} |
| 344 |
|
| 345 |
result = self._plugin.run(task_vars=self._task_vars) |
| 346 |
|
| 347 |
assert result["changed"] is True |
| 348 |
assert result.get("failed") is not True |
| 349 |
|
| 350 |
# Update Note Tests |
| 351 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 352 |
def test_update_note_success(self, connection, monkeypatch): |
| 353 |
"""Test successful update of an existing note.""" |
| 354 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 355 |
self._plugin._connection._shell = MagicMock() |
| 356 |
|
| 357 |
def get_by_path(self, path, query_params=None): |
| 358 |
return {"items": [copy.deepcopy(NOTE_RESPONSE)]} |
| 359 |
|
| 360 |
updated_response = copy.deepcopy(NOTE_RESPONSE) |
| 361 |
updated_response["content"] = "Updated content." |
| 362 |
|
| 363 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 364 |
return updated_response |
| 365 |
|
| 366 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 367 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 368 |
|
| 369 |
self._plugin._task.args = { |
| 370 |
"target_type": "investigation", |
| 371 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 372 |
"note_id": NOTE_UUID, |
| 373 |
"content": "Updated content.", |
| 374 |
"state": "present", |
| 375 |
} |
| 376 |
|
| 377 |
result = self._plugin.run(task_vars=self._task_vars) |
| 378 |
|
| 379 |
assert result["changed"] is True |
| 380 |
assert result.get("failed") is not True |
| 381 |
assert "updated" in _get_msg_str(result) |
| 382 |
|
| 383 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 384 |
def test_update_note_idempotent(self, connection, monkeypatch): |
| 385 |
"""Test that update is idempotent when content is the same.""" |
| 386 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 387 |
self._plugin._connection._shell = MagicMock() |
| 388 |
|
| 389 |
def get_by_path(self, path, query_params=None): |
| 390 |
return {"items": [copy.deepcopy(NOTE_RESPONSE)]} |
| 391 |
|
| 392 |
update_called = [] |
| 393 |
|
| 394 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 395 |
update_called.append(True) |
| 396 |
return copy.deepcopy(NOTE_RESPONSE) |
| 397 |
|
| 398 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 399 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 400 |
|
| 401 |
self._plugin._task.args = { |
| 402 |
"target_type": "investigation", |
| 403 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 404 |
"note_id": NOTE_UUID, |
| 405 |
"content": "This is the note content.", # Same as NOTE_RESPONSE |
| 406 |
"state": "present", |
| 407 |
} |
| 408 |
|
| 409 |
result = self._plugin.run(task_vars=self._task_vars) |
| 410 |
|
| 411 |
assert result["changed"] is False |
| 412 |
assert result.get("failed") is not True |
| 413 |
assert len(update_called) == 0 # API should not be called |
| 414 |
assert "no change" in _get_msg_str(result) |
| 415 |
|
| 416 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 417 |
def test_update_note_not_found(self, connection, monkeypatch): |
| 418 |
"""Test update when note doesn't exist.""" |
| 419 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 420 |
self._plugin._connection._shell = MagicMock() |
| 421 |
|
| 422 |
def get_by_path(self, path, query_params=None): |
| 423 |
return {"items": []} |
| 424 |
|
| 425 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 426 |
|
| 427 |
self._plugin._task.args = { |
| 428 |
"target_type": "investigation", |
| 429 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 430 |
"note_id": "non-existent-note", |
| 431 |
"content": "Some content.", |
| 432 |
"state": "present", |
| 433 |
} |
| 434 |
|
| 435 |
result = self._plugin.run(task_vars=self._task_vars) |
| 436 |
|
| 437 |
assert result["failed"] is True |
| 438 |
assert "not found" in _get_msg_str(result) |
| 439 |
|
| 440 |
# Delete Note Tests |
| 441 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 442 |
def test_delete_note_success(self, connection, monkeypatch): |
| 443 |
"""Test successful deletion of a note.""" |
| 444 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 445 |
self._plugin._connection._shell = MagicMock() |
| 446 |
|
| 447 |
def get_by_path(self, path, query_params=None): |
| 448 |
return {"items": [copy.deepcopy(NOTE_RESPONSE)]} |
| 449 |
|
| 450 |
delete_called = [] |
| 451 |
|
| 452 |
def delete_by_path(self, path): |
| 453 |
delete_called.append(path) |
| 454 |
return {} |
| 455 |
|
| 456 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 457 |
monkeypatch.setattr(SplunkRequest, "delete_by_path", delete_by_path) |
| 458 |
|
| 459 |
self._plugin._task.args = { |
| 460 |
"target_type": "investigation", |
| 461 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 462 |
"note_id": NOTE_UUID, |
| 463 |
"state": "absent", |
| 464 |
} |
| 465 |
|
| 466 |
result = self._plugin.run(task_vars=self._task_vars) |
| 467 |
|
| 468 |
assert result["changed"] is True |
| 469 |
assert result.get("failed") is not True |
| 470 |
assert len(delete_called) == 1 |
| 471 |
assert NOTE_UUID in delete_called[0] |
| 472 |
assert "deleted" in _get_msg_str(result) |
| 473 |
|
| 474 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 475 |
def test_delete_note_already_absent(self, connection, monkeypatch): |
| 476 |
"""Test deleting a note that doesn't exist returns changed=False.""" |
| 477 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 478 |
self._plugin._connection._shell = MagicMock() |
| 479 |
|
| 480 |
def get_by_path(self, path, query_params=None): |
| 481 |
return {"items": []} |
| 482 |
|
| 483 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 484 |
|
| 485 |
self._plugin._task.args = { |
| 486 |
"target_type": "investigation", |
| 487 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 488 |
"note_id": NOTE_UUID, |
| 489 |
"state": "absent", |
| 490 |
} |
| 491 |
|
| 492 |
result = self._plugin.run(task_vars=self._task_vars) |
| 493 |
|
| 494 |
assert result["changed"] is False |
| 495 |
assert result.get("failed") is not True |
| 496 |
assert "already absent" in _get_msg_str(result) or "not found" in _get_msg_str(result) |
| 497 |
|
| 498 |
# Check Mode Tests |
| 499 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 500 |
def test_create_note_check_mode(self, connection, monkeypatch): |
| 501 |
"""Test check mode for creating a note.""" |
| 502 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 503 |
self._plugin._connection._shell = MagicMock() |
| 504 |
|
| 505 |
# Enable check mode |
| 506 |
self._plugin._task.check_mode = True |
| 507 |
|
| 508 |
create_called = [] |
| 509 |
|
| 510 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 511 |
create_called.append(True) |
| 512 |
return copy.deepcopy(NOTE_RESPONSE) |
| 513 |
|
| 514 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 515 |
|
| 516 |
self._plugin._task.args = { |
| 517 |
"target_type": "investigation", |
| 518 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 519 |
"content": "Check mode test content.", |
| 520 |
"state": "present", |
| 521 |
} |
| 522 |
|
| 523 |
result = self._plugin.run(task_vars=self._task_vars) |
| 524 |
|
| 525 |
assert result["changed"] is True |
| 526 |
assert result.get("failed") is not True |
| 527 |
assert len(create_called) == 0 # API should not be called |
| 528 |
assert "check mode" in _get_msg_str(result) |
| 529 |
|
| 530 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 531 |
def test_update_note_check_mode(self, connection, monkeypatch): |
| 532 |
"""Test check mode for updating a note.""" |
| 533 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 534 |
self._plugin._connection._shell = MagicMock() |
| 535 |
|
| 536 |
# Enable check mode |
| 537 |
self._plugin._task.check_mode = True |
| 538 |
|
| 539 |
def get_by_path(self, path, query_params=None): |
| 540 |
return {"items": [copy.deepcopy(NOTE_RESPONSE)]} |
| 541 |
|
| 542 |
update_called = [] |
| 543 |
|
| 544 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 545 |
update_called.append(True) |
| 546 |
return {} |
| 547 |
|
| 548 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 549 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 550 |
|
| 551 |
self._plugin._task.args = { |
| 552 |
"target_type": "investigation", |
| 553 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 554 |
"note_id": NOTE_UUID, |
| 555 |
"content": "Updated content for check mode.", |
| 556 |
"state": "present", |
| 557 |
} |
| 558 |
|
| 559 |
result = self._plugin.run(task_vars=self._task_vars) |
| 560 |
|
| 561 |
assert result["changed"] is True |
| 562 |
assert result.get("failed") is not True |
| 563 |
assert len(update_called) == 0 # API should not be called |
| 564 |
assert "check mode" in _get_msg_str(result) |
| 565 |
|
| 566 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 567 |
def test_delete_note_check_mode(self, connection, monkeypatch): |
| 568 |
"""Test check mode for deleting a note.""" |
| 569 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 570 |
self._plugin._connection._shell = MagicMock() |
| 571 |
|
| 572 |
# Enable check mode |
| 573 |
self._plugin._task.check_mode = True |
| 574 |
|
| 575 |
def get_by_path(self, path, query_params=None): |
| 576 |
return {"items": [copy.deepcopy(NOTE_RESPONSE)]} |
| 577 |
|
| 578 |
delete_called = [] |
| 579 |
|
| 580 |
def delete_by_path(self, path): |
| 581 |
delete_called.append(True) |
| 582 |
return {} |
| 583 |
|
| 584 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 585 |
monkeypatch.setattr(SplunkRequest, "delete_by_path", delete_by_path) |
| 586 |
|
| 587 |
self._plugin._task.args = { |
| 588 |
"target_type": "investigation", |
| 589 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 590 |
"note_id": NOTE_UUID, |
| 591 |
"state": "absent", |
| 592 |
} |
| 593 |
|
| 594 |
result = self._plugin.run(task_vars=self._task_vars) |
| 595 |
|
| 596 |
assert result["changed"] is True |
| 597 |
assert result.get("failed") is not True |
| 598 |
assert len(delete_called) == 0 # DELETE should not be called |
| 599 |
assert "check mode" in _get_msg_str(result) |
| 600 |
|
| 601 |
# Validation Tests |
| 602 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 603 |
def test_missing_target_type(self, connection): |
| 604 |
"""Test that missing target_type returns an error.""" |
| 605 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 606 |
self._plugin._connection._shell = MagicMock() |
| 607 |
|
| 608 |
self._plugin._task.args = { |
| 609 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 610 |
"content": "Test content.", |
| 611 |
"state": "present", |
| 612 |
} |
| 613 |
|
| 614 |
result = self._plugin.run(task_vars=self._task_vars) |
| 615 |
|
| 616 |
assert result["failed"] is True |
| 617 |
|
| 618 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 619 |
def test_missing_ref_id_for_finding(self, connection): |
| 620 |
"""Test that missing ref_id for finding target returns an error.""" |
| 621 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 622 |
self._plugin._connection._shell = MagicMock() |
| 623 |
|
| 624 |
self._plugin._task.args = { |
| 625 |
"target_type": "finding", |
| 626 |
"content": "Test content.", |
| 627 |
"state": "present", |
| 628 |
} |
| 629 |
|
| 630 |
result = self._plugin.run(task_vars=self._task_vars) |
| 631 |
|
| 632 |
assert result["failed"] is True |
| 633 |
assert "finding_ref_id" in _get_msg_str(result) |
| 634 |
|
| 635 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 636 |
def test_missing_investigation_ref_id_for_investigation(self, connection): |
| 637 |
"""Test that missing investigation_ref_id for investigation target returns an error.""" |
| 638 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 639 |
self._plugin._connection._shell = MagicMock() |
| 640 |
|
| 641 |
self._plugin._task.args = { |
| 642 |
"target_type": "investigation", |
| 643 |
"content": "Test content.", |
| 644 |
"state": "present", |
| 645 |
} |
| 646 |
|
| 647 |
result = self._plugin.run(task_vars=self._task_vars) |
| 648 |
|
| 649 |
assert result["failed"] is True |
| 650 |
assert "investigation_ref_id" in _get_msg_str(result) |
| 651 |
|
| 652 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 653 |
def test_missing_params_for_response_plan_task(self, connection): |
| 654 |
"""Test that missing parameters for response_plan_task target returns an error.""" |
| 655 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 656 |
self._plugin._connection._shell = MagicMock() |
| 657 |
|
| 658 |
self._plugin._task.args = { |
| 659 |
"target_type": "response_plan_task", |
| 660 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 661 |
# Missing response_plan_id, phase_id, task_id |
| 662 |
"content": "Test content.", |
| 663 |
"state": "present", |
| 664 |
} |
| 665 |
|
| 666 |
result = self._plugin.run(task_vars=self._task_vars) |
| 667 |
|
| 668 |
assert result["failed"] is True |
| 669 |
assert "response_plan_id" in _get_msg_str(result) or "phase_id" in _get_msg_str(result) |
| 670 |
|
| 671 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 672 |
def test_missing_content_for_present_state(self, connection): |
| 673 |
"""Test that missing content for state=present returns an error.""" |
| 674 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 675 |
self._plugin._connection._shell = MagicMock() |
| 676 |
|
| 677 |
self._plugin._task.args = { |
| 678 |
"target_type": "investigation", |
| 679 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 680 |
"state": "present", |
| 681 |
# Missing content |
| 682 |
} |
| 683 |
|
| 684 |
result = self._plugin.run(task_vars=self._task_vars) |
| 685 |
|
| 686 |
assert result["failed"] is True |
| 687 |
assert "content" in _get_msg_str(result) |
| 688 |
|
| 689 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 690 |
def test_missing_note_id_for_absent_state(self, connection): |
| 691 |
"""Test that missing note_id for state=absent returns an error.""" |
| 692 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 693 |
self._plugin._connection._shell = MagicMock() |
| 694 |
|
| 695 |
self._plugin._task.args = { |
| 696 |
"target_type": "investigation", |
| 697 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 698 |
"state": "absent", |
| 699 |
# Missing note_id |
| 700 |
} |
| 701 |
|
| 702 |
result = self._plugin.run(task_vars=self._task_vars) |
| 703 |
|
| 704 |
assert result["failed"] is True |
| 705 |
assert "note_id" in _get_msg_str(result) |
| 706 |
|
| 707 |
# Custom API Path Tests |
| 708 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 709 |
def test_custom_api_path(self, connection, monkeypatch): |
| 710 |
"""Test that custom API path parameters are used.""" |
| 711 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 712 |
self._plugin._connection._shell = MagicMock() |
| 713 |
|
| 714 |
captured_paths = [] |
| 715 |
|
| 716 |
def create_update(self, rest_path, data=None, query_params=None, json_payload=False): |
| 717 |
captured_paths.append(rest_path) |
| 718 |
return copy.deepcopy(NOTE_RESPONSE) |
| 719 |
|
| 720 |
monkeypatch.setattr(SplunkRequest, "create_update", create_update) |
| 721 |
|
| 722 |
self._plugin._task.args = { |
| 723 |
"target_type": "investigation", |
| 724 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 725 |
"content": "Test content.", |
| 726 |
"state": "present", |
| 727 |
"api_namespace": "customNS", |
| 728 |
"api_user": "customuser", |
| 729 |
"api_app": "CustomApp", |
| 730 |
} |
| 731 |
|
| 732 |
result = self._plugin.run(task_vars=self._task_vars) |
| 733 |
|
| 734 |
assert result["changed"] is True |
| 735 |
assert len(captured_paths) >= 1 |
| 736 |
for path in captured_paths: |
| 737 |
assert "customNS" in path |
| 738 |
assert "customuser" in path |
| 739 |
assert "CustomApp" in path |
| 740 |
|
| 741 |
|
| 742 |
class TestEsNotesHelperMethods: |
| 743 |
"""Tests for the helper methods in the splunk_notes action plugin.""" |
| 744 |
|
| 745 |
def setup_method(self): |
| 746 |
"""Set up test fixtures before each test method.""" |
| 747 |
task = MagicMock(Task) |
| 748 |
task.check_mode = False |
| 749 |
|
| 750 |
play_context = MagicMock() |
| 751 |
connection = patch( |
| 752 |
"ansible_collections.splunk.es.plugins.module_utils.splunk.Connection", |
| 753 |
) |
| 754 |
|
| 755 |
fake_loader = {} |
| 756 |
templar = Templar(loader=fake_loader) |
| 757 |
|
| 758 |
self._plugin = ActionModule( |
| 759 |
task=task, |
| 760 |
connection=connection, |
| 761 |
play_context=play_context, |
| 762 |
loader=fake_loader, |
| 763 |
templar=templar, |
| 764 |
shared_loader_obj=None, |
| 765 |
) |
| 766 |
|
| 767 |
self._plugin._task.action = "splunk_notes" |
| 768 |
self._plugin._task.async_val = False |
| 769 |
|
| 770 |
def test_validate_target_params_finding_valid(self): |
| 771 |
"""Test validation passes for finding with finding_ref_id.""" |
| 772 |
args = {"finding_ref_id": FINDING_REF_ID} |
| 773 |
|
| 774 |
result = validate_target_params("finding", args) |
| 775 |
|
| 776 |
assert result is None |
| 777 |
|
| 778 |
def test_validate_target_params_finding_missing_finding_ref_id(self): |
| 779 |
"""Test validation fails for finding without finding_ref_id.""" |
| 780 |
args = {} |
| 781 |
|
| 782 |
result = validate_target_params("finding", args) |
| 783 |
|
| 784 |
assert result is not None |
| 785 |
assert "finding_ref_id" in result |
| 786 |
|
| 787 |
def test_validate_target_params_investigation_valid(self): |
| 788 |
"""Test validation passes for investigation with investigation_ref_id.""" |
| 789 |
args = {"investigation_ref_id": INVESTIGATION_UUID} |
| 790 |
|
| 791 |
result = validate_target_params("investigation", args) |
| 792 |
|
| 793 |
assert result is None |
| 794 |
|
| 795 |
def test_validate_target_params_investigation_missing_ref_id(self): |
| 796 |
"""Test validation fails for investigation without investigation_ref_id.""" |
| 797 |
args = {} |
| 798 |
|
| 799 |
result = validate_target_params("investigation", args) |
| 800 |
|
| 801 |
assert result is not None |
| 802 |
assert "investigation_ref_id" in result |
| 803 |
|
| 804 |
def test_validate_target_params_response_plan_task_valid(self): |
| 805 |
"""Test validation passes for response_plan_task with all required params.""" |
| 806 |
args = { |
| 807 |
"investigation_ref_id": INVESTIGATION_UUID, |
| 808 |
"response_plan_id": RESPONSE_PLAN_UUID, |
| 809 |
"phase_id": PHASE_UUID, |
| 810 |
"task_id": TASK_UUID, |
| 811 |
} |
| 812 |
|
| 813 |
result = validate_target_params("response_plan_task", args) |
| 814 |
|
| 815 |
assert result is None |
| 816 |
|
| 817 |
def test_validate_target_params_response_plan_task_missing_params(self): |
| 818 |
"""Test validation fails for response_plan_task with missing params.""" |
| 819 |
args = {"investigation_ref_id": INVESTIGATION_UUID} |
| 820 |
|
| 821 |
result = validate_target_params("response_plan_task", args) |
| 822 |
|
| 823 |
assert result is not None |
| 824 |
assert "response_plan_id" in result |
| 825 |
|
| 826 |
def test_validate_state_params_present_valid(self): |
| 827 |
"""Test validation passes for present state with content.""" |
| 828 |
self._plugin._task.args = {"content": "Test content"} |
| 829 |
|
| 830 |
result = self._plugin._validate_state_params("present", None) |
| 831 |
|
| 832 |
assert result is None |
| 833 |
|
| 834 |
def test_validate_state_params_present_missing_content(self): |
| 835 |
"""Test validation fails for present state without content.""" |
| 836 |
self._plugin._task.args = {} |
| 837 |
|
| 838 |
result = self._plugin._validate_state_params("present", None) |
| 839 |
|
| 840 |
assert result is not None |
| 841 |
assert "content" in result |
| 842 |
|
| 843 |
def test_validate_state_params_absent_valid(self): |
| 844 |
"""Test validation passes for absent state with note_id.""" |
| 845 |
self._plugin._task.args = {} |
| 846 |
|
| 847 |
result = self._plugin._validate_state_params("absent", NOTE_UUID) |
| 848 |
|
| 849 |
assert result is None |
| 850 |
|
| 851 |
def test_validate_state_params_absent_missing_note_id(self): |
| 852 |
"""Test validation fails for absent state without note_id.""" |
| 853 |
self._plugin._task.args = {} |
| 854 |
|
| 855 |
result = self._plugin._validate_state_params("absent", None) |
| 856 |
|
| 857 |
assert result is not None |
| 858 |
assert "note_id" in result |
| 859 |
|
| 860 |
def test_compare_notes_same(self): |
| 861 |
"""Test comparing identical notes returns False (no difference).""" |
| 862 |
existing = {"content": "Same content"} |
| 863 |
desired = {"content": "Same content"} |
| 864 |
|
| 865 |
result = self._plugin._compare_notes(existing, desired) |
| 866 |
|
| 867 |
assert result is False |
| 868 |
|
| 869 |
def test_compare_notes_different_content(self): |
| 870 |
"""Test comparing notes with different content returns True.""" |
| 871 |
existing = {"content": "Original content"} |
| 872 |
desired = {"content": "Updated content"} |
| 873 |
|
| 874 |
result = self._plugin._compare_notes(existing, desired) |
| 875 |
|
| 876 |
assert result is True |
| 877 |
|
| 878 |
def test_build_note_params(self): |
| 879 |
"""Test building note parameters from task args.""" |
| 880 |
self._plugin._task.args = { |
| 881 |
"content": "Test content", |
| 882 |
} |
| 883 |
|
| 884 |
result = self._plugin._build_note_params() |
| 885 |
|
| 886 |
assert result["content"] == "Test content" |