Skip to content

Commit

Permalink
Support snap-installation resource which includes yaml and snap files
Browse files Browse the repository at this point in the history
  • Loading branch information
addyess committed Sep 20, 2024
1 parent b6e97e6 commit 61ad363
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
21 changes: 21 additions & 0 deletions charms/worker/k8s/src/snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@ def _overridden_snap_installation() -> Path:
return Path("./snap-installation/resource/snap_installation.yaml")


def _normalize_paths(snap_installation):
"""Normalize the paths in the snap_installation manifest.
Arguments:
snap_installation: The path to the snap_installation manifest
"""
snap_installation = snap_installation.resolve()
content = yaml.safe_load(snap_installation.read_text(encoding="utf-8"))
updated = False
for arch, snaps in content.items():
for idx, snap in enumerate(snaps):
if snap.get("filename"):
resolved = (snap_installation.parent / snap["filename"]).resolve()
log.info("Resolving snap filename: %s to %s", snap["filename"], resolved)
content[arch][idx]["filename"] = str(resolved)
updated = True
if updated:
yaml.safe_dump(content, snap_installation.open(mode="w", encoding="utf-8"))


def _select_snap_installation(charm: ops.CharmBase) -> Path:
"""Select the snap_installation manifest.
Expand Down Expand Up @@ -161,6 +181,7 @@ def _select_snap_installation(charm: ops.CharmBase) -> Path:
snap_installation = unpack_path / "snap_installation.yaml"
if snap_installation.exists():
log.info("Found snap_installation manifest")
_normalize_paths(snap_installation)
return snap_installation

snap_path = list(unpack_path.glob("*.snap"))
Expand Down
35 changes: 32 additions & 3 deletions charms/worker/k8s/tests/unit/test_snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
import subprocess
import tarfile
from pathlib import Path
from textwrap import dedent
from unittest import mock

import ops
import ops.testing
import pytest
import snap
from charm import K8sCharm
Expand Down Expand Up @@ -134,25 +135,53 @@ def _create_gzip_tar_string(file_data_dict):
return gzip_buffer.getvalue()


def test_resource_supplied_installation(harness):
def test_resource_supplied_installation_by_channel(harness):
"""Test file cannot be parsed."""
arch = snap._local_arch()
yaml_data = f"{arch}:\n- install-type: store\n name: k8s\n channel: edge"
file_data = {"./snap_installation.yaml": yaml_data}
harness.add_resource("snap-installation", _create_gzip_tar_string(file_data))
args = snap._parse_management_arguments(harness.charm)
assert len(args) == 1
assert isinstance(args[0], snap.SnapStoreArgument)
assert args[0].channel == "edge"
assert args[0].name == "k8s"
assert args[0].install_type == "store"


def test_resource_supplied_snap(harness):
def test_resource_supplied_installation_by_filename(harness, resource_snap_installation):
"""Test file cannot be parsed."""
arch = snap._local_arch()
yaml_data = dedent(
f"""
{arch}:
- install-type: file
name: k8s
filename: ./k8s_xxxx.snap
dangerous: true
"""
).strip()
file_data = {"./snap_installation.yaml": yaml_data, "./k8s_xxxx.snap": ""}
harness.add_resource("snap-installation", _create_gzip_tar_string(file_data))
args = snap._parse_management_arguments(harness.charm)
assert len(args) == 1
assert isinstance(args[0], snap.SnapFileArgument)
assert args[0].install_type == "file"
assert args[0].name == "k8s"
assert args[0].filename == resource_snap_installation.parent / "k8s_xxxx.snap"
assert args[0].dangerous


def test_resource_supplied_snap(harness, resource_snap_installation):
"""Test file cannot be parsed."""
file_data = {"./k8s_xxxx.snap": ""}
harness.add_resource("snap-installation", _create_gzip_tar_string(file_data))
args = snap._parse_management_arguments(harness.charm)
assert len(args) == 1
assert isinstance(args[0], snap.SnapFileArgument)
assert args[0].name == "k8s"
assert args[0].install_type == "file"
assert args[0].filename == resource_snap_installation.parent / "k8s_xxxx.snap"
assert args[0].dangerous


Expand Down

0 comments on commit 61ad363

Please sign in to comment.