def RunCmd(cmd, env=None, output=None, cwd="/", reset_env=False,
- interactive=False, timeout=None, noclose_fds=None):
+ interactive=False, timeout=None, noclose_fds=None,
+ _postfork_fn=None):
"""Execute a (shell) command.
The command should not read from its standard input, as it will be
@type noclose_fds: list
@param noclose_fds: list of additional (fd >=3) file descriptors to leave
open for the child process
+ @param _postfork_fn: Callback run after fork but before timeout (unittest)
@rtype: L{RunResult}
@return: RunResult instance
@raise errors.ProgrammerError: if we call this when forks are disabled
if output is None:
out, err, status, timeout_action = _RunCmdPipe(cmd, cmd_env, shell, cwd,
interactive, timeout,
- noclose_fds)
+ noclose_fds,
+ _postfork_fn=_postfork_fn)
else:
+ assert _postfork_fn is None, \
+ "_postfork_fn not supported if output provided"
timeout_action = _TIMEOUT_NONE
status = _RunCmdFile(cmd, cmd_env, shell, output, cwd, noclose_fds)
out = err = ""
def _RunCmdPipe(cmd, env, via_shell, cwd, interactive, timeout, noclose_fds,
- _linger_timeout=constants.CHILD_LINGER_TIMEOUT):
+ _linger_timeout=constants.CHILD_LINGER_TIMEOUT,
+ _postfork_fn=None):
"""Run a command and return its output.
@type cmd: string or list
@type noclose_fds: list
@param noclose_fds: list of additional (fd >=3) file descriptors to leave
open for the child process
+ @param _postfork_fn: Function run after fork but before timeout (unittest)
@rtype: tuple
@return: (out, err, status)
cwd=cwd,
preexec_fn=preexec_fn)
+ if _postfork_fn:
+ _postfork_fn(child.pid)
+
out = StringIO()
err = StringIO()
import os
import stat
import time
+import select
import signal
from ganeti import constants
self.assert_(utils.RunInSeparateProcess(self._TestRealProcess))
+class _PostforkProcessReadyHelper:
+ """A helper to use with _postfork_fn in RunCmd.
+
+ It makes sure a process has reached a certain state by reading from a fifo.
+
+ @ivar write_fd: The fd number to write to
+
+ """
+ def __init__(self, timeout):
+ """Initialize the helper.
+
+ @param fifo_dir: The dir where we can create the fifo
+ @param timeout: The time in seconds to wait before giving up
+
+ """
+ self.timeout = timeout
+ (self.read_fd, self.write_fd) = os.pipe()
+
+ def Ready(self, pid):
+ """Waits until the process is ready.
+
+ @param pid: The pid of the process
+
+ """
+ (read_ready, _, _) = select.select([self.read_fd], [], [], self.timeout)
+
+ if not read_ready:
+ # We hit the timeout
+ raise AssertionError("Timeout %d reached while waiting for process %d"
+ " to become ready" % (self.timeout, pid))
+
+ def Cleanup(self):
+ """Cleans up the helper.
+
+ """
+ os.close(self.read_fd)
+ os.close(self.write_fd)
+
+
class TestRunCmd(testutils.GanetiTestCase):
"""Testing case for the RunCmd function"""
self.fifo_file = os.path.join(self.fifo_tmpdir, "ganeti_test_fifo")
os.mkfifo(self.fifo_file)
+ # If the process is not ready after 20 seconds we have bigger issues
+ self.proc_ready_helper = _PostforkProcessReadyHelper(20)
+
def tearDown(self):
+ self.proc_ready_helper.Cleanup()
shutil.rmtree(self.fifo_tmpdir)
testutils.GanetiTestCase.tearDown(self)
self.assertEqual(result.output, "")
def testTimeoutClean(self):
- cmd = "trap 'exit 0' TERM; read < %s" % self.fifo_file
- result = utils.RunCmd(["/bin/sh", "-c", cmd], timeout=0.2)
+ cmd = ("trap 'exit 0' TERM; echo >&%d; read < %s" %
+ (self.proc_ready_helper.write_fd, self.fifo_file))
+ result = utils.RunCmd(["/bin/sh", "-c", cmd], timeout=0.2,
+ noclose_fds=[self.proc_ready_helper.write_fd],
+ _postfork_fn=self.proc_ready_helper.Ready)
self.assertEqual(result.exit_code, 0)
def testTimeoutKill(self):
- cmd = ["/bin/sh", "-c", "trap '' TERM; read < %s" % self.fifo_file]
+ cmd = ["/bin/sh", "-c", "trap '' TERM; echo >&%d; read < %s" %
+ (self.proc_ready_helper.write_fd, self.fifo_file)]
timeout = 0.2
(out, err, status, ta) = \
utils.process._RunCmdPipe(cmd, {}, False, "/", False,
- timeout, None, _linger_timeout=0.2)
+ timeout, [self.proc_ready_helper.write_fd],
+ _linger_timeout=0.2,
+ _postfork_fn=self.proc_ready_helper.Ready)
self.assert_(status < 0)
self.assertEqual(-status, signal.SIGKILL)
def testTimeoutOutputAfterTerm(self):
- cmd = "trap 'echo sigtermed; exit 1' TERM; read < %s" % self.fifo_file
- result = utils.RunCmd(["/bin/sh", "-c", cmd], timeout=0.2)
+ cmd = ("trap 'echo sigtermed; exit 1' TERM; echo >&%d; read < %s" %
+ (self.proc_ready_helper.write_fd, self.fifo_file))
+ result = utils.RunCmd(["/bin/sh", "-c", cmd], timeout=0.2,
+ noclose_fds=[self.proc_ready_helper.write_fd],
+ _postfork_fn=self.proc_ready_helper.Ready)
self.assert_(result.failed)
self.assertEqual(result.stdout, "sigtermed\n")