Skip to content

Add unit tests for ironic.py pure helpers#2342

Open
berendt wants to merge 1 commit into
mainfrom
implement/issue-2226-ironic-helper-tests
Open

Add unit tests for ironic.py pure helpers#2342
berendt wants to merge 1 commit into
mainfrom
implement/issue-2226-ironic-helper-tests

Conversation

@berendt

@berendt berendt commented Jun 10, 2026

Copy link
Copy Markdown
Member

Closes #2226.

Adds tests/unit/tasks/conductor/test_ironic_helpers.py covering the pure
helpers and the _prepare_node_attributes builder in
osism/tasks/conductor/ironic.py — everything testable without the full
sync 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,
    < 5 parts → None, single-digit padding, already-two-digit values left
    alone.
  • _get_metalbox_primary_ip4_fallback — invalid YAML, non-list setting,
    non-dict element skipped, tag stripped + role="metalbox" added (asserted
    via captured filter kwargs), first/second metalbox IP selection, and the
    two no-IP / no-metalbox warning paths.
  • _get_metalbox_primary_ip4 — no OOB IP (fallback not called), subnet
    match, no-match → fallback, matching subnet but no primary_ip4 → early
    None (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 merging
    and order, driver pruning, template variables (board creds, OOB address,
    ironic_osism_* propagation), osism-ipa-type=yrzn001 kernel enrichment
    (frr params, metalbox, AS fallback, unknown type), skip_kernel_params,
    extra_kernel_params, driver_info persistence, extra serialization, and
    the returned tuple.
  • _prettify_for_display — JSON parsing in extra, non-JSON left
    untouched, deep copy with/without extra.

Mocking notes

  • Module-level collaborators (deep_decrypt, deep_merge, get_vault,
    get_device_oob_ip, SUPPORTED_IPA_TYPES, _derive_as_from_hostname_yrzn,
    _get_metalbox_primary_ip4) are patched at osism.tasks.conductor.ironic.*.
  • The NetBox client is patched via osism.utils.nb (with create=True), which
    is what the helpers' local from osism import utils resolves to — matching
    the existing test_netbox.py and sonic test fixtures. ironic.py has no
    module-level utils attribute (it imports it as osism_utils), so the
    literal osism.tasks.conductor.ironic.utils.nb path from the issue is not a
    valid patch target; osism.utils.nb is the established equivalent.
  • deep_decrypt / deep_merge are stubbed as no-ops; their real behavior is
    covered 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 example
stor-nw-22-60-59-64200155960). The test encodes the verified value
"4200140703"; the same case (non-storage type 4, single-digit padding) is
still covered.

Verification

  • black --check, flake8 — pass on the new file.
  • pytest / coverage — left to the python-osism-unit-tests Zuul job.

AI-assisted: Claude Code

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>
@berendt berendt force-pushed the implement/issue-2226-ironic-helper-tests branch from 75a2e60 to 1ce607c Compare June 10, 2026 20:49
@berendt berendt marked this pull request as ready for review June 10, 2026 20:55
@berendt berendt requested a review from ideaship June 10, 2026 20:55

@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 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.
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.

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/tasks/conductor/ironic.py — pure helpers

2 participants