def test_ReportHandler_message_saving_order(): """ Handle 3 HWPCReport with a pusher.ReportHandler The maximum size of the handler buffer is 2 This 3 reports are not sent in their chronological order First report : timestamp = 0 Second report : timestamp = 2 Third report : timestamp = 1 When the handler save the received reports test if the reports are saved in their chronological order """ fake_database = get_fake_db() fake_database.save_many = Mock() pusher_state = PusherState(Mock(), fake_database, HWPCModel()) report_handler = ReportHandler(pusher_state, delay=10000, max_size=2) report0 = HWPCReport(datetime.fromtimestamp(0), None, None, None) report1 = HWPCReport(datetime.fromtimestamp(2), None, None, None) report2 = HWPCReport(datetime.fromtimestamp(1), None, None, None) report_handler.handle(report0) report_handler.handle(report1) report_handler.handle(report2) buffer = fake_database.save_many.call_args[0][0] assert len(buffer) == 3 assert buffer[0].timestamp < buffer[1].timestamp assert buffer[1].timestamp < buffer[2].timestamp
def test_with_two_filter(self, database): """ Test filter with two filter - 2 first report return first dispatcher - 2 next report return first and second dispatcher - 2 next report return None """ mongodb = MongoDB(URI, "test_filter", "test_filter1", report_model=HWPCModel()) mongodb.connect() hwpc_filter = Filter() hwpc_filter.filter(lambda msg: True if "sensor" in msg.sensor else False, 1) hwpc_filter.filter(lambda msg: True if "test1" in msg.sensor else False, 2) hwpc_filter.filter(lambda msg: True if msg.sensor == "sensor_test2" else False, 3) mongodb_it = iter(mongodb) for _ in range(2): hwpc_report = HWPCReport.deserialize(next(mongodb_it)) assert hwpc_filter.route(hwpc_report) == [1, 2] for _ in range(2): hwpc_report = HWPCReport.deserialize(next(mongodb_it)) assert hwpc_filter.route(hwpc_report) == [1, 3] for _ in range(2): hwpc_report = HWPCReport.deserialize(next(mongodb_it)) assert hwpc_filter.route(hwpc_report) == [1]
def create_report_root(group_list, timestamp=datetime.fromtimestamp(0), sensor='toto', target='system'): sensor = HWPCReport(timestamp=timestamp, sensor=sensor, target=target, groups={}) for (group_id, group) in group_list: sensor.groups[group_id] = group return sensor
def test_dispatcher_start_handler(self): """ Test the StartHandler of DispatcherActor """ # Define DispatcherState dispatcher_state = DispatcherState(Mock(), None, None) assert dispatcher_state.initialized is False # Define StartHandler start_handler = StartHandler(dispatcher_state) # Test Random message when state is not initialized to_send = [ OKMessage(), ErrorMessage("Error"), HWPCReport("test", "test", "test", {}) ] for msg in to_send: start_handler.handle(msg) assert dispatcher_state.initialized is False # Try to initialize the state start_handler.handle(StartMessage()) assert dispatcher_state.initialized is True
def test_dispatcher_start_handler(self): """ Test the StartHandler of DispatcherActor """ # Define DispatcherState fake_socket_interface = get_fake_socket_interface() dispatcher_state = DispatcherState(Actor._initial_behaviour, fake_socket_interface, Mock(), None, None) assert dispatcher_state.initialized is False # Define StartHandler start_handler = StartHandler() # Test Random message when state is not initialized to_send = [ OKMessage(), ErrorMessage("Error"), HWPCReport("test", "test", "test", {}) ] for msg in to_send: start_handler.handle(msg, dispatcher_state) assert fake_socket_interface.method_calls == [] assert dispatcher_state.initialized is False # Try to initialize the state start_handler.handle(StartMessage(), dispatcher_state) assert dispatcher_state.initialized is True
def test_create_hwpc_report_from_json_with_datetime_timestamp_format_create_a_HWPCReport( ): json_input = extract_rapl_reports_with_2_sockets(1)[0] json_input['timestamp'] = datetime.strptime(json_input['timestamp'], "%Y-%m-%dT%H:%M:%S.%f") report = HWPCReport.from_json(json_input) assert isinstance(report, HWPCReport)
def test_send_hwpc_report_to_rapl_formula_return_correct_result( self, system, started_actor, dummy_pipe_out): report = HWPCReport.from_json({ "timestamp": "2021-10-05T09:14:58.226", "sensor": "toto", "target": "all", "groups": { "rapl": { "0": { "7": { "RAPL_ENERGY_PKG": 5558763520.0, "time_enabled": 1000770053.0, "time_running": 1000770053.0 } } } } }) system.tell(started_actor, report) _, msg = recv_from_pipe(dummy_pipe_out, 1) assert isinstance(msg, PowerReport) assert msg.power == math.ldexp(5558763520.0, -32)
def test_create_hwpc_report_from_csv_without_socket_field_raise_BadInputData(): csv_lines = [('rapl', { 'sensor': 'toto', 'timestamp': '1970-09-01T09:09:09.543', 'target': 'all', 'cpu': '7', 'RAPL_VALUE': '1234' })] with pytest.raises(BadInputData): report = HWPCReport.from_csv_lines(csv_lines)
def test_create_hwpc_report_from_csv_with_one_lines_create_an_hwpc_report(): csv_lines = [('rapl', { 'sensor': 'toto', 'timestamp': '1970-09-01T09:09:09.543', 'target': 'all', 'socket': '0', 'cpu': '7', 'RAPL_VALUE': '1234' })] report = HWPCReport.from_csv_lines(csv_lines) assert isinstance(report, HWPCReport)
def test_create_report_from_csv_with_metadata(): csv_lines = [('rapl', { 'sensor': 'toto', 'timestamp': '1970-09-01T09:09:09.543', 'target': 'all', 'socket': '0', 'cpu': '7', 'RAPL_VALUE': '1234', 'tag': 1 })] report = HWPCReport.from_csv_lines(csv_lines) assert report.metadata["tag"] == 1
def test_create_hwpc_report_from_csv_with_two_lines_with_different_target_name_raise_BadInputData( ): csv_lines = [('rapl', { 'sensor': 'toto', 'timestamp': '1970-09-01T09:09:09.543', 'target': 'all', 'socket': '0', 'cpu': '7', 'RAPL_VALUE': '1234' }), ('rapl', { 'sensor': 'toto', 'timestamp': '1970-09-01T09:09:09.543', 'target': 'allall', 'socket': '1', 'cpu': '7', 'RAPL_VALUE': '4321' })] with pytest.raises(BadInputData): report = HWPCReport.from_csv_lines(csv_lines)
def test_create_hwpc_report_from_json_without_groups_field_raise_BadInputData( ): json_input = extract_rapl_reports_with_2_sockets(1)[0] del json_input['groups'] with pytest.raises(BadInputData): report = HWPCReport.from_json(json_input)
def test_create_hwpc_report_from_json_with_str_timestamp_with_bad_format_raise_BadInputData( ): json_input = extract_rapl_reports_with_2_sockets(1)[0] json_input['timestamp'] = '1970-09-01T090909.543' with pytest.raises(BadInputData): report = HWPCReport.from_json(json_input)
def test_create_hwpc_report_from_json_wit_str_timestamp_create_a_HWPCReport(): json_input = extract_rapl_reports_with_2_sockets(1)[0] report = HWPCReport.from_json(json_input) assert isinstance(report, HWPCReport)
def test_create_report_from_json_with_metadata(): json_input = extract_rapl_reports_with_2_sockets(1)[0] json_input["metadata"] = {} json_input["metadata"]["tag"] = 1 report = HWPCReport.from_json(json_input) assert report.metadata["tag"] == 1
def test_creating_report_with_metadata(): report = HWPCReport(('1970-09-01T09:09:10.543'), 'toto', 'all', {}, {"tag": 1}) assert report.metadata["tag"] == 1