operator icon indicating copy to clipboard operation
operator copied to clipboard

Add support for passing charmcraft.yaml as metadata to testing.Context

Open tonyandrewmeyer opened this issue 1 year ago • 3 comments

Description

Charmcraft.yaml (replacing metadata.yaml) now removes the need to have different files for metadata, config, and actions. When providing metadata in the charmcraft.yaml format, we get a InconsistentScenarioError error because the config option is not recognized.

METADATA = yaml.safe_load(Path("tests/unit/charms/tls_certificates_interface/v4/dummy_requirer_charm/charmcraft.yaml").read_text())
state_in = scenario.State(
    config={"common_name": "example.com"},
)
ctx = scenario.Context(
    charm_type=DummyTLSCertificatesRequirerCharm,
    meta=METADATA,
)

Logs

scenario.runtime.InconsistentScenarioError: Inconsistent scenario. The following errors were found: config option 'common_name' in state.config but not specified in config.yaml.

Desired state

self.ctx = scenario.Context(
    charm_type=DummyTLSCertificatesRequirerCharm,
    meta=METADATA,
)

Workaround

self.ctx = scenario.Context(
    charm_type=DummyTLSCertificatesRequirerCharm,
    meta=METADATA,
    config=METADATA["config"],
    actions=METADATA["actions"],
)

Moved from https://github.com/canonical/ops-scenario/issues/156

tonyandrewmeyer avatar Oct 10 '24 02:10 tonyandrewmeyer

Aim to overload the meta argument to handle both metadata.yaml content and charmcraft.yaml content.

tonyandrewmeyer avatar Mar 03 '25 01:03 tonyandrewmeyer

Added to 25.10 proposed roadmap

benhoyt avatar Mar 25 '25 01:03 benhoyt

Discussion:

How about we do something like this:

# class ops.testing.Context
def __init__(self, charm_type, meta=None, *, config=None, actions=None):
    if config is None and meta is not None:
        config = meta.get("config")
    if actions is None and meta is not None:
        actions = meta.get("actions")

Observations:

  • if you pass any of meta/config/actions, magic auto-loading is switched off, this approach is consistent with that.
  • we’re relying on how charcraft splits singular charmcraft.yaml file into metadata/config/actions.yaml
  • meta is a positional argument
  • not introducing new arguments now is great
  • @dimaqq: if meta is a large dict with non-metadata things like bases, then we are assuming that it’s actually the (new-style, complete, singular) charmcraft.yaml file
  • [ ] we’d need to update the docstring with the above
  • [ ] update our tests, cover the functionality above
  • [ ] go through "how to manage config/actions" to see if the text needs an update

dimaqq avatar Oct 30 '25 10:10 dimaqq