Revision a0c3e726

b/Makefile.am
498 498
	tools/ensure-dirs
499 499

  
500 500
qa_scripts = \
501
	qa/__init__.py \
501 502
	qa/ganeti-qa.py \
502 503
	qa/qa_cluster.py \
503 504
	qa/qa_config.py \
......
847 848
	test/ganeti.utils.x509_unittest.py \
848 849
	test/ganeti.utils_unittest.py \
849 850
	test/ganeti.workerpool_unittest.py \
851
	test/qa.qa_config_unittest.py \
850 852
	test/cfgupgrade_unittest.py \
851 853
	test/docs_unittest.py \
852 854
	test/pycurl_reset_unittest.py \
b/autotools/run-in-tempdir
10 10

  
11 11
mkdir $tmpdir/doc
12 12

  
13
cp -r autotools daemons scripts lib tools test $tmpdir
13
cp -r autotools daemons scripts lib tools test qa $tmpdir
14 14
cp -r doc/examples $tmpdir/doc
15 15

  
16 16
mv $tmpdir/lib $tmpdir/ganeti
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():
b/test/qa.qa_config_unittest.py
1
#!/usr/bin/python
2
#
3

  
4
# Copyright (C) 2012 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
# 02110-1301, USA.
20

  
21

  
22
"""Script for testing qa.qa_config"""
23

  
24
import unittest
25

  
26
from qa import qa_config
27

  
28
import testutils
29

  
30

  
31
class TestTestEnabled(unittest.TestCase):
32
  def testSimple(self):
33
    for name in ["test", ["foobar"], ["a", "b"]]:
34
      self.assertTrue(qa_config.TestEnabled(name, _cfg={}))
35

  
36
    for default in [False, True]:
37
      self.assertFalse(qa_config.TestEnabled("foo", _cfg={
38
        "tests": {
39
          "default": default,
40
          "foo": False,
41
          },
42
        }))
43

  
44
      self.assertTrue(qa_config.TestEnabled("bar", _cfg={
45
        "tests": {
46
          "default": default,
47
          "bar": True,
48
          },
49
        }))
50

  
51
  def testEitherWithDefault(self):
52
    names = qa_config.Either("one")
53

  
54
    self.assertTrue(qa_config.TestEnabled(names, _cfg={
55
      "tests": {
56
        "default": True,
57
        },
58
      }))
59

  
60
    self.assertFalse(qa_config.TestEnabled(names, _cfg={
61
      "tests": {
62
        "default": False,
63
        },
64
      }))
65

  
66
  def testEither(self):
67
    names = [qa_config.Either(["one", "two"]),
68
             qa_config.Either("foo"),
69
             "hello",
70
             ["bar", "baz"]]
71

  
72
    self.assertTrue(qa_config.TestEnabled(names, _cfg={
73
      "tests": {
74
        "default": True,
75
        },
76
      }))
77

  
78
    self.assertFalse(qa_config.TestEnabled(names, _cfg={
79
      "tests": {
80
        "default": False,
81
        },
82
      }))
83

  
84
    for name in ["foo", "bar", "baz", "hello"]:
85
      self.assertFalse(qa_config.TestEnabled(names, _cfg={
86
        "tests": {
87
          "default": True,
88
          name: False,
89
          },
90
        }))
91

  
92
    self.assertFalse(qa_config.TestEnabled(names, _cfg={
93
      "tests": {
94
        "default": True,
95
        "one": False,
96
        "two": False,
97
        },
98
      }))
99

  
100
    self.assertTrue(qa_config.TestEnabled(names, _cfg={
101
      "tests": {
102
        "default": True,
103
        "one": False,
104
        "two": True,
105
        },
106
      }))
107

  
108
    self.assertFalse(qa_config.TestEnabled(names, _cfg={
109
      "tests": {
110
        "default": True,
111
        "one": True,
112
        "two": True,
113
        "foo": False,
114
        },
115
      }))
116

  
117
  def testEitherNestedWithAnd(self):
118
    names = qa_config.Either([["one", "two"], "foo"])
119

  
120
    self.assertTrue(qa_config.TestEnabled(names, _cfg={
121
      "tests": {
122
        "default": True,
123
        },
124
      }))
125

  
126
    for name in ["one", "two"]:
127
      self.assertFalse(qa_config.TestEnabled(names, _cfg={
128
        "tests": {
129
          "default": True,
130
          "foo": False,
131
          name: False,
132
          },
133
        }))
134

  
135

  
136
if __name__ == "__main__":
137
  testutils.GanetiTestProgram()

Also available in: Unified diff