def test_logs_to_csv(self): logs = V1Logs( last_file=1000, logs=[ V1Log( value="foo", timestamp=parse_datetime("2018-12-11 10:24:57 UTC"), node="node1", pod="pod1", container="container1", ), V1Log( value="foo", timestamp=parse_datetime("2018-12-11 10:24:57 UTC"), node="node1", pod="pod1", container="container1", ), V1Log( value="foo", timestamp=parse_datetime("2018-12-11 10:24:57 UTC"), node="node1", pod="pod1", container="container1", ), ], ) assert logs.to_csv() == "".join([ "\n{}".format(logs.logs[0].to_csv()), "\n{}".format(logs.logs[1].to_csv()), "\n{}".format(logs.logs[2].to_csv()), ])
def test_chunk_logs(self): logs = [ V1Log( value="foo1", timestamp=parse_datetime("2018-12-11 10:24:57 UTC"), node="node1", pod="pod1", container="container1", ), V1Log( value="foo2", timestamp=parse_datetime("2018-12-11 10:24:57 UTC"), node="node1", pod="pod1", container="container1", ), V1Log( value="foo3", timestamp=parse_datetime("2018-12-11 10:24:57 UTC"), node="node1", pod="pod1", container="container1", ), ] V1Logs.CHUNK_SIZE = 2 chunks = [c for c in V1Logs.chunk_logs(logs)] # 1 chunk assert [i.value for i in chunks[0].logs] == ["foo1", "foo2"] # 2 chunk assert [i.value for i in chunks[1].logs] == ["foo3"]
def test_logs_with_files(self): logs = V1Logs( last_file=1000, last_time=now(), files=["file1", "file2"], logs=[ V1Log( value="foo", timestamp=parse_datetime("2018-12-11 10:24:57 UTC"), node="node1", pod="pod1", container="container1", ), V1Log( value="foo", timestamp=parse_datetime("2018-12-11 10:24:57 UTC"), node="node1", pod="pod1", container="container1", ), V1Log( value="foo", timestamp=parse_datetime("2018-12-11 10:24:57 UTC"), node="node1", pod="pod1", container="container1", ), ], ) logs_dict = logs.to_light_dict() assert logs_dict == logs.from_dict(logs_dict).to_light_dict() assert logs_dict == logs.read(logs.to_dict(dump=True)).to_light_dict()
def test_logs(self): logs = V1Logs( last_file=1000, logs=[ V1Log( value="foo", timestamp=dt_parser.parse("2018-12-11 10:24:57 UTC"), node="node1", pod="pod1", container="container1", ), V1Log( value="foo", timestamp=dt_parser.parse("2018-12-11 10:24:57 UTC"), node="node1", pod="pod1", container="container1", ), V1Log( value="foo", timestamp=dt_parser.parse("2018-12-11 10:24:57 UTC"), node="node1", pod="pod1", container="container1", ), ], ) logs_dict = logs.to_light_dict() assert logs_dict == logs.from_dict(logs_dict).to_light_dict() assert logs_dict == logs.read(logs.to_dict(dump=True)).to_light_dict()
def test_log_line_has_iso_datetime(self): parsed = V1Log.process_log_line( value="2018-12-11T08:49:07.163495183Z foo", node="node1", pod="pod1", container="container1", ) expected = V1Log( value="foo", timestamp=parse_datetime("2018-12-11T08:49:07.163495+00:00"), node="node1", pod="pod1", container="container1", ) assert parsed == expected
def test_log_line_has_datetime(self): parsed = V1Log.process_log_line( value="2018-12-11 10:24:57 UTC foo", node="node1", pod="pod1", container="container1", ) expected = V1Log( value="foo", timestamp=parse_datetime("2018-12-11 10:24:57 UTC"), node="node1", pod="pod1", container="container1", ) assert parsed == expected
def handle_log_line(log: V1Log): log_dict = log.to_dict() log_line = "" if log.timestamp and show_timestamp: date_value = local_datetime(log_dict.get("timestamp")) log_line = Printer.add_color(date_value, "white") + " | " def get_container_info(): if container_info in job_to_color: color = job_to_color[container_info] else: color = colors[0] colors.rotate(-1) job_to_color[container_info] = color return Printer.add_color(container_info, color) + " | " if not all_containers and log.container != MAIN_JOB_CONTAINER: return log_line if all_info: container_info = "" if log.node: log_line += Printer.add_color(log_dict.get("node"), "white") + " | " if log.pod: log_line += Printer.add_color(log_dict.get("pod"), "white") + " | " if log.container: container_info = log_dict.get("container") log_line += get_container_info() log_line += log_dict.get("value") Printer.log(log_line, nl=True)
def test_has_datetime_timestamp(self): log_result = V1Log.process_log_line( timestamp=now(), value="foo", node="node1", pod="pod1", container="container1", ) assert log_result.timestamp.date() == now().date()
def test_to_csv(self): log_line = V1Log( value="foo", timestamp=parse_datetime("2018-12-11 10:24:57 UTC"), node="node1", pod="pod1", container="container1", ) assert log_line.to_csv( ) == '{}|node1|pod1|container1|{{"_":"foo"}}'.format( log_line.timestamp)