Skip to content

Commit

Permalink
tests: refactor deb822 unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
james-garner-canonical committed Dec 3, 2024
1 parent 569c39e commit b081253
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
deb [signed-by=/usr/share/keyrings/ubuntu-archive-keyring.gpg] http://nz.archive.ubuntu.com/ubuntu/ noble main restricted universe multiverse
deb [signed-by=/usr/share/keyrings/ubuntu-archive-keyring.gpg] http://nz.archive.ubuntu.com/ubuntu/ noble-updates main restricted universe multiverse
deb [signed-by=/usr/share/keyrings/ubuntu-archive-keyring.gpg] http://nz.archive.ubuntu.com/ubuntu/ noble-backports main restricted universe multiverse
deb [signed-by=/usr/share/keyrings/ubuntu-archive-keyring.gpg] http://security.ubuntu.com/ubuntu noble-security main restricted universe multiverse
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Components: main restricted universe multiverse # Components first! I don't think deb822 cares about ordering
Types: deb deb-src # this could be one or both of these options
URIs: http://nz.archive.ubuntu.com/ubuntu/ http://archive.ubuntu.com/ubuntu/
# there can be multiple space separated URIs
# sources are specified in priority order
# apt does some de-duplication of sources after parsing too
#Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
# let's make this insecure! (jk, just testing parsing)
Suites: noble noble-updates noble-backports

Foo: Bar # this is a separate (malformed) entry

## a fully commented out stanza
#Types: deb
#URIs: http://security.ubuntu.com/ubuntu
#Suites: noble-security
#Components: main restricted universe multiverse
#Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
## disable security updates while we're at it

Types: deb
URIs: http://nz.archive.ubuntu.com/ubuntu/
Suites: an/exact/path/

Types: deb
URIs: http://nz.archive.ubuntu.com/ubuntu/
Suites: an/exact/path/
Components: main # this is an error, can't have components with an exact path

Types: deb
URIs: http://nz.archive.ubuntu.com/ubuntu/
Suites: noble noble-updates noble-backports
# this is an error, must have at least one component if suites isn't an exact path
1 change: 1 addition & 0 deletions tests/unit/data/fake-apt-dirs/noble/sources.list
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Ubuntu sources have moved to /etc/apt/sources.list.d/ubuntu.sources

This file was deleted.

96 changes: 25 additions & 71 deletions tests/unit/test_deb822.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

from charms.operator_libs_linux.v0 import apt

UNIT_TEST_DIR = Path(__file__).parent
TEST_DATA_DIR = Path(__file__).parent / "data"
FAKE_APT_DIRS = TEST_DATA_DIR / "fake-apt-dirs"


ubuntu_sources_deb822 = """
Expand Down Expand Up @@ -55,43 +56,6 @@
deb [signed-by=/usr/share/keyrings/ubuntu-archive-keyring.gpg] http://security.ubuntu.com/ubuntu noble-security main restricted universe multiverse
"""

