While reviewing and discussing charm development documentation, my fellow Charm Tech teammates and I sometimes wonder “is this claim correct?” and decide that “someone should test this”. Definitely, 100%, someone will, we enthusiastically agree…
And actually, often one of us does end up testing the claim. But the test is usually opaque to everyone else.
Wouldn’t it be nice to have a system that:
- Can accept a description of what needs testing, then automatically construct suitable tests
- Produces an easily-reviewable artifact that presents the test methodology and results
This post explains my attempt to build such a system!
A quick primer on charmed applications
An application in Juju is essentially a component of a larger software system. For example, a blog’s web server or its database. Each application has one or many units, where a unit consists of the running software (the web server or database) plus charm code that manages the software in response to events from Juju. For more detail, see How Juju works.
Why might there be more than one unit? Congratulations, your blog post just hit the front page of Hacker News! You now need multiple units to handle the traffic.[1]
Suppose that the user (sitting outside Juju) wants to change the configuration of an application. Let’s say, to adjust a web server option, such as a timeout value. When the user issues the appropriate Juju CLI command, a config-changed event is dispatched by Juju to the charm code of each web server unit, then the charm code reconfigures the running web server.
A claim we’d like to test
I just claimed that “a config-changed event is dispatched by Juju to the charm code of each web server unit”. Is it really true that each unit sees a config-changed event? Yes, it’s true — but let’s pretend we have our doubts, and so we’d like to test the claim.
The full claim is:
If a Juju application has multiple units, the config-changed event is received by the charm code in all units.
Here’s how we might manually test this claim:
- Write a charm with a configurable option, whose code reports the current value of the option.
- Set up a Juju controller, deploy the charm, then request two units of the resulting application.
- Run a Juju CLI command to change the value of the option.
- Check that both units report the new value of the option.[2]
The rest of the post explores how we could turn these manual steps into a general system for testing claims about charm development.
The testing system, in theory
Let’s work backwards. First — I mean, last — we need somewhere we can review the test methodology and results. GitHub pull requests seem like a good fit:
- The added/changed files can be viewed as our test methodology.
- The results of some CI checks can be viewed as our test results.
- Everyone on the team can view and comment on the test.
There are complications, though. For instance, which repository are pull requests opened against? Are pull requests expected to be merged? How are checks defined? Is a pull request allowed to touch the underlying workflows? The answers to these questions could have security implications.
To make progress, let’s recap how we’d test that config-changed is received by all units of an application:
- Write a charm with a configurable option.
- Set up Juju, deploy the charm, then request two units.
- Change the value of the option.
- Check that both units saw the option change.
These steps map onto the standard charm development setup of metadata + charm code + unit tests + integration tests. Step 1 is the metadata and charm code. Steps 2-4 are integration tests — typically Python-based that deploy the charm and drive Juju. There isn’t a particular need for unit tests in this example, as they only test what happens within a single unit of the charm, but we could include unit tests as a way to increase confidence that the charm code is correct.
So it sounds like our approach should be:
| Test methodology | Test results |
|---|---|
| (a) Write a charm, with unit tests and integration tests. (b) Run the unit tests. Requires Python. (c) Run the integration tests. Requires packing the charm, Python, and Juju. |
The outcome of running the unit tests and integration tests. |
Let’s massage this approach into the form of a GitHub pull request. Instead of expecting the pull request to write every file from scratch — meaning unnecessary boilerplate for the author and reviewer — the pull request can modify a known-good starting point of:
- Metadata, including
charmcraft.yamlandpyproject.toml - Charm code, unit tests, and integration tests
- A workflow that lints the code and runs the tests on each pull request
In other words, we’ll need a base repository that contains these minimal elements.[3] Our approach is then:
| Test methodology | Test results |
|---|---|
| (a) Create a new branch in the base repository. (b) Edit the metadata, charm code, unit tests, and integration tests. The edits should encode the claim under test. (c) Open a pull request. This triggers the lint-and-test workflow. |
The pull request’s check results, which come from the lint-and-test workflow. |
It’s worth noting that we are misusing pull requests here; we emphatically don’t want our edits to be incorporated into the base repository. Each pull request is purely a vehicle for running checks and seeking review. I still haven’t decided whether this is entirely sensible… I’m open to other ideas.
The testing system, in practice
Let’s see how our approach comes together to test that config-changed is received by all units of an application.
First, the base repository. I’ve set up a repository called basic-charms that contains a minimal charm. The metadata, charm code, unit tests, and integration tests are in the usual places — matching what you get if you use charmcraft init to start a charm project.
Next, the pull request. Pull request #73 edits the charm code so that the value of a configurable option is reported whenever the option changes. (The metadata in the base repository already defines the option.) The pull request also adds unit tests to confirm that the reporting works as intended. Then, crucially, the pull request edits the integration tests:
- Changes the Juju
deploy()call to request two units - Adds a test that sets the option and asserts that both units report the same value of the option
Finally, the check results. The integration tests and other checks ran successfully, which means the config-changed claim is correct!
Making the system agentic
As you might have guessed when looking at #73, I didn’t create the pull request by hand. It was created by an agentic workflow:
- The workflow starts when an issue is created in the
basic-charmsrepository. The issue is expected to pose a claim about charm development. For example, #72 asks whether the config-changed event is received by all units of an application. - An agent evaluates the claim and researches relevant charm development docs and source code. Then the agent predicts whether the claim is true.
- The agent edits the known-good charm, including its tests, in an attempt to validate the prediction.
- The agent checks its work by linting the charm and running its unit tests. The agent doesn’t run the charm’s integration tests — mainly to save time.
- The agent opens a pull request and explains the reasoning behind its work.
The end result is a test that is fully deterministic. The agent’s value is in researching and writing a suitable test, saving a significant amount of manual work.
How the agent works
The agentic parts of the workflow use OpenCode with an OpenRouter key. Thank you @secondskoll for inspiring this setup!
The agent is only allowed to edit files within the charm directory; it’s not allowed to touch the repository’s workflows. Even if the agent did edit a workflow, the workflow wouldn’t run without approval, but restricting the agent is a stronger level of protection. In my experimentation so far, there’s been no need for agents to edit workflows. It’s been sufficient for agents to edit the Python code that is run by the repository’s lint-and-test workflow.
Unfortunately, that agent-edited Python is still untrusted code that has access to the workflow environment. Even linting the charm could be dangerous, as the agent could have added a malicious linting dependency to pyproject.toml. The risks are partially mitigated because the lint-and-test requires approval to run — but that guardrail only applies to the final pull request. Step 4 has the agent checking its work as it goes along, which appears to involve running untrusted & unreviewed code in the workflow environment.
To fix this flaw, the agent has a custom tool called run_tox. This tool spins up a Docker container that doesn’t have access to the workflow environment, runs the linter and unit tests inside the container, then presents the results to the agent. The OpenCode configuration forbids the agent from using other tools (except the one it uses to fetch development docs and source code).
The complexity in the agentic setup is worth the effort. Before I gave agents the ability to use run_tox, pull requests would often have small problems, causing the lint-and-test workflow to fail prematurely. Agents also benefit from being able to modify a charm that already passes all checks.
There’s one final nuance I want to emphasize about the agentic setup: there isn’t a neat mapping “claim true if and only if pull request checks pass.” Instead, the agent usually aims for passing checks, based on its best understanding of the world. The agent is expected to explain what the check results imply about the original claim. For example, see “How the test engages the claim” in the description of #73. Sometimes the agent aims for failing checks, if the claim is about a failure mode. For example, see “What the CI result means” in the description of #69.
What’s next?
So far, I’ve used the testing system to answer several questions that have come up during documentation work. Each time I’ve been able to refine the system to be more usable or precise. I plan to continue refining the system over the coming months, and especially encourage other people to explore similar systems. If you have any ideas or suggestions, please let me know!
In reality there’d probably be a cache/CDN to help with the traffic spike, but still, hopefully you get the idea. ↩︎
Experienced charmers: The claim we’re really testing is “all units receive config-changed when app config changes”, which in principle might behave differently from config-changed on startup. I’ve omitted this distinction for the sake of a more accessible post. ↩︎
Actually, the base repository needs two minimal Kubernetes charms and two minimal machine charms, so that we can probe claims about relations and substrate-specific claims. ↩︎