1# 2# Copyright (C) 2016 Intel Corporation 3# 4# SPDX-License-Identifier: MIT 5# 6 7from oeqa.core.decorator import OETestDecorator, registerDecorator 8 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() 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 mlprefix = self.case.td.get("MLPREFIX") 42 for pkg in self.need_pkgs: 43 if pkg.startswith('!'): 44 unneed_pkgs.add(mlprefix + pkg[1:]) 45 else: 46 need_pkgs.add(mlprefix + pkg) 47 48 if unneed_pkgs: 49 msg = 'Checking if %s is not installed' % ', '.join(unneed_pkgs) 50 self.logger.debug(msg) 51 if not self.case.tc.image_packages.isdisjoint(unneed_pkgs): 52 msg = "Test can't run with %s installed" % ', or '.join(unneed_pkgs) 53 self._decorator_fail(msg) 54 55 if need_pkgs: 56 msg = 'Checking if at least one of %s is installed' % ', '.join(need_pkgs) 57 self.logger.debug(msg) 58 if self.case.tc.image_packages.isdisjoint(need_pkgs): 59 msg = "Test requires %s to be installed" % ', or '.join(need_pkgs) 60 self._decorator_fail(msg) 61 62 def _decorator_fail(self, msg): 63 self.case.skipTest(msg) 64 65@registerDecorator 66class OERequirePackage(OEHasPackage): 67 """ 68 Checks if image has packages (un)installed. 69 It is almost the same as OEHasPackage, but if dependencies are missing 70 the test case fails. 71 72 The argument must be a string, set, or list of packages that must be 73 installed or not present in the image. 74 75 The way to tell a package must not be in an image is using an 76 exclamation point ('!') before the name of the package. 77 78 If test depends on pkg1 or pkg2 you need to use: 79 @OERequirePackage({'pkg1', 'pkg2'}) 80 81 If test depends on pkg1 and pkg2 you need to use: 82 @OERequirePackage('pkg1') 83 @OERequirePackage('pkg2') 84 85 If test depends on pkg1 but pkg2 must not be present use: 86 @OERequirePackage({'pkg1', '!pkg2'}) 87 """ 88 89 def _decorator_fail(self, msg): 90 self.case.fail(msg) 91