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