Skip to content

docs: make the Pebble check and stored state examples run - #2777

Open
tonyandrewmeyer wants to merge 6 commits into
canonical:mainfrom
tonyandrewmeyer:docs-howto-fixes
Open

tonyandrewmeyer wants to merge 6 commits into
canonical:mainfrom
tonyandrewmeyer:docs-howto-fixes

Conversation

@tonyandrewmeyer

@tonyandrewmeyer tonyandrewmeyer commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Two how-to guides had examples that don't work exactly as described.

The Pebble health checks how-to's unit-test example raises UnboundLocalError once its assert state_out... is filled in, because layer is used before it's defined.

  • The layer is now defined first, with an http section, so the CheckInfo can take its level, startup and threshold from the same place the charm does.
  • failures moves out of the layer. Pebble reports it back in a CheckInfo; it isn't a check field, so it was being silently dropped.
  • The example charm compared event.info to 'online', which is never true (LazyCheckInfo has no __eq__), so it now compares event.info.name.
  • The second example charm, which the text says should share one handler between the two events, observed a recovered handler it never defined. It now uses a single _on_pebble_check for both.

None of the three unit-test examples in the stored state how-to ran, and each fix exposed the next problem.

  • _calculate_expensive_value was never defined, and the example charm didn't observe the events it has handlers for.
  • The example computed the value in start and expected to read it in install, but install always comes first, so the two handlers (and the tests) are swapped.
  • The log assertion indexed ctx.juju_log[0], which is ops' own startup line, so it now looks for the charm's line anywhere in the list.
  • The peer-relation charm wrote an int into the databag and wrote application data without being the leader, and the test read expensive_value where the charm wrote expensive-value.
  • The peer-relation half now has its own charm class, so it works when read on its own, and its handlers cope with the relation being gone, or the value not yet written, at stop.
  • The explanation page the how-to sends readers to first had an unterminated f-string in three blocks, so none of them parsed.

The example in the "write unit tests" section doesn't parse: it ends in
`assert state_out...`. Once that's fixed it raises `UnboundLocalError`,
because `layer` is read three lines above where it's defined. The layer
it then builds isn't a check the reader would want either: `failures` is
runtime state reported in a `CheckInfo`, not a field of a layer's check,
so it's silently dropped, and a check named `http-test` has no `http`
section.

The example now defines the layer first, uses `threshold` in the layer
and `failures` in the `CheckInfo`, makes the check an actual HTTP check,
and asserts on the status the page's own charm sets. Two paragraphs
after it say why, since the `threshold`/`failures` pair is easy to mix
up.

The two `event.info == 'online'` comparisons in the charm example are
also `event.info.name` now; as printed they were always false.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01524jq1rRtFtxGhsyV8Aeem
All three of the how-to's unit-test examples fail as printed, and the
explanation page it opens by citing has three Python blocks that don't
parse.

In the how-to:

* `_calculate_expensive_value` was never defined, so both example charms
  called something that doesn't exist. It's a method on the charm now,
  returning the 42 the page's own assertions expect, and the charm
  observes the events whose handlers the page goes on to show.
* `ctx.juju_log[0]` is the `ops X.Y.Z up and running.` line, not the
  charm's first message, so that assertion could never pass. The test
  looks for the charm's line in `ctx.juju_log` instead.
* The peer-relation charm assigned an `int` to a databag key, which
  raises `RelationDataTypeError`, while the page's own test asserted on
  `'42'`. The handler converts the value now.
* Writing to the application databag needs leadership, so the handler
  returns early on non-leaders and the test's state is the leader.
* The test read `expensive_value` where the charm wrote
  `expensive-value`. Both are `expensive-value` now, and the test is
  named for what it does rather than duplicating the name of the
  stored-state test above it.

In the explanation page, three blocks had an unterminated f-string and
one of those also had an unclosed `format()` call.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01524jq1rRtFtxGhsyV8Aeem
@tonyandrewmeyer
tonyandrewmeyer force-pushed the docs-howto-fixes branch 2 times, most recently from dc12dbc to fe7308c Compare September 29, 2026 03:30
@tonyandrewmeyer tonyandrewmeyer changed the title docs: fix three how-to examples and cover adopting secrets docs: make the Pebble check and stored state examples run Sep 29, 2026
@tonyandrewmeyer
tonyandrewmeyer marked this pull request as ready for review September 29, 2026 03:50
…lation

Give the peer relation half of the stored state how-to its own charm class,
so the handlers are observed when it is read on its own, and guard against
the relation being gone or the value not yet written at stop. Explain that
threshold is set in the layer for typing, not for the consistency check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@dwilding dwilding left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing these!

Comment thread docs/howto/manage-containers/manage-pebble-health-checks.md Outdated
Comment thread docs/howto/manage-containers/manage-pebble-health-checks.md
Comment thread docs/howto/manage-stored-state.md Outdated
Comment thread docs/howto/manage-stored-state.md Outdated
Comment thread docs/howto/manage-stored-state.md Outdated
Comment thread docs/howto/manage-containers/manage-pebble-health-checks.md Outdated
Comment thread docs/howto/manage-containers/manage-pebble-health-checks.md Outdated
Comment thread docs/howto/manage-stored-state.md Outdated
tonyandrewmeyer and others added 3 commits October 13, 2026 22:14
…w-tos

Give the second Pebble check example the common handler its paragraph
describes; it observed a recovered handler that didn't exist. Set
can_connect on the test container to avoid a consistency warning, and
reword the threshold explanation in terms of what the charm sees.

Compute the stored value in install and read it in start, since install
always comes first, and unwrap and tighten the explanatory paragraphs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants