def test_given_interpretors_can_interpret_then_use_first_interpretor(self): interpretor_true_1, interpretor_true_2, interpretor_false = Mock(), Mock(), Mock() interpretor_true_1.can_interpret.return_value = True interpretor_true_2.can_interpret.return_value = True interpretor_false.can_interpret.return_value = False generator = CallLogsGenerator([interpretor_false, interpretor_true_1, interpretor_true_2, interpretor_false]) cels = self._generate_cel_for_call(['545783248']) generator.call_logs_from_cel(cels) interpretor_true_1.interpret_cels.assert_called_once_with(cels, ANY) assert_that(interpretor_true_2.interpret_cels.called, is_(False)) assert_that(interpretor_false.interpret_cels.called, is_(False))
def _generate_call_logs(): parser = argparse.ArgumentParser(description='Call logs generator') options = parse_args(parser) key_config = load_key_file(DEFAULT_CONFIG) config = ChainMap(key_config, DEFAULT_CONFIG) auth_client = AuthClient(**config['auth']) confd_client = ConfdClient(**config['confd']) token_renewer = TokenRenewer(auth_client) token_renewer.subscribe_to_token_change(confd_client.set_token) cel_fetcher = CELFetcher() generator = CallLogsGenerator([ LocalOriginateCELInterpretor(confd_client), DispatchCELInterpretor(CallerCELInterpretor(confd_client), CalleeCELInterpretor(confd_client)) ]) writer = CallLogsWriter() publisher = BusPublisher(config) manager = CallLogsManager(cel_fetcher, generator, writer, publisher) options = vars(options) with token_renewer: if options.get('action') == 'delete': if options.get('all'): manager.delete_all() elif options.get('days'): manager.delete_from_days(options['days']) else: if options.get('days'): manager.generate_from_days(days=options['days']) else: manager.generate_from_count(cel_count=options['cel_count'])
def test_given_no_interpretor_can_interpret_then_raise(self): interpretor = Mock() interpretor.can_interpret.return_value = False generator = CallLogsGenerator([interpretor]) cels = self._generate_cel_for_call(['545783248']) assert_that(calling(generator.call_logs_from_cel).with_args(cels), raises(RuntimeError))
def _generate_call_logs(): parser = argparse.ArgumentParser(description='Call logs generator') options = parse_args(parser) file_config = { key: value for key, value in read_config_file_hierarchy(DEFAULT_CONFIG).items() if key in ('confd', 'bus', 'auth', 'db_uri', 'cel_db_uri') } key_config = {} auth_username = file_config['auth'].get('username') auth_password = file_config['auth'].get('password') if not (auth_username and auth_password): key_config = load_key_file(ChainMap(file_config, DEFAULT_CONFIG)) config = ChainMap(key_config, file_config, DEFAULT_CONFIG) set_xivo_uuid(config, logger) init_db_from_config({'db_uri': config['cel_db_uri']}) DBSession = new_db_session(config['db_uri']) CELDBSession = new_db_session(config['cel_db_uri']) dao = DAO(DBSession, CELDBSession) auth_client = AuthClient(**config['auth']) confd_client = ConfdClient(**config['confd']) token_renewer = TokenRenewer(auth_client) token_renewer.subscribe_to_token_change(confd_client.set_token) generator = CallLogsGenerator( confd_client, [ LocalOriginateCELInterpretor(), DispatchCELInterpretor(CallerCELInterpretor(), CalleeCELInterpretor()), ], ) token_renewer.subscribe_to_next_token_details_change( generator.set_default_tenant_uuid) writer = CallLogsWriter(dao) publisher = BusPublisher(service_uuid=config['uuid'], **config['bus']) manager = CallLogsManager(dao, generator, writer, publisher) options = vars(options) with token_renewer: if options.get('action') == 'delete': if options.get('all'): manager.delete_all() elif options.get('days'): manager.delete_from_days(options['days']) else: if options.get('days'): manager.generate_from_days(days=options['days']) else: manager.generate_from_count(cel_count=options['cel_count'])
def _generate_call_logs(): parser = argparse.ArgumentParser(description='Call logs generator') options = parse_args(parser) file_config = { key: value for key, value in read_config_file_hierarchy(DEFAULT_CONFIG).items() if key in ('confd', 'bus', 'auth', 'db_uri') } key_config = load_key_file(ChainMap(file_config, DEFAULT_CONFIG)) config = ChainMap(key_config, file_config, DEFAULT_CONFIG) init_db_from_config(config) auth_client = AuthClient(**config['auth']) confd_client = ConfdClient(**config['confd']) token_renewer = TokenRenewer(auth_client) token_renewer.subscribe_to_token_change(confd_client.set_token) cel_fetcher = CELFetcher() generator = CallLogsGenerator( confd_client, [ LocalOriginateCELInterpretor(confd_client), DispatchCELInterpretor( CallerCELInterpretor(confd_client), CalleeCELInterpretor(confd_client) ), ], ) token_renewer.subscribe_to_next_token_details_change( generator.set_default_tenant_uuid ) writer = CallLogsWriter() publisher = BusPublisher(config) manager = CallLogsManager(cel_fetcher, generator, writer, publisher) options = vars(options) with token_renewer: if options.get('action') == 'delete': if options.get('all'): manager.delete_all() elif options.get('days'): manager.delete_from_days(options['days']) else: if options.get('days'): manager.generate_from_days(days=options['days']) else: manager.generate_from_count(cel_count=options['cel_count'])
def __init__(self, config): auth_config = dict(config['auth']) auth_config.pop('key_file', None) auth_client = AuthClient(**auth_config) cel_fetcher = CELFetcher() confd_client = ConfdClient(**config['confd']) generator = CallLogsGenerator([ LocalOriginateCELInterpretor(confd_client), DispatchCELInterpretor(CallerCELInterpretor(confd_client), CalleeCELInterpretor(confd_client)) ]) writer = CallLogsWriter() self._publisher = BusPublisher(config) self.manager = CallLogsManager(cel_fetcher, generator, writer, self._publisher) self.bus_client = BusClient(config) self.rest_api = CoreRestApi(config) self.token_renewer = TokenRenewer(auth_client) self.token_renewer.subscribe_to_token_change(confd_client.set_token) self._load_plugins(config)
def __init__(self, config): auth_client = AuthClient(**config['auth']) cel_fetcher = CELFetcher() confd_client = ConfdClient(**config['confd']) generator = CallLogsGenerator( confd_client, [ LocalOriginateCELInterpretor(confd_client), DispatchCELInterpretor( CallerCELInterpretor(confd_client), CalleeCELInterpretor(confd_client), ), ], ) writer = CallLogsWriter() self.token_renewer = TokenRenewer(auth_client) self.token_renewer.subscribe_to_token_change(confd_client.set_token) self.token_renewer.subscribe_to_next_token_details_change( generator.set_default_tenant_uuid) self._publisher = BusPublisher(config) self.manager = CallLogsManager(cel_fetcher, generator, writer, self._publisher) self.bus_client = BusClient(config) self.rest_api = CoreRestApi(config) self.status_aggregator = StatusAggregator() self.token_status = TokenStatus() plugin_helpers.load( namespace='wazo_call_logd.plugins', names=config['enabled_plugins'], dependencies={ 'api': api, 'config': config, 'token_renewer': self.token_renewer, 'status_aggregator': self.status_aggregator, }, )
def __init__(self, config): self.config = config DBSession = new_db_session(config['db_uri']) CELDBSession = new_db_session(config['cel_db_uri']) self.dao = DAO(DBSession, CELDBSession) writer = CallLogsWriter(self.dao) # NOTE(afournier): it is important to load the tasks before configuring the Celery app self.celery_task_manager = plugin_helpers.load( namespace='wazo_call_logd.celery_tasks', names=config['enabled_celery_tasks'], dependencies={ 'config': self.config, 'dao': self.dao, 'app': celery.app, }, ) celery.configure(config) self._celery_process = celery.spawn_workers(config) auth_client = AuthClient(**config['auth']) confd_client = ConfdClient(**config['confd']) generator = CallLogsGenerator( confd_client, [ LocalOriginateCELInterpretor(), DispatchCELInterpretor( CallerCELInterpretor(), CalleeCELInterpretor(), ), ], ) self.token_renewer = TokenRenewer(auth_client) self.token_renewer.subscribe_to_token_change(confd_client.set_token) self.token_renewer.subscribe_to_next_token_details_change( generator.set_default_tenant_uuid) self.bus_publisher = BusPublisher(service_uuid=config['uuid'], **config['bus']) self.bus_consumer = BusConsumer(**config['bus']) self.manager = CallLogsManager(self.dao, generator, writer, self.bus_publisher) self._bus_subscribe() self.http_server = HTTPServer(config) if not app.config['auth'].get('master_tenant_uuid'): self.token_renewer.subscribe_to_next_token_details_change( init_master_tenant) self.status_aggregator = StatusAggregator() self.token_status = TokenStatus() plugin_helpers.load( namespace='wazo_call_logd.plugins', names=config['enabled_plugins'], dependencies={ 'api': api, 'config': config, 'dao': self.dao, 'token_renewer': self.token_renewer, 'status_aggregator': self.status_aggregator, 'bus_publisher': self.bus_publisher, }, )
def setUp(self): self.interpretor = Mock() self.generator = CallLogsGenerator([self.interpretor])
class TestCallLogsGenerator(TestCase): def setUp(self): self.interpretor = Mock() self.generator = CallLogsGenerator([self.interpretor]) def test_from_cel(self): self.generator.call_logs_from_cel = Mock() self.generator.list_call_log_ids = Mock() expected_calls = self.generator.call_logs_from_cel.return_value = Mock() expected_to_delete = self.generator.list_call_log_ids.return_value = Mock() cels = Mock() result = self.generator.from_cel(cels) self.generator.call_logs_from_cel.assert_called_once_with(cels) assert_that(result, all_of(has_property('new_call_logs', expected_calls), has_property('call_logs_to_delete', expected_to_delete))) def test_call_logs_from_cel_no_cels(self): cels = [] result = self.generator.call_logs_from_cel(cels) assert_that(result, equal_to([])) @patch('wazo_call_logd.raw_call_log.RawCallLog') def test_call_logs_from_cel_one_call(self, raw_call_log_constructor): linkedid = '9328742934' cels = self._generate_cel_for_call([linkedid]) call = raw_call_log_constructor.return_value = self.interpretor.interpret_cels.return_value expected_call = call.to_call_log.return_value result = self.generator.call_logs_from_cel(cels) self.interpretor.interpret_cels.assert_called_once_with(cels, call) assert_that(result, contains(expected_call)) @patch('wazo_call_logd.raw_call_log.RawCallLog') def test_call_logs_from_cel_two_calls(self, raw_call_log_constructor): cels_1 = self._generate_cel_for_call('9328742934') cels_2 = self._generate_cel_for_call('2707230959') cels = cels_1 + cels_2 call_1, call_2 = self.interpretor.interpret_cels.side_effect \ = raw_call_log_constructor.side_effect \ = [Mock(), Mock()] expected_call_1 = call_1.to_call_log.return_value expected_call_2 = call_2.to_call_log.return_value result = self.generator.call_logs_from_cel(cels) self.interpretor.interpret_cels.assert_any_call(cels_1, ANY) self.interpretor.interpret_cels.assert_any_call(cels_2, ANY) assert_that(result, contains_inanyorder(expected_call_1, expected_call_2)) @patch('wazo_call_logd.raw_call_log.RawCallLog') def test_call_logs_from_cel_two_calls_one_valid_one_invalid(self, raw_call_log_constructor): cels_1 = self._generate_cel_for_call('9328742934') cels_2 = self._generate_cel_for_call('2707230959') cels = cels_1 + cels_2 call_1, call_2 = self.interpretor.interpret_cels.side_effect \ = raw_call_log_constructor.side_effect \ = [Mock(), Mock()] expected_call_1 = call_1.to_call_log.return_value call_2.to_call_log.side_effect = InvalidCallLogException() result = self.generator.call_logs_from_cel(cels) self.interpretor.interpret_cels.assert_any_call(cels_1, ANY) self.interpretor.interpret_cels.assert_any_call(cels_2, ANY) assert_that(result, contains(expected_call_1)) def test_list_call_log_ids(self): cel_1, cel_2 = Mock(call_log_id=1), Mock(call_log_id=1) cel_3, cel_4 = Mock(call_log_id=2), Mock(call_log_id=None) cels = [cel_1, cel_2, cel_3, cel_4] result = self.generator.list_call_log_ids(cels) assert_that(result, contains_inanyorder(1, 2)) def test_given_interpretors_can_interpret_then_use_first_interpretor(self): interpretor_true_1, interpretor_true_2, interpretor_false = Mock(), Mock(), Mock() interpretor_true_1.can_interpret.return_value = True interpretor_true_2.can_interpret.return_value = True interpretor_false.can_interpret.return_value = False generator = CallLogsGenerator([interpretor_false, interpretor_true_1, interpretor_true_2, interpretor_false]) cels = self._generate_cel_for_call(['545783248']) generator.call_logs_from_cel(cels) interpretor_true_1.interpret_cels.assert_called_once_with(cels, ANY) assert_that(interpretor_true_2.interpret_cels.called, is_(False)) assert_that(interpretor_false.interpret_cels.called, is_(False)) def test_given_no_interpretor_can_interpret_then_raise(self): interpretor = Mock() interpretor.can_interpret.return_value = False generator = CallLogsGenerator([interpretor]) cels = self._generate_cel_for_call(['545783248']) assert_that(calling(generator.call_logs_from_cel).with_args(cels), raises(RuntimeError)) def _generate_cel_for_call(self, linked_id, cel_count=3): result = [] for _ in range(cel_count): result.append(Mock(linkedid=linked_id)) return result