summaryrefslogtreecommitdiffstats
path: root/src/software/test/test_software_installed_package.py
blob: 5bcb9d284889084712ee2c5eaf7b4f81fe92e21a (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
#!/usr/bin/env python

from common import *
import shutil
import socket
import stat

class TestSoftwareInstalledPackage(SoftwareBaseTestCase):

    CLASS_NAME = "LMI_SoftwareInstalledPackage"
    KEYS = ( "Software", "System")

    def make_op(self, name, epoch, ver, rel, arch, ses=2):
        op = self.op.copy()
        pkg_op = pywbem.CIMInstanceName(
                classname="LMI_SoftwarePackage", namespace="root/cimv2",
                keybindings={
                    "Name" : name,
                    "SoftwareElementID" : make_nevra(
                        name, epoch, ver, rel, arch, "ALWAYS"),
                    "SoftwareElementState" : pywbem.Uint16(ses),
                    "TargetOperatingSystem" : pywbem.Uint16(36),
                    "Version" : ver })
        system_op = pywbem.CIMInstanceName(
                classname="CIM_ComputerSystem", namespace="root/cimv2",
                keybindings={
                    "CreationClassName" : "CIM_ComputerSystem",
                    "Name" : socket.gethostname() })
        op["Software"] = pkg_op
        op["System"] = system_op
        return op

    def test_get_instance(self):
        for pkg in packages:
            if not is_installed(*pkg[:5]):
                install_pkg(*pkg[:5])
            op = self.make_op(*pkg[:5])
            inst = self.conn.GetInstance(InstanceName=op, LocalOnly=False)
            self.assertIsSubclass(inst.path.classname, self.CLASS_NAME)
            self.assertIsSubclass(
                    inst.path['System'].classname, op['System'].classname)
            self.assertIsSubclass(
                    inst.path['Software'].classname, op['Software'].classname)
            self.assertEqual(len(inst.path.keys()), 2)
            for key in self.KEYS:
                self.assertEqual(inst[key], inst.path[key])
            self.assertEqual(inst['Software'], op['Software'])
            remove_pkg(pkg.name)
            op['Software']['SoftwareElementState'] = pywbem.Uint16(1)
            with self.assertRaises(pywbem.CIMError) as cm:
                self.conn.GetInstance(InstanceName=op, LocalOnly=False)
            self.assertEqual(cm.exception.args[0], pywbem.CIM_ERR_NOT_FOUND)

    def test_enum_instances(self):
        pkg = packages[0]
        if is_installed(*pkg[:5]):
            remove_pkg(pkg.name)
        insts1 = self.conn.EnumerateInstanceNames(ClassName=self.CLASS_NAME)
        install_pkg(*pkg[:5])
        insts2 = self.conn.EnumerateInstanceNames(ClassName=self.CLASS_NAME)
        self.assertEqual(len(insts1) + 1, len(insts2))

        op = self.make_op(*pkg[:5])
        self.assertIn(op['Software'], (inst['Software'] for inst in insts2))
        self.assertTrue(all(isinstance(inst, pywbem.CIMInstanceName)
            for inst in insts1))

        insts1 = self.conn.EnumerateInstances(ClassName=self.CLASS_NAME)
        remove_pkg(pkg.name)
        insts2 = self.conn.EnumerateInstances(ClassName=self.CLASS_NAME)

        self.assertEqual(len(insts2) + 1, len(insts1))
        self.assertIn(op['Software'], (inst['Software'] for inst in insts1))
        self.assertTrue(all(inst['Software'] == inst.path['Software']
            for inst in insts1))
        self.assertTrue(all(inst['System'] == inst.path['System']
            for inst in insts1))

    def test_create_instance(self):
        for pkg in packages:
            if is_installed(pkg.name):
                remove_pkg(pkg.name)
            self.assertFalse(is_installed(pkg.name))
            op = self.make_op(*(list(pkg[:5]) + [1]))
            inst = pywbem.CIMInstance(classname=op.classname, path=op)
            inst["Software"] = op["Software"]
            inst["System"] = op["System"]
            iname = self.conn.CreateInstance(NewInstance=inst)
            self.assertIsInstance(iname, pywbem.CIMInstanceName)
            op["Software"]["SoftwareElementState"] = pywbem.Uint16(2)
            self.assertEqual(iname["Software"], op["Software"])
            self.assertIsInstance(iname["System"], pywbem.CIMInstanceName)
            self.assertIsSubclass(iname["System"].classname,
                    op["System"].classname)
            self.assertEqual(set(iname.keys()), set(op.keys()))
            self.assertTrue(is_installed(pkg.name))

            with self.assertRaises(pywbem.CIMError) as cm:
                self.conn.CreateInstance(NewInstance=inst)
            self.assertEqual(cm.exception.args[0],
                    pywbem.CIM_ERR_ALREADY_EXISTS)
            
    def test_delete_instance(self):
        for pkg in packages:
            if not is_installed(pkg.name):
                install_pkg(*pkg[:5])
            self.assertTrue(is_installed(pkg.name))
            op = self.make_op(*pkg[:5])
            self.conn.DeleteInstance(op)
            self.assertFalse(is_installed(pkg.name))
            with self.assertRaises(pywbem.CIMError) as cm:
                self.conn.DeleteInstance(op)
            self.assertEqual(cm.exception.args[0], pywbem.CIM_ERR_NOT_FOUND)

    def test_check_integrity(self):
        for pkg_nevra, files in pkg_files:
            m = re_nevra.match(pkg_nevra)
            name, epoch, ver, rel, arch = [ m.group(k) for k in (
                "name", "epoch", "ver", "rel", "arch") ]
            if (  (is_installed(nevra=pkg_nevra) and not verify_pkg(name))
               or (is_installed(name) and not is_installed(nevra=pkg_nevra))) :
                remove_pkg(name)
            if not is_installed(name):
                install_pkg(name, epoch, ver, rel, arch)
            self.assertTrue(is_installed(nevra=pkg_nevra))

            op = self.make_op(name, epoch, ver, rel, arch)
            (rval, oparms) = self.conn.InvokeMethod(
                    MethodName="CheckIntegrity",
                    ObjectName=op)
            self.assertEqual(rval, pywbem.Uint32(0)) # check passed
            self.assertEqual(len(oparms), 1)
            self.assertIn("Failed", oparms)
            self.assertEqual(len(oparms["Failed"]), 0)

            cnt_bad = 0
            for f in files:
                stats = os.lstat(f)
                if os.path.islink(f):       # modify symbolic link
                    target = os.readlink(f)
                    os.remove(f)
                    os.symlink(target, f) # just touch symlink
                    (rval, oparms) = self.conn.InvokeMethod(
                            MethodName="CheckIntegrity",
                            ObjectName=op)
                    # symlink must pass
                    self.assertEqual(len(oparms["Failed"]), cnt_bad)
                    os.remove(f)
                    # now change target
                    os.symlink("wrong_link_target", f)
                elif os.path.isdir(f):      # check directory
                    os.chmod(f, stats.st_mode)   # just touch dir
                    (rval, oparms) = self.conn.InvokeMethod(
                            MethodName="CheckIntegrity",
                            ObjectName=op)
                    # dir must pass
                    self.assertEqual(len(oparms["Failed"]), cnt_bad)
                    # modify read access of directory
                    os.chmod(f, stats.st_mode ^ stat.S_IROTH)
                else:                       # modify regular file
                    # just touch file - this is enough to make it fail
                    with open(f, "w+") as fobj: pass
                cnt_bad += 1
                (rval, oparms) = self.conn.InvokeMethod(
                        MethodName="CheckIntegrity",
                        ObjectName=op)
                self.assertEqual(rval, pywbem.Uint32(1))
                self.assertEqual(len(oparms), 1)
                self.assertIn("Failed", oparms)
                self.assertEqual(len(oparms["Failed"]), cnt_bad)
                self.assertIn(f, (p["Name"] for p in oparms["Failed"]))

    def test_method_update(self):
        for pkg in packages:
            if not pkg.updatable: continue
            if is_installed(pkg.name) and not is_installed(*pkg[:5]):
                remove_pkg(pkg.name)
            if not is_installed(pkg.name):
                install_pkg(*pkg[:5])
            self.assertTrue(is_installed(*pkg[:5]))
            
            op = self.make_op(*pkg[:5])
            op_up = self.make_op(pkg.name, pkg.up_epoch,
                    pkg.up_ver, pkg.up_rel, pkg.arch)
            (rval, oparms) = self.conn.InvokeMethod(
                    MethodName="Update",
                    ObjectName=op)
            self.assertEqual(rval, pywbem.Uint16(1))
            self.assertEqual(len(oparms), 1)
            self.assertIn("Installed", oparms)
            self.assertEqual(oparms["Installed"], op_up["Software"])
            self.assertTrue(is_installed(pkg.name, pkg.up_epoch,
                pkg.up_ver, pkg.up_rel, pkg.arch))

            (rval, oparms) = self.conn.InvokeMethod(
                    MethodName="Update",
                    ObjectName=op_up)
            self.assertEqual(rval, pywbem.Uint16(0))
            self.assertEqual(len(oparms), 1)
            self.assertEqual(oparms["Installed"], op_up["Software"])

            with self.assertRaises(pywbem.CIMError) as cm:
                self.conn.InvokeMethod(
                        MethodName="Update",
                        ObjectName=op)
            self.assertEqual(cm.exception.args[0],
                    pywbem.CIM_ERR_NOT_FOUND)

            remove_pkg(pkg.name)
            install_pkg(*pkg[:5])
            self.assertTrue(is_installed(*pkg[:5]))

            (rval, oparms) = self.conn.InvokeMethod(
                    MethodName="Update",
                    ObjectName=op,
                    Epoch=pkg.epoch,
                    Version=pkg.ver,
                    Release=pkg.rel)
            self.assertEqual(rval, pywbem.Uint16(0))
            self.assertEqual(oparms["Installed"], op["Software"])
                    
            (rval, oparms) = self.conn.InvokeMethod(
                    MethodName="Update",
                    ObjectName=op,
                    Epoch=pkg.up_epoch,
                    Version=pkg.up_ver,
                    Release=pkg.up_rel)
            self.assertEqual(rval, pywbem.Uint16(1))
            self.assertEqual(oparms["Installed"], op_up["Software"])

    @classmethod
    def tearDownClass(cls):
        for pkg_nevra, files in pkg_files:
            name = re_nevra.match(pkg_nevra).group('name')
            if is_installed(name) and not verify_pkg(name):
                remove_pkg(name)
            if not is_installed(name):
                install_pkg(nevra=pkg_nevra)

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