aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2021-03-09 15:16:05 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2021-03-09 15:16:05 -0500
commit796ada809178994ec4b05c57f15e8c9d80469b42 (patch)
treef584c5edb4d9d46d49f5e92673759fb28930cc4b
parente785bb4f6d1e83a93f92ac205c50fb6f08f1693b (diff)
downloadlog_raider-796ada809178994ec4b05c57f15e8c9d80469b42.tar.gz
Add test and data
-rw-r--r--log_raider/tests/__init__.py0
-rw-r--r--log_raider/tests/data/jenkins.log45
-rw-r--r--log_raider/tests/test_jenkins.py116
3 files changed, 161 insertions, 0 deletions
diff --git a/log_raider/tests/__init__.py b/log_raider/tests/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/log_raider/tests/__init__.py
diff --git a/log_raider/tests/data/jenkins.log b/log_raider/tests/data/jenkins.log
new file mode 100644
index 0000000..8adb3a7
--- /dev/null
+++ b/log_raider/tests/data/jenkins.log
@@ -0,0 +1,45 @@
+Started by user Test
+Running in Durability level: MAX_SURVIVABILITY
+[Pipeline] Start of Pipeline
+[Pipeline] node
+Running on testing on test.local in /home/jenkins/workspace/sandbox/test
+[Pipeline] {
+[Pipeline] stage
+[Pipeline] { (hello)
+[Pipeline] sh
+[0] Hello, world!
+[1] Hello, world!
+[2] Hello, world!
+[3] Hello, world!
+[4] Hello, world!
+[5] Hello, world!
+[6] Hello, world!
+[7] Hello, world!
+[8] Hello, world!
+[9] Hello, world!
+[10] Hello, world!
+[11] Hello, world!
+[12] Hello, world!
+[13] Hello, world!
+[14] Hello, world!
+[15] Hello, world!
+[16] Hello, world!
+[17] Hello, world!
+[18] Hello, world!
+[19] Hello, world!
+[20] Hello, world!
+[21] Hello, world!
+[22] Hello, world!
+[23] Hello, world!
+[24] Hello, world!
+[25] Hello, world!
+[26] Hello, world!
+[27] Hello, world!
+[28] Hello, world!
+[29] Hello, world!
+[Pipeline] }
+[Pipeline] // stage
+[Pipeline] }
+[Pipeline] // node
+[Pipeline] End of Pipeline
+Finished: SUCCESS \ No newline at end of file
diff --git a/log_raider/tests/test_jenkins.py b/log_raider/tests/test_jenkins.py
new file mode 100644
index 0000000..e388637
--- /dev/null
+++ b/log_raider/tests/test_jenkins.py
@@ -0,0 +1,116 @@
+import os
+import pytest
+from .. import jenkins
+
+DATADIR = os.path.join(os.path.dirname(__file__), "data")
+INPUT_FILE = os.path.join(DATADIR, "jenkins.log")
+
+
+@pytest.fixture(scope="module")
+def records():
+ return jenkins.parse_log(INPUT_FILE)
+
+
+@pytest.fixture(scope="module")
+def master_log(records):
+ return log_combine(records, "type", "masterLog")
+
+
+@pytest.fixture(scope="module")
+def hello_stage(records):
+ return log_search(records, "name", "hello")
+
+
+def log_search(data, key, by_value=""):
+ result = list()
+ for record in data:
+ for k, v in record.items():
+ if not by_value:
+ v = ""
+ if key == k and v == by_value:
+ result.append(record)
+ return result
+
+
+def log_combine(data, key, key_value):
+ result = ""
+ for record in data:
+ for k, v in record.items():
+ if not key_value:
+ v = ""
+ if key == k and v == key_value:
+ result += record["data"]
+ return result
+
+
+def test_pipeline_specification():
+ assert jenkins.PIPELINE_MARKER == "[Pipeline]"
+ assert jenkins.PIPELINE_BLOCK_START == '{'
+ assert jenkins.PIPELINE_BLOCK_END == '}'
+ assert jenkins.PIPELINE_BLOCK_START_NOTE == '('
+ assert jenkins.PIPELINE_BLOCK_START_NOTE_END == ')'
+ assert jenkins.PIPELINE_BLOCK_END_NOTE == "//"
+
+
+@pytest.mark.parametrize("test_input,token,expected,test_args", [
+ # consume_token(s, token, l_trunc=0, r_trunc=0, strip_leading=True)
+ ("^See you later alligator^", "^", "See you later alligator", [0, 1, True]),
+ ("### In a while crocodile", "###", "In a while crocodile", [0, 0, True]),
+ ("Bye bye butterfly", "Bye bye ", "utter", [1, 3, False]),
+ ("[Pipeline] value", "[Pipeline]", "value", [0, 0, True]),
+ ("[Pipeline] { (name)", "{", "(name)", [0, 0, True]),
+ ("[Pipeline] { (name)", "(", "name", [0, 1, True]),
+])
+def test_consume_token(test_input, token, expected, test_args):
+ assert jenkins.consume_token(test_input, token, *test_args) == expected
+
+
+def test_parse_log_empty_file():
+ assert jenkins.parse_log(os.devnull) == list()
+
+
+def test_parse_log_path_failure():
+ with pytest.raises(FileNotFoundError):
+ assert jenkins.parse_log("1234unlikely_to_exist4321.log")
+
+
+def test_parse_log_master_log_keys(records):
+ log = log_search(records, "type", "masterLog")
+ assert len(log[0].keys()) == len(["name", "type", "depth", "line", "data"])
+
+
+def test_parse_log_master_log_genesis(records):
+ log = log_search(records, "type", "masterLog")
+ assert log
+ assert log[0]["name"] == "master"
+ assert log[0]["type"] == "masterLog"
+ assert log[0]["depth"] == 0 # First record, lowest depth
+ assert log[0]["line"] == 0
+ assert log[0]["data"]
+
+
+def test_parse_log_master_log_complete(master_log):
+ """Verify the entire master log is present
+ """
+ assert master_log.startswith("Started by")
+ assert master_log.endswith("SUCCESS\n")
+
+
+def test_parse_log_hello_stage_length(hello_stage):
+ assert len(hello_stage) == 2
+
+
+def test_parse_log_hello_stage_types(hello_stage):
+ assert hello_stage[0]["type"] == "stage"
+ assert hello_stage[1]["type"] == "sh"
+
+
+def test_parse_log_hello_stage_depths(hello_stage):
+ assert len(hello_stage) == 2
+ assert hello_stage[0]["depth"] == 2
+ assert hello_stage[1]["depth"] == 2
+
+
+def test_parse_log_hello_contents(hello_stage):
+ assert len(hello_stage[1]["data"].splitlines()) == 30
+