Skip to content

Commit 335b9f4

Browse files
vzhestkovm-czernek
andcommitted
Fixes for test_aptpkg
Co-authored-by: Marek Czernek <[email protected]>
1 parent 277a8a3 commit 335b9f4

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

tests/pytests/unit/modules/test_aptpkg.py

+103
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,11 @@ def __init__(self, uri, source_type, line, invalid, dist="", file=None):
189189
self.file = file
190190
self.disabled = False
191191
self.dist = dist
192+
self.suites = [dist]
192193
self.comps = []
193194
self.architectures = []
194195
self.signedby = ""
196+
self.types = []
195197

196198
def mysplit(self, line):
197199
return line.split()
@@ -213,6 +215,107 @@ def configure_loader_modules():
213215
return {aptpkg: {"__grains__": {}}}
214216

215217

218+
@pytest.fixture
219+
def deb822_repo_content():
220+
return """
221+
Types: deb
222+
URIs: http://cz.archive.ubuntu.com/ubuntu/
223+
Suites: noble noble-updates noble-backports
224+
Components: main
225+
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
226+
"""
227+
228+
229+
@pytest.fixture
230+
def deb822_repo_file(tmp_path: pathlib.Path, deb822_repo_content: str):
231+
"""
232+
Create a Debian-style repository in the deb822 format and return
233+
the path of the repository file.
234+
"""
235+
repo = tmp_path / "sources.list.d" / "test.sources"
236+
repo.parent.mkdir(parents=True, exist_ok=True)
237+
repo.write_text(deb822_repo_content.strip(), encoding="UTF-8")
238+
return repo
239+
240+
241+
@pytest.fixture
242+
def mock_apt_config(deb822_repo_file: pathlib.Path):
243+
"""
244+
Mocking common to deb822 testing so that apt_pkg uses the
245+
tmp_path/sources.list.d as the sourceparts location
246+
"""
247+
with patch.dict(
248+
aptpkg.__salt__,
249+
{"config.option": MagicMock()},
250+
) as mock_config, patch(
251+
"salt.utils.pkg.deb._APT_SOURCES_PARTSDIR",
252+
os.path.dirname(str(deb822_repo_file)),
253+
):
254+
yield mock_config
255+
256+
257+
def test_mod_repo_deb822_modify(deb822_repo_file: pathlib.Path, mock_apt_config):
258+
"""
259+
Test that aptpkg can modify an existing repository in the deb822 format.
260+
In this test, we match the repository by name and disable it.
261+
"""
262+
uri = "http://cz.archive.ubuntu.com/ubuntu/"
263+
repo = f"deb [signed-by=/usr/share/keyrings/ubuntu-archive-keyring.gpg] {uri} noble main"
264+
265+
aptpkg.mod_repo(repo, enabled=False, file=str(deb822_repo_file), refresh_db=False)
266+
267+
repo_file = deb822_repo_file.read_text(encoding="UTF-8")
268+
assert "Enabled: no" in repo_file
269+
assert f"URIs: {uri}" in repo_file
270+
271+
272+
def test_mod_repo_deb822_add(deb822_repo_file: pathlib.Path, mock_apt_config):
273+
"""
274+
Test that aptpkg can add a repository in the deb822 format.
275+
"""
276+
uri = "http://security.ubuntu.com/ubuntu/"
277+
repo = f"deb [signed-by=/usr/share/keyrings/ubuntu-archive-keyring.gpg] {uri} noble-security main"
278+
279+
aptpkg.mod_repo(repo, file=str(deb822_repo_file), refresh_db=False)
280+
281+
repo_file = deb822_repo_file.read_text(encoding="UTF-8")
282+
assert f"URIs: {uri}" in repo_file
283+
assert "URIs: http://cz.archive.ubuntu.com/ubuntu/" in repo_file
284+
285+
286+
def test_del_repo_deb822(deb822_repo_file: pathlib.Path, mock_apt_config):
287+
"""
288+
Test that aptpkg can delete a repository in the deb822 format.
289+
"""
290+
uri = "http://cz.archive.ubuntu.com/ubuntu/"
291+
292+
with patch.object(aptpkg, "refresh_db"):
293+
repo = f"deb {uri} noble main"
294+
aptpkg.del_repo(repo, file=str(deb822_repo_file))
295+
assert os.path.isfile(str(deb822_repo_file))
296+
297+
repo = f"deb {uri} noble-updates main"
298+
aptpkg.del_repo(repo, file=str(deb822_repo_file))
299+
assert os.path.isfile(str(deb822_repo_file))
300+
301+
repo = f"deb {uri} noble-backports main"
302+
aptpkg.del_repo(repo, file=str(deb822_repo_file))
303+
assert not os.path.isfile(str(deb822_repo_file))
304+
305+
306+
def test_get_repo_deb822(deb822_repo_file: pathlib.Path, mock_apt_config):
307+
"""
308+
Test that aptpkg can match a repository in the deb822 format.
309+
"""
310+
uri = "http://cz.archive.ubuntu.com/ubuntu/"
311+
repo = f"deb {uri} noble main"
312+
313+
result = aptpkg.get_repo(repo)
314+
315+
assert bool(result)
316+
assert result["uri"] == uri
317+
318+
216319
def test_version(lowpkg_info_var):
217320
"""
218321
Test - Returns a string representing the package version or an empty string if

0 commit comments

Comments
 (0)