robottelo.decorators.func_locker

Implements test function locking, using pytest_services file locking

Usage:

from robottelo.decorators.func_locker import (
    locking_function,
    lock_function,
 )

# in many cases we have tests that need some test functions to run isolated
# from other py.test workers when used in --boxed mode
class SomeTestCase(TestCase):

    @classmethod
    @lock_function
    def setUpClass(cls):
        pass

# in other cases we want only a portion of the test function to be isolated
class SomeTestCase(TestCase):

    @classmethod
    def setUpClass(cls):

        with locking_function(cls.setUpClass,
                              scope_context='publish_puppet_class'):
            # call the publish function


# some tests can be in conflicts with other tests parts
class SomeTestCase(TestCase):

   @lock_function
   def test_to_lock(self):
      pass

   def test_that_conflict_with_test_to_lock(self)
        with locking_function(self.test_to_lock):
            # do some operations that conflict with test_to_lock

Module Contents

Functions

set_default_scope(value)

Set the default namespace scope

_get_default_scope()

get_temp_dir()

_get_temp_lock_function_dir(create=True)

_get_scope_path(scope, scope_kwargs=None, scope_context=None, create=True)

Returns the scopes path and create it if create is true

_get_function_name(function, class_name=None)

Return a string representation of the function as

_get_function_name_lock_path(function_name, scope=None, scope_kwargs=None, scope_context=None)

Return the path of the file to lock

_check_deadlock(lock_file_path, process_id)

To prevent process deadlock, raise exception if the file content is the

_write_content(handler, content)

write content to locked file

lock_function(function=None, scope=_get_default_scope, scope_context=None, scope_kwargs=None, timeout=LOCK_DEFAULT_TIMEOUT)

Generic function locker, lock any decorated function. Any parallel

locking_function(function, scope=_get_default_scope, scope_context=None, scope_kwargs=None, timeout=LOCK_DEFAULT_TIMEOUT)

Lock a function in combination with a scope and scope_context.

Attributes

logger

TEMP_ROOT_DIR

TEMP_FUNC_LOCK_DIR

LOCK_DIR

LOCK_DEFAULT_TIMEOUT

LOCK_FILE_NAME_EXT

LOCK_DEFAULT_SCOPE

_DEFAULT_CLASS_NAME_DEPTH

robottelo.decorators.func_locker.logger
robottelo.decorators.func_locker.TEMP_ROOT_DIR = robottelo
robottelo.decorators.func_locker.TEMP_FUNC_LOCK_DIR = lock_functions
robottelo.decorators.func_locker.LOCK_DIR
robottelo.decorators.func_locker.LOCK_DEFAULT_TIMEOUT = 1800
robottelo.decorators.func_locker.LOCK_FILE_NAME_EXT = lock
robottelo.decorators.func_locker.LOCK_DEFAULT_SCOPE
robottelo.decorators.func_locker._DEFAULT_CLASS_NAME_DEPTH = 3
exception robottelo.decorators.func_locker.FunctionLockerError

Bases: Exception

the default function locker error

robottelo.decorators.func_locker.set_default_scope(value)

Set the default namespace scope

robottelo.decorators.func_locker._get_default_scope()
robottelo.decorators.func_locker.get_temp_dir()
robottelo.decorators.func_locker._get_temp_lock_function_dir(create=True)
robottelo.decorators.func_locker._get_scope_path(scope, scope_kwargs=None, scope_context=None, create=True)

Returns the scopes path and create it if create is true

robottelo.decorators.func_locker._get_function_name(function, class_name=None)

Return a string representation of the function as module_path.Class_name.function_name

note: the class name is the first parent class

robottelo.decorators.func_locker._get_function_name_lock_path(function_name, scope=None, scope_kwargs=None, scope_context=None)

Return the path of the file to lock

robottelo.decorators.func_locker._check_deadlock(lock_file_path, process_id)

To prevent process deadlock, raise exception if the file content is the same as process_id

note: this function is called before the lock

robottelo.decorators.func_locker._write_content(handler, content)

write content to locked file

robottelo.decorators.func_locker.lock_function(function=None, scope=_get_default_scope, scope_context=None, scope_kwargs=None, timeout=LOCK_DEFAULT_TIMEOUT)
Generic function locker, lock any decorated function. Any parallel

pytest xdist worker will wait for this function to finish

Parameters
  • function (callable) – the function that is intended to be locked

  • scope (str or callable) – this parameter will define the namespace of locking

  • scope_context (str) – an added context string if applicable, of a concrete lock in combination with scope and function.

  • scope_kwargs (dict) – kwargs to be passed to scope if is a callable

  • timeout (int) – the time in seconds to wait for acquiring the lock

robottelo.decorators.func_locker.locking_function(function, scope=_get_default_scope, scope_context=None, scope_kwargs=None, timeout=LOCK_DEFAULT_TIMEOUT)

Lock a function in combination with a scope and scope_context. Any parallel pytest xdist worker will wait for this function to finish.

Parameters
  • function (callable) – the function that is intended to be locked

  • scope (str or callable) – this parameter will define the namespace of locking

  • scope_context (str) – an added context string if applicable, of a concrete lock in combination with scope and function.

  • scope_kwargs (dict) – kwargs to be passed to scope if is a callable

  • timeout (int) – the time in seconds to wait for acquiring the lock