summaryrefslogtreecommitdiffstats
path: root/src/python/lmi/test/util.py
blob: 699507c2340e9a50ff22ce08b67fe3e0a3f9eef8 (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
# 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: Michal Minar <miminar@redhat.com>
#
"""
LMI test utilities.
"""

import os
import hashlib
import pywbem
import random
import tempfile
import socket
import string
import subprocess
from collections import OrderedDict
from lmi.test import unittest

def is_this_system(system_name):
    """
    :returns: Whether the given *system_name* matches the hostname of currently
    running system.
    :rtype: boolean
    """
    return (  socket.gethostbyaddr(system_name)[0]
           == socket.gethostbyaddr(socket.gethostname())[0])

def get_environvar(variable, default='', convert=str):
    """
    Get the value of environment variable.

    :param string variable: Name of environment variable.
    :param default: Any value that should be returned when the variable is not
        set. If None, the conversion won't be done.
    :param callable convert: Function transforming value to something else.
    :returns: Converted value of the environment variable.
    """
    val = os.environ.get(variable, default)
    if convert is bool:
        return val.lower() in ('true', 'yes', 'on', '1')
    if val is None:
        return None
    return convert(val)

def mark_dangerous(method):
    """
    Decorator for methods of :py:class:`unittest.TestCase` subclasses that
    skips dangerous tests if an environment variable says so.
    ``LMI_RUN_DANGEROUS`` is the environment variabled read.

    These tests will be skipped by default.
    """
    if get_environvar('LMI_RUN_DANGEROUS', '0', bool):
        return method
    else:
        return unittest.skip("This test is marked as dangerous.")(method)

def mark_tedious(method):
    """
    Decorator for methods of :py:class:`unittest.TestCase` subclasses that
    skips tedious tests. Those running for very long time and usually need a
    lot of memory. They are run by default. Environment variable
    ``LMI_RUN_TEDIOUS`` can be used to skip them.
    """
    if get_environvar('LMI_RUN_TEDIOUS', '1', bool):
        return method
    else:
        return unittest.skip("This test is marked as tedious.")(method)

def check_inames_equal(fst, snd):
    """
    Compare two objects of :py:class:`pywbem.CIMInstanceName`. Their ``host``
    property is not checked. Be benevolent when checking names system
    creation class names.

    :returns: ``True`` if both instance names are equal.
    :rtype: boolean
    """
    if not isinstance(fst, pywbem.CIMInstanceName):
        raise TypeError("fst argument must be a pywbem.CIMInstanceName, not %s"
                % repr(fst))
    if not isinstance(snd, pywbem.CIMInstanceName):
        raise TypeError("snd argument must be a pywbem.CIMInstanceName, not %s"
                % repr(snd))
    if fst.classname != snd.classname or fst.namespace != snd.namespace:
        return False

    snd_keys = dict((k, v) for (k, v) in snd.keybindings.iteritems())
    for key, value in fst.keybindings.iteritems():
        if key not in snd_keys:
            return False
        snd_value = snd_keys.pop(key)
        if (   isinstance(value, pywbem.CIMInstanceName)
           and isinstance(snd_value, pywbem.CIMInstanceName)):
            if not check_inames_equal(value, snd_value):
                return False

        # accept also aliases in the Name attribute of ComputerSystem
        elif (  (   fst.classname.endswith('_ComputerSystem')
                and key.lower() == 'name')
             or (   key.lower() == 'systemname'
                and 'SystemCreationClassName' in fst)):
            if (  value != snd_value
               and (  not is_this_system(value)
                   or not is_this_system(snd_value))):
                return False

        elif (   fst.classname.endswith('_ComputerSystem')
             and key.lower() == 'creationclassname'):
            if (   value != snd_value
               and 'CIM_ComputerSystem' not in [
                   p['CreationClassName'] for p in (fst, snd)]):
                return False

        elif isinstance(value, basestring) \
                and isinstance(snd_value, basestring):
            value = value.decode('utf-8') if isinstance(value, str) else value
            snd_value = (  snd_value.decode('utf-8')
                        if isinstance(snd_value, str) else snd_value)
            if value != snd_value:
                return False

        elif value != snd_value:
            return False

    if snd_keys:    # second path has more key properties than first one
        return False

    return True

def random_string(strength=6, chars=None, prefix=""):
    """
    Generate a random string, e.g. usable as UID/GID

    strength is count of random characters in the final string.  chars
    is sequence of characters to choose from, and prefix can be provided
    to prepend it to final string.
    """
    if chars is None:
        chars = string.ascii_uppercase + string.digits
    salt = ''.join([random.choice(chars) for x in range(strength)])
    return prefix + salt


class BackupStorage():
    """
    Simple file backup storage.

    * Only supports files.
    * Only supports absolute paths.
    * Consecutive backups rewrite each other.
    * Does not autodestroy the backup.
    """

    def __init__(self):
        self.root = tempfile.mkdtemp(prefix=self.__class__.__name__ + ".")
        self.backups = OrderedDict()
        subprocess.check_call(["mkdir", "-p", self.root])

    def _copy(self, src, dest):
        """
        Copy src to dst --- force, keep meta, no questions asked
        """
        subprocess.check_call(["cp", "-a", "-f", src, dest])

    def _get_bpath(self, path):
        """
        Take original path and return path to backup.
        """
        if not path.startswith("/"):
            raise ValueError("only absolute paths are supported")
        digest = hashlib.sha1(path).hexdigest()
        return self.root + "/" + digest

    def _update_index(self):
        """
        Create/update an index file to help in case of backup investigation

        For convenience, index file is sorted by real path.
        """
        paths = sorted(self.backups.keys())
        with open(self.root + "/index", "w+") as fh:
            for path in paths:
                fh.write("%s %s\n" % (self.backups[path], path))

    def add_files(self, paths):
        """
        Add list of tiles to backup storage
        """
        for path in paths:
            self.add_file(path)

    def add_file(self, path):
        """
        Add a file to backup storage
        """
        bpath = self._get_bpath(path)
        self._copy(path, bpath)
        self.backups[path] = bpath
        self._update_index()

    def restore(self, path):
        """
        Restore particular path
        """
        try:
            self._copy(self.backups[path], path)
        except KeyError:
            raise ValueError("path not stored: %s" % path)

    def restore_all(self):
        """
        Restore all stored paths in same order as they were stored
        """
        for key in self.backups.keys():
            self.restore(key)

    def destroy_backup(self):
        """
        Destroy the temporary backup
        """
        subprocess.call(["rm", "-rf", self.root])


class BaseCrippler:
    """
    Helper class for crippling system files.

    To use the class, you need to sub-class it and implement
    _define_cases method.
    """

    LINE_LENGTH = 500
    LINE_COUNT = 50
    BINARY_LENGTH = 10 * 1024 * 1024

    ## virtual
    #

    def _define_cases(self):
        """
        Define cases per file supported

        This function must return a dict with one set of cases per
        file: key is path and value is another dict defining cases
        as pairs of name ([a-zA-Z_]) and content.

        Quick example:

            {
                '/etc/file1': {
                    'case1': "some triggering content",
                    'case2': "some other triggering content",
                    'case3': "some funny triggering content",
                },
                '/etc/file2': {
                    'case1': "some triggering content",
                    'case2': "some other triggering content",
                    'case3': "some funny triggering content",
                },
            }

        Note that trailing newline is added automatically to each content
        string. Also, whether content will be appended or replaced is decided
        by caller of the BaseCrippler.cripple method.
        """
        pass

    ## internal
    #

    def __init__(self):
        self.autocases = {
            'empty': lambda: '',
            'random_line': self._random_line,
            'random_lines': self._random_lines,
            'random_binary': self._random_binary,
        }
        self.cases = self._define_cases()

    def _append_to(self, path, content):
        with open(path, 'a+') as fh:
            fh.write(content)

    def _clobber(self, path, content):
        with open(path, 'w+') as fh:
            fh.write(content)

    def _random_binary(self, size=BINARY_LENGTH):
        chars = ''.join([chr(i) for i in xrange(256)])
        return random_string(strength=size, chars=chars)

    def _random_line(self, size=LINE_LENGTH):
        chars = string.letters + string.punctuation + " \t"
        return random_string(strength=size, chars=chars) + "\n"

    def _random_lines(self, size=LINE_LENGTH, count=LINE_COUNT):
        return "".join([self._random_line(size) for i in xrange(count)])

    def _get_content(self, path, case):

        try:
            content = self.autocases[case]()
        except KeyError:
            try:
                content = self.cases[path][case] + "\n"
            except KeyError:
                raise ValueError("unknown case: %s for: %s" % (case, path))
        return content

    ## public
    #

    def all_cases_for(self, path):
        """
        Return list of cases available for path
        """
        return self.cases[path].keys() + self.autocases.keys()

    def all_paths(self):
        """
        Return list of paths served by this implementation
        """
        return self.cases.keys()

    def cripple(self, path, case, op="replace"):
        """
        Cripple file according to selected case.

        op is either "append" or "replace" and means that the content will
        be appended to the file, otherwise it will replace it.
        """
        if op == 'replace':
            self._clobber(path, self._get_content(path, case))
        elif op == 'append':
            self._append_to(path, self._get_content(path, case))
        else:
            raise ValueError("unknown op: %s" % op)