summaryrefslogtreecommitdiffstats
path: root/src/python/lmi/test/base.py
blob: a4640f3eab96a435435c7b3e24de6fd6beaa98a5 (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
# 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: Jan Safranek <jsafrane@redhat.com>
# Authors: Michal Minar <miminar@redhat.com>
# Authors: Roman Rakus <rrakus@redhat.com>
#
"""
Base classes for *OpenLMI Provider* test cases.
"""

import os
import pywbem
import socket
import unittest

from lmi.test import util

def render_iname(iname, indent=2):
    """
    Render object path in human readable way. Result will occupy multiple
    lines. The first line is a class name, which is not indented at all. Other
    lines will be indented with *indent* spaces.

    :param iname: Object path to render.
    :type iname: :py:class:`pywbem.CIMInstanceName`
    :param integer ident: Number of spaces prefixing all lines but the first.
    :returns: *iname* nicely rendered.
    :rtype: string
    """
    if not isinstance(iname, pywbem.CIMInstanceName):
        return repr(iname)
    lines = [ "%s" % iname.classname
            , " "*indent + "namespace: %s" % iname.namespace
            , " "*indent + "keys:"]
    align = max([len(k) for k in iname.keybindings.iterkeys()])
    for key, value in iname.keybindings.iteritems():
        if isinstance(value, pywbem.CIMInstanceName):
            value = render_iname(value, indent + 4)
        lines.append(" "*indent + ("  %%-%ds : %%s" % align) % (key, value))
    return "\n".join(lines)

class BaseLmiTestCase(unittest.TestCase):
    """
    Base class for all LMI test cases.
    """

    #: Value used in ``SystemName`` key properties in various *CIM* instances.
    #: It's also used to fill ``CIM_ComputerySystem.Name`` property.
    SYSTEM_NAME = socket.gethostname()

    @classmethod
    def setUpClass(cls):
        #: Cached value of SystemCreationClassName set with
        #: ``LMI_CS_CLASSNAME`` environment variable.
        cls.system_cs_name = os.environ.get(
                "LMI_CS_CLASSNAME", "PG_ComputerSystem")
        #: *URL* of *CIMOM* we connect to. Overriden with ``LMI_CIMOM_URL``
        #: environment variable.
        cls.url = os.environ.get("LMI_CIMOM_URL", "https://localhost:5989")
        #: User name for authentication with *CIMOM*. Overriden with
        #: ``LMI_CIMOM_USERNAME`` variable.
        cls.username = os.environ.get("LMI_CIMOM_USERNAME", "root")
        #: User's password for authentication with *CIMOM*. Overriden with
        #: ``LMI_CIMOM_PASSWORD`` environment variable.
        cls.password = os.environ.get("LMI_CIMOM_PASSWORD", "")
        #: Name of *CIMOM* we connect to. There are two possible values:
        #: ``"tog-pegasus"`` and ``"sblim-sfcb"``. Overriden with
        #: ``LMI_CIMOM_BROKER`` environment variable.
        cls.cimom = os.environ.get("LMI_CIMOM_BROKER", "tog-pegasus")
        #: Boolean value saying whether to run dangerous tests. These are marked
        #: with :py:func:`mark_dangerous` decorator. This is set with
        #: ``LMI_RUN_DANGEROUS`` environment variable.
        cls.run_dangerous = util.get_environvar('LMI_RUN_DANGEROUS', '0', bool)
        #: Boolean value saying whether to run tedious tests. These are marked
        #: with :py:func:`mark_tedious` decorator. This is set with
        #: ``LMI_RUN_TEDIOUS`` environment variable.
        cls.run_tedious = util.get_environvar('LMI_RUN_TEDIOUS', '1', bool)

    def assertRaisesCIM(self, cim_err_code, func, *args, **kwds):
        """
        This test passes if given function called with supplied arguments
        raises :py:class:`pywbem.CIMError` with given cim error code.
        """
        with self.assertRaises(pywbem.CIMError) as cm:
            func(*args, **kwds)
        self.assertEqual(cim_err_code, cm.exception.args[0])

    def assertCIMNameEqual(self, fst, snd, msg=None):
        """
        Compare two objects of :py:class:`pywbem.CIMInstanceName`. Their host
        properties are not checked.
        """
        if msg is None:
            msg = ( "%s\n\nis not equal to: %s"
                  % (render_iname(fst), render_iname(snd)))
        self.assertTrue(util.check_inames_equal(fst, snd), msg)

    def assertCIMNameIn(self, name, candidates):
        """
        Checks that given :py:class:`pywbem.CIMInstanceName` is present in
        set of candidates. It compares all properties but ``host``.
        """
        for candidate in candidates:
            if util.check_inames_equal(name, candidate):
                return
        self.assertTrue(False, 'name "%s" is not in candidates' % str(name))

    def assertNocaseDictEqual(self, fst, snd, msg=None):
        """
        Compare two no-case dictionaries ignoring the case of their keys.
        """
        fst_dict = {}
        for (key, value) in fst.iteritems():
            fst_dict[key.lower()] = value
        snd_dict = {}
        for (key, value) in snd.iteritems():
            snd_dict[key.lower()] = value
        self.assertEqual(fst_dict, snd_dict, msg)