diff --git a/tests/unit/data/fake_apt_dir_bionic/sources.list b/tests/unit/data/fake-apt-dirs/bionic/sources.list similarity index 100% rename from tests/unit/data/fake_apt_dir_bionic/sources.list rename to tests/unit/data/fake-apt-dirs/bionic/sources.list diff --git a/tests/unit/data/fake_apt_dir_noble/sources.list b/tests/unit/data/fake-apt-dirs/noble-empty-sources/sources.list similarity index 100% rename from tests/unit/data/fake_apt_dir_noble/sources.list rename to tests/unit/data/fake-apt-dirs/noble-empty-sources/sources.list diff --git a/tests/unit/data/fake_apt_dir_noble_empty_sources/sources.list.d/ubuntu.sources b/tests/unit/data/fake-apt-dirs/noble-empty-sources/sources.list.d/ubuntu.sources similarity index 100% rename from tests/unit/data/fake_apt_dir_noble_empty_sources/sources.list.d/ubuntu.sources rename to tests/unit/data/fake-apt-dirs/noble-empty-sources/sources.list.d/ubuntu.sources diff --git a/tests/unit/data/fake-apt-dirs/noble-in-one-per-line-format/sources.list b/tests/unit/data/fake-apt-dirs/noble-in-one-per-line-format/sources.list new file mode 100644 index 0000000..03481c5 --- /dev/null +++ b/tests/unit/data/fake-apt-dirs/noble-in-one-per-line-format/sources.list @@ -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 \ No newline at end of file diff --git a/tests/unit/data/fake_apt_dir_noble_empty_sources/sources.list b/tests/unit/data/fake-apt-dirs/noble-no-sources/sources.list similarity index 100% rename from tests/unit/data/fake_apt_dir_noble_empty_sources/sources.list rename to tests/unit/data/fake-apt-dirs/noble-no-sources/sources.list diff --git a/tests/unit/data/fake_apt_dir_noble_no_sources/sources.list b/tests/unit/data/fake-apt-dirs/noble-with-comments-etc/sources.list similarity index 100% rename from tests/unit/data/fake_apt_dir_noble_no_sources/sources.list rename to tests/unit/data/fake-apt-dirs/noble-with-comments-etc/sources.list diff --git a/tests/unit/data/fake-apt-dirs/noble-with-comments-etc/sources.list.d/ubuntu.sources b/tests/unit/data/fake-apt-dirs/noble-with-comments-etc/sources.list.d/ubuntu.sources new file mode 100644 index 0000000..eff1961 --- /dev/null +++ b/tests/unit/data/fake-apt-dirs/noble-with-comments-etc/sources.list.d/ubuntu.sources @@ -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 \ No newline at end of file diff --git a/tests/unit/data/fake_apt_dir_noble_with_inkscape/sources.list b/tests/unit/data/fake-apt-dirs/noble-with-inkscape/sources.list similarity index 100% rename from tests/unit/data/fake_apt_dir_noble_with_inkscape/sources.list rename to tests/unit/data/fake-apt-dirs/noble-with-inkscape/sources.list diff --git a/tests/unit/data/fake_apt_dir_noble/sources.list.d/inkscape_dev-ubuntu-stable-noble.sources b/tests/unit/data/fake-apt-dirs/noble-with-inkscape/sources.list.d/inkscape_dev-ubuntu-stable-noble.sources similarity index 100% rename from tests/unit/data/fake_apt_dir_noble/sources.list.d/inkscape_dev-ubuntu-stable-noble.sources rename to tests/unit/data/fake-apt-dirs/noble-with-inkscape/sources.list.d/inkscape_dev-ubuntu-stable-noble.sources diff --git a/tests/unit/data/fake_apt_dir_noble/sources.list.d/ubuntu.sources b/tests/unit/data/fake-apt-dirs/noble-with-inkscape/sources.list.d/ubuntu.sources similarity index 100% rename from tests/unit/data/fake_apt_dir_noble/sources.list.d/ubuntu.sources rename to tests/unit/data/fake-apt-dirs/noble-with-inkscape/sources.list.d/ubuntu.sources diff --git a/tests/unit/data/fake-apt-dirs/noble/sources.list b/tests/unit/data/fake-apt-dirs/noble/sources.list new file mode 100644 index 0000000..eb39b94 --- /dev/null +++ b/tests/unit/data/fake-apt-dirs/noble/sources.list @@ -0,0 +1 @@ +# Ubuntu sources have moved to /etc/apt/sources.list.d/ubuntu.sources diff --git a/tests/unit/data/fake_apt_dir_noble_with_inkscape/sources.list.d/ubuntu.sources b/tests/unit/data/fake-apt-dirs/noble/sources.list.d/ubuntu.sources similarity index 100% rename from tests/unit/data/fake_apt_dir_noble_with_inkscape/sources.list.d/ubuntu.sources rename to tests/unit/data/fake-apt-dirs/noble/sources.list.d/ubuntu.sources diff --git a/tests/unit/data/fake_apt_dir_noble_with_inkscape/sources.list.d/inkscape_dev-ubuntu-stable-noble.sources b/tests/unit/data/fake_apt_dir_noble_with_inkscape/sources.list.d/inkscape_dev-ubuntu-stable-noble.sources deleted file mode 100644 index 9bce7f1..0000000 --- a/tests/unit/data/fake_apt_dir_noble_with_inkscape/sources.list.d/inkscape_dev-ubuntu-stable-noble.sources +++ /dev/null @@ -1,34 +0,0 @@ -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 - ToKVudE0ijfsksxbcHKmGdvWntCxlinp0i39Jfz6y54pppjmbHRQgasmqm2EQLfA - RUNW/zB7gX80KTUCGjcLOTPajBIN5ZfjVITetryAFjv7fnK0qpf2OpqwF832W4V/ - S3GZtErupPktYG77Z9jOUxcJeEGYjWbVlbit8mTKDRdQUXOeOO6TzB4RcwARAQAB - tCVMYXVuY2hwYWQgUFBBIGZvciBJbmtzY2FwZSBEZXZlbG9wZXJziQJOBBMBCgA4 - FiEEVr3/0vHJaz0VdeO4XJoLhs0vyzgFAmY0ViQCGwMFCwkIBwIGFQoJCAsCBBYC - AwECHgECF4AACgkQXJoLhs0vyzh3RBAAo7Hee8i2I4n03/iq58lqml/OVJH9ZEle - amk3e0wsiVS0QdT/zB8/AMVDB1obazBfrHKJP9Ck+JKH0uxaGRxYBohTbO3Y3sBO - qRHz5VLcFzuyk7AA53AZkNx8Zbv6D0O4JTCPDJn9Gwbd/PpnvJm9Ri+zEiVPhXNu - oSBryGM09un2Yvi0DA+ulouSKTy9dkbI1R7flPZ2M/mKT8Lk0n1pJu5FvgPC4E6R - PT0Njw9+k/iHtP76U4SqHJZZx2I/TGlXMD1memyTK4adWZeGLaAiFadsoeJsDoDE - MkHFxFkr9n0E4wJhRGgL0OxDWugJemkUvHbzXFNUaeX5Spw/aO7r1CtTh8lyqiom - 4ebAkURjESRFOFzcsM7nyQnmt2OgQkEShSL3NrDMkj1+3+FgQtd8sbeVpmpGBq87 - J3iq0YMsUysWq8DJSz4RnBTfeGlJWJkl3XxB9VbG3BqqbN9fRp+ccgZ51g5/DEA/ - q8jYS7Ur5bAlSoeA4M3SvKSlQM8+aT35dteFzejvo1N+2n0E0KoymuRsDBdzby0z - lJDKe244L5D6UPJo9YTmtE4kG/fGNZ5/JdRA+pbe7f/z84HVcJ3ziGdF/Nio/D8k - uFjZP2M/mxC7j+WnmKAruqmY+5vkAEqUPTobsloDjT3B+z0rzWk8FG/G5KFccsBO - 2ekz6IVTXVA= - =VF33 - -----END PGP PUBLIC KEY BLOCK----- diff --git a/tests/unit/test_deb822.py b/tests/unit/test_deb822.py index 9abefe3..688634f 100644 --- a/tests/unit/test_deb822.py +++ b/tests/unit/test_deb822.py @@ -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 = """ @@ -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----- - . - mQINBGY0ViQBEACsQsdIRXzyEk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PC4E6R - PT0Njw9+k/iHtP76U4SqHJZZx2I/TGlXMD1memyTK4adWZeGLaAiFadsoeJsDoDE - MkHFxFkr9n0E4wJhRGgL0OxDWugJemkUvHbzXFNUaeX5Spw/aO7r1CtTh8lyqiom - 4ebAkURjESRFOFzcsM7nyQnmt2OgQkEShSL3NrDMkj1+3+FgQtd8sbeVpmpGBq87 - J3iq0YMsUysWq8DJSz4RnBTfeGlJWJkl3XxB9VbG3BqqbN9fRp+ccgZ51g5/DEA/ - q8jYS7Ur5bAlSoeA4M3SvKSlQM8+aT35dteFzejvo1N+2n0E0KoymuRsDBdzby0z - lJDKe244L5D6UPJo9YTmtE4kG/fGNZ5/JdRA+pbe7f/z84HVcJ3ziGdF/Nio/D8k - uFjZP2M/mxC7j+WnmKAruqmY+5vkAEqUPTobsloDjT3B+z0rzWk8FG/G5KFccsBO - 2ekz6IVTXVA= - =VF33 - -----END PGP PUBLIC KEY BLOCK----- -""" - class TestRepositoryMappingDeb822Behaviour(unittest.TestCase): def test_iter_deb822_paragraphs_ubuntu_sources(self): @@ -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 @@ -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 @@ -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() @@ -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 @@ -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() @@ -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 @@ -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 @@ -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