Revision 6de7c41d

b/Makefile.am
191 191
	test/data/proc_drbd8.txt
192 192

  
193 193
dist_TESTS = \
194
	test/ganeti.bdev_unittest.py \
195
	test/ganeti.cli_unittest.py \
196
	test/ganeti.cmdlib_unittest.py \
194 197
	test/ganeti.config_unittest.py \
198
	test/ganeti.constants_unittest.py \
195 199
	test/ganeti.hooks_unittest.py \
196
	test/ganeti.utils_unittest.py \
197
	test/ganeti.bdev_unittest.py \
198
	test/ganeti.ssh_unittest.py \
200
	test/ganeti.http_unittest.py \
199 201
	test/ganeti.locking_unittest.py \
200
	test/ganeti.serializer_unittest.py \
201
	test/ganeti.workerpool_unittest.py \
202 202
	test/ganeti.rapi.resources_unittest.py \
203
	test/ganeti.http_unittest.py \
204
	test/ganeti.constants_unittest.py \
205
	test/ganeti.cli_unittest.py
203
	test/ganeti.serializer_unittest.py \
204
	test/ganeti.ssh_unittest.py \
205
	test/ganeti.utils_unittest.py \
206
	test/ganeti.workerpool_unittest.py
206 207

  
207 208
nodist_TESTS =
208 209

  
b/lib/cmdlib.py
32 32
import platform
33 33
import logging
34 34
import copy
35
import itertools
35 36

  
36 37
from ganeti import ssh
37 38
from ganeti import utils
......
322 323
  HTYPE = None
323 324

  
324 325

  
326
class _FieldSet(object):
327
  """A simple field set.
328

  
329
  Among the features are:
330
    - checking if a string is among a list of static string or regex objects
331
    - checking if a whole list of string matches
332
    - returning the matching groups from a regex match
333

  
334
  Internally, all fields are held as regular expression objects.
335

  
336
  """
337
  def __init__(self, *items):
338
    self.items = [re.compile("^%s$" % value) for value in items]
339

  
340
  def Extend(self, other_set):
341
    """Extend the field set with the items from another one"""
342
    self.items.extend(other_set.items)
343

  
344
  def Matches(self, field):
345
    """Checks if a field matches the current set
346

  
347
    @type field: str
348
    @param field: the string to match
349
    @return: either False or a regular expression match object
350

  
351
    """
352
    for m in itertools.ifilter(None, (val.match(field) for val in self.items)):
353
      return m
354
    return False
355

  
356
  def NonMatching(self, items):
357
    """Returns the list of fields not matching the current set
358

  
359
    @type items: list
360
    @param items: the list of fields to check
361
    @rtype: list
362
    @return: list of non-matching fields
363

  
364
    """
365
    return [val for val in items if not self.Matches(val)]
366

  
367

  
325 368
def _GetWantedNodes(lu, nodes):
326 369
  """Returns list of checked and expanded node names.
327 370

  
b/test/ganeti.cmdlib_unittest.py
1
#!/usr/bin/python
2
#
3

  
4
# Copyright (C) 2008 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 0.0510-1301, USA.
20

  
21

  
22
"""Script for unittesting the cmdlib module"""
23

  
24

  
25
import os
26
import unittest
27
import time
28
import Queue
29

  
30
from ganeti import cmdlib
31
from ganeti import errors
32

  
33

  
34
class FieldSetTestCase(unittest.TestCase):
35
  """Test case for FieldSets"""
36

  
37
  def testSimpleMatch(self):
38
    f = cmdlib._FieldSet("a", "b", "c", "def")
39
    self.failUnless(f.Matches("a"))
40
    self.failIf(f.Matches("d"), "Substring matched")
41
    self.failIf(f.Matches("defghi"), "Prefix string matched")
42
    self.failIf(f.NonMatching(["b", "c"]))
43
    self.failIf(f.NonMatching(["a", "b", "c", "def"]))
44
    self.failUnless(f.NonMatching(["a", "d"]))
45

  
46
  def testRegexMatch(self):
47
    f = cmdlib._FieldSet("a", "b([0-9]+)", "c")
48
    self.failUnless(f.Matches("b1"))
49
    self.failUnless(f.Matches("b99"))
50
    self.failIf(f.Matches("b/1"))
51
    self.failIf(f.NonMatching(["b12", "c"]))
52
    self.failUnless(f.NonMatching(["a", "1"]))
53

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

Also available in: Unified diff