Skip to content

Run instance ID tests with a fake MAC on hosts without one - #422

Merged
bjester merged 2 commits into
learningequality:release-v0.9.xfrom
bjester:fix-crypto-mac-flipflop-tests
Nov 13, 2026
Merged

bjester merged 2 commits into
learningequality:release-v0.9.xfrom
bjester:fix-crypto-mac-flipflop-tests

Conversation

@bjester

@bjester bjester commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Summary

Eight instance ID tests fail on hosts where Morango cannot read a MAC address. Each test changes MORANGO_SYSTEM_ID and expects a new instance, but get_or_create_current_instance returns the existing one. On such a host, the 0.4 and 0.5 node IDs are both empty, so the legacy 0.4 lookup matches the instance that setUp created. Some CI runners have no readable MAC address, so the tests pass and fail between runs on the same commit.

This PR changes only tests:

  • The test helpers check once, at import time, whether get_0_5_mac_address() returns a node ID for the host.
  • A new class decorator, with_readable_mac, goes on the 6 test classes that contain the failing tests.
  • If the host has no node ID, the decorator patches in a fake MAC address before the setUp of the class runs. All tests in the class then run with the fake MAC address.
  • If the host has a node ID, the decorator returns the class unchanged, and the tests run against the real host identity.

The patches start before setUp, not on the test method. Otherwise the instance from setUp keeps the real host identity, and a test can pass because the MAC address changed, not because MORANGO_SYSTEM_ID changed.

I chose this over a fake MAC address for the whole suite, because the tests then still cover real hosts that have a MAC address. Production code does not change, so the instance ID behavior on hosts without a MAC address stays as it is.

A second commit adds two assertions to test_new_rmc_for_existing_model. On a host without a node ID, that test passed even though the instance did not change. It now checks that the instance changed and that the facility has a second RecordMaxCounter.

TODO

  • Have tests been written for the new code?
  • Has documentation been written/updated? N/A, this PR changes only tests.
  • New dependencies (if any) added to requirements file. N/A, no new dependencies.

Reviewer guidance

To reproduce, unset MORANGO_SYSTEM_ID and MORANGO_NODE_ID, then use the no_mac pytest plugin from #413. A second plugin that returns a real MAC address (00:11:22:33:44:55) simulates the other case.

Results for the 4 changed test modules:

Host Result
Simulated host without a MAC address 201 passed
Simulated host with a MAC address 201 passed
Real Linux host without a readable MAC address 201 passed
Real Linux host without a readable MAC address, decorator turned off 9 failed: the 8 tests from #413 and test_new_rmc_for_existing_model

Other checks:

  • Full suite on the real host: 605 passed, 37 errors. All errors are PermissionError in the sync and integration test_syncsession.py modules, caused by the local sandbox. The sync module had the same 31 errors before this change.
  • prek hooks pass.

Points to check:

  • HOST_HAS_READABLE_MAC is also true when MORANGO_NODE_ID is set, because get_0_5_mac_address() uses that variable first. In that case the decorator changes nothing and the tests pass against the real host.
  • The decorator covers every test in the 6 classes, not only the 8 failing tests. For example, the queue tests that use create_dummy_store_data() now get two separate instances on hosts without a MAC address.
  • The Instance ID tests fail on hosts without a readable MAC address #413 deliverables now match this approach. These items are out of scope there: a test that records the production behavior on hosts without a MAC address, and the failure of test_consistent_0_5_instance_id when MORANGO_NODE_ID is set. The tests also do not clear MORANGO_SYSTEM_ID or MORANGO_NODE_ID from the shell environment.

Issues addressed

Fixes #413

Documentation

N/A

AI usage

I used Claude Code for this change. I rejected two earlier attempts at #413, then directed the design: detect the condition, and use a fake MAC address only on hosts without one. The first version copied the failing tests into separate classes. After review, I asked for a way to avoid the copies, and Claude Code replaced them with the class decorator. Claude Code wrote the helper, reproduced the failures with the plugin from #413, and ran the host checks above. It also confirmed that the tests fail with the decorator turned off. Claude Code also drafted this description.

🤖 Generated with Claude Code

@bjester
bjester marked this pull request as ready for review November 11, 2026 23:49
@bjester
bjester requested review from a team and leq-bot November 11, 2026 23:49

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

Patching uuid.getnode, ifcfg.interfaces, and InstanceIDModel._cached_instance before setUp fixes the 8 listed test failures when MORANGO_SYSTEM_ID and MORANGO_NODE_ID are unset, but this PR cannot close #413 as written because three of #413's deliverables remain unmet and several neighboring tests still break or lose coverage.

  • 🚨 Key Blockers:
    • Merging with Fixes #413 leaves three #413 deliverables unmet, including shell environment isolation (MORANGO_SYSTEM_ID=new_sys_id fails 7 of the 8 tests in both original and FakeMac modes, and MORANGO_NODE_ID still breaks test_consistent_0_5_instance_id), suite-wide host identity regression safety, and a test recording the intended MAC-less 0.4 lookup behavior (tests/testapp/tests/helpers.py).
    • RecordMaxCounterUpdatesDuringSerialization.test_new_rmc_for_existing_model hits the same MAC-less fallback (new_id.id == self.current_id.id) and updates self.old_rmc in place instead of creating a second RecordMaxCounter, but was omitted from RecordMaxCounterUpdatesDuringSerializationFakeMacTestCase (tests/testapp/tests/sync/test_controller.py).
  • Inline Comments: 2 🚨 Blockers, 1 ⚠️ Architecture & Design, 1 🧹 Nit
  • ✅ Positive Highlight: use_fake_mac_address (tests/testapp/tests/helpers.py) identifies and resets all three host-identity channels (uuid.getnode for 0.4 lookups, ifcfg.interfaces for 0.5 lookups, and InstanceIDModel._cached_instance across tests) prior to setUp.

