summaryrefslogtreecommitdiffstats
path: root/src/python/lmi/test/cases/test_lmi_instances.py
blob: 02e3e6ce50289de8e9c1b41c88db6efeb30dacf4 (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
# OpenLMI tests
#
# Copyright (C) 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: Jan Safranek <jsafrane@redhat.com>
#

from lmi.test.lmibase import LmiTestCase
import lmi.shell
import unittest
import pywbem
import traceback
import sys

class TestInstances(LmiTestCase):
    """
    Test that EnumerateInstances, EnumerateInstanceNames, GetInstances,
    Associators and References works on all LMI_* classes.

    It does not check actual content of the instances, just that the above
    methods do not crash.
    """
    SKIP_CLASSES = [
            # Enumeration is not implemented (and never will be):
            "LMI_SoftwareIdentityFileCheck",
            "LMI_FIFOPipeFile",
            "LMI_DataFile",
            "LMI_FileIdentity",
            "LMI_UnixDirectory",
            "LMI_UnixFile",
            "LMI_UnixDeviceFile",
            "LMI_DirectoryContainsFile",
            "LMI_SymbolicLink",
            "LMI_UnixSocket",
            "LMI_SoftwareInstModification",
            "LMI_StorageInstModification",
            "LMI_SoftwareIdentity",
            "LMI_SoftwareIdentityChecks",
            "LMI_SoftwareInstCreation",
            "LMI_StorageInstCreation",
            "LMI_AccountInstanceDeletionIndication",
            "LMI_NetworkInstCreation",
            "LMI_NetworkInstModification",
            "LMI_NetworkInstDeletion",
            "LMI_JournalLogRecordInstanceCreationIndication",
            "LMI_ServiceInstanceModificationIndication",
            "LMI_AccountInstanceCreationIndication",

            # too much memory needed, TODO?
            "LMI_ResourceForSoftwareIdentity",
            "LMI_MemberOfSoftwareCollection",
            "LMI_SoftwareInstallationServiceAffectsElement",
    ]

    SKIP_ASSOCIATORS = [
            # too many associators(), too much memory needed:
            "LMI_SoftwareIdentityResource",
            "LMI_InstalledSoftwareIdentity",
            "LMI_SoftwareInstallationService",
            "LMI_SystemSoftwareCollection",
    ]

    def compare_paths(self, instance_names, instances):
        """
        Compare list of LMIInstanceNames and LMIInstances.
        Raise assertion error if these two lists do not represent the same
        CIM instances.
        """
        # Check every instance has appropriate instance_name
        for i in instances:
            path = i.path
            self.assertCIMNameIn(path, instance_names)

        # The other way around - check every instance_name has appropriate
        # instance
        instances_paths = [i.path for i in instances]
        for i in instance_names:
            self.assertCIMNameIn(i, instances_paths)

        # Check that there are no duplicates in instance_names
        # (this automatically ensures there are no duplicates in instances)
        inames_set = set(instance_names)
        self.assertEqual(len(inames_set), len(instance_names))

        # check, that instance key properties match their counterparts in path
        for i in instances:
            for key in i.path.key_properties():
                self.assertEqual(getattr(i, key), getattr(i.path, key))

    def check_instances(self, cls, skip_associators):
        """
        Check that EnumerateInstances and EnumerateInstanceNames work and
        return the same objects.
        """
        instances = cls.instances()
        instance_names = cls.instance_names()

        # Check that EnumerateInstances and EnumerateInstanceNames returns the
        # same instances
        self.compare_paths(instance_names, instances)

        # try GetInstance on each name, just to check it works
        for i in instance_names:
            i.to_instance()

        if skip_associators:
            return

        element_class = self.conn.root.cimv2.CIM_ManagedElement
        for instance in instances:
            if not lmi.shell.LMIUtil.lmi_isinstance(instance, element_class):
                # skip this class, it cannot have associations
                return
            # call Associators & AssociatorNames
            associators = instance.associators()
            associator_names = instance.associator_names()

            # Check that Associators and AssociatorNames returns the
            # same instances
            self.compare_paths(associator_names, associators)

            # call References & ReferenceNames
            references = instance.references()
            reference_names = instance.reference_names()

            # Check that References and ReferenceNames returns the
            # same instances
            self.compare_paths(reference_names, references)


    def test_all(self):
        """
        Enumerate all classes and start tests for all LMI_ ones.
        """
        lmi.shell.LMIUtil.lmi_set_use_exceptions(True)
        classes = self.conn.root.cimv2.classes()
        # do not stop on the first exception, scan all classes
        exception_count = 0

        for name in classes:
            try:
                if not name.startswith("LMI_"):
                    continue
                if name in self.SKIP_CLASSES:
                    print >>sys.stderr, "Skipping", name
                    continue
                skip_associators = name in self.SKIP_ASSOCIATORS
                print >>sys.stderr, "Checking ", name
                cls = lmi.shell.LMIClass(self.conn, self.conn.root.cimv2, name)
                self.check_instances(cls, skip_associators)
            except Exception, exc:
                print >>sys.stderr, "ERROR scanning class", cls, exc
                traceback.print_exc()
                exception_count += 1

        self.assertEqual(exception_count, 0)

if __name__ == '__main__':
    # Run the standard unittest.main() function to load and run all tests
    unittest.main()