Commit a9e1819b authored by Klaus Aehlig's avatar Klaus Aehlig

Add a constructor to BaseOpCode

In order to guarantee all mandatory slots to be present, add
a custom constructor to BaseOpCode adding those fields with
default value, instead of inheriting the constructor from
outils.ValidatedSlots.
Signed-off-by: default avatarKlaus Aehlig <aehlig@google.com>
Reviewed-by: default avatarMichele Tartara <mtartara@google.com>
parent e4236bbd
......@@ -160,6 +160,12 @@ class BaseOpCode(outils.ValidatedSlots):
# as OP_ID is dynamically defined
__metaclass__ = _AutoOpParamSlots
def __init__(self, **kwargs):
outils.ValidatedSlots.__init__(self, **kwargs)
for key, default, _, _ in self.__class__.GetAllParams():
if not hasattr(self, key):
setattr(self, key, default)
def __getstate__(self):
"""Generic serializer.
......
......@@ -298,7 +298,7 @@ class TestEncodeOpError(unittest.TestCase):
class TestQueuedOpCode(unittest.TestCase):
def testDefaults(self):
def _Check(op):
self.assertFalse(hasattr(op.input, "dry_run"))
self.assertFalse(op.input.dry_run)
self.assertEqual(op.priority, constants.OP_PRIO_DEFAULT)
self.assertFalse(op.log)
self.assert_(op.start_timestamp is None)
......@@ -432,8 +432,6 @@ class TestQueuedJob(unittest.TestCase):
self.assertTrue(compat.all(op.priority == constants.OP_PRIO_DEFAULT
for op in job.ops))
self.assertEqual(job.CalcPriority(), constants.OP_PRIO_DEFAULT)
self.assertFalse(compat.any(hasattr(op.input, "priority")
for op in job.ops))
return job
......@@ -445,8 +443,6 @@ class TestQueuedJob(unittest.TestCase):
result = job.ChangePriority(-10)
self.assertEqual(job.CalcPriority(), -10)
self.assertTrue(compat.all(op.priority == -10 for op in job.ops))
self.assertFalse(compat.any(hasattr(op.input, "priority")
for op in job.ops))
self.assertEqual(result,
(True, ("Priorities of pending opcodes for job 24984 have"
" been changed to -10")))
......@@ -466,8 +462,6 @@ class TestQueuedJob(unittest.TestCase):
self.assertEqual(job.CalcPriority(), constants.OP_PRIO_DEFAULT)
self.assertTrue(compat.all(op.priority == constants.OP_PRIO_DEFAULT
for op in job.ops))
self.assertFalse(compat.any(hasattr(op.input, "priority")
for op in job.ops))
self.assertEqual(map(operator.attrgetter("status"), job.ops), [
constants.OP_STATUS_SUCCESS,
constants.OP_STATUS_SUCCESS,
......@@ -491,8 +485,6 @@ class TestQueuedJob(unittest.TestCase):
self.assertEqual(job.CalcPriority(), constants.OP_PRIO_DEFAULT)
self.assertTrue(compat.all(op.priority == constants.OP_PRIO_DEFAULT
for op in job.ops))
self.assertFalse(compat.any(hasattr(op.input, "priority")
for op in job.ops))
self.assertEqual(map(operator.attrgetter("status"), job.ops), [
constants.OP_STATUS_SUCCESS,
constants.OP_STATUS_SUCCESS,
......@@ -516,8 +508,6 @@ class TestQueuedJob(unittest.TestCase):
self.assertEqual(job.CalcPriority(), constants.OP_PRIO_DEFAULT)
self.assertEqual(map(operator.attrgetter("priority"), job.ops),
[constants.OP_PRIO_DEFAULT, 7, 7, 7])
self.assertFalse(compat.any(hasattr(op.input, "priority")
for op in job.ops))
self.assertEqual(map(operator.attrgetter("status"), job.ops), [
constants.OP_STATUS_RUNNING,
constants.OP_STATUS_QUEUED,
......@@ -543,8 +533,6 @@ class TestQueuedJob(unittest.TestCase):
self.assertEqual(job.CalcPriority(), constants.OP_PRIO_DEFAULT)
self.assertTrue(compat.all(op.priority == constants.OP_PRIO_DEFAULT
for op in job.ops))
self.assertFalse(compat.any(hasattr(op.input, "priority")
for op in job.ops))
self.assertEqual(map(operator.attrgetter("status"), job.ops), [
constants.OP_STATUS_SUCCESS,
constants.OP_STATUS_SUCCESS,
......@@ -568,8 +556,6 @@ class TestQueuedJob(unittest.TestCase):
self.assertEqual(map(operator.attrgetter("priority"), job.ops),
[constants.OP_PRIO_DEFAULT, constants.OP_PRIO_DEFAULT,
-19, -19])
self.assertFalse(compat.any(hasattr(op.input, "priority")
for op in job.ops))
self.assertEqual(map(operator.attrgetter("status"), job.ops), [
constants.OP_STATUS_SUCCESS,
constants.OP_STATUS_RUNNING,
......@@ -2269,10 +2255,6 @@ class TestJobProcessorTimeouts(unittest.TestCase, _JobProcessorTestUtils):
result = proc(_nextop_fn=self._NextOpcode)
assert self.curop is not None
# Input priority should never be set or modified
self.assertFalse(compat.any(hasattr(op.input, "priority")
for op in job.ops))
if result == jqueue._JobProcessor.FINISHED or self.gave_lock:
# Got lock and/or job is done, result must've been written
self.assertFalse(job.cur_opctx)
......
......@@ -136,8 +136,6 @@ class TestProcessResult(unittest.TestCase):
for op in [op1, op2, op3]:
self.assertTrue("OP_TEST_DUMMY" in op.comment)
self.assertFalse(hasattr(op, "priority"))
self.assertFalse(hasattr(op, "debug_level"))
def testParams(self):
src = opcodes.OpTestDummy(priority=constants.OP_PRIO_HIGH,
......@@ -163,7 +161,8 @@ class TestProcessResult(unittest.TestCase):
self.assertTrue("OP_TEST_DUMMY" in op1.comment)
self.assertEqual(op1.debug_level, 3)
self.assertEqual(op2.priority, constants.OP_PRIO_HIGH)
# FIXME: as priority is mandatory, there is no way
# of specifying "just inherit the priority".
self.assertEqual(op2.comment, "foobar")
self.assertEqual(op2.debug_level, 3)
......
......@@ -219,10 +219,10 @@ class TestOpcodes(unittest.TestCase):
op = OpTest()
before = op.__getstate__()
self.assertRaises(errors.OpPrereqError, op.Validate, False)
self.assertFalse(hasattr(op, "nodef"))
self.assertFalse(hasattr(op, "wdef"))
self.assertFalse(hasattr(op, "number"))
self.assertFalse(hasattr(op, "notype"))
self.assertTrue(op.nodef is None)
self.assertEqual(op.wdef, "default")
self.assertEqual(op.number, 0)
self.assertTrue(op.notype is None)
self.assertEqual(op.__getstate__(), before, msg="Opcode was modified")
# Required parameter "nodef" is provided
......@@ -231,16 +231,16 @@ class TestOpcodes(unittest.TestCase):
op.Validate(False)
self.assertEqual(op.__getstate__(), before, msg="Opcode was modified")
self.assertEqual(op.nodef, "foo")
self.assertFalse(hasattr(op, "wdef"))
self.assertFalse(hasattr(op, "number"))
self.assertFalse(hasattr(op, "notype"))
self.assertEqual(op.wdef, "default")
self.assertEqual(op.number, 0)
self.assertTrue(op.notype is None)
# Missing required parameter "nodef"
op = OpTest(wdef="hello", number=999)
before = op.__getstate__()
self.assertRaises(errors.OpPrereqError, op.Validate, False)
self.assertFalse(hasattr(op, "nodef"))
self.assertFalse(hasattr(op, "notype"))
self.assertTrue(op.nodef is None)
self.assertTrue(op.notype is None)
self.assertEqual(op.__getstate__(), before, msg="Opcode was modified")
# Wrong type for "nodef"
......@@ -248,7 +248,7 @@ class TestOpcodes(unittest.TestCase):
before = op.__getstate__()
self.assertRaises(errors.OpPrereqError, op.Validate, False)
self.assertEqual(op.nodef, 987)
self.assertFalse(hasattr(op, "notype"))
self.assertTrue(op.notype is None)
self.assertEqual(op.__getstate__(), before, msg="Opcode was modified")
# Testing different types for "notype"
......@@ -274,10 +274,7 @@ class TestOpcodes(unittest.TestCase):
]
op = OpTest()
before = op.__getstate__()
op.Validate(True)
self.assertNotEqual(op.__getstate__(), before,
msg="Opcode was not modified")
self.assertEqual(op.value1, "default")
self.assertEqual(op.value2, "result")
self.assert_(op.dry_run is None)
......@@ -285,10 +282,7 @@ class TestOpcodes(unittest.TestCase):
self.assertEqual(op.priority, constants.OP_PRIO_DEFAULT)
op = OpTest(value1="hello", value2="world", debug_level=123)
before = op.__getstate__()
op.Validate(True)
self.assertNotEqual(op.__getstate__(), before,
msg="Opcode was not modified")
self.assertEqual(op.value1, "hello")
self.assertEqual(op.value2, "world")
self.assertEqual(op.debug_level, 123)
......
......@@ -44,7 +44,7 @@ class TestFillOpcode(unittest.TestCase):
for static in [None, {}]:
op = baserlib.FillOpcode(self.OpTest, {}, static)
self.assertTrue(isinstance(op, self.OpTest))
self.assertFalse(hasattr(op, "test"))
self.assertTrue(op.test is None)
def testStatic(self):
op = baserlib.FillOpcode(self.OpTest, {}, {"test": "abc"})
......
......@@ -468,7 +468,7 @@ class TestInstanceActivateDisks(unittest.TestCase):
self.assertTrue(isinstance(op, opcodes.OpInstanceActivateDisks))
self.assertEqual(op.instance_name, "xyz")
self.assertTrue(op.ignore_size)
self.assertFalse(hasattr(op, "dry_run"))
self.assertFalse(op.dry_run)
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -487,8 +487,8 @@ class TestInstanceDeactivateDisks(unittest.TestCase):
self.assertEqual(job_id, exp_job_id)
self.assertTrue(isinstance(op, opcodes.OpInstanceDeactivateDisks))
self.assertEqual(op.instance_name, "inst22357")
self.assertFalse(hasattr(op, "dry_run"))
self.assertFalse(hasattr(op, "force"))
self.assertFalse(op.dry_run)
self.assertFalse(op.force)
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -507,7 +507,7 @@ class TestInstanceRecreateDisks(unittest.TestCase):
self.assertEqual(job_id, exp_job_id)
self.assertTrue(isinstance(op, opcodes.OpInstanceRecreateDisks))
self.assertEqual(op.instance_name, "inst22357")
self.assertFalse(hasattr(op, "dry_run"))
self.assertFalse(op.dry_run)
self.assertFalse(hasattr(op, "force"))
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -527,7 +527,7 @@ class TestInstanceFailover(unittest.TestCase):
self.assertEqual(job_id, exp_job_id)
self.assertTrue(isinstance(op, opcodes.OpInstanceFailover))
self.assertEqual(op.instance_name, "inst12794")
self.assertFalse(hasattr(op, "dry_run"))
self.assertFalse(op.dry_run)
self.assertFalse(hasattr(op, "force"))
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -552,7 +552,7 @@ class TestInstanceDiskGrow(unittest.TestCase):
self.assertEqual(op.instance_name, "inst10742")
self.assertEqual(op.disk, 3)
self.assertEqual(op.amount, 1024)
self.assertFalse(hasattr(op, "dry_run"))
self.assertFalse(op.dry_run)
self.assertFalse(hasattr(op, "force"))
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -576,7 +576,7 @@ class TestBackupPrepare(unittest.TestCase):
self.assertTrue(isinstance(op, opcodes.OpBackupPrepare))
self.assertEqual(op.instance_name, "inst17925")
self.assertEqual(op.mode, constants.EXPORT_MODE_REMOTE)
self.assertFalse(hasattr(op, "dry_run"))
self.assertFalse(op.dry_run)
self.assertFalse(hasattr(op, "force"))
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -622,7 +622,7 @@ class TestStorageQuery(unittest.TestCase):
self.assertEqual(op.nodes, ["node21075"])
self.assertEqual(op.storage_type, constants.ST_LVM_PV)
self.assertEqual(op.output_fields, ["name", "other"])
self.assertFalse(hasattr(op, "dry_run"))
self.assertFalse(op.dry_run)
self.assertFalse(hasattr(op, "force"))
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -687,7 +687,7 @@ class TestStorageModify(unittest.TestCase):
self.assertEqual(op.changes, {
constants.SF_ALLOCATABLE: (allocatable == "1"),
})
self.assertFalse(hasattr(op, "dry_run"))
self.assertFalse(op.dry_run)
self.assertFalse(hasattr(op, "force"))
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -742,7 +742,7 @@ class TestStorageRepair(unittest.TestCase):
self.assertEqual(op.node_name, "node19265")
self.assertEqual(op.storage_type, constants.ST_LVM_PV)
self.assertEqual(op.name, "pv16611")
self.assertFalse(hasattr(op, "dry_run"))
self.assertFalse(op.dry_run)
self.assertFalse(hasattr(op, "force"))
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -921,12 +921,12 @@ class TestInstanceCreation(testutils.GanetiTestCase):
self.assertFalse("foobar" in opnic)
if beparams is None:
self.assertFalse(hasattr(op, "beparams"))
self.assertTrue(op.beparams in [None, {}])
else:
self.assertEqualValues(op.beparams, beparams)
if hvparams is None:
self.assertFalse(hasattr(op, "hvparams"))
self.assertTrue(op.hvparams in [None, {}])
else:
self.assertEqualValues(op.hvparams, hvparams)
......@@ -1102,7 +1102,7 @@ class TestBackupExport(unittest.TestCase):
self.assertEqual(op.remove_instance, True)
self.assertEqual(op.x509_key_name, ["name", "hash"])
self.assertEqual(op.destination_x509_ca, "---cert---")
self.assertFalse(hasattr(op, "dry_run"))
self.assertFalse(op.dry_run)
self.assertFalse(hasattr(op, "force"))
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -1128,10 +1128,10 @@ class TestBackupExport(unittest.TestCase):
self.assertTrue(isinstance(op, opcodes.OpBackupExport))
self.assertEqual(op.instance_name, name)
self.assertEqual(op.target_node, "node2")
self.assertFalse(hasattr(op, "mode"))
self.assertFalse(hasattr(op, "remove_instance"))
self.assertEqual(op.mode, "local")
self.assertFalse(op.remove_instance)
self.assertFalse(hasattr(op, "destination"))
self.assertFalse(hasattr(op, "dry_run"))
self.assertFalse(op.dry_run)
self.assertFalse(hasattr(op, "force"))
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -1172,7 +1172,7 @@ class TestInstanceMigrate(testutils.GanetiTestCase):
self.assertEqual(op.instance_name, name)
self.assertEqual(op.mode, mode)
self.assertEqual(op.cleanup, cleanup)
self.assertFalse(hasattr(op, "dry_run"))
self.assertFalse(op.dry_run)
self.assertFalse(hasattr(op, "force"))
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -1193,9 +1193,9 @@ class TestInstanceMigrate(testutils.GanetiTestCase):
self.assertEqual(job_id, exp_job_id)
self.assertTrue(isinstance(op, opcodes.OpInstanceMigrate))
self.assertEqual(op.instance_name, name)
self.assertFalse(hasattr(op, "mode"))
self.assertFalse(hasattr(op, "cleanup"))
self.assertFalse(hasattr(op, "dry_run"))
self.assertTrue(op.mode is None)
self.assertFalse(op.cleanup)
self.assertFalse(op.dry_run)
self.assertFalse(hasattr(op, "force"))
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -1230,7 +1230,7 @@ class TestParseRenameInstanceRequest(testutils.GanetiTestCase):
self.assertEqual(op.new_name, new_name)
self.assertEqual(op.ip_check, ip_check)
self.assertEqual(op.name_check, name_check)
self.assertFalse(hasattr(op, "dry_run"))
self.assertFalse(op.dry_run)
self.assertFalse(hasattr(op, "force"))
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -1257,9 +1257,9 @@ class TestParseRenameInstanceRequest(testutils.GanetiTestCase):
self.assertTrue(isinstance(op, opcodes.OpInstanceRename))
self.assertEqual(op.instance_name, name)
self.assertEqual(op.new_name, new_name)
self.assertFalse(hasattr(op, "ip_check"))
self.assertFalse(hasattr(op, "name_check"))
self.assertFalse(hasattr(op, "dry_run"))
self.assertTrue(op.ip_check)
self.assertTrue(op.name_check)
self.assertFalse(op.dry_run)
self.assertFalse(hasattr(op, "force"))
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -1311,10 +1311,10 @@ class TestParseModifyInstanceRequest(unittest.TestCase):
self.assertEqual(op.nics, nics)
self.assertEqual(op.disks, disks)
self.assertEqual(op.disk_template, disk_template)
self.assertFalse(hasattr(op, "remote_node"))
self.assertFalse(hasattr(op, "os_name"))
self.assertFalse(hasattr(op, "force_variant"))
self.assertFalse(hasattr(op, "dry_run"))
self.assertTrue(op.remote_node is None)
self.assertTrue(op.os_name is None)
self.assertFalse(op.force_variant)
self.assertFalse(op.dry_run)
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -1337,7 +1337,7 @@ class TestParseModifyInstanceRequest(unittest.TestCase):
for i in ["hvparams", "beparams", "osparams", "force", "nics", "disks",
"disk_template", "remote_node", "os_name", "force_variant"]:
self.assertFalse(hasattr(op, i))
self.assertTrue(hasattr(op, i))
class TestParseInstanceReinstallRequest(testutils.GanetiTestCase):
......@@ -1497,8 +1497,8 @@ class TestInstanceReplaceDisks(unittest.TestCase):
self.assertTrue(isinstance(op, opcodes.OpInstanceReplaceDisks))
self.assertEqual(op.instance_name, name)
self.assertEqual(op.mode, constants.REPLACE_DISK_AUTO)
self.assertFalse(hasattr(op, "iallocator"))
self.assertFalse(hasattr(op, "disks"))
self.assertTrue(op.iallocator is None)
self.assertEqual(op.disks, [])
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
def testNoDisks(self):
......@@ -1552,7 +1552,7 @@ class TestGroupModify(unittest.TestCase):
self.assertTrue(isinstance(op, opcodes.OpGroupSetParams))
self.assertEqual(op.group_name, name)
self.assertEqual(op.alloc_policy, policy)
self.assertFalse(hasattr(op, "dry_run"))
self.assertFalse(op.dry_run)
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
def testUnknownPolicy(self):
......@@ -1584,8 +1584,8 @@ class TestGroupModify(unittest.TestCase):
self.assertTrue(isinstance(op, opcodes.OpGroupSetParams))
self.assertEqual(op.group_name, name)
self.assertFalse(hasattr(op, "alloc_policy"))
self.assertFalse(hasattr(op, "dry_run"))
self.assertTrue(op.alloc_policy is None)
self.assertFalse(op.dry_run)
self.assertRaises(IndexError, cl.GetNextSubmittedJob)
......@@ -1646,7 +1646,7 @@ class TestGroupAdd(unittest.TestCase):
self.assertTrue(isinstance(op, opcodes.OpGroupAdd))
self.assertEqual(op.group_name, name)
self.assertFalse(hasattr(op, "alloc_policy"))
self.assertTrue(op.alloc_policy is None)
self.assertFalse(op.dry_run)
def testLegacyName(self):
......@@ -1670,7 +1670,7 @@ class TestGroupAdd(unittest.TestCase):
self.assertTrue(isinstance(op, opcodes.OpGroupAdd))
self.assertEqual(op.group_name, name)
self.assertFalse(hasattr(op, "alloc_policy"))
self.assertTrue(op.alloc_policy is None)
self.assertTrue(op.dry_run)
......@@ -1694,7 +1694,7 @@ class TestNodeRole(unittest.TestCase):
self.assertTrue(isinstance(op, opcodes.OpNodeSetParams))
self.assertEqual(op.node_name, "node-z")
self.assertFalse(op.force)
self.assertFalse(hasattr(op, "dry_run"))
self.assertFalse(op.dry_run)
if role == rlib2._NR_REGULAR:
self.assertFalse(op.drained)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment