askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_object_logs.py · 9.3 KB · 314 lines
| 1 | import gzip |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | import object_correlation |
| 6 | import object_logs |
| 7 | import object_versions |
| 8 | |
| 9 | |
| 10 | def write_log(path, content): |
| 11 | path.parent.mkdir(parents=True, exist_ok=True) |
| 12 | path.write_text(content) |
| 13 | return path |
| 14 | |
| 15 | |
| 16 | def test_get_object_logs_returns_empty_when_log_file_is_missing(tmp_path): |
| 17 | logs = object_logs.get_object_logs("basics_counter", base_dir=tmp_path / "data") |
| 18 | |
| 19 | assert logs == [] |
| 20 | |
| 21 | |
| 22 | def test_get_object_logs_reads_tsv_entries(tmp_path): |
| 23 | write_log( |
| 24 | tmp_path / "data" / "logs" / "basics_counter" / "log.tsv", |
| 25 | "entry_id\ttimestamp\tlevel\tmessage\tmethod\n" |
| 26 | "a1\t2026-01-01T00:00:00\tINFO\tGET started\tGET\n" |
| 27 | "a2\t2026-01-01T00:00:01\tERROR\tboom\tGET\n", |
| 28 | ) |
| 29 | |
| 30 | logs = object_logs.get_object_logs("basics_counter", base_dir=tmp_path / "data") |
| 31 | |
| 32 | assert logs == [ |
| 33 | { |
| 34 | "entry_id": "a1", |
| 35 | "timestamp": "2026-01-01T00:00:00", |
| 36 | "level": "INFO", |
| 37 | "message": "GET started", |
| 38 | "method": "GET", |
| 39 | }, |
| 40 | { |
| 41 | "entry_id": "a2", |
| 42 | "timestamp": "2026-01-01T00:00:01", |
| 43 | "level": "ERROR", |
| 44 | "message": "boom", |
| 45 | "method": "GET", |
| 46 | }, |
| 47 | ] |
| 48 | |
| 49 | |
| 50 | def test_get_object_logs_filters_by_level_and_limit(tmp_path): |
| 51 | write_log( |
| 52 | tmp_path / "data" / "logs" / "basics_counter" / "log.tsv", |
| 53 | "entry_id\ttimestamp\tlevel\tmessage\n" |
| 54 | "a1\t2026-01-01T00:00:00\tINFO\tfirst\n" |
| 55 | "a2\t2026-01-01T00:00:01\tERROR\tsecond\n" |
| 56 | "a3\t2026-01-01T00:00:02\tERROR\tthird\n", |
| 57 | ) |
| 58 | |
| 59 | logs = object_logs.get_object_logs( |
| 60 | "basics_counter", |
| 61 | base_dir=tmp_path / "data", |
| 62 | level="ERROR", |
| 63 | limit=1, |
| 64 | ) |
| 65 | |
| 66 | assert logs == [ |
| 67 | { |
| 68 | "entry_id": "a2", |
| 69 | "timestamp": "2026-01-01T00:00:01", |
| 70 | "level": "ERROR", |
| 71 | "message": "second", |
| 72 | } |
| 73 | ] |
| 74 | |
| 75 | |
| 76 | def test_get_object_logs_reads_current_then_rotated_files(tmp_path): |
| 77 | log_dir = tmp_path / "data" / "logs" / "basics_counter" |
| 78 | write_log( |
| 79 | log_dir / "log.tsv", |
| 80 | "entry_id\ttimestamp\tlevel\tmessage\n" |
| 81 | "current\t2026-01-02T00:00:00\tINFO\tcurrent\n", |
| 82 | ) |
| 83 | write_log( |
| 84 | log_dir / "log-20260101-000000.tsv", |
| 85 | "entry_id\ttimestamp\tlevel\tmessage\n" |
| 86 | "old1\t2026-01-01T00:00:00\tINFO\told one\n", |
| 87 | ) |
| 88 | write_log( |
| 89 | log_dir / "log-20260101-010000.tsv", |
| 90 | "entry_id\ttimestamp\tlevel\tmessage\n" |
| 91 | "old2\t2026-01-01T01:00:00\tINFO\told two\n", |
| 92 | ) |
| 93 | |
| 94 | logs = object_logs.get_object_logs("basics_counter", base_dir=tmp_path / "data") |
| 95 | |
| 96 | assert [entry["entry_id"] for entry in logs] == ["current", "old1", "old2"] |
| 97 | |
| 98 | |
| 99 | def test_get_object_logs_reads_compressed_rotated_files(tmp_path): |
| 100 | log_dir = tmp_path / "data" / "logs" / "basics_counter" |
| 101 | write_log( |
| 102 | log_dir / "log.tsv", |
| 103 | "entry_id\ttimestamp\tlevel\tmessage\n" |
| 104 | "current\t2026-01-02T00:00:00\tINFO\tcurrent\n", |
| 105 | ) |
| 106 | log_dir.mkdir(parents=True, exist_ok=True) |
| 107 | with gzip.open(log_dir / "log-20260101-000000.tsv.gz", "wt", newline="") as f: |
| 108 | f.write( |
| 109 | "entry_id\ttimestamp\tlevel\tmessage\n" |
| 110 | "old1\t2026-01-01T00:00:00\tINFO\told one\n" |
| 111 | ) |
| 112 | |
| 113 | logs = object_logs.get_object_logs("basics_counter", base_dir=tmp_path / "data") |
| 114 | |
| 115 | assert [entry["entry_id"] for entry in logs] == ["current", "old1"] |
| 116 | |
| 117 | |
| 118 | def test_append_object_log_creates_tsv_entry(tmp_path): |
| 119 | entry = object_logs.append_object_log( |
| 120 | "basics_counter", |
| 121 | "DEBUG", |
| 122 | "GET completed successfully", |
| 123 | base_dir=tmp_path / "data", |
| 124 | method="GET", |
| 125 | status="success", |
| 126 | duration_ms=1.25, |
| 127 | ) |
| 128 | |
| 129 | logs = object_logs.get_object_logs("basics_counter", base_dir=tmp_path / "data") |
| 130 | |
| 131 | assert entry["entry_id"] |
| 132 | assert logs == [ |
| 133 | { |
| 134 | "entry_id": entry["entry_id"], |
| 135 | "correlation_id": "", |
| 136 | "timestamp": entry["timestamp"], |
| 137 | "level": "DEBUG", |
| 138 | "message": "GET completed successfully", |
| 139 | "method": "GET", |
| 140 | "status": "success", |
| 141 | "duration_ms": "1.25", |
| 142 | "error_type": "", |
| 143 | "error": "", |
| 144 | } |
| 145 | ] |
| 146 | |
| 147 | |
| 148 | def test_append_object_log_inherits_current_correlation_id(tmp_path): |
| 149 | correlation_id = "123e4567-e89b-42d3-a456-426614174000" |
| 150 | token = object_correlation.set_current_correlation_id(correlation_id) |
| 151 | try: |
| 152 | object_logs.append_object_log( |
| 153 | "basics_counter", |
| 154 | "INFO", |
| 155 | "inside object", |
| 156 | base_dir=tmp_path / "data", |
| 157 | ) |
| 158 | finally: |
| 159 | object_correlation.reset_current_correlation_id(token) |
| 160 | |
| 161 | logs = object_logs.get_object_logs("basics_counter", base_dir=tmp_path / "data") |
| 162 | |
| 163 | assert logs[0]["correlation_id"] == correlation_id |
| 164 | |
| 165 | |
| 166 | def test_append_object_log_extends_existing_header(tmp_path): |
| 167 | write_log( |
| 168 | tmp_path / "data" / "logs" / "basics_counter" / "log.tsv", |
| 169 | "entry_id\ttimestamp\tlevel\tmessage\n" |
| 170 | "a1\t2026-01-01T00:00:00\tINFO\told\n", |
| 171 | ) |
| 172 | |
| 173 | object_logs.append_object_log( |
| 174 | "basics_counter", |
| 175 | "ERROR", |
| 176 | "GET failed: boom", |
| 177 | base_dir=tmp_path / "data", |
| 178 | method="GET", |
| 179 | status="error", |
| 180 | error_type="RuntimeError", |
| 181 | error="boom", |
| 182 | ) |
| 183 | |
| 184 | logs = object_logs.get_object_logs("basics_counter", base_dir=tmp_path / "data") |
| 185 | |
| 186 | assert logs[0]["message"] == "old" |
| 187 | assert logs[0]["status"] == "" |
| 188 | assert logs[1]["method"] == "GET" |
| 189 | assert logs[1]["status"] == "error" |
| 190 | assert logs[1]["error_type"] == "RuntimeError" |
| 191 | |
| 192 | |
| 193 | def test_append_object_log_rotates_and_compresses_large_current_log(tmp_path): |
| 194 | log_dir = tmp_path / "data" / "logs" / "basics_counter" |
| 195 | write_log( |
| 196 | log_dir / "log.tsv", |
| 197 | "entry_id\ttimestamp\tlevel\tmessage\n" |
| 198 | "old\t2026-01-01T00:00:00\tINFO\t" + ("old message " * 80) + "\n", |
| 199 | ) |
| 200 | |
| 201 | entry = object_logs.append_object_log( |
| 202 | "basics_counter", |
| 203 | "INFO", |
| 204 | "new message", |
| 205 | base_dir=tmp_path / "data", |
| 206 | max_log_bytes=128, |
| 207 | ) |
| 208 | |
| 209 | compressed_logs = list(log_dir.glob("log-*.tsv.gz")) |
| 210 | assert len(compressed_logs) == 1 |
| 211 | with gzip.open(compressed_logs[0], "rt", newline="") as f: |
| 212 | compressed_text = f.read() |
| 213 | assert "old message" in compressed_text |
| 214 | |
| 215 | logs = object_logs.get_object_logs("basics_counter", base_dir=tmp_path / "data") |
| 216 | assert [row["entry_id"] for row in logs] == [entry["entry_id"], "old"] |
| 217 | assert logs[0]["message"] == "new message" |
| 218 | |
| 219 | |
| 220 | def test_append_object_log_can_rotate_without_compression(tmp_path): |
| 221 | log_dir = tmp_path / "data" / "logs" / "basics_counter" |
| 222 | write_log( |
| 223 | log_dir / "log.tsv", |
| 224 | "entry_id\ttimestamp\tlevel\tmessage\n" |
| 225 | "old\t2026-01-01T00:00:00\tINFO\t" + ("old message " * 80) + "\n", |
| 226 | ) |
| 227 | |
| 228 | object_logs.append_object_log( |
| 229 | "basics_counter", |
| 230 | "INFO", |
| 231 | "new message", |
| 232 | base_dir=tmp_path / "data", |
| 233 | max_log_bytes=128, |
| 234 | compress_rotated=False, |
| 235 | ) |
| 236 | |
| 237 | assert len(list(log_dir.glob("log-*.tsv"))) == 1 |
| 238 | assert not list(log_dir.glob("log-*.tsv.gz")) |
| 239 | |
| 240 | |
| 241 | def test_append_object_log_keeps_only_configured_rotated_files(tmp_path): |
| 242 | log_dir = tmp_path / "data" / "logs" / "basics_counter" |
| 243 | for index in range(3): |
| 244 | write_log( |
| 245 | log_dir / f"log-20260101-00000{index}.tsv", |
| 246 | "entry_id\ttimestamp\tlevel\tmessage\n" |
| 247 | f"old{index}\t2026-01-01T00:00:0{index}\tINFO\told {index}\n", |
| 248 | ) |
| 249 | write_log( |
| 250 | log_dir / "log.tsv", |
| 251 | "entry_id\ttimestamp\tlevel\tmessage\n" |
| 252 | "current\t2026-01-01T00:01:00\tINFO\t" + ("current message " * 80) + "\n", |
| 253 | ) |
| 254 | |
| 255 | object_logs.append_object_log( |
| 256 | "basics_counter", |
| 257 | "INFO", |
| 258 | "new message", |
| 259 | base_dir=tmp_path / "data", |
| 260 | max_log_bytes=128, |
| 261 | keep_rotated=2, |
| 262 | ) |
| 263 | |
| 264 | rotated_names = [path.name for path in object_logs._rotated_log_files(log_dir)] |
| 265 | assert len(rotated_names) == 2 |
| 266 | assert "log-20260101-000000.tsv" not in rotated_names |
| 267 | assert "log.tsv" not in rotated_names |
| 268 | |
| 269 | |
| 270 | def test_object_logger_writes_object_owned_logs(tmp_path): |
| 271 | logger = object_logs.ObjectLogger("basics_counter", base_dir=tmp_path / "data") |
| 272 | |
| 273 | logger.info("Counter incremented", user_id="user-1", count=2) |
| 274 | logger.error("Counter failed", error_code="E_COUNTER") |
| 275 | |
| 276 | logs = logger.get_logs() |
| 277 | |
| 278 | assert [entry["level"] for entry in logs] == ["INFO", "ERROR"] |
| 279 | assert logs[0]["message"] == "Counter incremented" |
| 280 | assert logs[0]["user_id"] == "user-1" |
| 281 | assert logs[0]["count"] == "2" |
| 282 | assert logs[1]["error_code"] == "E_COUNTER" |
| 283 | |
| 284 | |
| 285 | def test_object_logger_filters_logs(tmp_path): |
| 286 | logger = object_logs.ObjectLogger("basics_counter", base_dir=tmp_path / "data") |
| 287 | |
| 288 | logger.debug("first") |
| 289 | logger.error("second", status="error") |
| 290 | logger.error("third", status="error") |
| 291 | |
| 292 | logs = logger.get_logs(level="ERROR", limit=1, status="error") |
| 293 | |
| 294 | assert len(logs) == 1 |
| 295 | assert logs[0]["message"] == "second" |
| 296 | |
| 297 | |
| 298 | @pytest.mark.parametrize( |
| 299 | "object_id", |
| 300 | [ |
| 301 | "", |
| 302 | "../outside", |
| 303 | "basics/counter", |
| 304 | "basics.counter", |
| 305 | "object id", |
| 306 | "object@station", |
| 307 | "object;drop", |
| 308 | "object\x00id", |
| 309 | "a" * 65, |
| 310 | ], |
| 311 | ) |
| 312 | def test_get_object_logs_rejects_invalid_object_ids(tmp_path, object_id): |
| 313 | with pytest.raises(object_versions.InvalidObjectIdError): |
| 314 | object_logs.get_object_logs(object_id, base_dir=tmp_path / "data") |