Basic executors

Base executor with the most basic functionality.

class mirakuru.base.Executor(command, shell=False, timeout=None, sleep=0.1)[source]

Bases: object

Basic executor with the most basic functionality.

Initialize executor.

Parameters:
  • command (str) – command to run to start service
  • shell (bool) – see subprocess.Popen
  • timeout (int) – time to wait for process to start or stop. if None, wait indefinitely.
  • sleep (float) – how often to check for start/stop condition

Note

timeout set for executor is valid for all the level of waits on the way up. That means that if some more advanced executor sets timout to 10 seconds, and it’ll take 5 seconds for first check, second check will only have 5 seconds left.

_set_timeout(timeout=None)

Set timout for possible wait.

Parameters:timeout (int) – [optional] specific timeout to set. If not set, Executor._timeout will be used instead.
check_timeout()

Check if timeout has expired.

Returns True if there is no timeout set or the timeout has not expired. Kills the process and raises TimeoutExpired exception otherwise.

This method should be used in while loops waiting for some data.

Returns:True if timeout expired, False if not
Return type:bool
kill(wait=True)

Kill the process if running.

Parameters:wait (bool) – set to True to wait for the process to end, or False, to simply proceed after sending signal.
output()

Return process output.

process = None

A subprocess.Popen instance once process is started.

running()

Check if executor is running.

Returns:True if process is running, False otherwise
Return type:bool
start()

Start defined process.

After process gets started, timeout countdown begins as well.

Note

We want to open stdin, stdout and stderr as text streams in universal newlines mode, so we have to set universal_newlines to True.

stop()

Stop process running.

Wait 10 seconds for the process to end, then just kill it.

Note

When gathering coverage for the subprocess in tests, you have to allow subprocesses to end gracefully.

stopped(*args, **kwds)

Stopping process for given context and starts it afterwards.

Allows for easier writing resistance integration tests whenever one of the service fails.

wait_for(wait_for)

Wait for callback to return True.

Simply returns if wait_for condition has been met, raises TimeoutExpired otherwise and kills the process.

Parameters:wait_for (callback) – callback to call
Raises:mirakuru.exceptions.TimeoutExpired
class mirakuru.base.StartCheckExecutor(command, shell=False, timeout=None, sleep=0.1)[source]

Bases: mirakuru.base.Executor

Base class for executors with a pre- and after-start checks.

Initialize executor.

Parameters:
  • command (str) – command to run to start service
  • shell (bool) – see subprocess.Popen
  • timeout (int) – time to wait for process to start or stop. if None, wait indefinitely.
  • sleep (float) – how often to check for start/stop condition

Note

timeout set for executor is valid for all the level of waits on the way up. That means that if some more advanced executor sets timout to 10 seconds, and it’ll take 5 seconds for first check, second check will only have 5 seconds left.

after_start_check()

Method fired after the start of executor.

Should be overridden in order to return boolean value if executor can be treated as started. :rtype: bool

pre_start_check()

Method fired before the start of executor.

Should be overridden in order to return boolean value if some process is already started. :rtype: bool

start()

Start executor with additional checks.

Checks if previous executor isn’t running then start process (executor) and wait until it’s started.

This executor awaits for appearance of a predefined banner in output.

class mirakuru.output.OutputExecutor(command, banner, shell=False, timeout=None, sleep=0.1)[source]

Bases: mirakuru.base.Executor

Executor that awaits for string output being present in output.

Initialize OutputExecutor executor.

Parameters:
  • command (str) – command to run to start service
  • banner (str) – string that has to appear in process output - should compile to regular expression.
  • shell (bool) – see subprocess.Popen
  • timeout (int) – time to wait for process to start or stop. if None, wait indefinitely.
  • sleep (float) – how often to check for start/stop condition
_wait_for_output()

Check if output matches banner.

Warning

Waiting for I/O completion. It does not work on Windows. Sorry.

start()

Start process.

Note

Process will be considered started, when defined banner will appear in process output.

TCP executor definition.

class mirakuru.tcp.TCPExecutor(command, host, port, shell=False, timeout=None, sleep=0.1)[source]

Bases: mirakuru.base.StartCheckExecutor

TCP-listening process executor.

Used to start (and wait to actually be running) processes that can accept TCP connections.

Initialize TCPExecutor executor.

Parameters:
  • command (str) – command to run to start service
  • host (str) – host under which process is accessible
  • port (int) – port under which process is accessible
  • shell (bool) – see subprocess.Popen
  • timeout (int) – time to wait for process to start or stop. if None, wait indefinitely.
  • sleep (float) – how often to check for start/stop condition
after_start_check()

Check if process accepts connections.

Note

Process will be considered started, when it’ll be able to accept TCP connections as defined in initializer.

host = None

Host name, process is listening on.

port = None

Port number, process is listening on.

pre_start_check()

Check if process accepts connections.

Note

Process will be considered started, when it’ll be able to accept TCP connections as defined in initializer.

HTTP enabled process executor.

class mirakuru.http.HTTPExecutor(command, url, shell=False, timeout=None, sleep=0.1)[source]

Bases: mirakuru.tcp.TCPExecutor

Http enabled process executor.

Initialize HTTPExecutor executor.

Parameters:
  • command (str) – command to run to start service
  • url (str) – url where executor can check if process has already started.
  • shell (bool) – see subprocess.Popen
  • timeout (int) – time to wait for process to start or stop. if None, wait indefinitely.
  • sleep (float) – how often to check for start/stop condition
after_start_check()

Check if defined url returns successful head.

url = None

An urlparse.urlparse() representation of an url.

It’ll be used to check process status on.

Pid executor definition.

class mirakuru.pid.PidExecutor(command, filename, shell=False, timeout=None, sleep=0.1)[source]

Bases: mirakuru.base.StartCheckExecutor

File existence checking process executor.

Used to start processes that create pid files (or any other for that matter). Starts the given process and waits for the given file to be created.

Initialize the PidExecutor executor.

If the filename is empty, a ValueError is thrown.

Parameters:
  • command (str) – the command to run which is to create a file
  • filename (str) – the file which is to exist
  • shell (bool) – see subprocess.Popen
  • timeout (int) – time to wait for the process to start or stop. if None, wait indefinitely.
  • sleep (float) – how often to check for start/stop conditions
Raises:

ValueError

after_start_check()

Check if the process has created the specified file.

Note

The process will be considered started when it will have created the specified file as defined in the initializer.

filename = None

the name of the file which the process is to create.

pre_start_check()

Check if the specified file has been created.

Note

The process will be considered started when it will have created the specified file as defined in the initializer.