summaryrefslogtreecommitdiffstats
path: root/src/software/test/test_hosted_software_identity_resource.py
blob: af7b491718842e557c1a43aeb2b345f7d45db687 (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
#!/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_HostedSoftwareIdentityResource provider.
"""

import pywbem
import socket
import unittest

import base

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

    CLASS_NAME = "LMI_HostedSoftwareIdentityResource"
    KEYS = ("Antecedent", "Dependent")

    @classmethod
    def needs_pkgdb(cls):
        return False

    @classmethod
    def needs_repodb(cls):
        return True

    def make_op(self, repo):
        """
        @return object path of HostedSoftwareIdentityResource association
        """
        objpath = self.objpath.copy()
        objpath["Antecedent"] = pywbem.CIMInstanceName(
                classname="Linux_ComputerSystem",
                namespace="root/cimv2",
                keybindings=pywbem.NocaseDict({
                    "CreationClassName" : "Linux_ComputerSystem",
                    "Name" : socket.gethostname()
                }))
        objpath["Dependent"] = pywbem.CIMInstanceName(
                classname="LMI_SoftwareIdentityResource",
                namespace="root/cimv2",
                keybindings=pywbem.NocaseDict({
                    "CreationClassName" : "LMI_SoftwareIdentityResource",
                    "SystemCreationClassName" : "Linux_ComputerSystem",
                    "SystemName" : socket.gethostname(),
                    "Name" : repo.repoid
                }))
        return objpath

    def test_get_instance(self):
        """
        Tests GetInstance call on repositories from our rpm cache.
        """
        for repo in self.repodb:
            objpath = self.make_op(repo)
            inst = self.conn.GetInstance(InstanceName=objpath)
            self.assertEqual(objpath, inst.path,
                    "Object paths should match for repo %s" % repo.repoid)
            for key in self.KEYS:
                self.assertTrue(inst.properties.has_key(key),
                    'OP is missing \"%s\" key for repo "%s".' % (key, repo.repoid))
                self.assertEqual(inst[key], inst.path[key],
                        'Key property "%s" does not match for repo "%s"!' %
                        (key, repo.repoid))

    def test_enum_instance_names(self):
        """
        Tests EnumInstanceNames call on repositories from our cache.
        """
        inames = self.conn.EnumerateInstanceNames(ClassName=self.CLASS_NAME)
        repos = { r.repoid: r for r in self.repodb }
        self.assertEqual(len(repos), len(inames))
        for iname in inames:
            self.assertIn('Dependent', iname)
            self.assertIn('Name', iname['Dependent'])
            self.assertIn(iname['Dependent']['Name'], repos)
            objpath = self.make_op(repos[iname['Dependent']['Name']])
            self.assertEqual(objpath, iname)

    def test_enum_instances(self):
        """
        Tests EnumInstanceNames call on repositories from our cache.
        """
        inames = self.conn.EnumerateInstances(ClassName=self.CLASS_NAME)
        repos = { r.repoid: r for r in self.repodb }
        self.assertEqual(len(repos), len(inames))
        for inst in inames:
            self.assertIn('Dependent', inst)
            self.assertIn('Name', inst['Dependent'])
            repoid = inst['Dependent']['Name']
            self.assertIn(repoid, repos)
            objpath = self.make_op(repos[repoid])
            self.assertEqual(objpath, inst.path)
            for key in self.KEYS:
                self.assertTrue(inst.properties.has_key(key),
                    'OP is missing \"%s\" key for repo "%s".' % (key, repoid))
                self.assertEqual(inst[key], inst.path[key],
                        'Key property "%s" does not match for repo "%s"!' %
                        (key, repoid))

    def test_get_antecedent_referents(self):
        """
        Test ReferenceNames for ComputerSystem.
        """
        if not self.repodb:
            return
        repo = self.repodb[0]
        objpath = self.make_op(repo)
        refs = self.conn.AssociatorNames(
                Role="Antecedent",
                ObjectName=objpath["Antecedent"],
                ResultRole="Dependent",
                ResultClass="LMI_SoftwareIdentityResource")
        repos = {r.repoid: r for r in self.repodb}
        for ref in refs:
            self.assertIsInstance(ref, pywbem.CIMInstanceName)
            self.assertEqual(ref.namespace, 'root/cimv2')
            self.assertEqual(ref.classname, "LMI_SoftwareIdentityResource")
            self.assertIn("Name", ref.keys())
            objpath = self.make_op(repos[ref["Name"]])
            self.assertEqual(objpath["Dependent"], ref)
            del repos[ref["Name"]]
        self.assertEqual(0, len(repos))

    def test_get_dependent_referents(self):
        """
        Test ReferenceNames for repository.
        """
        for repo in self.repodb:
            objpath = self.make_op(repo=repo)
            refs = self.conn.AssociatorNames(
                    Role="Dependent",
                    ObjectName=objpath["Dependent"],
                    ResultRole="Antecedent",
                    ResultClass="Linux_ComputerSystem")
            self.assertEqual(len(refs), 1)
            self.assertEqual(objpath["Antecedent"], refs[0])

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

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