diff --git a/charms/garm-configurator/lib/garm_configurator_relation.py b/charms/garm-configurator/lib/garm_configurator_relation.py index b4422df1..d4e76c1a 100644 --- a/charms/garm-configurator/lib/garm_configurator_relation.py +++ b/charms/garm-configurator/lib/garm_configurator_relation.py @@ -6,9 +6,10 @@ The ``garm-configurator`` charm owns the relation contract and publishes one provider configuration per application with ``ops.Relation.save``. The models live with the charm so consumers can share the same definitions and use ``ops.Relation.load`` instead of hand-writing -databag keys. ``ProviderConfig`` models the complete published payload (scaleset fields, runner -options, and provider credentials), so the configurator writes it with a single -``ops.Relation.save`` call. +databag keys. The application databag holds two nested values: ``provider`` (the provider and +GitHub App credentials) and ``scaleset`` (the scaleset and runner fields). +``ConfiguratorApplicationData`` is the write model that nests both, so the configurator writes +the complete payload with a single ``ops.Relation.save`` call. The payload lives on the application databag: the configurator's leader unit writes it once for the whole application. The ``GarmConfiguratorRequirer`` aggregates every joined @@ -21,7 +22,6 @@ """ import dataclasses -import json import logging import typing @@ -35,14 +35,13 @@ @pydantic.dataclasses.dataclass class ProviderConfig: - """Complete relation payload published by one ``garm-configurator`` application. + """Provider and GitHub App credentials published by one ``garm-configurator`` application. - This is the write model for the relation: the configurator fills the scaleset and runner - fields plus the provider and GitHub App credentials and publishes it with one - ``ops.Relation.save`` to the application databag. The field names are the relation databag - keys, so they are the wire contract between the configurator and GARM. The auth URL is - required: an application publishes the rest of the provider payload alongside it, so a - databag without it is incomplete and treated as invalid rather than as an empty provider. + This is the nested ``provider`` value of the application databag. The field names are the + keys inside that value, so they are the wire contract between the configurator and GARM. + The auth URL is required: an application publishes the rest of the provider payload + alongside it, so a databag without it is incomplete and treated as invalid rather than as an + empty provider. Attributes: openstack_auth_url: Keystone authentication URL. @@ -57,27 +56,6 @@ class ProviderConfig: github_app_id: GitHub App ID. github_installation_id: GitHub App installation ID. github_private_key_secret_uri: Juju secret URI holding the GitHub App private key. - name: The name of the scaleset. - provider_name: The GARM provider the scaleset runs on. - image_id: Image name or ID used for runners. - image_visibility: Glance visibility used for an image-name lookup. - security_group: Security group attached to runner instances. - flavor: Runner flavor. - os_arch: Runner CPU architecture. - min_idle_runner: Minimum number of idle runners. - max_runner: Maximum number of runners. - labels: Comma-separated runner labels. - runner_group: Runner group for org registration. - enable_shell: Whether GARM's remote shell is enabled, as a boolean string. - pre_install_scripts: JSON object of pre-install script name to script body. - org: Organization the runners register to. - repo: Repository the runners register to. - dockerhub_mirror: Docker registry mirror URL. - runner_http_proxy: Upstream HTTP proxy for runners. - aproxy_exclude_addresses: Comma-separated IPv4 addresses, CIDRs, or ranges to bypass. - aproxy_redirect_ports: Comma-separated ports or port ranges to forward. - otel_collector_endpoint: OTEL exporter endpoint. - pre_job_script: Bash script appended to the runner pre-job hook. """ openstack_auth_url: str @@ -92,36 +70,15 @@ class ProviderConfig: github_app_id: int | None = None github_installation_id: int | None = None github_private_key_secret_uri: str | None = None - name: str = "" - provider_name: str = "" - image_id: str = "" - image_visibility: str = "" - security_group: str = "" - flavor: str = "" - os_arch: str = "" - min_idle_runner: str = "" - max_runner: str = "" - labels: str = "" - runner_group: str = "" - enable_shell: str = "" - pre_install_scripts: str = "" - org: str = "" - repo: str = "" - dockerhub_mirror: str = "" - runner_http_proxy: str = "" - aproxy_exclude_addresses: str = "" - aproxy_redirect_ports: str = "" - otel_collector_endpoint: str = "" - pre_job_script: str = "" @dataclasses.dataclass(frozen=True) class ScalesetConfig: """Scaleset and runner fields published by one ``garm-configurator`` application. - The requirer derives this view from the ``ProviderConfig`` it loads from the application - databag, so consumers apply their own parsing and validation without re-reading the - databag. The fields keep the relation's string wire form; an empty string means "unset". + This is the nested ``scaleset`` value of the application databag. The requirer exposes it + as-is, so consumers apply their own parsing and validation without re-reading the databag. + The fields keep the relation's string wire form; an empty string means "unset". These fields are published on the application databag by the configurator's leader unit, so every unit of a deployment sees one consistent payload. @@ -173,6 +130,23 @@ class ScalesetConfig: pre_job_script: str = "" +@pydantic.dataclasses.dataclass +class ConfiguratorApplicationData: + """Complete application databag payload published by one ``garm-configurator`` application. + + This is the write model for the relation: the configurator fills both nested values and + publishes them with one ``ops.Relation.save``, so the application databag holds a + ``provider`` object and a ``scaleset`` object instead of a flat set of keys. + + Attributes: + provider: The provider and GitHub App credentials. + scaleset: The scaleset and runner fields. + """ + + provider: ProviderConfig + scaleset: ScalesetConfig + + @dataclasses.dataclass(frozen=True) class ConfiguratorRelation: """Relation data published by one ``garm-configurator`` application. @@ -194,10 +168,10 @@ def provider_name(self) -> str: """Return the GARM provider name published by the configurator. Returns: - The provider name the configurator wrote to its application databag, matching + The provider name the configurator wrote to the ``scaleset`` value, matching GARM's provider registry and the name its scalesets reference. """ - return self.provider.provider_name + return self.scaleset.provider_name # A scaleset name identifies the scaleset; a provider can legitimately serve several scalesets, @@ -300,9 +274,7 @@ def get_data(self) -> ConfiguratorData: ) continue try: - provider = relation.load( - ProviderConfig, application, decoder=_decode_relation_value - ) + application_data = relation.load(ConfiguratorApplicationData, application) except ValueError: logger.warning( "Ignoring incomplete garm-configurator relation data: relation_id=%d app=%s", @@ -314,13 +286,8 @@ def get_data(self) -> ConfiguratorData: configurator_relation = ConfiguratorRelation( relation_id=relation.id, application_name=application.name, - provider=provider, - scaleset=ScalesetConfig( - **{ - field.name: getattr(provider, field.name) - for field in dataclasses.fields(ScalesetConfig) - } - ), + provider=application_data.provider, + scaleset=application_data.scaleset, ) aggregated.append(configurator_relation) identity = tuple( @@ -349,29 +316,3 @@ def get_data(self) -> ConfiguratorData: relations=tuple(aggregated), invalid_relation_ids=frozenset(invalid_relation_ids), ) - - -def _decode_relation_value(value: str) -> typing.Any: - """Decode one relation databag value, tolerating data from an older configurator. - - ``ops.Relation.save`` JSON-encodes every value, but a configurator upgraded from a charm - that still wrote raw strings only republishes on its next reconcile. Decoding with a - fallback lets the consumer read both forms without treating the relation as invalid. - - Only JSON strings and null are returned as decoded values; numeric JSON stays in its raw - string form so string fields (the scaleset fields keep their string wire form) are not - rejected and integer fields still receive a value pydantic can coerce. - - Args: - value: The raw string stored in the relation databag. - - Returns: - The decoded string or None, or the raw string for any other value. - """ - try: - decoded = json.loads(value) - except (json.JSONDecodeError, TypeError): - return value - if decoded is None or isinstance(decoded, str): - return decoded - return value diff --git a/charms/garm-configurator/src/charm.py b/charms/garm-configurator/src/charm.py index 525d18d7..672bb9cc 100755 --- a/charms/garm-configurator/src/charm.py +++ b/charms/garm-configurator/src/charm.py @@ -9,7 +9,9 @@ import typing import ops +from garm_configurator_relation import ConfiguratorApplicationData from garm_configurator_relation import ProviderConfig as RelationProviderConfig +from garm_configurator_relation import ScalesetConfig as RelationScalesetConfig from charm_state import ( GARM_RELATION_NAME, @@ -139,12 +141,13 @@ def _ensure_relation_secret( def _configure_garm_relation(self, state: CharmState) -> None: """Publish the complete configurator payload to the garm-configurator relation. - Builds one ``RelationProviderConfig`` holding the scaleset fields, runner options, - image reference, and OpenStack/GitHub App credentials, then writes it to the - application databag with a single ``ops.Relation.save`` call. ``Relation.save`` - persists every model field, so an unset optional field is published as an empty string - rather than omitted; consumers treat the empty string as "unset". The ``image_id`` field - carries either an OpenStack image name or ID for compatibility. + Builds a ``ConfiguratorApplicationData`` nesting the provider and GitHub App credentials + under ``provider`` and the scaleset fields, runner options, and image reference under + ``scaleset``, then writes it to the application databag with a single + ``ops.Relation.save`` call. ``Relation.save`` persists every model field, so an unset + optional field is published as an empty string rather than omitted; consumers treat the + empty string as "unset". The ``image_id`` field carries either an OpenStack image name or + ID for compatibility. Also provisions Juju secrets for the OpenStack password and GitHub App private key and writes their secret URIs alongside the credential fields. Secret provisioning is gated @@ -175,7 +178,7 @@ def _configure_garm_relation(self, state: CharmState) -> None: pre_install = state.scaleset_config.pre_install_scripts rc = state.runner_config - basic_data: dict[str, str] = { + scaleset_data: dict[str, str] = { "name": state.scaleset_config.name, "provider_name": state.provider_config.provider_name, "image_id": state.image or "", @@ -199,28 +202,30 @@ def _configure_garm_relation(self, state: CharmState) -> None: "pre_job_script": rc.pre_job_script or "", } if state.scaleset_config.org: - basic_data["org"] = state.scaleset_config.org + scaleset_data["org"] = state.scaleset_config.org if state.scaleset_config.repo: - basic_data["repo"] = state.scaleset_config.repo + scaleset_data["repo"] = state.scaleset_config.repo garm_relation.save( - RelationProviderConfig( - **basic_data, - openstack_auth_url=state.provider_config.auth_url, - openstack_username=state.provider_config.username, - openstack_password_secret_uri=( - str(password_secret.id) if password_secret is not None else None - ), - openstack_project_name=state.provider_config.project_name, - openstack_user_domain_name=state.provider_config.user_domain_name, - openstack_project_domain_name=state.provider_config.project_domain_name, - openstack_region_name=state.provider_config.region_name, - openstack_network=state.provider_config.network, - github_app_id=state.github_app_config.app_id, - github_installation_id=state.github_app_config.installation_id, - github_private_key_secret_uri=( - str(github_key_secret.id) if github_key_secret is not None else None + ConfiguratorApplicationData( + provider=RelationProviderConfig( + openstack_auth_url=state.provider_config.auth_url, + openstack_username=state.provider_config.username, + openstack_password_secret_uri=( + str(password_secret.id) if password_secret is not None else None + ), + openstack_project_name=state.provider_config.project_name, + openstack_user_domain_name=state.provider_config.user_domain_name, + openstack_project_domain_name=state.provider_config.project_domain_name, + openstack_region_name=state.provider_config.region_name, + openstack_network=state.provider_config.network, + github_app_id=state.github_app_config.app_id, + github_installation_id=state.github_app_config.installation_id, + github_private_key_secret_uri=( + str(github_key_secret.id) if github_key_secret is not None else None + ), ), + scaleset=RelationScalesetConfig(**scaleset_data), ), self.app, ) diff --git a/charms/garm-configurator/tests/unit/test_charm.py b/charms/garm-configurator/tests/unit/test_charm.py index 4a96bdb9..798739c0 100644 --- a/charms/garm-configurator/tests/unit/test_charm.py +++ b/charms/garm-configurator/tests/unit/test_charm.py @@ -25,10 +25,18 @@ def _make_garm_configurator_relation(): return Relation(endpoint="garm-configurator") +def _relation_section(relation, section: str) -> dict: + """Return a nested application databag value decoded from its JSON form.""" + return json.loads(relation.local_app_data[section]) + + def _relation_value(relation, key: str): - """Return an application databag value decoded from the JSON form ``Relation.save`` writes.""" - value = relation.local_app_data.get(key) - return "" if value is None else json.loads(value) + """Return a value from the nested ``provider`` or ``scaleset`` application databag value.""" + for section in ("provider", "scaleset"): + data = _relation_section(relation, section) + if key in data: + return data[key] + raise KeyError(key) def _valid_config(secret: Secret, private_key_secret: Secret) -> dict: @@ -118,7 +126,7 @@ def test_configured_image_is_published_without_image_relation(): garm_out = out.get_relation(garm_relation.id) assert out.unit_status == ops.ActiveStatus("Ready") - assert "image" not in garm_out.local_app_data + assert "image" not in _relation_section(garm_out, "scaleset") assert _relation_value(garm_out, "image_id") == "runner-noble-amd64" assert _relation_value(garm_out, "openstack_auth_url") == ( "https://keystone.example.com:5000/v3" @@ -250,7 +258,7 @@ def test_clearing_configured_image_without_relation_withdraws_only_image(): assert _relation_value(garm_out, "openstack_auth_url") == ( "https://keystone.example.com:5000/v3" ) - assert "openstack_password_secret_uri" in garm_out.local_app_data + assert "openstack_password_secret_uri" in _relation_section(garm_out, "provider") assert _relation_value(garm_out, "github_private_key_secret_uri") relation_id = garm_relation.id relation_secrets = { @@ -741,6 +749,12 @@ def test_reconcile_writes_full_config_to_garm_relation(): out = ctx.run(ctx.on.config_changed(), state) garm_out = out.get_relation(garm_relation.id) + assert set(garm_out.local_app_data) == {"provider", "scaleset"} + provider_data = _relation_section(garm_out, "provider") + scaleset_data = _relation_section(garm_out, "scaleset") + assert "name" not in provider_data + assert "openstack_auth_url" not in scaleset_data + # Provider config assert _relation_value(garm_out, "openstack_auth_url") == ( "https://keystone.example.com:5000/v3" @@ -757,7 +771,7 @@ def test_reconcile_writes_full_config_to_garm_relation(): # GitHub config assert _relation_value(garm_out, "github_app_id") == 99999 assert _relation_value(garm_out, "github_installation_id") == 67890 - assert "github_private_key" not in garm_out.local_app_data + assert "github_private_key" not in provider_data assert _relation_value(garm_out, "github_private_key_secret_uri") # Scaleset config @@ -834,7 +848,7 @@ def test_reconcile_writes_optional_scaleset_fields_to_garm_relation(): Image UUID is present, garm relation is joined. act: config-changed fires. assert: org, runner_group, and pre_install_scripts are present in the - garm relation data. repo is published as an empty string. + garm relation data. repo is absent. """ ctx = Context(GarmConfiguratorCharm) secret = _make_secret() @@ -1010,11 +1024,11 @@ def test_runner_config_fields_written_to_garm_configurator_relation(): assert _relation_value(rel_out, "pre_job_script") == "echo start" -def test_runner_config_fields_empty_when_unset(): +def test_runner_config_fields_absent_when_unset(): """ arrange: Valid config with no runner config options set. act: Run config-changed with a garm-configurator relation present. - assert: The six runner config keys are published as empty strings. + assert: The six runner config keys are absent from the databag. """ ctx = Context(GarmConfiguratorCharm) secret = _make_secret() diff --git a/charms/garm-configurator/tests/unit/test_garm_configurator_relation.py b/charms/garm-configurator/tests/unit/test_garm_configurator_relation.py index ef483f9e..945c21ef 100644 --- a/charms/garm-configurator/tests/unit/test_garm_configurator_relation.py +++ b/charms/garm-configurator/tests/unit/test_garm_configurator_relation.py @@ -3,10 +3,12 @@ """Unit tests for the ``garm-configurator`` relation dataclasses.""" +import dataclasses from types import SimpleNamespace import pytest from garm_configurator_relation import ( + ConfiguratorApplicationData, ConfiguratorData, ConfiguratorRelation, GarmConfiguratorRequirer, @@ -22,15 +24,17 @@ def _relation(relation_id: int = 1, scaleset_name: str = "my-scaleset") -> Confi application_name=f"configurator-{relation_id}", provider=ProviderConfig( openstack_auth_url="https://ks.example.com:5000/v3", + ), + scaleset=ScalesetConfig( + name=scaleset_name, provider_name=f"configurator-{relation_id}-0", ), - scaleset=ScalesetConfig(name=scaleset_name), ) def test_configurator_relation_provider_name(): """ - arrange: A configurator relation whose application databag carries a provider name. + arrange: A configurator relation whose scaleset value carries a provider name. act: Read its provider name. assert: The name is the one the configurator published, matching GARM's provider registry and the provider name its scalesets reference. @@ -71,6 +75,50 @@ def test_configurator_data_reports_empty_and_invalid_state( assert data.has_invalid_relations is expected_invalid +def test_application_data_nests_provider_and_scaleset(): + """ + arrange: A complete configurator payload with provider credentials and scaleset fields. + act: Serialise it the way ``ops.Relation.save`` does. + assert: The databag has one ``provider`` object and one ``scaleset`` object, each holding + only its own fields, so consumers parse the two concerns separately. + """ + application_data = ConfiguratorApplicationData( + provider=ProviderConfig( + openstack_auth_url="https://ks.example.com:5000/v3", + github_app_id=123, + ), + scaleset=ScalesetConfig( + name="my-scaleset", + provider_name="configurator-0", + image_id="ubuntu:24.04", + max_runner="6", + ), + ) + + nested = dataclasses.asdict(application_data) + + assert set(nested) == {"provider", "scaleset"} + assert nested["provider"] == { + "openstack_auth_url": "https://ks.example.com:5000/v3", + "openstack_username": None, + "openstack_password": None, + "openstack_password_secret_uri": None, + "openstack_project_name": None, + "openstack_user_domain_name": None, + "openstack_project_domain_name": None, + "openstack_region_name": None, + "openstack_network": None, + "github_app_id": 123, + "github_installation_id": None, + "github_private_key_secret_uri": None, + } + assert nested["scaleset"]["name"] == "my-scaleset" + assert nested["scaleset"]["provider_name"] == "configurator-0" + assert nested["scaleset"]["max_runner"] == "6" + assert "name" not in nested["provider"] + assert "openstack_auth_url" not in nested["scaleset"] + + class _FakeApp: """Minimal related application for exercising the requirer without a live model.""" @@ -82,36 +130,41 @@ class _FakeRelation: id = 1 - def __init__(self, provider: ProviderConfig | None = None) -> None: + def __init__(self, application_data: ConfiguratorApplicationData | None = None) -> None: self.app = _FakeApp() - self.data = {self.app: {"published": "true"}} if provider is not None else {} - self._provider = provider + self.data = {self.app: {"published": "true"}} if application_data is not None else {} + self._application_data = application_data self.loaded_entity = None def load(self, cls, entity, *args, **kwargs): - """Return the provider payload only; the scaleset view must be derived from it.""" - if cls is not ProviderConfig: + """Return the nested application payload loaded from the application databag.""" + if cls is not ConfiguratorApplicationData: raise AssertionError(f"unexpected databag load for {cls.__name__}") self.loaded_entity = entity - return self._provider + return self._application_data -def test_requirer_derives_scaleset_view_from_provider_payload(): +def test_requirer_reads_nested_provider_and_scaleset(): """ - arrange: A configurator relation whose application payload also carries the basic fields. + arrange: A configurator relation whose application databag holds nested provider and + scaleset values. act: Aggregate the relation data. - assert: The payload is read from the application databag and the scaleset view carries the - same basic fields without re-reading the databag, so the configurator's single save - remains the source of truth. + assert: The payload is read from the application databag and the relation exposes each + nested value unchanged, so the configurator's single save remains the source of truth. """ - provider = ProviderConfig( - openstack_auth_url="https://ks.example.com:5000/v3", - provider_name="configurator-0", - name="my-scaleset", - org="myorg", - max_runner="6", + application_data = ConfiguratorApplicationData( + provider=ProviderConfig( + openstack_auth_url="https://ks.example.com:5000/v3", + github_app_id=123, + ), + scaleset=ScalesetConfig( + name="my-scaleset", + provider_name="configurator-0", + org="myorg", + max_runner="6", + ), ) - relation = _FakeRelation(provider) + relation = _FakeRelation(application_data) charm = SimpleNamespace(model=SimpleNamespace(relations={"garm-configurator": [relation]})) data = GarmConfiguratorRequirer(charm).get_data() @@ -120,6 +173,7 @@ def test_requirer_derives_scaleset_view_from_provider_payload(): assert len(data.relations) == 1 assert data.relations[0].application_name == "configurator" assert data.relations[0].provider_name == "configurator-0" + assert data.relations[0].provider.openstack_auth_url == "https://ks.example.com:5000/v3" assert data.relations[0].scaleset.name == "my-scaleset" assert data.relations[0].scaleset.org == "myorg" assert data.relations[0].scaleset.max_runner == "6" diff --git a/charms/garm/requirements.txt b/charms/garm/requirements.txt index 11845955..a28dc2fe 100644 --- a/charms/garm/requirements.txt +++ b/charms/garm/requirements.txt @@ -9,4 +9,4 @@ pydantic==2.14.3 typing-extensions==4.16.0 owasp-logger==0.1.6 # garm_configurator/v0.0.1 -garm-configurator-relation @ git+https://github.com/canonical/github-runner-operators.git@ac732047f070ffcaeae85fe679c8a6092aa564c7#subdirectory=charms/garm-configurator/lib +garm-configurator-relation @ git+https://github.com/canonical/github-runner-operators.git@1a68829a8a7f10a38879850cad030d64f2f5aa16#subdirectory=charms/garm-configurator/lib diff --git a/charms/garm/tests/unit/test_charm.py b/charms/garm/tests/unit/test_charm.py index 7b8d2a46..d3d58a44 100644 --- a/charms/garm/tests/unit/test_charm.py +++ b/charms/garm/tests/unit/test_charm.py @@ -99,20 +99,7 @@ "openstack_network": "net1", } - -def _provider_data(**overrides: str) -> dict[str, str]: - """Return provider fields JSON-encoded the way the configurator publishes them.""" - values = { - **_PROVIDER_VALUES, - "provider_name": "garm-configurator-0", - **overrides, - } - return {key: json.dumps(value) for key, value in values.items()} - - -_PROVIDER_UNIT_DATA = _provider_data() - -_SCALESET_UNIT_DATA = { +_SCALESET_VALUES = { "name": "my-scaleset", "provider_name": "garm-configurator-0", "image_id": "img-1", @@ -123,6 +110,31 @@ def _provider_data(**overrides: str) -> dict[str, str]: "repo": "myorg/myrepo", } + +def _provider_values(**overrides: str) -> dict[str, str]: + """Return the provider fields a configurator application publishes.""" + return {**_PROVIDER_VALUES, **overrides} + + +def _scaleset_values(**overrides: str) -> dict[str, str]: + """Return the scaleset fields a configurator application publishes.""" + return {**_SCALESET_VALUES, **overrides} + + +def _provider_data(**overrides: str) -> dict[str, str]: + """Return the nested ``provider`` value JSON-encoded the way the configurator publishes it.""" + return {"provider": json.dumps(_provider_values(**overrides))} + + +def _scaleset_data(**overrides: str) -> dict[str, str]: + """Return the nested ``scaleset`` value JSON-encoded the way the configurator publishes it.""" + return {"scaleset": json.dumps(_scaleset_values(**overrides))} + + +_PROVIDER_UNIT_DATA = _provider_data() + +_SCALESET_UNIT_DATA = _scaleset_data() + _TEMPLATE_ID = 99 _AGENT_VERSION = "v0.1.1" @@ -321,10 +333,8 @@ def _configurator_relation(app_name: str) -> Relation: endpoint=GARM_CONFIGURATOR_RELATION_NAME, remote_app_name=app_name, remote_app_data={ - **_provider_data(provider_name=f"{app_name}-0"), - **_SCALESET_UNIT_DATA, - "name": f"scaleset-{app_name}", - "provider_name": f"{app_name}-0", + **_PROVIDER_UNIT_DATA, + **_scaleset_data(name=f"scaleset-{app_name}", provider_name=f"{app_name}-0"), }, ) @@ -576,23 +586,16 @@ def test_multiple_configurator_applications_are_supported(ctx: Context, garm_api endpoint=GARM_CONFIGURATOR_RELATION_NAME, remote_app_name="configurator-a", remote_app_data={ - **_provider_data(provider_name="configurator-a-0"), - **_SCALESET_UNIT_DATA, - "name": "scaleset-a", - "provider_name": "configurator-a-0", + **_PROVIDER_UNIT_DATA, + **_scaleset_data(name="scaleset-a", provider_name="configurator-a-0"), }, ), Relation( endpoint=GARM_CONFIGURATOR_RELATION_NAME, remote_app_name="configurator-b", remote_app_data={ - **_provider_data( - openstack_auth_url="https://ks2.example.com:5000/v3", - provider_name="configurator-b-0", - ), - **_SCALESET_UNIT_DATA, - "name": "scaleset-b", - "provider_name": "configurator-b-0", + **_provider_data(openstack_auth_url="https://ks2.example.com:5000/v3"), + **_scaleset_data(name="scaleset-b", provider_name="configurator-b-0"), }, ), ] @@ -1263,10 +1266,13 @@ def test_invalid_configurator_relation_blocks_the_update(ctx: Context, garm_api: assert: The unit blocks on the invalid relation and nothing is applied, so a partial provider is never rendered and its scalesets are not pruned. """ - provider_data = _provider_data() - provider_data.pop("openstack_auth_url") + provider = _provider_values() + provider.pop("openstack_auth_url") state = _state( - configurator_app_data={**provider_data, **_SCALESET_UNIT_DATA}, + configurator_app_data={ + "provider": json.dumps(provider), + **_SCALESET_UNIT_DATA, + }, recorded_providers=[_provider_config("garm-configurator-0")], ) @@ -1292,18 +1298,16 @@ def test_conflicting_configurator_scalesets_block_the_update( endpoint=GARM_CONFIGURATOR_RELATION_NAME, remote_app_name="configurator-a", remote_app_data={ - **_provider_data(provider_name="configurator-a-0"), - **_SCALESET_UNIT_DATA, - "provider_name": "configurator-a-0", + **_PROVIDER_UNIT_DATA, + **_scaleset_data(provider_name="configurator-a-0"), }, ) second = Relation( endpoint=GARM_CONFIGURATOR_RELATION_NAME, remote_app_name="configurator-b", remote_app_data={ - **_provider_data(provider_name="configurator-b-0"), - **_SCALESET_UNIT_DATA, - "provider_name": "configurator-b-0", + **_PROVIDER_UNIT_DATA, + **_scaleset_data(provider_name="configurator-b-0"), }, ) @@ -1791,15 +1795,14 @@ def test_charmed_template_error_degrades_to_waiting(ctx: Context, garm_api: _Gar # --- Desired state from relation data ----------------------------------------------------- -def _github_unit_data(secret_id: str, app_id: str = "12345", installation_id: str = "67890"): - """Build the GitHub App fields a configurator application publishes.""" +def _github_provider_values( + secret_id: str, app_id: str = "12345", installation_id: str = "67890" +) -> dict[str, str]: + """Build the GitHub App provider fields a configurator application publishes.""" return { - key: json.dumps(value) - for key, value in { - "github_app_id": app_id, - "github_installation_id": installation_id, - "github_private_key_secret_uri": secret_id, - }.items() + "github_app_id": app_id, + "github_installation_id": installation_id, + "github_private_key_secret_uri": secret_id, } @@ -1813,7 +1816,10 @@ def test_github_credential_is_built_from_relation_data(ctx: Context, garm_api: _ """ key_secret = Secret(tracked_content={"value": "PEMDATA"}) state = _state( - configurator_app_data={**_PROVIDER_UNIT_DATA, **_github_unit_data(key_secret.id)}, + configurator_app_data={ + **_provider_data(**_github_provider_values(key_secret.id)), + **_SCALESET_UNIT_DATA, + }, secrets=[key_secret], ) @@ -1846,8 +1852,7 @@ def test_github_credential_is_built_from_json_encoded_relation_data( key_secret = Secret(tracked_content={"value": "PEMDATA"}) state = _state( configurator_app_data={ - **_PROVIDER_UNIT_DATA, - **_github_unit_data(key_secret.id), + **_provider_data(**_github_provider_values(key_secret.id)), **_SCALESET_UNIT_DATA, }, secrets=[key_secret], @@ -1878,23 +1883,23 @@ def test_applications_sharing_a_github_app_yield_one_credential( assert: They collapse to a single credential. """ key_secret = Secret(tracked_content={"value": "PEMDATA"}) - github_data = _github_unit_data(key_secret.id, "1", "2") + github_data = _github_provider_values(key_secret.id, "1", "2") state = _state( configurator_relations=[ Relation( endpoint=GARM_CONFIGURATOR_RELATION_NAME, remote_app_name="configurator-a", remote_app_data={ - **_provider_data(provider_name="configurator-a-0"), - **github_data, + **_provider_data(**github_data), + **_scaleset_data(name="scaleset-a", provider_name="configurator-a-0"), }, ), Relation( endpoint=GARM_CONFIGURATOR_RELATION_NAME, remote_app_name="configurator-b", remote_app_data={ - **_provider_data(provider_name="configurator-b-0"), - **github_data, + **_provider_data(**github_data), + **_scaleset_data(name="scaleset-b", provider_name="configurator-b-0"), }, ), ], @@ -1910,8 +1915,11 @@ def test_applications_sharing_a_github_app_yield_one_credential( @pytest.mark.parametrize( "github_data", [ - pytest.param({"github_app_id": json.dumps("1")}, id="missing-fields"), - pytest.param(_github_unit_data("secret:unavailable"), id="unreadable-key-secret"), + pytest.param(_provider_data(github_app_id="1"), id="missing-fields"), + pytest.param( + _provider_data(**_github_provider_values("secret:unavailable")), + id="unreadable-key-secret", + ), ], ) def test_incomplete_github_config_yields_no_credential( @@ -1923,7 +1931,7 @@ def test_incomplete_github_config_yields_no_credential( act: Run update-status. assert: No credential is built — GARM rejects a partial one. """ - state = _state(configurator_app_data={**_PROVIDER_UNIT_DATA, **github_data}) + state = _state(configurator_app_data={**github_data, **_SCALESET_UNIT_DATA}) ctx.run(ctx.on.update_status(), state) @@ -1956,9 +1964,9 @@ def test_stale_image_key_does_not_prevent_scaleset_removal(ctx: Context, garm_ap act: Run update-status. assert: GARM ignores the stale key and removes the scale set. """ - scaleset_data = {**_SCALESET_UNIT_DATA, "image": "stable-image"} - scaleset_data.pop("image_id") - data = {**_PROVIDER_UNIT_DATA, **scaleset_data} + scaleset = _scaleset_values(image="stable-image") + scaleset.pop("image_id") + data = {**_PROVIDER_UNIT_DATA, "scaleset": json.dumps(scaleset)} ctx.run(ctx.on.update_status(), _state(configurator_app_data=data)) @@ -1973,12 +1981,17 @@ def test_missing_image_reconciles_existing_scaleset_away(ctx: Context, garm_api: assert: GARM remains provider-ready and reconciles an empty scale-set list, removing the old scale set instead of waiting for configurator data. """ - scaleset_data = {**_SCALESET_UNIT_DATA} - scaleset_data.pop("image_id") + scaleset = _scaleset_values() + scaleset.pop("image_id") out = ctx.run( ctx.on.update_status(), - _state(configurator_app_data={**_PROVIDER_UNIT_DATA, **scaleset_data}), + _state( + configurator_app_data={ + **_PROVIDER_UNIT_DATA, + "scaleset": json.dumps(scaleset), + } + ), ) garm_api.scaleset.return_value.reconcile.assert_called_once_with([]) @@ -2101,9 +2114,8 @@ def test_remote_shell_follows_the_configurator_databag( act: Run update-status. assert: The scaleset spec carries the requested setting, defaulting to off. """ - data = {**_PROVIDER_UNIT_DATA, **_SCALESET_UNIT_DATA} - if databag_value is not None: - data["enable_shell"] = databag_value + scaleset = {} if databag_value is None else {"enable_shell": databag_value} + data = {**_PROVIDER_UNIT_DATA, **_scaleset_data(**scaleset)} ctx.run(ctx.on.update_status(), _state(configurator_app_data=data)) specs = garm_api.scaleset.return_value.reconcile.call_args[0][0] @@ -2124,9 +2136,8 @@ def test_image_visibility_follows_the_configurator_databag( act: Run update-status. assert: The scaleset spec carries the visibility, or none so the provider default applies. """ - data = {**_PROVIDER_UNIT_DATA, **_SCALESET_UNIT_DATA} - if databag_value is not None: - data["image_visibility"] = databag_value + scaleset = {} if databag_value is None else {"image_visibility": databag_value} + data = {**_PROVIDER_UNIT_DATA, **_scaleset_data(**scaleset)} ctx.run(ctx.on.update_status(), _state(configurator_app_data=data)) specs = garm_api.scaleset.return_value.reconcile.call_args[0][0] @@ -2147,9 +2158,8 @@ def test_security_group_follows_the_configurator_databag( act: Run update-status. assert: The scaleset spec carries the group, or none so the provider's default applies. """ - data = {**_PROVIDER_UNIT_DATA, **_SCALESET_UNIT_DATA} - if databag_value is not None: - data["security_group"] = databag_value + scaleset = {} if databag_value is None else {"security_group": databag_value} + data = {**_PROVIDER_UNIT_DATA, **_scaleset_data(**scaleset)} ctx.run(ctx.on.update_status(), _state(configurator_app_data=data)) specs = garm_api.scaleset.return_value.reconcile.call_args[0][0]