Repository navigation
fix: fill in action parameter defaults in ops.testing - #2919
Indian-boult wants to merge 4 commits into
Conversation
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
left a comment
There was a problem hiding this comment.
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."
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.
There's no need to ping people; please be patient. GitHub sends a notification when a review is requested. |
In Juju, the charm gets the default value of any action parameter that isn't passed to
juju run.ops.testinggave the charm only the parameters passed toctx.on.action(), soevent.params['compress']raisedKeyErrorfor a parameter declared with a default. Harness fills in the defaults, so tests ported from Harness can hit this.The mock
action-getnow adds the defaults from the charm's actions metadata. This followsInsertDefaultsin 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.paramsdocstring now says that parameters that aren't passed get their default from the action's metadata.A test that compares
event.paramswith exactly the parameters it passed now also sees the defaults.