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 |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 8 | |
| 9 | @registerDecorator |
| 10 | class 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 Williams | 4585273 | 2022-04-02 08:58:32 -0500 | [diff] [blame] | 36 | |
| 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 42 | 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 Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 51 | msg = "Test can't run with %s installed" % ', or '.join(unneed_pkgs) |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 52 | self._decorator_fail(msg) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 53 | |
| 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 Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 58 | msg = "Test requires %s to be installed" % ', or '.join(need_pkgs) |
Andrew Geissler | d25ed32 | 2020-06-27 00:28:28 -0500 | [diff] [blame] | 59 | self._decorator_fail(msg) |
| 60 | |
| 61 | def _decorator_fail(self, msg): |
| 62 | self.case.skipTest(msg) |
| 63 | |
| 64 | @registerDecorator |
| 65 | class 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) |