Handle gbp crashes gracefully
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Thu, 3 Apr 2014 16:11:58 +0000 (19:11 +0300)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Tue, 8 Apr 2014 09:17:46 +0000 (12:17 +0300)
Catch unhandled exceptions in gitbuildpackage[-rpm] in which case print
a traceback and return an error.

Change-Id: I9d3f51ee5a2f63e1656ae06c1f808a1f418829d6

obs_service_gbp/command.py
tests/test_obs_service_gbp.py

index c474ef2..4b684dd 100644 (file)
@@ -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)!')
index 66cfb30..27123db 100644 (file)
@@ -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')