#280 Remove the Mock chroot also in case of exception in runroot command.
Merged 4 years ago by jkaluza. Opened 4 years ago by jkaluza.
jkaluza/odcs rmtree-exception  into  master

@@ -292,16 +292,24 @@ 

          # here, otherwise Lorax fails.

          args = ["--old-chroot", "--chroot", "--"] + sh_wrapper

          execute_mock(runroot_key, args, False)

-     finally:

-         # In the end of run, umount the conf.targetdir.

+ 

+     except Exception:

+         # In the case of Exception, always call rmtree, because Pungi will

+         # not call the "rpm -qa" last command, so we would never remove

+         # the chroot.

          undo_mounts(rootdir, mounts)

-         # TODO: Pungi so far does not indicate anyhow that the runroot root can

-         # be removed. The last command is always "rpm -qa ...", so we use it

-         # as a mark for now that runroot root can be removed.

-         # We should try improving Pungi OpenSSH runroot method to remove this

-         # workaround later.

-         if cmd[0] == "rpm":

-             rmtree_skip_mounts(rootdir, mounts)

+         rmtree_skip_mounts(rootdir, mounts)

+         raise

+ 

+     # In the end of run, umount the conf.targetdir.

+     undo_mounts(rootdir, mounts)

+     # TODO: Pungi so far does not indicate anyhow that the runroot root can

+     # be removed. The last command is always "rpm -qa ...", so we use it

+     # as a mark for now that runroot root can be removed.

+     # We should try improving Pungi OpenSSH runroot method to remove this

+     # workaround later.

+     if cmd[0] == "rpm":

+         rmtree_skip_mounts(rootdir, mounts)

  

  

  def mock_runroot_main(argv=None):

@@ -91,6 +91,21 @@ 

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

          rmtree_skip_mounts.assert_called_once()

  

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

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

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

+     def test_mock_runroot_run_exception(self, rmtree_skip_mounts, execute_cmd, execute_mock):

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

+         with self.assertRaises(RuntimeError):

+             mock_runroot_run("foo-bar", ["df", "-h"])

+ 

+         execute_mock.assert_called_once_with('foo-bar', [

+             '--old-chroot', '--chroot', '--', '/bin/sh', '-c', '{ df -h; }'], False)

+         execute_cmd.assert_any_call([

+             'mount', '-o', 'bind', AnyStringWith('test_composes'), AnyStringWith('test_composes')])

+         execute_cmd.assert_any_call(['umount', '-l', AnyStringWith('test_composes')])

+         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")

Before this commit, in the case of some error when running the
runroot command, the Mock chroot for this runroot command was
never removed. This is big issue, because these chroots could
consume all the free space on root fs.

In this commit, the Mock chroot is removed on any issue with the
runroot command execution.

Pull-Request has been merged by jkaluza

4 years ago