Skip to content

fix: fill in action parameter defaults in ops.testing - #2919

Open
Indian-boult wants to merge 4 commits into
canonical:mainfrom
Indian-boult:fix/scenario-action-param-defaults
Open

Indian-boult wants to merge 4 commits into
canonical:mainfrom
Indian-boult:fix/scenario-action-param-defaults

Conversation

@Indian-boult

@Indian-boult Indian-boult commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

In Juju, the charm gets the default value of any action parameter that isn't passed to juju run. ops.testing gave the charm only the parameters passed to ctx.on.action(), so event.params['compress'] raised KeyError for a parameter declared with a default. Harness fills in the defaults, so tests ported from Harness can hit this.

The mock action-get now adds the defaults from the charm's actions metadata. This follows InsertDefaults in juju/gojsonschema, which Juju uses on the controller in 3.6 and in the unit agent in Juju 4. It also handles object parameters: a passed object gets the defaults of its missing properties, and an object that isn't passed is added if any of its properties have defaults. Harness only fills in top-level defaults, and is unchanged.

The Action.params docstring now says that parameters that aren't passed get their default from the action's metadata.

A test that compares event.params with exactly the parameters it passed now also sees the defaults.

Indian-boult and others added 2 commits November 18, 2026 11:34
Juju gives the charm the default value of any action parameter that
isn't passed to `juju run`. `ops.testing` gave the charm only the
parameters passed to `ctx.on.action()`, so `event.params['compress']`
raised `KeyError` for a parameter declared with a default. Harness
fills in the defaults, so tests ported from Harness can hit this.

The mock `action-get` now adds the defaults from the charm's actions
metadata. This follows Juju's `InsertDefaults` in juju/gojsonschema,
which also handles object parameters: a passed object gets the
defaults of its missing properties, and an object that isn't passed is
added if any of its properties have defaults. Harness only fills in
top-level defaults, and is unchanged.

A test that compares `event.params` with exactly the parameters it
passed now also sees the defaults.

@tonyandrewmeyer tonyandrewmeyer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for this! I checked it against what Juju does: InsertDefaults in juju/gojsonschema matches _insert_defaults branch for branch, and Juju calls it in state/unit.go (3.6) and in the uniter's runner factory (4). The new tests fail without the fix, too.

One small thing outside the diff: _Action.params says "Parameter values passed to the action.", and it would be nice to say that defaults from the action metadata get filled in, as Harness's run_action docstring does. Something like "Parameters that aren't passed get their default from the action's metadata, as in Juju."

Comment thread testing/tests/test_e2e/test_actions.py Resolved
A charm that changed a list default in place would otherwise change the
default for the next run. Also say in the Action.params docstring that
missing parameters get their default from the action metadata.
@tonyandrewmeyer

Copy link
Copy Markdown
Collaborator

@tromai, please review the PR.

There's no need to ping people; please be patient. GitHub sends a notification when a review is requested.

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