#237 Fix PungiLogs exception when toplevel_work_dir does not exist.
Merged 7 months ago by jkaluza. Opened 7 months ago by jkaluza.

@@ -697,9 +697,13 @@ 

              else:

                  log.exception("Error while generating compose %d", compose_id)

  

-             pungi_logs = PungiLogs(compose)

-             state_reason = "Error while generating compose: {}\n{}".format(

-                 str(e), pungi_logs.get_error_string())

+             state_reason = "Error while generating compose: {}".format(str(e))

+ 

+             try:

+                 pungi_logs = PungiLogs(compose)

+                 state_reason += "\n{}".format(pungi_logs.get_error_string())

+             except Exception:

+                 log.exception("Exception raised when getting Pungi logs.")

  

              compose.transition(COMPOSE_STATES["failed"], state_reason)

  

file modified
+9 -3

@@ -449,10 +449,13 @@ 

      @property

      def global_log_path(self):

          """

-         Returns the path to pungi.global.log.

+         Returns the path to pungi.global.log if it exists.

          """

+         toplevel_work_dir = self.compose.toplevel_work_dir

+         if not toplevel_work_dir:

+             return None

          return os.path.join(

-             self.compose.toplevel_work_dir, "logs", "global", "pungi.global.log")

+             toplevel_work_dir, "logs", "global", "pungi.global.log")

  

      def _get_global_log_errors(self):

          """

@@ -463,8 +466,11 @@ 

          :return: List of error strings.

          """

          errors = []

+         global_log_path = self.global_log_path

+         if not global_log_path:

+             return errors

          try:

-             with open(self.global_log_path, "r") as global_log:

+             with open(global_log_path, "r") as global_log:

                  error = ""

                  for line in global_log.readlines():

                      idx = line.find("[ERROR   ]")

@@ -555,6 +555,28 @@ 

              r'Error while generating compose: Expected exception\n'

              'Compose failed for unknown reason*')

  

+     @patch("odcs.server.backend.resolve_compose")

+     @patch("odcs.server.backend.generate_pungi_compose")

+     @patch("odcs.server.pungi.PungiLogs.get_error_string")

+     def test_generate_compose_pungi_logs_exceptions(

+             self, get_error_string, generate_pungi_compose, resolve_compose):

+         get_error_string.side_effect = RuntimeError("PungiLogs Expected exception")

+         generate_pungi_compose.side_effect = RuntimeError("Expected exception")

+ 

+         c = Compose.create(

+             db.session, "me", PungiSourceType.KOJI_TAG, "foo-1",

+             COMPOSE_RESULTS["repository"], 3600)

+         db.session.add(c)

+         db.session.commit()

+ 

+         generate_compose(1)

+ 

+         c1 = Compose.query.filter(Compose.id == 1).one()

+         self.assertEqual(c1.state, COMPOSE_STATES["failed"])

+         self.assertRegexpMatches(

+             c1.state_reason,

+             r'Error while generating compose: Expected exception*')

+ 

      @patch('odcs.server.backend.tag_changed', return_value=True)

      @patch('odcs.server.backend.create_koji_session')

      def test_resolve_compose_from_koji_tag_get_last_event_if_tag_changed(

@@ -346,6 +346,13 @@ 

          errors = pungi_logs.get_error_string()

          self.assertEqual(errors, "")

  

+     def test_toplevel_work_dir(self):

+         # The self.glob is inherited from ModelsBaseTest.

+         self.glob.return_value = []

+         pungi_logs = PungiLogs(self.compose)

+         errors = pungi_logs.get_error_string()

+         self.assertEqual(errors, "")

+ 

  

  class TestPungiRunroot(unittest.TestCase):

  

This can happen for example when Pulp compose fails for some reason,
for example when the requested content set does not exist in Pulp.

This also catches any possible future PungiLogs exception and logs it,
so these exceptions won't be lost in the future again.

Pull-Request has been merged by jkaluza

7 months ago