def test_write_logs(tmpdir): with open(str(tmpdir / "output.yaml"), "wb") as f_logs: with open(str(tmpdir / "output.idx"), "wb") as f_idx: write_logs(f_logs, f_idx, "hello world\n".encode("utf-8")) write_logs(f_logs, f_idx, "how are you?\n".encode("utf-8")) assert read_logs(str(tmpdir)) == "hello world\nhow are you?\n" # nosec assert size_logs(str(tmpdir)) == 25 # nosec with open(str(tmpdir / "output.idx"), "rb") as f_idx: assert f_idx.read(8) == b"\x00\x00\x00\x00\x00\x00\x00\x00" # nosec assert f_idx.read(8) == b"\x0c\x00\x00\x00\x00\x00\x00\x00" # nosec
def logs(self, request, **kwargs): start = safe_str2int(request.query_params.get("start", 0)) end = safe_str2int(request.query_params.get("end", None)) try: data = read_logs(self.get_object().output_dir, start, end) if not data: raise NotFound() response = HttpResponse(data, content_type="application/yaml") response["Content-Disposition"] = ( "attachment; filename=job_%d.yaml" % self.get_object().id) return response except FileNotFoundError: raise NotFound()
def test_read_logs_compressed(tmpdir): with lzma.open(str(tmpdir / "output.yaml.xz"), "wb") as f_logs: f_logs.write("compressed\nor\nnot".encode("utf-8")) assert read_logs(str(tmpdir)) == "compressed\nor\nnot" # nosec assert not (tmpdir / "output.idx").exists() # nosec # Use the index assert read_logs(str(tmpdir), start=1) == "or\nnot" # nosec assert (tmpdir / "output.idx").exists() # nosec assert read_logs(str(tmpdir), start=1, end=2) == "or\n" # nosec assert read_logs(str(tmpdir), start=1, end=20) == "or\nnot" # nosec assert read_logs(str(tmpdir), start=2, end=2) == "" # nosec assert read_logs(str(tmpdir), start=1, end=0) == "" # nosec
def logs(self, job_id, start=0, end=None): """ Name ---- `scheduler.jobs.logs` (`job_id`, `start=0`, `end=None`) Description ----------- Return the logs for the given job Arguments --------- `job_id`: str Job id `start`: int Show only after the given line `end`: int Do not return after the fiven line Return value ------------ This function returns a tuple made of (job_finished, data). job_finished is True if and only if the job is finished. """ try: job = TestJob.get_by_job_number(job_id) except TestJob.DoesNotExist: raise xmlrpc.client.Fault(404, "Job '%s' was not found." % job_id) if not job.can_view(self.user): raise xmlrpc.client.Fault( 403, "Job '%s' not available to user '%s'." % (job_id, self.user)) job_finished = (job.state == TestJob.STATE_FINISHED) try: data = read_logs(job.output_dir, start, end) return (job_finished, xmlrpc.client.Binary(data.encode("utf-8"))) except OSError: return (job_finished, xmlrpc.client.Binary("[]".encode("utf-8")))
def test_read_logs_uncompressed(tmpdir): (tmpdir / "output.yaml").write_text("hello\nworld\nhow\nare\nyou", encoding="utf-8") assert read_logs(str(tmpdir)) == "hello\nworld\nhow\nare\nyou" # nosec assert not (tmpdir / "output.idx").exists() # nosec # If output.yaml exists, read_logs should use it with lzma.open(str(tmpdir / "output.yaml.xz"), "wb") as f_logs: f_logs.write("compressed".encode("utf-8")) assert read_logs(str(tmpdir)) == "hello\nworld\nhow\nare\nyou" # nosec assert not (tmpdir / "output.idx").exists() # nosec # Test the index assert read_logs(str(tmpdir), start=1) == "world\nhow\nare\nyou" # nosec assert (tmpdir / "output.idx").exists() # nosec assert read_logs(str(tmpdir), start=1, end=2) == "world\n" # nosec assert read_logs(str(tmpdir), start=1, end=3) == "world\nhow\n" # nosec assert read_logs(str(tmpdir), start=4, end=5) == "you" # nosec assert read_logs(str(tmpdir), start=5, end=50) == "" # nosec