Repository navigation
docs: make the Pebble check and stored state examples run - #2777
Open
tonyandrewmeyer wants to merge 6 commits into
Open
tonyandrewmeyer wants to merge 6 commits into
tonyandrewmeyer wants to merge 6 commits into
Conversation
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
force-pushed
the
docs-howto-fixes
branch
2 times, most recently
from
September 29, 2026 03:30
dc12dbc to
fe7308c
Compare
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
approved these changes
Oct 12, 2026
dwilding
left a comment
Contributor
There was a problem hiding this comment.
Thanks for fixing these!
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two how-to guides had examples that don't work exactly as described.
The Pebble health checks how-to's unit-test example raises
UnboundLocalErroronce itsassert state_out...is filled in, becauselayeris used before it's defined.httpsection, so theCheckInfocan take itslevel,startupandthresholdfrom the same place the charm does.failuresmoves out of the layer. Pebble reports it back in aCheckInfo; it isn't a check field, so it was being silently dropped.event.infoto'online', which is never true (LazyCheckInfohas no__eq__), so it now comparesevent.info.name._on_pebble_checkfor both.None of the three unit-test examples in the stored state how-to ran, and each fix exposed the next problem.
_calculate_expensive_valuewas never defined, and the example charm didn't observe the events it has handlers for.startand expected to read it ininstall, butinstallalways comes first, so the two handlers (and the tests) are swapped.ctx.juju_log[0], which is ops' own startup line, so it now looks for the charm's line anywhere in the list.intinto the databag and wrote application data without being the leader, and the test readexpensive_valuewhere the charm wroteexpensive-value.stop.