Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tests/testapp/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Helper functions for use across syncing related functionality.
"""

import functools
import json
import uuid

Expand All @@ -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
Expand All @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions tests/testapp/tests/sync/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -38,6 +39,7 @@ class Meta:
dirty_bit = True


@with_readable_mac
class SerializeIntoStoreTestCase(TestCase):
def setUp(self):
InstanceIDModel.get_or_create_current_instance()
Expand Down Expand Up @@ -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()
Expand All @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions tests/testapp/tests/sync/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions tests/testapp/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions tests/testapp/tests/test_uuid_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
Loading