| 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 unittesting the cli module""" |
| 32 |
|
| 33 |
import copy |
| 34 |
import testutils |
| 35 |
import time |
| 36 |
import unittest |
| 37 |
import yaml |
| 38 |
from io import StringIO |
| 39 |
|
| 40 |
from ganeti import constants |
| 41 |
from ganeti import cli |
| 42 |
from ganeti import errors |
| 43 |
from ganeti import utils |
| 44 |
from ganeti import objects |
| 45 |
from ganeti import qlang |
| 46 |
from ganeti.errors import OpPrereqError, ParameterError |
| 47 |
|
| 48 |
|
| 49 |
class TestParseTimespec(unittest.TestCase): |
| 50 |
"""Testing case for ParseTimespec""" |
| 51 |
|
| 52 |
def testValidTimes(self): |
| 53 |
"""Test valid timespecs""" |
| 54 |
test_data = [ |
| 55 |
("1s", 1), |
| 56 |
("1", 1), |
| 57 |
("1m", 60), |
| 58 |
("1h", 60 * 60), |
| 59 |
("1d", 60 * 60 * 24), |
| 60 |
("1w", 60 * 60 * 24 * 7), |
| 61 |
("4h", 4 * 60 * 60), |
| 62 |
("61m", 61 * 60), |
| 63 |
] |
| 64 |
for value, expected_result in test_data: |
| 65 |
self.assertEqual(cli.ParseTimespec(value), expected_result) |
| 66 |
|
| 67 |
def testInvalidTime(self): |
| 68 |
"""Test invalid timespecs""" |
| 69 |
test_data = [ |
| 70 |
"1y", |
| 71 |
"", |
| 72 |
"aaa", |
| 73 |
"s", |
| 74 |
] |
| 75 |
for value in test_data: |
| 76 |
self.assertRaises(OpPrereqError, cli.ParseTimespec, value) |
| 77 |
|
| 78 |
|
| 79 |
class TestToStream(unittest.TestCase): |
| 80 |
"""Test the ToStream functions""" |
| 81 |
|
| 82 |
def testBasic(self): |
| 83 |
for data in ["foo", |
| 84 |
"foo %s", |
| 85 |
"foo %(test)s", |
| 86 |
"foo %s %s", |
| 87 |
"", |
| 88 |
]: |
| 89 |
buf = StringIO() |
| 90 |
cli._ToStream(buf, data) |
| 91 |
self.assertEqual(buf.getvalue(), data + "\n") |
| 92 |
|
| 93 |
def testParams(self): |
| 94 |
buf = StringIO() |
| 95 |
cli._ToStream(buf, "foo %s", 1) |
| 96 |
self.assertEqual(buf.getvalue(), "foo 1\n") |
| 97 |
buf = StringIO() |
| 98 |
cli._ToStream(buf, "foo %s", (15,16)) |
| 99 |
self.assertEqual(buf.getvalue(), "foo (15, 16)\n") |
| 100 |
buf = StringIO() |
| 101 |
cli._ToStream(buf, "foo %s %s", "a", "b") |
| 102 |
self.assertEqual(buf.getvalue(), "foo a b\n") |
| 103 |
|
| 104 |
|
| 105 |
class TestGenerateTable(unittest.TestCase): |
| 106 |
HEADERS = dict([("f%s" % i, "Field%s" % i) for i in range(5)]) |
| 107 |
|
| 108 |
FIELDS1 = ["f1", "f2"] |
| 109 |
DATA1 = [ |
| 110 |
["abc", 1234], |
| 111 |
["foobar", 56], |
| 112 |
["b", -14], |
| 113 |
] |
| 114 |
|
| 115 |
def _test(self, headers, fields, separator, data, |
| 116 |
numfields, unitfields, units, expected): |
| 117 |
table = cli.GenerateTable(headers, fields, separator, data, |
| 118 |
numfields=numfields, unitfields=unitfields, |
| 119 |
units=units) |
| 120 |
self.assertEqual(table, expected) |
| 121 |
|
| 122 |
def testPlain(self): |
| 123 |
exp = [ |
| 124 |
"Field1 Field2", |
| 125 |
"abc 1234", |
| 126 |
"foobar 56", |
| 127 |
"b -14", |
| 128 |
] |
| 129 |
self._test(self.HEADERS, self.FIELDS1, None, self.DATA1, |
| 130 |
None, None, "m", exp) |
| 131 |
|
| 132 |
def testNoFields(self): |
| 133 |
self._test(self.HEADERS, [], None, [[], []], |
| 134 |
None, None, "m", ["", "", ""]) |
| 135 |
self._test(None, [], None, [[], []], |
| 136 |
None, None, "m", ["", ""]) |
| 137 |
|
| 138 |
def testSeparator(self): |
| 139 |
for sep in ["#", ":", ",", "^", "!", "%", "|", "###", "%%", "!!!", "||"]: |
| 140 |
exp = [ |
| 141 |
"Field1%sField2" % sep, |
| 142 |
"abc%s1234" % sep, |
| 143 |
"foobar%s56" % sep, |
| 144 |
"b%s-14" % sep, |
| 145 |
] |
| 146 |
self._test(self.HEADERS, self.FIELDS1, sep, self.DATA1, |
| 147 |
None, None, "m", exp) |
| 148 |
|
| 149 |
def testNoHeader(self): |
| 150 |
exp = [ |
| 151 |
"abc 1234", |
| 152 |
"foobar 56", |
| 153 |
"b -14", |
| 154 |
] |
| 155 |
self._test(None, self.FIELDS1, None, self.DATA1, |
| 156 |
None, None, "m", exp) |
| 157 |
|
| 158 |
def testUnknownField(self): |
| 159 |
headers = { |
| 160 |
"f1": "Field1", |
| 161 |
} |
| 162 |
exp = [ |
| 163 |
"Field1 UNKNOWN", |
| 164 |
"abc 1234", |
| 165 |
"foobar 56", |
| 166 |
"b -14", |
| 167 |
] |
| 168 |
self._test(headers, ["f1", "UNKNOWN"], None, self.DATA1, |
| 169 |
None, None, "m", exp) |
| 170 |
|
| 171 |
def testNumfields(self): |
| 172 |
fields = ["f1", "f2", "f3"] |
| 173 |
data = [ |
| 174 |
["abc", 1234, 0], |
| 175 |
["foobar", 56, 3], |
| 176 |
["b", -14, "-"], |
| 177 |
] |
| 178 |
exp = [ |
| 179 |
"Field1 Field2 Field3", |
| 180 |
"abc 1234 0", |
| 181 |
"foobar 56 3", |
| 182 |
"b -14 -", |
| 183 |
] |
| 184 |
self._test(self.HEADERS, fields, None, data, |
| 185 |
["f2", "f3"], None, "m", exp) |
| 186 |
|
| 187 |
def testUnitfields(self): |
| 188 |
expnosep = [ |
| 189 |
"Field1 Field2 Field3", |
| 190 |
"abc 1234 0M", |
| 191 |
"foobar 56 3M", |
| 192 |
"b -14 -", |
| 193 |
] |
| 194 |
|
| 195 |
expsep = [ |
| 196 |
"Field1:Field2:Field3", |
| 197 |
"abc:1234:0M", |
| 198 |
"foobar:56:3M", |
| 199 |
"b:-14:-", |
| 200 |
] |
| 201 |
|
| 202 |
for sep, expected in [(None, expnosep), (":", expsep)]: |
| 203 |
fields = ["f1", "f2", "f3"] |
| 204 |
data = [ |
| 205 |
["abc", 1234, 0], |
| 206 |
["foobar", 56, 3], |
| 207 |
["b", -14, "-"], |
| 208 |
] |
| 209 |
self._test(self.HEADERS, fields, sep, data, |
| 210 |
["f2", "f3"], ["f3"], "h", expected) |
| 211 |
|
| 212 |
def testUnusual(self): |
| 213 |
data = [ |
| 214 |
["%", "xyz"], |
| 215 |
["%%", "abc"], |
| 216 |
] |
| 217 |
exp = [ |
| 218 |
"Field1 Field2", |
| 219 |
"% xyz", |
| 220 |
"%% abc", |
| 221 |
] |
| 222 |
self._test(self.HEADERS, ["f1", "f2"], None, data, |
| 223 |
None, None, "m", exp) |
| 224 |
|
| 225 |
|
| 226 |
class TestFormatQueryResult(unittest.TestCase): |
| 227 |
def test(self): |
| 228 |
fields = [ |
| 229 |
objects.QueryFieldDefinition(name="name", title="Name", |
| 230 |
kind=constants.QFT_TEXT), |
| 231 |
objects.QueryFieldDefinition(name="size", title="Size", |
| 232 |
kind=constants.QFT_NUMBER), |
| 233 |
objects.QueryFieldDefinition(name="act", title="Active", |
| 234 |
kind=constants.QFT_BOOL), |
| 235 |
objects.QueryFieldDefinition(name="mem", title="Memory", |
| 236 |
kind=constants.QFT_UNIT), |
| 237 |
objects.QueryFieldDefinition(name="other", title="SomeList", |
| 238 |
kind=constants.QFT_OTHER), |
| 239 |
] |
| 240 |
|
| 241 |
response = objects.QueryResponse(fields=fields, data=[ |
| 242 |
[(constants.RS_NORMAL, "nodeA"), (constants.RS_NORMAL, 128), |
| 243 |
(constants.RS_NORMAL, False), (constants.RS_NORMAL, 1468006), |
| 244 |
(constants.RS_NORMAL, [])], |
| 245 |
[(constants.RS_NORMAL, "other"), (constants.RS_NORMAL, 512), |
| 246 |
(constants.RS_NORMAL, True), (constants.RS_NORMAL, 16), |
| 247 |
(constants.RS_NORMAL, [1, 2, 3])], |
| 248 |
[(constants.RS_NORMAL, "xyz"), (constants.RS_NORMAL, 1024), |
| 249 |
(constants.RS_NORMAL, True), (constants.RS_NORMAL, 4096), |
| 250 |
(constants.RS_NORMAL, [{}, {}])], |
| 251 |
]) |
| 252 |
|
| 253 |
self.assertEqual(cli.FormatQueryResult(response, unit="h", header=True), |
| 254 |
(cli.QR_NORMAL, [ |
| 255 |
"Name Size Active Memory SomeList", |
| 256 |
"nodeA 128 N 1.4T []", |
| 257 |
"other 512 Y 16M [1, 2, 3]", |
| 258 |
"xyz 1024 Y 4.0G [{}, {}]", |
| 259 |
])) |
| 260 |
|
| 261 |
def testTimestampAndUnit(self): |
| 262 |
fields = [ |
| 263 |
objects.QueryFieldDefinition(name="name", title="Name", |
| 264 |
kind=constants.QFT_TEXT), |
| 265 |
objects.QueryFieldDefinition(name="size", title="Size", |
| 266 |
kind=constants.QFT_UNIT), |
| 267 |
objects.QueryFieldDefinition(name="mtime", title="ModTime", |
| 268 |
kind=constants.QFT_TIMESTAMP), |
| 269 |
] |
| 270 |
|
| 271 |
response = objects.QueryResponse(fields=fields, data=[ |
| 272 |
[(constants.RS_NORMAL, "a"), (constants.RS_NORMAL, 1024), |
| 273 |
(constants.RS_NORMAL, 0)], |
| 274 |
[(constants.RS_NORMAL, "b"), (constants.RS_NORMAL, 144996), |
| 275 |
(constants.RS_NORMAL, 1291746295)], |
| 276 |
]) |
| 277 |
|
| 278 |
self.assertEqual(cli.FormatQueryResult(response, unit="m", header=True), |
| 279 |
(cli.QR_NORMAL, [ |
| 280 |
"Name Size ModTime", |
| 281 |
"a 1024 %s" % utils.FormatTime(0), |
| 282 |
"b 144996 %s" % utils.FormatTime(1291746295), |
| 283 |
])) |
| 284 |
|
| 285 |
def testOverride(self): |
| 286 |
fields = [ |
| 287 |
objects.QueryFieldDefinition(name="name", title="Name", |
| 288 |
kind=constants.QFT_TEXT), |
| 289 |
objects.QueryFieldDefinition(name="cust", title="Custom", |
| 290 |
kind=constants.QFT_OTHER), |
| 291 |
objects.QueryFieldDefinition(name="xt", title="XTime", |
| 292 |
kind=constants.QFT_TIMESTAMP), |
| 293 |
] |
| 294 |
|
| 295 |
response = objects.QueryResponse(fields=fields, data=[ |
| 296 |
[(constants.RS_NORMAL, "x"), (constants.RS_NORMAL, ["a", "b", "c"]), |
| 297 |
(constants.RS_NORMAL, 1234)], |
| 298 |
[(constants.RS_NORMAL, "y"), (constants.RS_NORMAL, range(10)), |
| 299 |
(constants.RS_NORMAL, 1291746295)], |
| 300 |
]) |
| 301 |
|
| 302 |
override = { |
| 303 |
"cust": (utils.CommaJoin, False), |
| 304 |
"xt": (hex, True), |
| 305 |
} |
| 306 |
|
| 307 |
self.assertEqual(cli.FormatQueryResult(response, unit="h", header=True, |
| 308 |
format_override=override), |
| 309 |
(cli.QR_NORMAL, [ |
| 310 |
"Name Custom XTime", |
| 311 |
"x a, b, c 0x4d2", |
| 312 |
"y 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 0x4cfe7bf7", |
| 313 |
])) |
| 314 |
|
| 315 |
def testSeparator(self): |
| 316 |
fields = [ |
| 317 |
objects.QueryFieldDefinition(name="name", title="Name", |
| 318 |
kind=constants.QFT_TEXT), |
| 319 |
objects.QueryFieldDefinition(name="count", title="Count", |
| 320 |
kind=constants.QFT_NUMBER), |
| 321 |
objects.QueryFieldDefinition(name="desc", title="Description", |
| 322 |
kind=constants.QFT_TEXT), |
| 323 |
] |
| 324 |
|
| 325 |
response = objects.QueryResponse(fields=fields, data=[ |
| 326 |
[(constants.RS_NORMAL, "instance1.example.com"), |
| 327 |
(constants.RS_NORMAL, 21125), (constants.RS_NORMAL, "Hello World!")], |
| 328 |
[(constants.RS_NORMAL, "mail.other.net"), |
| 329 |
(constants.RS_NORMAL, -9000), (constants.RS_NORMAL, "a,b,c")], |
| 330 |
]) |
| 331 |
|
| 332 |
for sep in [":", "|", "#", "|||", "###", "@@@", "@#@"]: |
| 333 |
for header in [None, "Name%sCount%sDescription" % (sep, sep)]: |
| 334 |
exp = [] |
| 335 |
if header: |
| 336 |
exp.append(header) |
| 337 |
exp.extend([ |
| 338 |
"instance1.example.com%s21125%sHello World!" % (sep, sep), |
| 339 |
"mail.other.net%s-9000%sa,b,c" % (sep, sep), |
| 340 |
]) |
| 341 |
|
| 342 |
self.assertEqual(cli.FormatQueryResult(response, separator=sep, |
| 343 |
header=bool(header)), |
| 344 |
(cli.QR_NORMAL, exp)) |
| 345 |
|
| 346 |
def testStatusWithUnknown(self): |
| 347 |
fields = [ |
| 348 |
objects.QueryFieldDefinition(name="id", title="ID", |
| 349 |
kind=constants.QFT_NUMBER), |
| 350 |
objects.QueryFieldDefinition(name="unk", title="unk", |
| 351 |
kind=constants.QFT_UNKNOWN), |
| 352 |
objects.QueryFieldDefinition(name="unavail", title="Unavail", |
| 353 |
kind=constants.QFT_BOOL), |
| 354 |
objects.QueryFieldDefinition(name="nodata", title="NoData", |
| 355 |
kind=constants.QFT_TEXT), |
| 356 |
objects.QueryFieldDefinition(name="offline", title="OffLine", |
| 357 |
kind=constants.QFT_TEXT), |
| 358 |
] |
| 359 |
|
| 360 |
response = objects.QueryResponse(fields=fields, data=[ |
| 361 |
[(constants.RS_NORMAL, 1), (constants.RS_UNKNOWN, None), |
| 362 |
(constants.RS_NORMAL, False), (constants.RS_NORMAL, ""), |
| 363 |
(constants.RS_OFFLINE, None)], |
| 364 |
[(constants.RS_NORMAL, 2), (constants.RS_UNKNOWN, None), |
| 365 |
(constants.RS_NODATA, None), (constants.RS_NORMAL, "x"), |
| 366 |
(constants.RS_OFFLINE, None)], |
| 367 |
[(constants.RS_NORMAL, 3), (constants.RS_UNKNOWN, None), |
| 368 |
(constants.RS_NORMAL, False), (constants.RS_UNAVAIL, None), |
| 369 |
(constants.RS_OFFLINE, None)], |
| 370 |
]) |
| 371 |
|
| 372 |
self.assertEqual(cli.FormatQueryResult(response, header=True, |
| 373 |
separator="|", verbose=True), |
| 374 |
(cli.QR_UNKNOWN, [ |
| 375 |
"ID|unk|Unavail|NoData|OffLine", |
| 376 |
"1|(unknown)|N||(offline)", |
| 377 |
"2|(unknown)|(nodata)|x|(offline)", |
| 378 |
"3|(unknown)|N|(unavail)|(offline)", |
| 379 |
])) |
| 380 |
self.assertEqual(cli.FormatQueryResult(response, header=True, |
| 381 |
separator="|", verbose=False), |
| 382 |
(cli.QR_UNKNOWN, [ |
| 383 |
"ID|unk|Unavail|NoData|OffLine", |
| 384 |
"1|??|N||*", |
| 385 |
"2|??|?|x|*", |
| 386 |
"3|??|N|-|*", |
| 387 |
])) |
| 388 |
|
| 389 |
def testNoData(self): |
| 390 |
fields = [ |
| 391 |
objects.QueryFieldDefinition(name="id", title="ID", |
| 392 |
kind=constants.QFT_NUMBER), |
| 393 |
objects.QueryFieldDefinition(name="name", title="Name", |
| 394 |
kind=constants.QFT_TEXT), |
| 395 |
] |
| 396 |
|
| 397 |
response = objects.QueryResponse(fields=fields, data=[]) |
| 398 |
|
| 399 |
self.assertEqual(cli.FormatQueryResult(response, header=True), |
| 400 |
(cli.QR_NORMAL, ["ID Name"])) |
| 401 |
|
| 402 |
def testNoDataWithUnknown(self): |
| 403 |
fields = [ |
| 404 |
objects.QueryFieldDefinition(name="id", title="ID", |
| 405 |
kind=constants.QFT_NUMBER), |
| 406 |
objects.QueryFieldDefinition(name="unk", title="unk", |
| 407 |
kind=constants.QFT_UNKNOWN), |
| 408 |
] |
| 409 |
|
| 410 |
response = objects.QueryResponse(fields=fields, data=[]) |
| 411 |
|
| 412 |
self.assertEqual(cli.FormatQueryResult(response, header=False), |
| 413 |
(cli.QR_UNKNOWN, [])) |
| 414 |
|
| 415 |
def testStatus(self): |
| 416 |
fields = [ |
| 417 |
objects.QueryFieldDefinition(name="id", title="ID", |
| 418 |
kind=constants.QFT_NUMBER), |
| 419 |
objects.QueryFieldDefinition(name="unavail", title="Unavail", |
| 420 |
kind=constants.QFT_BOOL), |
| 421 |
objects.QueryFieldDefinition(name="nodata", title="NoData", |
| 422 |
kind=constants.QFT_TEXT), |
| 423 |
objects.QueryFieldDefinition(name="offline", title="OffLine", |
| 424 |
kind=constants.QFT_TEXT), |
| 425 |
] |
| 426 |
|
| 427 |
response = objects.QueryResponse(fields=fields, data=[ |
| 428 |
[(constants.RS_NORMAL, 1), (constants.RS_NORMAL, False), |
| 429 |
(constants.RS_NORMAL, ""), (constants.RS_OFFLINE, None)], |
| 430 |
[(constants.RS_NORMAL, 2), (constants.RS_NODATA, None), |
| 431 |
(constants.RS_NORMAL, "x"), (constants.RS_NORMAL, "abc")], |
| 432 |
[(constants.RS_NORMAL, 3), (constants.RS_NORMAL, False), |
| 433 |
(constants.RS_UNAVAIL, None), (constants.RS_OFFLINE, None)], |
| 434 |
]) |
| 435 |
|
| 436 |
self.assertEqual(cli.FormatQueryResult(response, header=False, |
| 437 |
separator="|", verbose=True), |
| 438 |
(cli.QR_INCOMPLETE, [ |
| 439 |
"1|N||(offline)", |
| 440 |
"2|(nodata)|x|abc", |
| 441 |
"3|N|(unavail)|(offline)", |
| 442 |
])) |
| 443 |
self.assertEqual(cli.FormatQueryResult(response, header=False, |
| 444 |
separator="|", verbose=False), |
| 445 |
(cli.QR_INCOMPLETE, [ |
| 446 |
"1|N||*", |
| 447 |
"2|?|x|abc", |
| 448 |
"3|N|-|*", |
| 449 |
])) |
| 450 |
|
| 451 |
def testInvalidFieldType(self): |
| 452 |
fields = [ |
| 453 |
objects.QueryFieldDefinition(name="x", title="x", |
| 454 |
kind="#some#other#type"), |
| 455 |
] |
| 456 |
|
| 457 |
response = objects.QueryResponse(fields=fields, data=[]) |
| 458 |
|
| 459 |
self.assertRaises(NotImplementedError, cli.FormatQueryResult, response) |
| 460 |
|
| 461 |
def testInvalidFieldStatus(self): |
| 462 |
fields = [ |
| 463 |
objects.QueryFieldDefinition(name="x", title="x", |
| 464 |
kind=constants.QFT_TEXT), |
| 465 |
] |
| 466 |
|
| 467 |
response = objects.QueryResponse(fields=fields, data=[[(-1, None)]]) |
| 468 |
self.assertRaises(NotImplementedError, cli.FormatQueryResult, response) |
| 469 |
|
| 470 |
response = objects.QueryResponse(fields=fields, data=[[(-1, "x")]]) |
| 471 |
self.assertRaises(AssertionError, cli.FormatQueryResult, response) |
| 472 |
|
| 473 |
def testEmptyFieldTitle(self): |
| 474 |
fields = [ |
| 475 |
objects.QueryFieldDefinition(name="x", title="", |
| 476 |
kind=constants.QFT_TEXT), |
| 477 |
] |
| 478 |
|
| 479 |
response = objects.QueryResponse(fields=fields, data=[]) |
| 480 |
self.assertRaises(AssertionError, cli.FormatQueryResult, response) |
| 481 |
|
| 482 |
|
| 483 |
class _MockJobPollCb(cli.JobPollCbBase, cli.JobPollReportCbBase): |
| 484 |
def __init__(self, tc, job_id): |
| 485 |
self.tc = tc |
| 486 |
self.job_id = job_id |
| 487 |
self._wfjcr = [] |
| 488 |
self._jobstatus = [] |
| 489 |
self._expect_notchanged = False |
| 490 |
self._expect_log = [] |
| 491 |
|
| 492 |
def CheckEmpty(self): |
| 493 |
self.tc.assertFalse(self._wfjcr) |
| 494 |
self.tc.assertFalse(self._jobstatus) |
| 495 |
self.tc.assertFalse(self._expect_notchanged) |
| 496 |
self.tc.assertFalse(self._expect_log) |
| 497 |
|
| 498 |
def AddWfjcResult(self, *args): |
| 499 |
self._wfjcr.append(args) |
| 500 |
|
| 501 |
def AddQueryJobsResult(self, *args): |
| 502 |
self._jobstatus.append(args) |
| 503 |
|
| 504 |
def WaitForJobChangeOnce(self, job_id, fields, |
| 505 |
prev_job_info, prev_log_serial, |
| 506 |
timeout=constants.DEFAULT_WFJC_TIMEOUT): |
| 507 |
self.tc.assertEqual(job_id, self.job_id) |
| 508 |
self.tc.assertEqualValues(fields, ["status"]) |
| 509 |
self.tc.assertFalse(self._expect_notchanged) |
| 510 |
self.tc.assertFalse(self._expect_log) |
| 511 |
|
| 512 |
(exp_prev_job_info, exp_prev_log_serial, result) = self._wfjcr.pop(0) |
| 513 |
self.tc.assertEqualValues(prev_job_info, exp_prev_job_info) |
| 514 |
self.tc.assertEqual(prev_log_serial, exp_prev_log_serial) |
| 515 |
|
| 516 |
if result == constants.JOB_NOTCHANGED: |
| 517 |
self._expect_notchanged = True |
| 518 |
elif result: |
| 519 |
(_, logmsgs) = result |
| 520 |
if logmsgs: |
| 521 |
self._expect_log.extend(logmsgs) |
| 522 |
|
| 523 |
return result |
| 524 |
|
| 525 |
def QueryJobs(self, job_ids, fields): |
| 526 |
self.tc.assertEqual(job_ids, [self.job_id]) |
| 527 |
self.tc.assertEqualValues(fields, ["status", "opstatus", "opresult"]) |
| 528 |
self.tc.assertFalse(self._expect_notchanged) |
| 529 |
self.tc.assertFalse(self._expect_log) |
| 530 |
|
| 531 |
result = self._jobstatus.pop(0) |
| 532 |
self.tc.assertEqual(len(fields), len(result)) |
| 533 |
return [result] |
| 534 |
|
| 535 |
def CancelJob(self, job_id): |
| 536 |
self.tc.assertEqual(job_id, self.job_id) |
| 537 |
|
| 538 |
def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg): |
| 539 |
self.tc.assertEqual(job_id, self.job_id) |
| 540 |
self.tc.assertEqualValues((serial, timestamp, log_type, log_msg), |
| 541 |
self._expect_log.pop(0)) |
| 542 |
|
| 543 |
def ReportNotChanged(self, job_id, status): |
| 544 |
self.tc.assertEqual(job_id, self.job_id) |
| 545 |
self.tc.assertTrue(self._expect_notchanged) |
| 546 |
self._expect_notchanged = False |
| 547 |
|
| 548 |
|
| 549 |
class TestGenericPollJob(testutils.GanetiTestCase): |
| 550 |
def testSuccessWithLog(self): |
| 551 |
job_id = 29609 |
| 552 |
cbs = _MockJobPollCb(self, job_id) |
| 553 |
|
| 554 |
cbs.AddWfjcResult(None, None, constants.JOB_NOTCHANGED) |
| 555 |
|
| 556 |
cbs.AddWfjcResult(None, None, |
| 557 |
((constants.JOB_STATUS_QUEUED, ), None)) |
| 558 |
|
| 559 |
cbs.AddWfjcResult((constants.JOB_STATUS_QUEUED, ), None, |
| 560 |
constants.JOB_NOTCHANGED) |
| 561 |
|
| 562 |
cbs.AddWfjcResult((constants.JOB_STATUS_QUEUED, ), None, |
| 563 |
((constants.JOB_STATUS_RUNNING, ), |
| 564 |
[(1, utils.SplitTime(1273491611.0), |
| 565 |
constants.ELOG_MESSAGE, "Step 1"), |
| 566 |
(2, utils.SplitTime(1273491615.9), |
| 567 |
constants.ELOG_MESSAGE, "Step 2"), |
| 568 |
(3, utils.SplitTime(1273491625.02), |
| 569 |
constants.ELOG_MESSAGE, "Step 3"), |
| 570 |
(4, utils.SplitTime(1273491635.05), |
| 571 |
constants.ELOG_MESSAGE, "Step 4"), |
| 572 |
(37, utils.SplitTime(1273491645.0), |
| 573 |
constants.ELOG_MESSAGE, "Step 5"), |
| 574 |
(203, utils.SplitTime(127349155.0), |
| 575 |
constants.ELOG_MESSAGE, "Step 6")])) |
| 576 |
|
| 577 |
cbs.AddWfjcResult((constants.JOB_STATUS_RUNNING, ), 203, |
| 578 |
((constants.JOB_STATUS_RUNNING, ), |
| 579 |
[(300, utils.SplitTime(1273491711.01), |
| 580 |
constants.ELOG_MESSAGE, "Step X"), |
| 581 |
(302, utils.SplitTime(1273491815.8), |
| 582 |
constants.ELOG_MESSAGE, "Step Y"), |
| 583 |
(303, utils.SplitTime(1273491925.32), |
| 584 |
constants.ELOG_MESSAGE, "Step Z")])) |
| 585 |
|
| 586 |
cbs.AddWfjcResult((constants.JOB_STATUS_RUNNING, ), 303, |
| 587 |
((constants.JOB_STATUS_SUCCESS, ), None)) |
| 588 |
|
| 589 |
cbs.AddQueryJobsResult(constants.JOB_STATUS_SUCCESS, |
| 590 |
[constants.OP_STATUS_SUCCESS, |
| 591 |
constants.OP_STATUS_SUCCESS], |
| 592 |
["Hello World", "Foo man bar"]) |
| 593 |
|
| 594 |
self.assertEqual(["Hello World", "Foo man bar"], |
| 595 |
cli.GenericPollJob(job_id, cbs, cbs)) |
| 596 |
cbs.CheckEmpty() |
| 597 |
|
| 598 |
def testJobLost(self): |
| 599 |
job_id = 13746 |
| 600 |
|
| 601 |
cbs = _MockJobPollCb(self, job_id) |
| 602 |
cbs.AddWfjcResult(None, None, constants.JOB_NOTCHANGED) |
| 603 |
cbs.AddWfjcResult(None, None, None) |
| 604 |
self.assertRaises(errors.JobLost, cli.GenericPollJob, job_id, cbs, cbs) |
| 605 |
cbs.CheckEmpty() |
| 606 |
|
| 607 |
def testError(self): |
| 608 |
job_id = 31088 |
| 609 |
|
| 610 |
cbs = _MockJobPollCb(self, job_id) |
| 611 |
cbs.AddWfjcResult(None, None, constants.JOB_NOTCHANGED) |
| 612 |
cbs.AddWfjcResult(None, None, ((constants.JOB_STATUS_ERROR, ), None)) |
| 613 |
cbs.AddQueryJobsResult(constants.JOB_STATUS_ERROR, |
| 614 |
[constants.OP_STATUS_SUCCESS, |
| 615 |
constants.OP_STATUS_ERROR], |
| 616 |
["Hello World", "Error code 123"]) |
| 617 |
self.assertRaises(errors.OpExecError, cli.GenericPollJob, job_id, cbs, cbs) |
| 618 |
cbs.CheckEmpty() |
| 619 |
|
| 620 |
def testError2(self): |
| 621 |
job_id = 22235 |
| 622 |
|
| 623 |
cbs = _MockJobPollCb(self, job_id) |
| 624 |
cbs.AddWfjcResult(None, None, ((constants.JOB_STATUS_ERROR, ), None)) |
| 625 |
encexc = errors.EncodeException(errors.LockError("problem")) |
| 626 |
cbs.AddQueryJobsResult(constants.JOB_STATUS_ERROR, |
| 627 |
[constants.OP_STATUS_ERROR], [encexc]) |
| 628 |
self.assertRaises(errors.LockError, cli.GenericPollJob, job_id, cbs, cbs) |
| 629 |
cbs.CheckEmpty() |
| 630 |
|
| 631 |
def testWeirdError(self): |
| 632 |
job_id = 28847 |
| 633 |
|
| 634 |
cbs = _MockJobPollCb(self, job_id) |
| 635 |
cbs.AddWfjcResult(None, None, ((constants.JOB_STATUS_ERROR, ), None)) |
| 636 |
cbs.AddQueryJobsResult(constants.JOB_STATUS_ERROR, |
| 637 |
[constants.OP_STATUS_RUNNING, |
| 638 |
constants.OP_STATUS_RUNNING], |
| 639 |
[None, None]) |
| 640 |
self.assertRaises(errors.OpExecError, cli.GenericPollJob, job_id, cbs, cbs) |
| 641 |
cbs.CheckEmpty() |
| 642 |
|
| 643 |
def testCancel(self): |
| 644 |
job_id = 4275 |
| 645 |
|
| 646 |
cbs = _MockJobPollCb(self, job_id) |
| 647 |
cbs.AddWfjcResult(None, None, constants.JOB_NOTCHANGED) |
| 648 |
cbs.AddWfjcResult(None, None, ((constants.JOB_STATUS_CANCELING, ), None)) |
| 649 |
cbs.AddQueryJobsResult(constants.JOB_STATUS_CANCELING, |
| 650 |
[constants.OP_STATUS_CANCELING, |
| 651 |
constants.OP_STATUS_CANCELING], |
| 652 |
[None, None]) |
| 653 |
self.assertRaises(errors.JobCanceled, cli.GenericPollJob, job_id, cbs, cbs) |
| 654 |
cbs.CheckEmpty() |
| 655 |
|
| 656 |
def testNegativeUpdateFreqParameter(self): |
| 657 |
job_id = 12345 |
| 658 |
|
| 659 |
cbs = _MockJobPollCb(self, job_id) |
| 660 |
self.assertRaises(errors.ParameterError, cli.GenericPollJob, job_id, cbs, |
| 661 |
cbs, update_freq=-30) |
| 662 |
|
| 663 |
def testZeroUpdateFreqParameter(self): |
| 664 |
job_id = 12345 |
| 665 |
|
| 666 |
cbs = _MockJobPollCb(self, job_id) |
| 667 |
self.assertRaises(errors.ParameterError, cli.GenericPollJob, job_id, cbs, |
| 668 |
cbs, update_freq=0) |
| 669 |
|
| 670 |
def testShouldCancel(self): |
| 671 |
job_id = 12345 |
| 672 |
|
| 673 |
cbs = _MockJobPollCb(self, job_id) |
| 674 |
cbs.AddWfjcResult(None, None, constants.JOB_NOTCHANGED) |
| 675 |
self.assertRaises(errors.JobCanceled, cli.GenericPollJob, job_id, cbs, cbs, |
| 676 |
cancel_fn=(lambda: True)) |
| 677 |
|
| 678 |
def testIgnoreCancel(self): |
| 679 |
job_id = 12345 |
| 680 |
cbs = _MockJobPollCb(self, job_id) |
| 681 |
cbs.AddWfjcResult(None, None, ((constants.JOB_STATUS_SUCCESS, ), None)) |
| 682 |
cbs.AddQueryJobsResult(constants.JOB_STATUS_SUCCESS, |
| 683 |
[constants.OP_STATUS_SUCCESS, |
| 684 |
constants.OP_STATUS_SUCCESS], |
| 685 |
["Hello World", "Foo man bar"]) |
| 686 |
self.assertEqual(["Hello World", "Foo man bar"], |
| 687 |
cli.GenericPollJob( |
| 688 |
job_id, cbs, cbs, cancel_fn=(lambda: False))) |
| 689 |
cbs.CheckEmpty() |
| 690 |
|
| 691 |
class TestFormatLogMessage(unittest.TestCase): |
| 692 |
def test(self): |
| 693 |
self.assertEqual(cli.FormatLogMessage(constants.ELOG_MESSAGE, |
| 694 |
"Hello World"), |
| 695 |
"Hello World") |
| 696 |
self.assertRaises(TypeError, cli.FormatLogMessage, |
| 697 |
constants.ELOG_MESSAGE, [1, 2, 3]) |
| 698 |
|
| 699 |
self.assertTrue(cli.FormatLogMessage("some other type", (1, 2, 3))) |
| 700 |
|
| 701 |
|
| 702 |
class TestParseFields(unittest.TestCase): |
| 703 |
def test(self): |
| 704 |
self.assertEqual(cli.ParseFields(None, []), []) |
| 705 |
self.assertEqual(cli.ParseFields("name,foo,hello", []), |
| 706 |
["name", "foo", "hello"]) |
| 707 |
self.assertEqual(cli.ParseFields(None, ["def", "ault", "fields", "here"]), |
| 708 |
["def", "ault", "fields", "here"]) |
| 709 |
self.assertEqual(cli.ParseFields("name,foo", ["def", "ault"]), |
| 710 |
["name", "foo"]) |
| 711 |
self.assertEqual(cli.ParseFields("+name,foo", ["def", "ault"]), |
| 712 |
["def", "ault", "name", "foo"]) |
| 713 |
|
| 714 |
|
| 715 |
class TestParseNicOption(unittest.TestCase): |
| 716 |
def test(self): |
| 717 |
self.assertEqual(cli.ParseNicOption([("0", { "link": "eth0", })]), |
| 718 |
[{ "link": "eth0", }]) |
| 719 |
self.assertEqual(cli.ParseNicOption([("5", { "ip": "192.0.2.7", })]), |
| 720 |
[{}, {}, {}, {}, {}, { "ip": "192.0.2.7", }]) |
| 721 |
|
| 722 |
def testErrors(self): |
| 723 |
for i in [None, "", "abc", "zero", "Hello World", "\0", []]: |
| 724 |
self.assertRaises(errors.OpPrereqError, cli.ParseNicOption, |
| 725 |
[(i, { "link": "eth0", })]) |
| 726 |
self.assertRaises(errors.OpPrereqError, cli.ParseNicOption, |
| 727 |
[("0", i)]) |
| 728 |
|
| 729 |
self.assertRaises(errors.TypeEnforcementError, cli.ParseNicOption, |
| 730 |
[(0, { True: False, })]) |
| 731 |
|
| 732 |
self.assertRaises(errors.TypeEnforcementError, cli.ParseNicOption, |
| 733 |
[(3, { "mode": [], })]) |
| 734 |
|
| 735 |
|
| 736 |
class TestFormatResultError(unittest.TestCase): |
| 737 |
def testNormal(self): |
| 738 |
for verbose in [False, True]: |
| 739 |
self.assertRaises(AssertionError, cli.FormatResultError, |
| 740 |
constants.RS_NORMAL, verbose) |
| 741 |
|
| 742 |
def testUnknown(self): |
| 743 |
for verbose in [False, True]: |
| 744 |
self.assertRaises(NotImplementedError, cli.FormatResultError, |
| 745 |
"#some!other!status#", verbose) |
| 746 |
|
| 747 |
def test(self): |
| 748 |
for status in constants.RS_ALL: |
| 749 |
if status == constants.RS_NORMAL: |
| 750 |
continue |
| 751 |
|
| 752 |
self.assertNotEqual(cli.FormatResultError(status, False), |
| 753 |
cli.FormatResultError(status, True)) |
| 754 |
|
| 755 |
result = cli.FormatResultError(status, True) |
| 756 |
self.assertTrue(result.startswith("(")) |
| 757 |
self.assertTrue(result.endswith(")")) |
| 758 |
|
| 759 |
|
| 760 |
class TestGetOnlineNodes(unittest.TestCase): |
| 761 |
class _FakeClient: |
| 762 |
def __init__(self): |
| 763 |
self._query = [] |
| 764 |
|
| 765 |
def AddQueryResult(self, *args): |
| 766 |
self._query.append(args) |
| 767 |
|
| 768 |
def CountPending(self): |
| 769 |
return len(self._query) |
| 770 |
|
| 771 |
def Query(self, res, fields, qfilter): |
| 772 |
if res != constants.QR_NODE: |
| 773 |
raise Exception("Querying wrong resource") |
| 774 |
|
| 775 |
(exp_fields, check_filter, result) = self._query.pop(0) |
| 776 |
|
| 777 |
if exp_fields != fields: |
| 778 |
raise Exception("Expected fields %s, got %s" % (exp_fields, fields)) |
| 779 |
|
| 780 |
if not (qfilter is None or check_filter(qfilter)): |
| 781 |
raise Exception("Filter doesn't match expectations") |
| 782 |
|
| 783 |
return objects.QueryResponse(fields=None, data=result) |
| 784 |
|
| 785 |
def testEmpty(self): |
| 786 |
cl = self._FakeClient() |
| 787 |
|
| 788 |
cl.AddQueryResult(["name", "offline", "sip"], None, []) |
| 789 |
self.assertEqual(cli.GetOnlineNodes(None, cl=cl), []) |
| 790 |
self.assertEqual(cl.CountPending(), 0) |
| 791 |
|
| 792 |
def testNoSpecialFilter(self): |
| 793 |
cl = self._FakeClient() |
| 794 |
|
| 795 |
cl.AddQueryResult(["name", "offline", "sip"], None, [ |
| 796 |
[(constants.RS_NORMAL, "master.example.com"), |
| 797 |
(constants.RS_NORMAL, False), |
| 798 |
(constants.RS_NORMAL, "192.0.2.1")], |
| 799 |
[(constants.RS_NORMAL, "node2.example.com"), |
| 800 |
(constants.RS_NORMAL, False), |
| 801 |
(constants.RS_NORMAL, "192.0.2.2")], |
| 802 |
]) |
| 803 |
self.assertEqual(cli.GetOnlineNodes(None, cl=cl), |
| 804 |
["master.example.com", "node2.example.com"]) |
| 805 |
self.assertEqual(cl.CountPending(), 0) |
| 806 |
|
| 807 |
def testNoMaster(self): |
| 808 |
cl = self._FakeClient() |
| 809 |
|
| 810 |
def _CheckFilter(qfilter): |
| 811 |
self.assertEqual(qfilter, [qlang.OP_NOT, [qlang.OP_TRUE, "master"]]) |
| 812 |
return True |
| 813 |
|
| 814 |
cl.AddQueryResult(["name", "offline", "sip"], _CheckFilter, [ |
| 815 |
[(constants.RS_NORMAL, "node2.example.com"), |
| 816 |
(constants.RS_NORMAL, False), |
| 817 |
(constants.RS_NORMAL, "192.0.2.2")], |
| 818 |
]) |
| 819 |
self.assertEqual(cli.GetOnlineNodes(None, cl=cl, filter_master=True), |
| 820 |
["node2.example.com"]) |
| 821 |
self.assertEqual(cl.CountPending(), 0) |
| 822 |
|
| 823 |
def testSecondaryIpAddress(self): |
| 824 |
cl = self._FakeClient() |
| 825 |
|
| 826 |
cl.AddQueryResult(["name", "offline", "sip"], None, [ |
| 827 |
[(constants.RS_NORMAL, "master.example.com"), |
| 828 |
(constants.RS_NORMAL, False), |
| 829 |
(constants.RS_NORMAL, "192.0.2.1")], |
| 830 |
[(constants.RS_NORMAL, "node2.example.com"), |
| 831 |
(constants.RS_NORMAL, False), |
| 832 |
(constants.RS_NORMAL, "192.0.2.2")], |
| 833 |
]) |
| 834 |
self.assertEqual(cli.GetOnlineNodes(None, cl=cl, secondary_ips=True), |
| 835 |
["192.0.2.1", "192.0.2.2"]) |
| 836 |
self.assertEqual(cl.CountPending(), 0) |
| 837 |
|
| 838 |
def testNoMasterFilterNodeName(self): |
| 839 |
cl = self._FakeClient() |
| 840 |
|
| 841 |
def _CheckFilter(qfilter): |
| 842 |
self.assertEqual(qfilter, |
| 843 |
[qlang.OP_AND, |
| 844 |
[qlang.OP_OR] + [[qlang.OP_EQUAL, "name", name] |
| 845 |
for name in ["node2", "node3"]], |
| 846 |
[qlang.OP_NOT, [qlang.OP_TRUE, "master"]]]) |
| 847 |
return True |
| 848 |
|
| 849 |
cl.AddQueryResult(["name", "offline", "sip"], _CheckFilter, [ |
| 850 |
[(constants.RS_NORMAL, "node2.example.com"), |
| 851 |
(constants.RS_NORMAL, False), |
| 852 |
(constants.RS_NORMAL, "192.0.2.12")], |
| 853 |
[(constants.RS_NORMAL, "node3.example.com"), |
| 854 |
(constants.RS_NORMAL, False), |
| 855 |
(constants.RS_NORMAL, "192.0.2.13")], |
| 856 |
]) |
| 857 |
self.assertEqual(cli.GetOnlineNodes(["node2", "node3"], cl=cl, |
| 858 |
secondary_ips=True, filter_master=True), |
| 859 |
["192.0.2.12", "192.0.2.13"]) |
| 860 |
self.assertEqual(cl.CountPending(), 0) |
| 861 |
|
| 862 |
def testOfflineNodes(self): |
| 863 |
cl = self._FakeClient() |
| 864 |
|
| 865 |
cl.AddQueryResult(["name", "offline", "sip"], None, [ |
| 866 |
[(constants.RS_NORMAL, "master.example.com"), |
| 867 |
(constants.RS_NORMAL, False), |
| 868 |
(constants.RS_NORMAL, "192.0.2.1")], |
| 869 |
[(constants.RS_NORMAL, "node2.example.com"), |
| 870 |
(constants.RS_NORMAL, True), |
| 871 |
(constants.RS_NORMAL, "192.0.2.2")], |
| 872 |
[(constants.RS_NORMAL, "node3.example.com"), |
| 873 |
(constants.RS_NORMAL, True), |
| 874 |
(constants.RS_NORMAL, "192.0.2.3")], |
| 875 |
]) |
| 876 |
self.assertEqual(cli.GetOnlineNodes(None, cl=cl, nowarn=True), |
| 877 |
["master.example.com"]) |
| 878 |
self.assertEqual(cl.CountPending(), 0) |
| 879 |
|
| 880 |
def testNodeGroup(self): |
| 881 |
cl = self._FakeClient() |
| 882 |
|
| 883 |
def _CheckFilter(qfilter): |
| 884 |
self.assertEqual(qfilter, |
| 885 |
[qlang.OP_OR, [qlang.OP_EQUAL, "group", "foobar"], |
| 886 |
[qlang.OP_EQUAL, "group.uuid", "foobar"]]) |
| 887 |
return True |
| 888 |
|
| 889 |
cl.AddQueryResult(["name", "offline", "sip"], _CheckFilter, [ |
| 890 |
[(constants.RS_NORMAL, "master.example.com"), |
| 891 |
(constants.RS_NORMAL, False), |
| 892 |
(constants.RS_NORMAL, "192.0.2.1")], |
| 893 |
[(constants.RS_NORMAL, "node3.example.com"), |
| 894 |
(constants.RS_NORMAL, False), |
| 895 |
(constants.RS_NORMAL, "192.0.2.3")], |
| 896 |
]) |
| 897 |
self.assertEqual(cli.GetOnlineNodes(None, cl=cl, nodegroup="foobar"), |
| 898 |
["master.example.com", "node3.example.com"]) |
| 899 |
self.assertEqual(cl.CountPending(), 0) |
| 900 |
|
| 901 |
|
| 902 |
class TestFormatTimestamp(unittest.TestCase): |
| 903 |
def testGood(self): |
| 904 |
self.assertEqual(cli.FormatTimestamp((0, 1)), |
| 905 |
time.strftime("%F %T", time.localtime(0)) + ".000001") |
| 906 |
self.assertEqual(cli.FormatTimestamp((1332944009, 17376)), |
| 907 |
(time.strftime("%F %T", time.localtime(1332944009)) + |
| 908 |
".017376")) |
| 909 |
|
| 910 |
def testWrong(self): |
| 911 |
for i in [0, [], {}, "", [1]]: |
| 912 |
self.assertEqual(cli.FormatTimestamp(i), "?") |
| 913 |
|
| 914 |
|
| 915 |
class TestFormatUsage(unittest.TestCase): |
| 916 |
def test(self): |
| 917 |
binary = "gnt-unittest" |
| 918 |
commands = { |
| 919 |
"cmdA": |
| 920 |
(NotImplemented, NotImplemented, NotImplemented, NotImplemented, |
| 921 |
"description of A"), |
| 922 |
"bbb": |
| 923 |
(NotImplemented, NotImplemented, NotImplemented, NotImplemented, |
| 924 |
"Hello World," * 10), |
| 925 |
"longname": |
| 926 |
(NotImplemented, NotImplemented, NotImplemented, NotImplemented, |
| 927 |
"Another description"), |
| 928 |
} |
| 929 |
|
| 930 |
self.assertEqual(list(cli._FormatUsage(binary, commands)), [ |
| 931 |
"Usage: gnt-unittest {command} [options...] [argument...]", |
| 932 |
"gnt-unittest <command> --help to see details, or man gnt-unittest", |
| 933 |
"", |
| 934 |
"Commands:", |
| 935 |
(" bbb - Hello World,Hello World,Hello World,Hello World,Hello" |
| 936 |
" World,Hello"), |
| 937 |
" World,Hello World,Hello World,Hello World,Hello World,", |
| 938 |
" cmdA - description of A", |
| 939 |
" longname - Another description", |
| 940 |
"", |
| 941 |
]) |
| 942 |
|
| 943 |
|
| 944 |
class TestParseArgs(unittest.TestCase): |
| 945 |
def testNoArguments(self): |
| 946 |
for argv in [[], ["gnt-unittest"]]: |
| 947 |
try: |
| 948 |
cli._ParseArgs("gnt-unittest", argv, {}, {}, set()) |
| 949 |
except cli._ShowUsage as err: |
| 950 |
self.assertTrue(err.exit_error) |
| 951 |
else: |
| 952 |
self.fail("Did not raise exception") |
| 953 |
|
| 954 |
def testVersion(self): |
| 955 |
for argv in [["test", "--version"], ["test", "--version", "somethingelse"]]: |
| 956 |
try: |
| 957 |
cli._ParseArgs("test", argv, {}, {}, set()) |
| 958 |
except cli._ShowVersion: |
| 959 |
pass |
| 960 |
else: |
| 961 |
self.fail("Did not raise exception") |
| 962 |
|
| 963 |
def testHelp(self): |
| 964 |
for argv in [["test", "--help"], ["test", "--help", "somethingelse"]]: |
| 965 |
try: |
| 966 |
cli._ParseArgs("test", argv, {}, {}, set()) |
| 967 |
except cli._ShowUsage as err: |
| 968 |
self.assertFalse(err.exit_error) |
| 969 |
else: |
| 970 |
self.fail("Did not raise exception") |
| 971 |
|
| 972 |
def testUnknownCommandOrAlias(self): |
| 973 |
for argv in [["test", "list"], ["test", "somethingelse", "--help"]]: |
| 974 |
try: |
| 975 |
cli._ParseArgs("test", argv, {}, {}, set()) |
| 976 |
except cli._ShowUsage as err: |
| 977 |
self.assertTrue(err.exit_error) |
| 978 |
else: |
| 979 |
self.fail("Did not raise exception") |
| 980 |
|
| 981 |
def testInvalidAliasList(self): |
| 982 |
cmd = { |
| 983 |
"list": NotImplemented, |
| 984 |
"foo": NotImplemented, |
| 985 |
} |
| 986 |
aliases = { |
| 987 |
"list": NotImplemented, |
| 988 |
"foo": NotImplemented, |
| 989 |
} |
| 990 |
assert sorted(cmd.keys()) == sorted(aliases.keys()) |
| 991 |
self.assertRaises(AssertionError, cli._ParseArgs, "test", |
| 992 |
["test", "list"], cmd, aliases, set()) |
| 993 |
|
| 994 |
def testAliasForNonExistantCommand(self): |
| 995 |
cmd = {} |
| 996 |
aliases = { |
| 997 |
"list": NotImplemented, |
| 998 |
} |
| 999 |
self.assertRaises(errors.ProgrammerError, cli._ParseArgs, "test", |
| 1000 |
["test", "list"], cmd, aliases, set()) |
| 1001 |
|
| 1002 |
|
| 1003 |
class TestQftNames(unittest.TestCase): |
| 1004 |
def testComplete(self): |
| 1005 |
self.assertEqual(frozenset(cli._QFT_NAMES), constants.QFT_ALL) |
| 1006 |
|
| 1007 |
def testUnique(self): |
| 1008 |
lcnames = [s.lower() for s in cli._QFT_NAMES.values()] |
| 1009 |
self.assertFalse(utils.FindDuplicates(lcnames)) |
| 1010 |
|
| 1011 |
def testUppercase(self): |
| 1012 |
for name in cli._QFT_NAMES.values(): |
| 1013 |
self.assertEqual(name[0], name[0].upper()) |
| 1014 |
|
| 1015 |
|
| 1016 |
class TestFieldDescValues(unittest.TestCase): |
| 1017 |
def testKnownKind(self): |
| 1018 |
fdef = objects.QueryFieldDefinition(name="aname", |
| 1019 |
title="Atitle", |
| 1020 |
kind=constants.QFT_TEXT, |
| 1021 |
doc="aaa doc aaa") |
| 1022 |
self.assertEqual(cli._FieldDescValues(fdef), |
| 1023 |
["aname", "Text", "Atitle", "aaa doc aaa"]) |
| 1024 |
|
| 1025 |
def testUnknownKind(self): |
| 1026 |
kind = "#foo#" |
| 1027 |
|
| 1028 |
self.assertFalse(kind in constants.QFT_ALL) |
| 1029 |
self.assertFalse(kind in cli._QFT_NAMES) |
| 1030 |
|
| 1031 |
fdef = objects.QueryFieldDefinition(name="zname", title="Ztitle", |
| 1032 |
kind=kind, doc="zzz doc zzz") |
| 1033 |
self.assertEqual(cli._FieldDescValues(fdef), |
| 1034 |
["zname", kind, "Ztitle", "zzz doc zzz"]) |
| 1035 |
|
| 1036 |
|
| 1037 |
class TestSerializeGenericInfo(unittest.TestCase): |
| 1038 |
"""Test case for cli._SerializeGenericInfo""" |
| 1039 |
def _RunTest(self, data, expected): |
| 1040 |
buf = StringIO() |
| 1041 |
cli._SerializeGenericInfo(buf, data, 0) |
| 1042 |
self.assertEqual(buf.getvalue(), expected) |
| 1043 |
|
| 1044 |
def testSimple(self): |
| 1045 |
test_samples = [ |
| 1046 |
("abc", "abc\n"), |
| 1047 |
([], "\n"), |
| 1048 |
({}, "\n"), |
| 1049 |
(["1", "2", "3"], "- 1\n- 2\n- 3\n"), |
| 1050 |
([("z", "26")], "z: 26\n"), |
| 1051 |
({"z": "26"}, "z: 26\n"), |
| 1052 |
([("z", "26"), ("a", "1")], "z: 26\na: 1\n"), |
| 1053 |
({"z": "26", "a": "1"}, "a: 1\nz: 26\n"), |
| 1054 |
] |
| 1055 |
for (data, expected) in test_samples: |
| 1056 |
self._RunTest(data, expected) |
| 1057 |
|
| 1058 |
def testLists(self): |
| 1059 |
adict = { |
| 1060 |
"aa": "11", |
| 1061 |
"bb": "22", |
| 1062 |
"cc": "33", |
| 1063 |
} |
| 1064 |
adict_exp = ("- aa: 11\n" |
| 1065 |
" bb: 22\n" |
| 1066 |
" cc: 33\n") |
| 1067 |
anobj = [ |
| 1068 |
("zz", "11"), |
| 1069 |
("ww", "33"), |
| 1070 |
("xx", "22"), |
| 1071 |
] |
| 1072 |
anobj_exp = ("- zz: 11\n" |
| 1073 |
" ww: 33\n" |
| 1074 |
" xx: 22\n") |
| 1075 |
alist = ["aa", "cc", "bb"] |
| 1076 |
alist_exp = ("- - aa\n" |
| 1077 |
" - cc\n" |
| 1078 |
" - bb\n") |
| 1079 |
test_samples = [ |
| 1080 |
(adict, adict_exp), |
| 1081 |
(anobj, anobj_exp), |
| 1082 |
(alist, alist_exp), |
| 1083 |
] |
| 1084 |
for (base_data, base_expected) in test_samples: |
| 1085 |
for k in range(1, 4): |
| 1086 |
data = k * [base_data] |
| 1087 |
expected = k * base_expected |
| 1088 |
self._RunTest(data, expected) |
| 1089 |
|
| 1090 |
def testDictionaries(self): |
| 1091 |
data = [ |
| 1092 |
("aaa", ["x", "y"]), |
| 1093 |
("bbb", { |
| 1094 |
"w": "1", |
| 1095 |
"z": "2", |
| 1096 |
}), |
| 1097 |
("ccc", [ |
| 1098 |
("xyz", "123"), |
| 1099 |
("efg", "456"), |
| 1100 |
]), |
| 1101 |
] |
| 1102 |
expected = ("aaa: \n" |
| 1103 |
" - x\n" |
| 1104 |
" - y\n" |
| 1105 |
"bbb: \n" |
| 1106 |
" w: 1\n" |
| 1107 |
" z: 2\n" |
| 1108 |
"ccc: \n" |
| 1109 |
" xyz: 123\n" |
| 1110 |
" efg: 456\n") |
| 1111 |
self._RunTest(data, expected) |
| 1112 |
self._RunTest(dict(data), expected) |
| 1113 |
|
| 1114 |
|
| 1115 |
class TestFormatPolicyInfo(unittest.TestCase): |
| 1116 |
"""Test case for cli.FormatPolicyInfo. |
| 1117 |
|
| 1118 |
These tests rely on cli._SerializeGenericInfo (tested elsewhere). |
| 1119 |
|
| 1120 |
""" |
| 1121 |
def setUp(self): |
| 1122 |
|
| 1123 |
self.maxDiff = None |
| 1124 |
|
| 1125 |
def _RenameDictItem(self, parsed, old, new): |
| 1126 |
self.assertTrue(old in parsed) |
| 1127 |
self.assertTrue(new not in parsed) |
| 1128 |
parsed[new] = parsed[old] |
| 1129 |
del parsed[old] |
| 1130 |
|
| 1131 |
def _TranslateParsedNames(self, parsed): |
| 1132 |
for (pretty, raw) in [ |
| 1133 |
("bounds specs", constants.ISPECS_MINMAX), |
| 1134 |
("allowed disk templates", constants.IPOLICY_DTS) |
| 1135 |
]: |
| 1136 |
self._RenameDictItem(parsed, pretty, raw) |
| 1137 |
for minmax in parsed[constants.ISPECS_MINMAX]: |
| 1138 |
for key in set(minmax.keys()): |
| 1139 |
keyparts = key.split("/", 1) |
| 1140 |
if len(keyparts) > 1: |
| 1141 |
self._RenameDictItem(minmax, key, keyparts[0]) |
| 1142 |
self.assertTrue(constants.IPOLICY_DTS in parsed) |
| 1143 |
parsed[constants.IPOLICY_DTS] = yaml.load("[%s]" % |
| 1144 |
parsed[constants.IPOLICY_DTS], |
| 1145 |
Loader=yaml.SafeLoader) |
| 1146 |
|
| 1147 |
@staticmethod |
| 1148 |
def _PrintAndParsePolicy(custom, effective, iscluster): |
| 1149 |
formatted = cli.FormatPolicyInfo(custom, effective, iscluster) |
| 1150 |
buf = StringIO() |
| 1151 |
cli._SerializeGenericInfo(buf, formatted, 0) |
| 1152 |
return yaml.load(buf.getvalue(), Loader=yaml.SafeLoader) |
| 1153 |
|
| 1154 |
def _PrintAndCheckParsed(self, policy): |
| 1155 |
parsed = self._PrintAndParsePolicy(policy, NotImplemented, True) |
| 1156 |
self._TranslateParsedNames(parsed) |
| 1157 |
self.assertEqual(parsed, policy) |
| 1158 |
|
| 1159 |
def _CompareClusterGroupItems(self, cluster, group, skip=None): |
| 1160 |
if isinstance(group, dict): |
| 1161 |
self.assertTrue(isinstance(cluster, dict)) |
| 1162 |
if skip is None: |
| 1163 |
skip = frozenset() |
| 1164 |
self.assertEqual(frozenset(cluster).difference(skip), |
| 1165 |
frozenset(group)) |
| 1166 |
for key in group: |
| 1167 |
self._CompareClusterGroupItems(cluster[key], group[key]) |
| 1168 |
elif isinstance(group, list): |
| 1169 |
self.assertTrue(isinstance(cluster, list)) |
| 1170 |
self.assertEqual(len(cluster), len(group)) |
| 1171 |
for (cval, gval) in zip(cluster, group): |
| 1172 |
self._CompareClusterGroupItems(cval, gval) |
| 1173 |
else: |
| 1174 |
self.assertTrue(isinstance(group, str)) |
| 1175 |
self.assertEqual("default (%s)" % cluster, group) |
| 1176 |
|
| 1177 |
def _TestClusterVsGroup(self, policy): |
| 1178 |
cluster = self._PrintAndParsePolicy(policy, NotImplemented, True) |
| 1179 |
group = self._PrintAndParsePolicy({}, policy, False) |
| 1180 |
self._CompareClusterGroupItems(cluster, group, ["std"]) |
| 1181 |
|
| 1182 |
def testWithDefaults(self): |
| 1183 |
self._PrintAndCheckParsed(constants.IPOLICY_DEFAULTS) |
| 1184 |
self._TestClusterVsGroup(constants.IPOLICY_DEFAULTS) |
| 1185 |
|
| 1186 |
|
| 1187 |
class TestCreateIPolicyFromOpts(unittest.TestCase): |
| 1188 |
"""Test case for cli.CreateIPolicyFromOpts.""" |
| 1189 |
def setUp(self): |
| 1190 |
|
| 1191 |
self.maxDiff = None |
| 1192 |
|
| 1193 |
def _RecursiveCheckMergedDicts(self, default_pol, diff_pol, merged_pol, |
| 1194 |
merge_minmax=False): |
| 1195 |
self.assertTrue(type(default_pol) is dict) |
| 1196 |
self.assertTrue(type(diff_pol) is dict) |
| 1197 |
self.assertTrue(type(merged_pol) is dict) |
| 1198 |
self.assertEqual(frozenset(default_pol), |
| 1199 |
frozenset(merged_pol)) |
| 1200 |
for (key, val) in merged_pol.items(): |
| 1201 |
if key in diff_pol: |
| 1202 |
if type(val) is dict: |
| 1203 |
self._RecursiveCheckMergedDicts(default_pol[key], diff_pol[key], val) |
| 1204 |
elif (merge_minmax and key == "minmax" and type(val) is list and |
| 1205 |
len(val) == 1): |
| 1206 |
self.assertEqual(len(default_pol[key]), 1) |
| 1207 |
self.assertEqual(len(diff_pol[key]), 1) |
| 1208 |
self._RecursiveCheckMergedDicts(default_pol[key][0], |
| 1209 |
diff_pol[key][0], val[0]) |
| 1210 |
else: |
| 1211 |
self.assertEqual(val, diff_pol[key]) |
| 1212 |
else: |
| 1213 |
self.assertEqual(val, default_pol[key]) |
| 1214 |
|
| 1215 |
def testClusterPolicy(self): |
| 1216 |
pol0 = cli.CreateIPolicyFromOpts( |
| 1217 |
ispecs_mem_size={}, |
| 1218 |
ispecs_cpu_count={}, |
| 1219 |
ispecs_disk_count={}, |
| 1220 |
ispecs_disk_size={}, |
| 1221 |
ispecs_nic_count={}, |
| 1222 |
ipolicy_disk_templates=None, |
| 1223 |
ipolicy_vcpu_ratio=None, |
| 1224 |
ipolicy_spindle_ratio=None, |
| 1225 |
fill_all=True |
| 1226 |
) |
| 1227 |
self.assertEqual(pol0, constants.IPOLICY_DEFAULTS) |
| 1228 |
|
| 1229 |
exp_pol1 = { |
| 1230 |
constants.ISPECS_MINMAX: [ |
| 1231 |
{ |
| 1232 |
constants.ISPECS_MIN: { |
| 1233 |
constants.ISPEC_CPU_COUNT: 2, |
| 1234 |
constants.ISPEC_DISK_COUNT: 1, |
| 1235 |
}, |
| 1236 |
constants.ISPECS_MAX: { |
| 1237 |
constants.ISPEC_MEM_SIZE: 12*1024, |
| 1238 |
constants.ISPEC_DISK_COUNT: 2, |
| 1239 |
}, |
| 1240 |
}, |
| 1241 |
], |
| 1242 |
constants.ISPECS_STD: { |
| 1243 |
constants.ISPEC_CPU_COUNT: 2, |
| 1244 |
constants.ISPEC_DISK_COUNT: 2, |
| 1245 |
}, |
| 1246 |
constants.IPOLICY_VCPU_RATIO: 3.1, |
| 1247 |
} |
| 1248 |
pol1 = cli.CreateIPolicyFromOpts( |
| 1249 |
ispecs_mem_size={"max": "12g"}, |
| 1250 |
ispecs_cpu_count={"min": 2, "std": 2}, |
| 1251 |
ispecs_disk_count={"min": 1, "max": 2, "std": 2}, |
| 1252 |
ispecs_disk_size={}, |
| 1253 |
ispecs_nic_count={}, |
| 1254 |
ipolicy_disk_templates=None, |
| 1255 |
ipolicy_vcpu_ratio=3.1, |
| 1256 |
ipolicy_spindle_ratio=None, |
| 1257 |
fill_all=True |
| 1258 |
) |
| 1259 |
self._RecursiveCheckMergedDicts(constants.IPOLICY_DEFAULTS, |
| 1260 |
exp_pol1, pol1, merge_minmax=True) |
| 1261 |
|
| 1262 |
exp_pol2 = { |
| 1263 |
constants.ISPECS_MINMAX: [ |
| 1264 |
{ |
| 1265 |
constants.ISPECS_MIN: { |
| 1266 |
constants.ISPEC_DISK_SIZE: 512, |
| 1267 |
constants.ISPEC_NIC_COUNT: 2, |
| 1268 |
}, |
| 1269 |
constants.ISPECS_MAX: { |
| 1270 |
constants.ISPEC_NIC_COUNT: 3, |
| 1271 |
}, |
| 1272 |
}, |
| 1273 |
], |
| 1274 |
constants.ISPECS_STD: { |
| 1275 |
constants.ISPEC_CPU_COUNT: 2, |
| 1276 |
constants.ISPEC_NIC_COUNT: 3, |
| 1277 |
}, |
| 1278 |
constants.IPOLICY_SPINDLE_RATIO: 1.3, |
| 1279 |
constants.IPOLICY_DTS: ["templates"], |
| 1280 |
} |
| 1281 |
pol2 = cli.CreateIPolicyFromOpts( |
| 1282 |
ispecs_mem_size={}, |
| 1283 |
ispecs_cpu_count={"std": 2}, |
| 1284 |
ispecs_disk_count={}, |
| 1285 |
ispecs_disk_size={"min": "0.5g"}, |
| 1286 |
ispecs_nic_count={"min": 2, "max": 3, "std": 3}, |
| 1287 |
ipolicy_disk_templates=["templates"], |
| 1288 |
ipolicy_vcpu_ratio=None, |
| 1289 |
ipolicy_spindle_ratio=1.3, |
| 1290 |
fill_all=True |
| 1291 |
) |
| 1292 |
self._RecursiveCheckMergedDicts(constants.IPOLICY_DEFAULTS, |
| 1293 |
exp_pol2, pol2, merge_minmax=True) |
| 1294 |
|
| 1295 |
for fill_all in [False, True]: |
| 1296 |
exp_pol3 = { |
| 1297 |
constants.ISPECS_STD: { |
| 1298 |
constants.ISPEC_CPU_COUNT: 2, |
| 1299 |
constants.ISPEC_NIC_COUNT: 3, |
| 1300 |
}, |
| 1301 |
} |
| 1302 |
pol3 = cli.CreateIPolicyFromOpts( |
| 1303 |
std_ispecs={ |
| 1304 |
constants.ISPEC_CPU_COUNT: "2", |
| 1305 |
constants.ISPEC_NIC_COUNT: "3", |
| 1306 |
}, |
| 1307 |
ipolicy_disk_templates=None, |
| 1308 |
ipolicy_vcpu_ratio=None, |
| 1309 |
ipolicy_spindle_ratio=None, |
| 1310 |
fill_all=fill_all |
| 1311 |
) |
| 1312 |
if fill_all: |
| 1313 |
self._RecursiveCheckMergedDicts(constants.IPOLICY_DEFAULTS, |
| 1314 |
exp_pol3, pol3, merge_minmax=True) |
| 1315 |
else: |
| 1316 |
self.assertEqual(pol3, exp_pol3) |
| 1317 |
|
| 1318 |
def testPartialPolicy(self): |
| 1319 |
exp_pol0 = objects.MakeEmptyIPolicy() |
| 1320 |
pol0 = cli.CreateIPolicyFromOpts( |
| 1321 |
minmax_ispecs=None, |
| 1322 |
std_ispecs=None, |
| 1323 |
ipolicy_disk_templates=None, |
| 1324 |
ipolicy_vcpu_ratio=None, |
| 1325 |
ipolicy_spindle_ratio=None, |
| 1326 |
fill_all=False |
| 1327 |
) |
| 1328 |
self.assertEqual(pol0, exp_pol0) |
| 1329 |
|
| 1330 |
exp_pol1 = { |
| 1331 |
constants.IPOLICY_VCPU_RATIO: 3.1, |
| 1332 |
} |
| 1333 |
pol1 = cli.CreateIPolicyFromOpts( |
| 1334 |
minmax_ispecs=None, |
| 1335 |
std_ispecs=None, |
| 1336 |
ipolicy_disk_templates=None, |
| 1337 |
ipolicy_vcpu_ratio=3.1, |
| 1338 |
ipolicy_spindle_ratio=None, |
| 1339 |
fill_all=False |
| 1340 |
) |
| 1341 |
self.assertEqual(pol1, exp_pol1) |
| 1342 |
|
| 1343 |
exp_pol2 = { |
| 1344 |
constants.IPOLICY_SPINDLE_RATIO: 1.3, |
| 1345 |
constants.IPOLICY_DTS: ["templates"], |
| 1346 |
} |
| 1347 |
pol2 = cli.CreateIPolicyFromOpts( |
| 1348 |
minmax_ispecs=None, |
| 1349 |
std_ispecs=None, |
| 1350 |
ipolicy_disk_templates=["templates"], |
| 1351 |
ipolicy_vcpu_ratio=None, |
| 1352 |
ipolicy_spindle_ratio=1.3, |
| 1353 |
fill_all=False |
| 1354 |
) |
| 1355 |
self.assertEqual(pol2, exp_pol2) |
| 1356 |
|
| 1357 |
def _TestInvalidISpecs(self, minmax_ispecs, std_ispecs, fail=True): |
| 1358 |
for fill_all in [False, True]: |
| 1359 |
if fail: |
| 1360 |
self.assertRaises((errors.OpPrereqError, |
| 1361 |
errors.UnitParseError, |
| 1362 |
errors.TypeEnforcementError), |
| 1363 |
cli.CreateIPolicyFromOpts, |
| 1364 |
minmax_ispecs=minmax_ispecs, |
| 1365 |
std_ispecs=std_ispecs, |
| 1366 |
fill_all=fill_all) |
| 1367 |
else: |
| 1368 |
cli.CreateIPolicyFromOpts(minmax_ispecs=minmax_ispecs, |
| 1369 |
std_ispecs=std_ispecs, |
| 1370 |
fill_all=fill_all) |
| 1371 |
|
| 1372 |
def testInvalidPolicies(self): |
| 1373 |
self.assertRaises(AssertionError, cli.CreateIPolicyFromOpts, |
| 1374 |
std_ispecs={constants.ISPEC_MEM_SIZE: 1024}, |
| 1375 |
ipolicy_disk_templates=None, ipolicy_vcpu_ratio=None, |
| 1376 |
ipolicy_spindle_ratio=None, group_ipolicy=True) |
| 1377 |
self.assertRaises(errors.OpPrereqError, cli.CreateIPolicyFromOpts, |
| 1378 |
ispecs_mem_size={"wrong": "x"}, ispecs_cpu_count={}, |
| 1379 |
ispecs_disk_count={}, ispecs_disk_size={}, |
| 1380 |
ispecs_nic_count={}, ipolicy_disk_templates=None, |
| 1381 |
ipolicy_vcpu_ratio=None, ipolicy_spindle_ratio=None, |
| 1382 |
fill_all=True) |
| 1383 |
self.assertRaises(errors.TypeEnforcementError, cli.CreateIPolicyFromOpts, |
| 1384 |
ispecs_mem_size={}, ispecs_cpu_count={"min": "default"}, |
| 1385 |
ispecs_disk_count={}, ispecs_disk_size={}, |
| 1386 |
ispecs_nic_count={}, ipolicy_disk_templates=None, |
| 1387 |
ipolicy_vcpu_ratio=None, ipolicy_spindle_ratio=None, |
| 1388 |
fill_all=True) |
| 1389 |
|
| 1390 |
good_mmspecs = [ |
| 1391 |
constants.ISPECS_MINMAX_DEFAULTS, |
| 1392 |
constants.ISPECS_MINMAX_DEFAULTS, |
| 1393 |
] |
| 1394 |
self._TestInvalidISpecs(good_mmspecs, None, fail=False) |
| 1395 |
broken_mmspecs = copy.deepcopy(good_mmspecs) |
| 1396 |
for minmaxpair in broken_mmspecs: |
| 1397 |
for key in constants.ISPECS_MINMAX_KEYS: |
| 1398 |
for par in constants.ISPECS_PARAMETERS: |
| 1399 |
old = minmaxpair[key][par] |
| 1400 |
del minmaxpair[key][par] |
| 1401 |
self._TestInvalidISpecs(broken_mmspecs, None) |
| 1402 |
minmaxpair[key][par] = "invalid" |
| 1403 |
self._TestInvalidISpecs(broken_mmspecs, None) |
| 1404 |
minmaxpair[key][par] = old |
| 1405 |
minmaxpair[key]["invalid_key"] = None |
| 1406 |
self._TestInvalidISpecs(broken_mmspecs, None) |
| 1407 |
del minmaxpair[key]["invalid_key"] |
| 1408 |
minmaxpair["invalid_key"] = None |
| 1409 |
self._TestInvalidISpecs(broken_mmspecs, None) |
| 1410 |
del minmaxpair["invalid_key"] |
| 1411 |
assert broken_mmspecs == good_mmspecs |
| 1412 |
|
| 1413 |
good_stdspecs = constants.IPOLICY_DEFAULTS[constants.ISPECS_STD] |
| 1414 |
self._TestInvalidISpecs(None, good_stdspecs, fail=False) |
| 1415 |
broken_stdspecs = copy.deepcopy(good_stdspecs) |
| 1416 |
for par in constants.ISPECS_PARAMETERS: |
| 1417 |
old = broken_stdspecs[par] |
| 1418 |
broken_stdspecs[par] = "invalid" |
| 1419 |
self._TestInvalidISpecs(None, broken_stdspecs) |
| 1420 |
broken_stdspecs[par] = old |
| 1421 |
broken_stdspecs["invalid_key"] = None |
| 1422 |
self._TestInvalidISpecs(None, broken_stdspecs) |
| 1423 |
del broken_stdspecs["invalid_key"] |
| 1424 |
assert broken_stdspecs == good_stdspecs |
| 1425 |
|
| 1426 |
def testAllowedValues(self): |
| 1427 |
allowedv = "blah" |
| 1428 |
exp_pol1 = { |
| 1429 |
constants.ISPECS_MINMAX: allowedv, |
| 1430 |
constants.IPOLICY_DTS: allowedv, |
| 1431 |
constants.IPOLICY_VCPU_RATIO: allowedv, |
| 1432 |
constants.IPOLICY_SPINDLE_RATIO: allowedv, |
| 1433 |
} |
| 1434 |
pol1 = cli.CreateIPolicyFromOpts(minmax_ispecs=[{allowedv: {}}], |
| 1435 |
std_ispecs=None, |
| 1436 |
ipolicy_disk_templates=allowedv, |
| 1437 |
ipolicy_vcpu_ratio=allowedv, |
| 1438 |
ipolicy_spindle_ratio=allowedv, |
| 1439 |
allowed_values=[allowedv]) |
| 1440 |
self.assertEqual(pol1, exp_pol1) |
| 1441 |
|
| 1442 |
@staticmethod |
| 1443 |
def _ConvertSpecToStrings(spec): |
| 1444 |
ret = {} |
| 1445 |
for (par, val) in spec.items(): |
| 1446 |
ret[par] = str(val) |
| 1447 |
return ret |
| 1448 |
|
| 1449 |
def _CheckNewStyleSpecsCall(self, exp_ipolicy, minmax_ispecs, std_ispecs, |
| 1450 |
group_ipolicy, fill_all): |
| 1451 |
ipolicy = cli.CreateIPolicyFromOpts(minmax_ispecs=minmax_ispecs, |
| 1452 |
std_ispecs=std_ispecs, |
| 1453 |
group_ipolicy=group_ipolicy, |
| 1454 |
fill_all=fill_all) |
| 1455 |
self.assertEqual(ipolicy, exp_ipolicy) |
| 1456 |
|
| 1457 |
def _TestFullISpecsInner(self, skel_exp_ipol, exp_minmax, exp_std, |
| 1458 |
group_ipolicy, fill_all): |
| 1459 |
exp_ipol = skel_exp_ipol.copy() |
| 1460 |
if exp_minmax is not None: |
| 1461 |
minmax_ispecs = [] |
| 1462 |
for exp_mm_pair in exp_minmax: |
| 1463 |
mmpair = {} |
| 1464 |
for (key, spec) in exp_mm_pair.items(): |
| 1465 |
mmpair[key] = self._ConvertSpecToStrings(spec) |
| 1466 |
minmax_ispecs.append(mmpair) |
| 1467 |
exp_ipol[constants.ISPECS_MINMAX] = exp_minmax |
| 1468 |
else: |
| 1469 |
minmax_ispecs = None |
| 1470 |
if exp_std is not None: |
| 1471 |
std_ispecs = self._ConvertSpecToStrings(exp_std) |
| 1472 |
exp_ipol[constants.ISPECS_STD] = exp_std |
| 1473 |
else: |
| 1474 |
std_ispecs = None |
| 1475 |
|
| 1476 |
self._CheckNewStyleSpecsCall(exp_ipol, minmax_ispecs, std_ispecs, |
| 1477 |
group_ipolicy, fill_all) |
| 1478 |
if minmax_ispecs: |
| 1479 |
for mmpair in minmax_ispecs: |
| 1480 |
for (key, spec) in mmpair.items(): |
| 1481 |
for par in [constants.ISPEC_MEM_SIZE, constants.ISPEC_DISK_SIZE]: |
| 1482 |
if par in spec: |
| 1483 |
spec[par] += "m" |
| 1484 |
self._CheckNewStyleSpecsCall(exp_ipol, minmax_ispecs, std_ispecs, |
| 1485 |
group_ipolicy, fill_all) |
| 1486 |
if std_ispecs: |
| 1487 |
for par in [constants.ISPEC_MEM_SIZE, constants.ISPEC_DISK_SIZE]: |
| 1488 |
if par in std_ispecs: |
| 1489 |
std_ispecs[par] += "m" |
| 1490 |
self._CheckNewStyleSpecsCall(exp_ipol, minmax_ispecs, std_ispecs, |
| 1491 |
group_ipolicy, fill_all) |
| 1492 |
|
| 1493 |
def testFullISpecs(self): |
| 1494 |
exp_minmax1 = [ |
| 1495 |
{ |
| 1496 |
constants.ISPECS_MIN: { |
| 1497 |
constants.ISPEC_MEM_SIZE: 512, |
| 1498 |
constants.ISPEC_CPU_COUNT: 2, |
| 1499 |
constants.ISPEC_DISK_COUNT: 2, |
| 1500 |
constants.ISPEC_DISK_SIZE: 512, |
| 1501 |
constants.ISPEC_NIC_COUNT: 2, |
| 1502 |
constants.ISPEC_SPINDLE_USE: 2, |
| 1503 |
}, |
| 1504 |
constants.ISPECS_MAX: { |
| 1505 |
constants.ISPEC_MEM_SIZE: 768*1024, |
| 1506 |
constants.ISPEC_CPU_COUNT: 7, |
| 1507 |
constants.ISPEC_DISK_COUNT: 6, |
| 1508 |
constants.ISPEC_DISK_SIZE: 2048*1024, |
| 1509 |
constants.ISPEC_NIC_COUNT: 3, |
| 1510 |
constants.ISPEC_SPINDLE_USE: 3, |
| 1511 |
}, |
| 1512 |
}, |
| 1513 |
] |
| 1514 |
exp_minmax2 = [ |
| 1515 |
{ |
| 1516 |
constants.ISPECS_MIN: { |
| 1517 |
constants.ISPEC_MEM_SIZE: 512, |
| 1518 |
constants.ISPEC_CPU_COUNT: 2, |
| 1519 |
constants.ISPEC_DISK_COUNT: 2, |
| 1520 |
constants.ISPEC_DISK_SIZE: 512, |
| 1521 |
constants.ISPEC_NIC_COUNT: 2, |
| 1522 |
constants.ISPEC_SPINDLE_USE: 2, |
| 1523 |
}, |
| 1524 |
constants.ISPECS_MAX: { |
| 1525 |
constants.ISPEC_MEM_SIZE: 768*1024, |
| 1526 |
constants.ISPEC_CPU_COUNT: 7, |
| 1527 |
constants.ISPEC_DISK_COUNT: 6, |
| 1528 |
constants.ISPEC_DISK_SIZE: 2048*1024, |
| 1529 |
constants.ISPEC_NIC_COUNT: 3, |
| 1530 |
constants.ISPEC_SPINDLE_USE: 3, |
| 1531 |
}, |
| 1532 |
}, |
| 1533 |
{ |
| 1534 |
constants.ISPECS_MIN: { |
| 1535 |
constants.ISPEC_MEM_SIZE: 1024*1024, |
| 1536 |
constants.ISPEC_CPU_COUNT: 3, |
| 1537 |
constants.ISPEC_DISK_COUNT: 3, |
| 1538 |
constants.ISPEC_DISK_SIZE: 256, |
| 1539 |
constants.ISPEC_NIC_COUNT: 4, |
| 1540 |
constants.ISPEC_SPINDLE_USE: 5, |
| 1541 |
}, |
| 1542 |
constants.ISPECS_MAX: { |
| 1543 |
constants.ISPEC_MEM_SIZE: 2048*1024, |
| 1544 |
constants.ISPEC_CPU_COUNT: 5, |
| 1545 |
constants.ISPEC_DISK_COUNT: 5, |
| 1546 |
constants.ISPEC_DISK_SIZE: 1024*1024, |
| 1547 |
constants.ISPEC_NIC_COUNT: 5, |
| 1548 |
constants.ISPEC_SPINDLE_USE: 7, |
| 1549 |
}, |
| 1550 |
}, |
| 1551 |
] |
| 1552 |
exp_std1 = { |
| 1553 |
constants.ISPEC_MEM_SIZE: 768*1024, |
| 1554 |
constants.ISPEC_CPU_COUNT: 7, |
| 1555 |
constants.ISPEC_DISK_COUNT: 6, |
| 1556 |
constants.ISPEC_DISK_SIZE: 2048*1024, |
| 1557 |
constants.ISPEC_NIC_COUNT: 3, |
| 1558 |
constants.ISPEC_SPINDLE_USE: 1, |
| 1559 |
} |
| 1560 |
for fill_all in [False, True]: |
| 1561 |
if fill_all: |
| 1562 |
skel_ipolicy = constants.IPOLICY_DEFAULTS |
| 1563 |
else: |
| 1564 |
skel_ipolicy = {} |
| 1565 |
self._TestFullISpecsInner(skel_ipolicy, None, exp_std1, |
| 1566 |
False, fill_all) |
| 1567 |
for exp_minmax in [exp_minmax1, exp_minmax2]: |
| 1568 |
self._TestFullISpecsInner(skel_ipolicy, exp_minmax, exp_std1, |
| 1569 |
False, fill_all) |
| 1570 |
self._TestFullISpecsInner(skel_ipolicy, exp_minmax, None, |
| 1571 |
False, fill_all) |
| 1572 |
|
| 1573 |
|
| 1574 |
class TestPrintIPolicyCommand(unittest.TestCase): |
| 1575 |
"""Test case for cli.PrintIPolicyCommand""" |
| 1576 |
_SPECS1 = { |
| 1577 |
"par1": 42, |
| 1578 |
"par2": "xyz", |
| 1579 |
} |
| 1580 |
_SPECS1_STR = "par1=42,par2=xyz" |
| 1581 |
_SPECS2 = { |
| 1582 |
"param": 10, |
| 1583 |
"another_param": 101, |
| 1584 |
} |
| 1585 |
_SPECS2_STR = "another_param=101,param=10" |
| 1586 |
_SPECS3 = { |
| 1587 |
"par1": 1024, |
| 1588 |
"param": "abc", |
| 1589 |
} |
| 1590 |
_SPECS3_STR = "par1=1024,param=abc" |
| 1591 |
|
| 1592 |
def _CheckPrintIPolicyCommand(self, ipolicy, isgroup, expected): |
| 1593 |
buf = StringIO() |
| 1594 |
cli.PrintIPolicyCommand(buf, ipolicy, isgroup) |
| 1595 |
self.assertEqual(buf.getvalue(), expected) |
| 1596 |
|
| 1597 |
def testIgnoreStdForGroup(self): |
| 1598 |
self._CheckPrintIPolicyCommand({"std": self._SPECS1}, True, "") |
| 1599 |
|
| 1600 |
def testIgnoreEmpty(self): |
| 1601 |
policies = [ |
| 1602 |
{}, |
| 1603 |
{"std": {}}, |
| 1604 |
{"minmax": []}, |
| 1605 |
{"minmax": [{}]}, |
| 1606 |
{"minmax": [{ |
| 1607 |
"min": {}, |
| 1608 |
"max": {}, |
| 1609 |
}]}, |
| 1610 |
{"minmax": [{ |
| 1611 |
"min": self._SPECS1, |
| 1612 |
"max": {}, |
| 1613 |
}]}, |
| 1614 |
] |
| 1615 |
for pol in policies: |
| 1616 |
self._CheckPrintIPolicyCommand(pol, False, "") |
| 1617 |
|
| 1618 |
def testFullPolicies(self): |
| 1619 |
cases = [ |
| 1620 |
({"std": self._SPECS1}, |
| 1621 |
" %s %s" % (cli.IPOLICY_STD_SPECS_STR, self._SPECS1_STR)), |
| 1622 |
({"minmax": [{ |
| 1623 |
"min": self._SPECS1, |
| 1624 |
"max": self._SPECS2, |
| 1625 |
}]}, |
| 1626 |
" %s min:%s/max:%s" % (cli.IPOLICY_BOUNDS_SPECS_STR, |
| 1627 |
self._SPECS1_STR, self._SPECS2_STR)), |
| 1628 |
({"minmax": [ |
| 1629 |
{ |
| 1630 |
"min": self._SPECS1, |
| 1631 |
"max": self._SPECS2, |
| 1632 |
}, |
| 1633 |
{ |
| 1634 |
"min": self._SPECS2, |
| 1635 |
"max": self._SPECS3, |
| 1636 |
}, |
| 1637 |
]}, |
| 1638 |
" %s min:%s/max:%s//min:%s/max:%s" % |
| 1639 |
(cli.IPOLICY_BOUNDS_SPECS_STR, self._SPECS1_STR, self._SPECS2_STR, |
| 1640 |
self._SPECS2_STR, self._SPECS3_STR)), |
| 1641 |
] |
| 1642 |
for (pol, exp) in cases: |
| 1643 |
self._CheckPrintIPolicyCommand(pol, False, exp) |
| 1644 |
|
| 1645 |
|
| 1646 |
if __name__ == "__main__": |
| 1647 |
testutils.GanetiTestProgram() |