inkscape_sources_deb822 = """
Types: deb
URIs: https://ppa.launchpadcontent.net/inkscape.dev/stable/ubuntu/
Suites: noble
Components: main
Signed-By:
-----BEGIN PGP PUBLIC KEY BLOCK-----
.
mQINBGY0ViQBEACsQsdIRXzyEkk38x2oDt1yQ/Kt3dsiDKJFNLbs/xiDHrgIW6cU
1wZB0pfb3lCyG3/ZH5uvR0arCSHJvCvkdCFTqqkZndSA+/pXreCSLeP8CNawf/RM
3cNbdJlE8jXzaX2qzSEC9FDNqu4cQHIHR7xMAbSCPW8rxKvRCWmkZccfndDuQyN2
vg3b2x9DWKS3DBRffivglF3yT49OuLemG5qJHujKOmNJZ32JoRniIivsuk1CCS1U
NDK6xWkr13aNe056QhVAh2iPF6MRE85zail+mQxt4LAgl/aLR0JSDSpWkbQH7kbu
5cJVan8nYF9HelVJ3QuMwdz3VQn4YVO2Wc8s0YfnNdQttYaUx3lz35Fct6CaOqjP
pgsZ4467lmB9ut74G+wDCQXmT232hsBkTz4D0DyVPB/ziBgGWUKti0AWNT/3kFww
2VM/80XADaDz0Sc/Hzi/cr9ZrbW3drMwoVvKtfMtOT7FMbeuPRWZKYZnDbvNm62e
ToKVudE0ijfsksxbcHKmGdvWntCxlinp0i39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unittest.TestCase):
def test_iter_deb822_paragraphs_ubuntu_sources(self):
Expand Down Expand Up @@ -209,7 +173,7 @@ def test_init_no_files(self):
with patch.object(
apt.RepositoryMapping,
"_apt_dir",
str(UNIT_TEST_DIR / "data/fake_apt_dir_empty"),
str(FAKE_APT_DIRS / "empty"),
):
repository_mapping = apt.RepositoryMapping()
assert not repository_mapping._repository_map
Expand All @@ -218,7 +182,7 @@ def test_init_with_good_sources_list(self):
with patch.object(
apt.RepositoryMapping,
"_apt_dir",
str(UNIT_TEST_DIR / "data/fake_apt_dir_bionic"),
str(FAKE_APT_DIRS / "bionic"),
):
repository_mapping = apt.RepositoryMapping()
assert repository_mapping._repository_map
Expand All @@ -227,7 +191,7 @@ def test_init_with_bad_sources_list_no_fallback(self):
with patch.object(
apt.RepositoryMapping,
"_apt_dir",
str(UNIT_TEST_DIR / "data/fake_apt_dir_noble_no_sources"),
str(FAKE_APT_DIRS / "noble-no-sources"),
):
with self.assertRaises(apt.InvalidSourceError):
apt.RepositoryMapping()
Expand All @@ -236,7 +200,7 @@ def test_init_with_bad_sources_list_fallback_ok(self):
with patch.object(
apt.RepositoryMapping,
"_apt_dir",
str(UNIT_TEST_DIR / "data/fake_apt_dir_noble"),
str(FAKE_APT_DIRS / "noble"),
):
repository_mapping = apt.RepositoryMapping()
assert repository_mapping._repository_map
Expand All @@ -245,7 +209,7 @@ def test_init_with_bad_ubuntu_sources(self):
with patch.object(
apt.RepositoryMapping,
"_apt_dir",
str(UNIT_TEST_DIR / "data/fake_apt_dir_noble_empty_sources"),
str(FAKE_APT_DIRS / "noble-empty-sources"),
):
with self.assertRaises(apt.InvalidSourceError):
apt.RepositoryMapping()
Expand All @@ -254,7 +218,7 @@ def test_init_with_third_party_inkscape_source(self):
with patch.object(
apt.RepositoryMapping,
"_apt_dir",
str(UNIT_TEST_DIR / "data/fake_apt_dir_noble_with_inkscape"),
str(FAKE_APT_DIRS / "noble-with-inkscape"),
):
repository_mapping = apt.RepositoryMapping()
assert repository_mapping._repository_map
Expand All @@ -263,7 +227,7 @@ def test_load_deb822_ubuntu_sources(self):
with patch.object(
apt.RepositoryMapping,
"_apt_dir",
str(UNIT_TEST_DIR / "data/fake_apt_dir_empty"),
str(FAKE_APT_DIRS / "empty"),
):
repository_mapping = apt.RepositoryMapping()
assert not repository_mapping._repository_map
Expand Down Expand Up @@ -310,37 +274,27 @@ def test_init_with_deb822(self):
They should be equivalent with the sample data being used.
"""
with patch.object(
apt.RepositoryMapping,
"_apt_dir",
str(FAKE_APT_DIRS / "noble"),
):
repos_deb822 = apt.RepositoryMapping()

def isnt_file(f: str) -> bool:
return False

def iglob_list(s: str) -> typing.Iterable[str]:
if s.endswith(".list"):
return ["FILENAME"]
return []

def iglob_sources(s: str) -> typing.Iterable[str]:
if s.endswith(".sources"):
return ["FILENAME"]
return []

with patch("builtins.open", new_callable=mock_open, read_data=ubuntu_sources_one_line):
with patch("os.path.isfile", new=isnt_file):
with patch("glob.iglob", new=iglob_list):
repository_mapping_list = apt.RepositoryMapping()

with patch("builtins.open", new_callable=mock_open, read_data=ubuntu_sources_deb822):
with patch("os.path.isfile", new=isnt_file):
with patch("glob.iglob", new=iglob_sources):
repository_mapping_sources = apt.RepositoryMapping()
with patch.object(
apt.RepositoryMapping,
"_apt_dir",
str(FAKE_APT_DIRS / "noble-in-one-per-line-format"),
):
repos_one_per_line = apt.RepositoryMapping()

list_keys = sorted(repository_mapping_list._repository_map.keys())
sources_keys = sorted(repository_mapping_sources._repository_map.keys())
list_keys = sorted(repos_one_per_line._repository_map.keys())
sources_keys = sorted(repos_deb822._repository_map.keys())
assert sources_keys == list_keys

for list_key, sources_key in zip(list_keys, sources_keys):
list_repo = repository_mapping_list[list_key]
sources_repo = repository_mapping_sources[sources_key]
list_repo = repos_one_per_line[list_key]
sources_repo = repos_deb822[sources_key]
assert list_repo.enabled == sources_repo.enabled
assert list_repo.repotype == sources_repo.repotype
assert list_repo.uri == sources_repo.uri
Expand Down

0 comments on commit b081253

Please sign in to comment.