Verification Status

  • Simulated no-MAC (MORANGO_SYSTEM_ID and MORANGO_NODE_ID unset): 201 passed, 8 skipped across the 4 modified test modules (python -O -m pytest)
  • Simulated real-MAC (MORANGO_SYSTEM_ID and MORANGO_NODE_ID unset): 201 passed, 8 skipped across the 4 modified test modules
  • Shell environment sensitivity check: 7 failed with MORANGO_SYSTEM_ID=new_sys_id (under both no-MAC and real-MAC plugins) and 1 failed (test_consistent_0_5_instance_id) with MORANGO_NODE_ID set
  • Lint & formatting: Passing (uv run prek run --all-files)
  • CI note: Postgres CI failure on Ubuntu 26.04 is an unrelated M2Crypto SWIG build failure addressed in #423

Generated by Antigravity using model Gemini 4 Carbon

Comment thread tests/testapp/tests/helpers.py Resolved
On a host where Morango cannot read a MAC address, the 0.4 and 0.5 node
IDs are both empty. The legacy lookup in get_or_create_current_instance
then keeps the current instance after MORANGO_SYSTEM_ID changes, and 8
tests that expect a new instance fail.

The test helpers now detect this condition once. On such a host, the 8
original tests are skipped, and copies of them run with a fake MAC
address that is applied in setUp. On a host with a readable MAC address,
the originals run against the real host and the copies are skipped.

Fixes learningequality#413

The copies repeated each test body, and on hosts with a MAC address they
were skipped, so edits to an original could drift from its copy without
any local failure.

The with_readable_mac class decorator wraps setUp instead. On a host
without a node ID, it patches in the fake MAC address before the setUp
of the class runs. On other hosts, it returns the class unchanged, so
the tests still run against the real host identity.

The decorator applies to every test in the 6 affected classes. This
also covers tests that passed on hosts without a MAC address only
because they did not check for a new instance, such as the queue tests
that use create_dummy_store_data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bjester
bjester force-pushed the fix-crypto-mac-flipflop-tests branch from 1ca7e68 to f98fecf Compare November 12, 2026 15:46
On a host without a node ID, the system ID change in this test returned
the same instance. The test still passed, because serialization updated
the existing RecordMaxCounter in place and the test only compared that
counter with the store record.

The test now checks that the instance changed and that the facility has
a second RecordMaxCounter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

All four findings from the initial review are resolved, and the @with_readable_mac class decorator cleanly satisfies the updated #413 deliverables without duplicating test classes.

  • Prior Findings: 4/4 RESOLVED
    • #413 scope & deliverables (tests/testapp/tests/helpers.py): RESOLVED via updated #413 deliverables and @with_readable_mac.
    • test_new_rmc_for_existing_model (tests/testapp/tests/sync/test_controller.py): RESOLVED in c8b3984 by asserting new_id.id != self.current_id.id and count() == 2 under @with_readable_mac.
    • create_dummy_store_data() fixture collapse & duplicate classes (tests/testapp/tests/sync/test_operations.py): RESOLVED in f98fecf by decorating the 6 TestCase classes directly so all 25 queue tests receive distinct group1_id and group2_id instances.
    • Decorator drift in test_instance_id_caching (tests/testapp/tests/test_uuid_utilities.py): RESOLVED in f98fecf by removing the duplicate class.
  • ✅ Positive Highlight: Wrapping cls.setUp in with_readable_mac (tests/testapp/tests/helpers.py) keeps a single copy of each test class, preserves LIFO cleanup ordering with method-level @mock.patch decorators via self.addCleanup(patcher.stop), and leaves execution untouched on hosts with a readable MAC address.
  • 🧹 Commit Message Note (Optional): If merging without squashing, commit f98fecf's message body still contains the earlier paragraph describing skipped originals and copies alongside the updated with_readable_mac description.

Verification Status

  • Simulated no-MAC (MORANGO_SYSTEM_ID and MORANGO_NODE_ID unset): 54 passed across all 6 decorated test classes (python -O -m pytest -p no_mac)
  • Simulated real-MAC (MORANGO_SYSTEM_ID and MORANGO_NODE_ID unset): 54 passed across all 6 decorated test classes (python -O -m pytest -p real_mac)
  • Lint & formatting: Passing (uv run prek run --all-files)

Generated by Antigravity using model Gemini 4 Carbon

@marcellamaki marcellamaki left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure my review really adds much here (as I certainly would have not had any comments similar to the pre-me bot review) but I did locally reproduce the steps in the reviewer guidance with some help from claude, and do a code read through and the commit message read through and it all makes sense to me. Since :bot: review says it's okay and I say it's "followable" and it only is changing tests... :shipit:

@bjester
bjester merged commit abf95a9 into learningequality:release-v0.9.x Nov 13, 2026
63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Instance ID tests fail on hosts without a readable MAC address

3 participants