Skip to content

Commit

Permalink
Merge pull request #18 from rpm-software-management/add-cache-and-tests
Browse files Browse the repository at this point in the history
Add cache and tests
  • Loading branch information
dcermak authored Sep 2, 2024
2 parents ca1bd2b + a58349e commit fcc2920
Show file tree
Hide file tree
Showing 8 changed files with 373 additions and 96 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/cache_up_to_date.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
name: Check that the cached aliases are up to date

on:
schedule:
- cron: "44 4 */2 * *"
pull_request:

jobs:
cache_check:
name: Check the cached constant
runs-on: "ubuntu-latest"
strategy:
fail-fast: false

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.x"

- uses: actions/cache@v4
with:
path: ~/.cache/pypoetry/virtualenvs
key: poetry-${{ hashFiles('poetry.lock') }}

- run: sudo pipx install poetry
env:
PIPX_BIN_DIR: /usr/bin

- name: Install dependencies
run: poetry install

- name: Run the test checking the cache
run: poetry run pytest -vv -k cache_up_to_date
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,38 @@ jobs:
poetry install
poetry run ruff format --check --diff .
tests:
name: Run the integration/smoke tests
runs-on: "ubuntu-latest"
strategy:
fail-fast: false
matrix:
python_version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python_version }}
- uses: actions/cache@v4
with:
path: ~/.cache/pypoetry/virtualenvs
key: poetry-${{ hashFiles('poetry.lock') }}

- run: sudo pipx install poetry
env:
PIPX_BIN_DIR: /usr/bin

- name: Install dependencies
run: poetry install

- name: Run the tests
run: poetry run pytest -vv

build:
name: Build the package
runs-on: ubuntu-latest
needs: tests

