#277 Remove the Mock chroot in case "init" or "install" phase fails.
Merged 4 years ago by jkaluza. Opened 4 years ago by jkaluza.
jkaluza/odcs rmtree-exception  into  master

@@ -231,7 +231,13 @@ 

      print(runroot_key)

  

      # Run the `mock --init` with some log files.

-     execute_mock(runroot_key, ["--init"])

+     try:

+         execute_mock(runroot_key, ["--init"])

+     except Exception:

+         rootdir = "/var/lib/mock/%s/root" % runroot_key

+         mounts = [conf.target_dir] + conf.runroot_extra_mounts

+         rmtree_skip_mounts(rootdir, mounts)

+         raise

  

  

  def raise_if_runroot_key_invalid(runroot_key):
@@ -254,7 +260,13 @@ 

      :param list packages: List of packages to install.

      """

      raise_if_runroot_key_invalid(runroot_key)

-     execute_mock(runroot_key, ["--install"] + packages)

+     try:

+         execute_mock(runroot_key, ["--install"] + packages)

+     except Exception:

+         rootdir = "/var/lib/mock/%s/root" % runroot_key

+         mounts = [conf.target_dir] + conf.runroot_extra_mounts

+         rmtree_skip_mounts(rootdir, mounts)

+         raise

  

  

  def mock_runroot_run(runroot_key, cmd):

@@ -40,18 +40,23 @@ 

      @patch("odcs.server.mock_runroot.create_koji_session")

      @patch("odcs.server.mock_runroot.execute_mock")

      @patch("odcs.server.mock_runroot.print", create=True)

-     def test_mock_runroot_init(self, fake_print, execute_mock, create_koji_session):

+     @patch("odcs.server.mock_runroot.rmtree_skip_mounts")

+     def test_mock_runroot_init(

+             self, rmtree_skip_mounts, fake_print, execute_mock, create_koji_session):

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

          koji_session = create_koji_session.return_value

          koji_session.getRepo.return_value = {"id": 1}

  

          m = mock_open()

          with patch('odcs.server.mock_runroot.open', m, create=True):

-             mock_runroot_init("f30-build")

+             with self.assertRaises(RuntimeError):

+                 mock_runroot_init("f30-build")

  

          fake_print.assert_called_once()

          m.return_value.write.assert_called_once_with(AnyStringWith("f30-build"))

  

          execute_mock.assert_called_once_with(AnyStringWith("-"), ['--init'])

+         rmtree_skip_mounts.assert_called_once()

  

      def test_raise_if_runroot_key_invalid(self):

          with self.assertRaises(ValueError):
@@ -78,6 +83,14 @@ 

          mock_runroot_install("foo-bar", ["lorax", "dracut"])

          execute_mock.assert_called_once_with('foo-bar', ['--install', 'lorax', 'dracut'])

  

+     @patch("odcs.server.mock_runroot.execute_mock")

+     @patch("odcs.server.mock_runroot.rmtree_skip_mounts")

+     def test_mock_runroot_install_exception(self, rmtree_skip_mounts, execute_mock):

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

+         with self.assertRaises(RuntimeError):

+             mock_runroot_install("foo-bar", ["lorax", "dracut"])

+         rmtree_skip_mounts.assert_called_once()

+ 

      @patch("odcs.server.mock_runroot.os.rmdir")

      @patch("odcs.server.mock_runroot.os.listdir")

      @patch("odcs.server.mock_runroot.os.lstat")

So far we only removed the Mock chroot after the "run" phase, but in
case there is some issue before we even get to that phase during the
Mock "init" or "install" phases, the chroot data would remain on
filesystem forever.

In this commit, this is fixed by catching any issue in "init" and
"install" phases and removing the Mock chroot before re-raising.

rebased onto d803c56

4 years ago

Pull-Request has been merged by jkaluza

4 years ago