| 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_finding_info action plugin. |
| 21 |
|
| 22 |
These tests verify that the splunk_finding_info action plugin works correctly for: |
| 23 |
- Querying a specific finding by ref_id |
| 24 |
- Querying findings by title (with filtering) |
| 25 |
- Querying all findings |
| 26 |
- Handling time range parameters (earliest/latest) |
| 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_finding_info import ActionModule |
| 41 |
from ansible_collections.splunk.es.plugins.module_utils.splunk import SplunkRequest |
| 42 |
|
| 43 |
|
| 44 |
# Test data: API Response Payloads |
| 45 |
# These represent what the Splunk API returns for findings queries. |
| 46 |
|
| 47 |
FINDING_API_RESPONSE_SINGLE = { |
| 48 |
"finding_id": "2008e99d-af14-4fec-89da-b9b17a81820a@@notable@@time1768225865", |
| 49 |
"rule_title": "Suspicious Login Activity", |
| 50 |
"rule_description": "Multiple failed login attempts detected", |
| 51 |
"security_domain": "access", |
| 52 |
"risk_object": "testuser", |
| 53 |
"risk_object_type": "user", |
| 54 |
"risk_score": "50.0", |
| 55 |
"owner": "admin", |
| 56 |
"status": "1", |
| 57 |
"urgency": "high", |
| 58 |
"disposition": "disposition:6", |
| 59 |
} |
| 60 |
|
| 61 |
FINDING_API_RESPONSE_LIST = { |
| 62 |
"items": [ |
| 63 |
{ |
| 64 |
"finding_id": "finding-001@@notable@@time1768225865", |
| 65 |
"rule_title": "Suspicious Login Activity", |
| 66 |
"rule_description": "Multiple failed login attempts", |
| 67 |
"security_domain": "access", |
| 68 |
"risk_object": "testuser", |
| 69 |
"risk_object_type": "user", |
| 70 |
"risk_score": "50.0", |
| 71 |
"owner": "admin", |
| 72 |
"status": "1", |
| 73 |
"urgency": "high", |
| 74 |
"disposition": "disposition:6", |
| 75 |
}, |
| 76 |
{ |
| 77 |
"finding_id": "finding-002@@notable@@time1768225866", |
| 78 |
"rule_title": "Malware Detection", |
| 79 |
"rule_description": "Potential malware detected", |
| 80 |
"security_domain": "endpoint", |
| 81 |
"risk_object": "server01", |
| 82 |
"risk_object_type": "system", |
| 83 |
"risk_score": "80.0", |
| 84 |
"owner": "analyst", |
| 85 |
"status": "2", |
| 86 |
"urgency": "critical", |
| 87 |
"disposition": "disposition:1", |
| 88 |
}, |
| 89 |
{ |
| 90 |
"finding_id": "finding-003@@notable@@time1768225867", |
| 91 |
"rule_title": "Suspicious Login Activity", |
| 92 |
"rule_description": "Another login attempt", |
| 93 |
"security_domain": "access", |
| 94 |
"risk_object": "admin", |
| 95 |
"risk_object_type": "user", |
| 96 |
"risk_score": "45.0", |
| 97 |
"owner": "admin", |
| 98 |
"status": "1", |
| 99 |
"urgency": "medium", |
| 100 |
"disposition": "disposition:6", |
| 101 |
}, |
| 102 |
], |
| 103 |
} |
| 104 |
|
| 105 |
EMPTY_FINDINGS_RESPONSE = { |
| 106 |
"items": [], |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
class TestSplunkFindingInfo: |
| 111 |
"""Test class for the splunk_finding_info action plugin. |
| 112 |
|
| 113 |
The splunk_finding_info module is a "read-only" info module that queries |
| 114 |
Splunk for findings without making changes. It should always return |
| 115 |
changed=False. |
| 116 |
|
| 117 |
Query modes: |
| 118 |
1. By ref_id: Returns a single specific finding |
| 119 |
2. By title: Returns all findings matching the exact title |
| 120 |
3. All: Returns all findings (when no filters provided) |
| 121 |
""" |
| 122 |
|
| 123 |
def setup_method(self): |
| 124 |
"""Set up test fixtures before each test method. |
| 125 |
|
| 126 |
Creates mock Ansible components needed to test the action plugin. |
| 127 |
""" |
| 128 |
# Create a mock Task object |
| 129 |
task = MagicMock(Task) |
| 130 |
task.check_mode = False |
| 131 |
|
| 132 |
# Create mock play context |
| 133 |
play_context = MagicMock() |
| 134 |
play_context.check_mode = False |
| 135 |
|
| 136 |
# Create a mock connection |
| 137 |
connection = patch( |
| 138 |
"ansible_collections.splunk.es.plugins.module_utils.splunk.Connection", |
| 139 |
) |
| 140 |
|
| 141 |
# Ansible's template engine |
| 142 |
fake_loader = {} |
| 143 |
templar = Templar(loader=fake_loader) |
| 144 |
|
| 145 |
# Create the action plugin instance |
| 146 |
self._plugin = ActionModule( |
| 147 |
task=task, |
| 148 |
connection=connection, |
| 149 |
play_context=play_context, |
| 150 |
loader=fake_loader, |
| 151 |
templar=templar, |
| 152 |
shared_loader_obj=None, |
| 153 |
) |
| 154 |
|
| 155 |
# Set required task attributes |
| 156 |
self._plugin._task.action = "splunk_finding_info" |
| 157 |
self._plugin._task.async_val = False |
| 158 |
|
| 159 |
# Task variables |
| 160 |
self._task_vars = {} |
| 161 |
|
| 162 |
# Query by ref_id Tests # |
| 163 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 164 |
def test_finding_info_by_ref_id(self, connection, monkeypatch): |
| 165 |
"""Test querying a specific finding by ref_id. |
| 166 |
|
| 167 |
When ref_id is provided, the module should query for that specific |
| 168 |
finding and return it in a list (for consistency with other query modes). |
| 169 |
""" |
| 170 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 171 |
self._plugin._connection._shell = MagicMock() |
| 172 |
|
| 173 |
def get_by_path(self, path, query_params=None): |
| 174 |
# When querying by ref_id, path includes the ref_id |
| 175 |
# Use deepcopy to prevent mutation of test data |
| 176 |
if "2008e99d" in path: |
| 177 |
return copy.deepcopy(FINDING_API_RESPONSE_SINGLE) |
| 178 |
return {} |
| 179 |
|
| 180 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 181 |
|
| 182 |
self._plugin._task.args = { |
| 183 |
"ref_id": "2008e99d-af14-4fec-89da-b9b17a81820a@@notable@@time1768225865", |
| 184 |
} |
| 185 |
|
| 186 |
result = self._plugin.run(task_vars=self._task_vars) |
| 187 |
|
| 188 |
# Info modules should always return changed=False |
| 189 |
assert result["changed"] is False |
| 190 |
assert result.get("failed") is not True |
| 191 |
assert "findings" in result |
| 192 |
assert len(result["findings"]) == 1 |
| 193 |
assert result["findings"][0]["title"] == "Suspicious Login Activity" |
| 194 |
assert ( |
| 195 |
result["findings"][0]["ref_id"] |
| 196 |
== "2008e99d-af14-4fec-89da-b9b17a81820a@@notable@@time1768225865" |
| 197 |
) |
| 198 |
|
| 199 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 200 |
def test_finding_info_by_ref_id_not_found(self, connection, monkeypatch): |
| 201 |
"""Test querying a non-existent finding by ref_id. |
| 202 |
|
| 203 |
When the finding doesn't exist, the module should return an empty |
| 204 |
list rather than failing. |
| 205 |
""" |
| 206 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 207 |
self._plugin._connection._shell = MagicMock() |
| 208 |
|
| 209 |
def get_by_path(self, path, query_params=None): |
| 210 |
return {} # Empty response = not found |
| 211 |
|
| 212 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 213 |
|
| 214 |
self._plugin._task.args = { |
| 215 |
"ref_id": "non-existent-id@@notable@@time1234567890", |
| 216 |
} |
| 217 |
|
| 218 |
result = self._plugin.run(task_vars=self._task_vars) |
| 219 |
|
| 220 |
assert result["changed"] is False |
| 221 |
assert result.get("failed") is not True |
| 222 |
assert "findings" in result |
| 223 |
assert len(result["findings"]) == 0 |
| 224 |
|
| 225 |
# Query by Title Tests # |
| 226 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 227 |
def test_finding_info_by_title(self, connection, monkeypatch): |
| 228 |
"""Test querying findings by title. |
| 229 |
|
| 230 |
When title is provided, the module should fetch all findings and |
| 231 |
filter by exact title match. |
| 232 |
""" |
| 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(FINDING_API_RESPONSE_LIST) |
| 238 |
|
| 239 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 240 |
|
| 241 |
self._plugin._task.args = { |
| 242 |
"title": "Suspicious Login Activity", |
| 243 |
} |
| 244 |
|
| 245 |
result = self._plugin.run(task_vars=self._task_vars) |
| 246 |
|
| 247 |
assert result["changed"] is False |
| 248 |
assert result.get("failed") is not True |
| 249 |
assert "findings" in result |
| 250 |
# Should return 2 findings with matching title (items 0 and 2) |
| 251 |
assert len(result["findings"]) == 2 |
| 252 |
for finding in result["findings"]: |
| 253 |
assert finding["title"] == "Suspicious Login Activity" |
| 254 |
|
| 255 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 256 |
def test_finding_info_by_title_no_match(self, connection, monkeypatch): |
| 257 |
"""Test querying by title with no matches. |
| 258 |
|
| 259 |
When no findings match the title, should return an empty list. |
| 260 |
""" |
| 261 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 262 |
self._plugin._connection._shell = MagicMock() |
| 263 |
|
| 264 |
def get_by_path(self, path, query_params=None): |
| 265 |
return copy.deepcopy(FINDING_API_RESPONSE_LIST) |
| 266 |
|
| 267 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 268 |
|
| 269 |
self._plugin._task.args = { |
| 270 |
"title": "Non-Existent Finding Title", |
| 271 |
} |
| 272 |
|
| 273 |
result = self._plugin.run(task_vars=self._task_vars) |
| 274 |
|
| 275 |
assert result["changed"] is False |
| 276 |
assert result.get("failed") is not True |
| 277 |
assert len(result["findings"]) == 0 |
| 278 |
|
| 279 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 280 |
def test_finding_info_by_title_exact_match(self, connection, monkeypatch): |
| 281 |
"""Test that title filtering uses exact match. |
| 282 |
|
| 283 |
"Suspicious" should not match "Suspicious Login Activity". |
| 284 |
""" |
| 285 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 286 |
self._plugin._connection._shell = MagicMock() |
| 287 |
|
| 288 |
def get_by_path(self, path, query_params=None): |
| 289 |
return copy.deepcopy(FINDING_API_RESPONSE_LIST) |
| 290 |
|
| 291 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 292 |
|
| 293 |
# Partial title should not match |
| 294 |
self._plugin._task.args = { |
| 295 |
"title": "Suspicious", |
| 296 |
} |
| 297 |
|
| 298 |
result = self._plugin.run(task_vars=self._task_vars) |
| 299 |
|
| 300 |
assert result["changed"] is False |
| 301 |
assert len(result["findings"]) == 0 # No exact match |
| 302 |
|
| 303 |
# Query All Findings Tests # |
| 304 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 305 |
def test_finding_info_all(self, connection, monkeypatch): |
| 306 |
"""Test querying all findings without filters. |
| 307 |
|
| 308 |
When no ref_id or title is provided, should return all findings. |
| 309 |
""" |
| 310 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 311 |
self._plugin._connection._shell = MagicMock() |
| 312 |
|
| 313 |
def get_by_path(self, path, query_params=None): |
| 314 |
return copy.deepcopy(FINDING_API_RESPONSE_LIST) |
| 315 |
|
| 316 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 317 |
|
| 318 |
self._plugin._task.args = {} |
| 319 |
|
| 320 |
result = self._plugin.run(task_vars=self._task_vars) |
| 321 |
|
| 322 |
assert result["changed"] is False |
| 323 |
assert result.get("failed") is not True |
| 324 |
assert "findings" in result |
| 325 |
assert len(result["findings"]) == 3 |
| 326 |
|
| 327 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 328 |
def test_finding_info_all_empty(self, connection, monkeypatch): |
| 329 |
"""Test querying all findings when none exist. |
| 330 |
|
| 331 |
Should return an empty list without error. |
| 332 |
""" |
| 333 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 334 |
self._plugin._connection._shell = MagicMock() |
| 335 |
|
| 336 |
def get_by_path(self, path, query_params=None): |
| 337 |
return copy.deepcopy(EMPTY_FINDINGS_RESPONSE) |
| 338 |
|
| 339 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 340 |
|
| 341 |
self._plugin._task.args = {} |
| 342 |
|
| 343 |
result = self._plugin.run(task_vars=self._task_vars) |
| 344 |
|
| 345 |
assert result["changed"] is False |
| 346 |
assert result.get("failed") is not True |
| 347 |
assert len(result["findings"]) == 0 |
| 348 |
|
| 349 |
# Time Range Tests # |
| 350 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 351 |
def test_finding_info_with_earliest(self, connection, monkeypatch): |
| 352 |
"""Test that earliest time parameter is passed to API. |
| 353 |
|
| 354 |
The earliest parameter controls the time range for returned findings. |
| 355 |
""" |
| 356 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 357 |
self._plugin._connection._shell = MagicMock() |
| 358 |
|
| 359 |
captured_params = [] |
| 360 |
|
| 361 |
def get_by_path(self, path, query_params=None): |
| 362 |
captured_params.append(query_params) |
| 363 |
return copy.deepcopy(FINDING_API_RESPONSE_LIST) |
| 364 |
|
| 365 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 366 |
|
| 367 |
self._plugin._task.args = { |
| 368 |
"earliest": "-7d", |
| 369 |
} |
| 370 |
|
| 371 |
result = self._plugin.run(task_vars=self._task_vars) |
| 372 |
|
| 373 |
assert result["changed"] is False |
| 374 |
assert result.get("failed") is not True |
| 375 |
# Verify earliest was passed to API |
| 376 |
assert len(captured_params) > 0 |
| 377 |
assert captured_params[0] is not None |
| 378 |
assert captured_params[0].get("earliest") == "-7d" |
| 379 |
|
| 380 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 381 |
def test_finding_info_with_latest(self, connection, monkeypatch): |
| 382 |
"""Test that latest time parameter is passed to API.""" |
| 383 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 384 |
self._plugin._connection._shell = MagicMock() |
| 385 |
|
| 386 |
captured_params = [] |
| 387 |
|
| 388 |
def get_by_path(self, path, query_params=None): |
| 389 |
captured_params.append(query_params) |
| 390 |
return copy.deepcopy(FINDING_API_RESPONSE_LIST) |
| 391 |
|
| 392 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 393 |
|
| 394 |
self._plugin._task.args = { |
| 395 |
"latest": "now", |
| 396 |
} |
| 397 |
|
| 398 |
result = self._plugin.run(task_vars=self._task_vars) |
| 399 |
|
| 400 |
assert result["changed"] is False |
| 401 |
assert result.get("failed") is not True |
| 402 |
assert len(captured_params) > 0 |
| 403 |
assert captured_params[0] is not None |
| 404 |
assert captured_params[0].get("latest") == "now" |
| 405 |
|
| 406 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 407 |
def test_finding_info_with_time_range(self, connection, monkeypatch): |
| 408 |
"""Test that both earliest and latest are passed to API.""" |
| 409 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 410 |
self._plugin._connection._shell = MagicMock() |
| 411 |
|
| 412 |
captured_params = [] |
| 413 |
|
| 414 |
def get_by_path(self, path, query_params=None): |
| 415 |
captured_params.append(query_params) |
| 416 |
return copy.deepcopy(FINDING_API_RESPONSE_LIST) |
| 417 |
|
| 418 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 419 |
|
| 420 |
self._plugin._task.args = { |
| 421 |
"earliest": "-30d", |
| 422 |
"latest": "-1d", |
| 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 len(captured_params) > 0 |
| 430 |
assert captured_params[0].get("earliest") == "-30d" |
| 431 |
assert captured_params[0].get("latest") == "-1d" |
| 432 |
|
| 433 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 434 |
def test_finding_info_ref_id_extracts_time_from_ref_id(self, connection, monkeypatch): |
| 435 |
"""Test that time is extracted from ref_id and user-provided time params are ignored. |
| 436 |
|
| 437 |
When querying by ref_id, the earliest time should be automatically extracted from |
| 438 |
the ref_id (format: uuid@@notable@@time{timestamp}), and any user-provided |
| 439 |
earliest/latest parameters should be ignored. |
| 440 |
""" |
| 441 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 442 |
self._plugin._connection._shell = MagicMock() |
| 443 |
|
| 444 |
captured_params = [] |
| 445 |
|
| 446 |
def get_by_path(self, path, query_params=None): |
| 447 |
captured_params.append(query_params) |
| 448 |
return copy.deepcopy(FINDING_API_RESPONSE_SINGLE) |
| 449 |
|
| 450 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 451 |
|
| 452 |
self._plugin._task.args = { |
| 453 |
"ref_id": "2008e99d-af14-4fec-89da-b9b17a81820a@@notable@@time1768225865", |
| 454 |
"earliest": "-7d", # This should be ignored |
| 455 |
"latest": "now", # This should be ignored |
| 456 |
} |
| 457 |
|
| 458 |
result = self._plugin.run(task_vars=self._task_vars) |
| 459 |
|
| 460 |
assert result["changed"] is False |
| 461 |
assert result.get("failed") is not True |
| 462 |
assert len(captured_params) > 0 |
| 463 |
# Time should be extracted from ref_id (with 1s buffer), not from user-provided params |
| 464 |
assert captured_params[0].get("earliest") == "1768225864" |
| 465 |
# latest should not be passed when querying by ref_id |
| 466 |
assert captured_params[0].get("latest") is None |
| 467 |
|
| 468 |
# Always Changed=False Tests # |
| 469 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 470 |
def test_finding_info_always_changed_false(self, connection, monkeypatch): |
| 471 |
"""Verify that info module always returns changed=False. |
| 472 |
|
| 473 |
Info modules are read-only and should never report changes. |
| 474 |
""" |
| 475 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 476 |
self._plugin._connection._shell = MagicMock() |
| 477 |
|
| 478 |
def get_by_path(self, path, query_params=None): |
| 479 |
return copy.deepcopy(FINDING_API_RESPONSE_LIST) |
| 480 |
|
| 481 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 482 |
|
| 483 |
# Test with various query types |
| 484 |
test_cases = [ |
| 485 |
{}, # All findings |
| 486 |
{"ref_id": "finding-001@@notable@@time1768225865"}, # By ref_id |
| 487 |
{"title": "Suspicious Login Activity"}, # By title |
| 488 |
{"earliest": "-7d"}, # With time filter |
| 489 |
] |
| 490 |
|
| 491 |
for args in test_cases: |
| 492 |
self._plugin._task.args = args |
| 493 |
result = self._plugin.run(task_vars=self._task_vars) |
| 494 |
assert result["changed"] is False, f"Expected changed=False for args: {args}" |
| 495 |
|
| 496 |
# Custom API Path Tests # |
| 497 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 498 |
def test_finding_info_custom_api_path(self, connection, monkeypatch): |
| 499 |
"""Test that custom API path parameters are used.""" |
| 500 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 501 |
self._plugin._connection._shell = MagicMock() |
| 502 |
|
| 503 |
captured_paths = [] |
| 504 |
|
| 505 |
def get_by_path(self, path, query_params=None): |
| 506 |
captured_paths.append(path) |
| 507 |
return copy.deepcopy(FINDING_API_RESPONSE_LIST) |
| 508 |
|
| 509 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 510 |
|
| 511 |
self._plugin._task.args = { |
| 512 |
"api_namespace": "customNS", |
| 513 |
"api_user": "customuser", |
| 514 |
"api_app": "CustomApp", |
| 515 |
} |
| 516 |
|
| 517 |
result = self._plugin.run(task_vars=self._task_vars) |
| 518 |
|
| 519 |
assert result["changed"] is False |
| 520 |
assert result.get("failed") is not True |
| 521 |
# Verify custom path was used |
| 522 |
assert len(captured_paths) > 0 |
| 523 |
assert "customNS" in captured_paths[0] |
| 524 |
assert "customuser" in captured_paths[0] |
| 525 |
assert "CustomApp" in captured_paths[0] |
| 526 |
|
| 527 |
# Field Mapping Tests # |
| 528 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 529 |
def test_finding_info_field_mapping(self, connection, monkeypatch): |
| 530 |
"""Test that API fields are correctly mapped to module format. |
| 531 |
|
| 532 |
Verify that API field names (rule_title, risk_score) are converted |
| 533 |
to module field names (title, finding_score). |
| 534 |
""" |
| 535 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 536 |
self._plugin._connection._shell = MagicMock() |
| 537 |
|
| 538 |
def get_by_path(self, path, query_params=None): |
| 539 |
return copy.deepcopy(FINDING_API_RESPONSE_SINGLE) |
| 540 |
|
| 541 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 542 |
|
| 543 |
self._plugin._task.args = { |
| 544 |
"ref_id": "2008e99d-af14-4fec-89da-b9b17a81820a@@notable@@time1768225865", |
| 545 |
} |
| 546 |
|
| 547 |
result = self._plugin.run(task_vars=self._task_vars) |
| 548 |
|
| 549 |
finding = result["findings"][0] |
| 550 |
|
| 551 |
# Verify field mapping |
| 552 |
assert "title" in finding # rule_title -> title |
| 553 |
assert "description" in finding # rule_description -> description |
| 554 |
assert "entity" in finding # risk_object -> entity |
| 555 |
assert "entity_type" in finding # risk_object_type -> entity_type |
| 556 |
assert "finding_score" in finding # risk_score -> finding_score |
| 557 |
|
| 558 |
# Verify status/disposition are converted to human-readable |
| 559 |
assert finding["status"] == "new" # "1" -> "new" |
| 560 |
assert finding["disposition"] == "undetermined" # "disposition:6" -> "undetermined" |
| 561 |
|
| 562 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 563 |
def test_finding_info_finding_score_type(self, connection, monkeypatch): |
| 564 |
"""Test that finding_score is converted to integer. |
| 565 |
|
| 566 |
The API returns risk_score as a string like "50.0", but the module |
| 567 |
should return it as an integer. |
| 568 |
""" |
| 569 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 570 |
self._plugin._connection._shell = MagicMock() |
| 571 |
|
| 572 |
def get_by_path(self, path, query_params=None): |
| 573 |
return copy.deepcopy(FINDING_API_RESPONSE_SINGLE) |
| 574 |
|
| 575 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 576 |
|
| 577 |
self._plugin._task.args = { |
| 578 |
"ref_id": "2008e99d-af14-4fec-89da-b9b17a81820a@@notable@@time1768225865", |
| 579 |
} |
| 580 |
|
| 581 |
result = self._plugin.run(task_vars=self._task_vars) |
| 582 |
|
| 583 |
finding = result["findings"][0] |
| 584 |
assert finding["finding_score"] == 50 |
| 585 |
assert isinstance(finding["finding_score"], int) |
| 586 |
|
| 587 |
# Priority Tests (ref_id takes precedence over title) # |
| 588 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 589 |
def test_finding_info_ref_id_priority(self, connection, monkeypatch): |
| 590 |
"""Test that ref_id takes precedence over title when both are provided. |
| 591 |
|
| 592 |
If both ref_id and title are specified, the module should query |
| 593 |
by ref_id and ignore the title parameter. |
| 594 |
""" |
| 595 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 596 |
self._plugin._connection._shell = MagicMock() |
| 597 |
|
| 598 |
call_count = {"ref_id_calls": 0, "all_calls": 0} |
| 599 |
|
| 600 |
def get_by_path(self, path, query_params=None): |
| 601 |
if "2008e99d" in path: |
| 602 |
call_count["ref_id_calls"] += 1 |
| 603 |
return copy.deepcopy(FINDING_API_RESPONSE_SINGLE) |
| 604 |
else: |
| 605 |
call_count["all_calls"] += 1 |
| 606 |
return copy.deepcopy(FINDING_API_RESPONSE_LIST) |
| 607 |
|
| 608 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 609 |
|
| 610 |
self._plugin._task.args = { |
| 611 |
"ref_id": "2008e99d-af14-4fec-89da-b9b17a81820a@@notable@@time1768225865", |
| 612 |
"title": "Some Other Title", # Should be ignored |
| 613 |
} |
| 614 |
|
| 615 |
result = self._plugin.run(task_vars=self._task_vars) |
| 616 |
|
| 617 |
# Should have called the ref_id path, not the all-findings path |
| 618 |
assert call_count["ref_id_calls"] == 1 |
| 619 |
assert call_count["all_calls"] == 0 |
| 620 |
assert len(result["findings"]) == 1 |
| 621 |
|
| 622 |
# Error Handling Tests # |
| 623 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 624 |
def test_finding_info_handles_null_items(self, connection, monkeypatch): |
| 625 |
"""Test handling of null items in API response. |
| 626 |
|
| 627 |
The API might return None values in the items list; these should |
| 628 |
be filtered out. |
| 629 |
""" |
| 630 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 631 |
self._plugin._connection._shell = MagicMock() |
| 632 |
|
| 633 |
def get_by_path(self, path, query_params=None): |
| 634 |
return { |
| 635 |
"items": [ |
| 636 |
None, # Null item |
| 637 |
copy.deepcopy(FINDING_API_RESPONSE_LIST["items"][0]), |
| 638 |
None, # Another null |
| 639 |
], |
| 640 |
} |
| 641 |
|
| 642 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 643 |
|
| 644 |
self._plugin._task.args = {} |
| 645 |
|
| 646 |
result = self._plugin.run(task_vars=self._task_vars) |
| 647 |
|
| 648 |
assert result["changed"] is False |
| 649 |
assert result.get("failed") is not True |
| 650 |
# Should only have 1 valid finding (the null ones filtered out) |
| 651 |
assert len(result["findings"]) == 1 |
| 652 |
|
| 653 |
@patch("ansible.module_utils.connection.Connection.__rpc__") |
| 654 |
def test_finding_info_handles_empty_items(self, connection, monkeypatch): |
| 655 |
"""Test handling of empty dict items in API response.""" |
| 656 |
self._plugin._connection.socket_path = tempfile.NamedTemporaryFile().name |
| 657 |
self._plugin._connection._shell = MagicMock() |
| 658 |
|
| 659 |
def get_by_path(self, path, query_params=None): |
| 660 |
return { |
| 661 |
"items": [ |
| 662 |
{}, # Empty item |
| 663 |
copy.deepcopy(FINDING_API_RESPONSE_LIST["items"][0]), |
| 664 |
], |
| 665 |
} |
| 666 |
|
| 667 |
monkeypatch.setattr(SplunkRequest, "get_by_path", get_by_path) |
| 668 |
|
| 669 |
self._plugin._task.args = {} |
| 670 |
|
| 671 |
result = self._plugin.run(task_vars=self._task_vars) |
| 672 |
|
| 673 |
assert result["changed"] is False |
| 674 |
assert result.get("failed") is not True |