summaryrefslogtreecommitdiffstats
path: root/src/software/test/test_software_identity_checks.py
blob: b9d5fbbbc7795b496db52e82ffd95f9db0698460 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
#
# Copyright (C) 2012-2014 Red Hat, Inc.  All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# Authors: Michal Minar <miminar@redhat.com>
#
"""
Unit tests for ``LMI_SoftwareIdentityChecks`` provider.
"""

import os
import pywbem
import subprocess
import unittest

from lmi.test.lmibase import enable_lmi_exceptions
import package
import swbase
import util

SOFTWARE_ELEMENT_STATE_EXECUTABLE = 2

class TestSoftwareIdentityChecks(swbase.SwTestCase):
    """
    Basic cim operations test.
    """

    CLASS_NAME = "LMI_SoftwareIdentityChecks"
    KEYS = ("Check", "Element")

    def make_op(self, pkg, filepath):
        """
        :returns: Object path of ``LMI_SoftwareIdentityChecks``
        :rtype: :py:class:`lmi.shell.LMIInstanceName`
        """
        return self.cim_class.new_instance_name({
            "Check" : self.ns.LMI_SoftwareIdentityFileCheck.new_instance_name({
                "CheckID" : 'LMI:LMI_SoftwareIdentityFileCheck',
                "Name" : filepath,
                "SoftwareElementID" : pkg.nevra,
                "SoftwareElementState" : SOFTWARE_ELEMENT_STATE_EXECUTABLE,
                "TargetOperatingSystem" : util.get_target_operating_system(),
                "Version" : pkg.evra
                }),
            "Element" : self.ns.LMI_SoftwareIdentity.new_instance_name({
                "InstanceID" : 'LMI:LMI_SoftwareIdentity:' + pkg.nevra
                })
            })

    def setUp(self):
        to_uninstall = set()
        for repo in self.repodb.values():
            for pkg in repo.packages:
                to_uninstall.add(pkg.name)
        to_uninstall = list(package.filter_installed_packages(to_uninstall))
        package.remove_pkgs(to_uninstall, suppress_stderr=True)

    @swbase.test_with_packages('stable#pkg1')
    def test_get_instance(self):
        """
        Test ``GetInstance()`` call on ``LMI_SoftwareIdentityChecks``.
        """
        pkg = self.get_repo('stable')['pkg1']
        count = 0
        for filepath in pkg:
            objpath = self.make_op(pkg, filepath)
            inst = objpath.to_instance()
            self.assertNotEqual(inst, None,
                    "failed to get instance for %s:%s" % (pkg, filepath))
            self.assertCIMNameEqual(inst.path, objpath)
            for key in self.KEYS:
                self.assertCIMNameEqual(
                        getattr(inst, key), getattr(inst.path, key))
            count += 1
        self.assertEqual(count, 7, "there are 7 files in pkg1")

    @enable_lmi_exceptions
    @swbase.test_with_packages('stable#pkg1')
    def test_get_instance_invalid(self):
        """
        Test ``GetInstance()`` call on ``LMI_SoftwareIdentityChecks``
        on not installed file.
        """
        pkg = self.get_repo('stable')['pkg1']
        objpath = self.make_op(pkg, '/not-installed-file')
        self.assertRaisesCIM(pywbem.CIM_ERR_NOT_FOUND, objpath.to_instance)

    @enable_lmi_exceptions
    def test_enum_instance_names(self):
        """
        Test ``EnumInstanceNames()`` call on ``LMI_SoftwareIdentityChecks``.
        Should not be supported.
        """
        self.assertRaisesCIM(pywbem.CIM_ERR_NOT_SUPPORTED,
                self.cim_class.instance_names)

    @swbase.test_with_packages('stable#pkg3')
    def test_package_file_checks(self):
        """
        Try to get file checks associated with package.
        """
        pkg = self.get_repo('stable')['pkg3']
        filepath = '/usr/share/openlmi-sw-test-pkg3'
        objpath = self.make_op(pkg, filepath)
        inst = objpath.Element.to_instance()
        self.assertNotEqual(inst, None,
                "get instance on %s:%s succeeds" % (pkg, filepath))
        refs = inst.associators(
                AssocClass=self.CLASS_NAME,
                Role="Element",
                ResultRole="Check",
                ResultClass="LMI_SoftwareIdentityFileCheck")
        self.assertEqual(len(refs), len(pkg))
        self.assertEqual(len(refs), 10, "there are 10 files in %s" % pkg.name)
        pkg_names = set(pkg.files)
        for ref in refs:
            self.assertIn(ref.Name, set(r.Name for r in refs))
            self.assertEqual(sorted(ref.path.key_properties()),
                ["CheckID", "Name", "SoftwareElementID",
                    "SoftwareElementState",
                    "TargetOperatingSystem", "Version"])
            self.assertEqual(ref.SoftwareElementID, pkg.nevra)
            self.assertIn(ref.Name, pkg_names)
            self.assertEqual(ref.FailedFlags, [],
                    "FailedFlags are empty for unmodified file %s:%s"
                    % (pkg, filepath))
            pkg_names.remove(ref.Name)
        self.assertEqual(len(pkg_names), 0)

    @swbase.test_with_packages('stable#pkg3')
    def test_package_file_check_names(self):
        """
        Try to get file check names associated with package.
        """
        pkg = self.get_repo('stable')['pkg3']
        objpath = self.make_op(pkg, '/usr/share/openlmi-sw-test-pkg3')
        refs = objpath.Element.to_instance().associator_names(
                AssocClass=self.CLASS_NAME,
                Role="Element",
                ResultRole="Check",
                ResultClass="LMI_SoftwareIdentityFileCheck")
        self.assertEqual(len(refs), len(pkg))
        self.assertEqual(len(refs), 10, "there are 10 files in %s" % pkg.name)
        pkg_names = set(pkg.files)
        for ref in refs:
            self.assertIn(ref.Name, set(r.Name for r in refs))
            self.assertEqual(sorted(ref.key_properties()),
                ["CheckID", "Name", "SoftwareElementID",
                    "SoftwareElementState",
                    "TargetOperatingSystem", "Version"])
            self.assertEqual(ref.SoftwareElementID, pkg.nevra)
            self.assertIn(ref.Name, pkg_names)
            pkg_names.remove(ref.Name)
        self.assertEqual(len(pkg_names), 0)

    @swbase.test_with_packages('stable#pkg3')
    def test_file_check_package(self):
        """
        Try to get package assocatied to file check.
        """
        pkg = self.get_repo('stable')['pkg3']
        filepath = '/usr/share/openlmi-sw-test-pkg3/README'
        objpath = self.make_op(pkg, filepath)
        inst = objpath.Check.to_instance()
        self.assertNotEqual(inst, None,
                "get instance for %s:%s succeeds" % (pkg, filepath))
        refs = inst.associators(
                AssocClass=self.CLASS_NAME,
                Role="Check",
                ResultRole="Element",
                ResultClass="LMI_SoftwareIdentity")
        self.assertEqual(len(refs), 1)
        ref = refs[0]
        self.assertCIMNameEqual(ref.path, objpath.Element)
        self.assertEqual(ref.ElementName, pkg.nevra)
        self.assertEqual(ref.VersionString, pkg.evra)

        # try with removed file
        os.remove(filepath)
        refs = objpath.Check.to_instance().associators(
                AssocClass=self.CLASS_NAME,
                Role="Check",
                ResultRole="Element",
                ResultClass="LMI_SoftwareIdentity")
        self.assertEqual(len(refs), 1)

def suite():
    """For unittest loaders."""
    return unittest.TestLoader().loadTestsFromTestCase(
            TestSoftwareIdentityChecks)

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