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 commit is contained in:
Brian C. Lane 2018-05-30 15:13:02 -07:00
parent f1000b448d
commit 3f7997d7ae
4 changed files with 26 additions and 6 deletions

View File

@ -403,6 +403,10 @@ def source_to_repo(source):
"""
repo = yum.yumRepo.YumRepository(source["name"])
# 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:
yb.repos.delete(source["name"])
# XXX - BCL DIAGNOSTIC
repos = list(r.id for r in yb.repos.listEnabled())
if source["name"] in repos:
return jsonify(status=False, errors=["Failed to delete Yum repo %s" % source["name"]]), 400
repo = source_to_repo(source)
yb.repos.add(repo)
@ -1476,6 +1471,18 @@ 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
with api.config["YUMLOCK"].lock:
repos = list(r.id for r in yb.repos.listEnabled())
if source["name"] in repos:
yb = api.config["YUMLOCK"].yb
yb.repos.delete(source["name"])
log.info("Updating repository metadata after adding %s failed", source["name"])
update_metadata(yb)
return jsonify(status=False, errors=[str(e)]), 400
return jsonify(status=True)

View File

@ -542,6 +542,16 @@ 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/base")

View File

@ -83,6 +83,5 @@ class CreateYumDirsTest(unittest.TestCase):
# will create the above directory if missing
make_yum_dirs(config)
_ = get_base_object(config)
self.assertTrue(os.path.exists(self.tmp_dir + '/var/tmp/composer/yum/root'))