Esempio n. 1
0
def test_atomiccmd__cleanup_sigterm():
    sigs_sent, exit_called = {}, []

    def _wrap_killpg(pid, sig):
        assert pid not in sigs_sent
        sigs_sent[pid] = sig

    def _wrap_exit(rc):
        exit_called.append(rc)

    _procs = [
        flexmock(pid=7913),
        # I've got the same combination on my luggage!
        flexmock(pid=12345)
    ]

    assert not pypeline.atomiccmd.command._PROCS
    with Monkeypatch("pypeline.atomiccmd.command._PROCS", _procs):
        assert_equal(len(pypeline.atomiccmd.command._PROCS), 2)
        with Monkeypatch("os.killpg", _wrap_killpg):
            with Monkeypatch("sys.exit", _wrap_exit):
                pypeline.atomiccmd.command._cleanup_children(
                    signal.SIGTERM, None)

    assert_equal(exit_called, [-signal.SIGTERM])
    assert_equal(sigs_sent, {7913: signal.SIGTERM, 12345: signal.SIGTERM})
Esempio n. 2
0
 def __init__(self, root = "/tmp", subfolder = "xTMPx"):
     self._mkdir_patch = Monkeypatch("pypeline.common.fileutils.create_temp_dir", self._mkdir)
     self._rmdir_patch = Monkeypatch("os.rmdir", self._rmdir)
     self._root_dir = root
     self._sub_dir  = subfolder
     self._mkdir_called = False
     self._rmdir_called = False
Esempio n. 3
0
class MonkeypatchCreateTempDir:
    """Monkeypatches functions used by Node.run to setup the
    temporary folders. This is done to reduce the amount of
    file operations that actually need to be done."""
    def __init__(self, root = "/tmp", subfolder = "xTMPx"):
        self._mkdir_patch = Monkeypatch("pypeline.common.fileutils.create_temp_dir", self._mkdir)
        self._rmdir_patch = Monkeypatch("os.rmdir", self._rmdir)
        self._root_dir = root
        self._sub_dir  = subfolder
        self._mkdir_called = False
        self._rmdir_called = False

    def __enter__(self):
        self._mkdir_patch.__enter__()
        self._rmdir_patch.__enter__()
        return self

    def __exit__(self, type, value, traceback): # pylint: disable=W0622
        self._mkdir_patch.__exit__(type, value, traceback)
        self._rmdir_patch.__exit__(type, value, traceback)
        if not value:
            assert self._mkdir_called
            assert self._rmdir_called

    def _mkdir(self, path):
        self._mkdir_called = True
        assert_equal(self._root_dir, path)
        return os.path.join(self._root_dir, self._sub_dir)

    def _rmdir(self, path):
        self._rmdir_called = True
        return assert_equal(os.path.join(self._root_dir, self._sub_dir), path)
Esempio n. 4
0
def test_atomiccmd__cleanup_sigterm__dead_weakrefs():
    exit_called = []
    procs_wrapper = [weakref.ref(Monkeypatch("sys.exit", None))]

    assert_equal(procs_wrapper[0](), None)
    def _wrap_killpg(_pid, _sig):
        assert False # pragma: no coverage
    def _wrap_exit(rc):
        exit_called.append(rc)

    with Monkeypatch("pypeline.atomiccmd.command._PROCS", procs_wrapper):
        with Monkeypatch("os.killpg", _wrap_killpg):
            with Monkeypatch("sys.exit", _wrap_exit):
                pypeline.atomiccmd.command._cleanup_children(signal.SIGTERM, None)
    assert_equal(exit_called, [-signal.SIGTERM])
Esempio n. 5
0
def test_make_dirs__creation_preemted(temp_folder):
    unwrapped, preempted = os.makedirs, []

    def _wrap_os_makedirs(*args, **kwargs):
        # Simulate somebody else creating the directory first
        preempted.append(True)
        unwrapped(*args, **kwargs)
        unwrapped(*args, **kwargs)

    with Monkeypatch("os.makedirs", _wrap_os_makedirs):
        work_folder = os.path.join(temp_folder, "test")
        assert not make_dirs(work_folder)
        assert os.path.exists(work_folder)
        assert_equal(os.listdir(temp_folder), ["test"])
        assert_equal(preempted, [True])
Esempio n. 6
0
    def _do_test_atomiccmd__terminate(temp_folder, raise_on_terminate):
        cmd = AtomicCmd(("sleep", "10"))
        cmd.run(temp_folder)

        killpg_was_called = []
        def _wrap_killpg(pid, sig):
            assert_equal(pid, cmd._proc.pid)
            assert_equal(sig, signal.SIGTERM)
            killpg_was_called.append(True)
            if raise_on_terminate:
                raise OSError("KABOOM!")

        with Monkeypatch("os.killpg", _wrap_killpg):
            cmd.terminate()
        cmd.terminate()
        assert_equal(cmd.join(), ["SIGTERM"])
        assert killpg_was_called
Esempio n. 7
0
def test_create_temp_dir__creation_preempted(temp_folder):
    unwrapped, preempted_once = os.makedirs, []

    def _wrap_os_makedirs(*args, **kwargs):
        # Simulate somebody else creating the directory first
        if not preempted_once:
            unwrapped(*args, **kwargs)
            preempted_once.append(True)
        unwrapped(*args, **kwargs)

    with Monkeypatch("os.makedirs", _wrap_os_makedirs):
        assert not os.listdir(temp_folder)
        work_dir = create_temp_dir(temp_folder)
        assert os.path.exists(temp_folder)
        dirs = os.listdir(temp_folder)
        assert_equal(len(dirs), 2)
        assert_in(os.path.basename(work_dir), dirs)
        assert bool(preempted_once)
Esempio n. 8
0
def test_builder__finalize__calls_atomiccmd():
    was_called = []

    class _AtomicCmdMock:
        def __init__(self, *args, **kwargs):
            assert_equal(args,
                         (["echo", "-out", "%(OUT_FILE)s", "%(IN_FILE)s"], ))
            assert_equal(kwargs, {
                "IN_FILE": "/in/file",
                "OUT_FILE": "/out/file",
                "set_cwd": True
            })
            was_called.append(True)

    with Monkeypatch("pypeline.atomiccmd.builder.AtomicCmd", _AtomicCmdMock):
        builder = AtomicCmdBuilder("echo", set_cwd=True)
        builder.add_option("-out", "%(OUT_FILE)s")
        builder.add_value("%(IN_FILE)s")
        builder.set_kwargs(OUT_FILE="/out/file", IN_FILE="/in/file")

        builder.finalize()
        assert was_called
Esempio n. 9
0
def test_create_temp_dir__permission_denied():
    def _wrap_os_makedirs(*_args, **_kwargs):
        raise OSError((errno.EACCES, "Simulated premission denied"))

    with Monkeypatch("os.makedirs", _wrap_os_makedirs):
        assert_raises(OSError, create_temp_dir, "/tmp")