From f98fecfbe587b8f3fc03f90bd79a69a67e8b76e9 Mon Sep 17 00:00:00 2001 From: Blaine Jester Date: Wed, 11 Nov 2026 15:39:42 -0800 Subject: [PATCH 1/2] Run instance ID tests with a fake MAC on hosts without one 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 #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 --- tests/testapp/tests/helpers.py | 46 +++++++++++++++++++++ tests/testapp/tests/sync/test_controller.py | 3 ++ tests/testapp/tests/sync/test_operations.py | 3 ++ tests/testapp/tests/test_api.py | 2 + tests/testapp/tests/test_uuid_utilities.py | 2 + 5 files changed, 56 insertions(+) diff --git a/tests/testapp/tests/helpers.py b/tests/testapp/tests/helpers.py index 55bc9467..7d7a06eb 100644 --- a/tests/testapp/tests/helpers.py +++ b/tests/testapp/tests/helpers.py @@ -2,6 +2,7 @@ Helper functions for use across syncing related functionality. """ +import functools import json import uuid @@ -25,6 +26,7 @@ from morango.models.core import Store from morango.models.core import SyncSession from morango.models.core import TransferSession +from morango.models.utils import get_0_5_mac_address from morango.sync.context import SessionContext from morango.sync.controller import MorangoProfileController from morango.sync.controller import SessionController @@ -34,6 +36,50 @@ from .compat import EnvironmentVarGuard +# Whether Morango gets a node ID for this host, from a MAC address or from the MORANGO_NODE_ID +# environment variable. When it gets none, the 0.4 node ID is also empty, so the legacy lookup in +# InstanceIDModel.get_or_create_current_instance (morango/models/core.py) keeps returning the +# current instance after MORANGO_SYSTEM_ID changes. Tests that expect a new instance then fail. +HOST_HAS_READABLE_MAC = bool(get_0_5_mac_address()) + +# A universally administered unicast MAC address, which get_0_4_system_parameters and +# get_0_5_mac_address (morango/models/utils.py) both accept as a real hardware address +FAKE_MAC_ADDRESS = "00:11:22:33:44:55" + + +def with_readable_mac(cls): + """ + Class decorator for tests that need Morango to get a node ID from the host. If the host has + none, each test in the class reads FAKE_MAC_ADDRESS as the host MAC address. The patches start + before the setUp of the class, so instances that setUp creates have the same host identity as + instances that the test creates. If the host has a node ID, the class runs unchanged against + the real host identity. + """ + if HOST_HAS_READABLE_MAC: + return cls + + original_set_up = cls.setUp + + @functools.wraps(original_set_up) + def setUp(self): + patchers = [ + mock.patch("uuid.getnode", return_value=int(FAKE_MAC_ADDRESS.replace(":", ""), 16)), + mock.patch( + "ifcfg.interfaces", + return_value={"eth0": {"device": "eth0", "ether": FAKE_MAC_ADDRESS}}, + ), + # InstanceIDModel caches the current instance on the class across tests, and an + # instance cached before this patch has the real host identity + mock.patch.object(InstanceIDModel, "_cached_instance", None), + ] + for patcher in patchers: + patcher.start() + self.addCleanup(patcher.stop) + original_set_up(self) + + cls.setUp = setUp + return cls + class FacilityModelFactory(factory.django.DjangoModelFactory): class Meta: diff --git a/tests/testapp/tests/sync/test_controller.py b/tests/testapp/tests/sync/test_controller.py index f436a84b..143b77bd 100644 --- a/tests/testapp/tests/sync/test_controller.py +++ b/tests/testapp/tests/sync/test_controller.py @@ -25,6 +25,7 @@ from ..helpers import FacilityModelFactory from ..helpers import serialized_facility_factory from ..helpers import TestSessionContext +from ..helpers import with_readable_mac class StoreModelFacilityFactory(factory.django.DjangoModelFactory): @@ -38,6 +39,7 @@ class Meta: dirty_bit = True +@with_readable_mac class SerializeIntoStoreTestCase(TestCase): def setUp(self): InstanceIDModel.get_or_create_current_instance() @@ -280,6 +282,7 @@ def test_store_hard_delete_propagates(self): self.assertFalse(SummaryLog.objects.filter(id=log.id).exists()) +@with_readable_mac class RecordMaxCounterUpdatesDuringSerialization(TestCase): def setUp(self): self.current_id, _ = InstanceIDModel.get_or_create_current_instance() diff --git a/tests/testapp/tests/sync/test_operations.py b/tests/testapp/tests/sync/test_operations.py index 979c32c2..67c4178d 100644 --- a/tests/testapp/tests/sync/test_operations.py +++ b/tests/testapp/tests/sync/test_operations.py @@ -45,6 +45,7 @@ from ..helpers import create_buffer_and_store_dummy_data from ..helpers import create_dummy_store_data +from ..helpers import with_readable_mac DBBackend = load_backend(connection) @@ -68,6 +69,7 @@ def assertRecordsNotBuffered(records): @override_settings(MORANGO_SERIALIZE_BEFORE_QUEUING=False, MORANGO_DISABLE_FSIC_V2_FORMAT=True) +@with_readable_mac class QueueStoreIntoBufferV1TestCase(TestCase): def setUp(self): super(QueueStoreIntoBufferV1TestCase, self).setUp() @@ -275,6 +277,7 @@ def test_local_queue_operation__noop(self, mock_queue): @override_settings(MORANGO_SERIALIZE_BEFORE_QUEUING=False, MORANGO_DISABLE_FSIC_V2_FORMAT=False) +@with_readable_mac class QueueStoreIntoBufferV2TestCase(TestCase): def setUp(self): super(QueueStoreIntoBufferV2TestCase, self).setUp() diff --git a/tests/testapp/tests/test_api.py b/tests/testapp/tests/test_api.py index 594b1be0..e5d89ee3 100644 --- a/tests/testapp/tests/test_api.py +++ b/tests/testapp/tests/test_api.py @@ -33,6 +33,7 @@ from morango.sync.utils import validate_and_create_buffer_data from .compat import EnvironmentVarGuard +from .helpers import with_readable_mac class CertificateTestCaseMixin(object): @@ -1135,6 +1136,7 @@ def _lazy_settings(): lazy_settings = SimpleLazyObject(_lazy_settings) +@with_readable_mac class MorangoInfoTestCase(APITestCase): def setUp(self): InstanceIDModel.get_or_create_current_instance() diff --git a/tests/testapp/tests/test_uuid_utilities.py b/tests/testapp/tests/test_uuid_utilities.py index 5f9df151..17c30963 100644 --- a/tests/testapp/tests/test_uuid_utilities.py +++ b/tests/testapp/tests/test_uuid_utilities.py @@ -17,6 +17,7 @@ from morango.models.utils import get_0_5_system_id from .compat import EnvironmentVarGuard +from .helpers import with_readable_mac class UUIDModelMixinTestCase(TestCase): @@ -56,6 +57,7 @@ def test_save_without_id(self): self.assertEqual(Facility.objects.first().id, ID) +@with_readable_mac class InstanceIDModelTestCase(TestCase): def setUp(self): InstanceIDModel.get_or_create_current_instance() From c8b39846037a21524306fc10a0a60bec39b6c98e Mon Sep 17 00:00:00 2001 From: Blaine Jester Date: Thu, 12 Nov 2026 07:50:39 -0800 Subject: [PATCH 2/2] Assert a new instance in test_new_rmc_for_existing_model 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 --- tests/testapp/tests/sync/test_controller.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testapp/tests/sync/test_controller.py b/tests/testapp/tests/sync/test_controller.py index 143b77bd..75c8ccc1 100644 --- a/tests/testapp/tests/sync/test_controller.py +++ b/tests/testapp/tests/sync/test_controller.py @@ -302,6 +302,8 @@ def test_new_rmc_for_existing_model(self): new_rmc = RecordMaxCounter.objects.get(instance_id=new_id.id, store_model_id=self.fac1.id) new_store_record = Store.objects.get(id=self.fac1.id) + self.assertNotEqual(new_id.id, self.current_id.id) + self.assertEqual(RecordMaxCounter.objects.filter(store_model_id=self.fac1.id).count(), 2) self.assertEqual(new_rmc.counter, new_store_record.last_saved_counter) self.assertEqual(new_rmc.instance_id, new_store_record.last_saved_instance)