summaryrefslogtreecommitdiffstats
path: root/src/software/test/test_resource_for_software_identity.py
blob: d38da463a4577864d38862d408a945c1da239c4a (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
#!/usr/bin/env python
#
# Copyright (C) 2012-2013 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_ResourceForSoftwareIdentity provider.
"""

import pywbem
import socket
import unittest

import base

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

    CLASS_NAME = "LMI_ResourceForSoftwareIdentity"
    KEYS = ("ManagedElement", "AvailableSAP")

    @classmethod
    def needs_repodb(cls):
        return True

    def make_op(self, pkg=None, newer=True, repo=None):
        """
        @return object path of ResourceForSoftwareIdentity association
        """
        objpath = self.objpath.copy()
        objpath["AvailableSAP"] = pywbem.CIMInstanceName(
                classname="LMI_SoftwareIdentityResource",
                namespace="root/cimv2",
                keybindings=pywbem.NocaseDict({
                    "CreationClassName" : "LMI_SoftwareIdentityResource",
                    "SystemCreationClassName" : "Linux_ComputerSystem",
                    "SystemName" : socket.gethostname()
                }))
        if repo is not None:
            objpath["AvailableSAP"]["Name"] = repo.repoid
        elif pkg is not None:
            objpath["AvailableSAP"]["Name"] = getattr(pkg,
                    'up_repo' if newer else 'repo')
        objpath["ManagedElement"] = pywbem.CIMInstanceName(
                classname="LMI_SoftwareIdentity",
                namespace="root/cimv2")
        if pkg is not None:
            objpath["ManagedElement"]["InstanceID"] = \
                'LMI:LMI_SoftwareIdentity:' + pkg.get_nevra(newer=newer, with_epoch="ALWAYS")
        return objpath

    @base.mark_dangerous
    def test_get_instance(self):
        """
        Tests GetInstance call on packages from our rpm cache.
        """
        for pkg in self.dangerous_pkgs:
            objpath = self.make_op(pkg)
            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(inst[key], inst.path[key],
                        'Key property "%s" does not match for package "%s"!' %
                        (key, pkg))

    @base.mark_tedious
    def test_get_resource_referents(self):
        """
        Test ReferenceNames for AvailableSAP.
        """
        for repo in self.repodb:
            objpath = self.make_op(repo=repo)
            if not repo.pkg_count or repo.pkg_count > 10000:
                # do not test too big repositories
                continue
            refs = self.conn.AssociatorNames(
                    Role="AvailableSAP",
                    ObjectName=objpath["AvailableSAP"],
                    ResultRole="ManagedElement",
                    ResultClass="LMI_SoftwareIdentity")
            # there may be empty repositories
            #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:LMI_SoftwareIdentity:"))

            nevra_set = set(i["InstanceID"] for i in refs)
            # NOTE: installed packages might not be available
            for pkg, up in ((pkg, up) for pkg in self.dangerous_pkgs
                    for up in (True, False)):
                nevra = 'LMI:LMI_SoftwareIdentity:'+pkg.get_nevra(
                        newer=up, with_epoch="ALWAYS")
                reponame = getattr(pkg, 'up_repo' if up else 'repo')
                if reponame == repo.repoid:
                    self.assertTrue(nevra in nevra_set,
                            'Missing nevra "%s" for repo "%s".' % (nevra,
                                reponame))

    @base.mark_tedious
    def test_get_resource_referents_for_disabled_repo(self):
        """
        Test ReferenceNames for AvailableSAP, which is disabled.
        """
        for repo in self.repodb:
            if repo.status:
                continue    # test only disabled repositories
            objpath = self.make_op(repo=repo)
            refs = self.conn.AssociatorNames(
                    Role="AvailableSAP",
                    ObjectName=objpath["AvailableSAP"],
                    ResultRole="ManagedElement",
                    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:LMI_SoftwareIdentity:"))

    @base.mark_dangerous
    def test_get_managed_element_referents(self):
        """
        Test ReferenceNames for SoftwareIdentity.
        """
        for pkg, up in ((pkg, up) for pkg in self.dangerous_pkgs
                for up in (True, False)):
            objpath = self.make_op(pkg, newer=up)
            refs = self.conn.AssociatorNames(
                    ObjectName=objpath["ManagedElement"],
                    Role="ManagedElement",
                    ResultRole="AvailableSAP",
                    ResultClass="LMI_SoftwareIdentityResource")
            self.assertEqual(1, len(refs),
                    'No repo found for pkg "%s".' % pkg.get_nevra(newer=up,
                        with_epoch="ALWAYS"))
            ref = refs[0]
            self.assertIsInstance(ref, pywbem.CIMInstanceName)
            self.assertEqual(ref.namespace, 'root/cimv2')
            self.assertEqual(ref.classname, "LMI_SoftwareIdentityResource")
            self.assertEqual(sorted(ref.keys()),
                    sorted(["SystemCreationClassName", "SystemName",
                        "Name", "CreationClassName"]))
            self.assertEqual(
                    getattr(pkg, 'up_repo' if up else 'repo'),
                    ref["Name"], 'Repository name does not match for pkg "%s"'%
                    pkg.get_nevra(newer=up, with_epoch="ALWAYS"))

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

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