Statistics
| Branch: | Tag: | Revision:

root / test / py / qa.qa_config_unittest.py @ 8a96c5a6

History | View | Annotate | Download (6.5 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2012, 2013 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
import tempfile
26
import shutil
27
import os
28

    
29
from ganeti import utils
30
from ganeti import serializer
31
from ganeti import constants
32
from ganeti import compat
33

    
34
from qa import qa_config
35
from qa import qa_error
36

    
37
import testutils
38

    
39

    
40
class TestTestEnabled(unittest.TestCase):
41
  def testSimple(self):
42
    for name in ["test", ["foobar"], ["a", "b"]]:
43
      self.assertTrue(qa_config.TestEnabled(name, _cfg={}))
44

    
45
    for default in [False, True]:
46
      self.assertFalse(qa_config.TestEnabled("foo", _cfg={
47
        "tests": {
48
          "default": default,
49
          "foo": False,
50
          },
51
        }))
52

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

    
60
  def testEitherWithDefault(self):
61
    names = qa_config.Either("one")
62

    
63
    self.assertTrue(qa_config.TestEnabled(names, _cfg={
64
      "tests": {
65
        "default": True,
66
        },
67
      }))
68

    
69
    self.assertFalse(qa_config.TestEnabled(names, _cfg={
70
      "tests": {
71
        "default": False,
72
        },
73
      }))
74

    
75
  def testEither(self):
76
    names = [qa_config.Either(["one", "two"]),
77
             qa_config.Either("foo"),
78
             "hello",
79
             ["bar", "baz"]]
80

    
81
    self.assertTrue(qa_config.TestEnabled(names, _cfg={
82
      "tests": {
83
        "default": True,
84
        },
85
      }))
86

    
87
    self.assertFalse(qa_config.TestEnabled(names, _cfg={
88
      "tests": {
89
        "default": False,
90
        },
91
      }))
92

    
93
    for name in ["foo", "bar", "baz", "hello"]:
94
      self.assertFalse(qa_config.TestEnabled(names, _cfg={
95
        "tests": {
96
          "default": True,
97
          name: False,
98
          },
99
        }))
100

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

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

    
117
    self.assertFalse(qa_config.TestEnabled(names, _cfg={
118
      "tests": {
119
        "default": True,
120
        "one": True,
121
        "two": True,
122
        "foo": False,
123
        },
124
      }))
125

    
126
  def testEitherNestedWithAnd(self):
127
    names = qa_config.Either([["one", "two"], "foo"])
128

    
129
    self.assertTrue(qa_config.TestEnabled(names, _cfg={
130
      "tests": {
131
        "default": True,
132
        },
133
      }))
134

    
135
    for name in ["one", "two"]:
136
      self.assertFalse(qa_config.TestEnabled(names, _cfg={
137
        "tests": {
138
          "default": True,
139
          "foo": False,
140
          name: False,
141
          },
142
        }))
143

    
144

    
145
class TestQaConfigLoad(unittest.TestCase):
146
  def setUp(self):
147
    self.tmpdir = tempfile.mkdtemp()
148

    
149
  def tearDown(self):
150
    shutil.rmtree(self.tmpdir)
151

    
152
  def testLoadNonExistent(self):
153
    filename = utils.PathJoin(self.tmpdir, "does.not.exist")
154
    self.assertRaises(EnvironmentError, qa_config._QaConfig.Load, filename)
155

    
156
  @staticmethod
157
  def _WriteConfig(filename, data):
158
    utils.WriteFile(filename, data=serializer.DumpJson(data))
159

    
160
  def _CheckLoadError(self, filename, data, expected):
161
    self._WriteConfig(filename, data)
162

    
163
    try:
164
      qa_config._QaConfig.Load(filename)
165
    except qa_error.Error, err:
166
      self.assertTrue(str(err).startswith(expected))
167
    else:
168
      self.fail("Exception was not raised")
169

    
170
  def testFailsValidation(self):
171
    filename = utils.PathJoin(self.tmpdir, "qa.json")
172
    testconfig = {}
173

    
174
    check_fn = compat.partial(self._CheckLoadError, filename, testconfig)
175

    
176
    # No nodes
177
    check_fn("Need at least one node")
178

    
179
    testconfig["nodes"] = [
180
      {
181
        "primary": "xen-test-0",
182
        "secondary": "192.0.2.1",
183
        },
184
      ]
185

    
186
    # No instances
187
    check_fn("Need at least one instance")
188

    
189
    testconfig["instances"] = [
190
      {
191
        "name": "xen-test-inst1",
192
        },
193
      ]
194

    
195
    # Missing "disk" and "disk-growth"
196
    check_fn("Config options 'disk' and 'disk-growth' ")
197

    
198
    testconfig["disk"] = []
199
    testconfig["disk-growth"] = testconfig["disk"]
200

    
201
    # Minimal accepted configuration
202
    self._WriteConfig(filename, testconfig)
203
    result = qa_config._QaConfig.Load(filename)
204
    self.assertTrue(result.get("nodes"))
205

    
206
    # Non-existent instance check script
207
    testconfig[qa_config._INSTANCE_CHECK_KEY] = \
208
      utils.PathJoin(self.tmpdir, "instcheck")
209
    check_fn("Can't find instance check script")
210
    del testconfig[qa_config._INSTANCE_CHECK_KEY]
211

    
212
    # No enabled hypervisor
213
    testconfig[qa_config._ENABLED_HV_KEY] = None
214
    check_fn("No hypervisor is enabled")
215

    
216
    # Unknown hypervisor
217
    testconfig[qa_config._ENABLED_HV_KEY] = ["#unknownhv#"]
218
    check_fn("Unknown hypervisor(s) enabled:")
219

    
220

    
221
class TestQaConfigWithSampleConfig(unittest.TestCase):
222
  """Tests using C{qa-sample.json}.
223

224
  This test case serves two purposes:
225

226
    - Ensure shipped C{qa-sample.json} file is considered a valid QA
227
      configuration
228
    - Test some functions of L{qa_config._QaConfig} without having to
229
      mock a whole configuration file
230

231
  """
232
  def setUp(self):
233
    filename = "%s/qa/qa-sample.json" % testutils.GetSourceDir()
234

    
235
    self.config = qa_config._QaConfig.Load(filename)
236

    
237
  def testGetEnabledHypervisors(self):
238
    self.assertEqual(self.config.GetEnabledHypervisors(),
239
                     [constants.DEFAULT_ENABLED_HYPERVISOR])
240

    
241
  def testGetDefaultHypervisor(self):
242
    self.assertEqual(self.config.GetDefaultHypervisor(),
243
                     constants.DEFAULT_ENABLED_HYPERVISOR)
244

    
245
  def testGetInstanceCheckScript(self):
246
    self.assertTrue(self.config.GetInstanceCheckScript() is None)
247

    
248
  def testGetAndGetItem(self):
249
    self.assertEqual(self.config["nodes"], self.config.get("nodes"))
250

    
251
  def testGetMasterNode(self):
252
    self.assertEqual(self.config.GetMasterNode(), self.config["nodes"][0])
253

    
254

    
255
if __name__ == "__main__":
256
  testutils.GanetiTestProgram()