Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_csvdb_second_rapl_missing(self, csvdb):
"""
Create two reports, one is full, second without rapl, then return None
"""
csvdb.add_files(SECOND_RAPL_MISSING)
csvdb.connect()
group_name = [path.split('/')[-1][:-4] for path in SECOND_RAPL_MISSING]
csvdb_iter = csvdb.iter(HWPCModel(), False)
for i in range(2):
report = next(csvdb_iter)
for group in group_name:
if i == 1 and "rapl" in group:
assert group not in report.groups
else:
assert group in report.groups
with pytest.raises(StopIteration) as pytest_wrapped:
next(csvdb_iter)
assert pytest_wrapped.type == StopIteration
def test_csvdb_second_primary_missing(self, csvdb):
"""
Create one full HWPCReport (the first), then return None
"""
csvdb.add_files(SECOND_PRIMARY_MISSING)
csvdb.connect()
group_name = [path.split('/')[-1][:-4] for path in SECOND_PRIMARY_MISSING]
csvdb_iter = csvdb.iter(HWPCModel(), False)
report = next(csvdb_iter)
for group in group_name:
assert group in report.groups
assert report.timestamp == timestamp_to_datetime(1539260664189)
with pytest.raises(StopIteration) as pytest_wrapped:
next(csvdb_iter)
assert pytest_wrapped.type == StopIteration
def test_csvdb_first_primary_missing(self, csvdb):
"""
Create one full HWPCReport (the second), then return None
"""
csvdb.add_files(FIRST_PRIMARY_MISSING)
csvdb.connect()
group_name = [path.split('/')[-1][:-4] for path in FIRST_PRIMARY_MISSING]
csvdb_iter = csvdb.iter(HWPCModel(), False)
report = next(csvdb_iter)
for group in group_name:
assert group in report.groups
assert report.timestamp == timestamp_to_datetime(1539260665189)
with pytest.raises(StopIteration) as pytest_wrapped:
next(csvdb_iter)
assert pytest_wrapped.type == StopIteration
def test_csvdb_first_rapl_missing(self, csvdb):
"""
Create two reports, one without rapl, second is full, then return None
"""
csvdb.add_files(FIRST_RAPL_MISSING)
csvdb.connect()
group_name = [path.split('/')[-1][:-4] for path in FIRST_RAPL_MISSING]
csvdb_iter = csvdb.iter(HWPCModel(), False)
for i in range(2):
report = next(csvdb_iter)
for group in group_name:
if i == 0 and "rapl" in group:
assert group not in report.groups
else:
assert group in report.groups
with pytest.raises(StopIteration) as pytest_wrapped:
next(csvdb_iter)
assert pytest_wrapped.type == StopIteration
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
for _ in range(1):
mongodb.save(report, HWPCModel())
# Check if there is nothing after
# Formula
formula_factory = (lambda name, verbose:
DummyFormulaActor(name, {'my_pusher': pusher}, level_logger=verbose, sleep_time=0.1))
# 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)
supervisor.join()
assert get_number_of_output_reports() == 30 * 2
def test_mongodb_save_basic_db(database):
"""
Test save mongodb collection
"""
# Load DB
mongodb = MongoDB(URI, "test_mongodb", "test_mongodb3")
mongodb.connect()
# Check if save work
basic_count = mongodb.collection.count_documents({})
for _ in range(2):
mongodb.save(gen_hwpc_report(), HWPCModel())
assert mongodb.collection.count_documents({}) == basic_count + 2
# 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)
signal.signal(signal.SIGTERM, term_handler)
def __init__(self, component_group_name):
Generator.__init__(self, component_group_name)
self.model_factory = {
'HWPCReport': HWPCModel(),
'PowerReport': PowerModel(),
'FormulaReport': FormulaModel(),
}
self.db_factory = {
'mongodb': lambda db_config: MongoDB(db_config['uri'], db_config['db'], db_config['collection']),
'csv': lambda db_config: CsvDB(current_path=os.getcwd() if 'directory' not in db_config else db_config['directory'],
files=[] if 'files' not in db_config else db_config['files']),
'influxdb': lambda db_config: InfluxDB(db_config['uri'], db_config['port'], db_config['db']),
'opentsdb': lambda db_config: OpenTSDB(db_config['uri'], db_config['port'], db_config['metric_name']),
}
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)
# Puller