diff --git a/src/pylorax/api/projects.py b/src/pylorax/api/projects.py index 4214059d..40171363 100644 --- a/src/pylorax/api/projects.py +++ b/src/pylorax/api/projects.py @@ -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": diff --git a/src/pylorax/api/v0.py b/src/pylorax/api/v0.py index 4047ed58..56d850aa 100644 --- a/src/pylorax/api/v0.py +++ b/src/pylorax/api/v0.py @@ -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) diff --git a/tests/pylorax/test_server.py b/tests/pylorax/test_server.py index 8ea14c89..24bb1dea 100644 --- a/tests/pylorax/test_server.py +++ b/tests/pylorax/test_server.py @@ -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") diff --git a/tests/pylorax/test_yumbase.py b/tests/pylorax/test_yumbase.py index b0569028..54d0f969 100644 --- a/tests/pylorax/test_yumbase.py +++ b/tests/pylorax/test_yumbase.py @@ -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'))