summaryrefslogtreecommitdiffstats
path: root/src/software/test/test_software_package.py
blob: 34116780d8f6d894359691ddcae3bb1341341093 (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
#!/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_SoftwareInstalledPackage provider.
"""

import pywbem
import unittest

import common
import rpmcache

class TestSoftwarePackage(common.SoftwareBaseTestCase): #pylint: disable=R0904
    """
    Basic cim operations test.
    """

    CLASS_NAME = "LMI_SoftwarePackage"
    KEYS = ( "Name", "SoftwareElementID", "SoftwareElementState"
           , "TargetOperatingSystem", "Version")

    def make_op(self, pkg, newer=True, ses=2):
        """
        @param ses SoftwareElementState property value
        @return object path of SoftwarePackage
        """
        objpath = self.objpath.copy()
        objpath["Name"] = pkg.name
        objpath["SoftwareElementID"] = pkg.get_nevra(newer, "ALWAYS")
        objpath["SoftwareElementState"] = pywbem.Uint16(ses)
        objpath["TargetOperatingSystem"] = pywbem.Uint16(36)
        objpath["Version"] = getattr(pkg, 'up_ver' if newer else 'ver')
        return objpath

    @common.mark_dangerous
    def test_get_instance(self):
        """
        Tests GetInstance call on packages from our rpm cache.
        TODO: test this in non-dangerous way
        """
        for pkg in self.pkgdb:
            if common.is_installed(pkg.name):
                common.remove_pkg(pkg.name)
            objpath = self.make_op(pkg, ses=1)
            inst = self.conn.GetInstance(InstanceName=objpath, LocalOnly=False)
            self.assertEqual(inst.path, objpath,
                    "Object paths should match for %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.path[key], inst[key],
                        "Object paths of instance should match for %s"%pkg)
            self.assertEqual(pkg.up_rel, inst['Release'],
                    "Release property should match for %s"%pkg)
            common.install_pkg(pkg)
            objpath['SoftwareElementState'] = pywbem.Uint16(2)
            inst = self.conn.GetInstance(InstanceName=objpath, LocalOnly=False)
            self.assertEqual(inst.path, objpath,
                    "Object paths should match for %s"%pkg)

            # try to leave out epoch part
            if pkg.epoch == "0":
                op_no_epoch = objpath.copy()
                op_no_epoch["SoftwareElementID"] = rpmcache.make_nevra(pkg.name,
                        pkg.up_epoch, pkg.up_ver, pkg.up_rel, pkg.arch, "NEVER")
                self.assertNotIn(":", op_no_epoch["SoftwareElementID"])
                inst = self.conn.GetInstance(
                        InstanceName=op_no_epoch, LocalOnly=False)
                self.assertIn(inst.path, (objpath, op_no_epoch))

    @common.mark_dangerous
    def test_method_install(self):
        """
        Tests Install method invocation.
        """
        for pkg in self.pkgdb:
            if common.is_installed(pkg.name):
                common.remove_pkg(pkg.name)
            objpath = self.make_op(pkg, ses=1)
            (rval, oparms) = self.conn.InvokeMethod(
                    MethodName="Install",
                    ObjectName=objpath)
            self.assertEqual(pywbem.Uint32(1), rval,
                    "Installation of %s should be successful"%pkg)
            self.assertEqual(1, len(oparms))
            self.assertTrue(oparms.has_key('Installed'))
            objpath['SoftwareElementState'] = pywbem.Uint16(2)
            self.assertEqual(oparms['Installed'], objpath,
                    "Object paths should match for %s"%pkg)
            self.assertTrue(common.is_installed(pkg.name),
                    "Package %s must be installed"%pkg)
            (rval, oparms) = self.conn.InvokeMethod(
                    MethodName="Install",
                    ObjectName=objpath)
            self.assertEqual(pywbem.Uint32(0), rval,
                    "Installation of %s should fail"%pkg)
            self.assertEqual(len(oparms), 1)
            self.assertEqual(oparms['Installed'], objpath,
                    "Object paths should match for %s"%pkg)

    @common.mark_dangerous
    def test_method_remove(self):
        """
        Tests Remove method invocation.
        """
        for pkg in self.pkgdb:
            if not common.is_installed(pkg.name):
                common.install_pkg(pkg)
            objpath = self.make_op(pkg)
            (rval, oparms) = self.conn.InvokeMethod(
                    MethodName="Remove",
                    ObjectName=objpath)
            self.assertEqual(pywbem.Uint16(1), rval,
                    "Removal of package should succeed"%pkg)
            self.assertEqual(0, len(oparms))
            self.assertFalse(common.is_installed(pkg.name),
                    "Package %s should not be installed"%pkg)
            objpath["SoftwareElementState"] = pywbem.Uint16(1)
            (rval, oparms) = self.conn.InvokeMethod(
                    MethodName="Remove",
                    ObjectName=objpath)
            self.assertEqual(pywbem.Uint32(0), rval,
                    "Removal of %s should fail"%pkg)
            self.assertEqual(len(oparms), 0)

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

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