Add unit tests for ironic.py pure helpers#2342
Open
berendt wants to merge 1 commit into
Open
Conversation
Cover the helpers in osism/tasks/conductor/ironic.py that can be tested without the full sync orchestration (issue #2226): - _derive_as_from_hostname_yrzn - _get_metalbox_primary_ip4_fallback - _get_metalbox_primary_ip4 - _render_templates - _prepare_node_attributes - _prettify_for_display Collaborators are patched at their ironic.py import sites. The NetBox client is replaced via osism.utils.nb, which the helpers' local "from osism import utils" resolves to, matching the existing test_netbox and sonic test conventions. Note: the issue's example asserting _derive_as_from_hostname_yrzn("comp-nw-22-3-7-1") == "4200143307" does not match the implementation, which pads rack/server and yields "4200140703" (consistent with the function's own docstring example). The test encodes the verified value. Assisted-by: Claude:anthropic-opus-4-8 Signed-off-by: Christian Berendt <berendt@osism.tech>
75a2e60 to
1ce607c
Compare
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The local
_YRZN_MAPPINGtest fixture duplicates theSUPPORTED_IPA_TYPESstructure and can silently diverge from the real mapping over time; consider deriving it fromSUPPORTED_IPA_TYPESat runtime (e.g.SUPPORTED_IPA_TYPES['yrzn001'].copy()or similar) and only overriding what needs to be controlled for the tests.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The local `_YRZN_MAPPING` test fixture duplicates the `SUPPORTED_IPA_TYPES` structure and can silently diverge from the real mapping over time; consider deriving it from `SUPPORTED_IPA_TYPES` at runtime (e.g. `SUPPORTED_IPA_TYPES['yrzn001'].copy()` or similar) and only overriding what needs to be controlled for the tests.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 #2226.
Adds
tests/unit/tasks/conductor/test_ironic_helpers.pycovering the purehelpers and the
_prepare_node_attributesbuilder inosism/tasks/conductor/ironic.py— everything testable without the fullsync orchestration (the sync entry points stay in the companion sub-issue).
Change set
A single new test module (no production code touched), organized by helper:
_derive_as_from_hostname_yrzn— storage vs. non-storage type digit,< 5parts →None, single-digit padding, already-two-digit values leftalone.
_get_metalbox_primary_ip4_fallback— invalid YAML, non-list setting,non-dict element skipped,
tagstripped +role="metalbox"added (assertedvia captured
filterkwargs), first/second metalbox IP selection, and thetwo no-IP / no-metalbox warning paths.
_get_metalbox_primary_ip4— no OOB IP (fallback not called), subnetmatch, no-match → fallback, matching subnet but no
primary_ip4→ earlyNone(no fallback), match on the second interface, and IPv4/IPv6 mixing._render_templates— flat/nested dict, nested list, list of dicts,non-template and non-string values untouched, multiple vars, in-place
mutation returning
None._prepare_node_attributes— base/config-context/custom-field mergingand order, driver pruning, template variables (board creds, OOB address,
ironic_osism_*propagation),osism-ipa-type=yrzn001kernel enrichment(frr params, metalbox, AS fallback, unknown type),
skip_kernel_params,extra_kernel_params,driver_infopersistence,extraserialization, andthe returned tuple.
_prettify_for_display— JSON parsing inextra, non-JSON leftuntouched, deep copy with/without
extra.Mocking notes
deep_decrypt,deep_merge,get_vault,get_device_oob_ip,SUPPORTED_IPA_TYPES,_derive_as_from_hostname_yrzn,_get_metalbox_primary_ip4) are patched atosism.tasks.conductor.ironic.*.osism.utils.nb(withcreate=True), whichis what the helpers' local
from osism import utilsresolves to — matchingthe existing
test_netbox.pyand sonic test fixtures.ironic.pyhas nomodule-level
utilsattribute (it imports it asosism_utils), so theliteral
osism.tasks.conductor.ironic.utils.nbpath from the issue is not avalid patch target;
osism.utils.nbis the established equivalent.deep_decrypt/deep_mergeare stubbed as no-ops; their real behavior iscovered by the conductor.utils tests, so the merge cases assert call
identity/order rather than merged output.
Deviation from the issue
The issue lists
_derive_as_from_hostname_yrzn("comp-nw-22-3-7-1")→"4200143307". The implementation pads rack/server and produces"4200140703"(matches the function's own docstring examplestor-nw-22-60-59-6→4200155960). The test encodes the verified value"4200140703"; the same case (non-storage type 4, single-digit padding) isstill covered.
Verification
black --check,flake8— pass on the new file.pytest/ coverage — left to thepython-osism-unit-testsZuul job.AI-assisted: Claude Code