Skip to content

Commit 2de3564

Browse files
committed
feat(provider-tck): emit a machine-readable conformance report
Setting PROVIDER_TCK_REPORT_DIR makes each suite write its run to <dir>/<name>.json against the report schema in the specification repository (open-feature/spec#425, part of open-feature/spec#424). Unset means no report, which is the default and is not an error. An environment variable rather than a TckConfig field, so that emitting a report is a property of the run and not of the code: CI sets it, a local run does not, and no adopter changes a line to publish one. Several suites in one pytest session each write their own file, so flagd's two resolvers would not collide. The load-bearing part is the per-scenario list. Appendix F requires that a scenario skipped for an undeclared capability is reported as skipped with the reason and never as passed, and nothing downstream can check that against a summary line. Recording every scenario's outcome individually makes the rule checkable by the consumer instead of dependent on the runner. It is also required to be complete, because a document that quietly dropped what it skipped would satisfy the letter of the rule and still mislead whoever read it. pytest, unlike godog, reports a skip honestly -- so the interesting divergence here is elsewhere. The one scenario the Python SDK cannot satisfy is marked xfail, so the run finishes green; the provider still did not satisfy it, and the document says failed with the reason. An expected failure is a recorded deviation, not an excused one. Scenarios are therefore enumerated at collection and resolved at the end of the session rather than as fixtures run, which is also what keeps a scenario skipped by a marker -- whose fixtures never run at all -- from vanishing from the document. Identity comes from spec_revision.json, generated by hatch_build_sync.py beside the copied assets and force-included into the wheel. It has to be captured at build time: the submodule that knows the answer is not in the distribution, so an installed copy has nothing left to ask. A build that cannot reach git -- an unpacked sdist -- warns and records "unknown" rather than inventing a commit. Both the commit and the tree hash are recorded, the tree because it identifies the assets alone: unchanged by unrelated edits elsewhere in the specification, so two runs of identical assets agree even when pinned to different commits, and checkable because `git rev-parse <commit>:specification/assets/provider-tck` reproduces it. Two smaller decisions. The provider is identified by the name it reports through its own metadata, with TckConfig.name recorded as the configuration, because TckConfig.name is chosen to read well in a failure message -- "flagd-rpc" -- and a provider with two materially different modes produces two reports that are not interchangeable. And how the backend was driven is read off an optional control_api property rather than added to the BackendControl protocol, so that adding it leaves every existing control complete and one that stays quiet simply omits the field. The tests assert the two properties a consumer is entitled to assume -- that no scenario the capability gate stopped is ever reported as passed, and that every collected scenario appears exactly once, counted against pytest's own collection rather than against a number written down beside it. Signed-off-by: Simon Schrottner <simon.schrottner@flagsmith.com>
1 parent b6368f4 commit 2de3564

12 files changed

Lines changed: 1499 additions & 11 deletions

File tree

tools/openfeature-provider-tck/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
src/openfeature/contrib/tools/provider_tck/gherkin/
66
src/openfeature/contrib/tools/provider_tck/flag_data/
77
src/openfeature/contrib/tools/provider_tck/control-api.yaml
8+
# Generated alongside them, from the submodule pin, so a conformance report can
9+
# name the spec revision it ran against.
10+
src/openfeature/contrib/tools/provider_tck/spec_revision.json

tools/openfeature-provider-tck/README.md

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,72 @@ is recorded by the pin and nowhere else, so the two cannot drift apart unnoticed
478478

479479
This mirrors what `openfeature-flagd-api-testkit` already does for the flagd test harness.
480480

481+
## Conformance reports
482+
483+
Set `PROVIDER_TCK_REPORT_DIR` and each suite writes a machine-readable record of its run to
484+
`<dir>/<name>.json`, conforming to the [report schema][report-schema] in the specification.
485+
486+
```console
487+
$ PROVIDER_TCK_REPORT_DIR=./reports pytest
488+
provider-tck [in-memory]: report written to reports/in-memory.json (1 failed, 5 not-declared, 23 passed)
489+
490+
$ jq '.scenarios | group_by(.outcome) | map({(.[0].outcome): length}) | add' reports/in-memory.json
491+
{
492+
"failed": 1,
493+
"not-declared": 5,
494+
"passed": 23
495+
}
496+
```
497+
498+
It is an environment variable rather than a `TckConfig` field so that emitting a report is a property
499+
of the *run* and not of the code: CI sets it, a developer running the suite locally does not, and no
500+
adopter changes a line to publish one. Unset means no report, which is not an error. Several suites
501+
in one pytest session each write their own file, so flagd's two resolvers would not collide.
502+
503+
### Why every scenario is listed
504+
505+
Appendix F requires that a scenario skipped for an undeclared capability is reported as skipped
506+
**with the reason** and never as passed. A consumer cannot check that against a summary line, so the
507+
report records the outcome of *every* scenario individually — and is required to be complete, because
508+
a document that quietly dropped what it skipped would satisfy the letter of the rule and still
509+
mislead whoever read it.
510+
511+
Which also means the report is not a transcription of pytest's summary. The run above finishes green:
512+
the one scenario the Python SDK cannot satisfy is marked `xfail` (finding 1), so pytest counts it as
513+
expected and exits zero. The provider still did not satisfy it, and the document says `failed` with
514+
the reason — an expected failure is a recorded deviation, not an excused one.
515+
516+
Four outcomes rather than two, because "did not run" is not one thing:
517+
518+
| Outcome | Means |
519+
| --- | --- |
520+
| `passed` | the scenario ran and passed |
521+
| `failed` | the scenario ran and failed, including a known deviation marked `xfail` |
522+
| `not-declared` | skipped because the provider did not declare a capability the scenario is tagged with |
523+
| `not-applicable` | skipped for any other reason — a marker an adopter applied, a step calling `pytest.skip` |
524+
525+
### What identifies a report
526+
527+
`tck.specRevision` and `tck.assetsTree` come from `spec_revision.json`, which `hatch_build_sync.py`
528+
generates from the submodule alongside the copied assets. It has to be captured at build time: the
529+
submodule is not in the wheel, so an installed copy has nothing left to ask. A build that cannot
530+
reach git — an unpacked sdist, say — warns and records `unknown` rather than inventing a commit.
531+
532+
The tree hash is carried as well as the commit because it identifies the assets alone. It is
533+
unchanged by unrelated edits elsewhere in the specification, so two runs that executed identical
534+
assets report the same value even when pinned to different commits — and it is checkable, since
535+
`git rev-parse <specRevision>:specification/assets/provider-tck` must reproduce it.
536+
537+
`provider.name` is what the provider reports through its own metadata, not `TckConfig.name`.
538+
`TckConfig.name` is chosen to read well in a failure message — `flagd-rpc` — which makes it the
539+
*configuration*, and it is reported as such. One provider with two materially different modes
540+
produces two reports that are not interchangeable.
541+
542+
`backend.controlApi` is read off an optional `control_api` property on your `BackendControl`,
543+
returning `"http"` or `"in-process"`. It is not a member of the protocol: adding one would make every
544+
existing control incomplete for the sake of one string, and a control that stays quiet simply omits
545+
the field.
546+
481547
## The self-tests
482548

483549
| Suite | Subject | Why |
@@ -489,14 +555,15 @@ This mirrors what `openfeature-flagd-api-testkit` already does for the flagd tes
489555
| `test_declaration` | what a `TckConfig` claims | none of it is observable in a pass or a fail, so nothing else would catch it |
490556
| `test_extensions` | an adopter's own scenarios | an extension runs inside the canonical suite, changes nothing for an adopter who has none, and cannot take a canonical scenario's identity |
491557
| `test_http_control` | `HttpControl` | the `/reset` fallback, the disconnect bookkeeping and the control-API it reports, against a stubbed control API |
558+
| `test_report` | the conformance report | checks the two properties a consumer is entitled to assume |
492559

493560
```
494-
163 passed, 35 skipped, 2 xfailed
561+
PLACEHOLDER passed, 27 skipped, 2 xfailed
495562
```
496563

497-
No Docker and no network beyond loopback. The conformance suites take under a second;
498-
`test_extensions` takes most of the rest, because the properties it checks are properties of a whole
499-
pytest session and it runs a generated adoption in a subprocess to check them.
564+
No Docker and no network beyond loopback. The conformance suites take under a second; `test_report`
565+
and `test_extensions` take most of the rest, because the properties they check are properties of a
566+
whole pytest session and they run generated adoptions in subprocesses to check them.
500567

501568
Neither in-memory suite declares `@lifecycle`, so the six lifecycle scenarios — three about
502569
initialisation, three about shutdown — are skipped in both. That is the point: with no backend to
@@ -521,7 +588,14 @@ both. Both declare `@variants`, since an in-memory flag set is keyed by variant
521588
stack and discovering its mapped ports is still each adopter's own code. Abstracting that from a
522589
single example tends to produce the wrong abstraction; it should wait for a second adopter.
523590
- **Caching, hooks and flag metadata** are not covered.
524-
591+
- **A report cannot name a Scenario Outline row portably.** Every row of an outline shares one
592+
scenario name, and the report schema has nowhere to put the row, so several entries would be
593+
indistinguishable — including, here, one that differs in outcome from its siblings. This
594+
implementation qualifies the name with pytest's example id (`... [boolean-flag-Integer-1]`), which
595+
is unambiguous but is not what another language would produce for the same row. Raised on
596+
[open-feature/spec#424](https://github.com/open-feature/spec/issues/424).
597+
598+
[report-schema]: https://github.com/open-feature/spec/blob/main/specification/assets/provider-tck/report/conformance-report.schema.json
525599
[appendix-a]: https://github.com/open-feature/spec/blob/main/specification/appendix-a-included-utilities.md
526600
[appendix-f]: https://github.com/open-feature/spec/blob/main/specification/appendix-f-provider-conformance.md
527601
[spec]: https://github.com/open-feature/spec

tools/openfeature-provider-tck/hatch_build.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,28 @@
1818
# the single definition of what gets copied where -- would not be importable.
1919
sys.path.insert(0, str(Path(__file__).parent))
2020

21-
from hatch_build_sync import FILES, PACKAGE_REL, SPEC_ASSETS, TREES, sync
21+
from hatch_build_sync import (
22+
FILES,
23+
PACKAGE_REL,
24+
REVISION_FILE,
25+
SPEC_ASSETS,
26+
TREES,
27+
sync,
28+
)
2229

2330

2431
class SpecAssetsCopyHook(BuildHookInterface):
2532
PLUGIN_NAME = "spec-assets-copy"
2633

2734
def initialize(self, version: str, build_data: dict) -> None:
2835
root = Path(self.root)
29-
copies = [root / PACKAGE_REL / dest for _, dest in TREES + FILES]
36+
# The generated revision file travels with the assets it describes. It
37+
# has to be built here rather than read at run time, because the
38+
# submodule that knows the answer is not in the wheel and a conformance
39+
# report has to name the revision it ran against.
40+
copies = [root / PACKAGE_REL / dest for _, dest in TREES + FILES] + [
41+
root / PACKAGE_REL / REVISION_FILE
42+
]
3043

3144
# Building from a checkout: refresh from the submodule, so what ships is
3245
# always the revision the pin names. Building from an sdist: there is no

tools/openfeature-provider-tck/hatch_build_sync.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
needs no submodule: the copies are inside the distribution.
1111
"""
1212

13+
import json
1314
import shutil
15+
import subprocess
16+
import warnings
1417
from pathlib import Path
1518

1619
ROOT = Path(__file__).parent
17-
SPEC_ASSETS = (ROOT / "spec/specification/assets/provider-tck").resolve()
20+
SPEC_ROOT = (ROOT / "spec").resolve()
21+
ASSETS_PATH_IN_SPEC = "specification/assets/provider-tck"
22+
SPEC_ASSETS = (SPEC_ROOT / ASSETS_PATH_IN_SPEC).resolve()
1823
PACKAGE_REL = Path("src/openfeature/contrib/tools/provider_tck")
1924
DEST_BASE = ROOT / PACKAGE_REL
2025

@@ -35,6 +40,27 @@
3540
TREES = [("gherkin", "gherkin"), ("flags", "flag_data")]
3641
FILES = [("openapi/control-api.yaml", "control-api.yaml")]
3742

43+
REVISION_FILE = "spec_revision.json"
44+
"""Which revision of the specification the copied assets came from.
45+
46+
Recorded at build time because the answer is only available at build time: the
47+
submodule that holds it is not in the wheel, and a conformance report that cannot
48+
name the revision it ran against cannot be compared with another. It is generated
49+
by the same command that copies the assets, which is what keeps the two from
50+
disagreeing.
51+
52+
Not committed, for the same reason the assets are not: the submodule pin is the
53+
single record of which revision this package targets.
54+
"""
55+
56+
UNKNOWN_REVISION = "unknown"
57+
"""Seven characters, the minimum the report schema accepts.
58+
59+
A build that cannot reach git says it does not know rather than inventing a
60+
commit, and still produces a document that validates. Which happens for real:
61+
building from a source tarball has no ``.git`` to ask.
62+
"""
63+
3864

3965
def sync() -> None:
4066
if not SPEC_ASSETS.exists():
@@ -58,6 +84,50 @@ def sync() -> None:
5884
dest.unlink()
5985
shutil.copy2(SPEC_ASSETS / src_name, dest)
6086

87+
write_revision()
88+
89+
90+
def write_revision() -> None:
91+
"""Record the spec commit and the asset tree these copies came from.
92+
93+
The tree hash is carried as well as the commit because it identifies the
94+
assets alone: it does not change when an unrelated part of the specification
95+
does, so two runs that executed identical assets report the same value even
96+
when pinned to different commits. It is also checkable rather than merely
97+
asserted, since ``git rev-parse <commit>:specification/assets/provider-tck``
98+
must reproduce it.
99+
"""
100+
commit = _git("rev-parse", "HEAD") or UNKNOWN_REVISION
101+
tree = _git("rev-parse", f"HEAD:{ASSETS_PATH_IN_SPEC}") or ""
102+
(DEST_BASE / REVISION_FILE).write_text(
103+
json.dumps({"specRevision": commit, "assetsTree": tree}, indent=2) + "\n",
104+
encoding="utf-8",
105+
)
106+
107+
108+
def _git(*args: str) -> str:
109+
"""Run git inside the submodule, returning its output or an empty string.
110+
111+
A build must not hard-fail because git is absent or the checkout is not a
112+
repository -- both are ordinary when building from an unpacked sdist. The
113+
failure is reported as a warning and the identity degrades to ``unknown``,
114+
which is legible in the resulting report rather than silently wrong.
115+
"""
116+
command = ["git", "-C", str(SPEC_ROOT), *args]
117+
try:
118+
completed = subprocess.run( # noqa: S603
119+
command, capture_output=True, check=True, text=True
120+
)
121+
except (OSError, subprocess.CalledProcessError) as error:
122+
warnings.warn(
123+
f"could not determine the spec revision ({' '.join(command)}: {error}); "
124+
f"conformance reports from this build will not name the revision they "
125+
f"ran against",
126+
stacklevel=2,
127+
)
128+
return ""
129+
return completed.stdout.strip()
130+
61131

62132
if __name__ == "__main__":
63133
sync()

tools/openfeature-provider-tck/pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ artifacts = [
5858
"src/openfeature/contrib/tools/provider_tck/gherkin/",
5959
"src/openfeature/contrib/tools/provider_tck/flag_data/",
6060
"src/openfeature/contrib/tools/provider_tck/control-api.yaml",
61+
# Which spec revision those assets came from, generated beside them. The
62+
# submodule is not in the wheel, so a conformance report emitted by an
63+
# installed copy has no other way to name the revision it ran against.
64+
"src/openfeature/contrib/tools/provider_tck/spec_revision.json",
6165
]
6266

6367
[tool.hatch.build.hooks.custom]

tools/openfeature-provider-tck/src/openfeature/contrib/tools/provider_tck/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,17 @@ def tck_config():
7676
canonical_flag_set,
7777
canonical_flags_json,
7878
)
79+
from .report import REPORT_DIR_ENV, SCHEMA_VERSION, Outcome
7980
from .state import TckState
8081

8182
__all__ = [
8283
"CHANGING_FLAG_KEY",
8384
"DECLARABLE_CAPABILITIES",
8485
"DEFAULT_CONFIGURATION",
8586
"EXTENSIONS_DIRECTORY",
87+
"REPORT_DIR_ENV",
8688
"RESERVED_CAPABILITIES",
89+
"SCHEMA_VERSION",
8790
"BackendControl",
8891
"Capability",
8992
"ConnectionControl",
@@ -92,6 +95,7 @@ def tck_config():
9295
"HttpControl",
9396
"InProcessControl",
9497
"KnownDeviation",
98+
"Outcome",
9599
"TckConfig",
96100
"TckState",
97101
"UnsupportedControlError",

0 commit comments

Comments
 (0)