def test_start_after_pause(daemon_setup): """Daemon really pauses subprocess. In case the subprocess doesn't pause, the command should complete instantly after starting the daemon again. """ # Add command and make sure it's running execute_add({'command': 'sleep 2 && sleep 2'}) status = get_status() assert status['status'] == 'running' # Pause the daemon and the process command_factory('pause', {'wait': False}) status = get_status() assert status['data'][0]['status'] == 'paused' time.sleep(5) # Start the daemon again assert that it is still running command_factory('start') status = get_status() assert status['data'][0]['status'] == 'running' # Wait for the process to finish status = wait_for_process(0) assert status['status'] == 'running' assert status['data'][0]['status'] == 'done'
def test_stash_running(daemon_setup): """Stash a running process.""" # Add process and pause it execute_add('sleep 60') # Stash it, but it should fail status = command_factory('stash')({'key': 0}) assert status['status'] == 'error'
def test_kill(daemon_setup): """Kill a running process.""" execute_add({'command': 'sleep 60'}) command_factory('kill', {'remove': False}) status = get_status() assert status['status'] == 'paused' assert status['process'] == 'No running process'
def test_kill(daemon_setup, signal): """Kill a running process.""" execute_add('sleep 60') command_factory('kill')({'signal': signal}) status = wait_for_process(0) assert status['status'] == 'paused' assert status['data'][0]['status'] == 'failed'
def test_log_running(daemon_setup, directory_setup): """The logging command executes without failing. This implies that the daemon runs and creates proper log files. """ execute_add('sleep 60') execute_log({'key': 0}, directory_setup[0])
def test_log_specific(daemon_setup, directory_setup): """The logging command executes without failing. This implies that the daemon runs and creates proper log files. """ execute_add('sleep 0.5') wait_for_process(0) execute_log({}, directory_setup[0])
def test_stop_remove(daemon_setup): """Stop a running process and remove it afterwards.""" execute_add({'command': 'sleep 2'}) command_factory('stop', {'remove': True}) status = get_status() assert status['status'] == 'paused' assert status['process'] == 'No running process' assert status['data'] == 'Queue is empty'
def test_stash_running(daemon_setup): """Stash a running process.""" # Add sleep entry execute_add('sleep 60') # Try to stash it, but it should fail status = command_factory('stash')({'keys': [0]}) assert status['status'] == 'error'
def test_log(daemon_setup, directory_setup): """The default `log` command executes without failing. This implies that the daemon runs and creates proper log files. """ execute_add('ls') wait_for_process(0) execute_log({}, directory_setup[0])
def test_log_failed(daemon_setup, directory_setup): """The logging command executes without failing. This implies that the daemon runs and creates proper log files. """ execute_add('testfailcommand') wait_for_process(0) execute_log({'key': 0}, directory_setup[0])
def test_show(daemon_setup, directory_setup): """The show command executes without failing. This implies that the daemon is running and the stdout file in /tmp is properly created. """ execute_add('sleep 120') execute_show({'watch': False}, directory_setup[0])
def test_reset_running(daemon_setup): """Reset a daemon with running subprocesses.""" command_factory('start')() execute_add('sleep 60') execute_add('sleep 60') command_factory('reset')() status = command_factory('status')() assert status['status'] == 'running' assert status['data'] == 'Queue is empty'
def test_reset_running(daemon_setup): """Reset a daemon with running subprocesses.""" command_factory('start') execute_add({'command': 'sleep 60'}) execute_add({'command': 'sleep 60'}) command_factory('reset') status = get_status() assert status['status'] == 'running' assert status['data'] == 'Queue is empty'
def test_reset_paused(daemon_setup): """Reset the daemon.""" command_factory('pause') execute_add({'command': 'sleep 60'}) execute_add({'command': 'sleep 60'}) command_factory('reset') status = get_status() assert status['status'] == 'paused' assert status['data'] == 'Queue is empty'
def test_stash_paused(daemon_setup): """Stash a paused entry.""" # Add process and pause it execute_add('sleep 60') command_factory('pause')() # Try to stash it, but it should fail status = command_factory('stash')({'keys': [0]}) assert status['status'] == 'error'
def test_log(daemon_setup): """The logging command executes without failing. This implies that the daemon is running and the stdout file in /tmp is properly created. """ execute_add({'command': 'sleep 0.5'}) wait_for_process(0) execute_show({'watch': False})
def test_switch(daemon_setup): """Switch the position of two commands in the queue.""" command_factory('pause')() execute_add('ls') execute_add('ls -l') command_factory('switch')({'first': 0, 'second': 1}) status = command_factory('status')() assert status['data'][0]['command'] == 'ls -l' assert status['data'][1]['command'] == 'ls'
def test_reset_paused(daemon_setup): """Reset the daemon.""" command_factory('pause')() execute_add('sleep 60') execute_add('sleep 60') command_factory('reset')() status = command_factory('status')() assert status['status'] == 'paused' assert status['data'] == 'Queue is empty'
def test_switch(daemon_setup): """Switch the position of two commands in the queue.""" command_factory('pause') execute_add({'command': 'ls'}) execute_add({'command': 'ls -l'}) command_factory('switch', {'first': 0, 'second': 1}) status = get_status() assert status['data'][0]['command'] == 'ls -l' assert status['data'][1]['command'] == 'ls'
def setup(processes=4, max_processes=3, sleep_time=60): # Set max processes to three concurrent processes command_factory('config')({ "option": "maxProcesses", "value": max_processes, }) # Add sleep commands for i in range(processes): execute_add('sleep {}'.format(sleep_time))
def test_show_specific_finished(daemon_setup, directory_setup): """The show command executes without failing. This implies that the daemon is running and the stdout file in /tmp is properly created. """ execute_add('ls') wait_for_process(0) execute_show({'watch': False, 'key': 0}, directory_setup[0])
def test_switch_running(daemon_setup): """Switching the position of running command fails.""" execute_add({'command': 'sleep 60'}) execute_add({'command': 'ls -l'}) response = send_command({ 'mode': 'switch', 'first': 0, 'second': 1 }) assert response['status'] == 'error'
def test_restart(daemon_setup): """Restart a command.""" execute_add({'command': 'ls'}) wait_for_process(0) response = send_command({'mode': 'restart', 'key': 0}) assert response['status'] == 'success' status = get_status() assert len(status['data']) == 2 assert status['data'][1]['path'] == status['data'][0]['path'] assert status['data'][1]['command'] == status['data'][0]['command']
def test_restart(daemon_setup): """Restart a command.""" execute_add('ls') wait_for_process(0) response = command_factory('restart')({'key': 0}) assert response['status'] == 'success' status = command_factory('status')() assert len(status['data']) == 2 assert status['data'][1]['path'] == status['data'][0]['path'] assert status['data'][1]['command'] == status['data'][0]['command']
def test_restart_running(daemon_setup): """Restart a running command fails.""" execute_add('sleep 5') response = command_factory('restart')({'keys': [0]}) assert response['status'] == 'error' # There is still only one running entry in the queue status = command_factory('status')() assert len(status['data']) == 1 assert status['data'][0]['status'] == 'running'
def test_remove(daemon_setup): """Remove a process from the queue.""" command_factory("pause") status = get_status() assert status["status"] == "paused" execute_add({"command": "ls"}) response = send_command({"mode": "remove", "key": 0}) assert response["status"] == "success" status = get_status() assert status["data"] == "Queue is empty"
def test_remove(daemon_setup): """Remove a process from the queue.""" command_factory('pause')() status = command_factory('status')() assert status['status'] == 'paused' execute_add('ls') response = command_factory('remove')({'key': 0}) assert response['status'] == 'success' status = command_factory('status')() assert status['data'] == 'Queue is empty'
def test_pause(daemon_setup): """Pause the daemon.""" # Assert that the daemon is running after setup status = command_factory('status')() assert status['status'] == 'running' # Add a single command execute_add('sleep 60') command_factory('pause')() status = command_factory('status')() assert status['status'] == 'paused' assert status['data'][0]['status'] == 'paused'
def test_remove(daemon_setup): """Remove a process from the queue.""" # Pause the daemon. Otherwise we may try to remove a running entry. command_factory('pause')() # Add entry and instantly remove it. execute_add('ls') response = command_factory('remove')({'keys': [0]}) assert response['status'] == 'success' # The queue should be empty status = command_factory('status')() assert status['data'] == 'Queue is empty'
def test_stop_remove_resume(daemon_setup): """Everything works properly after remove stopping a subprocess.""" # Add status execute_add({'command': 'sleep 2'}) command_factory('stop', {'remove': True}) status = get_status() assert status['status'] == 'paused' # Old process should be execute_add({'command': 'sleep 2'}) command_factory('start') status = wait_for_process(1) assert status['status'] == 'running' assert status['data'][1]['status'] == 'done'
def test_waiting_pause(daemon_setup): """Daemon waits for process to finish. With `wait=True` as a parameter the daemon pauses, but waits for the current process to finish instead of pausing it. """ # Add sleep command and pause it with `'wait': True` execute_add('sleep 2') command_factory('pause')({'wait': True}) # The paused daemon should wait for the process to finish status = wait_for_process(0) assert status['status'] == 'paused' assert status['data'][0]['status'] == 'done'
def test_kill_single(daemon_setup): """Kill a running process and check if it finishes as failed.""" # We need to add some bash syntax with "&&" otherwise python won't spawn a # shell parent process. But we need this for debugging. execute_add('sleep 60 && ls') # Unfortunately this is necessary as the shell parent process needs some time to spawn it's children time.sleep(1) # Kill the children of the parent process command_factory('kill')({'keys': [0], 'signal': 'sigkill'}) command_factory('status')() status = wait_for_process(0) assert status['status'] == 'running' assert status['data'][0]['status'] == 'failed'
def test_remove_multiple_specific(daemon_setup): """Remove various entries from the queue.""" # Pause the daemon. command_factory('pause')() # Add 4 entries to get a `failing`, `done`, `queued` and `running` entry. execute_add('failingtestcommand') execute_add('sleep 60') execute_add('ls') execute_add('ls') # Start 0, 1 and 2 and wait for the `failed` and `done` entry to finish. response = command_factory('start')({'keys': [0, 1, 2]}) status = wait_for_processes([0, 2]) assert status['data'][0]['status'] == 'failed' assert status['data'][1]['status'] == 'running' assert status['data'][2]['status'] == 'done' assert status['data'][3]['status'] == 'queued' # Remove all entries. The response should be an error, as we try to remove # a running process. response = command_factory('remove')({'keys': [0, 1, 2, 3]}) assert response['status'] == 'error' status = command_factory('status')() assert 0 not in status['data'] assert status['data'][1]['status'] == 'running' assert 2 not in status['data'] assert 3 not in status['data']
def test_restart(daemon_setup): """Restart a command.""" # Add command and let it finish execute_add('sleep 1') wait_for_process(0) # Restart the command. This should clone the entry and add it to the queue. response = command_factory('restart')({'keys': [0]}) assert response['status'] == 'success' status = command_factory('status')() assert len(status['data']) == 2 assert status['data'][1]['path'] == status['data'][0]['path'] assert status['data'][1]['command'] == status['data'][0]['command'] assert status['data'][1]['status'] == 'running'
def test_edit(daemon_setup): """It's possible to edit the command of a queue entry.""" # Pause, add command and check that it has been added correctly command_factory('pause')() execute_add('ls') status = command_factory('status')() assert status['data'][0]['command'] == 'ls' assert status['data'][0]['path'] == '/tmp' # Edit the command command_factory('edit')({'key': 0, 'command': 'ls -al'}) # Check for changed command status = command_factory('status')() assert status['data'][0]['command'] == 'ls -al' assert status['data'][0]['path'] == '/tmp'
def test_waiting_pause(daemon_setup): """Daemon waits for process to finish. With `wait=True` as a parameter the daemon pauses, but waits for the current process to finish instead of pausing it. """ # Add sleep command execute_add({'command': 'sleep 2'}) status = get_status() assert status['status'] == 'running' # Pause it with `'wait': True` command_factory('pause', {'wait': True}) # The paused daemon should wait for the process to finish status = wait_for_process(0) assert status['status'] == 'paused' assert status['data'][0]['status'] == 'done'
def test_kill_shell_of_single_with_multiple_commands(daemon_setup): """Assure that the signal will be sent to the shell process with '-a' provided.""" # Once the first sleep finishes the second sleep process will be spawned. # By this we can assure that the signal is only sent to the child processes. execute_add('sleep 60 ; sleep 60') # Unfortunately this is necessary as the shell parent process needs some time to spawn it's children time.sleep(1) # Kill the shell process as well as the child processes. command_factory('kill')({'keys': [0], 'signal': 'sigkill', 'all': True}) # Give the shell process some time to die and pueue to clean up the mess. status = wait_for_process(0) # Assert that the queue entry is finished and failed assert status['status'] == 'running' assert status['data'][0]['status'] == 'failed'
def test_stash(daemon_setup): """Kill a running process.""" # Pause daemon to prevent the process to start command_factory('pause')() # Add process execute_add('sleep 60') # Stash it command_factory('stash')({'key': 0}) # Check if it's stashed status = command_factory('status')() assert status['status'] == 'paused' assert status['data'][0]['status'] == 'stashed' # Enqueue, start and ensure that its running command_factory('enqueue')({'key': 0}) command_factory('start')() status = command_factory('status')() assert status['data'][0]['status'] == 'running'
def test_kill_single_with_multiple_commands(daemon_setup): """Assure that the signal will be only send to the shells child processes.""" # Once the first sleep finishes the second sleep process will be spawned. # By this we can assure that the signal is only sent to the child processes. execute_add('sleep 60 ; sleep 60') # Unfortunately this is necessary as the shell parent process needs some time to spawn it's children time.sleep(1) # Kill the children of the parent process command_factory('kill')({'keys': [0], 'signal': 'sigkill'}) # Give the shell process some time to clean the old process and spawn the new one. time.sleep(1) # Assert that the queue entry is still running status = command_factory('status')() assert status['status'] == 'running' assert status['data'][0]['status'] == 'running'
def test_kill_remove_resume(daemon_setup): """Old `done` and `failed` entries will be deleted.""" # Add a command that fails, and finishes as well as queued and running processes execute_add('failingstufftest') execute_add('ls') execute_add('ls') execute_add('sleep 60') execute_add('ls') status = wait_for_process(2) # Trigger the clear command_factory('clear')() # Check that 0,1,2 are missing, 3 is 'running' and 4 is 'queued' status = command_factory('status')() assert 0 not in status['data'] assert 1 not in status['data'] assert 2 not in status['data'] assert status['data'][3]['status'] == 'running' assert status['data'][4]['status'] == 'queued'
def test_multiple_stash_enqueue(daemon_setup): """Stash and enqueue multiple valid entries.""" # Pause daemon and add multiple entries command_factory('pause')() execute_add('ls') execute_add('sleep 1') execute_add('ls') # Stash all commands and ensure that they are stashed status = command_factory('stash')({'keys': [0, 1, 2]}) assert status['status'] == 'success' status = command_factory('status')() assert status['data'][0]['status'] == 'stashed' assert status['data'][1]['status'] == 'stashed' assert status['data'][2]['status'] == 'stashed' # Enqueue all commands and ensure that they are queued status = command_factory('enqueue')({'keys': [0, 1, 2]}) assert status['status'] == 'success' status = command_factory('status')() assert status['data'][0]['status'] == 'queued' assert status['data'][1]['status'] == 'queued' assert status['data'][2]['status'] == 'queued'
def test_multiple_stash_enqueue_invalid(daemon_setup): """Stash and enqueue multiple entries, but include invalid keys for enqueue.""" # Pause daemon and add multiple entries command_factory('pause')() execute_add('ls') execute_add('sleep 1') execute_add('ls') # Stash all commands and ensure that they are stashed status = command_factory('stash')({'keys': [1, 2]}) assert status['status'] == 'success' status = command_factory('status')() assert status['data'][0]['status'] == 'queued' assert status['data'][1]['status'] == 'stashed' assert status['data'][2]['status'] == 'stashed' # Enqueue all commands and ensure that they are queued # Response status should be `error` anyway, as we sent invalid keys status = command_factory('enqueue')({'keys': [0, 1, 2, 4]}) assert status['status'] == 'error' status = command_factory('status')() assert status['data'][0]['status'] == 'queued' assert status['data'][1]['status'] == 'queued' assert status['data'][2]['status'] == 'queued'
def test_restart_running(daemon_setup): """Restart a running command fails.""" execute_add({'command': 'sleep 5'}) response = send_command({'mode': 'restart', 'key': 0}) assert response['status'] == 'error'
def test_remove_running(daemon_setup): """Can't remove a running process.""" execute_add({"command": "sleep 60"}) response = send_command({"mode": "remove", "key": 0}) assert response["status"] == "error"