summaryrefslogtreecommitdiffstats
path: root/meta/lib/oeqa/core/decorator/__init__.py
blob: 14d7bfcd3567d789c53798d8dd60ba72d9d35f1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Copyright (C) 2016 Intel Corporation
# Released under the MIT license (see COPYING.MIT)

from functools import wraps
from abc import abstractmethod, ABCMeta

decoratorClasses = set()

def registerDecorator(cls):
    decoratorClasses.add(cls)
    return cls

class OETestDecorator(object, metaclass=ABCMeta):
    case = None # Reference of OETestCase decorated
    attrs = None # Attributes to be loaded by decorator implementation

    def __init__(self, *args, **kwargs):
        if not self.attrs:
            return

        for idx, attr in enumerate(self.attrs):
            if attr in kwargs:
                value = kwargs[attr]
            else:
                value = args[idx]
            setattr(self, attr, value)

    def __call__(self, func):
        @wraps(func)
        def wrapped_f(*args, **kwargs):
            self.attrs = self.attrs # XXX: Enables OETestLoader discover
            return func(*args, **kwargs)
        return wrapped_f

    # OETestLoader call it when is loading test cases.
    # XXX: Most methods would change the registry for later
    # processing; be aware that filtrate method needs to
    # run later than bind, so there could be data (in the
    # registry) of a cases that were filtered.
    def bind(self, registry, case):
        self.case = case
        self.logger = case.tc.logger
        self.case.decorators.append(self)

    # OETestRunner call this method when tries to run
    # the test case.
    def setUpDecorator(self):
        pass

    # OETestRunner call it after a test method has been
    # called even if the method raised an exception.
    def tearDownDecorator(self):
        pass

class OETestDiscover(OETestDecorator):

    # OETestLoader call it after discover test cases
    # needs to return the cases to be run.
    @staticmethod
    def discover(registry):
        return registry['cases']

class OETestFilter(OETestDecorator):

    # OETestLoader call it while loading the tests
    # in loadTestsFromTestCase method, it needs to
    # return a bool, True if needs to be filtered.
    # This method must consume the filter used.
    @abstractmethod
    def filtrate(self, filters):
        return False