charisma-cli 0.5.2__tar.gz → 0.5.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/PKG-INFO +1 -1
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/pyproject.toml +1 -1
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/src/charisma_cli/__init__.py +1 -1
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/src/charisma_cli/config.py +28 -3
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/src/charisma_cli/main.py +15 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/src/charisma_cli/uploader.py +76 -7
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_config.py +50 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_uploader.py +286 -3
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/.gitignore +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/README.md +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/src/charisma_cli/launch_url.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/src/charisma_cli/models.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/src/charisma_cli/parser.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/src/charisma_cli/read_client.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/src/charisma_cli/read_models.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/src/charisma_cli/retry.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/src/charisma_cli/subprocess_mgr.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/src/charisma_cli/watcher.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/__init__.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/conftest.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_bamboo_labels.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_cli.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_integration.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_launch_url.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_models.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_open_launch_payload.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_parser.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_read_client.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_read_client_attachments.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_read_client_integration.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_read_client_launches.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_read_client_pagination.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_read_client_results.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_read_client_single_result.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_retry.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_silent_mode.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_subprocess_mgr.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.3}/tests/test_watcher.py +0 -0
|
@@ -4,4 +4,4 @@
|
|
|
4
4
|
# is generated from pyproject.toml [project].version, so keeping this literal
|
|
5
5
|
# equal to pyproject guarantees `--version` reports the published version. The
|
|
6
6
|
# prepare-commit step owns the PATCH bump — do not hand-bump here.
|
|
7
|
-
__version__ = "0.5.
|
|
7
|
+
__version__ = "0.5.3"
|
|
@@ -5,6 +5,10 @@ from dataclasses import dataclass
|
|
|
5
5
|
|
|
6
6
|
import click
|
|
7
7
|
|
|
8
|
+
# Default drain/stop timeout in seconds when neither a flag nor an env var is
|
|
9
|
+
# given. Used by both drain() (stall window) and stop() (consumer-thread join).
|
|
10
|
+
_DEFAULT_DRAIN_TIMEOUT = 30
|
|
11
|
+
|
|
8
12
|
|
|
9
13
|
def _resolve_env(bare_name: str) -> str | None:
|
|
10
14
|
"""Resolve an env var by its bare name, falling back to the CHARISMA_ prefix.
|
|
@@ -57,7 +61,7 @@ class Config:
|
|
|
57
61
|
results: str | None = None,
|
|
58
62
|
silent: bool = False,
|
|
59
63
|
skip_too_big: bool = False,
|
|
60
|
-
drain_timeout: int =
|
|
64
|
+
drain_timeout: int | None = None,
|
|
61
65
|
component: str | None = None,
|
|
62
66
|
source_url: str | None = None,
|
|
63
67
|
build_plan_key: str | None = None,
|
|
@@ -76,7 +80,9 @@ class Config:
|
|
|
76
80
|
results: --results flag override
|
|
77
81
|
silent: --silent flag
|
|
78
82
|
skip_too_big: --skip-too-big flag
|
|
79
|
-
drain_timeout: drain
|
|
83
|
+
drain_timeout: --drain-timeout flag override (seconds). When None,
|
|
84
|
+
resolves from DRAIN_TIMEOUT / CHARISMA_DRAIN_TIMEOUT, else the
|
|
85
|
+
default. Governs both drain()'s stall window and stop()'s join.
|
|
80
86
|
component: --component flag override (component alias within the
|
|
81
87
|
project). Env: COMPONENT, fallback CHARISMA_COMPONENT.
|
|
82
88
|
source_url: --source-url flag override (CI/CD build link). Env:
|
|
@@ -121,6 +127,25 @@ class Config:
|
|
|
121
127
|
resolved_name = (_resolve_env("LAUNCH_NAME") or "").strip() or None
|
|
122
128
|
resolved_tags = [t.strip() for t in (_resolve_env("TAGS") or "").split(",") if t.strip()] or None
|
|
123
129
|
|
|
130
|
+
# Drain/stop timeout (seconds). Resolution order mirrors the other
|
|
131
|
+
# metadata fields: explicit flag (drain_timeout is not None) > env
|
|
132
|
+
# (DRAIN_TIMEOUT, CHARISMA_DRAIN_TIMEOUT via _resolve_env) > default.
|
|
133
|
+
# Governs both drain()'s stall window and stop()'s join, so a large/slow
|
|
134
|
+
# upload can be given more time without a code change. A non-positive or
|
|
135
|
+
# unparseable value falls back to the default.
|
|
136
|
+
resolved_drain_timeout = _DEFAULT_DRAIN_TIMEOUT
|
|
137
|
+
if drain_timeout is not None:
|
|
138
|
+
resolved_drain_timeout = drain_timeout
|
|
139
|
+
else:
|
|
140
|
+
env_drain = _resolve_env("DRAIN_TIMEOUT")
|
|
141
|
+
if env_drain:
|
|
142
|
+
try:
|
|
143
|
+
resolved_drain_timeout = int(env_drain)
|
|
144
|
+
except ValueError:
|
|
145
|
+
resolved_drain_timeout = _DEFAULT_DRAIN_TIMEOUT
|
|
146
|
+
if resolved_drain_timeout <= 0:
|
|
147
|
+
resolved_drain_timeout = _DEFAULT_DRAIN_TIMEOUT
|
|
148
|
+
|
|
124
149
|
# Merge Bamboo agent labels into resolved_tags (zero-config, opportunistic).
|
|
125
150
|
# bamboo_agentLabelString is a space-separated list of CI agent labels set
|
|
126
151
|
# automatically by Bamboo. Tokens are deduplicated against any explicit TAGS.
|
|
@@ -149,7 +174,7 @@ class Config:
|
|
|
149
174
|
results_dir=resolved_results,
|
|
150
175
|
silent=silent,
|
|
151
176
|
skip_too_big=skip_too_big,
|
|
152
|
-
drain_timeout=
|
|
177
|
+
drain_timeout=resolved_drain_timeout,
|
|
153
178
|
build_id=resolved_build_id,
|
|
154
179
|
commit_sha=_resolve_env("COMMIT_SHA"),
|
|
155
180
|
branch=_resolve_env("BRANCH"),
|
|
@@ -46,6 +46,7 @@ def _resolve_config(
|
|
|
46
46
|
build_id: str | None = None,
|
|
47
47
|
environment: str | None = None,
|
|
48
48
|
run_key: str | None = None,
|
|
49
|
+
drain_timeout: int | None = None,
|
|
49
50
|
) -> Config:
|
|
50
51
|
"""Resolve CLI config from flags + env vars. Re-raises UsageError on failure."""
|
|
51
52
|
return Config.from_env(
|
|
@@ -61,6 +62,7 @@ def _resolve_config(
|
|
|
61
62
|
build_id=build_id,
|
|
62
63
|
environment=environment,
|
|
63
64
|
run_key=run_key,
|
|
65
|
+
drain_timeout=drain_timeout,
|
|
64
66
|
)
|
|
65
67
|
|
|
66
68
|
|
|
@@ -163,6 +165,15 @@ def _common_options(func: Callable) -> Callable:
|
|
|
163
165
|
click.option("--project", envvar="CHARISMA_PROJECT_ID", required=True, help="Project alias."),
|
|
164
166
|
click.option("--silent", is_flag=True, default=False, help="Don't fail if upload errors occur."),
|
|
165
167
|
click.option("--skip-too-big", is_flag=True, default=False, help="Skip result files larger than 2MB."),
|
|
168
|
+
click.option(
|
|
169
|
+
"--drain-timeout",
|
|
170
|
+
envvar="CHARISMA_DRAIN_TIMEOUT",
|
|
171
|
+
type=int,
|
|
172
|
+
default=None,
|
|
173
|
+
help="Seconds to wait with no progress before giving up draining/stopping "
|
|
174
|
+
"the uploader (default 30). Raise for very large/slow uploads. Also "
|
|
175
|
+
"reads DRAIN_TIMEOUT / CHARISMA_DRAIN_TIMEOUT.",
|
|
176
|
+
),
|
|
166
177
|
click.option("--component", default=None, help="Component alias within the project."),
|
|
167
178
|
click.option("--source-url", default=None, help="CI/CD build link for this run."),
|
|
168
179
|
click.option("--build-plan-key", default=None, help="Explicit build plan key for resolution."),
|
|
@@ -198,6 +209,7 @@ def watch(
|
|
|
198
209
|
project: str,
|
|
199
210
|
silent: bool,
|
|
200
211
|
skip_too_big: bool,
|
|
212
|
+
drain_timeout: int | None,
|
|
201
213
|
component: str | None,
|
|
202
214
|
source_url: str | None,
|
|
203
215
|
build_plan_key: str | None,
|
|
@@ -223,6 +235,7 @@ def watch(
|
|
|
223
235
|
build_id,
|
|
224
236
|
environment,
|
|
225
237
|
run_key,
|
|
238
|
+
drain_timeout,
|
|
226
239
|
)
|
|
227
240
|
|
|
228
241
|
queue: PriorityQueue[FileEvent] = PriorityQueue()
|
|
@@ -316,6 +329,7 @@ def upload(
|
|
|
316
329
|
project: str,
|
|
317
330
|
silent: bool,
|
|
318
331
|
skip_too_big: bool,
|
|
332
|
+
drain_timeout: int | None,
|
|
319
333
|
component: str | None,
|
|
320
334
|
source_url: str | None,
|
|
321
335
|
build_plan_key: str | None,
|
|
@@ -340,6 +354,7 @@ def upload(
|
|
|
340
354
|
build_id,
|
|
341
355
|
environment,
|
|
342
356
|
run_key,
|
|
357
|
+
drain_timeout,
|
|
343
358
|
)
|
|
344
359
|
|
|
345
360
|
queue: PriorityQueue[FileEvent] = PriorityQueue()
|
|
@@ -4,6 +4,7 @@ import json
|
|
|
4
4
|
import logging
|
|
5
5
|
import mimetypes
|
|
6
6
|
import os
|
|
7
|
+
import sys
|
|
7
8
|
import threading
|
|
8
9
|
import time
|
|
9
10
|
from collections.abc import Callable
|
|
@@ -208,15 +209,65 @@ class Uploader:
|
|
|
208
209
|
self._thread = threading.Thread(target=self._run, daemon=True, name="charisma-uploader")
|
|
209
210
|
self._thread.start()
|
|
210
211
|
|
|
211
|
-
def stop(self) -> None:
|
|
212
|
-
"""Signal the uploader thread to exit
|
|
212
|
+
def stop(self, timeout: float | None = None) -> None:
|
|
213
|
+
"""Signal the uploader thread to exit, wait for it, then close the client.
|
|
214
|
+
|
|
215
|
+
Completion is owned by :meth:`drain`, which callers run FIRST: it waits
|
|
216
|
+
progress-based until every consumed result is sent (or the consumer
|
|
217
|
+
genuinely stalls). ``stop()`` is the teardown that follows — its only
|
|
218
|
+
jobs are to join the consumer thread and close the HTTP client.
|
|
219
|
+
|
|
220
|
+
The join timeout is CONFIGURABLE (``timeout`` arg → ``config.drain_timeout``
|
|
221
|
+
→ module default), replacing the old hardcoded 10s that was too short for
|
|
222
|
+
a large/slow final flush. If the worker is STILL alive after the join —
|
|
223
|
+
i.e. drain() already gave up and the consumer is wedged — a loud warning
|
|
224
|
+
is emitted on stderr (bypassing ``--silent``, which only raises the logger
|
|
225
|
+
to CRITICAL) naming the unsent counts, so a dropped upload is never
|
|
226
|
+
invisible. The client is then closed unconditionally: the worker is a
|
|
227
|
+
daemon thread and the process exits immediately after, so leaving the
|
|
228
|
+
client open would not let a wedged request complete — it only leaks the
|
|
229
|
+
connection. Normal runs never reach the warning because drain() has
|
|
230
|
+
already finished the work before stop() is called.
|
|
231
|
+
|
|
232
|
+
Args:
|
|
233
|
+
timeout: Seconds to wait for the consumer thread to exit. A missing or
|
|
234
|
+
non-positive value falls back to ``config.drain_timeout``.
|
|
235
|
+
"""
|
|
236
|
+
join_timeout = (
|
|
237
|
+
timeout if (timeout is not None and timeout > 0) else self._config.drain_timeout
|
|
238
|
+
)
|
|
213
239
|
self._stop_event.set()
|
|
214
240
|
if self._thread is not None:
|
|
215
|
-
self._thread.join(timeout=
|
|
241
|
+
self._thread.join(timeout=join_timeout)
|
|
242
|
+
if self._thread.is_alive():
|
|
243
|
+
self._warn_stop_incomplete(join_timeout)
|
|
216
244
|
if self._client is not None:
|
|
217
245
|
self._client.close()
|
|
218
246
|
self._client = None
|
|
219
247
|
|
|
248
|
+
def _warn_stop_incomplete(self, join_timeout: float) -> None:
|
|
249
|
+
"""Warn on stderr when the consumer is still alive after the join.
|
|
250
|
+
|
|
251
|
+
Reached only when drain() already gave up and the consumer is wedged.
|
|
252
|
+
Routed straight to stderr (not the ``charisma_cli`` logger, which
|
|
253
|
+
``--silent`` raises to CRITICAL) so a dropped/unfinished upload is never
|
|
254
|
+
swallowed. Names the counts and the ACTUAL join timeout used so the
|
|
255
|
+
operator can raise ``--drain-timeout`` or investigate connectivity.
|
|
256
|
+
|
|
257
|
+
Args:
|
|
258
|
+
join_timeout: The timeout (seconds) the join actually waited.
|
|
259
|
+
"""
|
|
260
|
+
print(
|
|
261
|
+
f"Warning: uploader did not finish within {join_timeout:g}s "
|
|
262
|
+
f"(results_sent={self._summary.results_sent}, "
|
|
263
|
+
f"results_failed={self._summary.results_failed}, "
|
|
264
|
+
f"attachments_sent={self._summary.attachments_sent}, "
|
|
265
|
+
f"attachments_skipped={self._summary.attachments_skipped}). "
|
|
266
|
+
"Some results/attachments may not have been uploaded; raise "
|
|
267
|
+
"--drain-timeout or check connectivity.",
|
|
268
|
+
file=sys.stderr,
|
|
269
|
+
)
|
|
270
|
+
|
|
220
271
|
def drain(self, timeout: float) -> bool:
|
|
221
272
|
"""Signal drain and wait until all consumed items are flushed/sent.
|
|
222
273
|
|
|
@@ -522,15 +573,33 @@ class Uploader:
|
|
|
522
573
|
attachment_suffix="stack-trace",
|
|
523
574
|
)
|
|
524
575
|
if result.labels:
|
|
525
|
-
#
|
|
526
|
-
#
|
|
576
|
+
# 'tag' is the one MULTI-VALUED Allure label: a result can carry
|
|
577
|
+
# several {"name":"tag","value":...} entries. Collapsing the whole
|
|
578
|
+
# label list into a {name: value} dict (as before) kept only the LAST
|
|
579
|
+
# tag — the per-character-tags production bug (launch efad6338-…),
|
|
580
|
+
# where a single surviving tag string was later promoted and rendered
|
|
581
|
+
# one character per chip. Collect EVERY tag value into the backend's
|
|
582
|
+
# dedicated flat `tags` list (used verbatim by TestResultInput.
|
|
583
|
+
# to_canonical), de-duplicated in encounter order to match the batch/
|
|
584
|
+
# Allure-ZIP path (allure_parser `all_tags`). The remaining
|
|
585
|
+
# single-valued labels (owner, layer, issue, tms, …) still flow
|
|
586
|
+
# through the labels dict for the backend's promote_labels.
|
|
527
587
|
labels_dict: dict[str, str] = {}
|
|
588
|
+
tags: list[str] = []
|
|
528
589
|
for label in result.labels:
|
|
529
590
|
name_key = label.get("name", "")
|
|
530
591
|
value_str = label.get("value", "")
|
|
531
|
-
if name_key
|
|
592
|
+
if not name_key or not value_str:
|
|
593
|
+
continue
|
|
594
|
+
if name_key == "tag":
|
|
595
|
+
if value_str not in tags:
|
|
596
|
+
tags.append(value_str)
|
|
597
|
+
else:
|
|
532
598
|
labels_dict[name_key] = value_str
|
|
533
|
-
|
|
599
|
+
if labels_dict:
|
|
600
|
+
payload["labels"] = labels_dict
|
|
601
|
+
if tags:
|
|
602
|
+
payload["tags"] = tags
|
|
534
603
|
if result.parameters:
|
|
535
604
|
# Convert Allure's [{name, value}] array to {name: value} dict
|
|
536
605
|
params_dict: dict[str, str] = {}
|
|
@@ -381,3 +381,53 @@ class TestConfigLaunchMetadata:
|
|
|
381
381
|
assert config.build_plan_key == "BI-KDLQA"
|
|
382
382
|
assert config.build_id == "22"
|
|
383
383
|
assert config.environment == "qa3"
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
class TestConfigDrainTimeout:
|
|
387
|
+
"""drain_timeout is configurable via flag + env (watch data-loss fix).
|
|
388
|
+
|
|
389
|
+
Before: Config.drain_timeout existed but was ALWAYS the default 30 — no CLI
|
|
390
|
+
flag or env var reached it, and Uploader.stop()'s join was a separate
|
|
391
|
+
hardcoded 10s. A large/slow upload could neither be given more drain time
|
|
392
|
+
nor kept from being abandoned. These tests pin the plumbing: an explicit
|
|
393
|
+
flag wins, an env var is honored, and the default is preserved.
|
|
394
|
+
"""
|
|
395
|
+
|
|
396
|
+
def test_drain_timeout_flag_overrides_default(
|
|
397
|
+
self, monkeypatch: pytest.MonkeyPatch
|
|
398
|
+
) -> None:
|
|
399
|
+
monkeypatch.setenv("CHARISMA_ENDPOINT", "https://api.example.com")
|
|
400
|
+
monkeypatch.setenv("CHARISMA_TOKEN", "tok")
|
|
401
|
+
monkeypatch.setenv("CHARISMA_PROJECT_ID", "proj")
|
|
402
|
+
monkeypatch.delenv("CHARISMA_DRAIN_TIMEOUT", raising=False)
|
|
403
|
+
monkeypatch.delenv("DRAIN_TIMEOUT", raising=False)
|
|
404
|
+
|
|
405
|
+
config = Config.from_env(drain_timeout=120)
|
|
406
|
+
|
|
407
|
+
assert config.drain_timeout == 120
|
|
408
|
+
|
|
409
|
+
def test_drain_timeout_resolves_from_env(
|
|
410
|
+
self, monkeypatch: pytest.MonkeyPatch
|
|
411
|
+
) -> None:
|
|
412
|
+
monkeypatch.setenv("CHARISMA_ENDPOINT", "https://api.example.com")
|
|
413
|
+
monkeypatch.setenv("CHARISMA_TOKEN", "tok")
|
|
414
|
+
monkeypatch.setenv("CHARISMA_PROJECT_ID", "proj")
|
|
415
|
+
monkeypatch.setenv("DRAIN_TIMEOUT", "90")
|
|
416
|
+
|
|
417
|
+
# No explicit flag → env var must be honored.
|
|
418
|
+
config = Config.from_env()
|
|
419
|
+
|
|
420
|
+
assert config.drain_timeout == 90
|
|
421
|
+
|
|
422
|
+
def test_drain_timeout_defaults_to_30(
|
|
423
|
+
self, monkeypatch: pytest.MonkeyPatch
|
|
424
|
+
) -> None:
|
|
425
|
+
monkeypatch.setenv("CHARISMA_ENDPOINT", "https://api.example.com")
|
|
426
|
+
monkeypatch.setenv("CHARISMA_TOKEN", "tok")
|
|
427
|
+
monkeypatch.setenv("CHARISMA_PROJECT_ID", "proj")
|
|
428
|
+
monkeypatch.delenv("CHARISMA_DRAIN_TIMEOUT", raising=False)
|
|
429
|
+
monkeypatch.delenv("DRAIN_TIMEOUT", raising=False)
|
|
430
|
+
|
|
431
|
+
config = Config.from_env()
|
|
432
|
+
|
|
433
|
+
assert config.drain_timeout == 30
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"""Unit tests for charisma_cli.uploader: batching, drain, priority ordering, error handling."""
|
|
2
2
|
|
|
3
|
+
import threading
|
|
3
4
|
import time
|
|
4
5
|
from pathlib import Path
|
|
5
6
|
from queue import PriorityQueue
|
|
@@ -199,8 +200,18 @@ class TestUploaderCircuitBreaker:
|
|
|
199
200
|
"""API unreachable triggers circuit breaker (disables further uploads)."""
|
|
200
201
|
|
|
201
202
|
@respx.mock
|
|
202
|
-
def test_connection_error_disables_uploads(self, tmp_path: Path) -> None:
|
|
203
|
-
"""Connection error triggers circuit breaker — no more requests.
|
|
203
|
+
def test_connection_error_disables_uploads(self, tmp_path: Path, monkeypatch) -> None:
|
|
204
|
+
"""Connection error triggers circuit breaker — no more requests.
|
|
205
|
+
|
|
206
|
+
Backoff is neutralized so the three ConnectError retries exhaust fast,
|
|
207
|
+
and the test POLLS for the disabled state instead of racing a fixed
|
|
208
|
+
sleep. (It previously relied on stop()'s old hardcoded 10s join to wait
|
|
209
|
+
out the ~7s of real backoff; stop()'s join is now the configured
|
|
210
|
+
drain_timeout, so the wait must be explicit rather than a side effect of
|
|
211
|
+
stop().)
|
|
212
|
+
"""
|
|
213
|
+
monkeypatch.setattr("charisma_cli.uploader.exponential_backoff", lambda attempt: 0.0)
|
|
214
|
+
|
|
204
215
|
config = _make_config()
|
|
205
216
|
queue: PriorityQueue = PriorityQueue()
|
|
206
217
|
|
|
@@ -216,7 +227,11 @@ class TestUploaderCircuitBreaker:
|
|
|
216
227
|
queue.put(FileEvent(path=filepath, category=FileCategory.RESULT))
|
|
217
228
|
|
|
218
229
|
uploader.start()
|
|
219
|
-
|
|
230
|
+
# Wait for the circuit breaker to engage (bounded) rather than a fixed
|
|
231
|
+
# sleep: the batch flushes at the 2s timeout, then the retries exhaust.
|
|
232
|
+
deadline = time.monotonic() + 10.0
|
|
233
|
+
while not uploader._disabled and time.monotonic() < deadline:
|
|
234
|
+
time.sleep(0.05)
|
|
220
235
|
uploader.stop()
|
|
221
236
|
|
|
222
237
|
# Circuit breaker should have engaged
|
|
@@ -1777,3 +1792,271 @@ class TestAttachmentTransportRouting:
|
|
|
1777
1792
|
# S3/domain policy.
|
|
1778
1793
|
presign_body = _json.loads(presign_route.calls[0].request.content)
|
|
1779
1794
|
assert presign_body["size"] == len(big_field.encode("utf-8"))
|
|
1795
|
+
|
|
1796
|
+
|
|
1797
|
+
class TestUploaderTagLabels:
|
|
1798
|
+
"""Multi-valued Allure labels (esp. 'tag') must survive the wire.
|
|
1799
|
+
|
|
1800
|
+
Regression for the per-character-tags production bug (launch efad6338-…):
|
|
1801
|
+
Allure writes each tag as its OWN label entry — {"name":"tag","value":"a"},
|
|
1802
|
+
{"name":"tag","value":"b"}. The uploader collapsed result.labels into a
|
|
1803
|
+
{name: value} dict (last-write-wins), so only the LAST tag survived; the
|
|
1804
|
+
backend then promoted that single string and the UI rendered per-character
|
|
1805
|
+
tags ("d e i l m s v").
|
|
1806
|
+
|
|
1807
|
+
Fix (parity with the batch/Allure-ZIP path, allure_parser all_tags): collect
|
|
1808
|
+
EVERY 'tag' value into the flat `tags` list the backend's TestResultInput
|
|
1809
|
+
uses verbatim (to_canonical: metadata_tags = self.tags if not None), and do
|
|
1810
|
+
NOT funnel tags through the single-valued labels dict.
|
|
1811
|
+
"""
|
|
1812
|
+
|
|
1813
|
+
def _write_json(self, path: Path, data: dict) -> None:
|
|
1814
|
+
import json
|
|
1815
|
+
path.write_text(json.dumps(data), encoding="utf-8")
|
|
1816
|
+
|
|
1817
|
+
@staticmethod
|
|
1818
|
+
def _sent_batch(route) -> list[dict]:
|
|
1819
|
+
"""Decode the JSON results batch from the recorded /results request."""
|
|
1820
|
+
import json
|
|
1821
|
+
request = route.calls.last.request
|
|
1822
|
+
body = json.loads(request.content.decode("utf-8"))
|
|
1823
|
+
return body["results"]
|
|
1824
|
+
|
|
1825
|
+
def _result_with_labels(self, tmp_path: Path, labels: list[dict]) -> Path:
|
|
1826
|
+
result_file = tmp_path / "tagged-result.json"
|
|
1827
|
+
self._write_json(result_file, {
|
|
1828
|
+
"uuid": "tagged-uuid",
|
|
1829
|
+
"historyId": "hist-tagged",
|
|
1830
|
+
"fullName": "tests.test_tagged",
|
|
1831
|
+
"status": "passed",
|
|
1832
|
+
"start": 1000,
|
|
1833
|
+
"stop": 2000,
|
|
1834
|
+
"labels": labels,
|
|
1835
|
+
"parameters": [],
|
|
1836
|
+
"attachments": [],
|
|
1837
|
+
})
|
|
1838
|
+
return result_file
|
|
1839
|
+
|
|
1840
|
+
def _upload_and_capture(self, config: Config, result_file: Path) -> dict:
|
|
1841
|
+
"""Drive one result through the uploader and return the sent payload."""
|
|
1842
|
+
queue: PriorityQueue = PriorityQueue()
|
|
1843
|
+
route = respx.post(
|
|
1844
|
+
"https://charisma.test/api/v1/launches/launch-1/results"
|
|
1845
|
+
).mock(return_value=httpx.Response(200, json={"accepted": 1}))
|
|
1846
|
+
uploader = Uploader(config, queue)
|
|
1847
|
+
uploader._launch_id = "launch-1"
|
|
1848
|
+
uploader.ensure_client()
|
|
1849
|
+
uploader._handle_result(result_file)
|
|
1850
|
+
uploader._flush_batch()
|
|
1851
|
+
assert route.called
|
|
1852
|
+
payload = self._sent_batch(route)[0]
|
|
1853
|
+
uploader._client.close()
|
|
1854
|
+
return payload
|
|
1855
|
+
|
|
1856
|
+
@respx.mock
|
|
1857
|
+
def test_multiple_tag_labels_all_survive_as_flat_tags(self, tmp_path: Path) -> None:
|
|
1858
|
+
"""Two 'tag' labels are sent as a flat tags list, not collapsed to one.
|
|
1859
|
+
|
|
1860
|
+
Fails on the buggy implementation: labels is a {name: value} dict so the
|
|
1861
|
+
two 'tag' entries collapse to {"tag": "smilevid"} and `tags` is absent.
|
|
1862
|
+
"""
|
|
1863
|
+
result_file = self._result_with_labels(tmp_path, [
|
|
1864
|
+
{"name": "tag", "value": "skip_on_china"},
|
|
1865
|
+
{"name": "tag", "value": "smilevid"},
|
|
1866
|
+
])
|
|
1867
|
+
payload = self._upload_and_capture(_make_config(), result_file)
|
|
1868
|
+
|
|
1869
|
+
assert payload.get("tags") == ["skip_on_china", "smilevid"], (
|
|
1870
|
+
"both tag values must reach the wire as a flat list; got "
|
|
1871
|
+
f"tags={payload.get('tags')!r}, labels={payload.get('labels')!r} "
|
|
1872
|
+
"(the per-character-tags bug collapses these to the last tag only)"
|
|
1873
|
+
)
|
|
1874
|
+
# Tags must NOT ride through the single-valued labels dict (that is what
|
|
1875
|
+
# caused last-write-wins); the dedicated tags field is the contract.
|
|
1876
|
+
assert "tag" not in (payload.get("labels") or {})
|
|
1877
|
+
|
|
1878
|
+
@respx.mock
|
|
1879
|
+
def test_single_tag_label_sent_as_one_element_list(self, tmp_path: Path) -> None:
|
|
1880
|
+
"""A single 'tag' label is sent as a one-element tags list (not a bare string)."""
|
|
1881
|
+
result_file = self._result_with_labels(tmp_path, [
|
|
1882
|
+
{"name": "tag", "value": "smilevid"},
|
|
1883
|
+
])
|
|
1884
|
+
payload = self._upload_and_capture(_make_config(), result_file)
|
|
1885
|
+
|
|
1886
|
+
assert payload.get("tags") == ["smilevid"]
|
|
1887
|
+
assert "tag" not in (payload.get("labels") or {})
|
|
1888
|
+
|
|
1889
|
+
@respx.mock
|
|
1890
|
+
def test_duplicate_tag_values_deduped_in_encounter_order(self, tmp_path: Path) -> None:
|
|
1891
|
+
"""Repeated identical tag values are de-duplicated, preserving first-seen order.
|
|
1892
|
+
|
|
1893
|
+
Matches the batch path (allure_parser: dict.fromkeys over raw 'tag'
|
|
1894
|
+
values) so streaming and ZIP ingestion agree on the tag set.
|
|
1895
|
+
"""
|
|
1896
|
+
result_file = self._result_with_labels(tmp_path, [
|
|
1897
|
+
{"name": "tag", "value": "smoke"},
|
|
1898
|
+
{"name": "tag", "value": "regression"},
|
|
1899
|
+
{"name": "tag", "value": "smoke"},
|
|
1900
|
+
])
|
|
1901
|
+
payload = self._upload_and_capture(_make_config(), result_file)
|
|
1902
|
+
|
|
1903
|
+
assert payload.get("tags") == ["smoke", "regression"]
|
|
1904
|
+
|
|
1905
|
+
@respx.mock
|
|
1906
|
+
def test_non_tag_labels_still_sent_in_labels_dict(self, tmp_path: Path) -> None:
|
|
1907
|
+
"""Non-tag labels (owner, layer) remain in the labels dict for promotion.
|
|
1908
|
+
|
|
1909
|
+
The fix must only redirect tags to the flat list; other promotable
|
|
1910
|
+
single-valued labels must keep flowing through labels so the backend's
|
|
1911
|
+
promote_labels still extracts owner/layer/etc.
|
|
1912
|
+
"""
|
|
1913
|
+
result_file = self._result_with_labels(tmp_path, [
|
|
1914
|
+
{"name": "tag", "value": "smoke"},
|
|
1915
|
+
{"name": "owner", "value": "qa-team"},
|
|
1916
|
+
{"name": "layer", "value": "e2e"},
|
|
1917
|
+
])
|
|
1918
|
+
payload = self._upload_and_capture(_make_config(), result_file)
|
|
1919
|
+
|
|
1920
|
+
assert payload.get("tags") == ["smoke"]
|
|
1921
|
+
labels = payload.get("labels") or {}
|
|
1922
|
+
assert labels.get("owner") == "qa-team"
|
|
1923
|
+
assert labels.get("layer") == "e2e"
|
|
1924
|
+
assert "tag" not in labels
|
|
1925
|
+
|
|
1926
|
+
@respx.mock
|
|
1927
|
+
def test_no_labels_sends_no_tags_field(self, tmp_path: Path) -> None:
|
|
1928
|
+
"""A result with no labels must not send an empty/garbage tags field."""
|
|
1929
|
+
result_file = self._result_with_labels(tmp_path, [])
|
|
1930
|
+
payload = self._upload_and_capture(_make_config(), result_file)
|
|
1931
|
+
|
|
1932
|
+
assert "tags" not in payload or payload.get("tags") in (None, [])
|
|
1933
|
+
|
|
1934
|
+
|
|
1935
|
+
class TestUploaderStopTimeoutAndWarning:
|
|
1936
|
+
"""stop() uses a configurable join timeout and warns loudly when wedged.
|
|
1937
|
+
|
|
1938
|
+
Production bug: Uploader.stop() joined the consumer thread with a HARDCODED
|
|
1939
|
+
10s timeout that was too short for a large/slow final flush. Completion is
|
|
1940
|
+
owned by drain() (progress-based), which callers run FIRST; stop() is only
|
|
1941
|
+
teardown — join + close client. The fix makes the join timeout configurable
|
|
1942
|
+
(--drain-timeout → config.drain_timeout) and, if the worker is STILL alive
|
|
1943
|
+
after the join (drain already gave up → genuinely wedged), warns on stderr
|
|
1944
|
+
bypassing --silent so a dropped upload is never invisible. The client is a
|
|
1945
|
+
daemon thread's and the process exits immediately after, so the client is
|
|
1946
|
+
closed unconditionally (leaving it open cannot save a wedged request).
|
|
1947
|
+
"""
|
|
1948
|
+
|
|
1949
|
+
@respx.mock
|
|
1950
|
+
def test_stop_uses_configured_timeout_for_join(self, tmp_path: Path, monkeypatch) -> None:
|
|
1951
|
+
"""stop() joins with config.drain_timeout, not a hardcoded 10s.
|
|
1952
|
+
|
|
1953
|
+
Captures the actual timeout passed to Thread.join to prove the value is
|
|
1954
|
+
driven by configuration (here drain_timeout=5 via _make_config), not the
|
|
1955
|
+
old hardcoded 10.0. A larger configured timeout must flow straight
|
|
1956
|
+
through so a big/slow upload can be given more time without a code edit.
|
|
1957
|
+
"""
|
|
1958
|
+
config = _make_config() # drain_timeout=5
|
|
1959
|
+
queue: PriorityQueue = PriorityQueue()
|
|
1960
|
+
|
|
1961
|
+
respx.post("https://charisma.test/api/v1/launches/launch-1/results").mock(
|
|
1962
|
+
return_value=httpx.Response(200, json={"accepted": 1})
|
|
1963
|
+
)
|
|
1964
|
+
|
|
1965
|
+
uploader = Uploader(config, queue)
|
|
1966
|
+
uploader._launch_id = "launch-1"
|
|
1967
|
+
uploader.start()
|
|
1968
|
+
|
|
1969
|
+
captured: dict[str, float | None] = {}
|
|
1970
|
+
real_join = uploader._thread.join
|
|
1971
|
+
|
|
1972
|
+
def _spy_join(timeout: float | None = None) -> None:
|
|
1973
|
+
captured["timeout"] = timeout
|
|
1974
|
+
real_join(timeout)
|
|
1975
|
+
|
|
1976
|
+
monkeypatch.setattr(uploader._thread, "join", _spy_join)
|
|
1977
|
+
uploader.stop()
|
|
1978
|
+
|
|
1979
|
+
assert captured.get("timeout") == 5, (
|
|
1980
|
+
f"stop() joined with timeout={captured.get('timeout')}; it must use "
|
|
1981
|
+
"config.drain_timeout (5), not the old hardcoded 10.0"
|
|
1982
|
+
)
|
|
1983
|
+
|
|
1984
|
+
def test_stop_warns_loudly_when_worker_still_alive_even_silent(
|
|
1985
|
+
self, tmp_path: Path, capsys
|
|
1986
|
+
) -> None:
|
|
1987
|
+
"""A wedged worker (still alive after join) triggers a loud stderr warning.
|
|
1988
|
+
|
|
1989
|
+
This is the reached-only-when-drain-gave-up path. Under --silent the
|
|
1990
|
+
logger is raised to CRITICAL, so the warning MUST go to stderr directly
|
|
1991
|
+
(not the logger) to stay visible. We also confirm the actual join
|
|
1992
|
+
timeout used is reported (not a stale config value).
|
|
1993
|
+
"""
|
|
1994
|
+
config = _make_config(silent=True) # logger is CRITICAL under silent
|
|
1995
|
+
queue: PriorityQueue = PriorityQueue()
|
|
1996
|
+
uploader = Uploader(config, queue)
|
|
1997
|
+
uploader._launch_id = "launch-1"
|
|
1998
|
+
uploader.configure_logging()
|
|
1999
|
+
uploader.ensure_client()
|
|
2000
|
+
|
|
2001
|
+
# A real thread that outlives the short stop timeout (a stand-in for a
|
|
2002
|
+
# wedged consumer that drain() already failed to finish).
|
|
2003
|
+
release = threading.Event()
|
|
2004
|
+
worker = threading.Thread(target=release.wait, daemon=True)
|
|
2005
|
+
worker.start()
|
|
2006
|
+
uploader._thread = worker
|
|
2007
|
+
|
|
2008
|
+
try:
|
|
2009
|
+
uploader.stop(timeout=0.2)
|
|
2010
|
+
err = capsys.readouterr().err
|
|
2011
|
+
# Loud warning present despite --silent, and names the real timeout.
|
|
2012
|
+
assert "did not finish within 0.2s" in err, err
|
|
2013
|
+
assert "raise --drain-timeout" in err
|
|
2014
|
+
# Teardown still closes the client (daemon worker + imminent exit:
|
|
2015
|
+
# keeping it open cannot rescue a wedged request).
|
|
2016
|
+
assert uploader._client is None
|
|
2017
|
+
finally:
|
|
2018
|
+
release.set()
|
|
2019
|
+
worker.join(timeout=1.0)
|
|
2020
|
+
|
|
2021
|
+
def test_stop_nonpositive_timeout_falls_back_to_config(self) -> None:
|
|
2022
|
+
"""stop(timeout<=0) must not degrade to an instant join(0) abandon.
|
|
2023
|
+
|
|
2024
|
+
A zero/negative explicit timeout falls back to config.drain_timeout so a
|
|
2025
|
+
stray 0 cannot reintroduce the instant-abandon behavior.
|
|
2026
|
+
"""
|
|
2027
|
+
import inspect
|
|
2028
|
+
|
|
2029
|
+
config = _make_config() # drain_timeout=5
|
|
2030
|
+
queue: PriorityQueue = PriorityQueue()
|
|
2031
|
+
uploader = Uploader(config, queue)
|
|
2032
|
+
uploader._launch_id = "launch-1"
|
|
2033
|
+
uploader.ensure_client()
|
|
2034
|
+
|
|
2035
|
+
captured: dict[str, float | None] = {}
|
|
2036
|
+
|
|
2037
|
+
class _SpyThread:
|
|
2038
|
+
def join(self, timeout: float | None = None) -> None:
|
|
2039
|
+
captured["timeout"] = timeout
|
|
2040
|
+
|
|
2041
|
+
def is_alive(self) -> bool:
|
|
2042
|
+
return False
|
|
2043
|
+
|
|
2044
|
+
uploader._thread = _SpyThread() # type: ignore[assignment]
|
|
2045
|
+
uploader.stop(timeout=0)
|
|
2046
|
+
|
|
2047
|
+
assert captured.get("timeout") == 5, (
|
|
2048
|
+
f"stop(timeout=0) joined with {captured.get('timeout')}; a non-positive "
|
|
2049
|
+
"timeout must fall back to config.drain_timeout (5), not join(0)"
|
|
2050
|
+
)
|
|
2051
|
+
# Keep inspect import meaningful for the sibling signature test below.
|
|
2052
|
+
assert "timeout" in inspect.signature(Uploader.stop).parameters
|
|
2053
|
+
|
|
2054
|
+
def test_stop_timeout_is_configurable_not_hardcoded(self) -> None:
|
|
2055
|
+
"""stop() accepts a timeout argument (no longer a hardcoded 10s)."""
|
|
2056
|
+
import inspect
|
|
2057
|
+
|
|
2058
|
+
sig = inspect.signature(Uploader.stop)
|
|
2059
|
+
assert "timeout" in sig.parameters, (
|
|
2060
|
+
"Uploader.stop must accept a configurable 'timeout' (the 10s join "
|
|
2061
|
+
"was hardcoded, cutting off large/slow uploads)"
|
|
2062
|
+
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|