| 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 |
"""Script for testing qa.qa_config""" |
| 32 |
|
| 33 |
import unittest |
| 34 |
import tempfile |
| 35 |
import shutil |
| 36 |
import os |
| 37 |
|
| 38 |
from ganeti import utils |
| 39 |
from ganeti import serializer |
| 40 |
from ganeti import constants |
| 41 |
from ganeti import compat |
| 42 |
|
| 43 |
from qa import qa_config |
| 44 |
from qa import qa_error |
| 45 |
|
| 46 |
import testutils |
| 47 |
|
| 48 |
|
| 49 |
class TestTestEnabled(unittest.TestCase): |
| 50 |
def testSimple(self): |
| 51 |
for name in ["test", ["foobar"], ["a", "b"]]: |
| 52 |
self.assertTrue(qa_config.TestEnabled(name, _cfg={})) |
| 53 |
|
| 54 |
for default in [False, True]: |
| 55 |
self.assertFalse(qa_config.TestEnabled("foo", _cfg={ |
| 56 |
"tests": { |
| 57 |
"default": default, |
| 58 |
"foo": False, |
| 59 |
}, |
| 60 |
})) |
| 61 |
|
| 62 |
self.assertTrue(qa_config.TestEnabled("bar", _cfg={ |
| 63 |
"tests": { |
| 64 |
"default": default, |
| 65 |
"bar": True, |
| 66 |
}, |
| 67 |
})) |
| 68 |
|
| 69 |
def testEitherWithDefault(self): |
| 70 |
names = qa_config.Either("one") |
| 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 |
def testEither(self): |
| 85 |
names = [qa_config.Either(["one", "two"]), |
| 86 |
qa_config.Either("foo"), |
| 87 |
"hello", |
| 88 |
["bar", "baz"]] |
| 89 |
|
| 90 |
self.assertTrue(qa_config.TestEnabled(names, _cfg={ |
| 91 |
"tests": { |
| 92 |
"default": True, |
| 93 |
}, |
| 94 |
})) |
| 95 |
|
| 96 |
self.assertFalse(qa_config.TestEnabled(names, _cfg={ |
| 97 |
"tests": { |
| 98 |
"default": False, |
| 99 |
}, |
| 100 |
})) |
| 101 |
|
| 102 |
for name in ["foo", "bar", "baz", "hello"]: |
| 103 |
self.assertFalse(qa_config.TestEnabled(names, _cfg={ |
| 104 |
"tests": { |
| 105 |
"default": True, |
| 106 |
name: False, |
| 107 |
}, |
| 108 |
})) |
| 109 |
|
| 110 |
self.assertFalse(qa_config.TestEnabled(names, _cfg={ |
| 111 |
"tests": { |
| 112 |
"default": True, |
| 113 |
"one": False, |
| 114 |
"two": False, |
| 115 |
}, |
| 116 |
})) |
| 117 |
|
| 118 |
self.assertTrue(qa_config.TestEnabled(names, _cfg={ |
| 119 |
"tests": { |
| 120 |
"default": True, |
| 121 |
"one": False, |
| 122 |
"two": True, |
| 123 |
}, |
| 124 |
})) |
| 125 |
|
| 126 |
self.assertFalse(qa_config.TestEnabled(names, _cfg={ |
| 127 |
"tests": { |
| 128 |
"default": True, |
| 129 |
"one": True, |
| 130 |
"two": True, |
| 131 |
"foo": False, |
| 132 |
}, |
| 133 |
})) |
| 134 |
|
| 135 |
def testEitherNestedWithAnd(self): |
| 136 |
names = qa_config.Either([["one", "two"], "foo"]) |
| 137 |
|
| 138 |
self.assertTrue(qa_config.TestEnabled(names, _cfg={ |
| 139 |
"tests": { |
| 140 |
"default": True, |
| 141 |
}, |
| 142 |
})) |
| 143 |
|
| 144 |
for name in ["one", "two"]: |
| 145 |
self.assertFalse(qa_config.TestEnabled(names, _cfg={ |
| 146 |
"tests": { |
| 147 |
"default": True, |
| 148 |
"foo": False, |
| 149 |
name: False, |
| 150 |
}, |
| 151 |
})) |
| 152 |
|
| 153 |
def testCallable(self): |
| 154 |
self.assertTrue(qa_config.TestEnabled([lambda: True], _cfg={})) |
| 155 |
|
| 156 |
for value in [None, False, "", 0]: |
| 157 |
self.assertFalse(qa_config.TestEnabled(lambda: value, _cfg={})) |
| 158 |
|
| 159 |
|
| 160 |
class TestQaConfigLoad(unittest.TestCase): |
| 161 |
def setUp(self): |
| 162 |
self.tmpdir = tempfile.mkdtemp() |
| 163 |
|
| 164 |
def tearDown(self): |
| 165 |
shutil.rmtree(self.tmpdir) |
| 166 |
|
| 167 |
def testLoadNonExistent(self): |
| 168 |
filename = utils.PathJoin(self.tmpdir, "does.not.exist") |
| 169 |
self.assertRaises(EnvironmentError, qa_config._QaConfig.Load, filename) |
| 170 |
|
| 171 |
@staticmethod |
| 172 |
def _WriteConfig(filename, data): |
| 173 |
utils.WriteFile(filename, data=serializer.DumpJson(data)) |
| 174 |
|
| 175 |
def _CheckLoadError(self, filename, data, expected): |
| 176 |
self._WriteConfig(filename, data) |
| 177 |
|
| 178 |
try: |
| 179 |
qa_config._QaConfig.Load(filename) |
| 180 |
except qa_error.Error as err: |
| 181 |
self.assertTrue(str(err).startswith(expected)) |
| 182 |
else: |
| 183 |
self.fail("Exception was not raised") |
| 184 |
|
| 185 |
def testFailsValidation(self): |
| 186 |
filename = utils.PathJoin(self.tmpdir, "qa.json") |
| 187 |
testconfig = {} |
| 188 |
|
| 189 |
check_fn = compat.partial(self._CheckLoadError, filename, testconfig) |
| 190 |
|
| 191 |
|
| 192 |
check_fn("Cluster name is required") |
| 193 |
|
| 194 |
testconfig["name"] = "cluster.example.com" |
| 195 |
|
| 196 |
|
| 197 |
check_fn("Need at least one node") |
| 198 |
|
| 199 |
testconfig["nodes"] = [ |
| 200 |
{ |
| 201 |
"primary": "xen-test-0", |
| 202 |
"secondary": "192.0.2.1", |
| 203 |
}, |
| 204 |
] |
| 205 |
|
| 206 |
|
| 207 |
check_fn("Need at least one instance") |
| 208 |
|
| 209 |
testconfig["instances"] = [ |
| 210 |
{ |
| 211 |
"name": "xen-test-inst1", |
| 212 |
}, |
| 213 |
] |
| 214 |
|
| 215 |
|
| 216 |
check_fn("Config option 'disks'") |
| 217 |
|
| 218 |
testconfig["disks"] = [] |
| 219 |
|
| 220 |
|
| 221 |
self._WriteConfig(filename, testconfig) |
| 222 |
result = qa_config._QaConfig.Load(filename) |
| 223 |
self.assertTrue(result.get("nodes")) |
| 224 |
|
| 225 |
|
| 226 |
testconfig[qa_config._INSTANCE_CHECK_KEY] = \ |
| 227 |
utils.PathJoin(self.tmpdir, "instcheck") |
| 228 |
check_fn("Can't find instance check script") |
| 229 |
del testconfig[qa_config._INSTANCE_CHECK_KEY] |
| 230 |
|
| 231 |
|
| 232 |
testconfig[qa_config._ENABLED_HV_KEY] = None |
| 233 |
check_fn("No hypervisor is enabled") |
| 234 |
|
| 235 |
|
| 236 |
testconfig[qa_config._ENABLED_HV_KEY] = ["#unknownhv#"] |
| 237 |
check_fn("Unknown hypervisor(s) enabled:") |
| 238 |
del testconfig[qa_config._ENABLED_HV_KEY] |
| 239 |
|
| 240 |
|
| 241 |
testconfig[qa_config._VCLUSTER_MASTER_KEY] = "value" |
| 242 |
testconfig[qa_config._VCLUSTER_BASEDIR_KEY] = "./not//normalized/" |
| 243 |
check_fn("Path given in option 'vcluster-basedir' must be") |
| 244 |
|
| 245 |
|
| 246 |
testconfig.pop(qa_config._VCLUSTER_MASTER_KEY) |
| 247 |
testconfig[qa_config._VCLUSTER_BASEDIR_KEY] = "/tmp" |
| 248 |
check_fn("All or none of the") |
| 249 |
|
| 250 |
testconfig[qa_config._VCLUSTER_MASTER_KEY] = "master.example.com" |
| 251 |
testconfig.pop(qa_config._VCLUSTER_BASEDIR_KEY) |
| 252 |
check_fn("All or none of the") |
| 253 |
|
| 254 |
|
| 255 |
testconfig[qa_config._VCLUSTER_MASTER_KEY] = "master.example.com" |
| 256 |
testconfig[qa_config._VCLUSTER_BASEDIR_KEY] = "/tmp" |
| 257 |
|
| 258 |
self._WriteConfig(filename, testconfig) |
| 259 |
result = qa_config._QaConfig.Load(filename) |
| 260 |
self.assertEqual(result.GetVclusterSettings(), |
| 261 |
("master.example.com", "/tmp")) |
| 262 |
|
| 263 |
|
| 264 |
class TestQaConfigWithSampleConfig(unittest.TestCase): |
| 265 |
"""Tests using C{qa-sample.json}. |
| 266 |
|
| 267 |
This test case serves two purposes: |
| 268 |
|
| 269 |
- Ensure shipped C{qa-sample.json} file is considered a valid QA |
| 270 |
configuration |
| 271 |
- Test some functions of L{qa_config._QaConfig} without having to |
| 272 |
mock a whole configuration file |
| 273 |
|
| 274 |
""" |
| 275 |
def setUp(self): |
| 276 |
filename = "%s/qa/qa-sample.json" % testutils.GetSourceDir() |
| 277 |
|
| 278 |
self.config = qa_config._QaConfig.Load(filename) |
| 279 |
|
| 280 |
def testGetEnabledHypervisors(self): |
| 281 |
self.assertEqual(self.config.GetEnabledHypervisors(), |
| 282 |
[constants.DEFAULT_ENABLED_HYPERVISOR]) |
| 283 |
|
| 284 |
def testGetDefaultHypervisor(self): |
| 285 |
self.assertEqual(self.config.GetDefaultHypervisor(), |
| 286 |
constants.DEFAULT_ENABLED_HYPERVISOR) |
| 287 |
|
| 288 |
def testGetInstanceCheckScript(self): |
| 289 |
self.assertTrue(self.config.GetInstanceCheckScript() is None) |
| 290 |
|
| 291 |
def testGetAndGetItem(self): |
| 292 |
self.assertEqual(self.config["nodes"], self.config.get("nodes")) |
| 293 |
|
| 294 |
def testGetMasterNode(self): |
| 295 |
self.assertEqual(self.config.GetMasterNode(), self.config["nodes"][0]) |
| 296 |
|
| 297 |
def testGetVclusterSettings(self): |
| 298 |
|
| 299 |
self.assertEqual(self.config.GetVclusterSettings(), (None, None)) |
| 300 |
|
| 301 |
self.assertFalse(qa_config.UseVirtualCluster(_cfg=self.config)) |
| 302 |
|
| 303 |
|
| 304 |
class TestQaConfig(unittest.TestCase): |
| 305 |
def setUp(self): |
| 306 |
filename = \ |
| 307 |
testutils.TestDataFilename("qa-minimal-nodes-instances-only.json") |
| 308 |
|
| 309 |
self.config = qa_config._QaConfig.Load(filename) |
| 310 |
|
| 311 |
def testExclusiveStorage(self): |
| 312 |
self.assertRaises(AssertionError, self.config.GetExclusiveStorage) |
| 313 |
|
| 314 |
for value in [False, True, 0, 1, 30804, ""]: |
| 315 |
self.config.SetExclusiveStorage(value) |
| 316 |
self.assertEqual(self.config.GetExclusiveStorage(), bool(value)) |
| 317 |
|
| 318 |
def testIsTemplateSupported(self): |
| 319 |
enabled_dts = self.config.GetEnabledDiskTemplates() |
| 320 |
for e_s in [False, True]: |
| 321 |
self.config.SetExclusiveStorage(e_s) |
| 322 |
for template in constants.DISK_TEMPLATES: |
| 323 |
if (template not in enabled_dts or |
| 324 |
e_s and template not in constants.DTS_EXCL_STORAGE): |
| 325 |
self.assertFalse(self.config.IsTemplateSupported(template)) |
| 326 |
else: |
| 327 |
self.assertTrue(self.config.IsTemplateSupported(template)) |
| 328 |
|
| 329 |
def testInstanceConversion(self): |
| 330 |
self.assertTrue(isinstance(self.config["instances"][0], |
| 331 |
qa_config._QaInstance)) |
| 332 |
|
| 333 |
def testNodeConversion(self): |
| 334 |
self.assertTrue(isinstance(self.config["nodes"][0], |
| 335 |
qa_config._QaNode)) |
| 336 |
|
| 337 |
def testAcquireAndReleaseInstance(self): |
| 338 |
self.assertFalse(compat.any(i.used for i in self.config["instances"])) |
| 339 |
|
| 340 |
inst = qa_config.AcquireInstance(_cfg=self.config) |
| 341 |
self.assertTrue(inst.used) |
| 342 |
self.assertTrue(inst.disk_template is None) |
| 343 |
|
| 344 |
inst.Release() |
| 345 |
|
| 346 |
self.assertFalse(inst.used) |
| 347 |
self.assertTrue(inst.disk_template is None) |
| 348 |
|
| 349 |
self.assertFalse(compat.any(i.used for i in self.config["instances"])) |
| 350 |
|
| 351 |
def testAcquireInstanceTooMany(self): |
| 352 |
|
| 353 |
for _ in range(len(self.config["instances"])): |
| 354 |
inst = qa_config.AcquireInstance(_cfg=self.config) |
| 355 |
self.assertTrue(inst.used) |
| 356 |
self.assertTrue(inst.disk_template is None) |
| 357 |
|
| 358 |
|
| 359 |
self.assertRaises(qa_error.OutOfInstancesError, |
| 360 |
qa_config.AcquireInstance, _cfg=self.config) |
| 361 |
|
| 362 |
def testAcquireNodeNoneAdded(self): |
| 363 |
self.assertFalse(compat.any(n.added for n in self.config["nodes"])) |
| 364 |
|
| 365 |
|
| 366 |
node = qa_config.AcquireNode(_cfg=self.config) |
| 367 |
self.assertEqual(node, self.config.GetMasterNode()) |
| 368 |
|
| 369 |
|
| 370 |
self.assertRaises(qa_error.OutOfNodesError, qa_config.AcquireNode, |
| 371 |
exclude=[node], _cfg=self.config) |
| 372 |
|
| 373 |
def testAcquireNodeTooMany(self): |
| 374 |
|
| 375 |
for node in self.config["nodes"]: |
| 376 |
if node != self.config.GetMasterNode(): |
| 377 |
node.MarkAdded() |
| 378 |
|
| 379 |
nodecount = len(self.config["nodes"]) |
| 380 |
|
| 381 |
self.assertTrue(nodecount > 1) |
| 382 |
|
| 383 |
acquired = [] |
| 384 |
|
| 385 |
for _ in range(nodecount): |
| 386 |
node = qa_config.AcquireNode(exclude=acquired, _cfg=self.config) |
| 387 |
if node == self.config.GetMasterNode(): |
| 388 |
self.assertFalse(node.added) |
| 389 |
else: |
| 390 |
self.assertTrue(node.added) |
| 391 |
self.assertEqual(node.use_count, 1) |
| 392 |
acquired.append(node) |
| 393 |
|
| 394 |
self.assertRaises(qa_error.OutOfNodesError, qa_config.AcquireNode, |
| 395 |
exclude=acquired, _cfg=self.config) |
| 396 |
|
| 397 |
def testAcquireNodeOrder(self): |
| 398 |
|
| 399 |
for node in self.config["nodes"]: |
| 400 |
if node != self.config.GetMasterNode(): |
| 401 |
node.MarkAdded() |
| 402 |
|
| 403 |
nodecount = len(self.config["nodes"]) |
| 404 |
|
| 405 |
for iterations in [0, 1, 3, 100, 127, 7964]: |
| 406 |
acquired = [] |
| 407 |
|
| 408 |
for i in range(iterations): |
| 409 |
node = qa_config.AcquireNode(_cfg=self.config) |
| 410 |
self.assertTrue(node.use_count > 0) |
| 411 |
self.assertEqual(node.use_count, (i // nodecount + 1)) |
| 412 |
acquired.append((node.use_count, node.primary, node)) |
| 413 |
|
| 414 |
|
| 415 |
key_fn = lambda a_b_c: (a_b_c[0], utils.NiceSortKey(a_b_c[1]), a_b_c[2]) |
| 416 |
self.assertEqual(acquired, sorted(acquired, key=key_fn)) |
| 417 |
|
| 418 |
|
| 419 |
qa_config.ReleaseManyNodes([a[2] for a in acquired]) |
| 420 |
|
| 421 |
|
| 422 |
for node in self.config["nodes"]: |
| 423 |
self.assertEqual(node.use_count, 0) |
| 424 |
self.assertTrue(node.added or node == self.config.GetMasterNode()) |
| 425 |
|
| 426 |
|
| 427 |
class TestRepresentation(unittest.TestCase): |
| 428 |
def _Check(self, target, part): |
| 429 |
self.assertTrue(part in repr(target).split()) |
| 430 |
|
| 431 |
def testQaInstance(self): |
| 432 |
inst = qa_config._QaInstance("inst1.example.com", []) |
| 433 |
self._Check(inst, "name=inst1.example.com") |
| 434 |
self._Check(inst, "nicmac=[]") |
| 435 |
|
| 436 |
|
| 437 |
self._Check(inst, "disk_template=None") |
| 438 |
self._Check(inst, "used=None") |
| 439 |
|
| 440 |
|
| 441 |
inst.Use() |
| 442 |
self._Check(inst, "used=True") |
| 443 |
|
| 444 |
|
| 445 |
inst.SetDiskTemplate(constants.DT_DRBD8) |
| 446 |
self._Check(inst, "disk_template=%s" % constants.DT_DRBD8) |
| 447 |
|
| 448 |
|
| 449 |
inst.Release() |
| 450 |
self._Check(inst, "used=False") |
| 451 |
self._Check(inst, "disk_template=None") |
| 452 |
|
| 453 |
def testQaNode(self): |
| 454 |
node = qa_config._QaNode("primary.example.com", "192.0.2.1") |
| 455 |
self._Check(node, "primary=primary.example.com") |
| 456 |
self._Check(node, "secondary=192.0.2.1") |
| 457 |
self._Check(node, "added=False") |
| 458 |
self._Check(node, "use_count=0") |
| 459 |
|
| 460 |
|
| 461 |
node.MarkAdded() |
| 462 |
self._Check(node, "added=True") |
| 463 |
|
| 464 |
|
| 465 |
for i in range(1, 5): |
| 466 |
node.Use() |
| 467 |
self._Check(node, "use_count=%s" % i) |
| 468 |
|
| 469 |
|
| 470 |
for i in reversed(range(1, 5)): |
| 471 |
node.Release() |
| 472 |
self._Check(node, "use_count=%s" % (i - 1)) |
| 473 |
|
| 474 |
self._Check(node, "use_count=0") |
| 475 |
|
| 476 |
|
| 477 |
node.MarkRemoved() |
| 478 |
self._Check(node, "added=False") |
| 479 |
|
| 480 |
|
| 481 |
if __name__ == "__main__": |
| 482 |
testutils.GanetiTestProgram() |