Skip to content

Commit

Permalink
tests: add some unit tests for load_deb822
Browse files Browse the repository at this point in the history
  • Loading branch information
james-garner-canonical committed Nov 21, 2024
1 parent 2dd9dcd commit 6c39d86
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion tests/unit/test_apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 6c39d86

Please sign in to comment.