summaryrefslogtreecommitdiffstats
path: root/src/software/test/test_resource_for_software_identity.py
blob: 9b89cf429dd0893f659e55b838d4bd5060df192e (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/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_ResourceForSoftwareIdentity`` provider.
"""

import unittest

import package
import swbase

ENABLED_STATE_ENABLED = 2

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

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

    def make_op(self, pkg=None, repo=None):
        """
        :returns: Object path of ``LMI_ResourceForSoftwareIdentity``
        :rtype: :py:class:`lmi.shell.LMIInstanceName`
        """
        objpath = self.cim_class.new_instance_name({
            "AvailableSAP" : self.ns.LMI_SoftwareIdentityResource. \
                new_instance_name({
                    "CreationClassName" : "LMI_SoftwareIdentityResource",
                    "SystemCreationClassName" : self.system_cs_name,
                    "SystemName" : self.SYSTEM_NAME
                }),
            "ManagedElement" :
                self.ns.LMI_SoftwareIdentity.new_instance_name({})
            })
        if repo is not None:
            objpath.AvailableSAP.wrapped_object["Name"] = repo.repoid
        elif pkg is not None:
            objpath.AvailableSAP.wrapped_object["Name"] = pkg.repoid
        if pkg is not None:
            objpath.ManagedElement.wrapped_object["InstanceID"] = (
                      'LMI:LMI_SoftwareIdentity:' + pkg.nevra)
        return objpath

    @swbase.test_with_repos('stable', 'updates', 'misc',
            **{'updates-testing' : True})
    def test_get_instance(self):
        """
        Test ``GetInstance()`` call on ``LMI_ResourceForSoftwareIdentity``.
        """
        for repo in self.repodb.values():
            self.assertTrue(repo.status)
            for pkg in repo.packages:
                objpath = self.make_op(pkg, repo)
                inst = objpath.to_instance()
                self.assertNotEqual(inst, None)
                self.assertEqual(set(inst.path.key_properties()),
                        set(self.KEYS))
                for key in self.KEYS:
                    self.assertCIMNameEqual(
                        getattr(inst, key), getattr(inst.path, key),
                        'Key property "%s" does not match for package "%s#%s"!'
                        % (key, repo.repoid, pkg))

    @swbase.test_with_repos(**{'updates' : False})
    def test_get_instance_disabled_repo(self):
        """
        Test ``GetInstance()`` call on ``LMI_ResourceForSoftwareIdentity``
        with disabled repository.
        """
        repo = self.get_repo('updates')
        self.assertFalse(repo.status)
        for pkg in repo.packages:
            objpath = self.make_op(pkg, repo)
            inst = objpath.to_instance()
            self.assertNotEqual(inst, None)
            self.assertEqual(set(inst.path.key_properties()),
                    set(self.KEYS))
            for key in self.KEYS:
                self.assertCIMNameEqual(
                    getattr(inst, key), getattr(inst.path, key),
                    'Key property "%s" does not match for package "%s#%s"!' %
                    (key, repo.repoid, pkg))

    @swbase.test_with_repos('stable')
    def test_repo_identity_names(self):
        """
        Test ``AssociatorNames()`` call on ``LMI_ResourceForSoftwareIdentity``.
        """
        repo = self.get_repo('stable')
        self.assertTrue(repo.status)
        self.assertGreater(repo.pkg_count, 0)

        objpath = self.make_op(repo=repo)
        refs = objpath.AvailableSAP.to_instance().associator_names(
                AssocClass=self.CLASS_NAME,
                Role="AvailableSAP",
                ResultRole="ManagedElement",
                ResultClass="LMI_SoftwareIdentity")
        self.assertEqual(len(refs), repo.pkg_count,
                'repository "%s" is missing software identities'
                % repo.name)
        for ref in refs:
            self.assertEqual(ref.namespace, 'root/cimv2')
            self.assertEqual(ref.classname, "LMI_SoftwareIdentity")
            self.assertEqual(ref.key_properties(), ["InstanceID"])
            self.assertTrue(
                    ref.InstanceID.startswith("LMI:LMI_SoftwareIdentity:"))

        nevra_set = set(i.InstanceID for i in refs)
        for pkg in repo.packages:
            nevra = 'LMI:LMI_SoftwareIdentity:'+pkg.nevra
            self.assertTrue(nevra in nevra_set,
                    'Missing nevra "%s" for repo "%s".' % (nevra,
                        repo.repoid))
            nevra_set.remove(nevra)
        self.assertEqual(len(nevra_set), 0,
                "all packages from repository have been listed")

    @swbase.test_with_repos(stable=False)
    def test_disabled_repo_identity_names(self):
        """
        Test ``AssociatorNames()`` call on ``LMI_ResourceForSoftwareIdentity``.
        """
        repo = self.get_repo('stable')
        self.assertFalse(repo.status)
        self.assertGreater(repo.pkg_count, 0)

        objpath = self.make_op(repo=repo)
        refs = objpath.AvailableSAP.to_instance().associator_names(
                AssocClass=self.CLASS_NAME,
                Role="AvailableSAP",
                ResultRole="ManagedElement",
                ResultClass="LMI_SoftwareIdentity")
        self.assertEqual(len(refs), repo.pkg_count,
                'repository "%s" is missing software identities'
                % repo.name)
        for ref in refs:
            self.assertEqual(ref.namespace, 'root/cimv2')
            self.assertEqual(ref.classname, "LMI_SoftwareIdentity")
            self.assertEqual(ref.key_properties(), ["InstanceID"])
            self.assertTrue(
                    ref.InstanceID.startswith("LMI:LMI_SoftwareIdentity:"))

    @swbase.test_with_repos('updates')
    @swbase.test_with_packages(**{
            'updates#pkg1' : True,
            'updates#pkg2' : True,
            'updates#pkg3' : False,
            'updates#pkg4' : False
    })
    def test_repo_identities(self):
        """
        Test ``Associators()`` call on ``LMI_ResourceForSoftwareIdentity``.
        """
        repo = self.get_repo('updates')
        self.assertTrue(repo.status)
        self.assertGreater(repo.pkg_count, 0)

        objpath = self.make_op(repo=repo)
        refs = objpath.AvailableSAP.to_instance().associators(
                AssocClass=self.CLASS_NAME,
                Role="AvailableSAP",
                ResultRole="ManagedElement",
                ResultClass="LMI_SoftwareIdentity")
        if repo.pkg_count:
            self.assertGreater(len(refs), 0,
                'no software identities associated to repo "%s"'
                % repo.repoid)
        for ref in refs:
            self.assertEqual(ref.namespace, 'root/cimv2')
            self.assertEqual(ref.classname, "LMI_SoftwareIdentity")
            self.assertEqual(ref.path.key_properties(), ["InstanceID"])

        nevra_dict = {i.ElementName: i for i in refs}
        installed_count = 0
        for pkg in repo.packages:
            self.assertTrue(pkg.nevra in nevra_dict,
                    'Missing package "%s" in repo "%s".' % (pkg, repo.repoid))
            if package.is_pkg_installed(pkg):
                self.assertNotEqual(nevra_dict[pkg.nevra].InstallDate, None,
                        "InstallDate property is set for installed and"
                        " available package %s" % pkg)
                installed_count += 1
            else:
                self.assertEqual(nevra_dict[pkg.nevra].InstallDate, None,
                        "InstallDate property is unset for available package"
                        " %s" % pkg)
            del nevra_dict[pkg.nevra]
        self.assertEqual(len(nevra_dict), 0,
                "all packages from repository have been listed")
        self.assertEqual(installed_count, 2)

    @swbase.test_with_repos('stable')
    def test_get_pkg_repository_name(self):
        """
        Try to get software identities associated with repository.
        """
        repo = self.get_repo('stable')
        self.assertTrue(repo.status)
        for pkg in repo.packages:
            objpath = self.make_op(pkg, repo)
            inst = objpath.ManagedElement.to_instance()
            self.assertNotEqual(inst, None,
                    "GetInstance() succeeds for package %s" % pkg)
            refs = inst.associator_names(
                    AssocClass=self.CLASS_NAME,
                    Role="ManagedElement",
                    ResultRole="AvailableSAP",
                    ResultClass="LMI_SoftwareIdentityResource")
            self.assertEqual(1, len(refs),
                    'No repo found for pkg "%s".' % pkg.nevra)
            ref = refs[0]
            self.assertEqual(ref.namespace, 'root/cimv2')
            self.assertEqual(ref.classname, "LMI_SoftwareIdentityResource")
            self.assertEqual(set(ref.key_properties()),
                    set(["SystemCreationClassName", "SystemName",
                        "Name", "CreationClassName"]))
            self.assertEqual(ref.Name, pkg.repoid,
                    'Repository name does not match for pkg "%s"'
                    % pkg.nevra)

    @swbase.test_with_repos('updates')
    @swbase.test_with_packages(**{
        'updates#pkg1' : True,
        'updates#pkg2' : True,
        'updates#pkg3' : False,
        'updates#pkg4' : False
    })
    def test_get_pkg_repository(self):
        """
        Try to get repository for installed and not installed package.
        """
        repo = self.get_repo('updates')
        self.assertTrue(repo.status)
        for pkg in repo.packages:
            objpath = self.make_op(pkg, repo)
            inst = objpath.ManagedElement
            self.assertNotEqual(inst, None,
                    "GetInstance() succeeds for package %s" % pkg)
            refs = inst.to_instance().associators(
                    AssocClass=self.CLASS_NAME,
                    Role="ManagedElement",
                    ResultRole="AvailableSAP",
                    ResultClass="LMI_SoftwareIdentityResource")
            self.assertEqual(1, len(refs),
                    'No repo found for pkg "%s".' % pkg.nevra)
            ref = refs[0]
            self.assertEqual(ref.namespace, 'root/cimv2')
            self.assertEqual(ref.classname, "LMI_SoftwareIdentityResource")
            self.assertEqual(set(ref.path.key_properties()),
                    set(["SystemCreationClassName", "SystemName",
                        "Name", "CreationClassName"]))
            self.assertEqual(ref.Name, pkg.repoid,
                    'Repository name does not match for pkg "%s"'
                    % pkg.nevra)
            self.assertEqual(ref.ElementName, pkg.repoid)
            self.assertEqual(ref.EnabledState, ENABLED_STATE_ENABLED)

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

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