Skip to content

test: migrate to observability pytest-bdd feature files pattern - #456

Merged
lucabello merged 13 commits into
feat/bdd-coveragefrom
test/bdd-wire-features
Nov 9, 2026
Merged

lucabello merged 13 commits into
feat/bdd-coveragefrom
test/bdd-wire-features

Conversation

@lucabello

@lucabello lucabello commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Issue

Stacked on #452. In that PR no test module loads its feature file (no scenarios() call), so bdd_features_base_dir has no effect and the @given/@when/@then decorators sit on test_* functions that pytest runs as plain tests in file order. The feature files describe tests that are not the ones that run, and they phrase the same things in different ways.

Solution

  • Each tests/integration/test_<feature>.py only loads its feature with scenarios("<feature>.feature"), so every scenario runs through its feature file. The steps live in tests/integration/steps, one module per topic, registered in conftest.py.
  • The language and layout follow the alertmanager features (test: migrate integration tests to feature files alertmanager-k8s-operator#495):
    • a feature has a description, and a scenario is named after what a charm receives or does ("Loki receives Pyroscope's logs");
    • relations read Pyroscope is related to Loki over "logging", with the endpoint quoted, and a COUNTERPARTS table in steps/relations.py says how to deploy each charm on demand;
    • Pyroscope is healthy replaces the active/idle checks: every application in the model is active and idle, nginx serves the UI on every coordinator unit, and every worker unit answers /ready;
    • And is used instead of *.
  • Given steps deploy or integrate only what is missing and wait for it to settle, so a scenario does not depend on the one before it.
  • The steps query Pyroscope and Grafana with observability-clients instead of requests and curl; assertions.py is gone.
  • self-monitoring.feature relates Pyroscope to an OpenTelemetry Collector, which stands for Prometheus, Loki and Tempo, instead of deploying that stack. The collector is blocked without a backend, but it receives, and a filter processor per application and signal, set with its processors config, counts what each application sends: the steps read those counts from its own metrics, and the alert rules and scrape jobs from the relation data. Grafana and Catalogue stay, for the datasource, dashboards and item.
  • The ingress scenarios name the entry path, not the workload: the coordinator's nginx serves the endpoints in every case, and they are hit "on the coordinator" or "through the ingress" (Traefik, over the ingress relation).
  • juju-doctor.feature is only the probes. The gradual deployment, with the workers blocked until the coordinator and an s3 backend are related, is deployment-distributed.feature.
  • profiling.feature replaces profiling-tls and the two collector features: a scenario outline runs the ingestion over HTTP and HTTPS, as alertmanager's tracing feature does, and Pyroscope serves over <scheme> brings the model to either.
  • retention.feature is configuration.feature, named after what it configures.
  • @retry goes below @then: pytest-bdd registers the function the decorator is applied to, so with the order of test: migrate to observability pytest-bdd feature files pattern #452 the retries would be dropped.
  • The collector scenarios of profiling.feature are tagged @skip, with the reason (Collector cannot forward profiles: otelcol 0.130 rejects the OTLP profiles schema Pyroscope v2 needs #439) in the feature description, so they no longer deploy a model to skip a step.
  • The teardown fixtures and the setup/teardown markers are removed: pytest-jubilant destroys each module's model, and markers do nothing on steps.
  • The incidental changes of test: migrate to observability pytest-bdd feature files pattern #452 are reverted (worker/src/pyroscope.py newline, test_ingress.py file mode), and CONTRIBUTING.md describes the layout.

Context

With scenarios(), pytest-bdd creates one test per scenario in the module's order, and re-runs the Background for each, hence the idempotent Given steps. The scenarios of a feature share the module's Juju model.

Pyroscope relates to the OpenTelemetry Collector over every telemetry endpoint without naming the collector's side, since each of Pyroscope's endpoints matches one of the collector's. The metrics, logs and traces steps check every application, not that some arrived: a filter processor matches an application's juju_application and the collector counts what it drops, so the counts are per application, and the collector's own telemetry, which goes to receivers of its own, is not among them. Charm and workload traces both arrive over OTLP, and are told apart by the charm_type of the charm's resource. The collector keeps 1% of the workloads' traces by default, so the feature sets tracing_sampling_rate_workload to 100. Alert rules are read from the relation data, so Prometheus and Loki no longer validate them. The charm traces step retries for 10 minutes: a charm only sends a trace on its next event, at the latest the next update-status.

Not done here:

  • Feature names stay equal to their file names, as test: migrate to observability pytest-bdd feature files pattern #452 sets, where alertmanager uses a title.
  • The first scenario of deployment-monolithic assumes nothing is integrated yet, so it must run before the ones that integrate the coordinator; the same goes for the first one of deployment-distributed.
  • The "removing COS / ingress / the stack leaves Pyroscope active" teardown checks of the old @pytest.mark.teardown tests are gone; they were not scenarios.

Testing Instructions

tox -e integration

Scenarios are collected with pytest tests/integration --collect-only -q: 22 tests, 2 of them skipped.

Verified on a local k8s controller, with charms packed from this branch: test_deployment_monolithic (4 passed), test_deployment_distributed and test_juju_doctor (3 passed), test_profiling (HTTP and HTTPS passed, the 2 collector scenarios skipped), test_configuration (passed) and test_self_monitoring (10 scenarios passed, with the per-application counts, before its metrics and logs scenarios were grouped into 2, which was not re-run). test_ingress was not run.

Upgrade Notes

None.

The BDD coverage branch drops the trailing newline of
worker/src/pyroscope.py, which is the only change to the worker charm,
and makes tests/integration/test_ingress.py executable. Neither belongs
to the change. Restore both so that the worker charm is not touched.
CONTRIBUTING.md only lists the tox environments, so nothing says where
the feature files live, which module runs them, or that their scenarios
share a model. Document the layout, the idempotent Given steps and the
@Skip tag.
The integration test modules never load their feature files: there is
no scenarios() call, so bdd_features_base_dir has no effect and the
@given/@when/@then decorators sit on test_* functions that pytest runs
as plain tests in file order. The feature files describe tests that are
not the ones that run, and some step texts no longer match the code
(the retention quotes, "Traefik").

Load each feature with scenarios() and turn the test_* functions into
steps, so that every scenario runs through its feature file:

- Given steps deploy or integrate only what is missing and wait for it
  to settle, so a scenario does not depend on the one before it.
- Steps that behave differently get different texts: the ingress
  scenarios say "through the ingress" where they hit Traefik.
- The juju-doctor scenarios, which were stages of one story, become one
  scenario.
- @Retry goes below @then, as pytest-bdd registers the function the
  decorator is applied to.
- The collector scenarios are tagged @Skip, with the reason in the
  feature description, so they no longer deploy a model to skip a step.
- The teardown fixtures and the setup/teardown markers go:
  pytest-jubilant destroys the model of each module, and markers do
  nothing on steps.
Both keywords continue the previous step type, but the feature files mix
the generic * from the proof of concept with the And that reads as a
sentence. Use And throughout.
@lucabello lucabello changed the title test: run the feature files with pytest-bdd test: uniform to pytest-bdd feature files pattern Nov 9, 2026
@lucabello lucabello changed the title test: uniform to pytest-bdd feature files pattern test: migrate to observability pytest-bdd feature files pattern Nov 9, 2026
Nginx is the coordinator's workload and serves the HTTP and gRPC
endpoints in every scenario; the ingress is whatever sits in front of it
over the ingress relation, here Traefik. The scenarios and steps say
"through nginx" for the case without an ingress, which names the
workload where the point is the entry path.

Name the entry path instead: the endpoints are hit directly on the
coordinator, or through the ingress.
The feature files each phrase the same things their own way: "a Pyroscope
cluster is deployed", "the coordinator and worker are active", relations
described as "integrate ... over ingress", scenarios named "Pyroscope is
able to ...". The step functions sit in the test modules, so shared steps
are copied or hidden in conftest.py.

Follow the language and layout of alertmanager-k8s-operator's features:

- A feature is described under its title, and a scenario is named after
  what a charm receives or does ("Loki receives Pyroscope's logs").
- A relation is "Pyroscope is related to Loki over "logging"", with the
  endpoint quoted, and a COUNTERPARTS table says how to deploy each charm
  on demand. "Pyroscope is healthy" replaces the active/idle checks.
- Self-monitoring relates one COS charm per scenario instead of deploying
  and relating them all in the background.
- Steps live in tests/integration/steps, one module per topic, registered
  as plugins; the test modules only load their scenarios.
- Where Pyroscope answers is named by the entry path, on the coordinator
  or through the ingress, not by the workload that serves it.
"Pyroscope is healthy" only checked the Juju statuses, so a unit that
was active but did not answer passed. It now also checks that nginx
serves the UI on every coordinator unit (8080, over TLS when Pyroscope
has a CA) and that every worker unit answers /ready on 4040.

The steps carried their own requests and curl calls to Prometheus,
Loki, Tempo, Grafana and Pyroscope. They use observability-clients
instead, and assertions.py is gone. The ports are named in helpers.py
instead of being repeated in the steps.

Three queries could not be moved as they were:
- Loki's rules API answers in YAML, which Loki.has_alert_rules cannot
  parse, so that step still reads the response text.
- Tempo.has_tag_value expects objects, and Tempo returns plain strings
  for the tag values, so the step reads them with get_tag_values.
- The workload traces query used the legacy tags search, which matches
  a substring; it is now a TraceQL regex for the same names.

The charm traces step retries for 10 minutes, so that it outlasts the 5
minutes between update-status events: Pyroscope is related to Tempo once
the model has settled, so an application only sends a trace on its next
event.
Some features tested two things at once, or the same thing in several
files, and self-monitoring deployed a whole COS stack to check what
Pyroscope sends to it.

- juju-doctor.feature is only the probes, on a cluster that is deployed
  with a worker for every role. The gradual deployment (workers blocked
  until the coordinator and an s3 backend are related) moves to
  deployment-distributed.feature, and its steps are idempotent.
- profiling.feature replaces profiling-tls and the two collector
  features: a scenario outline runs the ingestion over HTTP and HTTPS,
  as alertmanager's tracing feature does, with "Pyroscope serves over
  <scheme>" bringing the model to either.
- retention.feature becomes configuration.feature, named after what it
  configures.
- self-monitoring.feature relates Pyroscope to an OTel Collector for the
  metrics, logs, traces and alert rules, instead of Prometheus, Loki and
  Tempo with its workers and object storage. The collector is blocked
  without a backend, but its receivers work: the steps read what they
  accepted from its own metrics, and the alert rules and scrape jobs
  from the relation data. The workload traces are checked on the
  workers' environment, since the collector cannot tell them from charm
  traces. Grafana and Catalogue stay, for the datasource, dashboards
  and item.

The collector is left out of the waits for active, and the constants of
the COS stack that no step uses any more are removed.
CI runs the integration tests from coordinator/ with `uv run --frozen`,
which installs coordinator/pyproject.toml and coordinator/uv.lock, not
the root ones, so the steps could not import observability_clients. Add
it to the integration test dependencies of the coordinator and of the
worker, next to the other ones.
The features and steps called the collector "OTel Collector"; they use
its full name, like the charm.

scaling-monolithic.feature becomes deployment-monolithic.feature, named
like deployment-distributed.feature. Its two scenarios about a coordinator
that is blocked alone are one outline over 1 and 2 units, so the step that
scaled the coordinator is gone: the step that gives it a number of units
already scales it either way.
The steps only checked that the collector's receivers had accepted
something, so one application sending telemetry was enough for the
metrics, logs and traces of all of them.

The collector is configured with a filter processor per application and
signal, which matches the application's juju_application, and the steps
read what each processor matched from the collector's own metrics. A
charm's traces are told from a workload's by the charm_type of their
resource. The collector keeps 1% of the workloads' traces by default, so
their sampling rate is set to 100.

The workload traces step now checks that the spans of every worker
arrive, instead of the worker's trace endpoint.
@lucabello
lucabello force-pushed the test/bdd-wire-features branch from f75ccba to ad31bda Compare November 9, 2026 19:08
The three scenarios about the metrics-endpoint relation and the two about
the logging one each ran the same When. They are one scenario per
relation, with a Then per thing checked, and the traces scenarios are
named the same way.
@lucabello
lucabello marked this pull request as ready for review November 9, 2026 19:19
@lucabello
lucabello added this pull request to stack #457 November 9, 2026 19:19
@lucabello
lucabello removed this pull request from stack #457 November 9, 2026 20:03
@lucabello
lucabello merged commit 401ec71 into feat/bdd-coverage Nov 9, 2026
35 checks passed
@lucabello
lucabello deleted the test/bdd-wire-features branch November 9, 2026 20:03
lucabello added a commit that referenced this pull request Nov 9, 2026
* test: add BDD feature coverage for coordinator and worker

* test: remove unit feature files and optimize integration feature files

* fix: add ingress wait

* test: migrate to observability pytest-bdd feature files pattern (#456)

* chore: revert incidental changes to worker source and file mode

The BDD coverage branch drops the trailing newline of
worker/src/pyroscope.py, which is the only change to the worker charm,
and makes tests/integration/test_ingress.py executable. Neither belongs
to the change. Restore both so that the worker charm is not touched.

* docs: describe the feature files and how the integration tests load them

CONTRIBUTING.md only lists the tox environments, so nothing says where
the feature files live, which module runs them, or that their scenarios
share a model. Document the layout, the idempotent Given steps and the
@Skip tag.

* test: run the feature files with pytest-bdd

The integration test modules never load their feature files: there is
no scenarios() call, so bdd_features_base_dir has no effect and the
@given/@when/@then decorators sit on test_* functions that pytest runs
as plain tests in file order. The feature files describe tests that are
not the ones that run, and some step texts no longer match the code
(the retention quotes, "Traefik").

Load each feature with scenarios() and turn the test_* functions into
steps, so that every scenario runs through its feature file:

- Given steps deploy or integrate only what is missing and wait for it
  to settle, so a scenario does not depend on the one before it.
- Steps that behave differently get different texts: the ingress
  scenarios say "through the ingress" where they hit Traefik.
- The juju-doctor scenarios, which were stages of one story, become one
  scenario.
- @Retry goes below @then, as pytest-bdd registers the function the
  decorator is applied to.
- The collector scenarios are tagged @Skip, with the reason in the
  feature description, so they no longer deploy a model to skip a step.
- The teardown fixtures and the setup/teardown markers go:
  pytest-jubilant destroys the model of each module, and markers do
  nothing on steps.

* test: use And instead of * in the feature files

Both keywords continue the previous step type, but the feature files mix
the generic * from the proof of concept with the And that reads as a
sentence. Use And throughout.

* test: tell the ingress apart from nginx in the ingress feature

Nginx is the coordinator's workload and serves the HTTP and gRPC
endpoints in every scenario; the ingress is whatever sits in front of it
over the ingress relation, here Traefik. The scenarios and steps say
"through nginx" for the case without an ingress, which names the
workload where the point is the entry path.

Name the entry path instead: the endpoints are hit directly on the
coordinator, or through the ingress.

* test: phrase the features and organise the steps like alertmanager

The feature files each phrase the same things their own way: "a Pyroscope
cluster is deployed", "the coordinator and worker are active", relations
described as "integrate ... over ingress", scenarios named "Pyroscope is
able to ...". The step functions sit in the test modules, so shared steps
are copied or hidden in conftest.py.

Follow the language and layout of alertmanager-k8s-operator's features:

- A feature is described under its title, and a scenario is named after
  what a charm receives or does ("Loki receives Pyroscope's logs").
- A relation is "Pyroscope is related to Loki over "logging"", with the
  endpoint quoted, and a COUNTERPARTS table says how to deploy each charm
  on demand. "Pyroscope is healthy" replaces the active/idle checks.
- Self-monitoring relates one COS charm per scenario instead of deploying
  and relating them all in the background.
- Steps live in tests/integration/steps, one module per topic, registered
  as plugins; the test modules only load their scenarios.
- Where Pyroscope answers is named by the entry path, on the coordinator
  or through the ingress, not by the workload that serves it.

* test: query Pyroscope and COS with observability-clients

"Pyroscope is healthy" only checked the Juju statuses, so a unit that
was active but did not answer passed. It now also checks that nginx
serves the UI on every coordinator unit (8080, over TLS when Pyroscope
has a CA) and that every worker unit answers /ready on 4040.

The steps carried their own requests and curl calls to Prometheus,
Loki, Tempo, Grafana and Pyroscope. They use observability-clients
instead, and assertions.py is gone. The ports are named in helpers.py
instead of being repeated in the steps.

Three queries could not be moved as they were:
- Loki's rules API answers in YAML, which Loki.has_alert_rules cannot
  parse, so that step still reads the response text.
- Tempo.has_tag_value expects objects, and Tempo returns plain strings
  for the tag values, so the step reads them with get_tag_values.
- The workload traces query used the legacy tags search, which matches
  a substring; it is now a TraceQL regex for the same names.

The charm traces step retries for 10 minutes, so that it outlasts the 5
minutes between update-status events: Pyroscope is related to Tempo once
the model has settled, so an application only sends a trace on its next
event.

* test: split the features and use an OTel Collector for self-monitoring

Some features tested two things at once, or the same thing in several
files, and self-monitoring deployed a whole COS stack to check what
Pyroscope sends to it.

- juju-doctor.feature is only the probes, on a cluster that is deployed
  with a worker for every role. The gradual deployment (workers blocked
  until the coordinator and an s3 backend are related) moves to
  deployment-distributed.feature, and its steps are idempotent.
- profiling.feature replaces profiling-tls and the two collector
  features: a scenario outline runs the ingestion over HTTP and HTTPS,
  as alertmanager's tracing feature does, with "Pyroscope serves over
  <scheme>" bringing the model to either.
- retention.feature becomes configuration.feature, named after what it
  configures.
- self-monitoring.feature relates Pyroscope to an OTel Collector for the
  metrics, logs, traces and alert rules, instead of Prometheus, Loki and
  Tempo with its workers and object storage. The collector is blocked
  without a backend, but its receivers work: the steps read what they
  accepted from its own metrics, and the alert rules and scrape jobs
  from the relation data. The workload traces are checked on the
  workers' environment, since the collector cannot tell them from charm
  traces. Grafana and Catalogue stay, for the datasource, dashboards
  and item.

The collector is left out of the waits for active, and the constants of
the COS stack that no step uses any more are removed.

* test: add observability-clients to the charms' dev dependencies

CI runs the integration tests from coordinator/ with `uv run --frozen`,
which installs coordinator/pyproject.toml and coordinator/uv.lock, not
the root ones, so the steps could not import observability_clients. Add
it to the integration test dependencies of the coordinator and of the
worker, next to the other ones.

* test: name the OpenTelemetry Collector and merge scaling into deployment

The features and steps called the collector "OTel Collector"; they use
its full name, like the charm.

scaling-monolithic.feature becomes deployment-monolithic.feature, named
like deployment-distributed.feature. Its two scenarios about a coordinator
that is blocked alone are one outline over 1 and 2 units, so the step that
scaled the coordinator is gone: the step that gives it a number of units
already scales it either way.

* test: count the telemetry of each Pyroscope app in self-monitoring

The steps only checked that the collector's receivers had accepted
something, so one application sending telemetry was enough for the
metrics, logs and traces of all of them.

The collector is configured with a filter processor per application and
signal, which matches the application's juju_application, and the steps
read what each processor matched from the collector's own metrics. A
charm's traces are told from a workload's by the charm_type of their
resource. The collector keeps 1% of the workloads' traces by default, so
their sampling rate is set to 100.

The workload traces step now checks that the spans of every worker
arrive, instead of the worker's trace endpoint.

* test: group self-monitoring scenarios by what Pyroscope sends

The three scenarios about the metrics-endpoint relation and the two about
the logging one each ran the same When. They are one scenario per
relation, with a Then per thing checked, and the traces scenarios are
named the same way.

* test: use And in the self-monitoring scenarios

---------

Co-authored-by: Luca Bello <36242061+lucabello@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants