fix #58667 by checking for blank comps in both source and request

This commit is contained in:
Thomas Phipps 2023-06-20 18:25:50 +00:00 committed by Megan Wilhite
parent 6f5b4e4e54
commit 1d85229886
3 changed files with 42 additions and 8 deletions

1
changelog/58667.fixed.md Normal file
View file

@ -0,0 +1 @@
fixes 58667 by checking for blank comps.

View file

@ -2072,7 +2072,7 @@ def del_repo(repo, **kwargs):
s_comps = set(source.comps)
r_comps = set(repo_comps)
if s_comps.intersection(r_comps):
if s_comps.intersection(r_comps) or (not s_comps and not r_comps):
deleted_from[source.file] = 0
source.comps = list(s_comps.difference(r_comps))
if not source.comps:
@ -2093,7 +2093,7 @@ def del_repo(repo, **kwargs):
s_comps = set(source.comps)
r_comps = set(repo_comps)
if s_comps.intersection(r_comps):
if s_comps.intersection(r_comps) or (not s_comps and not r_comps):
deleted_from[source.file] = 0
source.comps = list(s_comps.difference(r_comps))
if not source.comps:

View file

@ -98,6 +98,38 @@ def revert_repo_file(tmp_path):
aptpkg.refresh_db()
@pytest.fixture
def build_repo_file():
source_path = "/etc/apt/sources.list.d/source_test_list.list"
try:
test_repos = [
"deb [signed-by=/etc/apt/keyrings/salt-archive-keyring-2023.gpg arch=amd64] https://repo.saltproject.io/salt/py3/ubuntu/22.04/amd64/latest jammy main",
"deb http://dist.list stable/all/",
]
with salt.utils.files.fopen(source_path, "wr+") as fp:
fp.write("\n".join(test_repos))
yield source_path
finally:
if os.path.exists(source_path):
os.remove(source_path)
def get_repos_from_file(source_path):
"""
Get list of repos from repo in source_path
"""
test_repos = []
try:
with salt.utils.files.fopen(source_path) as fp:
for line in fp:
test_repos.append(line.strip())
except FileNotFoundError as error:
pytest.skip("Missing {}".format(error.filename))
if not test_repos:
pytest.skip("Did not detect an APT repo")
return test_repos
def get_current_repo(multiple_comps=False):
"""
Get a repo currently in sources.list
@ -195,18 +227,19 @@ def test_get_repos_doesnot_exist():
@pytest.mark.destructive_test
def test_del_repo(revert_repo_file):
def test_del_repo(build_repo_file):
"""
Test aptpkg.del_repo when passing repo
that exists. And checking correct error
is returned when it no longer exists.
"""
test_repo, comps = get_current_repo()
ret = aptpkg.del_repo(repo=test_repo)
assert "Repo '{}' has been removed".format(test_repo)
with pytest.raises(salt.exceptions.CommandExecutionError) as exc:
test_repos = get_repos_from_file(build_repo_file)
for test_repo in test_repos:
ret = aptpkg.del_repo(repo=test_repo)
assert "Repo {} doesn't exist".format(test_repo) in exc.value.message
assert "Repo '{}' has been removed".format(test_repo)
with pytest.raises(salt.exceptions.CommandExecutionError) as exc:
ret = aptpkg.del_repo(repo=test_repo)
assert "Repo {} doesn't exist".format(test_repo) in exc.value.message
@pytest.mark.skipif(