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        for pkg in self.need_pkgs:
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):
51                msg = "Test can't run with %s installed" % ', or '.join(unneed_pkgs)
52                self._decorator_fail(msg)
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):
58                msg = "Test requires %s to be installed" % ', or '.join(need_pkgs)
59                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)
90