From b25995d9d4113e07db4d79668249a07fe89edcef Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Fri, 8 Mar 2019 16:43:26 -0800 Subject: [PATCH] rpmgit: catch potential errors while running git Log them and report them as RuntimeError. Also add a couple tests for them. (cherry picked from commit 61efa91a03dc675c5565a6cea4360e80f53c8826) (cherry picked from commit 087b0fe8c66ad8da997af8615dd56187d89454ce) --- src/pylorax/api/gitrpm.py | 18 ++++++++++--- tests/pylorax/test_gitrpm.py | 50 +++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/pylorax/api/gitrpm.py b/src/pylorax/api/gitrpm.py index 04daa400..3efb8a7d 100644 --- a/src/pylorax/api/gitrpm.py +++ b/src/pylorax/api/gitrpm.py @@ -59,7 +59,11 @@ class GitArchiveTarball: # Clone the repository into a temporary location cmd = ["git", "clone", self._gitRepo["repo"], joinpaths(sourcesDir, "gitrepo")] log.debug(cmd) - subprocess.check_call(cmd) + try: + subprocess.check_output(cmd) + except subprocess.CalledProcessError as e: + log.error("Failed to clone %s: %s", self._gitRepo["repo"], e.output) + raise RuntimeError("Failed to clone %s" % self._gitRepo["repo"]) oldcwd = os.getcwd() try: @@ -72,7 +76,11 @@ class GitArchiveTarball: cmd = ["git", "archive", "--prefix", self._gitRepo["rpmname"] + "/", "-o", joinpaths(sourcesDir, self.sourceName), self._gitRepo["ref"]] log.debug(cmd) - subprocess.check_call(cmd) + try: + subprocess.check_output(cmd) + except subprocess.CalledProcessError as e: + log.error("Failed to archive %s: %s", self._gitRepo["repo"], e.output) + raise RuntimeError("Failed to clone %s" % self._gitRepo["repo"]) finally: # Cleanup even if there was an error os.chdir(oldcwd) @@ -197,6 +205,10 @@ def create_gitrpm_repo(results_dir, recipe): make_git_rpm(r, gitrepo) cmd = ["createrepo_c", gitrepo] log.debug(cmd) - subprocess.check_call(cmd) + try: + subprocess.check_output(cmd) + except subprocess.CalledProcessError as e: + log.error("Failed to create repo at %s: %s", gitrepo, e.output) + raise RuntimeError("Failed to create repo at %s" % gitrepo) return gitrepo diff --git a/tests/pylorax/test_gitrpm.py b/tests/pylorax/test_gitrpm.py index 9a33a80d..40a2a2ab 100644 --- a/tests/pylorax/test_gitrpm.py +++ b/tests/pylorax/test_gitrpm.py @@ -24,7 +24,7 @@ import tarfile import tempfile import unittest -from pylorax.api.gitrpm import GitArchiveTarball, make_git_rpm, create_gitrpm_repo +from pylorax.api.gitrpm import GitArchiveTarball, GitRpmBuild, make_git_rpm, create_gitrpm_repo from pylorax.sysutils import joinpaths def _setup_git_repo(self): @@ -175,6 +175,38 @@ class GitArchiveTest(unittest.TestCase): archive = GitArchiveTarball(git_repo["repos"]["git"][0]) self._check_tar(archive, "git-rpm-test/", "second") + def git_fail_repo_test(self): + """Test creating an archive from a bad url""" + git_repo = toml.loads(""" + [[repos.git]] + rpmname="git-rpm-test" + rpmversion="1.0.0" + rpmrelease="1" + summary="Testing the git rpm code" + repo="file://%s" + ref="v1.1.0" + destination="/srv/testing-rpm/" + """ % ("/tmp/no-repo-here/")) + with self.assertRaises(RuntimeError): + archive = GitArchiveTarball(git_repo["repos"]["git"][0]) + self._check_tar(archive, "git-rpm-test/", None) + + def git_fail_ref_test(self): + """Test creating an archive from a bad ref""" + git_repo = toml.loads(""" + [[repos.git]] + rpmname="git-rpm-test" + rpmversion="1.0.0" + rpmrelease="1" + summary="Testing the git rpm code" + repo="file://%s" + ref="0297617d7b8baa263a69ae7dc901bbbcefd0eaa4" + destination="/srv/testing-rpm/" + """ % (self.repodir)) + with self.assertRaises(RuntimeError): + archive = GitArchiveTarball(git_repo["repos"]["git"][0]) + self._check_tar(archive, "git-rpm-test/", None) + class GitRpmTest(unittest.TestCase): @classmethod @@ -293,3 +325,19 @@ class GitRpmTest(unittest.TestCase): finally: shutil.rmtree(temp_dir) + + +class GitRpmBuildTest(unittest.TestCase): + def get_base_dir_test(self): + """Make sure base_dir is created""" + gitRpm = GitRpmBuild("rpmtest", "1.0.0", "1", ["noarch"]) + base_dir = gitRpm.get_base_dir() + self.assertTrue("lorax-git-rpm" in base_dir) + gitRpm.cleanup_tmpdir() + + def short_base_dir_test(self): + """Make sure cleanup of an unusually short base_dir fails""" + gitRpm = GitRpmBuild("rpmtest", "1.0.0", "1", ["noarch"]) + gitRpm._base_dir = "/aa/" + with self.assertRaises(RuntimeError): + gitRpm.cleanup_tmpdir()