blob: 8aba3f325bcfe18017355bb91be7a91ae8ea0441 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Brad Bishop6e60e8b2018-02-01 10:27:11 -05002# Copyright (C) 2016 Intel Corporation
Brad Bishopc342db32019-05-15 21:57:59 -04003#
4# SPDX-License-Identifier: MIT
5#
Brad Bishop6e60e8b2018-02-01 10:27:11 -05006
7from oeqa.core.decorator import OETestDecorator, registerDecorator
Brad Bishop6e60e8b2018-02-01 10:27:11 -05008
9@registerDecorator
10class OEHasPackage(OETestDecorator):
11 """
12 Checks if image has packages (un)installed.
13
14 The argument must be a string, set, or list of packages that must be
15 installed or not present in the image.
16
17 The way to tell a package must not be in an image is using an
18 exclamation point ('!') before the name of the package.
19
20 If test depends on pkg1 or pkg2 you need to use:
21 @OEHasPackage({'pkg1', 'pkg2'})
22
23 If test depends on pkg1 and pkg2 you need to use:
24 @OEHasPackage('pkg1')
25 @OEHasPackage('pkg2')
26
27 If test depends on pkg1 but pkg2 must not be present use:
28 @OEHasPackage({'pkg1', '!pkg2'})
29 """
30
31 attrs = ('need_pkgs',)
32
33 def setUpDecorator(self):
34 need_pkgs = set()
35 unneed_pkgs = set()
Patrick Williams45852732022-04-02 08:58:32 -050036
37 # Turn literal strings into a list so we can just iterate over it
38 if isinstance(self.need_pkgs, str):
39 self.need_pkgs = [self.need_pkgs,]
40
41 for pkg in self.need_pkgs:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050042 if pkg.startswith('!'):
43 unneed_pkgs.add(pkg[1:])
44 else:
45 need_pkgs.add(pkg)
46
47 if unneed_pkgs:
48 msg = 'Checking if %s is not installed' % ', '.join(unneed_pkgs)
49 self.logger.debug(msg)
50 if not self.case.tc.image_packages.isdisjoint(unneed_pkgs):
Andrew Geisslerc926e172021-05-07 16:11:35 -050051 msg = "Test can't run with %s installed" % ', or '.join(unneed_pkgs)
Andrew Geisslerd25ed322020-06-27 00:28:28 -050052 self._decorator_fail(msg)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050053
54 if need_pkgs:
55 msg = 'Checking if at least one of %s is installed' % ', '.join(need_pkgs)
56 self.logger.debug(msg)
57 if self.case.tc.image_packages.isdisjoint(need_pkgs):
Andrew Geisslerc926e172021-05-07 16:11:35 -050058 msg = "Test requires %s to be installed" % ', or '.join(need_pkgs)
Andrew Geisslerd25ed322020-06-27 00:28:28 -050059 self._decorator_fail(msg)
60
61 def _decorator_fail(self, msg):
62 self.case.skipTest(msg)
63
64@registerDecorator
65class OERequirePackage(OEHasPackage):
66 """
67 Checks if image has packages (un)installed.
68 It is almost the same as OEHasPackage, but if dependencies are missing
69 the test case fails.
70
71 The argument must be a string, set, or list of packages that must be
72 installed or not present in the image.
73
74 The way to tell a package must not be in an image is using an
75 exclamation point ('!') before the name of the package.
76
77 If test depends on pkg1 or pkg2 you need to use:
78 @OERequirePackage({'pkg1', 'pkg2'})
79
80 If test depends on pkg1 and pkg2 you need to use:
81 @OERequirePackage('pkg1')
82 @OERequirePackage('pkg2')
83
84 If test depends on pkg1 but pkg2 must not be present use:
85 @OERequirePackage({'pkg1', '!pkg2'})
86 """
87
88 def _decorator_fail(self, msg):
89 self.case.fail(msg)