| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
""" |
| 20 |
Unit tests for the splunk_investigation_info action plugin. |
| 21 |
|
| 22 |
These tests verify that the splunk_investigation_info action plugin works correctly for: |
| 23 |
- Querying a specific investigation by investigation_ref_id |
| 24 |
- Querying investigations by name (with filtering) |
| 25 |
- Querying all investigations |
| 26 |
- Handling time range parameters (create_time_min/create_time_max) |
| 27 |
- Handling not found scenarios |
| 28 |
|
| 29 |
The tests use mocking to simulate Splunk API responses. |
| 30 |
""" |
| 31 |
|
| 32 |
import copy |
| 33 |
import tempfile |
| 34 |
|
| 35 |
from unittest.mock import MagicMock, patch |
| 36 |
|
| 37 |
from ansible.playbook.task import Task |
| 38 |
from ansible.template import Templar |
| 39 |
|
| 40 |
from ansible_collections.splunk.es.plugins.action.splunk_investigation_info import ActionModule |
| 41 |
from ansible_collections.splunk.es.plugins.module_utils.splunk import SplunkRequest |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
INVESTIGATION_API_RESPONSE_SINGLE = { |
| 48 |
"investigation_guid": "inv-12345-abcde", |
| 49 |
"name": "Security Incident 2026-01", |
| 50 |
"description": "Investigation into suspicious login activity", |
| 51 |
"owner": "admin", |
| 52 |
"status": "1", |
| 53 |
"urgency": "high", |
| 54 |
"disposition": "disposition:6", |
| 55 |
"sensitivity": "Amber", |
| 56 |
"consolidated_findings": { |
| 57 |
"event_id": ["finding-001@@notable@@time123"], |
| 58 |
}, |
| 59 |
} |
| 60 |
|
| 61 |
INVESTIGATION_API_RESPONSE_LIST = [ |
| 62 |
{ |
| 63 |
"investigation_guid": "inv-001-aaaaa", |
| 64 |
"name": "Security Incident 2026-01", |
| 65 |
"description": "Investigation into suspicious login activity", |
| 66 |
"owner": "admin", |
| 67 |
"status": "1", |
| 68 |
"urgency": "high", |
| 69 |
"disposition": "disposition:6", |
| 70 |
"sensitivity": "Amber", |
| 71 |
}, |
| 72 |
{ |
| 73 |
"investigation_guid": "inv-002-bbbbb", |
| 74 |
"name": "Malware Investigation", |
| 75 |
"description": "Potential malware detected", |
| 76 |
"owner": "analyst", |
| 77 |
"status": "2", |
| 78 |
"urgency": "critical", |
| 79 |
"disposition": "disposition:1", |
| 80 |
"sensitivity": "Red", |
| 81 |
}, |
| 82 |
{ |
| 83 |
"investigation_guid": "inv-003-ccccc", |
| 84 |
"name": "Security Incident 2026-01", |
| 85 |
"description": "Another investigation with same name", |
| 86 |
"owner": "admin", |
| 87 |
"status": "1", |
| 88 |
"urgency": "medium", |
| 89 |
"disposition": "disposition:6", |
| 90 |
"sensitivity": "Green", |
| 91 |
}, |
| 92 |
] |
| 93 |
|
| 94 |
EMPTY_INVESTIGATIONS_RESPONSE = [] |
| 95 |
|
| 96 |
|
| 97 |
class TestSplunkInvestigationInfo: |
| 98 |
"""Test class for the splunk_investigation_info action plugin. |
| 99 |
|
| 100 |
The splunk_investigation_info module is a "read-only" info module that queries |
| 101 |
Splunk for investigations without making changes. It should always return |
| 102 |
changed=False. |
| 103 |
|
| 104 |
Query modes: |
| 105 |
1. By investigation_ref_id: Returns a single specific investigation |
| 106 |
2. By name: Returns all investigations matching the exact name |
| 107 |
3. All: Returns all investigations (when no filters provided) |
| 108 |
""" |
| 109 |
|
| 110 |
def setup_method(self): |
| 111 |
"""Set up test fixtures before each test method. |
| 112 |
|
| 113 |
Creates mock Ansible components needed to test the action plugin. |
| 114 |
""" |
| 115 |
|
| 116 |
task = MagicMock(Task) |
| 117 |
task.check_mode = False |
| 118 |
|
| 119 |
|
| 120 |
play_context = MagicMock() |
| 121 |
play_context.check_mode = False |
| 122 |
|
| 123 |
|
| 124 |
connection = patch( |
| 125 |
"ansible_collections.splunk.es.plugins.module_utils.splunk.Connection", |
| 126 |
) |
| 127 |
|
| 128 |
|
| 129 |
fake_loader = {} |
| 130 |
templar = Templar(loader=fake_loader) |
| 131 |
|
| 132 |
|
| 133 |
self._plugin = ActionModule( |
| 134 |
task=task, |
| 135 |
connection=connection, |
| 136 |
play_context=play_context, |
| 137 |
loader=fake_loader, |
| 138 |
templar=templar, |
| 139 |
shared_loader_obj=None, |
| 140 |
) |
| 141 |
|
| 142 |
|
| 143 |
self._plugin._task.action = "splunk_investigation_info" |
| 144 |
self._plugin._task.async_val = False |
| 145 |
|
| 146 |
|
| 147 |
self._task_vars = {} |
| 148 |
|
| 149 |
|
| 150 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 151 |
def test_investigation_info_by_ref_id(self, connection, monkeypatch): |
| 152 |
"""Test querying a specific investigation by investigation_ref_id. |
| 153 |
|
| 154 |
When investigation_ref_id is provided, the module should query for that |
| 155 |
specific investigation and return it in a list (for consistency). |
| 156 |
""" |
| 157 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 158 |
self._plugin._connection._shell = MagicMock() |
| 159 |
|
| 160 |
def get_by_path(self, path, query_params=None): |
| 161 |
|
| 162 |
if query_params and query_params.get("ids") == "inv-12345-abcde": |
| 163 |
return [copy.deepcopy(INVESTIGATION_API_RESPONSE_SINGLE)] |
| 164 |
return [] |
| 165 |
|
| 166 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 167 |
|
| 168 |
self._plugin._task.args = { |
| 169 |
"investigation_ref_id": "inv-12345-abcde", |
| 170 |
} |
| 171 |
|
| 172 |
result = self._plugin.run(task_vars=self._task_vars) |
| 173 |
|
| 174 |
|
| 175 |
assert result["changed"] is False |
| 176 |
assert result.get("failed") is not True |
| 177 |
assert "investigations" in result |
| 178 |
assert len(result["investigations"]) == 1 |
| 179 |
assert result["investigations"][0]["name"] == "Security Incident 2026-01" |
| 180 |
assert result["investigations"][0]["investigation_ref_id"] == "inv-12345-abcde" |
| 181 |
|
| 182 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 183 |
def test_investigation_info_by_ref_id_not_found(self, connection, monkeypatch): |
| 184 |
"""Test querying a non-existent investigation by investigation_ref_id. |
| 185 |
|
| 186 |
When the investigation doesn't exist, the module should return an empty |
| 187 |
list rather than failing. |
| 188 |
""" |
| 189 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 190 |
self._plugin._connection._shell = MagicMock() |
| 191 |
|
| 192 |
def get_by_path(self, path, query_params=None): |
| 193 |
return [] |
| 194 |
|
| 195 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 196 |
|
| 197 |
self._plugin._task.args = { |
| 198 |
"investigation_ref_id": "non-existent-id", |
| 199 |
} |
| 200 |
|
| 201 |
result = self._plugin.run(task_vars=self._task_vars) |
| 202 |
|
| 203 |
assert result["changed"] is False |
| 204 |
assert result.get("failed") is not True |
| 205 |
assert "investigations" in result |
| 206 |
assert len(result["investigations"]) == 0 |
| 207 |
|
| 208 |
|
| 209 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 210 |
def test_investigation_info_by_name(self, connection, monkeypatch): |
| 211 |
"""Test querying investigations by name. |
| 212 |
|
| 213 |
When name is provided, the module should fetch all investigations and |
| 214 |
filter by exact name match. |
| 215 |
""" |
| 216 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 217 |
self._plugin._connection._shell = MagicMock() |
| 218 |
|
| 219 |
def get_by_path(self, path, query_params=None): |
| 220 |
return copy.deepcopy(INVESTIGATION_API_RESPONSE_LIST) |
| 221 |
|
| 222 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 223 |
|
| 224 |
self._plugin._task.args = { |
| 225 |
"name": "Security Incident 2026-01", |
| 226 |
} |
| 227 |
|
| 228 |
result = self._plugin.run(task_vars=self._task_vars) |
| 229 |
|
| 230 |
assert result["changed"] is False |
| 231 |
assert result.get("failed") is not True |
| 232 |
assert "investigations" in result |
| 233 |
|
| 234 |
assert len(result["investigations"]) == 2 |
| 235 |
for investigation in result["investigations"]: |
| 236 |
assert investigation["name"] == "Security Incident 2026-01" |
| 237 |
|
| 238 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 239 |
def test_investigation_info_by_name_no_match(self, connection, monkeypatch): |
| 240 |
"""Test querying by name with no matches. |
| 241 |
|
| 242 |
When no investigations match the name, should return an empty list. |
| 243 |
""" |
| 244 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 245 |
self._plugin._connection._shell = MagicMock() |
| 246 |
|
| 247 |
def get_by_path(self, path, query_params=None): |
| 248 |
return copy.deepcopy(INVESTIGATION_API_RESPONSE_LIST) |
| 249 |
|
| 250 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 251 |
|
| 252 |
self._plugin._task.args = { |
| 253 |
"name": "Non-Existent Investigation Name", |
| 254 |
} |
| 255 |
|
| 256 |
result = self._plugin.run(task_vars=self._task_vars) |
| 257 |
|
| 258 |
assert result["changed"] is False |
| 259 |
assert result.get("failed") is not True |
| 260 |
assert len(result["investigations"]) == 0 |
| 261 |
|
| 262 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 263 |
def test_investigation_info_by_name_exact_match(self, connection, monkeypatch): |
| 264 |
"""Test that name filtering uses exact match. |
| 265 |
|
| 266 |
"Security" should not match "Security Incident 2026-01". |
| 267 |
""" |
| 268 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 269 |
self._plugin._connection._shell = MagicMock() |
| 270 |
|
| 271 |
def get_by_path(self, path, query_params=None): |
| 272 |
return copy.deepcopy(INVESTIGATION_API_RESPONSE_LIST) |
| 273 |
|
| 274 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 275 |
|
| 276 |
|
| 277 |
self._plugin._task.args = { |
| 278 |
"name": "Security", |
| 279 |
} |
| 280 |
|
| 281 |
result = self._plugin.run(task_vars=self._task_vars) |
| 282 |
|
| 283 |
assert result["changed"] is False |
| 284 |
assert len(result["investigations"]) == 0 |
| 285 |
|
| 286 |
|
| 287 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 288 |
def test_investigation_info_all(self, connection, monkeypatch): |
| 289 |
"""Test querying all investigations without filters. |
| 290 |
|
| 291 |
When no investigation_ref_id or name is provided, should return all investigations. |
| 292 |
""" |
| 293 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 294 |
self._plugin._connection._shell = MagicMock() |
| 295 |
|
| 296 |
def get_by_path(self, path, query_params=None): |
| 297 |
return copy.deepcopy(INVESTIGATION_API_RESPONSE_LIST) |
| 298 |
|
| 299 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 300 |
|
| 301 |
self._plugin._task.args = {} |
| 302 |
|
| 303 |
result = self._plugin.run(task_vars=self._task_vars) |
| 304 |
|
| 305 |
assert result["changed"] is False |
| 306 |
assert result.get("failed") is not True |
| 307 |
assert "investigations" in result |
| 308 |
assert len(result["investigations"]) == 3 |
| 309 |
|
| 310 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 311 |
def test_investigation_info_all_empty(self, connection, monkeypatch): |
| 312 |
"""Test querying all investigations when none exist. |
| 313 |
|
| 314 |
Should return an empty list without error. |
| 315 |
""" |
| 316 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 317 |
self._plugin._connection._shell = MagicMock() |
| 318 |
|
| 319 |
def get_by_path(self, path, query_params=None): |
| 320 |
return copy.deepcopy(EMPTY_INVESTIGATIONS_RESPONSE) |
| 321 |
|
| 322 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 323 |
|
| 324 |
self._plugin._task.args = {} |
| 325 |
|
| 326 |
result = self._plugin.run(task_vars=self._task_vars) |
| 327 |
|
| 328 |
assert result["changed"] is False |
| 329 |
assert result.get("failed") is not True |
| 330 |
assert len(result["investigations"]) == 0 |
| 331 |
|
| 332 |
|
| 333 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 334 |
def test_investigation_info_with_create_time_min(self, connection, monkeypatch): |
| 335 |
"""Test that create_time_min parameter is passed to API.""" |
| 336 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 337 |
self._plugin._connection._shell = MagicMock() |
| 338 |
|
| 339 |
captured_params = [] |
| 340 |
|
| 341 |
def get_by_path(self, path, query_params=None): |
| 342 |
captured_params.append(query_params) |
| 343 |
return copy.deepcopy(INVESTIGATION_API_RESPONSE_LIST) |
| 344 |
|
| 345 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 346 |
|
| 347 |
self._plugin._task.args = { |
| 348 |
"create_time_min": "-7d", |
| 349 |
} |
| 350 |
|
| 351 |
result = self._plugin.run(task_vars=self._task_vars) |
| 352 |
|
| 353 |
assert result["changed"] is False |
| 354 |
assert result.get("failed") is not True |
| 355 |
|
| 356 |
assert len(captured_params) > 0 |
| 357 |
assert captured_params[0] is not None |
| 358 |
assert captured_params[0].get("create_time_min") == "-7d" |
| 359 |
|
| 360 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 361 |
def test_investigation_info_with_create_time_max(self, connection, monkeypatch): |
| 362 |
"""Test that create_time_max parameter is passed to API.""" |
| 363 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 364 |
self._plugin._connection._shell = MagicMock() |
| 365 |
|
| 366 |
captured_params = [] |
| 367 |
|
| 368 |
def get_by_path(self, path, query_params=None): |
| 369 |
captured_params.append(query_params) |
| 370 |
return copy.deepcopy(INVESTIGATION_API_RESPONSE_LIST) |
| 371 |
|
| 372 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 373 |
|
| 374 |
self._plugin._task.args = { |
| 375 |
"create_time_max": "now", |
| 376 |
} |
| 377 |
|
| 378 |
result = self._plugin.run(task_vars=self._task_vars) |
| 379 |
|
| 380 |
assert result["changed"] is False |
| 381 |
assert result.get("failed") is not True |
| 382 |
assert len(captured_params) > 0 |
| 383 |
assert captured_params[0] is not None |
| 384 |
assert captured_params[0].get("create_time_max") == "now" |
| 385 |
|
| 386 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 387 |
def test_investigation_info_with_time_range(self, connection, monkeypatch): |
| 388 |
"""Test that both create_time_min and create_time_max are passed to API.""" |
| 389 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 390 |
self._plugin._connection._shell = MagicMock() |
| 391 |
|
| 392 |
captured_params = [] |
| 393 |
|
| 394 |
def get_by_path(self, path, query_params=None): |
| 395 |
captured_params.append(query_params) |
| 396 |
return copy.deepcopy(INVESTIGATION_API_RESPONSE_LIST) |
| 397 |
|
| 398 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 399 |
|
| 400 |
self._plugin._task.args = { |
| 401 |
"create_time_min": "-30d", |
| 402 |
"create_time_max": "-1d", |
| 403 |
} |
| 404 |
|
| 405 |
result = self._plugin.run(task_vars=self._task_vars) |
| 406 |
|
| 407 |
assert result["changed"] is False |
| 408 |
assert result.get("failed") is not True |
| 409 |
assert len(captured_params) > 0 |
| 410 |
assert captured_params[0].get("create_time_min") == "-30d" |
| 411 |
assert captured_params[0].get("create_time_max") == "-1d" |
| 412 |
|
| 413 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 414 |
def test_investigation_info_time_range_with_name(self, connection, monkeypatch): |
| 415 |
"""Test that time filters are applied when querying by name.""" |
| 416 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 417 |
self._plugin._connection._shell = MagicMock() |
| 418 |
|
| 419 |
captured_params = [] |
| 420 |
|
| 421 |
def get_by_path(self, path, query_params=None): |
| 422 |
captured_params.append(query_params) |
| 423 |
return copy.deepcopy(INVESTIGATION_API_RESPONSE_LIST) |
| 424 |
|
| 425 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 426 |
|
| 427 |
self._plugin._task.args = { |
| 428 |
"name": "Security Incident 2026-01", |
| 429 |
"create_time_min": "-7d", |
| 430 |
"create_time_max": "now", |
| 431 |
} |
| 432 |
|
| 433 |
result = self._plugin.run(task_vars=self._task_vars) |
| 434 |
|
| 435 |
assert result["changed"] is False |
| 436 |
assert len(captured_params) > 0 |
| 437 |
assert captured_params[0].get("create_time_min") == "-7d" |
| 438 |
assert captured_params[0].get("create_time_max") == "now" |
| 439 |
|
| 440 |
|
| 441 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 442 |
def test_investigation_info_always_changed_false(self, connection, monkeypatch): |
| 443 |
"""Verify that info module always returns changed=False. |
| 444 |
|
| 445 |
Info modules are read-only and should never report changes. |
| 446 |
""" |
| 447 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 448 |
self._plugin._connection._shell = MagicMock() |
| 449 |
|
| 450 |
def get_by_path(self, path, query_params=None): |
| 451 |
return copy.deepcopy(INVESTIGATION_API_RESPONSE_LIST) |
| 452 |
|
| 453 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 454 |
|
| 455 |
|
| 456 |
test_cases = [ |
| 457 |
{}, |
| 458 |
{"investigation_ref_id": "inv-001-aaaaa"}, |
| 459 |
{"name": "Security Incident 2026-01"}, |
| 460 |
{"create_time_min": "-7d"}, |
| 461 |
] |
| 462 |
|
| 463 |
for args in test_cases: |
| 464 |
self._plugin._task.args = args |
| 465 |
result = self._plugin.run(task_vars=self._task_vars) |
| 466 |
assert result["changed"] is False, f"Expected changed=False for args: {args}" |
| 467 |
|
| 468 |
|
| 469 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 470 |
def test_investigation_info_custom_api_path(self, connection, monkeypatch): |
| 471 |
"""Test that custom API path parameters are used.""" |
| 472 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 473 |
self._plugin._connection._shell = MagicMock() |
| 474 |
|
| 475 |
captured_paths = [] |
| 476 |
|
| 477 |
def get_by_path(self, path, query_params=None): |
| 478 |
captured_paths.append(path) |
| 479 |
return copy.deepcopy(INVESTIGATION_API_RESPONSE_LIST) |
| 480 |
|
| 481 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 482 |
|
| 483 |
self._plugin._task.args = { |
| 484 |
"api_namespace": "customNS", |
| 485 |
"api_user": "customuser", |
| 486 |
"api_app": "CustomApp", |
| 487 |
} |
| 488 |
|
| 489 |
result = self._plugin.run(task_vars=self._task_vars) |
| 490 |
|
| 491 |
assert result["changed"] is False |
| 492 |
assert result.get("failed") is not True |
| 493 |
|
| 494 |
assert len(captured_paths) > 0 |
| 495 |
assert "customNS" in captured_paths[0] |
| 496 |
assert "customuser" in captured_paths[0] |
| 497 |
assert "CustomApp" in captured_paths[0] |
| 498 |
|
| 499 |
|
| 500 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 501 |
def test_investigation_info_field_mapping(self, connection, monkeypatch): |
| 502 |
"""Test that API fields are correctly mapped to module format. |
| 503 |
|
| 504 |
Verify that API field names and values are converted to module format. |
| 505 |
""" |
| 506 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 507 |
self._plugin._connection._shell = MagicMock() |
| 508 |
|
| 509 |
def get_by_path(self, path, query_params=None): |
| 510 |
return [copy.deepcopy(INVESTIGATION_API_RESPONSE_SINGLE)] |
| 511 |
|
| 512 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 513 |
|
| 514 |
self._plugin._task.args = { |
| 515 |
"investigation_ref_id": "inv-12345-abcde", |
| 516 |
} |
| 517 |
|
| 518 |
result = self._plugin.run(task_vars=self._task_vars) |
| 519 |
|
| 520 |
investigation = result["investigations"][0] |
| 521 |
|
| 522 |
|
| 523 |
assert "investigation_ref_id" in investigation |
| 524 |
assert "name" in investigation |
| 525 |
assert "description" in investigation |
| 526 |
assert "owner" in investigation |
| 527 |
|
| 528 |
|
| 529 |
assert investigation["status"] == "new" |
| 530 |
assert investigation["disposition"] == "undetermined" |
| 531 |
assert investigation["sensitivity"] == "amber" |
| 532 |
|
| 533 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 534 |
def test_investigation_info_finding_ids_extraction(self, connection, monkeypatch): |
| 535 |
"""Test that finding_ids are extracted from consolidated_findings.""" |
| 536 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 537 |
self._plugin._connection._shell = MagicMock() |
| 538 |
|
| 539 |
def get_by_path(self, path, query_params=None): |
| 540 |
return [copy.deepcopy(INVESTIGATION_API_RESPONSE_SINGLE)] |
| 541 |
|
| 542 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 543 |
|
| 544 |
self._plugin._task.args = { |
| 545 |
"investigation_ref_id": "inv-12345-abcde", |
| 546 |
} |
| 547 |
|
| 548 |
result = self._plugin.run(task_vars=self._task_vars) |
| 549 |
|
| 550 |
investigation = result["investigations"][0] |
| 551 |
|
| 552 |
|
| 553 |
assert "finding_ids" in investigation |
| 554 |
assert investigation["finding_ids"] == ["finding-001@@notable@@time123"] |
| 555 |
|
| 556 |
|
| 557 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 558 |
def test_investigation_info_ref_id_priority(self, connection, monkeypatch): |
| 559 |
"""Test that investigation_ref_id takes precedence over name when both are provided.""" |
| 560 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 561 |
self._plugin._connection._shell = MagicMock() |
| 562 |
|
| 563 |
call_count = {"ref_id_calls": 0, "all_calls": 0} |
| 564 |
|
| 565 |
def get_by_path(self, path, query_params=None): |
| 566 |
if query_params and query_params.get("ids"): |
| 567 |
call_count["ref_id_calls"] += 1 |
| 568 |
return [copy.deepcopy(INVESTIGATION_API_RESPONSE_SINGLE)] |
| 569 |
else: |
| 570 |
call_count["all_calls"] += 1 |
| 571 |
return copy.deepcopy(INVESTIGATION_API_RESPONSE_LIST) |
| 572 |
|
| 573 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 574 |
|
| 575 |
self._plugin._task.args = { |
| 576 |
"investigation_ref_id": "inv-12345-abcde", |
| 577 |
"name": "Some Other Name", |
| 578 |
} |
| 579 |
|
| 580 |
result = self._plugin.run(task_vars=self._task_vars) |
| 581 |
|
| 582 |
|
| 583 |
assert call_count["ref_id_calls"] == 1 |
| 584 |
assert call_count["all_calls"] == 0 |
| 585 |
assert len(result["investigations"]) == 1 |
| 586 |
|
| 587 |
|
| 588 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 589 |
def test_investigation_info_handles_null_items(self, connection, monkeypatch): |
| 590 |
"""Test handling of null items in API response. |
| 591 |
|
| 592 |
The API might return None values in the list; these should be filtered out. |
| 593 |
""" |
| 594 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 595 |
self._plugin._connection._shell = MagicMock() |
| 596 |
|
| 597 |
def get_by_path(self, path, query_params=None): |
| 598 |
return [ |
| 599 |
None, |
| 600 |
copy.deepcopy(INVESTIGATION_API_RESPONSE_LIST[0]), |
| 601 |
None, |
| 602 |
] |
| 603 |
|
| 604 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 605 |
|
| 606 |
self._plugin._task.args = {} |
| 607 |
|
| 608 |
result = self._plugin.run(task_vars=self._task_vars) |
| 609 |
|
| 610 |
assert result["changed"] is False |
| 611 |
assert result.get("failed") is not True |
| 612 |
|
| 613 |
assert len(result["investigations"]) == 1 |
| 614 |
|
| 615 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 616 |
def test_investigation_info_handles_empty_items(self, connection, monkeypatch): |
| 617 |
"""Test handling of empty dict items in API response.""" |
| 618 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 619 |
self._plugin._connection._shell = MagicMock() |
| 620 |
|
| 621 |
def get_by_path(self, path, query_params=None): |
| 622 |
return [ |
| 623 |
{}, |
| 624 |
copy.deepcopy(INVESTIGATION_API_RESPONSE_LIST[0]), |
| 625 |
] |
| 626 |
|
| 627 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 628 |
|
| 629 |
self._plugin._task.args = {} |
| 630 |
|
| 631 |
result = self._plugin.run(task_vars=self._task_vars) |
| 632 |
|
| 633 |
assert result["changed"] is False |
| 634 |
assert result.get("failed") is not True |
| 635 |
|
| 636 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 637 |
def test_investigation_info_handles_404(self, connection, monkeypatch): |
| 638 |
"""Test graceful handling of 404 errors.""" |
| 639 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 640 |
self._plugin._connection._shell = MagicMock() |
| 641 |
|
| 642 |
def get_by_path(self, path, query_params=None): |
| 643 |
raise Exception("HTTP Error 404: Not Found") |
| 644 |
|
| 645 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 646 |
|
| 647 |
self._plugin._task.args = { |
| 648 |
"investigation_ref_id": "non-existent-id", |
| 649 |
} |
| 650 |
|
| 651 |
result = self._plugin.run(task_vars=self._task_vars) |
| 652 |
|
| 653 |
|
| 654 |
assert result["changed"] is False |
| 655 |
assert result.get("failed") is not True |
| 656 |
assert result["investigations"] == [] |
| 657 |
|
| 658 |
|
| 659 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 660 |
def test_investigation_info_returns_list(self, connection, monkeypatch): |
| 661 |
"""Test that investigations are always returned as a list. |
| 662 |
|
| 663 |
Even when querying by ref_id (single result), should return a list. |
| 664 |
""" |
| 665 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 666 |
self._plugin._connection._shell = MagicMock() |
| 667 |
|
| 668 |
def get_by_path(self, path, query_params=None): |
| 669 |
return [copy.deepcopy(INVESTIGATION_API_RESPONSE_SINGLE)] |
| 670 |
|
| 671 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 672 |
|
| 673 |
self._plugin._task.args = { |
| 674 |
"investigation_ref_id": "inv-12345-abcde", |
| 675 |
} |
| 676 |
|
| 677 |
result = self._plugin.run(task_vars=self._task_vars) |
| 678 |
|
| 679 |
assert isinstance(result["investigations"], list) |
| 680 |
|
| 681 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 682 |
def test_investigation_info_empty_returns_list(self, connection, monkeypatch): |
| 683 |
"""Test that empty results are returned as an empty list.""" |
| 684 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 685 |
self._plugin._connection._shell = MagicMock() |
| 686 |
|
| 687 |
def get_by_path(self, path, query_params=None): |
| 688 |
return [] |
| 689 |
|
| 690 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 691 |
|
| 692 |
self._plugin._task.args = {} |
| 693 |
|
| 694 |
result = self._plugin.run(task_vars=self._task_vars) |
| 695 |
|
| 696 |
assert isinstance(result["investigations"], list) |
| 697 |
assert len(result["investigations"]) == 0 |