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..75c8ccc1 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() @@ -299,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) 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()