From dec0e1d551f27b05938b37c702fbccec1df49737 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Thu, 3 Apr 2014 19:11:58 +0300 Subject: [PATCH] Handle gbp crashes gracefully Catch unhandled exceptions in gitbuildpackage[-rpm] in which case print a traceback and return an error. Change-Id: I9d3f51ee5a2f63e1656ae06c1f808a1f418829d6 --- obs_service_gbp/command.py | 9 +++++++-- tests/test_obs_service_gbp.py | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/obs_service_gbp/command.py b/obs_service_gbp/command.py index c474ef2..4b684dd 100644 --- a/obs_service_gbp/command.py +++ b/obs_service_gbp/command.py @@ -29,8 +29,8 @@ from gbp.scripts.buildpackage import main as gbp_deb from gbp.scripts.buildpackage_rpm import main as gbp_rpm from obs_service_gbp import LOGGER, gbplog -from obs_service_gbp_utils import GbpServiceError, fork_call, sanitize_uid_gid -from obs_service_gbp_utils import write_treeish_meta +from obs_service_gbp_utils import GbpServiceError, GbpChildBTError, fork_call +from obs_service_gbp_utils import sanitize_uid_gid, write_treeish_meta from gbp_repocache import CachedRepo, CachedRepoError import gbp_repocache @@ -142,6 +142,11 @@ def gbp_export(repo, args, config): for fname in os.listdir(tmp_out): shutil.move(os.path.join(tmp_out, fname), os.path.join(args.outdir, fname)) + except GbpChildBTError as err: + LOGGER.error('Unhandled exception in GBP:\n' + '%s', err.prettyprint_tb()) + LOGGER.error('Failed to export packaging files') + return 1 except GbpServiceError as err: LOGGER.error('Internal service error when trying to run GBP: %s', err) LOGGER.error('This is most likely a configuration error (or a BUG)!') diff --git a/tests/test_obs_service_gbp.py b/tests/test_obs_service_gbp.py index 66cfb30..27123db 100644 --- a/tests/test_obs_service_gbp.py +++ b/tests/test_obs_service_gbp.py @@ -20,6 +20,7 @@ import grp import json +import mock import os import stat from nose.tools import assert_raises, eq_, ok_ # pylint: disable=E0611 @@ -28,6 +29,15 @@ from obs_service_gbp.command import main as service from tests import UnitTestsBase +class FakeGbpError(Exception): + """Exception for testing gbp crashes""" + pass + +def _mock_gbp(): + """Fake gbp main function for testing""" + raise FakeGbpError() + + class TestService(UnitTestsBase): """Base class for unit tests""" s_rwx = stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC @@ -88,6 +98,16 @@ class TestService(UnitTestsBase): eq_(service(['--url', self.orig_repo.path, '--deb=yes', '--revision=source']), 3) + @mock.patch('obs_service_gbp.command.gbp_deb', _mock_gbp) + def test_deb_crash(self): + """Test crash in git-buildpackage""" + eq_(service(['--url', self.orig_repo.path, '--revision=deb']), 1) + + @mock.patch('obs_service_gbp.command.gbp_rpm', _mock_gbp) + def test_rpm_crash(self): + """Test crash in git-buildpackage-rpm""" + eq_(service(['--url', self.orig_repo.path, '--revision=rpm']), 1) + def test_options_outdir(self): """Test the --outdir option""" outdir = os.path.join(self.tmpdir, 'outdir') -- 2.7.4