def test_validate_connectivity(self, mock_close_dispatcher, mock_register_observer, mock_access_info_get): # Get a random host a = random.randint(0, 255) b = random.randint(0, 255) c = random.randint(0, 255) d = random.randint(0, 255) host = str(a) + '.' + str(b) + '.' + str(c) + '.' + str(d) # Get a random port port = random.randint(1024, 65535) # snmpv3 v3_alert_source = fakes.fake_v3_alert_source() v3_alert_source['host'] = host v3_alert_source['port'] = port mock_access_info_get.return_value = {'model': 'vsp'} snmp_validator.SNMPValidator.validate_connectivity( context.RequestContext(), v3_alert_source) self.assertEqual(mock_close_dispatcher.call_count, 1) self.assertEqual(mock_register_observer.call_count, 1) # snmpv2c v2_alert_source = fakes.fake_v2_alert_source() v2_alert_source['host'] = host v2_alert_source['port'] = port snmp_validator.SNMPValidator.validate_connectivity( context.RequestContext(), v2_alert_source) self.assertEqual(mock_close_dispatcher.call_count, 2) self.assertEqual(mock_register_observer.call_count, 1)
def blank(cls, *args, **kwargs): if not kwargs.get('base_url'): kwargs['base_url'] = 'http://localhost/v1' use_admin_context = kwargs.pop('use_admin_context', False) out = os_wsgi.Request.blank(*args, **kwargs) out.environ['delfin.context'] = context.RequestContext( is_admin=use_admin_context) return out
def test_request_context_read_deleted_invalid(self): self.assertRaises(ValueError, context.RequestContext, '111', '222', read_deleted=True) ctxt = context.RequestContext('111', '222') self.assertRaises(ValueError, setattr, ctxt, 'read_deleted', True)
def _cb_fun(self, state_reference, context_engine_id, context_name, var_binds, cb_ctx): """Callback function to process the incoming trap.""" exec_context = self.snmp_engine.observer.getExecutionContext( 'rfc3412.receiveMessage:request') LOG.debug("Get notification from: %s" % "#".join([str(x) for x in exec_context['transportAddress']])) alert = {} try: # transportAddress contains both ip and port, extract ip address source_ip = exec_context['transportAddress'][0] alert_source = self._get_alert_source_by_host(source_ip) # In case of non v3 version, community string is used to map the # trap. Pysnmp library helps to filter traps whose community string # are not configured. But if a given community name x is configured # for storage1, if the trap is received with x from storage 2, # library will allow the trap. So for non v3 version, we need to # verify that community name is configured at alert source db for # the storage which is sending traps. # context_name contains the incoming community string value if exec_context['securityModel'] != constants.SNMP_V3_INT \ and cryptor.decode(alert_source['community_string']) \ != str(context_name): msg = (_("Community string not matching with alert source %s, " "dropping it.") % source_ip) raise exception.InvalidResults(msg) for oid, val in var_binds: # Fill raw oid and values oid_str = str(oid) alert[oid_str] = str(val) # Fill additional info to alert info alert['transport_address'] = source_ip alert['storage_id'] = alert_source['storage_id'] filters = { 'mgmt_ip': source_ip, 'storage_id': alert_source['storage_id'] } ctxt = context.RequestContext() controllers = db.controller_get_all(ctxt, filters=filters) if controllers: alert['controller_name'] = controllers[0].get('name') # Handover to alert processor for model translation and export self.alert_processor.process_alert_info(alert) except exception.DelfinException as e: # Log and end the trap processing error flow err_msg = _("Failed to process alert report (%s).") % e.msg LOG.exception(err_msg) except Exception as e: err_msg = six.text_type(e) LOG.exception(err_msg)
def test_request_context_elevated(self): user_context = context.RequestContext('fake_user', 'fake_project', is_admin=False) self.assertFalse(user_context.is_admin) self.assertEqual([], user_context.roles) admin_context = user_context.elevated() self.assertFalse(user_context.is_admin) self.assertTrue(admin_context.is_admin) self.assertNotIn('admin', user_context.roles) self.assertIn('admin', admin_context.roles)
def _get_alert_source_by_host(source_ip): """Gets alert source for given source ip address.""" filters = {'host': source_ip} ctxt = context.RequestContext() # Using the known filter and db exceptions are handled by api alert_source = db.alert_source_get_all(ctxt, filters=filters) if not alert_source: raise exception.AlertSourceNotFoundWithHost(source_ip) # This is to make sure unique host is configured each alert source if len(alert_source) > 1: msg = (_("Failed to get unique alert source with host %s.") % source_ip) raise exception.InvalidResults(msg) return alert_source[0]
def perf_collect(self, storage_id, interval, is_historic, resource): """ This function received the request from scheduler to create tasks and push those tasks to rabbitmq. :param storage_id: The registered storage_id :param interval: collection interval period :param is_historic: to enable historic collection :param resource: resource type, ex: array, pool, volume etc. :return: """ ctxt = context.RequestContext() LOG.debug("Request received to create perf_collect task for storage_" "id :{0} and resource_type:{1}".format(storage_id, resource)) self.task_rpcapi.performance_metrics_collection( ctxt, storage_id, interval, is_historic, resources.PerformanceCollectionTask.__module__ + '.' + constants.RESOURCE_CLASS_TYPE.get(resource))
def _get_alert_source_by_host(source_ip): """Gets alert source for given source ip address.""" filters = {'host~': source_ip} ctxt = context.RequestContext() # Using the known filter and db exceptions are handled by api alert_sources = db.alert_source_get_all(ctxt, filters=filters) if not alert_sources: raise exception.AlertSourceNotFoundWithHost(source_ip) # This is to make sure unique host is configured each alert source unique_alert_source = None if len(alert_sources) > 1: # Clear invalid alert_source for alert_source in alert_sources: try: db.storage_get(ctxt, alert_source['storage_id']) except exception.StorageNotFound: LOG.warning('Found redundancy alert source for storage %s' % alert_source['storage_id']) try: db.alert_source_delete( ctxt, alert_source['storage_id']) except Exception as e: LOG.warning('Delete the invalid alert source failed, ' 'reason is %s' % six.text_type(e)) else: unique_alert_source = alert_source else: unique_alert_source = alert_sources[0] if unique_alert_source is None: msg = (_("Failed to get unique alert source with host %s.") % source_ip) raise exception.InvalidResults(msg) return unique_alert_source
def test_request_context_read_deleted(self): ctxt = context.RequestContext('111', '222', read_deleted='yes') self.assertEqual('yes', ctxt.read_deleted) ctxt.read_deleted = 'no' self.assertEqual('no', ctxt.read_deleted)
def __init__(self): self.environ = {'delfin.context': context.RequestContext()} pass
def __call__(self, req): req.environ['delfin.context'] = context.RequestContext() return self.application