From f1af108e5bfff41c5a0997a5c39d62f12bf07aa5 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Mon, 3 Dec 2018 15:38:59 -0800 Subject: [PATCH] lorax-composer: Check for CDN only repos Anaconda is currently not able to handle cdn repo urls in the kickstart. Add a new function that checks for extra repos and returns True. Related: rhbz#1655623 --- src/pylorax/api/dnfbase.py | 24 ++++++++ tests/pylorax/test_dnfbase.py | 107 +++++++++++++++++++++++++++++++++- 2 files changed, 130 insertions(+), 1 deletion(-) diff --git a/src/pylorax/api/dnfbase.py b/src/pylorax/api/dnfbase.py index bde82985..45f7198d 100644 --- a/src/pylorax/api/dnfbase.py +++ b/src/pylorax/api/dnfbase.py @@ -65,6 +65,30 @@ class DNFLock(object): self.dbo.update_cache() return self._lock +def check_repos(dbo): + """Check the enabled repos to see if only cdn.redhat.com repos are enabled. + + :param dbo: A DNF Base object + :type dbo: dnf.Base + :returns: True if there are non-CDN repos enabled. False otherwise. + :rtype: bool + + Anaconda is currently not able to handle cdn repo urls in the kickstart. lorax-composer + will depsolve just fine, but trying to execute a build will fail unless other + repos are also available (eg. a local mirror). + + This checks to see if *only* CDN repos are enabled and if so returns False + """ + for r in dbo.repos.iter_enabled(): + if r.baseurl and all("cdn.redhat.com" not in u.lower() for u in r.baseurl): + return True + elif r.mirrorlist and "cdn.redhat.com" not in r.mirrorlist.lower(): + return True + elif r.metalink and "cdn.redhat.com" not in r.metalink.lower(): + return True + + return False + def get_base_object(conf): """Get the DNF object with settings from the config file diff --git a/tests/pylorax/test_dnfbase.py b/tests/pylorax/test_dnfbase.py index e32978cb..98616863 100644 --- a/tests/pylorax/test_dnfbase.py +++ b/tests/pylorax/test_dnfbase.py @@ -22,7 +22,8 @@ import unittest import configparser from pylorax.api.config import configure, make_dnf_dirs -from pylorax.api.dnfbase import get_base_object +from pylorax.api.dnfbase import get_base_object, check_repos +from pylorax.api.projects import source_to_repo class DnfbaseNoSystemReposTest(unittest.TestCase): @@ -110,3 +111,107 @@ class CreateDnfDirsTest(unittest.TestCase): make_dnf_dirs(config) self.assertTrue(os.path.exists(self.tmp_dir + '/var/tmp/composer/dnf/root')) + +class DnfbaseCDNTest(unittest.TestCase): + @classmethod + def setUpClass(self): + self.tmp_dir = tempfile.mkdtemp(prefix="lorax.test.dnfbase.") + conf_file = os.path.join(self.tmp_dir, 'test.conf') + open(conf_file, 'w').write("""[composer] +[repos] +use_system_repos = False +""") + + # will read the above configuration + config = configure(conf_file=conf_file, root_dir=self.tmp_dir) + make_dnf_dirs(config) + + # will read composer config and store a dnf config file + self.dbo = get_base_object(config) + + # will read the stored dnf config file + self.dnfconf = configparser.ConfigParser() + self.dnfconf.read([config.get("composer", "dnf_conf")]) + + @classmethod + def tearDownClass(self): + shutil.rmtree(self.tmp_dir) + + def test_check_repos_no_cdn(self, check_empty=True): + """Test that no cdn.redhat.com returns True""" + repo = source_to_repo({"name": "no-cdn", + "type": "yum-baseurl", + "check_ssl": True, + "check_gpg": True, + "url": "https://repo.example.com"}, self.dbo.conf) + self.dbo.repos.add(repo) + self.assertTrue(check_repos(self.dbo)) + del self.dbo.repos["no-cdn"] + + repo = source_to_repo({"name": "no-cdn-metalink", + "type": "yum-metalink", + "check_ssl": True, + "check_gpg": True, + "url": "https://repo.example.com"}, self.dbo.conf) + self.dbo.repos.add(repo) + self.assertTrue(check_repos(self.dbo)) + del self.dbo.repos["no-cdn-metalink"] + + repo = source_to_repo({"name": "no-cdn-mirrorlist", + "type": "yum-mirrorlist", + "check_ssl": True, + "check_gpg": True, + "url": "https://repo.example.com"}, self.dbo.conf) + self.dbo.repos.add(repo) + self.assertTrue(check_repos(self.dbo)) + del self.dbo.repos["no-cdn-mirrorlist"] + + if check_empty: + self.assertTrue(self.dbo.repos == {}) + + def test_check_repos_only_cdn(self): + """Test that only cdn.redhat.com returns False""" + repo = source_to_repo({"name": "only-cdn", + "type": "yum-baseurl", + "check_ssl": True, + "check_gpg": True, + "url": "https://cdn.redhat.com"}, self.dbo.conf) + self.dbo.repos.add(repo) + self.assertFalse(check_repos(self.dbo), self.dbo.repos) + del self.dbo.repos["only-cdn"] + + repo = source_to_repo({"name": "only-cdn-metalink", + "type": "yum-metalink", + "check_ssl": True, + "check_gpg": True, + "url": "https://cdn.redhat.com"}, self.dbo.conf) + self.dbo.repos.add(repo) + self.assertFalse(check_repos(self.dbo), self.dbo.repos) + del self.dbo.repos["only-cdn-metalink"] + + repo = source_to_repo({"name": "only-cdn-mirrorlist", + "type": "yum-mirrorlist", + "check_ssl": True, + "check_gpg": True, + "url": "https://cdn.redhat.com"}, self.dbo.conf) + self.dbo.repos.add(repo) + self.assertFalse(check_repos(self.dbo), self.dbo.repos) + del self.dbo.repos["only-cdn-mirrorlist"] + + self.assertTrue(self.dbo.repos == {}) + + def test_check_repos_with_cdn(self): + """Test that adding a non-cdn repo to a cdn one returns True""" + repo = source_to_repo({"name": "cdn", + "type": "yum-baseurl", + "check_ssl": True, + "check_gpg": True, + "url": "https://cdn.redhat.com"}, self.dbo.conf) + self.dbo.repos.add(repo) + + # Use the no-cdn tests with a pre-populated dbo.repos + self.test_check_repos_no_cdn(check_empty=False) + del self.dbo.repos["cdn"] + + self.assertTrue(self.dbo.repos == {}) +