From 6c39d86017cb1896e014eaf51f625dd4740cf2f3 Mon Sep 17 00:00:00 2001 From: James Garner Date: Thu, 21 Nov 2024 16:45:57 +1300 Subject: [PATCH] tests: add some unit tests for load_deb822 --- tests/unit/test_apt.py | 45 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_apt.py b/tests/unit/test_apt.py index b3dc966f..834ac0b6 100644 --- a/tests/unit/test_apt.py +++ b/tests/unit/test_apt.py @@ -3,7 +3,7 @@ import subprocess import unittest -from unittest.mock import patch +from unittest.mock import mock_open, patch, ANY from charms.operator_libs_linux.v0 import apt @@ -572,6 +572,49 @@ def test_parse_deb822_lines_w_comments(self): [error] = errors assert isinstance(error, apt.InvalidSourceError) + def test_load_deb822_ubuntu_sources(self): + with ( + patch('os.path.isfile', new=lambda f: False), # pyright: ignore[reportUnknownArgumentType, reportUnknownLambdaType] + patch('glob.iglob', new=lambda s: []), # pyright: ignore[reportUnknownArgumentType, reportUnknownLambdaType] + ): + repository_mapping = apt.RepositoryMapping() + assert not repository_mapping._repository_map + mopen = mock_open(read_data=ubuntu_sources_deb822) + mopen.return_value.__iter__ = lambda self: iter(self.readline, '') # pyright: ignore[reportUnknownArgumentType, reportUnknownLambdaType, reportUnknownMemberType] + with patch('builtins.open', new=mopen): + repository_mapping.load_deb822("") + assert sorted(repository_mapping._repository_map.keys()) == [ + "deb-http://nz.archive.ubuntu.com/ubuntu/-noble", + "deb-http://nz.archive.ubuntu.com/ubuntu/-noble-backports", + "deb-http://nz.archive.ubuntu.com/ubuntu/-noble-updates", + "deb-http://security.ubuntu.com/ubuntu-noble-security", + ] + + def test_load_deb822_w_comments(self): + with ( + patch('os.path.isfile', new=lambda f: False), # pyright: ignore[reportUnknownArgumentType, reportUnknownLambdaType] + patch('glob.iglob', new=lambda s: []), # pyright: ignore[reportUnknownArgumentType, reportUnknownLambdaType] + ): + repository_mapping = apt.RepositoryMapping() + assert not repository_mapping._repository_map + mopen = mock_open(read_data=ubuntu_sources_deb822_with_comments) + mopen.return_value.__iter__ = lambda self: iter(self.readline, '') # pyright: ignore[reportUnknownArgumentType, reportUnknownLambdaType, reportUnknownMemberType] + with ( + patch('builtins.open', new=mopen), + patch.object(apt.logger, 'debug') as debug, + ): + repository_mapping.load_deb822("FILENAME") + assert sorted(repository_mapping._repository_map.keys()) == [ + "deb-http://nz.archive.ubuntu.com/ubuntu/-noble", + "deb-http://nz.archive.ubuntu.com/ubuntu/-noble-backports", + "deb-http://nz.archive.ubuntu.com/ubuntu/-noble-updates", + ] + debug.assert_called_once_with( + ANY, + 1, # number of errors + "Missing key 'Types' for entry starting on line 11 in FILENAME." + ) + class TestAptBareMethods(unittest.TestCase): @patch("charms.operator_libs_linux.v0.apt.check_output")