Fix handling bad source repos and add a test

When adding a source failed it wasn't being removed from the dnf object.
This fixes that, and returns an error when setting up the source fails.
Also adds a test for it.
This also includes detecting rawhide vs. non-rawhide releases and
adjusting the tests accordingly (some of the source names change).

(cherry picked from commit dd8e4d9e99)
This commit is contained in:
Brian C. Lane 2018-05-30 15:13:02 -07:00
parent 45f4d3e633
commit 1ab33b6416
3 changed files with 45 additions and 9 deletions

View File

@ -405,6 +405,10 @@ def source_to_repo(source, dnf_conf):
"""
repo = dnf.repo.Repo(source["name"], dnf_conf)
# This will allow errors to be raised so we can catch them
# without this they are logged, but the repo is silently disabled
repo.skip_if_unavailable = False
if source["type"] == "yum-baseurl":
repo.baseurl = [source["url"]]
elif source["type"] == "yum-metalink":

View File

@ -1451,11 +1451,6 @@ def v0_api(api):
if source["name"] in repos:
del dbo.repos[source["name"]]
# XXX - BCL DIAGNOSTIC
repos = list(r.id for r in dbo.repos.iter_enabled())
if source["name"] in repos:
return jsonify(status=False, errors=["Failed to delete DNF repo %s" % source["name"]]), 400
repo = source_to_repo(source, dbo.conf)
dbo.repos.add(repo)
@ -1477,6 +1472,19 @@ def v0_api(api):
with open(source_path, "w") as f:
f.write(str(repo))
except Exception as e:
log.error("(v0_projects_source_add) adding %s failed: %s", source["name"], str(e))
# Cleanup the mess, if loading it failed we don't want to leave it in memory
repos = list(r.id for r in dbo.repos.iter_enabled())
if source["name"] in repos:
with api.config["DNFLOCK"].lock:
dbo = api.config["DNFLOCK"].dbo
del dbo.repos[source["name"]]
log.info("Updating repository metadata after adding %s failed", source["name"])
dbo.fill_sack(load_system_repo=False)
dbo.read_comps()
return jsonify(status=False, errors=[str(e)]), 400
return jsonify(status=True)

View File

@ -35,6 +35,7 @@ class ServerTestCase(unittest.TestCase):
@classmethod
def setUpClass(self):
self.rawhide = False
self.maxDiff = None
repo_dir = tempfile.mkdtemp(prefix="lorax.test.repo.")
@ -55,6 +56,10 @@ class ServerTestCase(unittest.TestCase):
for f in glob("./tests/pylorax/repos/*.repo"):
shutil.copy2(f, dnf_repo_dir)
# Modify fedora vs. rawhide tests when running on rawhide
if os.path.exists("/etc/yum.repos.d/fedora-rawhide.repo"):
self.rawhide = True
# dnf repo baseurl has to point to an absolute directory, so we use /tmp/lorax-empty-repo/ in the files
# and create an empty repository
os.makedirs("/tmp/lorax-empty-repo/")
@ -486,7 +491,10 @@ class ServerTestCase(unittest.TestCase):
resp = self.server.get("/api/v0/projects/source/list")
data = json.loads(resp.data)
self.assertNotEqual(data, None)
self.assertEqual(data["sources"], ["lorax-1", "lorax-2", "lorax-3", "lorax-4", "other-repo", "rawhide", "single-repo"])
if self.rawhide:
self.assertEqual(data["sources"], ["lorax-1", "lorax-2", "lorax-3", "lorax-4", "other-repo", "rawhide", "single-repo"])
else:
self.assertEqual(data["sources"], ["fedora", "lorax-1", "lorax-2", "lorax-3", "lorax-4", "other-repo", "single-repo", "updates"])
def test_projects_source_00_info(self):
"""Test /api/v0/projects/source/info"""
@ -550,18 +558,34 @@ class ServerTestCase(unittest.TestCase):
self.assertEqual(repo["check_ssl"], False)
self.assertTrue("gpgkey_urls" not in repo)
def test_projects_source_00_bad_url(self):
"""Test /api/v0/projects/source/new with a new source that has an invalid url"""
toml_source = open("./tests/pylorax/source/bad-repo.toml").read()
self.assertTrue(len(toml_source) > 0)
resp = self.server.post("/api/v0/projects/source/new",
data=toml_source,
content_type="text/x-toml")
data = json.loads(resp.data)
self.assertEqual(data["status"], False)
def test_projects_source_01_delete_system(self):
"""Test /api/v0/projects/source/delete a system source"""
resp = self.server.delete("/api/v0/projects/source/delete/rawhide")
if self.rawhide:
resp = self.server.delete("/api/v0/projects/source/delete/rawhide")
else:
resp = self.server.delete("/api/v0/projects/source/delete/fedora")
data = json.loads(resp.data)
self.assertNotEqual(data, None)
self.assertEqual(data["status"], False)
# Make sure rawhide is still listed
# Make sure fedora/rawhide is still listed
resp = self.server.get("/api/v0/projects/source/list")
data = json.loads(resp.data)
self.assertNotEqual(data, None)
self.assertTrue("rawhide" in data["sources"])
if self.rawhide:
self.assertTrue("rawhide" in data["sources"])
else:
self.assertTrue("fedora" in data["sources"])
def test_projects_source_02_delete_single(self):
"""Test /api/v0/projects/source/delete a single source"""