1#
2# Copyright (C) 2016 Intel Corporation
3#
4# SPDX-License-Identifier: MIT
5#
6
7from oeqa.core.exception import OEQAMissingVariable
8
9from . import OETestDecorator, registerDecorator
10
11def has_feature(td, feature):
12    """
13        Checks for feature in DISTRO_FEATURES or IMAGE_FEATURES.
14    """
15
16    if (feature in td.get('DISTRO_FEATURES', '') or
17        feature in td.get('IMAGE_FEATURES', '')):
18        return True
19    return False
20
21def has_machine(td, machine):
22    """
23        Checks for MACHINE.
24    """
25
26    if (machine in td.get('MACHINE', '')):
27        return True
28    return False
29
30def is_qemu(td, qemu):
31    """
32        Checks if MACHINE is qemu.
33    """
34
35    machine = td.get('MACHINE', '')
36    if (qemu in td.get('MACHINE', '') or
37    machine.startswith('qemu')):
38        return True
39    return False
40
41@registerDecorator
42class skipIfDataVar(OETestDecorator):
43    """
44        Skip test based on value of a data store's variable.
45
46        It will get the info of var from the data store and will
47        check it against value; if are equal it will skip the test
48        with msg as the reason.
49    """
50
51    attrs = ('var', 'value', 'msg')
52
53    def setUpDecorator(self):
54        msg = ('Checking if %r value is %r to skip test' %
55               (self.var, self.value))
56        self.logger.debug(msg)
57        if self.case.td.get(self.var) == self.value:
58            self.case.skipTest(self.msg)
59
60@registerDecorator
61class skipIfNotDataVar(OETestDecorator):
62    """
63        Skip test based on value of a data store's variable.
64
65        It will get the info of var from the data store and will
66        check it against value; if are not equal it will skip the
67        test with msg as the reason.
68    """
69
70    attrs = ('var', 'value', 'msg')
71
72    def setUpDecorator(self):
73        msg = ('Checking if %r value is not %r to skip test' %
74               (self.var, self.value))
75        self.logger.debug(msg)
76        if not self.case.td.get(self.var) == self.value:
77            self.case.skipTest(self.msg)
78
79@registerDecorator
80class skipIfInDataVar(OETestDecorator):
81    """
82        Skip test if value is in data store's variable.
83    """
84
85    attrs = ('var', 'value', 'msg')
86    def setUpDecorator(self):
87        msg = ('Checking if %r value contains %r to skip '
88              'the test' % (self.var, self.value))
89        self.logger.debug(msg)
90        if self.value in (self.case.td.get(self.var)):
91            self.case.skipTest(self.msg)
92
93@registerDecorator
94class skipIfNotInDataVar(OETestDecorator):
95    """
96        Skip test if value is not in data store's variable.
97    """
98
99    attrs = ('var', 'value', 'msg')
100    def setUpDecorator(self):
101        msg = ('Checking if %r value contains %r to run '
102              'the test' % (self.var, self.value))
103        self.logger.debug(msg)
104        if not self.value in (self.case.td.get(self.var) or ""):
105            self.case.skipTest(self.msg)
106
107@registerDecorator
108class OETestDataDepends(OETestDecorator):
109    attrs = ('td_depends',)
110
111    def setUpDecorator(self):
112        for v in self.td_depends:
113            try:
114                value = self.case.td[v]
115            except KeyError:
116                raise OEQAMissingVariable("Test case need %s variable but"\
117                        " isn't into td" % v)
118
119@registerDecorator
120class skipIfNotFeature(OETestDecorator):
121    """
122        Skip test based on DISTRO_FEATURES.
123
124        value must be in distro features or it will skip the test
125        with msg as the reason.
126    """
127
128    attrs = ('value', 'msg')
129
130    def setUpDecorator(self):
131        msg = ('Checking if %s is in DISTRO_FEATURES '
132               'or IMAGE_FEATURES' % (self.value))
133        self.logger.debug(msg)
134        if not has_feature(self.case.td, self.value):
135            self.case.skipTest(self.msg)
136
137@registerDecorator
138class skipIfFeature(OETestDecorator):
139    """
140        Skip test based on DISTRO_FEATURES.
141
142        value must not be in distro features or it will skip the test
143        with msg as the reason.
144    """
145
146    attrs = ('value', 'msg')
147
148    def setUpDecorator(self):
149        msg = ('Checking if %s is not in DISTRO_FEATURES '
150               'or IMAGE_FEATURES' % (self.value))
151        self.logger.debug(msg)
152        if has_feature(self.case.td, self.value):
153            self.case.skipTest(self.msg)
154
155@registerDecorator
156class skipIfNotMachine(OETestDecorator):
157    """
158        Skip test based on MACHINE.
159
160        value must be match MACHINE or it will skip the test
161        with msg as the reason.
162    """
163
164    attrs = ('value', 'msg')
165
166    def setUpDecorator(self):
167        msg = ('Checking if %s is not this MACHINE' % self.value)
168        self.logger.debug(msg)
169        if not has_machine(self.case.td, self.value):
170            self.case.skipTest(self.msg)
171
172@registerDecorator
173class skipIfMachine(OETestDecorator):
174    """
175        Skip test based on Machine.
176
177        value must not be this machine or it will skip the test
178        with msg as the reason.
179    """
180
181    attrs = ('value', 'msg')
182
183    def setUpDecorator(self):
184        msg = ('Checking if %s is this MACHINE' % self.value)
185        self.logger.debug(msg)
186        if has_machine(self.case.td, self.value):
187            self.case.skipTest(self.msg)
188
189@registerDecorator
190class skipIfNotQemu(OETestDecorator):
191    """
192        Skip test based on MACHINE.
193
194        value must be a qemu MACHINE or it will skip the test
195        with msg as the reason.
196    """
197
198    attrs = ('value', 'msg')
199
200    def setUpDecorator(self):
201        msg = ('Checking if %s is not this MACHINE' % self.value)
202        self.logger.debug(msg)
203        if not is_qemu(self.case.td, self.value):
204            self.case.skipTest(self.msg)
205
206@registerDecorator
207class skipIfQemu(OETestDecorator):
208    """
209        Skip test based on Qemu Machine.
210
211        value must not be a qemu machine or it will skip the test
212        with msg as the reason.
213   """
214
215    attrs = ('value', 'msg')
216
217    def setUpDecorator(self):
218        msg = ('Checking if %s is this MACHINE' % self.value)
219        self.logger.debug(msg)
220        if is_qemu(self.case.td, self.value):
221             self.case.skipTest(self.msg)
222
223