summaryrefslogtreecommitdiffstats
path: root/src/software/test/test_software_identity_checks.py
blob: 9ebd7ffd446a5f5feda6c80d7d8126e49611ca64 (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
#!/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_SoftwareIdentityChecks provider.
"""

import pywbem
import socket
import unittest

import base
import rpmcache

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

    CLASS_NAME = "LMI_SoftwareIdentityChecks"
    KEYS = ("Check", "Element")

    @classmethod
    def needs_pkgdb_files(cls):
        return True

    def make_op(self, pkg, filepath, newer=True):
        """
        @return object path of SoftwareIdentityChecks association
        """
        objpath = self.objpath.copy()
        objpath["Check"] = pywbem.CIMInstanceName(
                classname="LMI_SoftwareIdentityFileCheck",
                namespace="root/cimv2",
                keybindings=pywbem.NocaseDict({
                    "CheckID" : 'LMI:LMI_SoftwareIdentityFileCheck',
                    "Name" : filepath,
                    "SoftwareElementID" : pkg.get_nevra(
                            newer=newer, with_epoch="ALWAYS"),
                    "SoftwareElementState" : pywbem.Uint16(2),     #Executable
                    "TargetOperatingSystem" : pywbem.Uint16(36),   #LINUX
                    "Version" : getattr(pkg, 'up_evra' if newer else 'evra')
                }))
        objpath["Element"] = pywbem.CIMInstanceName(
                classname="LMI_SoftwareIdentity",
                namespace="root/cimv2",
                keybindings=pywbem.NocaseDict({
                    "InstanceID" : 'LMI:LMI_SoftwareIdentity:'
                        + pkg.get_nevra(newer=newer, with_epoch="ALWAYS")
                }))
        return objpath

    def test_get_instance(self):
        """
        Tests GetInstance call on packages from our rpm cache.
        """
        for pkg in self.safe_pkgs:
            for filepath in self.pkgdb_files[pkg.name]:
                objpath = self.make_op(pkg, filepath)
                inst = self.conn.GetInstance(InstanceName=objpath)
                self.assertEqual(inst.path, objpath,
                        "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])
                self.assertEqual(objpath, inst.path,
                        "Object paths should match for package %s"%pkg)

    def test_enum_instance_names(self):
        """
        Tests EnumInstances call on identity checks - should not be supported.
        """
        self.assertRaisesCIM(pywbem.CIM_ERR_NOT_SUPPORTED,
                self.conn.EnumerateInstanceNames, ClassName=self.CLASS_NAME)

    def test_get_identity_referents(self):
        """
        Test AssociatorNames for Element(SoftwareIdentity).
        """
        for pkg in self.safe_pkgs:
            files = self.pkgdb_files[pkg.name]
            objpath = self.make_op(pkg, files[0])
            refs = self.conn.AssociatorNames(
                    Role="Element",
                    ObjectName=objpath["Element"],
                    ResultRole="Check",
                    ResultClass="LMI_SoftwareIdentityFileCheck")
            srefs = set(refs)
            for ref in refs:
                self.assertIsInstance(ref, pywbem.CIMInstanceName)
                self.assertEqual(ref.namespace, 'root/cimv2')
                self.assertEqual(ref.classname,
                    "LMI_SoftwareIdentityFileCheck")
                self.assertIn(ref["Name"], set(r["Name"] for r in refs))
                self.assertEqual(sorted(ref.keys()),
                    ["CheckID", "Name", "SoftwareElementID",
                        "SoftwareElementState",
                        "TargetOperatingSystem", "Version"])
                self.assertEqual(
                    pkg.get_nevra(newer=True, with_epoch='ALWAYS'),
                    ref["SoftwareElementID"])

    def test_get_check_referents(self):
        """
        Test AssociatorNames for Check(SoftwareIdentityFileCheck).
        """
        for pkg in self.safe_pkgs:
            files = self.pkgdb_files[pkg.name]
            for filepath in files:
                objpath = self.make_op(pkg, filepath)
                refs = self.conn.AssociatorNames(
                        ObjectName=objpath["Check"],
                        Role="Check",
                        ResultRole="Element",
                        ResultClass="LMI_SoftwareIdentity")
                self.assertEqual(len(refs), 1)
                ref = refs[0]
                self.assertEqual(objpath["Element"], ref)

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

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