def test_forgotten_stop(): """ Test if SimpleExecutor subprocess is killed after an instance is deleted. Existence can end because of context scope end or by calling 'del'. If someone forgot to stop() or kill() subprocess it should be killed by default on instance cleanup. """ mark = str(uuid.uuid1()) # We cannot simply do `sleep 300 #<our-uuid>` in a shell because in that # case bash (default shell on some systems) does `execve` without cloning # itself - that means there will be no process with commandline like: # '/bin/sh -c sleep 300 && true #<our-uuid>' - instead that process would # get substituted with 'sleep 300' and the marked commandline would be # overwritten. # Injecting some flow control (`&&`) forces bash to fork properly. marked_command = f"sleep 300 && true #{mark!s}" executor = SimpleExecutor(marked_command, shell=True) executor.start() assert executor.running() is True assert mark in ps_aux(), "The test process should be running." del executor gc.collect() # to force 'del' immediate effect assert (mark not in ps_aux() ), "The test process should not be running at this point."
def test_forgotten_stop(): """ Test if SimpleExecutor subprocess is killed after an instance is deleted. Existence can end because of context scope end or by calling 'del'. If someone forgot to stop() or kill() subprocess it should be killed by default on instance cleanup. """ mark = str(uuid.uuid1()) # We cannot simply do `sleep 300 #<our-uuid>` in a shell because in that # case bash (default shell on some systems) does `execve` without cloning # itself - that means there will be no process with commandline like: # '/bin/sh -c sleep 300 && true #<our-uuid>' - instead that process would # get substituted with 'sleep 300' and the marked commandline would be # overwritten. # Injecting some flow control (`&&`) forces bash to fork properly. marked_command = 'sleep 300 && true #{0!s}'.format(mark) executor = SimpleExecutor(marked_command, shell=True) executor.start() assert executor.running() is True assert mark in ps_aux(), "The test process should be running." del executor gc.collect() # to force 'del' immediate effect assert mark not in ps_aux(), \ "The test process should not be running at this point."
def test_daemons_killing(): """ Test if all subprocesses of SimpleExecutor can be killed. The most problematic subprocesses are daemons or other services that change the process group ID. This test verifies that daemon process is killed after executor's kill(). """ executor = SimpleExecutor(('python', SAMPLE_DAEMON_PATH), shell=True) executor.start() time.sleep(2) assert executor.running() is not True, \ "Executor should not have subprocess running as it started a daemon." assert SAMPLE_DAEMON_PATH in ps_aux() executor.kill() assert SAMPLE_DAEMON_PATH not in ps_aux()
def test_mirakuru_cleanup(): """Test if cleanup_subprocesses is fired correctly on python exit.""" cmd = ''' python -c 'from mirakuru import SimpleExecutor; from time import sleep; import gc; gc.disable(); ex = SimpleExecutor(("python", "{0}")).start(); sleep(1); ' '''.format(sample_daemon_path) check_output(shlex.split(cmd.replace('\n', ''))) assert sample_daemon_path not in ps_aux()
def test_mirakuru_cleanup(): """Test if cleanup_subprocesses is fired correctly on python exit.""" cmd = ''' python -c 'from mirakuru import SimpleExecutor; from time import sleep; import gc; gc.disable(); ex = SimpleExecutor(("python", "{0}")).start(); sleep(1); ' '''.format(SAMPLE_DAEMON_PATH) check_output(shlex.split(cmd.replace('\n', ''))) assert SAMPLE_DAEMON_PATH not in ps_aux()
def test_mirakuru_cleanup(): """Test if cleanup_subprocesses is fired correctly on python exit.""" cmd = f""" python -c 'from mirakuru import SimpleExecutor; from time import sleep; import gc; gc.disable(); ex = SimpleExecutor( ("python", "{SAMPLE_DAEMON_PATH}")).start(); sleep(1); ' """ check_output(shlex.split(cmd.replace("\n", ""))) assert SAMPLE_DAEMON_PATH not in ps_aux()