From 19184db0c826da331f82b9933b8c95f8b2a16a28 Mon Sep 17 00:00:00 2001 From: Alex Lowe Date: Mon, 23 Sep 2024 12:58:01 -0400 Subject: [PATCH] tests: create integration tests for talking to snapd --- craft_parts/packages/snaps.py | 5 +- tests/conftest.py | 6 +++ tests/integration/packages/test_snaps.py | 60 ++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 tests/integration/packages/test_snaps.py diff --git a/craft_parts/packages/snaps.py b/craft_parts/packages/snaps.py index b8e01bfee..2e983851c 100644 --- a/craft_parts/packages/snaps.py +++ b/craft_parts/packages/snaps.py @@ -19,6 +19,7 @@ import contextlib import logging import os +import pathlib import subprocess import sys from collections.abc import Iterator, Sequence @@ -200,7 +201,7 @@ def is_valid(self) -> bool: store_channels = self._get_store_channels() return self.channel in store_channels - def download(self, *, directory: str | None = None) -> None: + def download(self, *, directory: str | pathlib.Path | None = None) -> None: """Download a given snap.""" # We use the `snap download` command here on recommendation # of the snapd team. @@ -270,7 +271,7 @@ def refresh(self) -> None: self._is_installed = None -def download_snaps(*, snaps_list: Sequence[str], directory: str) -> None: +def download_snaps(*, snaps_list: Sequence[str], directory: str | pathlib.Path) -> None: """Download snaps of the format / into directory. The target directory is created if it does not exist. diff --git a/tests/conftest.py b/tests/conftest.py index b44e252c8..0c3f95b0d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -45,6 +45,12 @@ def new_dir(monkeypatch, tmpdir): return tmpdir +@pytest.fixture +def new_path(monkeypatch, tmp_path): + monkeypatch.chdir(tmp_path) + return tmp_path + + @pytest.fixture def tmp_homedir_path(): """A non-hidden temporary directory in the user's home directory. diff --git a/tests/integration/packages/test_snaps.py b/tests/integration/packages/test_snaps.py new file mode 100644 index 000000000..6d791cf18 --- /dev/null +++ b/tests/integration/packages/test_snaps.py @@ -0,0 +1,60 @@ +# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- +# +# Copyright 2024 Canonical Ltd. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 3 as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see . +"""Integration tests for interacting with snapd.""" + +import pathlib +from typing import Sequence +import pytest +import pytest_check + +from craft_parts.packages import snaps + + +def test_get_installed_snaps_success(): + """Test that get_installed_snaps returns a list of snaps.""" + actual = snaps.get_installed_snaps() + + for snap in actual: + name, _, revision = snap.partition("=") + pytest_check.is_true(len(name) >= 1) + if revision.startswith("x"): + # Locally installed snaps should be of the form "x" + with pytest_check.check(): + int(revision[1:]) + else: + # Store-instaled snaps shoudl simply have an integer revision. + with pytest_check.check(): + int(revision) + + +@pytest.mark.parametrize( + "snaps_list", + [ + {"snapcraft", "ruff"}, + {"snapcraft/7.x/stable"}, + ] +) +def test_download_snaps_success(new_path: pathlib.Path, snaps_list: Sequence[str]): + + snaps.download_snaps( + snaps_list=snaps_list, + directory=new_path + ) + + for snap in snaps_list: + snap_name, _, snap_channel = snap.partition("/") + assert len(list(new_path.glob(f"{snap_name}*.snap"))) == 1 + assert len(list(new_path.glob(f"{snap_name}*.assert"))) == 1