aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unittests/policy_tests.py
blob: 02d5ae30d3a3f536cbe59e779c70d1f5eec5fcbc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# This file is part of the sos project: https://github.com/sosreport/sos
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# version 2 of the GNU General Public License.
#
# See the LICENSE file in the source distribution for further information.
import unittest

from avocado.utils import distro

from sos.policies import Policy, import_policy
from sos.policies.distros import LinuxPolicy
from sos.policies.package_managers import PackageManager, MultiPackageManager
from sos.policies.package_managers.rpm import RpmPackageManager
from sos.policies.package_managers.dpkg import DpkgPackageManager
from sos.report.plugins import (Plugin, IndependentPlugin,
                                RedHatPlugin, DebianPlugin)


class FauxPolicy(Policy):
    distro = "Faux"


class FauxLinuxPolicy(LinuxPolicy):
    distro = "FauxLinux"

    @classmethod
    def set_forbidden_paths(cls):
        return ['/etc/secret']


class FauxPlugin(Plugin, IndependentPlugin):
    pass


class FauxRedHatPlugin(Plugin, RedHatPlugin):
    pass


class FauxDebianPlugin(Plugin, DebianPlugin):
    pass


class PolicyTests(unittest.TestCase):


    def test_independent_only(self):
        p = FauxPolicy()
        p.valid_subclasses = []

        self.assertTrue(p.validate_plugin(FauxPlugin))

    def test_forbidden_paths_building(self):
        p = FauxLinuxPolicy(probe_runtime=False)
        self.assertTrue('*.pyc' in p.forbidden_paths)
        self.assertTrue('/etc/passwd' in p.forbidden_paths)
        self.assertTrue('/etc/secret' in p.forbidden_paths)

    def test_redhat(self):
        p = FauxPolicy()
        p.valid_subclasses = [RedHatPlugin]

        self.assertTrue(p.validate_plugin(FauxRedHatPlugin))

    def test_debian(self):
        p = FauxPolicy()
        p.valid_subclasses = [DebianPlugin]

        self.assertTrue(p.validate_plugin(FauxDebianPlugin))

    def test_fails(self):
        p = FauxPolicy()
        p.valid_subclasses = []

        self.assertFalse(p.validate_plugin(FauxDebianPlugin))

    def test_can_import(self):
        self.assertTrue(import_policy('redhat') is not None)

    def test_cant_import(self):
        self.assertTrue(import_policy('notreal') is None)


class PackageManagerTests(unittest.TestCase):

    def setUp(self):
        self.pm = PackageManager()

    def test_default_all_pkgs(self):
        self.assertEqual(self.pm.packages, {})

    def test_default_all_pkgs_by_name(self):
        self.assertEqual(self.pm.all_pkgs_by_name('doesntmatter'), [])

    def test_default_all_pkgs_by_name_regex(self):
        self.assertEqual(
            self.pm.all_pkgs_by_name_regex('.*doesntmatter$'), [])

    def test_default_pkg_by_name(self):
        self.assertEqual(self.pm.pkg_by_name('foo'), None)


class RpmPackageManagerTests(unittest.TestCase):

    def setUp(self):
        if distro.detect().name not in ['fedora', 'centos', 'rhel']:
            self.skipTest('Not running on an RPM distribution')
        self.pm = RpmPackageManager()

    def test_load_all_packages(self):
        self.assertNotEquals(self.pm.packages, {})

    def test_pkg_is_formatted(self):
        kpkg = self.pm.pkg_by_name('coreutils')
        self.assertIsInstance(kpkg, dict)
        self.assertIsInstance(kpkg['version'], list)
        self.assertEqual(kpkg['pkg_manager'], 'rpm')


class DpkgPackageManagerTests(unittest.TestCase):

    def setUp(self):
        if distro.detect().name not in ['Ubuntu', 'debian']:
            self.skipTest('Not running on a dpkg distribution')
        self.pm = DpkgPackageManager()

    def test_load_all_packages(self):
        self.assertNotEquals(self.pm.packages, {})

    def test_pkg_is_formatted(self):
        kpkg = self.pm.pkg_by_name('coreutils')
        self.assertIsInstance(kpkg, dict)
        self.assertIsInstance(kpkg['version'], list)
        self.assertEqual(kpkg['pkg_manager'], 'dpkg')


class MultiPackageManagerTests(unittest.TestCase):

    def setUp(self):
        self.pm = MultiPackageManager(primary=RpmPackageManager,
                                      fallbacks=[DpkgPackageManager])

    def test_load_all_packages(self):
        self.assertNotEquals(self.pm.packages, {})

    def test_pkg_is_formatted(self):
        kpkg = self.pm.pkg_by_name('coreutils')
        self.assertIsInstance(kpkg, dict)
        self.assertIsInstance(kpkg['version'], list)
        _local = distro.detect().name
        if _local in ['Ubuntu', 'debian']:
            self.assertEqual(kpkg['pkg_manager'], 'dpkg')
        else:
            self.assertEqual(kpkg['pkg_manager'], 'rpm')


if __name__ == "__main__":
    unittest.main()

# vim: set et ts=4 sw=4 :