Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decide if the source should be actually skipped #55402

Merged
merged 14 commits into from
May 4, 2020
30 changes: 29 additions & 1 deletion salt/modules/aptpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,34 @@ def list_repo_pkgs(*args, **kwargs): # pylint: disable=unused-import
return ret


def _skip_source(source):
garethgreenaway marked this conversation as resolved.
Show resolved Hide resolved
"""
Decide to skip source or not.

:param source:
:return:
"""
if source.invalid:
if (
source.uri
and source.type
and source.type in ("deb", "deb-src", "rpm", "rpm-src")
):
pieces = source.mysplit(source.line)
if pieces[1].strip()[0] == "[":
options = pieces.pop(1).strip("[]").split()
if len(options) > 0:
log.debug(
"Source %s will be included although is marked invalid",
source.uri,
)
return False
return True
else:
return True
return False


def list_repos():
"""
Lists all repos in the sources.list (and sources.lists.d) files
Expand All @@ -1660,7 +1688,7 @@ def list_repos():
repos = {}
sources = sourceslist.SourcesList()
for source in sources.list:
if source.invalid:
if _skip_source(source):
continue
repo = {}
repo["file"] = source.file
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/modules/test_aptpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from __future__ import absolute_import, print_function, unicode_literals

import copy
import logging
import textwrap

import salt.modules.aptpkg as aptpkg
Expand All @@ -28,6 +29,8 @@
except ImportError:
pytest = None

log = logging.getLogger(__name__)


APT_KEY_LIST = r"""
pub:-:1024:17:46181433FBB75451:1104433784:::-:::scSC:
Expand Down Expand Up @@ -153,6 +156,17 @@
UNINSTALL = {"tmux": {"new": six.text_type(), "old": "1.8-5"}}


class MockSourceEntry(object):
def __init__(self, uri, source_type, line, invalid):
self.uri = uri
self.type = source_type
self.line = line
self.invalid = invalid

def mysplit(self, line):
return line.split()


class AptPkgTestCase(TestCase, LoaderModuleMockMixin):
"""
Test cases for salt.modules.aptpkg
Expand Down Expand Up @@ -595,6 +609,41 @@ def test_list_downloaded(self):
self.assertEqual(len(list_downloaded), 1)
self.assertDictEqual(list_downloaded, DOWNLOADED_RET)

def test__skip_source(self):
"""
Test __skip_source.
:return:
"""
# Valid source
source_type = "deb"
source_uri = "http://cdn-aws.deb.debian.org/debian"
source_line = "deb http://cdn-aws.deb.debian.org/debian stretch main\n"

mock_source = MockSourceEntry(source_uri, source_type, source_line, False)

ret = aptpkg._skip_source(mock_source)
self.assertFalse(ret)

# Invalid source type
source_type = "ded"
source_uri = "http://cdn-aws.deb.debian.org/debian"
source_line = "deb http://cdn-aws.deb.debian.org/debian stretch main\n"

mock_source = MockSourceEntry(source_uri, source_type, source_line, True)

ret = aptpkg._skip_source(mock_source)
self.assertTrue(ret)

# Invalid source type , not skipped
source_type = "deb"
source_uri = "http://cdn-aws.deb.debian.org/debian"
source_line = "deb [http://cdn-aws.deb.debian.org/debian] stretch main\n"

mock_source = MockSourceEntry(source_uri, source_type, source_line, True)

ret = aptpkg._skip_source(mock_source)
self.assertFalse(ret)


@skipIf(pytest is None, "PyTest is missing")
class AptUtilsTestCase(TestCase, LoaderModuleMockMixin):
Expand Down