Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Make Harness more strict in validating relation data contents (on write) #786

Merged
merged 3 commits into from
Jun 29, 2022

Conversation

PietroPasotti
Copy link
Contributor

@PietroPasotti PietroPasotti commented Jun 24, 2022

Brings the harness in line with the behaviour of RelationDataContents: key/value pairs MUST be of type str:str. Else: ModelError.
The harness is with this PR more 'correct' than ops itself, because while I was writing out this code I found #785. So ops allows you to write Any:str to relation data. Ops forces you to do str:str.
Once #785 is merged, ops will also enforce str:str and the harness will be at the same level.

Checklist

  • [ x ] Have any types changed? If so, have the type annotations been updated?
  • [ x ] If this code exposes model data, does it do so thoughtfully, in a way that aids understanding?
  • [ x ] Do error messages, if any, present charm authors or operators with enough information to solve the problem?

QA steps

Added integration tests for this bug.

Documentation changes

I believe the docs already make it clear that config keys must be strings.

Changelog

  • Fixed bug with relations data contents seemingly supporting arbitrary type-keys and silently 'forgetting' them.

@PietroPasotti PietroPasotti changed the title Fixed bug with relation data keys allowed to be non-string Make Harness more strict in validating relation data contents (on write) Jun 24, 2022
@PietroPasotti PietroPasotti requested review from pengale and removed request for benhoyt June 27, 2022 07:16
@rwcarlsen rwcarlsen force-pushed the harness-relation-data-strict branch from 32ef8bf to 5e5660a Compare June 29, 2022 20:51
@rwcarlsen rwcarlsen merged commit d67b1e8 into canonical:main Jun 29, 2022
benhoyt added a commit to benhoyt/traefik-k8s-operator that referenced this pull request Jan 18, 2023
* test_show_proxied_endpoints_action_no_relations previously had error
  "'ConfigData' object does not support item assignment". This was
  caused by canonical/operator#787 and the fix is to stop setting items
  on Model.config._data directly. Do it with update_config, but
  suppress the config-changed event it triggers.
* test_ingress_app_provider_relate_provide previosly had error
  ops.model.RelationDataTypeError: relation data values must be strings, not <class 'bool'>
  caused by canonical/operator#786 and the fix is to send string values
  instead of Python bools (lowercase as these would be sent to
  relation-set as YAML).
* Fix unit name in test_lib_per_unit* tests (it should be the remote
  app) to silence the warnings about that.

Also remove the unexpected config value "asyncio_mode" in
pyproject.toml's pytest settings to suppress the warning about that.

In addition, tweak some type annotations in the ingress charm libs to
fix these Pyright errors (I haven't done "charmcraft publish-lib" or
similar though):

pyright 1.1.290
/home/ben/w/traefik-k8s-operator/lib/charms/traefik_k8s/v1/ingress.py
  /home/ben/w/traefik-k8s-operator/lib/charms/traefik_k8s/v1/ingress.py:218:16 - error: Expression of type "_RelationEventSnapshot" cannot be assigned to return type "dict[Unknown, Unknown]"
    "_RelationEventSnapshot" is incompatible with "dict[Unknown, Unknown]" (reportGeneralTypeIssues)
  /home/ben/w/traefik-k8s-operator/lib/charms/traefik_k8s/v1/ingress.py:221:25 - error: Argument of type "dict[Unknown, Unknown]" cannot be assigned to parameter "snapshot" of type "_RelationEventSnapshot" in function "restore"
    "dict[Unknown, Unknown]" is incompatible with "_RelationEventSnapshot" (reportGeneralTypeIssues)
  /home/ben/w/traefik-k8s-operator/lib/charms/traefik_k8s/v1/ingress.py:253:10 - error: Expression of type "IngressPerAppProviderEvents" cannot be assigned to declared type "property"
    "IngressPerAppProviderEvents" is incompatible with "property" (reportGeneralTypeIssues)
  /home/ben/w/traefik-k8s-operator/lib/charms/traefik_k8s/v1/ingress.py:409:10 - error: Expression of type "IngressPerAppRequirerEvents" cannot be assigned to declared type "property"
    "IngressPerAppRequirerEvents" is incompatible with "property" (reportGeneralTypeIssues)
/home/ben/w/traefik-k8s-operator/lib/charms/traefik_k8s/v1/ingress_per_unit.py
  /home/ben/w/traefik-k8s-operator/lib/charms/traefik_k8s/v1/ingress_per_unit.py:320:10 - error: Expression of type "IngressPerUnitProviderEvents" cannot be assigned to declared type "property"
    "IngressPerUnitProviderEvents" is incompatible with "property" (reportGeneralTypeIssues)
  /home/ben/w/traefik-k8s-operator/lib/charms/traefik_k8s/v1/ingress_per_unit.py:578:16 - error: Expression of type "_RelationEventSnapshot" cannot be assigned to return type "dict[Unknown, Unknown]"
    "_RelationEventSnapshot" is incompatible with "dict[Unknown, Unknown]" (reportGeneralTypeIssues)
  /home/ben/w/traefik-k8s-operator/lib/charms/traefik_k8s/v1/ingress_per_unit.py:581:25 - error: Argument of type "dict[Unknown, Unknown]" cannot be assigned to parameter "snapshot" of type "_RelationEventSnapshot" in function "restore"
    "dict[Unknown, Unknown]" is incompatible with "_RelationEventSnapshot" (reportGeneralTypeIssues)
7 errors, 0 warnings, 0 informations
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants