Skip to content

Commit

Permalink
Render CharmURL to yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
addyess committed Dec 16, 2024
1 parent 963c140 commit 01cf131
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions tests/integration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from functools import cache, cached_property
from itertools import chain
from pathlib import Path
from typing import Dict, List, Mapping, Optional, Tuple
from typing import Dict, List, Mapping, Optional, Set, Tuple

import yaml
from juju import unit
Expand Down Expand Up @@ -267,13 +267,19 @@ def craft(cls, name: str, series: str, arch: str) -> "CharmUrl":
name = m.group("charm")
return cls(name, series, arch)

def __str__(self) -> str:
"""Return the charm URL.
@staticmethod
def representer(dumper: yaml.Dumper, data: "CharmUrl") -> yaml.ScalarNode:
"""Yaml representer for the CharmUrl object.
Args:
dumper: yaml dumper
data: CharmUrl object
Returns:
string: charm URL
yaml.ScalarNode: yaml node
"""
return f"ch:{self.arch}/{self.series}/{self.name}"
repr = f"ch:{data.arch}/{data.series}/{data.name}"
return dumper.represent_scalar("tag:yaml.org,2002:str", repr)


@dataclass
Expand Down Expand Up @@ -423,7 +429,7 @@ def content(self) -> Mapping:
loaded = yaml.safe_load(self.path.read_bytes())
series = loaded.get("series", "focal")
for app in loaded["applications"].values():
app["charm"] = CharmUrl(app["charm"], series=series, arch=self.arch)
app["charm"] = CharmUrl.craft(app["charm"], series=series, arch=self.arch)
self._content = loaded
return self._content

Expand Down Expand Up @@ -544,7 +550,7 @@ def render(self, tmp_path: Path) -> Path:
"""
target = tmp_path / "bundles" / self.path.name
target.parent.mkdir(exist_ok=True, parents=True)
yaml.safe_dump(self.content, target.open("w"))
yaml.dump(self.content, target.open("w"))
return target


Expand All @@ -560,11 +566,11 @@ async def cloud_arch(ops_test: OpsTest) -> str:
assert ops_test.model, "Model must be present"
controller = await ops_test.model.get_controller()
controller_model = await controller.get_model("controller")
arch = set(
arch: Set[str] = {
machine.safe_data["hardware-characteristics"]["arch"]
for machine in controller_model.machines.values()
)
return arch.pop()
}
return arch.pop().strip()


async def cloud_type(ops_test: OpsTest) -> Tuple[str, bool]:
Expand All @@ -586,3 +592,6 @@ async def cloud_type(ops_test: OpsTest) -> Tuple[str, bool]:
if _type == "lxd":
vms = not ops_test.request.config.getoption("--lxd-containers")
return _type, vms


yaml.add_representer(CharmUrl, CharmUrl.representer)

0 comments on commit 01cf131

Please sign in to comment.