Skip to content

Commit

Permalink
[deps] Updated netjsonconfig branch to gsoc23
Browse files Browse the repository at this point in the history
- Fixed test vpn deletion notification mock.
- Deleted ZT API tasks notification cache keys after vpn deletion.
  • Loading branch information
Aryamanz29 committed Jul 30, 2023
1 parent 737e05e commit 016248f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
3 changes: 3 additions & 0 deletions openwisp_controller/config/base/vpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import shortuuid
from cache_memoize import cache_memoize
from django.core.cache import cache
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models, transaction
Expand Down Expand Up @@ -340,6 +341,8 @@ class method for ``post_delete`` signal
vpn_id=instance.pk,
)
)
# Delete ZT API tasks notification cache keys
cache.delete_many(cache.keys(f'*{instance.pk.hex}_last_operation'))

def _create_zerotier_server(self, config):
server_config = ZerotierService(
Expand Down
2 changes: 1 addition & 1 deletion openwisp_controller/config/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def handle_api_call(self, fn, *args, send_notification=True, **kwargs):
info_msg = kwargs.get('info')
vpn = kwargs.get('instance')
if send_notification:
task_key = f'{self.name}_{vpn.id}_last_operation'
task_key = f'{self.name}_{vpn.pk.hex}_last_operation'
# Execute API call and get response
response = fn(*args)
if isinstance(response, tuple):
Expand Down
13 changes: 11 additions & 2 deletions openwisp_controller/config/tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class TestNotifications(
_ZT_API_TASKS_INFO_LOGGER = 'openwisp_controller.config.tasks.logger.info'
_ZT_API_TASKS_WARN_LOGGER = 'openwisp_controller.config.tasks.logger.warn'
_ZT_API_TASKS_ERR_LOGGER = 'openwisp_controller.config.tasks.logger.error'
# As the locmem cache does not support the redis backend cache.keys() method
_ZT_API_TASKS_LOCMEM_CACHE_KEYS = (
'django.core.cache.backends.locmem.LocMemCache.keys'
)

def setUp(self):
self.admin = self._get_admin()
Expand Down Expand Up @@ -98,6 +102,7 @@ def test_default_notification_type_already_unregistered(self):
app = apps.get_app_config(self.app_label)
app.ready()

@patch(_ZT_API_TASKS_LOCMEM_CACHE_KEYS, create=True)
@patch(_ZT_API_TASKS_ERR_LOGGER)
@patch(_ZT_API_TASKS_WARN_LOGGER)
@patch(_ZT_API_TASKS_INFO_LOGGER)
Expand All @@ -108,6 +113,7 @@ def test_zerotier_api_tasks_notification(
mock_info,
mock_warn,
mock_error,
mock_locmem_cache_keys,
):
mock_requests.get.side_effect = [
# For node status
Expand All @@ -121,6 +127,7 @@ def test_zerotier_api_tasks_notification(
# For controller auth and ip assignment
self._get_mock_response(200),
]
mock_locmem_cache_keys.return_value = ['test_zt_api_tasks_notification_key']
vpn = self._create_zerotier_vpn()
self.assertEqual(Vpn.objects.count(), 1)
notification_qs = Notification.objects.all()
Expand Down Expand Up @@ -328,11 +335,13 @@ def test_zerotier_api_tasks_notification(
mock_requests.delete.side_effect = [
# For delete network
self._get_mock_response(200, response={}),
# For controller leave network
self._get_mock_response(200, response={}),
]
vpn.delete()
self.assertEqual(Vpn.objects.count(), 0)
# Only logging for vpn deletion
self.assertEqual(mock_info.call_count, 1)
# Only logging for vpn deletion & leave network
self.assertEqual(mock_info.call_count, 2)
self.assertEqual(mock_warn.call_count, 0)
self.assertEqual(mock_error.call_count, 0)
self.assertEqual(notification_qs.count(), 0)
9 changes: 8 additions & 1 deletion openwisp_controller/config/tests/test_vpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,10 @@ class TestZeroTierTransaction(BaseTestVpn, TestZeroTierVpnMixin, TransactionTest
_ZT_API_TASKS_INFO_LOGGER = 'openwisp_controller.config.tasks.logger.info'
_ZT_API_TASKS_WARN_LOGGER = 'openwisp_controller.config.tasks.logger.warn'
_ZT_API_TASKS_ERR_LOGGER = 'openwisp_controller.config.tasks.logger.error'
# As the locmem cache does not support the redis backend cache.keys() method
_ZT_API_TASKS_LOCMEM_CACHE_KEYS = (
'django.core.cache.backends.locmem.LocMemCache.keys'
)

@mock.patch(_ZT_SERVICE_REQUESTS)
def test_zerotier_auto_clients_configuration(self, mock_requests):
Expand Down Expand Up @@ -1472,12 +1476,13 @@ def test_zerotier_update_vpn_server_configuration(
mock_info.assert_has_calls(_EXPECTED_INFO_CALLS)
mock_error.assert_has_calls(_EXPECTED_ERROR_CALLS)

@mock.patch(_ZT_API_TASKS_LOCMEM_CACHE_KEYS, create=True)
@mock.patch(_ZT_API_TASKS_ERR_LOGGER)
@mock.patch(_ZT_API_TASKS_WARN_LOGGER)
@mock.patch(_ZT_API_TASKS_INFO_LOGGER)
@mock.patch(_ZT_SERVICE_REQUESTS)
def test_zerotier_vpn_server_deletion(
self, mock_requests, mock_info, mock_warn, mock_error
self, mock_requests, mock_info, mock_warn, mock_error, mock_locmem_cache_keys
):
def _setup_requests_mocks():
mock_requests.get.side_effect = [
Expand All @@ -1492,9 +1497,11 @@ def _setup_requests_mocks():
# For controller auth and ip assignment
self._get_mock_response(200),
]
mock_locmem_cache_keys.return_value = ['test_zt_api_tasks_notification_key']

def _reset_requests_mocks():
mock_requests.reset_mock()
mock_locmem_cache_keys.reset_mock()
# Delete subnet created for previous assertion
Subnet.objects.all().delete()

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
django-sortedm2m~=3.1.1
django-reversion~=5.0.4
django-taggit~=4.0.0
netjsonconfig @ https://github.com/openwisp/netjsonconfig/tarball/issue-207/add-zerotier-to-openwrt-backend
netjsonconfig @ https://github.com/openwisp/netjsonconfig/tarball/gsoc23
django-x509 @ https://github.com/openwisp/django-x509/tarball/master
django-loci @ https://github.com/openwisp/django-loci/tarball/master
django-flat-json-widget @ https://github.com/openwisp/django-flat-json-widget/tarball/master
Expand Down

0 comments on commit 016248f

Please sign in to comment.