def test_send_report_in_special_order_receive_right_pair( procfs_timeline, power_timeline): timedet = datetime.timedelta(250) sync = Sync(lambda x: isinstance(x, PowerReport), lambda x: isinstance(x, ProcfsReport), timedet) sum = 0 r = power_timeline[0] sync.add_report(PowerReport.from_json(r)) r = power_timeline[1] sync.add_report(PowerReport.from_json(r)) for i in range(len(power_timeline) - 2): r = power_timeline[i + 2] sync.add_report(PowerReport.from_json(r)) r = procfs_timeline[i] sync.add_report(ProcfsReport.from_json(r)) r = sync.request() assert r is not None report1, report2 = r assert abs(report1.timestamp - report2.timestamp) <= timedet sum += 1 r = procfs_timeline[-2] sync.add_report(ProcfsReport.from_json(r)) r = procfs_timeline[-1] sync.add_report(ProcfsReport.from_json(r)) assert sum == len(power_timeline) - 2
def test_create_procfs_report_from_json_with_datetime_timestamp_format_create_a_ProcfsReport( ): json_input = gen_json_procfs_report(1)[0] json_input['timestamp'] = datetime.strptime(json_input['timestamp'], "%Y-%m-%dT%H:%M:%S.%f") report = ProcfsReport.from_json(json_input) assert isinstance(report, ProcfsReport)
def test_creating_report_with_metadata(): report = ProcfsReport( ("2021-09-14T12:37:37.168817"), 'toto', ["firefox_cgroup", "emacs_cgroup", "zsh_cgroup", "mongo_cgroup"], { "firefox_cgroup": 8.36, "emacs_cgroup": 5.52, "zsh_cgroup": 0.01, "mongo_cgroup": 0.64, }, 42, {"tag": 1}) assert report.metadata["tag"] == 1
def test_create_procfs_report_from_csv_with_one_lines_create_an_procfs_report( ): csv_lines = [("procfs", { "timestamp": "2021-09-14T12:37:37.669237", "sensor": "formula_group", "target": ["firefox_cgroup", "emacs_cgroup", "zsh_cgroup", "mongo_cgroup"], "usage": { "firefox_cgroup": 8.36, "emacs_cgroup": 5.52, "zsh_cgroup": 0.01, "mongo_cgroup": 0.64, }, "global_cpu_usage": 27.600000000000012, })] report = ProcfsReport.from_csv_lines(csv_lines) assert isinstance(report, ProcfsReport)
def test_create_procfs_report_from_csv_with_bad_timestamp_format_raise_BadInputData( ): csv_lines = [("procfs", { "timestamp": "2021-09-14T123737.669237", "sensor": "formula_group", "target": ["firefox_cgroup", "emacs_cgroup", "zsh_cgroup", "mongo_cgroup"], "usage": { "firefox_cgroup": 8.36, "emacs_cgroup": 5.52, "zsh_cgroup": 0.01, "mongo_cgroup": 0.64, }, "global_cpu_usage": 27.600000000000012, })] with pytest.raises(BadInputData): report = ProcfsReport.from_csv_lines(csv_lines)
def test_create_report_from_csv_with_metadata(): csv_lines = [("procfs", { "timestamp": "2021-09-14T12:37:38.669237", "sensor": "formula_group", "target": ["firefox_cgroup", "emacs_cgroup", "zsh_cgroup", "mongo_cgroup"], "usage": { "firefox_cgroup": 8.36, "emacs_cgroup": 5.52, "zsh_cgroup": 0.01, "mongo_cgroup": 0.64, }, "global_cpu_usage": 27.600000000000012, "tag": 1 })] report = ProcfsReport.from_csv_lines(csv_lines) assert report.metadata["tag"] == 1
def test_create_procfs_report_from_json_without_sensor_field_raise_BadInputData( ): json_input = gen_json_procfs_report(1)[0] del json_input['sensor'] with pytest.raises(BadInputData): report = ProcfsReport.from_json(json_input)
def test_create_procfs_report_from_json_with_str_timestamp_with_bad_format_raise_BadInputData( ): json_input = gen_json_procfs_report(1)[0] json_input['timestamp'] = "1970-09-01T090909.543" with pytest.raises(BadInputData): report = ProcfsReport.from_json(json_input)
def test_create_procfs_report_from_json_wit_str_timestamp_create_a_ProcfsReport( ): json_input = gen_json_procfs_report(1)[0] report = ProcfsReport.from_json(json_input) assert isinstance(report, ProcfsReport)
def test_create_report_from_json_with_metadata(): json_input = gen_json_procfs_report(1)[0] json_input["metadata"] = {} json_input["metadata"]["tag"] = 1 report = ProcfsReport.from_json(json_input) assert report.metadata["tag"] == 1