Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 2 | # Copyright (C) 2016 Intel Corporation |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 6 | |
| 7 | from oeqa.core.decorator import OETestDecorator, registerDecorator |
| 8 | from oeqa.core.utils.misc import strToSet |
| 9 | |
| 10 | @registerDecorator |
| 11 | class OEHasPackage(OETestDecorator): |
| 12 | """ |
| 13 | Checks if image has packages (un)installed. |
| 14 | |
| 15 | The argument must be a string, set, or list of packages that must be |
| 16 | installed or not present in the image. |
| 17 | |
| 18 | The way to tell a package must not be in an image is using an |
| 19 | exclamation point ('!') before the name of the package. |
| 20 | |
| 21 | If test depends on pkg1 or pkg2 you need to use: |
| 22 | @OEHasPackage({'pkg1', 'pkg2'}) |
| 23 | |
| 24 | If test depends on pkg1 and pkg2 you need to use: |
| 25 | @OEHasPackage('pkg1') |
| 26 | @OEHasPackage('pkg2') |
| 27 | |
| 28 | If test depends on pkg1 but pkg2 must not be present use: |
| 29 | @OEHasPackage({'pkg1', '!pkg2'}) |
| 30 | """ |
| 31 | |
| 32 | attrs = ('need_pkgs',) |
| 33 | |
| 34 | def setUpDecorator(self): |
| 35 | need_pkgs = set() |
| 36 | unneed_pkgs = set() |
| 37 | pkgs = strToSet(self.need_pkgs) |
| 38 | for pkg in pkgs: |
| 39 | if pkg.startswith('!'): |
| 40 | unneed_pkgs.add(pkg[1:]) |
| 41 | else: |
| 42 | need_pkgs.add(pkg) |
| 43 | |
| 44 | if unneed_pkgs: |
| 45 | msg = 'Checking if %s is not installed' % ', '.join(unneed_pkgs) |
| 46 | self.logger.debug(msg) |
| 47 | if not self.case.tc.image_packages.isdisjoint(unneed_pkgs): |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 48 | msg = "Test can't run with %s installed" % ', or '.join(unneed_pkgs) |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 49 | self._decorator_fail(msg) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 50 | |
| 51 | if need_pkgs: |
| 52 | msg = 'Checking if at least one of %s is installed' % ', '.join(need_pkgs) |
| 53 | self.logger.debug(msg) |
| 54 | if self.case.tc.image_packages.isdisjoint(need_pkgs): |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame^] | 55 | msg = "Test requires %s to be installed" % ', or '.join(need_pkgs) |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 56 | self._decorator_fail(msg) |
| 57 | |
| 58 | def _decorator_fail(self, msg): |
| 59 | self.case.skipTest(msg) |
| 60 | |
| 61 | @registerDecorator |
| 62 | class OERequirePackage(OEHasPackage): |
| 63 | """ |
| 64 | Checks if image has packages (un)installed. |
| 65 | It is almost the same as OEHasPackage, but if dependencies are missing |
| 66 | the test case fails. |
| 67 | |
| 68 | The argument must be a string, set, or list of packages that must be |
| 69 | installed or not present in the image. |
| 70 | |
| 71 | The way to tell a package must not be in an image is using an |
| 72 | exclamation point ('!') before the name of the package. |
| 73 | |
| 74 | If test depends on pkg1 or pkg2 you need to use: |
| 75 | @OERequirePackage({'pkg1', 'pkg2'}) |
| 76 | |
| 77 | If test depends on pkg1 and pkg2 you need to use: |
| 78 | @OERequirePackage('pkg1') |
| 79 | @OERequirePackage('pkg2') |
| 80 | |
| 81 | If test depends on pkg1 but pkg2 must not be present use: |
| 82 | @OERequirePackage({'pkg1', '!pkg2'}) |
| 83 | """ |
| 84 | |
| 85 | def _decorator_fail(self, msg): |
| 86 | self.case.fail(msg) |