Revision db5bce34 test/ganeti.jqueue_unittest.py

b/test/ganeti.jqueue_unittest.py
346 346
    job.ops[0].priority -= 19
347 347
    self.assertEqual(job.CalcPriority(), constants.OP_PRIO_DEFAULT - 20)
348 348

  
349
  def testCalcStatus(self):
350
    def _Queued(ops):
351
      # The default status is "queued"
352
      self.assert_(compat.all(op.status == constants.OP_STATUS_QUEUED
353
                              for op in ops))
354

  
355
    def _Waitlock1(ops):
356
      ops[0].status = constants.OP_STATUS_WAITLOCK
357

  
358
    def _Waitlock2(ops):
359
      ops[0].status = constants.OP_STATUS_SUCCESS
360
      ops[1].status = constants.OP_STATUS_SUCCESS
361
      ops[2].status = constants.OP_STATUS_WAITLOCK
362

  
363
    def _Running(ops):
364
      ops[0].status = constants.OP_STATUS_SUCCESS
365
      ops[1].status = constants.OP_STATUS_RUNNING
366
      for op in ops[2:]:
367
        op.status = constants.OP_STATUS_QUEUED
368

  
369
    def _Canceling1(ops):
370
      ops[0].status = constants.OP_STATUS_SUCCESS
371
      ops[1].status = constants.OP_STATUS_SUCCESS
372
      for op in ops[2:]:
373
        op.status = constants.OP_STATUS_CANCELING
374

  
375
    def _Canceling2(ops):
376
      for op in ops:
377
        op.status = constants.OP_STATUS_CANCELING
378

  
379
    def _Canceled(ops):
380
      for op in ops:
381
        op.status = constants.OP_STATUS_CANCELED
382

  
383
    def _Error1(ops):
384
      for idx, op in enumerate(ops):
385
        if idx > 3:
386
          op.status = constants.OP_STATUS_ERROR
387
        else:
388
          op.status = constants.OP_STATUS_SUCCESS
389

  
390
    def _Error2(ops):
391
      for op in ops:
392
        op.status = constants.OP_STATUS_ERROR
393

  
394
    def _Success(ops):
395
      for op in ops:
396
        op.status = constants.OP_STATUS_SUCCESS
397

  
398
    tests = {
399
      constants.JOB_STATUS_QUEUED: [_Queued],
400
      constants.JOB_STATUS_WAITLOCK: [_Waitlock1, _Waitlock2],
401
      constants.JOB_STATUS_RUNNING: [_Running],
402
      constants.JOB_STATUS_CANCELING: [_Canceling1, _Canceling2],
403
      constants.JOB_STATUS_CANCELED: [_Canceled],
404
      constants.JOB_STATUS_ERROR: [_Error1, _Error2],
405
      constants.JOB_STATUS_SUCCESS: [_Success],
406
      }
407

  
408
    def _NewJob():
409
      job = jqueue._QueuedJob(None, 1,
410
                              [opcodes.OpTestDelay() for _ in range(10)])
411
      self.assertEqual(job.CalcStatus(), constants.JOB_STATUS_QUEUED)
412
      self.assert_(compat.all(op.status == constants.OP_STATUS_QUEUED
413
                              for op in job.ops))
414
      return job
415

  
416
    for status in constants.JOB_STATUS_ALL:
417
      sttests = tests[status]
418
      assert sttests
419
      for fn in sttests:
420
        job = _NewJob()
421
        fn(job.ops)
422
        self.assertEqual(job.CalcStatus(), status)
423

  
349 424

  
350 425
class _FakeQueueForProc:
351 426
  def __init__(self):

Also available in: Unified diff