Revision a0c3e726 qa/qa_config.py

b/qa/qa_config.py
60 60
  return cfg.get(name, default)
61 61

  
62 62

  
63
def TestEnabled(tests):
63
class Either:
64
  def __init__(self, tests):
65
    """Initializes this class.
66

  
67
    @type tests: list or string
68
    @param tests: List of test names
69
    @see: L{TestEnabled} for details
70

  
71
    """
72
    self.tests = tests
73

  
74

  
75
def _MakeSequence(value):
76
  """Make sequence of single argument.
77

  
78
  If the single argument is not already a list or tuple, a list with the
79
  argument as a single item is returned.
80

  
81
  """
82
  if isinstance(value, (list, tuple)):
83
    return value
84
  else:
85
    return [value]
86

  
87

  
88
def _TestEnabledInner(check_fn, names, fn):
89
  """Evaluate test conditions.
90

  
91
  @type check_fn: callable
92
  @param check_fn: Callback to check whether a test is enabled
93
  @type names: sequence or string
94
  @param names: Test name(s)
95
  @type fn: callable
96
  @param fn: Aggregation function
97
  @rtype: bool
98
  @return: Whether test is enabled
99

  
100
  """
101
  names = _MakeSequence(names)
102

  
103
  result = []
104

  
105
  for name in names:
106
    if isinstance(name, Either):
107
      value = _TestEnabledInner(check_fn, name.tests, compat.any)
108
    elif isinstance(name, (list, tuple)):
109
      value = _TestEnabledInner(check_fn, name, compat.all)
110
    else:
111
      value = check_fn(name)
112

  
113
    result.append(value)
114

  
115
  return fn(result)
116

  
117

  
118
def TestEnabled(tests, _cfg=None):
64 119
  """Returns True if the given tests are enabled.
65 120

  
66
  @param tests: a single test, or a list of tests to check
121
  @param tests: A single test as a string, or a list of tests to check; can
122
    contain L{Either} for OR conditions, AND is default
67 123

  
68 124
  """
69
  if isinstance(tests, basestring):
70
    tests = [tests]
125
  if _cfg is None:
126
    _cfg = cfg
71 127

  
72 128
  # Get settings for all tests
73
  all_tests = cfg.get("tests", {})
129
  cfg_tests = _cfg.get("tests", {})
74 130

  
75 131
  # Get default setting
76
  default = all_tests.get("default", True)
132
  default = cfg_tests.get("default", True)
77 133

  
78
  return compat.all(all_tests.get(name, default) for name in tests)
134
  return _TestEnabledInner(lambda name: cfg_tests.get(name, default),
135
                           tests, compat.all)
79 136

  
80 137

  
81 138
def GetMasterNode():

Also available in: Unified diff