summaryrefslogtreecommitdiffstats
path: root/src/software/test/test_software_identity_resource.py
blob: 8a48d271c7fe7326810e607fb0a2659149f5359c (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/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: Jan Grec <jgrec@redhat.com>
# Authors: Michal Minar <miminar@redhat.com>
#
"""
Unit tests for ``LMI_SoftwareIdentityResource`` provider.
"""

import os
import pywbem
import time
import shutil
import subprocess
import unittest

from lmi.test.lmibase import enable_lmi_exceptions
import repository
import swbase
import util

ENABLED_DEFAULT_NOT_APPLICABLE = 5
ENABLED_STATE_DISABLED = 3
ENABLED_STATE_ENABLED = 2
EXTENDED_RESOURCE_TYPE_RPM = 3
HEALTH_STATE_MAJOR_FAILURE = 20
HEALTH_STATE_OK = 5
INFO_FORMAT_URL = 200
OPERATIONAL_STATUS_ERROR = 6
OPERATIONAL_STATUS_OK = 2
REQUESTED_STATE_ENABLED = 2
REQUESTED_STATE_DISABLED = 3
REQUEST_STATE_CHANGE_SUCCESSFUL = 0
REQUEST_STATE_CHANGE_ERROR = 2
RESOURCE_TYPE_OTHER = 1
PRIMARY_STATUS_ERROR = 3
PRIMARY_STATUS_OK = 1
TRANSITIONING_TO_STATE_NOT_APPLICABLE = 12

DUMMY_TEST_REPO = 'lmi-test'

class TestSoftwareIdentityResource(swbase.SwTestCase):
    """
    Basic cim operations test on ``LMI_SoftwareIdentityResource``.
    """

    CLASS_NAME = "LMI_SoftwareIdentityResource"
    KEYS = ("CreationClassName", "Name", "SystemCreationClassName",
            "SystemName")

    def make_op(self, repo):
        """
        :returns: Object path of ``LMI_SoftwareIdentityResource``
        :rtype: :py:class:`lmi.shell.LMIInstanceName`
        """
        if isinstance(repo, repository.Repository):
            repo = repo.repoid
        return self.cim_class.new_instance_name({
            "Name" : repo,
            "SystemName" : self.SYSTEM_NAME,
            "SystemCreationClassName" : self.system_cs_name,
            "CreationClassName" : self.CLASS_NAME
        })

    def _check_repo_instance(self, repo, inst):
        """
        Checks instance properties of repository.
        """
        objpath = self.make_op(repo)
        self.assertCIMNameEqual(objpath, inst.path)
        for key in self.KEYS:
            if key == 'SystemName':
                self.assertIn(inst.SystemName, (self.SYSTEM_NAME, 'localhost'))
                self.assertIn(inst.path.SystemName, (self.SYSTEM_NAME, 'localhost'))
            else:
                self.assertEqual(getattr(inst, key), getattr(inst.path, key))
        self.assertEqual(inst.Name, repo.repoid)

        self.assertIsInstance(inst.AccessContext, pywbem.Uint16)
        access_info = inst.AccessInfo
        if access_info is not None:
            access_info = access_info.rstrip('/')
        if access_info is not None:
            self.assertIsInstance(access_info, basestring)
        if repo.mirror_list:
            self.assertEqual(access_info, repo.mirror_list,
                    "AccessInfo does not match mirror_list for repo %s" %
                    repo.repoid)
        elif repo.metalink:
            self.assertEqual(access_info, repo.metalink,
                    "AccessInfo does not match metalink for repo %s" %
                    repo.repoid)
        elif access_info is not None:
            self.assertIn(access_info, {u.rstrip('/') for u in repo.base_urls},
                "AccessInfo missing in base_urls for repo %s" % repo.repoid)

        self.assertIsInstance(inst.AvailableRequestedStates, list)
        # Unfortunately, yum-config-manager shortens lines on its output
        # therefor we can not check very long names.
        # 61 characters in version yum-utils-1.1.31-19.fc21
        if len(repo.name) > 60:
            self.assertTrue(inst.Caption.startswith(repo.name))
        else:
            self.assertEqual(inst.Caption, repo.name)
        self.assertIsInstance(inst.Cost, pywbem.Sint32)
        self.assertIsInstance(inst.Description, basestring)
        self.assertEqual(inst.ElementName, repo.repoid)
        self.assertEqual(inst.EnabledDefault, ENABLED_DEFAULT_NOT_APPLICABLE)
        self.assertEqual(
                       ENABLED_STATE_ENABLED
                    if repo.status else ENABLED_STATE_DISABLED,
                inst.EnabledState,
                "EnabledState does not match for repo %s" % repo.repoid)
        self.assertEqual(inst.ExtendedResourceType, EXTENDED_RESOURCE_TYPE_RPM)
        if repo.revision is not None:
            self.assertTrue(isinstance(inst.Generation, (int, long)),
                    'Generation must be integer for repo %s' % repo.repoid)
        if repo.status:
            self.assertEqual(inst.HealthState, HEALTH_STATE_OK)
            self.assertEqual(inst.OperationalStatus, [OPERATIONAL_STATUS_OK])
            self.assertEqual(inst.PrimaryStatus, PRIMARY_STATUS_OK)
        else:
            # repo may not be enabled, but still it can be ready
            # (which is not documented), so let's check both posibilities
            # TODO: health state should be OK for disabled repository
            self.assertIn(inst.HealthState,
                    [HEALTH_STATE_OK, HEALTH_STATE_MAJOR_FAILURE])
            self.assertIn(inst.OperationalStatus,
                    [[OPERATIONAL_STATUS_OK], [OPERATIONAL_STATUS_ERROR]])
            self.assertIn(inst.PrimaryStatus, [PRIMARY_STATUS_OK, PRIMARY_STATUS_ERROR])
        self.assertIsInstance(inst.GPGCheck, bool)
        self.assertEqual(inst.InfoFormat, INFO_FORMAT_URL)
        self.assertEqual("LMI:LMI_SoftwareIdentityResource:"+repo.repoid,
                inst.InstanceID)
        self.assertEqual(inst.MirrorList, repo.mirror_list,
                'MirrorList must match for repo %s' % repo.repoid)
        self.assertIsInstance(inst.OtherAccessContext, basestring)
        self.assertIsInstance(inst.RepoGPGCheck, bool)
        self.assertEqual(
                   ENABLED_STATE_ENABLED
                if repo.status else ENABLED_STATE_DISABLED,
                inst.RequestedState)
        self.assertEqual(inst.ResourceType, RESOURCE_TYPE_OTHER)
        self.assertIsInstance(inst.StatusDescriptions, list)
        self.assertEqual(1, len(inst.StatusDescriptions))
        if repo.last_updated is not None:
            time_stamp = repo.last_updated.replace(
                    microsecond=0, tzinfo=pywbem.cim_types.MinutesFromUTC(0))
            self.assertGreaterEqual(inst.TimeOfLastUpdate.datetime, time_stamp)
        self.assertEqual(TRANSITIONING_TO_STATE_NOT_APPLICABLE,
                inst.TransitioningToState)

    @swbase.test_with_repos(stable=True, updates=False)
    def test_get_instance(self):
        """
        Test ``GetInstance()`` call on ``LMI_SoftwareIdentityResource``.
        """
        stablerepo = self.get_repo('stable')
        self.assertTrue(stablerepo.status, "stable repository is enabled")
        objpath = self.make_op(stablerepo)
        inst = objpath.to_instance()
        self.assertNotEqual(inst, None,
                "GetInstance() call on stable repository is successful"
                ' with path: "%s"' % str(objpath))
        self._check_repo_instance(stablerepo, inst)
        self.assertEqual(set(self.KEYS), set(inst.path.key_properties()))

        updaterepo = self.get_repo('updates')
        objpath = self.make_op(updaterepo)
        self.assertFalse(updaterepo.status, "updates repository is disabled")
        inst = objpath.to_instance()
        self.assertNotEqual(inst, None,
                'GetInstance() call on updates repository is successful'
                ' with path "%s"' % str(objpath))
        self._check_repo_instance(updaterepo, inst)
        self.assertEqual(set(self.KEYS), set(inst.path.key_properties()))

    def test_enum_instance_names(self):
        """
        Test ``EnumInstanceNames()`` call on ``LMI_SoftwareIdentityResource``.
        """
        all_repo_names = set(self.repodb.keys()).union(
                set(self.other_repos.keys()))
        inames = self.cim_class.instance_names()
        for iname in inames:
            self.assertEqual(set(iname.key_properties()), set(self.KEYS))
            self.assertIn(iname.Name, all_repo_names)
            all_repo_names.remove(iname.Name)
            self.assertEqual(iname.SystemCreationClassName, self.system_cs_name)
            self.assertEqual(iname.CreationClassName, self.CLASS_NAME)
        self.assertEqual(len(all_repo_names), 0)

    def test_enum_instances(self):
        """
        Test ``EnumInstances()`` call on ``LMI_SoftwareIdentityResource``.
        """
        all_repo_names = set(self.repodb.keys()).union(
                set(self.other_repos.keys()))
        insts = self.cim_class.instances()
        for inst in insts:
            self.assertEqual(set(inst.path.key_properties()), set(self.KEYS))
            self.assertIn(inst.Name, all_repo_names)
            all_repo_names.remove(inst.Name)
            if inst.Name in self.repodb:
                repo = self.repodb[inst.Name]
            else:
                repo = self.other_repos[inst.Name]
            self._check_repo_instance(repo, inst)
        self.assertEqual(len(all_repo_names), 0)

    @swbase.test_with_repos('updates')
    def test_disable_repo(self):
        """
        Test whether ``LMI_SoftwareIdentityResource`` provider recognizes
        that repository has been disabled.
        """
        repo = self.get_repo('updates')
        self.assertTrue(repo.status)
        objpath = self.make_op(repo)
        inst = objpath.to_instance()
        self.assertNotEqual(inst, None,
                'GetInstance() call on updates repository is successful'
                ' with path "%s"' % str(objpath))
        self.assertEqual(inst.EnabledState, ENABLED_STATE_ENABLED)
        self.assertEqual(inst.HealthState, HEALTH_STATE_OK)
        self.assertEqual(inst.OperationalStatus, [OPERATIONAL_STATUS_OK])
        self.assertEqual(inst.PrimaryStatus, PRIMARY_STATUS_OK)

        repository.set_repos_enabled(repo, False)
        repo.refresh()
        self.assertFalse(repo.status)
        time.sleep(0.1)
        inst.refresh()
        self.assertNotEqual(inst, None,
                'GetInstance() call on updates repository is successful'
                ' with path "%s"' % str(objpath))
        self.assertEqual(inst.EnabledState, ENABLED_STATE_DISABLED)
        self.assertEqual(inst.HealthState, HEALTH_STATE_OK)
        self.assertEqual(inst.OperationalStatus, [OPERATIONAL_STATUS_OK])
        self.assertEqual(inst.PrimaryStatus, PRIMARY_STATUS_OK)


    @swbase.test_with_repos(updates=False)
    def test_enable_repo(self):
        """
        Test whether ``LMI_SoftwareIdentityResource`` provider recognizes that
        repository has been enabled.
        """
        repo = self.get_repo('updates')
        self.assertFalse(repo.status)
        objpath = self.make_op(repo)
        inst = objpath.to_instance()
        self.assertNotEqual(inst, None,
                'GetInstance() call on updates repository is successful'
                ' with path "%s"' % str(objpath))
        self.assertEqual(inst.EnabledState, ENABLED_STATE_DISABLED)
        self.assertEqual(inst.HealthState, HEALTH_STATE_OK)
        self.assertEqual(inst.OperationalStatus, [OPERATIONAL_STATUS_OK])
        self.assertEqual(inst.PrimaryStatus, PRIMARY_STATUS_OK)

        repository.set_repos_enabled(repo, True)
        repo.refresh()
        self.assertTrue(repo.status)
        inst.refresh()
        self.assertNotEqual(inst, None,
                'GetInstance() call on updates repository is successful'
                ' with path "%s"' % str(objpath))
        self.assertEqual(inst.EnabledState, ENABLED_STATE_ENABLED)
        self.assertEqual(inst.HealthState, HEALTH_STATE_OK)
        self.assertEqual(inst.OperationalStatus, [OPERATIONAL_STATUS_OK])
        self.assertEqual(inst.PrimaryStatus, PRIMARY_STATUS_OK)

    @swbase.test_with_repos('stable')
    def test_disable_enabled_repo(self):
        """
        Test ``RequestStateChange()`` method invocation on
        ``LMI_SoftwareIdentityResource`` by requesting disablement of
        repository.
        """
        repo = self.get_repo('stable')
        self.assertTrue(repo.status)
        objpath = self.make_op(repo)
        inst = objpath.to_instance()
        self.assertNotEqual(inst, None,
                'GetInstance() call on stable repository is successful'
                ' with path "%s"' % str(objpath))
        self.assertEqual(inst.EnabledState, ENABLED_STATE_ENABLED)

        # try to disable it with method invocation
        (rval, oparms, _) = inst.RequestStateChange(
                RequestedState=REQUESTED_STATE_DISABLED)
        self.assertEqual(rval, REQUEST_STATE_CHANGE_SUCCESSFUL)
        self.assertEqual(len(oparms), 0)
        repo.refresh()
        self.assertFalse(repo.status)
        inst.refresh()
        self.assertEqual(inst.EnabledState, ENABLED_STATE_DISABLED)
        self.assertEqual(inst.HealthState, HEALTH_STATE_OK)
        self.assertEqual(inst.OperationalStatus, [OPERATIONAL_STATUS_OK])
        self.assertEqual(inst.PrimaryStatus, PRIMARY_STATUS_OK)

    @swbase.test_with_repos(stable=False)
    def test_enable_disabled_repo(self):
        """
        Test ``RequestStateChange()`` method invocation on
        ``LMI_SoftwareIdentityResource`` by requesting enablement of
        repository.
        """
        repo = self.get_repo('stable')
        self.assertFalse(repo.status)
        objpath = self.make_op(repo)
        inst = objpath.to_instance()
        self.assertNotEqual(inst, None,
                'GetInstance() call on stable repository is successful'
                ' with path "%s"' % str(objpath))
        self.assertEqual(inst.EnabledState, ENABLED_STATE_DISABLED)

        # try to enable it with method invocation
        (rval, oparms, _) = inst.RequestStateChange(
                RequestedState=REQUESTED_STATE_ENABLED)
        self.assertEqual(rval, REQUEST_STATE_CHANGE_SUCCESSFUL)
        self.assertEqual(len(oparms), 0)
        repo.refresh()
        self.assertTrue(repo.status)
        inst.refresh()
        self.assertEqual(inst.EnabledState, ENABLED_STATE_ENABLED)
        self.assertEqual(inst.HealthState, HEALTH_STATE_OK)
        self.assertEqual(inst.OperationalStatus, [OPERATIONAL_STATUS_OK])
        self.assertEqual(inst.PrimaryStatus, PRIMARY_STATUS_OK)

    @swbase.test_with_repos('stable')
    def test_enable_enabled_repo(self):
        """
        Test ``RequestStateChange()`` method invocation on
        ``LMI_SoftwareIdentityResource`` by requesting enablement of
        enabled repository.
        """
        repo = self.get_repo('stable')
        self.assertTrue(repo.status)
        objpath = self.make_op(repo)
        inst = objpath.to_instance()
        self.assertNotEqual(inst, None,
                'GetInstance() call on stable repository is successful'
                ' with path "%s"' % str(objpath))
        self.assertEqual(inst.EnabledState, ENABLED_STATE_ENABLED)

        # try to enable it with method invocation
        (rval, oparms, _) = inst.RequestStateChange(
                RequestedState=REQUESTED_STATE_ENABLED)
        self.assertEqual(rval, REQUEST_STATE_CHANGE_SUCCESSFUL)
        self.assertEqual(len(oparms), 0)
        repo.refresh()
        self.assertTrue(repo.status)
        inst.refresh()
        self.assertEqual(inst.EnabledState, ENABLED_STATE_ENABLED)
        self.assertEqual(inst.HealthState, HEALTH_STATE_OK)
        self.assertEqual(inst.OperationalStatus, [OPERATIONAL_STATUS_OK])
        self.assertEqual(inst.PrimaryStatus, PRIMARY_STATUS_OK)

    @swbase.test_with_repos(stable=False)
    def test_disable_disabled_repo(self):
        """
        Test ``RequestStateChange()`` method invocation on
        ``LMI_SoftwareIdentityResource`` by requesting disablement of
        disabled repository.
        """
        repo = self.get_repo('stable')
        self.assertFalse(repo.status)
        objpath = self.make_op(repo)
        inst = objpath.to_instance()
        self.assertNotEqual(inst, None,
                'GetInstance() call on stable repository is successful'
                ' with path "%s"' % str(objpath))
        self.assertEqual(inst.EnabledState, ENABLED_STATE_DISABLED)

        # try to enable it with method invocation
        (rval, oparms, _) = inst.RequestStateChange(
                RequestedState=REQUESTED_STATE_DISABLED)
        self.assertEqual(rval, REQUEST_STATE_CHANGE_SUCCESSFUL)
        self.assertEqual(len(oparms), 0)
        repo.refresh()
        self.assertFalse(repo.status)
        inst.refresh()
        self.assertEqual(inst.EnabledState, ENABLED_STATE_DISABLED)
        self.assertEqual(inst.HealthState, HEALTH_STATE_OK)
        self.assertEqual(inst.OperationalStatus, [OPERATIONAL_STATUS_OK])
        self.assertEqual(inst.PrimaryStatus, PRIMARY_STATUS_OK)

    @enable_lmi_exceptions
    def test_disable_unexisting_repository(self):
        """
        Test disable unexisting repository.

        Enable DUMMY_TEST_REPO repository, get its instance, delete it
        from /etc/yum.repos.d and try to disable it by OpenLMI.
        Cases:
            OpenLMI won't disable repository - returns error in LMIReturnValue
        """
        repo_file_path = os.path.join(self.yum_repos_dir,
                DUMMY_TEST_REPO + ".repo")
        shutil.copy2(
                "%s/%s.repo" % (
                    os.path.dirname(swbase.__file__),
                    DUMMY_TEST_REPO),
                repo_file_path)
        try:
            repository.set_repos_enabled(DUMMY_TEST_REPO, True)
            subprocess.call(["/usr/bin/yum-config-manager",
                "--enable", DUMMY_TEST_REPO],
                stdout=util.DEV_NULL, stderr=util.DEV_NULL)
            repo = self.cim_class.first_instance_name(
                    {"Name": DUMMY_TEST_REPO}).to_instance()
            self.assertNotEqual(repo, None, "dummy repository exists")

            os.remove(repo_file_path)

            self.assertRaisesCIM(pywbem.CIM_ERR_NOT_FOUND,
                    repo.RequestStateChange,
                    RequestedState=REQUESTED_STATE_DISABLED)
        finally:
            if os.path.exists(repo_file_path):
                os.remove(repo_file_path)

    @enable_lmi_exceptions
    def test_enable_unexisting_repository(self):
        """
        Test enable deleted repository.

        Get DUMMY_TEST_REPO's instance, delete it from /etc/yum.repos.d
        and try to enable it by OpenLMI.
        Cases:
            OpenLMI won't enable repository - returns error in LMIReturnValue
        """
        repo_file_path = os.path.join(self.yum_repos_dir,
                DUMMY_TEST_REPO + ".repo")
        shutil.copy2(
                "%s/%s.repo" % (
                    os.path.dirname(swbase.__file__),
                    DUMMY_TEST_REPO),
                repo_file_path)
        try:

            repo = self.cim_class.first_instance_name(
                    {"Name": DUMMY_TEST_REPO}).to_instance()

            os.remove(repo_file_path)

            self.assertRaisesCIM(pywbem.CIM_ERR_NOT_FOUND,
                    repo.RequestStateChange,
                    RequestedState=REQUESTED_STATE_ENABLED)
        finally:
            if os.path.exists(repo_file_path):
                os.remove(repo_file_path)

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

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