File size: 2,419 Bytes
b199558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import gzip
import json
import os
import shutil
import subprocess
from pathlib import Path

import pytest

WORK = Path(os.environ["TB_WORK"])
BASH = os.environ.get("TB_BASH", "bash")


def load_report():
    path = WORK / "out" / "report.json"
    assert path.is_file(), "out/report.json was not created"
    return json.loads(path.read_text(encoding="utf-8"))


def test_report_has_exactly_the_documented_keys():
    report = load_report()
    assert sorted(report) == [
        "bytes_total",
        "p95_ms",
        "status_counts",
        "top_paths",
        "total_requests",
        "unique_paths",
    ]


def test_total_requests_counts_only_valid_lines():
    assert load_report()["total_requests"] == 36


def test_unique_paths_ignores_query_strings():
    assert load_report()["unique_paths"] == 5


def test_status_counts():
    assert load_report()["status_counts"] == {"200": 28, "304": 4, "404": 3, "500": 1}


def test_top_paths_order_and_tiebreak():
    assert load_report()["top_paths"] == [
        {"path": "/api/users", "count": 12},
        {"path": "/index.html", "count": 12},
        {"path": "/static/app.js", "count": 6},
    ]


def test_bytes_total_treats_dash_as_zero():
    assert load_report()["bytes_total"] == 14535


def test_p95_uses_nearest_rank():
    assert load_report()["p95_ms"] == 162


def test_malformed_line_numbers():
    path = WORK / "out" / "malformed.txt"
    assert path.is_file(), "out/malformed.txt was not created"
    numbers = [int(line) for line in path.read_text(encoding="utf-8").split()]
    assert numbers == [7, 19, 28, 37]


def test_gzip_roundtrip_is_byte_identical():
    original = (WORK / "logs" / "access.log").read_bytes()
    archive = WORK / "out" / "access.log.gz"
    assert archive.is_file(), "out/access.log.gz was not created"
    assert gzip.decompress(archive.read_bytes()) == original


def test_script_regenerates_identical_output():
    out = WORK / "out"
    before = (out / "report.json").read_bytes()
    before_bad = (out / "malformed.txt").read_bytes()
    shutil.rmtree(out)
    proc = subprocess.run(
        [BASH, "tools/report.sh"],
        cwd=str(WORK),
        capture_output=True,
        text=True,
    )
    assert proc.returncode == 0, "tools/report.sh failed:\n%s\n%s" % (proc.stdout, proc.stderr)
    assert (out / "report.json").read_bytes() == before
    assert (out / "malformed.txt").read_bytes() == before_bad