`ops.testing` expands framework extensions with the pre-26.04 metadata for 26.04 charms

Open
#2,967 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
testing

Research direction

Start by reading _FrameworkFactory in Charmcraft's charmcraft/extensions/app.py to see how the base selects V1 vs V2, then look at _apply_extensions in testing/src/scenario/state.py (~line 2247) and the V1-only data in testing/src/scenario/_charmcraft_extensions.py. The data comes from .github/generate_charmcraft_extensions.py, which runs charmcraft init plus expand-extensions, so it needs a 26.04 project added. Reproduce with a charmcraft.yaml using base: [email protected] and extensions: [django-framework]; done means _charm_spec shows the V2 app container and app-secret-key option, with a test covering both base versions.

Written by the indexing model from the issue text.

Description

rainy day

When ops.testing expands a Charmcraft framework extension (added in #2367), it always uses the version of the extension that Charmcraft applies on Ubuntu 22.04 and 24.04. For a charm on [email protected], Charmcraft uses a V2 version of each of the six framework extensions, so the metadata ops.testing builds doesn't match the packed charm. For example, the V2 Django extension has an app container and a peers peer relation rather than django-app and secret-storage, and a single app-secret-key option in place of django-secret-key and django-secret-key-id.

With a charmcraft.yaml that has base: [email protected] and extensions: [django-framework]:

spec = testing.Context(DemoCharm)._charm_spec
print(sorted(spec.meta['containers']), sorted(spec.meta['peers']))
print(sorted(o for o in spec.config['options'] if 'secret' in o))
['django-app'] ['secret-storage']
['django-secret-key', 'django-secret-key-id']

Charmcraft picks the version in _FrameworkFactory: V1 if any of the charm's bases is one that V1 supports, otherwise V2:
https://github.com/canonical/charmcraft/blob/9bc9e5860895461db3ce8830fd2f1d3bce15d658/charmcraft/extensions/app.py#L145-L155

Our generated data only has V1 in it:
https://github.com/canonical/operator/blob/cec6b6468de2cb93a35647c200d7bab21b4657f3/testing/src/scenario/_charmcraft_extensions.py#L25-L28

and _apply_extensions doesn't look at the base:
https://github.com/canonical/operator/blob/cec6b6468de2cb93a35647c200d7bab21b4657f3/testing/src/scenario/state.py#L2247

We should generate both versions and choose between them by the charm's base, the way Charmcraft does.

  • Charmcraft gets the bases from base, the platforms labels and build-for, and the legacy bases (get_project_bases in charmcraft/extensions/extension.py). With no base at all, it uses V2, which is the opposite of what we do now.
  • The generator (.github/generate_charmcraft_extensions.py) runs charmcraft init --profile ... and expand-extensions, so it'll need to produce a 26.04 project as well.
  • netbox-k8s-operator is a 26.04 Django charm that works around this in its unit tests, by loading its charmcraft.yaml, adding the V2 container and relations by hand, and passing that as meta. #2965 would break that workaround, and the obvious fix (deleting it) won't work until this is fixed.
Dominant language
Python
Stars
267
Forks
159
Avg merge
3d 18h
Merged PRs (30d)
39

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from canonical/operator

All issues in canonical/operator

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.