docs: remove override = "merge" from test_pebble_layer in configurable charm tutorial

Open Beginner friendly
#2,956 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
85/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
documentation

Research direction

Edit docs/tutorial/from-zero-to-hero-write-your-first-kubernetes-charm/make-your-charm-configurable.md in the "Write unit tests" section and drop the line setting expected_plan.services["fastapi"].override = "merge". Cross-check examples/k8s-2-configurable/tests/unit/test_charm.py for the same line, and run tox -e unit in that example to confirm test_pebble_layer passes with the plan keeping 'override': 'replace'. Done means the tutorial text and the example test agree and the unit test is green.

Written by the indexing model from the issue text.

Description

Page URL

https://canonical.com/juju/docs/ops/latest/tutorial/from-zero-to-hero-write-your-first-kubernetes-charm/make-your-charm-configurable/#write-unit-tests

Description

In the Write unit tests section of the "Make your charm configurable" tutorial, the instructions ask to add expected_plan.services["fastapi"].override = "merge" to test_pebble_layer in tests/unit/test_charm.py:

# Expected plan after Pebble ready with default config.
expected_plan = ops.pebble.Plan(ROCK_LAYER.to_dict())
expected_plan.services["fastapi"].override = "merge"

When running tox -e unit in a fresh environment (ops 3.10.0), test_pebble_layer fails because the merged Plan keeps 'override': 'replace' from ROCK_LAYER rather than overwriting it with 'merge':

-------------------------------- Captured log call ---------------------------------
DEBUG    root:_main.py:345 ops 3.10.0 up and running.
DEBUG    root:storage.py:70 Initializing SQLite local storage: :memory:.
DEBUG    root:_main.py:481 Emitting Juju event demo_server_pebble_ready.
INFO     charm:charm.py:75 Added updated layer 'fastapi_demo' to Pebble plan
INFO     charm:charm.py:80 Replanned with 'fastapi' service
============================= short test summary info ==============================
FAILED tests/unit/test_charm.py::test_pebble_layer - AssertionError: assert Plan({'services': {'fastapi': {'summary': 'FastAPI demo server', 'startup': 'enabled', 'override': 'replace', 'command': '/bin/uvicorn api_demo_server.app:app --host 0.0.0.0 --port 8000', 'on-success': 'shutdown', 'on-failure': 'shutdown'}}}) == Plan({'services': {'fastapi': {'summary': 'FastAPI demo server', 'startup': 'enabled', 'override': 'merge', 'command': '/bin/uvicorn api_demo_server.app:app --host 0.0.0.0 --port 8000', 'on-success': 'shutdown', 'on-failure': 'shutdown'}}})
Root Cause

Inspecting ops.pebble.Service._merge inside the tox unit test environment confirms that ops 3.10.0 matches real Pebble's layer-merging behavior by preserving the existing service's override value when merging a layer:

$ .tox/unit/bin/python -c "import inspect, ops.pebble; print(inspect.getsource(ops.pebble.Service._merge))"
    def _merge(self, other: Service):
        """Merges this service object with another service definition.

        For attributes present in both objects, the passed in service
        attributes take precedence.
        """
        for name, value in other.__dict__.items():
            if name in ('user_id', 'group_id') and value is not None:
                setattr(self, name, value)
                continue
            # Pebble keeps the existing override when it merges a layer in.
            if not value or name in ('name', 'override'):
                continue
            if name in ['after', 'before', 'requires']:
                getattr(self, name).extend(value)
            elif name in ['environment', 'on_check_failure']:
                getattr(self, name).update(value)
            else:
                setattr(self, name, value)

Because ROCK_LAYER defines "override": "replace", the merged Plan retains 'override': 'replace', making expected_plan.services["fastapi"].override = "merge" fail.

Suggested Fix
  1. Remove the instruction to add expected_plan.services["fastapi"].override = "merge" from docs/tutorial/from-zero-to-hero-write-your-first-kubernetes-charm/make-your-charm-configurable.md.
  2. Update examples/k8s-2-configurable/tests/unit/test_charm.py (and subsequent tutorial chapters/examples) accordingly when bumping their ops dependency.

Reported from: https://canonical.com/juju/docs/ops/latest/tutorial/from-zero-to-hero-write-your-first-kubernetes-charm/make-your-charm-configurable/

Dominant language
Python
Stars
267
Forks
156
Avg merge
5d 5h
Merged PRs (30d)
43

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.