Skip to content

fix: treat remote unit zero as explicit#2454

Open
gcomneno wants to merge 3 commits into
canonical:mainfrom
gcomneno:contrib/issue-2341-scenario-remote-unit-zero
Open

fix: treat remote unit zero as explicit#2454
gcomneno wants to merge 3 commits into
canonical:mainfrom
gcomneno:contrib/issue-2341-scenario-remote-unit-zero

Conversation

@gcomneno
Copy link
Copy Markdown
Contributor

Fixes #2341.

This updates the Scenario consistency checker to distinguish between a missing remote unit id and an explicit remote unit id of 0.

0 is a valid Juju unit id, so the checker should not treat it as falsy/absent.

Changes:

  • check relation_remote_unit_id is None instead of relying on truthiness
  • add a regression test for relation_remote_unit_id=0

Validation:

  • uv tool run --with tox-uv tox -e unit -- testing/tests/test_consistency_checker.py -k "relation_not_in_state or remote_unit_zero" -rs -vv --tb=short
  • uv tool run --with tox-uv tox -e unit -- testing/tests/test_consistency_checker.py -rs
  • ruff check testing/src/scenario/_consistency_checker.py testing/tests/test_consistency_checker.py

@tonyandrewmeyer tonyandrewmeyer self-requested a review April 28, 2026 05:39
@gcomneno gcomneno force-pushed the contrib/issue-2341-scenario-remote-unit-zero branch from d8e8bac to b941d6c Compare May 24, 2026 15:43
@gcomneno
Copy link
Copy Markdown
Contributor Author

Hi! Just gently following up on this one.

I rebased the branch onto current upstream/main and re-ran the focused checks locally:

  • PYTHONPATH="$PWD:$PWD/testing/src" uv run --with pytest --with websocket-client python -m pytest -q testing/tests/test_consistency_checker.py -rs
  • uv run ruff check testing/src/scenario/_consistency_checker.py testing/tests/test_consistency_checker.py
  • uv run ruff format --check testing/src/scenario/_consistency_checker.py testing/tests/test_consistency_checker.py

The focused test run passes with 67 passed, 6 skipped, and Ruff is clean.

Happy to adjust anything else if useful.

Copy link
Copy Markdown
Collaborator

@tonyandrewmeyer tonyandrewmeyer left a comment

Choose a reason for hiding this comment

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

Apologies for the delay here; the last 3-4 weeks have been very busy with other things.

The fix looks correct to me -- zero is indeed a valid ID and we were wrongly checking false-iness.

Thanks!

@tonyandrewmeyer tonyandrewmeyer changed the title fix(testing): treat remote unit zero as explicit fix: treat remote unit zero as explicit May 24, 2026
@tonyandrewmeyer
Copy link
Copy Markdown
Collaborator

@copilot please fix the linting (run tox -e format and verify tox -e lint shows no issues).

Comment on lines +222 to +234
layer = ops.pebble.Layer({
'checks': {
'chk1': {
'override': 'replace',
'level': 'ready',
'startup': 'disabled',
'threshold': 42,
'http': {'url': 'http://localhost:5000/version'},
layer = ops.pebble.Layer(
{
'checks': {
'chk1': {
'override': 'replace',
'level': 'ready',
'startup': 'disabled',
'threshold': 42,
'http': {'url': 'http://localhost:5000/version'},
}
}
}
})
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Spurious change?

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.

I think perhaps this is why the linter is unhappy, but for some reason Copilot has not followed up on my request to fix it, and I was too lazy to check out the branch and do it myself.

@gcomneno can you run tox -e format and then tox -e lint? You should get the same result as in CI (the format should fix whatever it is). Generally we shouldn't end up with style changes like the above.

Comment on lines +484 to +492
def test_relation_changed_remote_unit_zero_is_explicit():
relation = Relation('foo', remote_units_data={0: {}})
assert_consistent(
State(relations={relation}),
_Event('foo_relation_changed', relation=relation, relation_remote_unit_id=0),
_CharmSpec(MyCharm, {'requires': {'foo': {'interface': 'bar'}}}),
)


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there still an implicit remote unit zero case that should raise a consistency checker error? Can we add a test for it?

@gcomneno
Copy link
Copy Markdown
Contributor Author

Thanks, good catch.

I ran tox -e format and tox -e lint via uv tool run --with tox-uv; both pass now.

For the extra negative coverage: Relation('foo') defaults to remote unit 0, so omitting remote_unit in that case only produces the existing implicit-remote-unit warning. I added the actual error-path test with remote_units_data={}, which verifies that a relation-changed event without an explicit remote unit is inconsistent when no remote unit is available.

I also verified the focused tests with:

tox -e py3.12-unit -- testing/tests/test_consistency_checker.py -k "remote_unit_zero or available_remote_unit" -rN

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.

state transition testing relation events don't like remote unit zero

3 participants