steps:
- uses: actions/checkout@v4
Expand Down
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ Get the corresponding main development project in the `Open Build Service
>>> [f"{d.name}: {d.obs_project_name}" for d in aliases["opensuse-all"]]
['openSUSE Leap: openSUSE:Leap:15.6', 'openSUSE Leap: openSUSE:Leap:15.5', 'openSUSE Leap Micro: openSUSE:Leap:Micro:6.0', 'openSUSE Leap Micro: openSUSE:Leap:Micro:5.5', 'openSUSE Tumbleweed: openSUSE:Factory']
The caveat of the function ``get_distro_aliases`` is that it performs network
calls, which can be slow, flaky or otherwise unavailable (e.g. in koji or OBS
builds). Since the openSUSE distribution releases are quite seldom, we provide
the pre-fetched result of ``get_distro_aliases`` as the constant
``CACHED_ACTIVE_DISTRIBUTION_ALIASES``. The only difference between the
pre-fetched and the fetched value should be the version of openSUSE Tumbleweed
(this version changes daily, but has otherwise little significance).
287 changes: 195 additions & 92 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "opensuse-distro-aliases"
version = "0.1.3"
version = "0.2.0"
description = "Aliases for active openSUSE releases"
authors = ["Dan Čermák <[email protected]>"]
license = "MIT"
Expand All @@ -17,6 +17,7 @@ mypy = ">=1.10"
ruff = ">=0.5"
twine = ">=5.1"
types-requests = ">=2.25"
pytest = ">=8.3.2"

[build-system]
requires = ["poetry-core"]
Expand Down
65 changes: 62 additions & 3 deletions src/opensuse_distro_aliases/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

import requests

__all__ = ["Distro", "get_distro_aliases"]
__all__ = ["Distro", "get_distro_aliases", "CACHED_ACTIVE_DISTRIBUTION_ALIASES"]


@dataclass
@dataclass(frozen=True)
class Distro:
"""An openSUSE distribution"""

Expand All @@ -28,6 +28,9 @@ class Distro:
#: main project on build.opensuse.org from which this distribution is built
obs_project_name: Optional[str]

#: flag whether this distribution is still under active maintenance
active: bool = True


class _Release(TypedDict):
name: str
Expand Down Expand Up @@ -71,14 +74,15 @@ def get_distro_aliases(include_eol: bool = False) -> Dict[str, List[Distro]]:
version=d["version"],
namever="opensuse-tumbleweed",
obs_project_name="openSUSE:Factory",
active=True,
)
)

else:
matching_products = productlist.findall(f"product[@name='{distro_name}']")

for distri in distro_list:
if include_eol or distri.get("state", "") != "EOL":
if (active := distri.get("state", "") != "EOL") or include_eol:
version = distri["version"]
obs_project = [
op
Expand All @@ -95,6 +99,7 @@ def get_distro_aliases(include_eol: bool = False) -> Dict[str, List[Distro]]:
# stay compatible with mock chroot names
namever=f"{n.lower().replace(' ', '-')}-{version}",
obs_project_name=obs_project[0] if obs_project else None,
active=active,
)
)

Expand All @@ -107,3 +112,57 @@ def get_distro_aliases(include_eol: bool = False) -> Dict[str, List[Distro]]:
res["opensuse-all"] = opensuse_all

return res


#: Cached openSUSE distribution aliases. Only includes distributions that are
#: active/maintained
#:
#: This constant is a pre-fetched result of :py:func:`get_distro_aliases` with
#: the only difference that the :py:attr:`Distro.version` of openSUSE Tumbleweed
#: is clipped.
#:
#: This constant is periodically updated and should be quite up to date most of
#: the time as most openSUSE releases don't change very often.
CACHED_ACTIVE_DISTRIBUTION_ALIASES: Dict[str, List[Distro]] = {
"opensuse-leap-all": (
_leap_all := [
Distro(
name="openSUSE Leap",
version="15.6",
namever="opensuse-leap-15.6",
obs_project_name="openSUSE:Leap:15.6",
),
Distro(
name="openSUSE Leap",
version="15.5",
namever="opensuse-leap-15.5",
obs_project_name="openSUSE:Leap:15.5",
),
]
),
"opensuse-leap-micro-all": (
_leap_micro_all := [
Distro(
name="openSUSE Leap Micro",
version="6.0",
namever="opensuse-leap-micro-6.0",
obs_project_name="openSUSE:Leap:Micro:6.0",
),
Distro(
name="openSUSE Leap Micro",
version="5.5",
namever="opensuse-leap-micro-5.5",
obs_project_name="openSUSE:Leap:Micro:5.5",
),
]
),
"opensuse-tumbleweed-all": [
_tw := Distro(
name="openSUSE Tumbleweed",
version="",
namever="opensuse-tumbleweed",
obs_project_name="openSUSE:Factory",
)
],
"opensuse-all": [*_leap_all, *_leap_micro_all, _tw],
}
Empty file added tests/__init__.py
Empty file.
41 changes: 41 additions & 0 deletions tests/test_distro_aliases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from typing import Set

from opensuse_distro_aliases import CACHED_ACTIVE_DISTRIBUTION_ALIASES
from opensuse_distro_aliases import Distro
from opensuse_distro_aliases import get_distro_aliases


def test_cache_up_to_date() -> None:
# remove the version field from Tumbleweed, we don't want to precache that
# value
current_version = get_distro_aliases()
for _, distros in current_version.items():
for i, distri in enumerate(distros):
if distri.name == "openSUSE Tumbleweed":
kwargs = distri.__dict__
kwargs["version"] = ""
distros[i] = Distro(**kwargs)

assert current_version == CACHED_ACTIVE_DISTRIBUTION_ALIASES


def test_distro_aliases_without_eol() -> None:
for _, distros in get_distro_aliases(include_eol=False).items():
for distri in distros:
assert distri.active


def test_distro_aliases_with_eol_includes_all_active_ones() -> None:
all_distris = get_distro_aliases(include_eol=True)
active_distris = get_distro_aliases(include_eol=False)

assert set(all_distris.keys()) == set(active_distris.keys())

for distro_group in all_distris:
cur_distro_group: Set[Distro] = set(all_distris[distro_group])
active_distro_group: Set[Distro] = set(active_distris[distro_group])

assert active_distro_group.issubset(cur_distro_group)

for eol_distro in cur_distro_group.difference(active_distro_group):
assert not eol_distro.active

0 comments on commit fcc2920

Please sign in to comment.