summaryrefslogtreecommitdiffstats
path: root/src/software/test/test_installed_software_identity.py
blob: 1065c2010a98daab91fe16fe831aa300d0f1c4c4 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env python
#
# Copyright (C) 2012 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_InstalledSoftwareIdentity provider.
"""

import pywbem
import socket
import unittest

import base
import rpmcache

class TestInstalledSoftwareIdentity(base.SoftwareBaseTestCase):
    """
    Basic cim operations test.
    """

    CLASS_NAME = "LMI_InstalledSoftwareIdentity"
    KEYS = ("InstalledSoftware", "System")

    def make_op(self, pkg, newer=True):
        """
        @return object path of InstalledSoftwareIdentity association
        """
        objpath = self.objpath.copy()
        objpath["System"] = pywbem.CIMInstanceName(
                classname="Linux_ComputerSystem",
                namespace="root/cimv2",
                keybindings=pywbem.NocaseDict({
                    "CreationClassName" : "Linux_ComputerSystem",
                    "Name" : socket.gethostname()
                }))
        objpath["InstalledSoftware"] = pywbem.CIMInstanceName(
                classname="LMI_SoftwareIdentity",
                namespace="root/cimv2",
                keybindings=pywbem.NocaseDict({
                    "InstanceID" : 'LMI:PKG:' + pkg.get_nevra(newer=newer,
                        with_epoch="ALWAYS")
                }))
        return objpath

    def test_get_instance(self):
        """
        Tests GetInstance call on packages from our rpm cache.
        """
        for pkg in self.safe_pkgs:
            objpath = self.make_op(pkg)
            inst = self.conn.GetInstance(InstanceName=objpath)
            self.assertEqual(inst.path, objpath,
                    "Object paths should match for package %s"%pkg)
            for key in self.KEYS:
                self.assertTrue(inst.properties.has_key(key),
                        "OP is missing \"%s\" key for package %s"%(key, pkg))
                self.assertEqual(inst[key], inst.path[key])
            self.assertEqual(objpath, inst.path,
                    "Object paths should match for package %s"%pkg)

            # try it now with CIM_ComputerSystem
            system = objpath["System"].copy()
            objpath["System"].classname = "CIM_ComputerSystem"
            objpath["System"]["CreationClassName"] = "CIM_ComputerSystem"

            inst = self.conn.GetInstance(InstanceName=objpath)
            self.assertEqual(objpath, inst.path,
                    "Object paths should match for package %s"%pkg)
            for key in self.KEYS:
                self.assertTrue(inst.properties.has_key(key),
                        "OP is missing \"%s\" key for package %s"%(key, pkg))
            self.assertEqual(system, inst["System"])
            self.assertEqual(objpath, inst.path)

    @base.mark_tedious
    def test_enum_instance_names_safe(self):
        """
        Tests EnumInstanceNames call on installed packages.
        """
        inames = self.conn.EnumerateInstanceNames(ClassName=self.CLASS_NAME)
        self.assertGreater(len(inames), 0)
        objpath = self.make_op(self.safe_pkgs[0])
        for iname in inames:
            self.assertIsInstance(iname, pywbem.CIMInstanceName)
            self.assertEqual(iname.namespace, 'root/cimv2')
            self.assertEqual(iname.classname, self.CLASS_NAME)
            self.assertEqual(sorted(iname.keys()), sorted(self.KEYS))
            self.assertEqual(objpath["System"], iname["System"])
        nevra_set = set(i["InstalledSoftware"]["InstanceID"] for i in inames)
        for pkg in self.safe_pkgs:
            nevra = 'LMI:PKG:'+pkg.get_nevra(with_epoch="ALWAYS")
            self.assertTrue(nevra in nevra_set,
                    'Missing nevra "%s".' % nevra)

    @base.mark_tedious
    def test_enum_instances(self):
        """
        Tests EnumInstances call on installed packages.
        """
        insts = self.conn.EnumerateInstances(ClassName=self.CLASS_NAME)
        self.assertGreater(len(insts), 0)
        for inst in insts:
            self.assertIsInstance(inst, pywbem.CIMInstance)
            self.assertEqual(inst.path.namespace, 'root/cimv2')
            self.assertEqual(inst.path.classname, self.CLASS_NAME)
            self.assertEqual(inst.classname, self.CLASS_NAME)
            self.assertEqual(sorted(inst.keys()), sorted(self.KEYS))
            self.assertEqual(sorted(inst.path.keys()), sorted(self.KEYS))
            for key in self.KEYS:
                self.assertEqual(inst[key], inst.path[key])
        nevra_set = set(i["InstalledSoftware"]["InstanceID"] for i in insts)
        for pkg in self.safe_pkgs:
            nevra = 'LMI:PKG:'+pkg.get_nevra(with_epoch="ALWAYS")
            self.assertTrue(nevra in nevra_set, "Missing pkg %s in nevra_set."
                    % nevra)

    @base.mark_tedious
    @base.mark_dangerous
    def test_enum_instance_names(self):
        """
        Tests EnumInstanceNames call on dangerous packages.
        """
        pkg = self.dangerous_pkgs[0]

        rpmcache.ensure_pkg_installed(pkg)

        inames1 = self.conn.EnumerateInstanceNames(
                ClassName=self.CLASS_NAME)
        self.assertGreater(len(inames1), 1)
        self.assertIn('LMI:PKG:'+pkg.get_nevra(with_epoch="ALWAYS"),
                set(i["InstalledSoftware"]["InstanceID"] for i in inames1))

        rpmcache.remove_pkg(pkg.name)
        inames2 = self.conn.EnumerateInstanceNames(
                ClassName=self.CLASS_NAME)
        self.assertEqual(len(inames1), len(inames2) + 1)
        self.assertNotIn('LMI:PKG:'+pkg.get_nevra(with_epoch="ALWAYS"),
                set(i["InstalledSoftware"]["InstanceID"] for i in inames2))

        rpmcache.install_pkg(pkg)
        inames3 = self.conn.EnumerateInstanceNames(
                ClassName=self.CLASS_NAME)
        self.assertEqual(len(inames1), len(inames3))
        self.assertIn('LMI:PKG:'+pkg.get_nevra(with_epoch="ALWAYS"),
                set(i["InstalledSoftware"]["InstanceID"] for i in inames3))

    @base.mark_dangerous
    def test_create_instance(self):
        """
        Tests new instance creation for dangerous packages.
        """
        for pkg in self.dangerous_pkgs:
            if rpmcache.is_pkg_installed(pkg.name):
                rpmcache.remove_pkg(pkg.name)
            objpath = self.make_op(pkg)
            self.assertFalse(rpmcache.is_pkg_installed(pkg))

            inst = pywbem.CIMInstance(self.CLASS_NAME, 
                    properties={
                        "InstalledSoftware" : objpath["InstalledSoftware"],
                        "System"            : objpath["System"]},
                    path=objpath)
            iname = self.conn.CreateInstance(NewInstance=inst)
            self.assertTrue(rpmcache.is_pkg_installed(pkg))
            self.assertEqual(objpath, iname,
                    'Object path does not match for %s.' % pkg)

            # try to install second time
            with self.assertRaises(pywbem.CIMError) as cm:
                self.conn.CreateInstance(NewInstance=inst)
            self.assertEqual(cm.exception.args[0],
                pywbem.CIM_ERR_ALREADY_EXISTS)

    @base.mark_dangerous
    def test_delete_instance(self):
        """
        Tests removing of dangerous packages by deleting instance name.
        """
        for pkg in self.dangerous_pkgs:
            self.ensure_pkg_installed(pkg)
            self.assertTrue(rpmcache.is_pkg_installed(pkg))
            objpath = self.make_op(pkg)
            self.conn.DeleteInstance(InstanceName=objpath)
            self.assertFalse(rpmcache.is_pkg_installed(pkg),
                    "Failed to delete instance for %s." % pkg)

            with self.assertRaises(pywbem.CIMError) as cm:
                self.conn.DeleteInstance(InstanceName=objpath)
            self.assertEqual(cm.exception.args[0],
                pywbem.CIM_ERR_NOT_FOUND)

    @base.mark_tedious
    def test_get_system_referents(self):
        """
        Test ReferenceNames for ComputerSystem.
        """
        objpath = self.make_op(self.safe_pkgs[0])
        refs = self.conn.AssociatorNames(
                Role="System",
                ObjectName=objpath["System"],
                ResultRole="InstalledSoftware",
                ResultClass="LMI_SoftwareIdentity")
        self.assertGreater(len(refs), 0)
        for ref in refs:
            self.assertIsInstance(ref, pywbem.CIMInstanceName)
            self.assertEqual(ref.namespace, 'root/cimv2')
            self.assertEqual(ref.classname, "LMI_SoftwareIdentity")
            self.assertEqual(sorted(ref.keys()), ["InstanceID"])
            self.assertTrue(ref["InstanceID"].startswith("LMI:PKG:"))

        nevra_set = set(i["InstanceID"] for i in refs)
        for pkg in self.safe_pkgs:
            nevra = 'LMI:PKG:'+pkg.get_nevra(with_epoch="ALWAYS")
            self.assertTrue(nevra in nevra_set,
                    'Missing nevra "%s".' % nevra)

    def test_get_installed_software_referents(self):
        """
        Test ReferenceNames for SoftwareIdentity.
        """
        for pkg in self.safe_pkgs:
            objpath = self.make_op(pkg)
            refs = self.conn.AssociatorNames(
                    ObjectName=objpath["InstalledSoftware"],
                    Role="InstalledSoftware",
                    ResultRole="System",
                    ResultClass="Linux_ComputerSystem")
            self.assertEqual(len(refs), 1)
            ref = refs[0]
            self.assertEqual(objpath["System"], ref)

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

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