Skip to content

Commit aa97110

Browse files
authored
[bug] Changing DeviceGroup.context invalidates config checksum cache #798
Closes #798
1 parent 8b8dda6 commit aa97110

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

openwisp_controller/config/base/device_group.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .. import settings as app_settings
1616
from ..signals import group_templates_changed
1717
from ..sortedm2m.fields import SortedManyToManyField
18+
from ..tasks import bulk_invalidate_config_get_cached_checksum
1819
from .config import TemplatesThrough
1920

2021

@@ -75,6 +76,19 @@ def clean(self):
7576
except SchemaError as e:
7677
raise ValidationError({'input': e.message})
7778

79+
def save(
80+
self, force_insert=False, force_update=False, using=None, update_fields=None
81+
):
82+
context_changed = False
83+
if not self._state.adding:
84+
db_instance = self.__class__.objects.only('context').get(id=self.id)
85+
context_changed = db_instance.context != self.context
86+
super().save(force_insert, force_update, using, update_fields)
87+
if context_changed:
88+
bulk_invalidate_config_get_cached_checksum.delay(
89+
{'device__group_id': str(self.id)}
90+
)
91+
7892
def get_context(self):
7993
return deepcopy(self.context)
8094

openwisp_controller/config/tests/test_device.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,31 @@ def test_changing_org_variable_invalidates_cache(self):
394394
new_checksum = config.get_cached_checksum()
395395
self.assertNotEqual(old_checksum, new_checksum)
396396

397+
def test_changing_group_variable_invalidates_cache(self):
398+
org = self._get_org()
399+
device_group = self._create_device_group(organization=org, context={})
400+
device = self._create_device(organization=org, group=device_group)
401+
config = self._create_config(device=device)
402+
template = self._create_template(
403+
config={'interfaces': [{'name': 'eth0', 'type': '{{ interface_type }}'}]},
404+
default_values={'interface_type': 'ethernet'},
405+
)
406+
config.templates.add(template)
407+
old_checksum = config.get_cached_checksum()
408+
409+
# Changing DeviceGroup.context should invalidate the cache
410+
# and a new checksum should be returned in the following request.
411+
device_group.context = {'interface_type': 'virtual'}
412+
device_group.full_clean()
413+
device_group.save()
414+
415+
# Config.backend_instance is a "cached_property", hence deleting
416+
# the attribute is required for testing.
417+
del config.backend_instance
418+
419+
new_checksum = config.get_cached_checksum()
420+
self.assertNotEqual(old_checksum, new_checksum)
421+
397422
def test_management_ip_changed_not_emitted_on_creation(self):
398423
with catch_signal(management_ip_changed) as handler:
399424
self._create_device(organization=self._get_org())

0 commit comments

Comments
 (0)