Revision 288d6440 qa/qa_utils.py

b/qa/qa_utils.py
27 27
import re
28 28
import sys
29 29
import subprocess
30
import random
30 31

  
31 32
from ganeti import utils
33
from ganeti import compat
32 34

  
33 35
import qa_config
34 36
import qa_error
......
284 286
  return instances
285 287

  
286 288

  
289
def _SelectQueryFields(rnd, fields):
290
  """Generates a list of fields for query tests.
291

  
292
  """
293
  # Create copy for shuffling
294
  fields = list(fields)
295
  rnd.shuffle(fields)
296

  
297
  # Check all fields
298
  yield fields
299
  yield sorted(fields)
300

  
301
  # Duplicate fields
302
  yield fields + fields
303

  
304
  # Check small groups of fields
305
  while fields:
306
    yield [fields.pop() for _ in range(rnd.randint(2, 10)) if fields]
307

  
308

  
309
def _List(listcmd, fields, names):
310
  """Runs a list command.
311

  
312
  """
313
  master = qa_config.GetMasterNode()
314

  
315
  cmd = [listcmd, "list", "--separator=|", "--no-header",
316
         "--output", ",".join(fields)]
317

  
318
  if names:
319
    cmd.extend(names)
320

  
321
  return GetCommandOutput(master["primary"],
322
                          utils.ShellQuoteArgs(cmd)).splitlines()
323

  
324

  
325
def GenericQueryTest(cmd, fields):
326
  """Runs a number of tests on query commands.
327

  
328
  @param cmd: Command name
329
  @param fields: List of field names
330

  
331
  """
332
  rnd = random.Random(hash(cmd))
333

  
334
  randfields = list(fields)
335
  rnd.shuffle(fields)
336

  
337
  # Test a number of field combinations
338
  for testfields in _SelectQueryFields(rnd, fields):
339
    AssertCommand([cmd, "list", "--output", ",".join(testfields)])
340

  
341
  namelist_fn = compat.partial(_List, cmd, ["name"])
342

  
343
  # When no names were requested, the list must be sorted
344
  names = namelist_fn(None)
345
  AssertEqual(names, utils.NiceSort(names))
346

  
347
  # When requesting specific names, the order must be kept
348
  revnames = list(reversed(names))
349
  AssertEqual(namelist_fn(revnames), revnames)
350

  
351
  randnames = list(names)
352
  rnd.shuffle(randnames)
353
  AssertEqual(namelist_fn(randnames), randnames)
354

  
355

  
287 356
def _FormatWithColor(text, seq):
288 357
  if not seq:
289 358
    return text

Also available in: Unified diff