rpmgit: catch potential errors while running git

Log them and report them as RuntimeError. Also add a couple tests for
them.
This commit is contained in:
Brian C. Lane 2019-03-08 16:43:26 -08:00
parent c26477a63c
commit 61efa91a03
2 changed files with 64 additions and 4 deletions

View File

@ -59,7 +59,11 @@ class GitArchiveTarball:
# Clone the repository into a temporary location # Clone the repository into a temporary location
cmd = ["git", "clone", self._gitRepo["repo"], joinpaths(sourcesDir, "gitrepo")] cmd = ["git", "clone", self._gitRepo["repo"], joinpaths(sourcesDir, "gitrepo")]
log.debug(cmd) 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() oldcwd = os.getcwd()
try: try:
@ -72,7 +76,11 @@ class GitArchiveTarball:
cmd = ["git", "archive", "--prefix", self._gitRepo["rpmname"] + "/", "-o", joinpaths(sourcesDir, self.sourceName), self._gitRepo["ref"]] cmd = ["git", "archive", "--prefix", self._gitRepo["rpmname"] + "/", "-o", joinpaths(sourcesDir, self.sourceName), self._gitRepo["ref"]]
log.debug(cmd) 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: finally:
# Cleanup even if there was an error # Cleanup even if there was an error
os.chdir(oldcwd) os.chdir(oldcwd)
@ -197,6 +205,10 @@ def create_gitrpm_repo(results_dir, recipe):
make_git_rpm(r, gitrepo) make_git_rpm(r, gitrepo)
cmd = ["createrepo_c", gitrepo] cmd = ["createrepo_c", gitrepo]
log.debug(cmd) 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 return gitrepo

View File

@ -24,7 +24,7 @@ import tarfile
import tempfile import tempfile
import unittest 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 from pylorax.sysutils import joinpaths
def _setup_git_repo(self): def _setup_git_repo(self):
@ -175,6 +175,38 @@ class GitArchiveTest(unittest.TestCase):
archive = GitArchiveTarball(git_repo["repos"]["git"][0]) archive = GitArchiveTarball(git_repo["repos"]["git"][0])
self._check_tar(archive, "git-rpm-test/", "second") 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): class GitRpmTest(unittest.TestCase):
@classmethod @classmethod
@ -293,3 +325,19 @@ class GitRpmTest(unittest.TestCase):
finally: finally:
shutil.rmtree(temp_dir) 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()