Sunil Mohan Adapa fecd6a3577
upgrades: Overhaul detection of distribution
- Move some utilities to utils.py from distupgrade.py and __init__.py.

- This fixes issues with apt preferences being set on unstable
distribution (despite code that tries to prevent it).

- There is no way to distinguish between 'testing' and 'unstable' distributions
in Debian using commands like lsb_release (powered by /etc/os-release). See:
https://lwn.net/Articles/984635/ . So, use the value set in
/etc/apt/sources.list.

Tests: (tested entire patchset)

- Deluge can be installed in trixie.

- Auto-distribution upgrade button is checked during setup on stable and
oldstable but not on testing and unstable.

- Auto-distribution upgrade button is enabled in the form on stable and
oldstable but not on testing and unstable.

- Backports wizard step is skipped on unstable (non-develop mode), but not on
oldstable, stable, testing, and unstable (develop mode).

- If backports are not activated during first wizard, then backports can be
activated on upgrades app page if distribution is oldstable, stable, testing, or
unstable (non-develop mode) but not unstable (develop mode).

- During re-run of setup, setting up backport sources is skipped if already
setup.

- Backports sources files are not added in testing (non-develop) and
unstable (non-develop) distributions. Backports sources are added to oldstable,
stable, testing (develop) and unstable (develop). Unstable sources sources are
not added to unstable but added to oldstable, stable, and testing.

- Backports sources file is added with correct code name bookworm/trixie for
oldstable, stable, and testing distributions.

- When backports sources is set to 'bookworm-backports' on Trixie distribution,
re-running setup updates them to 'trixie-backports'.

- Preferences files are added in oldstable, stable, and testing distributions
but not unstable.

- If unstable and another distro is present in apt sources, then it is treated
as unstable as shown in the distribution upgrade page.

- Current codename is shown properly from sources.list in oldstable, stable,
testing, and unstable in distribution upgrade page.

- NOT TESTED: If distribution upgrade is interrupted, then continue page is
shown.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2025-09-08 19:33:33 -04:00

96 lines
3.4 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Test various upgrade related utilities.
"""
from unittest.mock import patch
from .. import utils
def test_get_sources_list_codename(tmp_path):
"""Test retrieving codename from sources.list file."""
list1 = '''
deb http://deb.debian.org/debian bookworm main non-free-firmware
deb-src http://deb.debian.org/debian bookworm main non-free-firmware
deb http://deb.debian.org/debian bookworm-updates main non-free-firmware
deb-src http://deb.debian.org/debian bookworm-updates main non-free-firmware
deb http://security.debian.org/debian-security/ bookworm-security main non-free-firmware
deb-src http://security.debian.org/debian-security/ bookworm-security main non-free-firmware
''' # noqa: E501
list2 = '''
deb http://deb.debian.org/debian stable main non-free-firmware
deb-src http://deb.debian.org/debian stable main non-free-firmware
deb http://deb.debian.org/debian bookworm-updates main non-free-firmware
deb-src http://deb.debian.org/debian bookworm-updates main non-free-firmware
deb http://security.debian.org/debian-security/ bookworm-security main non-free-firmware
deb-src http://security.debian.org/debian-security/ bookworm-security main non-free-firmware
''' # noqa: E501
list3 = '''
deb http://deb.debian.org/debian unstable main
deb http://deb.debian.org/debian trixie main
'''
list4 = '''
deb http://deb.debian.org/debian sid main
deb http://deb.debian.org/debian trixie main
'''
list5 = '''
deb http://deb.debian.org/debian testing main
deb http://deb.debian.org/debian trixie main
'''
sources_list = tmp_path / 'sources.list'
module = 'plinth.modules.upgrades.utils'
with patch(f'{module}.sources_list', sources_list):
sources_list.write_text(list1)
assert utils.get_sources_list_codename() == 'bookworm'
sources_list.write_text(list2)
assert utils.get_sources_list_codename() is None
sources_list.write_text(list3)
assert utils.get_sources_list_codename() == 'unstable'
sources_list.write_text(list4)
assert utils.get_sources_list_codename() == 'unstable'
sources_list.write_text(list5)
assert utils.get_sources_list_codename() == 'testing'
@patch('subprocess.check_output')
def test_get_current_release(check_output):
"""Test that getting current release works."""
check_output.return_value = b'test-release\ntest-codename\n\n'
assert utils.get_current_release() == ('test-release', 'test-codename')
@patch('plinth.modules.upgrades.utils.get_sources_list_codename')
def test_is_distribution_unstable(get_sources_list_codename):
"""Test that checking for unstable distribution works."""
get_sources_list_codename.return_value = 'unstable'
assert utils.is_distribution_unstable()
get_sources_list_codename.return_value = 'sid'
assert utils.is_distribution_unstable()
get_sources_list_codename.return_value = 'testing'
assert not utils.is_distribution_unstable()
@patch('plinth.modules.upgrades.utils.get_current_release')
def test_is_distribution_rolling(get_current_release):
"""Test that checking for testing/unstable distribution works."""
for value in ['unstable', 'testing', 'n/a']:
get_current_release.return_value = (value, None)
assert utils.is_distribution_rolling()
get_current_release.return_value = ('forky', None)
assert not utils.is_distribution_rolling()