charisma-cli 0.5.2__tar.gz → 0.5.4__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.4}/PKG-INFO +1 -1
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/pyproject.toml +1 -1
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/src/charisma_cli/__init__.py +1 -1
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/src/charisma_cli/config.py +28 -3
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/src/charisma_cli/main.py +15 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/src/charisma_cli/models.py +3 -1
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/src/charisma_cli/uploader.py +124 -13
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_config.py +50 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_models.py +22 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_uploader.py +494 -3
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/.gitignore +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/README.md +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/src/charisma_cli/launch_url.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/src/charisma_cli/parser.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/src/charisma_cli/read_client.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/src/charisma_cli/read_models.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/src/charisma_cli/retry.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/src/charisma_cli/subprocess_mgr.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/src/charisma_cli/watcher.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/__init__.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/conftest.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_bamboo_labels.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_cli.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_integration.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_launch_url.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_open_launch_payload.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_parser.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_read_client.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_read_client_attachments.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_read_client_integration.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_read_client_launches.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_read_client_pagination.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_read_client_results.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_read_client_single_result.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_retry.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_silent_mode.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/tests/test_subprocess_mgr.py +0 -0
- {charisma_cli-0.5.2 → charisma_cli-0.5.4}/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.4"
|
|
@@ -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()
|
|
@@ -133,7 +133,9 @@ class AttachmentRef:
|
|
|
133
133
|
return None
|
|
134
134
|
return cls(
|
|
135
135
|
source=source,
|
|
136
|
-
name
|
|
136
|
+
# An empty/blank/null Allure name must not become a blank label:
|
|
137
|
+
# fall back to the source filename.
|
|
138
|
+
name=(attachment.get("name") or "").strip() or source,
|
|
137
139
|
mime_type=attachment.get("type", "application/octet-stream"),
|
|
138
140
|
result_uuid=result_uuid,
|
|
139
141
|
)
|
|
@@ -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
|
|
@@ -82,6 +83,10 @@ def _resolve_apigw_safe_bytes() -> int:
|
|
|
82
83
|
# presign-signed Content-Type and the PUT Content-Type match byte-for-byte.
|
|
83
84
|
_SPILL_CONTENT_TYPE = "text/plain; charset=utf-8"
|
|
84
85
|
|
|
86
|
+
# The backend rejects a displayName longer than this (400). Truncate the Allure
|
|
87
|
+
# label client-side so an overlong name never costs the attachment itself.
|
|
88
|
+
_MAX_DISPLAY_NAME_LEN = 255
|
|
89
|
+
|
|
85
90
|
# Sentinel expectedTests for CLI launches: the total count is unknown at open
|
|
86
91
|
# time (directory-watch streaming), so we send the backend maximum. This keeps
|
|
87
92
|
# the launch in 'receiving' across all appends; close_launch() finalizes it.
|
|
@@ -129,6 +134,10 @@ class Uploader:
|
|
|
129
134
|
# Allure's own structure: results own attachments and have a uuid;
|
|
130
135
|
# containers reference their children by uuid).
|
|
131
136
|
self._attachment_result_map: dict[str, str] = {}
|
|
137
|
+
# Map source filename → Allure attachment name (the human label such as
|
|
138
|
+
# "actual image"). Sent as ``displayName`` so listings are not the bare
|
|
139
|
+
# ``<uuid>-attachment.png`` filename. Written beside the result map.
|
|
140
|
+
self._attachment_name_map: dict[str, str] = {}
|
|
132
141
|
# Map Allure result uuid → historyId. Attachments reference their owning
|
|
133
142
|
# result by Allure uuid; the backend keys results by historyId, so the
|
|
134
143
|
# uuid must be translated to its historyId. Populated as result files are
|
|
@@ -208,15 +217,65 @@ class Uploader:
|
|
|
208
217
|
self._thread = threading.Thread(target=self._run, daemon=True, name="charisma-uploader")
|
|
209
218
|
self._thread.start()
|
|
210
219
|
|
|
211
|
-
def stop(self) -> None:
|
|
212
|
-
"""Signal the uploader thread to exit
|
|
220
|
+
def stop(self, timeout: float | None = None) -> None:
|
|
221
|
+
"""Signal the uploader thread to exit, wait for it, then close the client.
|
|
222
|
+
|
|
223
|
+
Completion is owned by :meth:`drain`, which callers run FIRST: it waits
|
|
224
|
+
progress-based until every consumed result is sent (or the consumer
|
|
225
|
+
genuinely stalls). ``stop()`` is the teardown that follows — its only
|
|
226
|
+
jobs are to join the consumer thread and close the HTTP client.
|
|
227
|
+
|
|
228
|
+
The join timeout is CONFIGURABLE (``timeout`` arg → ``config.drain_timeout``
|
|
229
|
+
→ module default), replacing the old hardcoded 10s that was too short for
|
|
230
|
+
a large/slow final flush. If the worker is STILL alive after the join —
|
|
231
|
+
i.e. drain() already gave up and the consumer is wedged — a loud warning
|
|
232
|
+
is emitted on stderr (bypassing ``--silent``, which only raises the logger
|
|
233
|
+
to CRITICAL) naming the unsent counts, so a dropped upload is never
|
|
234
|
+
invisible. The client is then closed unconditionally: the worker is a
|
|
235
|
+
daemon thread and the process exits immediately after, so leaving the
|
|
236
|
+
client open would not let a wedged request complete — it only leaks the
|
|
237
|
+
connection. Normal runs never reach the warning because drain() has
|
|
238
|
+
already finished the work before stop() is called.
|
|
239
|
+
|
|
240
|
+
Args:
|
|
241
|
+
timeout: Seconds to wait for the consumer thread to exit. A missing or
|
|
242
|
+
non-positive value falls back to ``config.drain_timeout``.
|
|
243
|
+
"""
|
|
244
|
+
join_timeout = (
|
|
245
|
+
timeout if (timeout is not None and timeout > 0) else self._config.drain_timeout
|
|
246
|
+
)
|
|
213
247
|
self._stop_event.set()
|
|
214
248
|
if self._thread is not None:
|
|
215
|
-
self._thread.join(timeout=
|
|
249
|
+
self._thread.join(timeout=join_timeout)
|
|
250
|
+
if self._thread.is_alive():
|
|
251
|
+
self._warn_stop_incomplete(join_timeout)
|
|
216
252
|
if self._client is not None:
|
|
217
253
|
self._client.close()
|
|
218
254
|
self._client = None
|
|
219
255
|
|
|
256
|
+
def _warn_stop_incomplete(self, join_timeout: float) -> None:
|
|
257
|
+
"""Warn on stderr when the consumer is still alive after the join.
|
|
258
|
+
|
|
259
|
+
Reached only when drain() already gave up and the consumer is wedged.
|
|
260
|
+
Routed straight to stderr (not the ``charisma_cli`` logger, which
|
|
261
|
+
``--silent`` raises to CRITICAL) so a dropped/unfinished upload is never
|
|
262
|
+
swallowed. Names the counts and the ACTUAL join timeout used so the
|
|
263
|
+
operator can raise ``--drain-timeout`` or investigate connectivity.
|
|
264
|
+
|
|
265
|
+
Args:
|
|
266
|
+
join_timeout: The timeout (seconds) the join actually waited.
|
|
267
|
+
"""
|
|
268
|
+
print(
|
|
269
|
+
f"Warning: uploader did not finish within {join_timeout:g}s "
|
|
270
|
+
f"(results_sent={self._summary.results_sent}, "
|
|
271
|
+
f"results_failed={self._summary.results_failed}, "
|
|
272
|
+
f"attachments_sent={self._summary.attachments_sent}, "
|
|
273
|
+
f"attachments_skipped={self._summary.attachments_skipped}). "
|
|
274
|
+
"Some results/attachments may not have been uploaded; raise "
|
|
275
|
+
"--drain-timeout or check connectivity.",
|
|
276
|
+
file=sys.stderr,
|
|
277
|
+
)
|
|
278
|
+
|
|
220
279
|
def drain(self, timeout: float) -> bool:
|
|
221
280
|
"""Signal drain and wait until all consumed items are flushed/sent.
|
|
222
281
|
|
|
@@ -522,15 +581,33 @@ class Uploader:
|
|
|
522
581
|
attachment_suffix="stack-trace",
|
|
523
582
|
)
|
|
524
583
|
if result.labels:
|
|
525
|
-
#
|
|
526
|
-
#
|
|
584
|
+
# 'tag' is the one MULTI-VALUED Allure label: a result can carry
|
|
585
|
+
# several {"name":"tag","value":...} entries. Collapsing the whole
|
|
586
|
+
# label list into a {name: value} dict (as before) kept only the LAST
|
|
587
|
+
# tag — the per-character-tags production bug (launch efad6338-…),
|
|
588
|
+
# where a single surviving tag string was later promoted and rendered
|
|
589
|
+
# one character per chip. Collect EVERY tag value into the backend's
|
|
590
|
+
# dedicated flat `tags` list (used verbatim by TestResultInput.
|
|
591
|
+
# to_canonical), de-duplicated in encounter order to match the batch/
|
|
592
|
+
# Allure-ZIP path (allure_parser `all_tags`). The remaining
|
|
593
|
+
# single-valued labels (owner, layer, issue, tms, …) still flow
|
|
594
|
+
# through the labels dict for the backend's promote_labels.
|
|
527
595
|
labels_dict: dict[str, str] = {}
|
|
596
|
+
tags: list[str] = []
|
|
528
597
|
for label in result.labels:
|
|
529
598
|
name_key = label.get("name", "")
|
|
530
599
|
value_str = label.get("value", "")
|
|
531
|
-
if name_key
|
|
600
|
+
if not name_key or not value_str:
|
|
601
|
+
continue
|
|
602
|
+
if name_key == "tag":
|
|
603
|
+
if value_str not in tags:
|
|
604
|
+
tags.append(value_str)
|
|
605
|
+
else:
|
|
532
606
|
labels_dict[name_key] = value_str
|
|
533
|
-
|
|
607
|
+
if labels_dict:
|
|
608
|
+
payload["labels"] = labels_dict
|
|
609
|
+
if tags:
|
|
610
|
+
payload["tags"] = tags
|
|
534
611
|
if result.parameters:
|
|
535
612
|
# Convert Allure's [{name, value}] array to {name: value} dict
|
|
536
613
|
params_dict: dict[str, str] = {}
|
|
@@ -686,6 +763,7 @@ class Uploader:
|
|
|
686
763
|
return
|
|
687
764
|
for ref in refs:
|
|
688
765
|
self._attachment_result_map[ref.source] = ref.result_uuid
|
|
766
|
+
self._attachment_name_map[ref.source] = ref.name
|
|
689
767
|
# A newly-registered mapping may unblock attachments that were dequeued
|
|
690
768
|
# before their owning result/container arrived (watch-mode ordering).
|
|
691
769
|
self._retry_pending_attachments()
|
|
@@ -792,10 +870,23 @@ class Uploader:
|
|
|
792
870
|
# would diverge from the signature). Same fallback the backend's own
|
|
793
871
|
# handle_upload_attachment uses.
|
|
794
872
|
content_type = mimetypes.guess_type(path.name)[0] or "application/octet-stream"
|
|
795
|
-
self._post_attachment(
|
|
873
|
+
self._post_attachment(
|
|
874
|
+
path.name,
|
|
875
|
+
content,
|
|
876
|
+
result_id,
|
|
877
|
+
content_type,
|
|
878
|
+
display_name=self._attachment_name_map.get(path.name, path.name)[
|
|
879
|
+
:_MAX_DISPLAY_NAME_LEN
|
|
880
|
+
],
|
|
881
|
+
)
|
|
796
882
|
|
|
797
883
|
def _post_attachment(
|
|
798
|
-
self,
|
|
884
|
+
self,
|
|
885
|
+
filename: str,
|
|
886
|
+
content: bytes,
|
|
887
|
+
result_id: str,
|
|
888
|
+
content_type: str,
|
|
889
|
+
display_name: str | None = None,
|
|
799
890
|
) -> None:
|
|
800
891
|
"""POST attachment bytes to Charisma, routing by size.
|
|
801
892
|
|
|
@@ -818,6 +909,10 @@ class Uploader:
|
|
|
818
909
|
content: Raw attachment bytes.
|
|
819
910
|
result_id: Backend-assigned ``test_results.id`` for the owning result.
|
|
820
911
|
content_type: Concrete MIME type (never None).
|
|
912
|
+
display_name: Human label (the Allure attachment name) sent as
|
|
913
|
+
``displayName`` so listings show it instead of the filename.
|
|
914
|
+
``None`` omits the field (e.g. oversized-field spills have no
|
|
915
|
+
Allure name).
|
|
821
916
|
"""
|
|
822
917
|
if self._client is None or not self._launch_id:
|
|
823
918
|
self._summary.attachments_skipped += 1
|
|
@@ -825,11 +920,13 @@ class Uploader:
|
|
|
825
920
|
return
|
|
826
921
|
|
|
827
922
|
if len(content) > self._attachment_apigw_safe_bytes:
|
|
828
|
-
self._post_attachment_via_s3(filename, content, result_id, content_type)
|
|
923
|
+
self._post_attachment_via_s3(filename, content, result_id, content_type, display_name)
|
|
829
924
|
else:
|
|
830
|
-
self._post_attachment_apigw(filename, content, result_id)
|
|
925
|
+
self._post_attachment_apigw(filename, content, result_id, display_name)
|
|
831
926
|
|
|
832
|
-
def _post_attachment_apigw(
|
|
927
|
+
def _post_attachment_apigw(
|
|
928
|
+
self, filename: str, content: bytes, result_id: str, display_name: str | None = None
|
|
929
|
+
) -> None:
|
|
833
930
|
"""POST attachment bytes via the API-Gateway multipart route, with retry.
|
|
834
931
|
|
|
835
932
|
The original (and still default, for content at or below the routing
|
|
@@ -844,9 +941,12 @@ class Uploader:
|
|
|
844
941
|
content: Raw attachment bytes.
|
|
845
942
|
result_id: Backend-assigned ``test_results.id`` for the owning result
|
|
846
943
|
(from the append-results response), sent as ``resultId``.
|
|
944
|
+
display_name: Allure attachment label, sent as ``displayName`` when set.
|
|
847
945
|
"""
|
|
848
946
|
files = {"file": (filename, content)}
|
|
849
947
|
data = {"name": filename, "resultId": result_id}
|
|
948
|
+
if display_name:
|
|
949
|
+
data["displayName"] = display_name
|
|
850
950
|
|
|
851
951
|
for attempt in range(MAX_RETRIES + 1):
|
|
852
952
|
try:
|
|
@@ -937,7 +1037,12 @@ class Uploader:
|
|
|
937
1037
|
return None
|
|
938
1038
|
|
|
939
1039
|
def _post_attachment_via_s3(
|
|
940
|
-
self,
|
|
1040
|
+
self,
|
|
1041
|
+
filename: str,
|
|
1042
|
+
content: bytes,
|
|
1043
|
+
result_id: str,
|
|
1044
|
+
content_type: str,
|
|
1045
|
+
display_name: str | None = None,
|
|
941
1046
|
) -> None:
|
|
942
1047
|
"""Upload a large attachment via presign -> direct S3 PUT -> complete.
|
|
943
1048
|
|
|
@@ -967,6 +1072,8 @@ class Uploader:
|
|
|
967
1072
|
result_id: Backend-assigned ``test_results.id`` for the owning result.
|
|
968
1073
|
content_type: Concrete MIME type, signed into the presigned URL and
|
|
969
1074
|
echoed verbatim on the PUT and complete steps.
|
|
1075
|
+
display_name: Allure attachment label, sent as ``displayName`` on the
|
|
1076
|
+
presign and complete requests when set.
|
|
970
1077
|
"""
|
|
971
1078
|
presign_body = {
|
|
972
1079
|
"resultId": result_id,
|
|
@@ -974,6 +1081,8 @@ class Uploader:
|
|
|
974
1081
|
"contentType": content_type,
|
|
975
1082
|
"size": len(content),
|
|
976
1083
|
}
|
|
1084
|
+
if display_name:
|
|
1085
|
+
presign_body["displayName"] = display_name
|
|
977
1086
|
presign_resp = self._request_with_retry(
|
|
978
1087
|
"presign",
|
|
979
1088
|
filename,
|
|
@@ -1033,6 +1142,8 @@ class Uploader:
|
|
|
1033
1142
|
"contentType": content_type,
|
|
1034
1143
|
"s3Key": s3_key,
|
|
1035
1144
|
}
|
|
1145
|
+
if display_name:
|
|
1146
|
+
complete_body["displayName"] = display_name
|
|
1036
1147
|
complete_resp = self._request_with_retry(
|
|
1037
1148
|
"complete",
|
|
1038
1149
|
filename,
|
|
@@ -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
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
from pathlib import Path
|
|
4
4
|
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
5
7
|
from charisma_cli.models import (
|
|
6
8
|
AttachmentRef,
|
|
7
9
|
CharismaResult,
|
|
@@ -148,6 +150,26 @@ class TestAttachmentRef:
|
|
|
148
150
|
assert ref.mime_type == "image/png"
|
|
149
151
|
assert ref.result_uuid == "result-uuid-456"
|
|
150
152
|
|
|
153
|
+
def test_from_allure_keeps_allure_name(self) -> None:
|
|
154
|
+
"""A set Allure name is the human label (e.g. 'actual image')."""
|
|
155
|
+
ref = AttachmentRef.from_allure(
|
|
156
|
+
{"source": "9f2-attachment.png", "name": "actual image", "type": "image/png"},
|
|
157
|
+
"res-1",
|
|
158
|
+
)
|
|
159
|
+
assert ref is not None
|
|
160
|
+
assert ref.name == "actual image"
|
|
161
|
+
assert ref.source == "9f2-attachment.png"
|
|
162
|
+
|
|
163
|
+
@pytest.mark.parametrize("attachment_name", ["", " ", None])
|
|
164
|
+
def test_from_allure_empty_name_falls_back_to_source(self, attachment_name) -> None:
|
|
165
|
+
"""An empty/blank/null Allure name must not yield a blank label."""
|
|
166
|
+
attachment = {"source": "9f2-attachment.png", "type": "image/png"}
|
|
167
|
+
if attachment_name is not None:
|
|
168
|
+
attachment["name"] = attachment_name
|
|
169
|
+
ref = AttachmentRef.from_allure(attachment, "res-1")
|
|
170
|
+
assert ref is not None
|
|
171
|
+
assert ref.name == "9f2-attachment.png"
|
|
172
|
+
|
|
151
173
|
def test_different_mime_types(self) -> None:
|
|
152
174
|
for mime in ["text/plain", "application/json", "video/mp4", "image/jpeg"]:
|
|
153
175
|
ref = AttachmentRef(source="file", name="f", mime_type=mime, result_uuid="r")
|
|
@@ -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,479 @@ 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
|
+
)
|
|
2063
|
+
|
|
2064
|
+
|
|
2065
|
+
class TestAttachmentDisplayName:
|
|
2066
|
+
"""The Allure attachment name reaches the backend as ``displayName``.
|
|
2067
|
+
|
|
2068
|
+
Bug: attachments showed as ``<uuid>-attachment.png`` because the CLI sent the
|
|
2069
|
+
on-disk filename as the only ``name`` and never the Allure label ("actual
|
|
2070
|
+
image" / "expected image"). ``name`` must stay the real filename (the backend
|
|
2071
|
+
derives extension, MIME and S3 key from it); the human label travels in
|
|
2072
|
+
``displayName`` on every transport (multipart, presign, complete). An empty
|
|
2073
|
+
Allure name falls back to the filename so the label is never blank.
|
|
2074
|
+
"""
|
|
2075
|
+
|
|
2076
|
+
LAUNCH = "launch-1"
|
|
2077
|
+
RESULTS_URL = f"https://charisma.test/api/v1/launches/{LAUNCH}/results"
|
|
2078
|
+
APIGW_URL = f"https://charisma.test/api/v1/launches/{LAUNCH}/attachments"
|
|
2079
|
+
PRESIGN_URL = f"https://charisma.test/api/v1/launches/{LAUNCH}/attachments/presign"
|
|
2080
|
+
COMPLETE_URL = f"https://charisma.test/api/v1/launches/{LAUNCH}/attachments/complete"
|
|
2081
|
+
S3_PUT_URL = "https://s3.amazonaws.com/charisma-bucket/attachments/p/l/r/obj.png?sig=abc"
|
|
2082
|
+
SOURCE = "9f2c1a7e-attachment.png"
|
|
2083
|
+
|
|
2084
|
+
@staticmethod
|
|
2085
|
+
def _multipart_field(route, field: str) -> str | None:
|
|
2086
|
+
"""Return a multipart form field value from the first recorded request."""
|
|
2087
|
+
body = route.calls[0].request.content.decode("utf-8", errors="replace")
|
|
2088
|
+
marker = f'name="{field}"'
|
|
2089
|
+
idx = body.find(marker)
|
|
2090
|
+
if idx == -1:
|
|
2091
|
+
return None
|
|
2092
|
+
value = body[idx + len(marker):].split("\r\n\r\n", 1)[1]
|
|
2093
|
+
return value.split("\r\n", 1)[0]
|
|
2094
|
+
|
|
2095
|
+
@staticmethod
|
|
2096
|
+
def _json_body(route) -> dict:
|
|
2097
|
+
import json
|
|
2098
|
+
|
|
2099
|
+
return json.loads(route.calls[0].request.content)
|
|
2100
|
+
|
|
2101
|
+
def _run(self, tmp_path: Path, result: dict, files: dict[str, bytes], monkeypatch,
|
|
2102
|
+
threshold: int = 1024 * 1024) -> Uploader:
|
|
2103
|
+
"""Upload one result plus its attachment files through a real Uploader."""
|
|
2104
|
+
import json
|
|
2105
|
+
|
|
2106
|
+
monkeypatch.setenv("CHARISMA_ATTACHMENT_APIGW_SAFE_BYTES", str(threshold))
|
|
2107
|
+
monkeypatch.setattr("charisma_cli.uploader.exponential_backoff", lambda attempt: 0.0)
|
|
2108
|
+
queue: PriorityQueue = PriorityQueue()
|
|
2109
|
+
uploader = Uploader(_make_config(), queue)
|
|
2110
|
+
uploader._launch_id = self.LAUNCH
|
|
2111
|
+
|
|
2112
|
+
result_file = tmp_path / "r1-result.json"
|
|
2113
|
+
result_file.write_text(json.dumps(result), encoding="utf-8")
|
|
2114
|
+
queue.put(FileEvent(path=result_file, category=FileCategory.RESULT))
|
|
2115
|
+
for fname, content in files.items():
|
|
2116
|
+
(tmp_path / fname).write_bytes(content)
|
|
2117
|
+
queue.put(FileEvent(path=tmp_path / fname, category=FileCategory.ATTACHMENT))
|
|
2118
|
+
|
|
2119
|
+
uploader.start()
|
|
2120
|
+
uploader.drain(timeout=5.0)
|
|
2121
|
+
uploader.stop()
|
|
2122
|
+
return uploader
|
|
2123
|
+
|
|
2124
|
+
@staticmethod
|
|
2125
|
+
def _result(attachments: list[dict], steps: list[dict] | None = None) -> dict:
|
|
2126
|
+
result = {
|
|
2127
|
+
"uuid": "r1", "historyId": "hist-r1", "fullName": "atd.png_check",
|
|
2128
|
+
"status": "failed", "start": 1000, "stop": 2000, "attachments": attachments,
|
|
2129
|
+
}
|
|
2130
|
+
if steps is not None:
|
|
2131
|
+
result["steps"] = steps
|
|
2132
|
+
return result
|
|
2133
|
+
|
|
2134
|
+
def _mock_results(self) -> None:
|
|
2135
|
+
respx.post(self.RESULTS_URL).mock(
|
|
2136
|
+
return_value=httpx.Response(200, json={
|
|
2137
|
+
"accepted": 1, "results": [{"testId": "hist-r1", "resultId": "rid-r1"}],
|
|
2138
|
+
})
|
|
2139
|
+
)
|
|
2140
|
+
|
|
2141
|
+
@respx.mock
|
|
2142
|
+
def test_multipart_sends_allure_name_as_display_name_and_filename_as_name(
|
|
2143
|
+
self, tmp_path: Path, monkeypatch
|
|
2144
|
+
) -> None:
|
|
2145
|
+
"""API-GW multipart: ``displayName`` is the Allure label, ``name`` the filename."""
|
|
2146
|
+
self._mock_results()
|
|
2147
|
+
route = respx.post(self.APIGW_URL).mock(return_value=httpx.Response(201, json={"id": "a"}))
|
|
2148
|
+
|
|
2149
|
+
uploader = self._run(
|
|
2150
|
+
tmp_path,
|
|
2151
|
+
self._result([{"source": self.SOURCE, "name": "actual image", "type": "image/png"}]),
|
|
2152
|
+
{self.SOURCE: b"\x89PNG fake"},
|
|
2153
|
+
monkeypatch,
|
|
2154
|
+
)
|
|
2155
|
+
|
|
2156
|
+
assert uploader.summary.attachments_sent == 1
|
|
2157
|
+
assert self._multipart_field(route, "displayName") == "actual image"
|
|
2158
|
+
assert self._multipart_field(route, "name") == self.SOURCE
|
|
2159
|
+
|
|
2160
|
+
@respx.mock
|
|
2161
|
+
def test_two_attachments_keep_their_own_labels(self, tmp_path: Path, monkeypatch) -> None:
|
|
2162
|
+
"""The ATD pattern: 'actual image' and 'expected image' stay distinguishable."""
|
|
2163
|
+
self._mock_results()
|
|
2164
|
+
route = respx.post(self.APIGW_URL).mock(return_value=httpx.Response(201, json={"id": "a"}))
|
|
2165
|
+
expected_src = "77ab-attachment.png"
|
|
2166
|
+
|
|
2167
|
+
self._run(
|
|
2168
|
+
tmp_path,
|
|
2169
|
+
self._result([
|
|
2170
|
+
{"source": self.SOURCE, "name": "actual image", "type": "image/png"},
|
|
2171
|
+
{"source": expected_src, "name": "expected image", "type": "image/png"},
|
|
2172
|
+
]),
|
|
2173
|
+
{self.SOURCE: b"actual-bytes", expected_src: b"expected-bytes"},
|
|
2174
|
+
monkeypatch,
|
|
2175
|
+
)
|
|
2176
|
+
|
|
2177
|
+
labels_by_filename = {}
|
|
2178
|
+
for call in route.calls:
|
|
2179
|
+
body = call.request.content.decode("utf-8", errors="replace")
|
|
2180
|
+
def field(f: str) -> str:
|
|
2181
|
+
value = body[body.find(f'name="{f}"') + len(f'name="{f}"'):]
|
|
2182
|
+
return value.split("\r\n\r\n", 1)[1].split("\r\n", 1)[0]
|
|
2183
|
+
labels_by_filename[field("name")] = field("displayName")
|
|
2184
|
+
assert labels_by_filename == {
|
|
2185
|
+
self.SOURCE: "actual image",
|
|
2186
|
+
expected_src: "expected image",
|
|
2187
|
+
}
|
|
2188
|
+
|
|
2189
|
+
@respx.mock
|
|
2190
|
+
def test_empty_allure_name_falls_back_to_filename(self, tmp_path: Path, monkeypatch) -> None:
|
|
2191
|
+
"""An empty Allure name must not produce a blank displayName."""
|
|
2192
|
+
self._mock_results()
|
|
2193
|
+
route = respx.post(self.APIGW_URL).mock(return_value=httpx.Response(201, json={"id": "a"}))
|
|
2194
|
+
|
|
2195
|
+
self._run(
|
|
2196
|
+
tmp_path,
|
|
2197
|
+
self._result([{"source": self.SOURCE, "name": "", "type": "image/png"}]),
|
|
2198
|
+
{self.SOURCE: b"\x89PNG fake"},
|
|
2199
|
+
monkeypatch,
|
|
2200
|
+
)
|
|
2201
|
+
|
|
2202
|
+
assert self._multipart_field(route, "displayName") == self.SOURCE
|
|
2203
|
+
|
|
2204
|
+
@respx.mock
|
|
2205
|
+
def test_overlong_allure_name_is_truncated_not_dropped(
|
|
2206
|
+
self, tmp_path: Path, monkeypatch
|
|
2207
|
+
) -> None:
|
|
2208
|
+
"""The backend caps displayName at 255; a longer label must not cost the attachment."""
|
|
2209
|
+
self._mock_results()
|
|
2210
|
+
route = respx.post(self.APIGW_URL).mock(return_value=httpx.Response(201, json={"id": "a"}))
|
|
2211
|
+
|
|
2212
|
+
uploader = self._run(
|
|
2213
|
+
tmp_path,
|
|
2214
|
+
self._result([{"source": self.SOURCE, "name": "L" * 400, "type": "image/png"}]),
|
|
2215
|
+
{self.SOURCE: b"\x89PNG fake"},
|
|
2216
|
+
monkeypatch,
|
|
2217
|
+
)
|
|
2218
|
+
|
|
2219
|
+
assert uploader.summary.attachments_sent == 1
|
|
2220
|
+
assert self._multipart_field(route, "displayName") == "L" * 255
|
|
2221
|
+
|
|
2222
|
+
@respx.mock
|
|
2223
|
+
def test_step_nested_attachment_label_is_sent(self, tmp_path: Path, monkeypatch) -> None:
|
|
2224
|
+
"""Allure stores most attachments inside steps; their label must survive too."""
|
|
2225
|
+
self._mock_results()
|
|
2226
|
+
route = respx.post(self.APIGW_URL).mock(return_value=httpx.Response(201, json={"id": "a"}))
|
|
2227
|
+
|
|
2228
|
+
self._run(
|
|
2229
|
+
tmp_path,
|
|
2230
|
+
self._result([], steps=[{
|
|
2231
|
+
"name": "compare images",
|
|
2232
|
+
"attachments": [{"source": self.SOURCE, "name": "expected image",
|
|
2233
|
+
"type": "image/png"}],
|
|
2234
|
+
}]),
|
|
2235
|
+
{self.SOURCE: b"\x89PNG fake"},
|
|
2236
|
+
monkeypatch,
|
|
2237
|
+
)
|
|
2238
|
+
|
|
2239
|
+
assert self._multipart_field(route, "displayName") == "expected image"
|
|
2240
|
+
|
|
2241
|
+
@respx.mock
|
|
2242
|
+
def test_presigned_path_sends_display_name_in_presign_and_complete(
|
|
2243
|
+
self, tmp_path: Path, monkeypatch
|
|
2244
|
+
) -> None:
|
|
2245
|
+
"""Large attachments (presign -> PUT -> complete) carry the label on both JSON calls."""
|
|
2246
|
+
self._mock_results()
|
|
2247
|
+
presign = respx.post(self.PRESIGN_URL).mock(
|
|
2248
|
+
return_value=httpx.Response(201, json={
|
|
2249
|
+
"uploadUrl": self.S3_PUT_URL, "attachmentId": "att-1",
|
|
2250
|
+
"s3Key": "attachments/p/l/r/obj.png",
|
|
2251
|
+
})
|
|
2252
|
+
)
|
|
2253
|
+
respx.put(self.S3_PUT_URL).mock(return_value=httpx.Response(200))
|
|
2254
|
+
complete = respx.post(self.COMPLETE_URL).mock(
|
|
2255
|
+
return_value=httpx.Response(201, json={"attachmentId": "att-1"})
|
|
2256
|
+
)
|
|
2257
|
+
|
|
2258
|
+
uploader = self._run(
|
|
2259
|
+
tmp_path,
|
|
2260
|
+
self._result([{"source": self.SOURCE, "name": "actual image", "type": "image/png"}]),
|
|
2261
|
+
{self.SOURCE: b"y" * 4096},
|
|
2262
|
+
monkeypatch,
|
|
2263
|
+
threshold=1024,
|
|
2264
|
+
)
|
|
2265
|
+
|
|
2266
|
+
assert uploader.summary.attachments_sent == 1
|
|
2267
|
+
for route in (presign, complete):
|
|
2268
|
+
body = self._json_body(route)
|
|
2269
|
+
assert body["displayName"] == "actual image"
|
|
2270
|
+
assert body["name"] == self.SOURCE
|
|
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
|