Revision cfa79a23

b/lib/cmdlib.py
9704 9704
  return cluster.SimpleFillBE(op.beparams)
9705 9705

  
9706 9706

  
9707
def _CheckOpportunisticLocking(op):
9708
  """Generate error if opportunistic locking is not possible.
9709

  
9710
  """
9711
  if op.opportunistic_locking and not op.iallocator:
9712
    raise errors.OpPrereqError("Opportunistic locking is only available in"
9713
                               " combination with an instance allocator",
9714
                               errors.ECODE_INVAL)
9715

  
9716

  
9707 9717
class LUInstanceCreate(LogicalUnit):
9708 9718
  """Create an instance.
9709 9719

  
......
9799 9809
                        " template")
9800 9810
        self.op.snode = None
9801 9811

  
9812
    _CheckOpportunisticLocking(self.op)
9813

  
9802 9814
    self._cds = _GetClusterDomainSecret()
9803 9815

  
9804 9816
    if self.op.mode == constants.INSTANCE_IMPORT:
......
10814 10826
                                   " or set a cluster-wide default iallocator",
10815 10827
                                   errors.ECODE_INVAL)
10816 10828

  
10829
    _CheckOpportunisticLocking(self.op)
10830

  
10817 10831
    dups = utils.FindDuplicates([op.instance_name for op in self.op.instances])
10818 10832
    if dups:
10819 10833
      raise errors.OpPrereqError("There are duplicate instance names: %s" %
b/test/ganeti.cmdlib_unittest.py
1501 1501
    self.assertNotEqual(id(names), id(output), msg="List was not copied")
1502 1502

  
1503 1503

  
1504
class TestCheckOpportunisticLocking(unittest.TestCase):
1505
  class OpTest(opcodes.OpCode):
1506
    OP_PARAMS = [
1507
      opcodes._POpportunisticLocking,
1508
      opcodes._PIAllocFromDesc(""),
1509
      ]
1510

  
1511
  @classmethod
1512
  def _MakeOp(cls, **kwargs):
1513
    op = cls.OpTest(**kwargs)
1514
    op.Validate(True)
1515
    return op
1516

  
1517
  def testMissingAttributes(self):
1518
    self.assertRaises(AttributeError, cmdlib._CheckOpportunisticLocking,
1519
                      object())
1520

  
1521
  def testDefaults(self):
1522
    op = self._MakeOp()
1523
    cmdlib._CheckOpportunisticLocking(op)
1524

  
1525
  def test(self):
1526
    for iallocator in [None, "something", "other"]:
1527
      for opplock in [False, True]:
1528
        op = self._MakeOp(iallocator=iallocator, opportunistic_locking=opplock)
1529
        if opplock and not iallocator:
1530
          self.assertRaises(errors.OpPrereqError,
1531
                            cmdlib._CheckOpportunisticLocking, op)
1532
        else:
1533
          cmdlib._CheckOpportunisticLocking(op)
1534

  
1535

  
1504 1536
if __name__ == "__main__":
1505 1537
  testutils.GanetiTestProgram()

Also available in: Unified diff