Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
args = {'verbose': True, 'stream': True, 'input': {'mongodb': {'toto': {'model': 'hwpc_report', 'name': 'toto', 'uri': 'titi',
'db': 'tata', 'collection': 'tutu'}}}}
generator = PullerGenerator(None)
result = generator.generate(args)
assert len(result) == 1
assert 'toto' in result
puller = result['toto']
assert isinstance(puller, PullerActor)
assert puller.name == 'toto'
db = puller.state.database
assert isinstance(db, MongoDB)
assert db.uri == 'titi'
assert db.db_name == 'tata'
assert db.collection_name == 'tutu'
def mongodb_database(uri, database_name, collection_name):
"""
Return MongoDB database
"""
database = MongoDB(uri, database_name, collection_name)
return database
def test_mongodb_bad_hostname(database):
"""
Test if the database doesn't exist (hostname/port error)
"""
with pytest.raises(MongoBadDBError) as pytest_wrapped:
MongoDB("mongodb://lel:27017/", "error", "error").connect()
assert pytest_wrapped.type == MongoBadDBError
def mongodb_database(uri, database_name, collection_name):
"""
Return MongoDB database
"""
database = MongoDB(uri, database_name, collection_name)
return database
def test_run(database, supervisor):
# Pusher
output_mongodb = MongoDB(DB_URI, 'MongoDB1', 'test_result')
pusher = PusherActor("pusher_mongodb", PowerModel(), output_mongodb, level_logger=LOG_LEVEL)
# Formula
formula_factory = (lambda name, verbose:
DummyFormulaActor(name, {'my_pusher': pusher}, level_logger=verbose))
# Dispatcher
route_table = RouteTable()
route_table.dispatch_rule(HWPCReport, HWPCDispatchRule(getattr(HWPCDepthLevel, 'SOCKET'), primary=True))
dispatcher = DispatcherActor('dispatcher', formula_factory, route_table, level_logger=LOG_LEVEL)
# Puller
input_mongodb = MongoDB(DB_URI, 'MongoDB1', 'test_hwrep')
report_filter = Filter()
report_filter.filter(lambda msg: True, dispatcher)
def test_mongodb_read_capped_db(database):
"""
Test read mongodb capped collection
"""
# Load DB
mongodb = MongoDB(URI, "test_mongodb", "test_mongodb2")
# Check if we can read one time
mongodb.connect()
mongodb_iter = mongodb.iter(HWPCModel(), False)
report = None
for _ in range(mongodb.collection.count_documents({})):
report = next(mongodb_iter)
assert report is not None
# Check if there is nothing after
with pytest.raises(StopIteration) as pytest_wrapped:
next(mongodb_iter)
assert pytest_wrapped.type == StopIteration
# Add data in the collection
# Pusher
output_mongodb = MongoDB(DB_URI, 'MongoDB1', 'test_result')
pusher = PusherActor("pusher_mongodb", PowerModel(), output_mongodb, level_logger=LOG_LEVEL)
# Formula
formula_factory = (lambda name, verbose:
DummyFormulaActor(name, {'my_pusher': pusher}, level_logger=verbose))
# Dispatcher
route_table = RouteTable()
route_table.dispatch_rule(HWPCReport, HWPCDispatchRule(getattr(HWPCDepthLevel, 'SOCKET'), primary=True))
dispatcher = DispatcherActor('dispatcher', formula_factory, route_table, level_logger=LOG_LEVEL)
# Puller
input_mongodb = MongoDB(DB_URI, 'MongoDB1', 'test_hwrep')
report_filter = Filter()
report_filter.filter(lambda msg: True, dispatcher)
puller = PullerActor("puller_mongodb", input_mongodb, report_filter, HWPCModel(), level_logger=LOG_LEVEL)
supervisor.launch_actor(pusher)
supervisor.launch_actor(dispatcher)
supervisor.launch_actor(puller)
t = time.time()
number_of_output_reports = 0
for i in range(3):
time.sleep(0.2)
current = get_number_of_output_reports()
assert current >= number_of_output_reports
number_of_output_reports = current
level_logger=args.verbose)
# Formula
formula_factory = (lambda name, verbose:
RAPLFormulaActor(name, pusher, level_logger=verbose))
# Dispatcher
route_table = RouteTable()
route_table.dispatch_rule(HWPCReport, HWPCDispatchRule(
getattr(HWPCDepthLevel, args.hwpc_dispatch_rule), primary=True))
dispatcher = DispatcherActor('dispatcher', formula_factory, route_table,
level_logger=args.verbose)
# Puller
input_mongodb = MongoDB(args.input_uri,
args.input_db, args.input_collection,
HWPCModel(), stream_mode=args.stream_mode)
report_filter = Filter()
report_filter.filter(lambda msg: True, dispatcher)
puller = PullerActor("puller_mongodb", input_mongodb,
report_filter, level_logger=args.verbose)
##########################################################################
# Actor start step
# Setup signal handler
def term_handler(_, __):
puller.send_kill()
dispatcher.send_kill()
pusher.send_kill()
exit(0)
def launch_powerapi(args, logger):
##########################################################################
# Actor Creation
# Pusher
output_mongodb = MongoDB(args.output_uri,
args.output_db, args.output_collection,
HWPCModel())
pusher = PusherActor("pusher_mongodb", PowerReport, output_mongodb,
level_logger=args.verbose)
# Formula
formula_factory = (lambda name, verbose:
RAPLFormulaActor(name, pusher, level_logger=verbose))
# Dispatcher
route_table = RouteTable()
route_table.dispatch_rule(HWPCReport, HWPCDispatchRule(
getattr(HWPCDepthLevel, args.hwpc_dispatch_rule), primary=True))
dispatcher = DispatcherActor('dispatcher', formula_factory, route_table,
level_logger=args.verbose)