2017-06-15 19:17:54 +03:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
# You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import os
|
|
|
|
|
2018-09-27 18:31:02 +03:00
|
|
|
from tests import ArgsMock
|
2017-06-15 19:17:54 +03:00
|
|
|
import tlscanary.runlog as rl
|
|
|
|
|
|
|
|
|
2018-09-27 18:31:02 +03:00
|
|
|
def test_runlog_db_instance(tmpdir):
|
2017-06-15 19:17:54 +03:00
|
|
|
"""RunLogDB can list log handles"""
|
|
|
|
|
2018-09-27 18:31:02 +03:00
|
|
|
db = rl.RunLogDB(ArgsMock(workdir=tmpdir))
|
2017-06-15 19:17:54 +03:00
|
|
|
handle_list = db.list()
|
|
|
|
dir_list = db.list_logs()
|
2018-09-27 18:31:02 +03:00
|
|
|
assert type(handle_list) is list, "handle listing is a python list"
|
|
|
|
assert type(dir_list) is list, "dir listing is a python list"
|
|
|
|
assert len(handle_list) == 0, "empty db yields empty handle list"
|
|
|
|
assert len(dir_list) == 0, "empty db yields empty dir list"
|
2017-06-15 19:17:54 +03:00
|
|
|
|
|
|
|
|
2018-09-27 18:31:02 +03:00
|
|
|
def test_runlog_db_file_handling(tmpdir):
|
2017-06-15 19:17:54 +03:00
|
|
|
"""RunLogDB associates handles and directories"""
|
|
|
|
|
2018-09-27 18:31:02 +03:00
|
|
|
db = rl.RunLogDB(ArgsMock(workdir=tmpdir))
|
2017-06-15 19:17:54 +03:00
|
|
|
|
|
|
|
now = datetime.datetime.utcnow().strftime("%Y-%m-%dZ%H-%M-%S")
|
|
|
|
dir_name = os.path.abspath(db.handle_to_dir_name(now))
|
2018-09-27 18:31:02 +03:00
|
|
|
assert dir_name.startswith(str(tmpdir)), "log files are stored in the right location"
|
2017-06-15 19:17:54 +03:00
|
|
|
handle_name = db.dir_name_to_handle(dir_name)
|
2018-09-27 18:31:02 +03:00
|
|
|
assert now == handle_name, "converting between handles and file names works"
|
2017-06-15 19:17:54 +03:00
|
|
|
|
|
|
|
|
2018-09-27 18:31:02 +03:00
|
|
|
def test_runlog_db_rw(tmpdir):
|
2017-07-14 12:36:36 +03:00
|
|
|
"""RunLogDB can read and write"""
|
2017-06-15 19:17:54 +03:00
|
|
|
|
2018-09-27 18:31:02 +03:00
|
|
|
db = rl.RunLogDB(ArgsMock(workdir=tmpdir))
|
2017-06-15 19:17:54 +03:00
|
|
|
|
|
|
|
in_db_before = db.list()
|
|
|
|
now = datetime.datetime.utcnow().strftime("%Y-%m-%dZ%H-%M-%S")
|
2018-09-27 18:31:02 +03:00
|
|
|
assert now not in in_db_before, "database is not cluttered"
|
2017-06-15 19:17:54 +03:00
|
|
|
test_data = "hello,test"
|
|
|
|
db.write(now, "test", test_data)
|
|
|
|
in_db_after = db.list()
|
2018-09-27 18:31:02 +03:00
|
|
|
assert now in in_db_after, "written log appears in db listing"
|
2017-06-15 19:17:54 +03:00
|
|
|
read_test_data = db.read(now, "test")
|
2018-09-27 18:31:02 +03:00
|
|
|
assert test_data == read_test_data, "read data is same as data"
|
2017-06-15 19:17:54 +03:00
|
|
|
|
|
|
|
|
2018-09-27 18:31:02 +03:00
|
|
|
def test_runlog_rw(tmpdir):
|
2017-06-15 19:17:54 +03:00
|
|
|
"""RunLog objects can write and read"""
|
|
|
|
|
2018-09-27 18:31:02 +03:00
|
|
|
db = rl.RunLogDB(ArgsMock(workdir=tmpdir))
|
2017-06-15 19:17:54 +03:00
|
|
|
now = datetime.datetime.utcnow().strftime("%Y-%m-%dZ%H-%M-%S")
|
|
|
|
log = rl.RunLog(now, "w", db)
|
|
|
|
|
2017-08-09 19:29:46 +03:00
|
|
|
# Write something to log using the with statement
|
|
|
|
with log:
|
|
|
|
log.log({"foo": 5})
|
|
|
|
log.update_meta({"just": "testing"})
|
2018-09-27 18:31:02 +03:00
|
|
|
assert log.is_running, "log is running in with"
|
|
|
|
assert not log.is_running, "log is not running after with"
|
2017-08-09 19:29:46 +03:00
|
|
|
|
|
|
|
# Write something to log. Previous content will be overwritten
|
2017-06-15 19:17:54 +03:00
|
|
|
log.start(meta={"first_meta": "one"})
|
|
|
|
log.log([{"foo": 1}, {"foo": 2}])
|
|
|
|
log.log({"foo": 3})
|
2018-09-27 18:31:02 +03:00
|
|
|
assert not log.has_finished(), "log is not marked `finished` while logging"
|
2017-06-15 19:17:54 +03:00
|
|
|
log.stop(meta={"last_meta": "two"})
|
2018-09-27 18:31:02 +03:00
|
|
|
assert log.has_finished(), "log is marked `finished` after logging"
|
2017-06-15 19:17:54 +03:00
|
|
|
|
|
|
|
test_log_dir = db.handle_to_dir_name(now)
|
2018-09-27 18:31:02 +03:00
|
|
|
assert os.path.isdir(test_log_dir), "log directory is created"
|
2017-06-15 19:17:54 +03:00
|
|
|
|
2018-09-27 18:31:02 +03:00
|
|
|
assert len(os.listdir(test_log_dir)) == 2, "log and meta files are written to disk"
|
2017-06-15 19:17:54 +03:00
|
|
|
|
|
|
|
# Read from log
|
|
|
|
log = rl.RunLog(now, "r", db)
|
2018-09-27 18:31:02 +03:00
|
|
|
assert log.has_finished(), "completed log is marked as `finished`"
|
2017-06-15 19:17:54 +03:00
|
|
|
meta = log.get_meta()
|
|
|
|
# Metadata always has "format_revision", "log_lines" and "run_completed" keys
|
2018-09-27 18:31:02 +03:00
|
|
|
assert len(list(meta.keys())) == 5, "log has correct number of meta data"
|
2017-06-15 19:17:54 +03:00
|
|
|
log_lines = [line for line in log]
|
2018-09-27 18:31:02 +03:00
|
|
|
assert len(log_lines) == 3, "log has correct number of lines"
|
2017-06-15 19:17:54 +03:00
|
|
|
|
|
|
|
|
2018-09-27 18:31:02 +03:00
|
|
|
def test_runlog_integration(tmpdir):
|
2017-06-15 19:17:54 +03:00
|
|
|
"""RunLog system works as intended"""
|
|
|
|
|
2018-09-27 18:31:02 +03:00
|
|
|
db = rl.RunLogDB(ArgsMock(workdir=tmpdir))
|
2017-06-15 19:17:54 +03:00
|
|
|
log_handles_before = db.list()
|
|
|
|
|
|
|
|
start_meta = {"begin": True}
|
|
|
|
stop_meta = {"end": True}
|
|
|
|
combined_meta = start_meta
|
|
|
|
combined_meta.update(stop_meta)
|
|
|
|
log_lines = [{"foo": 1}, {"foo": 2}, {"foo": 3, "nolog": True}]
|
|
|
|
log = db.new_log()
|
|
|
|
log.start(meta=start_meta, log_filter=lambda x: None if "nolog" in x else x)
|
|
|
|
log.log(log_lines)
|
|
|
|
log.stop(meta=stop_meta)
|
|
|
|
|
|
|
|
log_handles_after = db.list()
|
2018-09-27 18:31:02 +03:00
|
|
|
assert len(log_handles_after) == len(log_handles_before) + 1, "new log is listed"
|
2017-06-15 19:17:54 +03:00
|
|
|
log = db.read_log(log_handles_after[-1])
|
|
|
|
read_meta = log.get_meta()
|
2018-09-27 18:31:02 +03:00
|
|
|
assert "log_lines" in read_meta and read_meta["log_lines"] == 2, "has right number of lines in metadata"
|
2017-06-15 19:17:54 +03:00
|
|
|
has_meta = True
|
|
|
|
for k in combined_meta:
|
|
|
|
if k not in read_meta or read_meta[k] != combined_meta[k]:
|
|
|
|
has_meta = False
|
|
|
|
break
|
2018-09-27 18:31:02 +03:00
|
|
|
assert has_meta, "log metadata is correct"
|
2017-06-15 19:17:54 +03:00
|
|
|
read_lines = [line for line in log]
|
2018-09-27 18:31:02 +03:00
|
|
|
assert len(read_lines) == 2, "log has correct number of lines"
|
|
|
|
assert read_lines == log_lines[:2], "log lines have correct content"
|