1#
2# Copyright (C) 2016 Intel Corporation
3#
4# SPDX-License-Identifier: MIT
5#
6
7from oeqa.core.decorator import OETestDecorator, registerDecorator
8from oeqa.core.utils.misc import strToSet
9
10@registerDecorator
11class 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):
48                msg = "Test can't run with %s installed" % ', or '.join(unneed_pkgs)
49                self._decorator_fail(msg)
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):
55                msg = "Test requires %s to be installed" % ', or '.join(need_pkgs)
56                self._decorator_fail(msg)
57
58    def _decorator_fail(self, msg):
59        self.case.skipTest(msg)
60
61@registerDecorator
62class 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)
87