robottelo.decorators.func_shared.shared

Shared function is a decorator, that enable a function once called to store the results to storage, any ulterior call from the same or other processes will return the stored results, which make the shared function results persistent.

Note: Shared function store it’s data as json. The results of the decorated

function must be json compatible.

Usage:

from robottelo.decorators.func_shared.shared import shared

@shared
def module_level_shared(*args, **kwargs):
    # do some
    # the result of a shared function must be json compatible
    return any_data

class SomeTestCase1(TestCase):

    @shared
    def _shared_function(cls):

        org = make_org()
        # upload manifest
        repo = make_repository()
        return dict(org=org, repo=repo}

    @classmethod
    @shared
    def setUpClass(cls):

        data = cls._shared_function()
        other_data = module_level_shared()

        cls.org = data['org']
        cls.repo = data['repo']
        return

# the shared function can be called an other time to be able to initiate
# specific data

class SomeTestCase2(TestCase):

    @classmethod
    @shared(inject=True, injected_kw='_injected')
    def setUpClass(cls, org=None, repo=None, _injected=False):

        if _injected:
            cls.org = org
            cls.repo = repo
        else:
            # create the org
            cls.org = make_org()
            # upload manifest
            cls.repo = make_repository()

        # create a virtual machine

        # shared function with injected=True, must return a dict
        # the resulting dictionary will be injected in other calls as
        # kwargs, an added bool kw argument by default named _injected
        # should be added to the function kwargs, to be able to be notified
        # that the kwargs are injected from already stored result
        return dict(org=cls.org, repo=cls.repo}

    # in case we do not want the injected key word in kwargs
    # simply , declare injected_kw=None
    @classmethod
    @shared(inject=True, injected_kw=None)
    def shared_class_method(cls, org=None, repo=None):
         if org is not None:
            cls.org = org
         else:
            # create the org
            cls.org = make_org()
            # upload manifest
         if repo_id is not None:
            cls.repo = repo
         else:
            cls.repo = make_repository()

        # create a virtual machine

        return dict(org=cls.org, repo=cls.repo}

Module Contents

Classes

_SharedFunction

Internal class helper that is created each time the shared function is

Functions

_set_configured(value)

_check_config()

enable_shared_function(value)

force and override settings, by setting the global use shared data

set_default_scope(value)

Set the default namespace scope

_get_default_scope()

Return the shared function default scope

_get_default_storage_handler()

Return the storage handler instance

_get_kwargs_md5(**kwargs)

Create an md5 hexdigest from kwargs

_get_scope_name(scope=None, scope_kwargs=None, scope_context=None)

_get_function_name(function, class_name=None, kwargs=None)

Return a string representation of the function as

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

shared(function_=None, scope=_get_default_scope, scope_context=None, scope_kwargs=None, timeout=SHARE_DEFAULT_TIMEOUT, retries=DEFAULT_CALL_RETRIES, function_kw=None, inject=False, injected_kw='_injected')

Generic function sharing, share the results of any decorated function.

Attributes

logger

_storage_handlers

DEFAULT_STORAGE_HANDLER

ENABLED

NAMESPACE_SCOPE

SHARE_DEFAULT_TIMEOUT

DEFAULT_CALL_RETRIES

_configured

_NAMESPACE_SCOPE_KEY_TYPE

_DEFAULT_CLASS_NAME_DEPTH

_STATE_READY

_STATE_FAILED

_DATETIME_FORMAT

_SERVER_CERT_MD5

robottelo.decorators.func_shared.shared.logger
robottelo.decorators.func_shared.shared._storage_handlers
robottelo.decorators.func_shared.shared.DEFAULT_STORAGE_HANDLER = file
robottelo.decorators.func_shared.shared.ENABLED = False
robottelo.decorators.func_shared.shared.NAMESPACE_SCOPE
robottelo.decorators.func_shared.shared.SHARE_DEFAULT_TIMEOUT = 86400
robottelo.decorators.func_shared.shared.DEFAULT_CALL_RETRIES = 2
robottelo.decorators.func_shared.shared._configured = False
robottelo.decorators.func_shared.shared._NAMESPACE_SCOPE_KEY_TYPE = shared_function
robottelo.decorators.func_shared.shared._DEFAULT_CLASS_NAME_DEPTH = 3
robottelo.decorators.func_shared.shared._STATE_READY = READY
robottelo.decorators.func_shared.shared._STATE_FAILED = FAILED
robottelo.decorators.func_shared.shared._DATETIME_FORMAT = %Y-%m-%dT%H:%M:%S
robottelo.decorators.func_shared.shared._SERVER_CERT_MD5
robottelo.decorators.func_shared.shared._set_configured(value)
robottelo.decorators.func_shared.shared._check_config()
robottelo.decorators.func_shared.shared.enable_shared_function(value)

force and override settings, by setting the global use shared data attribute

robottelo.decorators.func_shared.shared.set_default_scope(value)

Set the default namespace scope :type value: str or callable

robottelo.decorators.func_shared.shared._get_default_scope()

Return the shared function default scope

robottelo.decorators.func_shared.shared._get_default_storage_handler()

Return the storage handler instance

exception robottelo.decorators.func_shared.shared.SharedFunctionError

Bases: Exception

Shared function related exception

exception robottelo.decorators.func_shared.shared.SharedFunctionException

Bases: Exception

Shared function call exception when not able to restore the original exception

class robottelo.decorators.func_shared.shared._SharedFunction(function_key, function, args=None, kwargs=None, retries=DEFAULT_CALL_RETRIES, storage_handler=None, timeout=SHARE_DEFAULT_TIMEOUT, inject=False, injected_kw='_inject')

Internal class helper that is created each time the shared function is launched and group all the necessary functionality

property storage(self)
property key(self)
property transaction(self)
_encode_result_kwargs(self, kwargs)

look for some special kwargs and convert them

_call_function(self)
_has_result_expired(self, creation_datetime)
__call__(self)
robottelo.decorators.func_shared.shared._get_kwargs_md5(**kwargs)

Create an md5 hexdigest from kwargs

robottelo.decorators.func_shared.shared._get_scope_name(scope=None, scope_kwargs=None, scope_context=None)
robottelo.decorators.func_shared.shared._get_function_name(function, class_name=None, kwargs=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_shared.shared._get_function_name_key(function_name, scope=None, scope_kwargs=None, scope_context=None)
robottelo.decorators.func_shared.shared.shared(function_=None, scope=_get_default_scope, scope_context=None, scope_kwargs=None, timeout=SHARE_DEFAULT_TIMEOUT, retries=DEFAULT_CALL_RETRIES, function_kw=None, inject=False, injected_kw='_injected')

Generic function sharing, share the results of 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 shared

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

  • scope_context (str) – an added context string if applicable, of a concrete sharing 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 waiting the shared function

  • retries (int) – if the shared function call fail, how much time should retry before setting the call with in failure state

  • function_kw (list) – The function kwargs to use as an additional scope, an md5 hexdigest of that kwargs will be created and added to the storage scope, that way we should have diffrent stored values for diffrent kw values.

  • inject (bool) – whether to recall the function with injecting the result as **kwargs

  • injected_kw (str) – the kw arg to set to True to inform the function that the kwargs was injected from a saved storage