Skip to content

Add unit tests for osism/utils/rabbitmq.py#2352

Open
berendt wants to merge 1 commit into
mainfrom
implement/issue-2232-test-rabbitmq
Open

Add unit tests for osism/utils/rabbitmq.py#2352
berendt wants to merge 1 commit into
mainfrom
implement/issue-2232-test-rabbitmq

Conversation

@berendt

@berendt berendt commented Jun 12, 2026

Copy link
Copy Markdown
Member

Closes #2232

Adds tests/unit/utils/test_rabbitmq.py covering both functions in
osism/utils/rabbitmq.py. Test-only change; no production code is touched.

What is covered

get_rabbitmq_node_addresses()

  • Inventory & host discovery: two hosts processed in alphabetical
    order (happy path returning [("10.0.0.5","host1"),("10.0.0.6","host2")]);
    empty rabbitmq group → None; first subprocess.check_output raising
    CalledProcessError; invalid JSON from the group listing
    (JSONDecodeError); outer generic exception.
  • Per-host resolution: cache miss skips the host and continues to the
    next; missing internal_interface; literal interface name used
    directly; Jinja2 template resolved via dotted-path traversal of the
    facts; template resolving to a non-string (None / dict / int);
    traversal hitting a non-dict mid-walk; interface-name normalization
    (eth0.100ansible_eth0_100, eth-0ansible_eth_0); missing
    normalized interface key; interface without ipv4; ipv4 without an
    address.
  • Aggregate: all hosts skipped → None; at least one address → the
    list is returned with no error logged.

load_rabbitmq_password() — missing file; empty / invalid / non-dict
secrets; missing rabbitmq_password key; whitespace stripping; int
coercion; loader raising.

Plus a check that the RABBITMQ_USER module constant is "openstack".

Notes for the reviewer

  • Redis mocking deviates from the issue's hint. The issue suggested
    mocker.patch("osism.utils.rabbitmq.utils.redis", ...), but that target
    does not resolve: redis is a lazy __getattr__ attribute on the
    osism.utils package, and the function reads it via a function-local
    from osism import utils, so the rabbitmq module never gains a utils
    global (AttributeError: module 'osism.utils.rabbitmq' has no attribute 'utils'). Instead the cached attribute is seeded on the package
    namespace with mocker.patch.dict(osism.utils.__dict__, {"redis": ...}),
    which also bypasses the lazy initializer that would otherwise open a real
    Redis connection. Reading the cache key the function builds is asserted in
    the happy-path test.
  • load_yaml_file and os.path.exists are patched at the call site, as the
    issue suggests.
  • Tests follow the existing mocker + loguru_logs conventions
    (see tests/unit/utils/test_init_connections.py).

Verification

  • pytest tests/unit/utils/test_rabbitmq.py --cov=osism.utils.rabbitmq
    28 passed, 100% line coverage (run in the CI-equivalent
    environment where ansible-core is absent and tests/conftest.py
    installs its ansible stubs).
  • black --check, flake8, mypy clean on the new file.

🤖 Generated with Claude Code

Cover both functions in osism/utils/rabbitmq.py:

- get_rabbitmq_node_addresses: inventory and host discovery,
  per-host interface resolution (literal interface names, Jinja2
  template traversal, interface-name normalization) and result
  aggregation, covering every error and skip branch.
- load_rabbitmq_password: missing file, empty, invalid and
  non-dict secrets, missing key, whitespace stripping, int
  coercion and loader failure.
- RABBITMQ_USER module constant.

The lazy redis attribute lives on the osism.utils package and is
read via a function-local import, so it is seeded with patch.dict
on the package namespace rather than patched on the rabbitmq
module (which never gains a utils global).

Tests follow the existing mocker + loguru_logs conventions and
reach 100% line coverage of the module.

Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Christian Berendt <berendt@osism.tech>
@berendt berendt force-pushed the implement/issue-2232-test-rabbitmq branch from 1a31079 to 3807e56 Compare June 12, 2026 11:37
@berendt berendt marked this pull request as ready for review June 12, 2026 11:47
@berendt berendt requested a review from ideaship June 12, 2026 11:47

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The test module is quite long and dense; consider splitting it into smaller logical sections (e.g., separate files or pytest classes for get_rabbitmq_node_addresses and load_rabbitmq_password) or introducing shared fixtures to reduce repetition and improve readability.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The test module is quite long and dense; consider splitting it into smaller logical sections (e.g., separate files or pytest classes for `get_rabbitmq_node_addresses` and `load_rabbitmq_password`) or introducing shared fixtures to reduce repetition and improve readability.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

Unit tests for osism/utils/rabbitmq.py

2 participants