Revision ee3aedff test/ganeti.cli_unittest.py
b/test/ganeti.cli_unittest.py | ||
---|---|---|
31 | 31 |
from ganeti import cli |
32 | 32 |
from ganeti import errors |
33 | 33 |
from ganeti import utils |
34 |
from ganeti import objects |
|
34 | 35 |
from ganeti.errors import OpPrereqError, ParameterError |
35 | 36 |
|
36 | 37 |
|
... | ... | |
248 | 249 |
None, None, "m", exp) |
249 | 250 |
|
250 | 251 |
|
252 |
class TestFormatQueryResult(unittest.TestCase): |
|
253 |
def test(self): |
|
254 |
fields = [ |
|
255 |
objects.QueryFieldDefinition(name="name", title="Name", |
|
256 |
kind=constants.QFT_TEXT), |
|
257 |
objects.QueryFieldDefinition(name="size", title="Size", |
|
258 |
kind=constants.QFT_NUMBER), |
|
259 |
objects.QueryFieldDefinition(name="act", title="Active", |
|
260 |
kind=constants.QFT_BOOL), |
|
261 |
objects.QueryFieldDefinition(name="mem", title="Memory", |
|
262 |
kind=constants.QFT_UNIT), |
|
263 |
objects.QueryFieldDefinition(name="other", title="SomeList", |
|
264 |
kind=constants.QFT_OTHER), |
|
265 |
] |
|
266 |
|
|
267 |
response = objects.QueryResponse(fields=fields, data=[ |
|
268 |
[(constants.QRFS_NORMAL, "nodeA"), (constants.QRFS_NORMAL, 128), |
|
269 |
(constants.QRFS_NORMAL, False), (constants.QRFS_NORMAL, 1468006), |
|
270 |
(constants.QRFS_NORMAL, [])], |
|
271 |
[(constants.QRFS_NORMAL, "other"), (constants.QRFS_NORMAL, 512), |
|
272 |
(constants.QRFS_NORMAL, True), (constants.QRFS_NORMAL, 16), |
|
273 |
(constants.QRFS_NORMAL, [1, 2, 3])], |
|
274 |
[(constants.QRFS_NORMAL, "xyz"), (constants.QRFS_NORMAL, 1024), |
|
275 |
(constants.QRFS_NORMAL, True), (constants.QRFS_NORMAL, 4096), |
|
276 |
(constants.QRFS_NORMAL, [{}, {}])], |
|
277 |
]) |
|
278 |
|
|
279 |
self.assertEqual(cli.FormatQueryResult(response, unit="h", header=True), |
|
280 |
(cli.QR_NORMAL, [ |
|
281 |
"Name Size Active Memory SomeList", |
|
282 |
"nodeA 128 N 1.4T []", |
|
283 |
"other 512 Y 16M [1, 2, 3]", |
|
284 |
"xyz 1024 Y 4.0G [{}, {}]", |
|
285 |
])) |
|
286 |
|
|
287 |
def testTimestampAndUnit(self): |
|
288 |
fields = [ |
|
289 |
objects.QueryFieldDefinition(name="name", title="Name", |
|
290 |
kind=constants.QFT_TEXT), |
|
291 |
objects.QueryFieldDefinition(name="size", title="Size", |
|
292 |
kind=constants.QFT_UNIT), |
|
293 |
objects.QueryFieldDefinition(name="mtime", title="ModTime", |
|
294 |
kind=constants.QFT_TIMESTAMP), |
|
295 |
] |
|
296 |
|
|
297 |
response = objects.QueryResponse(fields=fields, data=[ |
|
298 |
[(constants.QRFS_NORMAL, "a"), (constants.QRFS_NORMAL, 1024), |
|
299 |
(constants.QRFS_NORMAL, 0)], |
|
300 |
[(constants.QRFS_NORMAL, "b"), (constants.QRFS_NORMAL, 144996), |
|
301 |
(constants.QRFS_NORMAL, 1291746295)], |
|
302 |
]) |
|
303 |
|
|
304 |
self.assertEqual(cli.FormatQueryResult(response, unit="m", header=True), |
|
305 |
(cli.QR_NORMAL, [ |
|
306 |
"Name Size ModTime", |
|
307 |
"a 1024 %s" % utils.FormatTime(0), |
|
308 |
"b 144996 %s" % utils.FormatTime(1291746295), |
|
309 |
])) |
|
310 |
|
|
311 |
def testOverride(self): |
|
312 |
fields = [ |
|
313 |
objects.QueryFieldDefinition(name="name", title="Name", |
|
314 |
kind=constants.QFT_TEXT), |
|
315 |
objects.QueryFieldDefinition(name="cust", title="Custom", |
|
316 |
kind=constants.QFT_OTHER), |
|
317 |
objects.QueryFieldDefinition(name="xt", title="XTime", |
|
318 |
kind=constants.QFT_TIMESTAMP), |
|
319 |
] |
|
320 |
|
|
321 |
response = objects.QueryResponse(fields=fields, data=[ |
|
322 |
[(constants.QRFS_NORMAL, "x"), (constants.QRFS_NORMAL, ["a", "b", "c"]), |
|
323 |
(constants.QRFS_NORMAL, 1234)], |
|
324 |
[(constants.QRFS_NORMAL, "y"), (constants.QRFS_NORMAL, range(10)), |
|
325 |
(constants.QRFS_NORMAL, 1291746295)], |
|
326 |
]) |
|
327 |
|
|
328 |
override = { |
|
329 |
"cust": (utils.CommaJoin, False), |
|
330 |
"xt": (hex, True), |
|
331 |
} |
|
332 |
|
|
333 |
self.assertEqual(cli.FormatQueryResult(response, unit="h", header=True, |
|
334 |
format_override=override), |
|
335 |
(cli.QR_NORMAL, [ |
|
336 |
"Name Custom XTime", |
|
337 |
"x a, b, c 0x4d2", |
|
338 |
"y 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 0x4cfe7bf7", |
|
339 |
])) |
|
340 |
|
|
341 |
def testSeparator(self): |
|
342 |
fields = [ |
|
343 |
objects.QueryFieldDefinition(name="name", title="Name", |
|
344 |
kind=constants.QFT_TEXT), |
|
345 |
objects.QueryFieldDefinition(name="count", title="Count", |
|
346 |
kind=constants.QFT_NUMBER), |
|
347 |
objects.QueryFieldDefinition(name="desc", title="Description", |
|
348 |
kind=constants.QFT_TEXT), |
|
349 |
] |
|
350 |
|
|
351 |
response = objects.QueryResponse(fields=fields, data=[ |
|
352 |
[(constants.QRFS_NORMAL, "instance1.example.com"), |
|
353 |
(constants.QRFS_NORMAL, 21125), (constants.QRFS_NORMAL, "Hello World!")], |
|
354 |
[(constants.QRFS_NORMAL, "mail.other.net"), |
|
355 |
(constants.QRFS_NORMAL, -9000), (constants.QRFS_NORMAL, "a,b,c")], |
|
356 |
]) |
|
357 |
|
|
358 |
for sep in [":", "|", "#", "|||", "###", "@@@", "@#@"]: |
|
359 |
for header in [None, "Name%sCount%sDescription" % (sep, sep)]: |
|
360 |
exp = [] |
|
361 |
if header: |
|
362 |
exp.append(header) |
|
363 |
exp.extend([ |
|
364 |
"instance1.example.com%s21125%sHello World!" % (sep, sep), |
|
365 |
"mail.other.net%s-9000%sa,b,c" % (sep, sep), |
|
366 |
]) |
|
367 |
|
|
368 |
self.assertEqual(cli.FormatQueryResult(response, separator=sep, |
|
369 |
header=bool(header)), |
|
370 |
(cli.QR_NORMAL, exp)) |
|
371 |
|
|
372 |
def testStatusWithUnknown(self): |
|
373 |
fields = [ |
|
374 |
objects.QueryFieldDefinition(name="id", title="ID", |
|
375 |
kind=constants.QFT_NUMBER), |
|
376 |
objects.QueryFieldDefinition(name="unk", title="unk", |
|
377 |
kind=constants.QFT_UNKNOWN), |
|
378 |
objects.QueryFieldDefinition(name="unavail", title="Unavail", |
|
379 |
kind=constants.QFT_BOOL), |
|
380 |
objects.QueryFieldDefinition(name="nodata", title="NoData", |
|
381 |
kind=constants.QFT_TEXT), |
|
382 |
] |
|
383 |
|
|
384 |
response = objects.QueryResponse(fields=fields, data=[ |
|
385 |
[(constants.QRFS_NORMAL, 1), (constants.QRFS_UNKNOWN, None), |
|
386 |
(constants.QRFS_NORMAL, False), (constants.QRFS_NORMAL, "")], |
|
387 |
[(constants.QRFS_NORMAL, 2), (constants.QRFS_UNKNOWN, None), |
|
388 |
(constants.QRFS_NODATA, None), (constants.QRFS_NORMAL, "x")], |
|
389 |
[(constants.QRFS_NORMAL, 3), (constants.QRFS_UNKNOWN, None), |
|
390 |
(constants.QRFS_NORMAL, False), (constants.QRFS_UNAVAIL, None)], |
|
391 |
]) |
|
392 |
|
|
393 |
self.assertEqual(cli.FormatQueryResult(response, header=True, |
|
394 |
separator="|"), |
|
395 |
(cli.QR_UNKNOWN, [ |
|
396 |
"ID|unk|Unavail|NoData", |
|
397 |
"1|<unknown>|N|", |
|
398 |
"2|<unknown>|<nodata>|x", |
|
399 |
"3|<unknown>|N|<unavail>", |
|
400 |
])) |
|
401 |
|
|
402 |
def testNoData(self): |
|
403 |
fields = [ |
|
404 |
objects.QueryFieldDefinition(name="id", title="ID", |
|
405 |
kind=constants.QFT_NUMBER), |
|
406 |
objects.QueryFieldDefinition(name="name", title="Name", |
|
407 |
kind=constants.QFT_TEXT), |
|
408 |
] |
|
409 |
|
|
410 |
response = objects.QueryResponse(fields=fields, data=[]) |
|
411 |
|
|
412 |
self.assertEqual(cli.FormatQueryResult(response, header=True), |
|
413 |
(cli.QR_NORMAL, ["ID Name"])) |
|
414 |
|
|
415 |
def testNoDataWithUnknown(self): |
|
416 |
fields = [ |
|
417 |
objects.QueryFieldDefinition(name="id", title="ID", |
|
418 |
kind=constants.QFT_NUMBER), |
|
419 |
objects.QueryFieldDefinition(name="unk", title="unk", |
|
420 |
kind=constants.QFT_UNKNOWN), |
|
421 |
] |
|
422 |
|
|
423 |
response = objects.QueryResponse(fields=fields, data=[]) |
|
424 |
|
|
425 |
self.assertEqual(cli.FormatQueryResult(response, header=False), |
|
426 |
(cli.QR_UNKNOWN, [])) |
|
427 |
|
|
428 |
def testStatus(self): |
|
429 |
fields = [ |
|
430 |
objects.QueryFieldDefinition(name="id", title="ID", |
|
431 |
kind=constants.QFT_NUMBER), |
|
432 |
objects.QueryFieldDefinition(name="unavail", title="Unavail", |
|
433 |
kind=constants.QFT_BOOL), |
|
434 |
objects.QueryFieldDefinition(name="nodata", title="NoData", |
|
435 |
kind=constants.QFT_TEXT), |
|
436 |
] |
|
437 |
|
|
438 |
response = objects.QueryResponse(fields=fields, data=[ |
|
439 |
[(constants.QRFS_NORMAL, 1), (constants.QRFS_NORMAL, False), |
|
440 |
(constants.QRFS_NORMAL, "")], |
|
441 |
[(constants.QRFS_NORMAL, 2), (constants.QRFS_NODATA, None), |
|
442 |
(constants.QRFS_NORMAL, "x")], |
|
443 |
[(constants.QRFS_NORMAL, 3), (constants.QRFS_NORMAL, False), |
|
444 |
(constants.QRFS_UNAVAIL, None)], |
|
445 |
]) |
|
446 |
|
|
447 |
self.assertEqual(cli.FormatQueryResult(response, header=False, |
|
448 |
separator="|"), |
|
449 |
(cli.QR_INCOMPLETE, [ |
|
450 |
"1|N|", |
|
451 |
"2|<nodata>|x", |
|
452 |
"3|N|<unavail>", |
|
453 |
])) |
|
454 |
|
|
455 |
def testInvalidFieldType(self): |
|
456 |
fields = [ |
|
457 |
objects.QueryFieldDefinition(name="x", title="x", |
|
458 |
kind="#some#other#type"), |
|
459 |
] |
|
460 |
|
|
461 |
response = objects.QueryResponse(fields=fields, data=[]) |
|
462 |
|
|
463 |
self.assertRaises(NotImplementedError, cli.FormatQueryResult, response) |
|
464 |
|
|
465 |
def testInvalidFieldStatus(self): |
|
466 |
fields = [ |
|
467 |
objects.QueryFieldDefinition(name="x", title="x", |
|
468 |
kind=constants.QFT_TEXT), |
|
469 |
] |
|
470 |
|
|
471 |
response = objects.QueryResponse(fields=fields, data=[[(-1, None)]]) |
|
472 |
self.assertRaises(NotImplementedError, cli.FormatQueryResult, response) |
|
473 |
|
|
474 |
response = objects.QueryResponse(fields=fields, data=[[(-1, "x")]]) |
|
475 |
self.assertRaises(AssertionError, cli.FormatQueryResult, response) |
|
476 |
|
|
477 |
def testEmptyFieldTitle(self): |
|
478 |
fields = [ |
|
479 |
objects.QueryFieldDefinition(name="x", title="", |
|
480 |
kind=constants.QFT_TEXT), |
|
481 |
] |
|
482 |
|
|
483 |
response = objects.QueryResponse(fields=fields, data=[]) |
|
484 |
self.assertRaises(AssertionError, cli.FormatQueryResult, response) |
|
485 |
|
|
486 |
|
|
251 | 487 |
class _MockJobPollCb(cli.JobPollCbBase, cli.JobPollReportCbBase): |
252 | 488 |
def __init__(self, tc, job_id): |
253 | 489 |
self.tc = tc |
Also available in: Unified diff