Add unit tests for osism/utils/rabbitmq.py#2352
Open
berendt wants to merge 1 commit into
Open
Conversation
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>
1a31079 to
3807e56
Compare
There was a problem hiding this comment.
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_addressesandload_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.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2232
Adds
tests/unit/utils/test_rabbitmq.pycovering both functions inosism/utils/rabbitmq.py. Test-only change; no production code is touched.What is covered
get_rabbitmq_node_addresses()order (happy path returning
[("10.0.0.5","host1"),("10.0.0.6","host2")]);empty rabbitmq group →
None; firstsubprocess.check_outputraisingCalledProcessError; invalid JSON from the group listing(
JSONDecodeError); outer generic exception.next; missing
internal_interface; literal interface name useddirectly; 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.100→ansible_eth0_100,eth-0→ansible_eth_0); missingnormalized interface key; interface without
ipv4;ipv4without anaddress.None; at least one address → thelist is returned with no error logged.
load_rabbitmq_password()— missing file; empty / invalid / non-dictsecrets; missing
rabbitmq_passwordkey; whitespace stripping; intcoercion; loader raising.
Plus a check that the
RABBITMQ_USERmodule constant is"openstack".Notes for the reviewer
mocker.patch("osism.utils.rabbitmq.utils.redis", ...), but that targetdoes not resolve:
redisis a lazy__getattr__attribute on theosism.utilspackage, and the function reads it via a function-localfrom osism import utils, so therabbitmqmodule never gains autilsglobal (
AttributeError: module 'osism.utils.rabbitmq' has no attribute 'utils'). Instead the cached attribute is seeded on the packagenamespace 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_fileandos.path.existsare patched at the call site, as theissue suggests.
mocker+loguru_logsconventions(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-coreis absent andtests/conftest.pyinstalls its ansible stubs).
black --check,flake8,mypyclean on the new file.🤖 Generated with Claude Code