def test_config_status_modified_after_change(self): t = self._create_template() c = self._create_config(device=self._create_device(name='test-status')) self.assertEqual(c.status, 'modified') with catch_signal(config_status_changed) as handler: c.templates.add(t) handler.assert_not_called() c.status = 'applied' c.save() c.refresh_from_db() self.assertEqual(c.status, 'applied') t.config['interfaces'][0]['name'] = 'eth1' t.full_clean() with catch_signal(config_status_changed) as handler: t.save() c.refresh_from_db() handler.assert_called_once_with( sender=Config, signal=config_status_changed, instance=c, ) self.assertEqual(c.status, 'modified') # status has already changed to modified # sgnal should not be triggered again with catch_signal(config_status_changed) as handler: t.config['interfaces'][0]['name'] = 'eth2' t.full_clean() t.save() c.refresh_from_db() handler.assert_not_called() self.assertEqual(c.status, 'modified')
def test_config_modified_sent(self): with catch_signal(config_modified) as handler: c = self._create_config(status='applied') handler.assert_not_called() self.assertEqual(c.status, 'applied') with catch_signal(config_modified) as handler: c.config = {'general': {'description': 'test'}} c.full_clean() handler.assert_not_called() self.assertEqual(c.status, 'modified') with catch_signal(config_modified) as handler: c.save() handler.assert_called_once_with( sender=Config, signal=config_modified, instance=c, device=c.device, config=c, ) self.assertEqual(c.status, 'modified') with catch_signal(config_modified) as handler: c.config = {'general': {'description': 'changed again'}} c.full_clean() c.save() handler.assert_called_once() self.assertEqual(c.status, 'modified')
def test_config_status_changed_modified(self): org = self._get_org() with catch_signal(config_status_changed) as handler: c = self._create_config(organization=org, status='applied') handler.assert_not_called() self.assertEqual(c.status, 'applied') with catch_signal(config_status_changed) as handler: c.config = {'general': {'description': 'test'}} c.full_clean() self.assertEqual(c.status, 'modified') handler.assert_not_called() with catch_signal(config_status_changed) as handler: c.save() handler.assert_called_once_with( sender=Config, signal=config_status_changed, instance=c, ) self.assertEqual(c.status, 'modified') with catch_signal(config_status_changed) as handler: c.config = {'general': {'description': 'changed again'}} c.full_clean() c.save() handler.assert_not_called() self.assertEqual(c.status, 'modified')
def test_vpn_peers_changed(self): with self.subTest('VpnClient created'): with catch_signal(vpn_peers_changed) as handler: device, vpn, template = self._create_wireguard_vpn_template() handler.assert_called_once() with self.subTest('VpnClient deleted'): with catch_signal(vpn_peers_changed) as handler: device.config.templates.remove(template) handler.assert_called_once()
def test_config_modified_signal(self): temp = self._create_template() conf = self._create_config(device=self._create_device( name='test-status')) self.assertEqual(conf.status, 'modified') # refresh instance to reset _just_created attribute conf = Config.objects.get(pk=conf.pk) with self.subTest( 'signal not sent m2m if config status is already modified'): # (avoids executing push updates multiple times) with catch_signal(config_modified) as handler: conf.templates.add(temp) handler.assert_not_called() with self.subTest('signal sent after assigning template to config'): with catch_signal(config_modified) as handler: conf.set_status_applied() conf.templates.add(temp) handler.assert_called_once_with( sender=Config, signal=config_modified, instance=conf, device=conf.device, config=conf, ) conf.status = 'applied' conf.save() conf.refresh_from_db() self.assertEqual(conf.status, 'applied') temp.config['interfaces'][0]['name'] = 'eth1' with self.subTest('signal sent after changing a template'): with catch_signal(config_modified) as handler: temp.full_clean() temp.save() conf.refresh_from_db() handler.assert_called_once() self.assertEqual(conf.status, 'modified') with self.subTest( 'signal sent also if config is already in modified status'): # status has already changed to modified # sgnal should be triggered anyway with catch_signal(config_modified) as handler: temp.config['interfaces'][0]['name'] = 'eth2' temp.full_clean() temp.save() conf.refresh_from_db() handler.assert_called_once() self.assertEqual(conf.status, 'modified')
def test_status_signal_emitted(self): """ Tests the catch_signal test utility function """ with catch_signal(status_signal) as handler: self._generate_signal() handler.assert_called_once_with( status='working', sender=self, signal=status_signal )
def test_device_registered_signal(self): with catch_signal(device_registered) as handler: device = self.test_register() handler.assert_called_once_with(sender=Device, signal=device_registered, instance=device, is_new=True)
def test_pre_metric_write_signal(self): d = self._create_device(organization=self._create_org()) data = { 'type': 'DeviceMonitoring', 'resources': { 'cpus': 1, 'load': [0, 0, 0] } } self._create_object_metric(name='CPU usage', configuration='cpu', object_id=d.id) values = { 'cpu_usage': 0.0, 'load_1': 0.0, 'load_5': 0.0, 'load_15': 0.0 } time = start_time.strftime('%d-%m-%Y_%H:%M:%S.%f') with catch_signal(pre_metric_write) as handler: self._post_data(d.id, d.key, data, time=time) signal_calls = handler.call_args_list # assert signal is called once self.assertEqual(len(signal_calls), 1) signal_arguments = signal_calls[0][1] # remove metric from signal arguments del signal_arguments['metric'] expected_arguments = dict( signal=pre_metric_write, sender=Metric, values=values, time=start_time, current=False, ) self.assertEqual(signal_calls[0][1], expected_arguments)
def test_config_modified_sent(self): org = self._get_org() with catch_signal(config_modified) as handler: c = self._create_config(organization=org, status='applied') handler.assert_not_called() self.assertEqual(c.status, 'applied') with catch_signal(config_modified) as handler: c.config = {'general': {'description': 'test'}} c.full_clean() handler.assert_not_called() self.assertEqual(c.status, 'modified') with catch_signal(config_modified) as handler: c.config = {'general': {'description': 'hey'}} c.full_clean() handler.assert_not_called() with catch_signal(config_modified) as handler: c.save() handler.assert_called_once_with( sender=Config, signal=config_modified, instance=c, device=c.device, config=c, previous_status='applied', action='config_changed', ) self.assertEqual(c.status, 'modified') with catch_signal(config_modified) as handler: c.config = {'general': {'description': 'changed again'}} c.full_clean() # repeated on purpose c.full_clean() c.save() handler.assert_called_once_with( sender=Config, signal=config_modified, instance=c, device=c.device, config=c, previous_status='modified', action='config_changed', ) self.assertEqual(c.status, 'modified')
def test_device_group_changed_not_emitted_on_creation(self): org = self._get_org() device_group = self._create_device_group(organization=org) with catch_signal(device_group_changed) as handler: self._create_device(name='test', organization=org, group=device_group) handler.assert_not_called()
def test_status_change_signal_sent(self): self.signal_was_called = False t = self.topology_model.objects.first() node1, node2 = self._get_nodes() with catch_signal(link_status_changed) as handler: link = t._create_link(source=node1, target=node2, cost=1.0, status='up') link.save() handler.assert_not_called() with catch_signal(link_status_changed) as handler: link.status = 'down' link.save() handler.assert_called_once_with(link=link, sender=self.link_model, signal=link_status_changed) self.assertEqual(link.status, 'down')
def test_vpn_server_change_invalidates_device_cache(self): device, vpn, template = self._create_wireguard_vpn_template() with catch_signal(vpn_server_modified ) as mocked_vpn_server_modified, catch_signal( config_modified) as mocked_config_modified: vpn.host = 'localhost' vpn.save(update_fields=['host']) mocked_vpn_server_modified.assert_called_once_with( signal=vpn_server_modified, sender=Vpn, instance=vpn) mocked_config_modified.assert_called_once_with( signal=config_modified, sender=Config, instance=device.config, previous_status='modified', action='related_template_changed', config=device.config, device=device, )
def test_metric_post_write_signals_emitted(self): om = self._create_object_metric() with catch_signal(post_metric_write) as handler: om.write(3) handler.assert_called_once_with( sender=Metric, metric=om, values={om.field_name: 3}, signal=post_metric_write, )
def test_config_status_changed_receiver(self, mock_method): c = self._create_config(status='applied', organization=self._create_org()) c.config = {'general': {'description': 'test'}} c.full_clean() with catch_signal(config_status_changed) as handler: c.save() handler.assert_called_once() self.assertEqual(c.status, 'modified') self.assertEqual(mock_method.call_count, 1)
def test_device_group_signals(self): template = self._create_template() with catch_signal( group_templates_changed ) as group_templates_changed_handler, catch_signal( post_save ) as post_save_handler: dg = self._create_device_group() dg.templates.add(template) group_templates_changed_handler.assert_not_called() post_save_handler.assert_called_with( signal=post_save, sender=load_model('config', 'DeviceGroup'), instance=dg, created=True, update_fields=None, raw=False, using='default', )
def test_device_config_download_requested_signal_is_emitted(self): d = self._create_device_config() url = reverse('controller:device_download_config', args=[d.pk]) with catch_signal(config_download_requested) as handler: response = self.client.get(url, {'key': d.key, 'management_ip': '10.0.0.2'}) handler.assert_called_once_with( sender=Device, signal=config_download_requested, instance=d, request=response.wsgi_request, )
def test_vpn_checksum_requested_signal_is_emitted(self): v = self._create_vpn() url = reverse('controller:vpn_checksum', args=[v.pk]) with catch_signal(checksum_requested) as handler: response = self.client.get(url, {'key': v.key}) handler.assert_called_once_with( sender=Vpn, signal=checksum_requested, instance=v, request=response.wsgi_request, )
def test_device_name_changed_emitted(self): org = self._get_org() device = self._create_device(name='test', organization=org) with catch_signal(device_name_changed) as handler: device.name = 'newtestdevice' device.save() self.assertEqual(device.name, 'newtestdevice') handler.assert_called_once_with(sender=Device, instance=device, signal=device_name_changed)
def test_general_metric_signal_emitted(self): m = self._create_general_metric(name='load') t = self._create_threshold(metric=m, operator='>', value=90, seconds=0) with catch_signal(threshold_crossed) as handler: m.check_threshold(91) handler.assert_called_once_with( threshold=t, metric=m, target=None, sender=Metric, signal=threshold_crossed, )
def test_is_working_change_signal_emitted(self): ckey = self._create_credentials_with_key(port=self.ssh_server.port) dc = self._create_device_connection(credentials=ckey) with catch_signal(is_working_changed) as handler: dc.is_working = True dc.save() handler.assert_called_once_with( instance=dc, is_working=True, sender=DeviceConnection, signal=is_working_changed, )
def test_metric_post_write_signals_emitted(self): om = self._create_object_metric() with catch_signal(post_metric_write) as handler: om.write(3, current=True, time=start_time) handler.assert_called_once_with( sender=Metric, metric=om, values={om.field_name: 3}, signal=post_metric_write, time=start_time.strftime('%Y-%m-%dT%H:%M:%S.%fZ'), current=True, )
def test_subnet_ips_provisioned_signal(self): rule = self._get_vpn_subdivision_rule() with catch_signal(subnet_provisioned) as handler: self.config.templates.add(self.template) handler.assert_called_once() subnet_query = self.subnet_query.filter(organization_id=self.org.id).exclude( id=self.master_subnet.id ) self.assertEqual( subnet_query.count(), rule.number_of_subnets, )
def test_config_status_modified_after_template_added(self): t = self._create_template() c = self._create_config(device=self._create_device(name='test-status')) c.status = 'applied' c.save() c.refresh_from_db() with catch_signal(config_status_changed) as handler: c.templates.add(t) c.refresh_from_db() handler.assert_called_once_with(sender=Config, signal=config_status_changed, instance=c)
def test_config_modified_signal_always_sent(self): temp = self._create_template() conf = self._create_config(device=self._create_device( name='test-status')) self.assertEqual(conf.status, 'modified') # refresh instance to reset _just_created attribute conf = Config.objects.get(pk=conf.pk) with catch_signal(config_modified) as handler: conf.templates.add(temp) handler.assert_called_once_with( sender=Config, signal=config_modified, instance=conf, device=conf.device, config=conf, ) conf.status = 'applied' conf.save() conf.refresh_from_db() self.assertEqual(conf.status, 'applied') temp.config['interfaces'][0]['name'] = 'eth1' temp.full_clean() with catch_signal(config_modified) as handler: temp.save() conf.refresh_from_db() handler.assert_called_once() self.assertEqual(conf.status, 'modified') # status has already changed to modified # sgnal should be triggered anyway with catch_signal(config_modified) as handler: temp.config['interfaces'][0]['name'] = 'eth2' temp.full_clean() temp.save() conf.refresh_from_db() handler.assert_called_once() self.assertEqual(conf.status, 'modified')
def test_status_changed(self): dm, ping, load, process_count = self._create_env() # check signal with catch_signal(health_status_changed) as handler: dm.update_status('problem') dm.refresh_from_db() self.assertEqual(dm.status, 'problem') handler.assert_called_once_with( instance=dm, status='problem', sender=DeviceMonitoring, signal=health_status_changed, )
def test_config_backend_changed(self): org = self._get_org() old_backend = 'netjsonconfig.OpenWrt' backend = 'netjsonconfig.OpenWisp' group = self._create_device_group(organization=org) t1 = self._create_template(name='t1', backend=old_backend) t2 = self._create_template(name='t2', backend=backend) group.templates.add(*[t1, t2]) with self.subTest( 'config_backend_changed signal must not be sent on creation'): with catch_signal(config_backend_changed) as handler: d = self._create_device(group=group, organization=org) handler.assert_not_called() self.assertTrue(d.config.templates.filter(pk=t1.pk).exists()) self.assertFalse(d.config.templates.filter(pk=t2.pk).exists()) with self.subTest( 'config_backend_changed signal must not be sent on config status change' ): with catch_signal(config_backend_changed) as handler: c = d.config c.status = 'applied' c.save(update_fields=['status']) handler.assert_not_called() with self.subTest( 'config_backend_changed signal must be sent on backend change' ): with catch_signal(config_backend_changed) as handler: c = d.config c.backend = backend c.save(update_fields=['backend']) handler.assert_called_once_with( sender=Config, signal=config_backend_changed, instance=c, old_backend=old_backend, backend=backend, ) self.assertTrue(d.config.templates.filter(pk=t2.pk).exists()) self.assertFalse(d.config.templates.filter(pk=t1.pk).exists())
def test_management_ip_changed_emitted(self): device = self._create_device(organization=self._get_org()) with catch_signal(management_ip_changed) as handler: device.management_ip = '0.0.0.0' device.save() handler.assert_called_once_with( management_ip='0.0.0.0', old_management_ip=None, sender=Device, signal=management_ip_changed, instance=device, )
def test_device_metrics_received_signal(self): d = self._create_device(organization=self._create_org()) dd = DeviceData(name='test-device', pk=d.pk) data = self._data() self._create_object_metric(name='ping', configuration='ping', content_object=d) with catch_signal(device_metrics_received) as handler: response = self._post_data(d.id, d.key, data) request = response.renderer_context['request'] handler.assert_called_once_with( instance=dd, request=request, sender=DeviceData, signal=device_metrics_received, )
def test_device_report_status_applied(self): d = self._create_device_config() with catch_signal(config_status_changed) as handler: response = self.client.post( reverse('controller:device_report_status', args=[d.pk]), {'key': d.key, 'status': 'applied'}, ) d.config.refresh_from_db() handler.assert_called_once_with( sender=Config, signal=config_status_changed, instance=d.config ) self._check_header(response) d.config.refresh_from_db() self.assertEqual(d.config.status, 'applied')
def test_device_report_status_error(self): d = self._create_device_config() url = reverse('controller:device_report_status', args=[d.pk]) with self.subTest('Test without error reason'): with catch_signal(config_status_changed) as handler: response = self.client.post( url, {'key': d.key, 'status': 'error'}, ) d.config.refresh_from_db() handler.assert_called_once_with( sender=Config, signal=config_status_changed, instance=d.config ) self._check_header(response) d.config.refresh_from_db() self.assertEqual(d.config.status, 'error') with self.subTest('Test with error reason'): error_reason = ( 'daemon.crit openwisp: Could not apply configuration,' ' openwisp-update-config exit code was 1\n' 'daemon.info openwisp: The most recent configuration' ' backup was restored' ) with catch_signal(config_status_changed) as handler: response = self.client.post( url, {'key': d.key, 'status': 'error', 'error_reason': error_reason}, ) d.config.refresh_from_db() handler.assert_called_once_with( sender=Config, signal=config_status_changed, instance=d.config ) self._check_header(response) d.config.refresh_from_db() self.assertEqual(d.config.status, 'error') self.assertEqual(d.config.error_reason, error_reason)