Revision 5f06ce5e

b/lib/objects.py
989 989
    return cls.SplitNameVariant(name)[1]
990 990

  
991 991

  
992
class NodeHvState(ConfigObject):
993
  """Hypvervisor state on a node.
994

  
995
  @ivar mem_total: Total amount of memory
996
  @ivar mem_node: Memory used by, or reserved for, the node itself (not always
997
    available)
998
  @ivar mem_hv: Memory used by hypervisor or lost due to instance allocation
999
    rounding
1000
  @ivar mem_inst: Memory used by instances living on node
1001
  @ivar cpu_total: Total node CPU core count
1002
  @ivar cpu_node: Number of CPU cores reserved for the node itself
1003

  
1004
  """
1005
  __slots__ = [
1006
    "mem_total",
1007
    "mem_node",
1008
    "mem_hv",
1009
    "mem_inst",
1010
    "cpu_total",
1011
    "cpu_node",
1012
    ] + _TIMESTAMPS
1013

  
1014

  
1015
class NodeDiskState(ConfigObject):
1016
  """Disk state on a node.
1017

  
1018
  """
1019
  __slots__ = [
1020
    "total",
1021
    "reserved",
1022
    "overhead",
1023
    ] + _TIMESTAMPS
1024

  
1025

  
992 1026
class Node(TaggableObject):
993 1027
  """Config object representing a node.
994 1028

  
......
1035 1069
    if self.powered is None:
1036 1070
      self.powered = True
1037 1071

  
1072
  def ToDict(self):
1073
    """Custom function for serializing.
1074

  
1075
    """
1076
    data = super(Node, self).ToDict()
1077

  
1078
    hv_state = data.get("hv_state", None)
1079
    if hv_state is not None:
1080
      data["hv_state"] = self._ContainerToDicts(hv_state)
1081

  
1082
    disk_state = data.get("disk_state", None)
1083
    if disk_state is not None:
1084
      data["disk_state"] = \
1085
        dict((key, self._ContainerToDicts(value))
1086
             for (key, value) in disk_state.items())
1087

  
1088
    return data
1089

  
1090
  @classmethod
1091
  def FromDict(cls, val):
1092
    """Custom function for deserializing.
1093

  
1094
    """
1095
    obj = super(Node, cls).FromDict(val)
1096

  
1097
    if obj.hv_state is not None:
1098
      obj.hv_state = cls._ContainerFromDicts(obj.hv_state, dict, NodeHvState)
1099

  
1100
    if obj.disk_state is not None:
1101
      obj.disk_state = \
1102
        dict((key, cls._ContainerFromDicts(value, dict, NodeDiskState))
1103
             for (key, value) in obj.disk_state.items())
1104

  
1105
    return obj
1106

  
1038 1107

  
1039 1108
class NodeGroup(TaggableObject):
1040 1109
  """Config object representing a node group."""
b/test/ganeti.objects_unittest.py
283 283
    self.assertRaises(errors.OpPrereqError, inst.FindDisk, 1)
284 284

  
285 285

  
286
class TestNode(unittest.TestCase):
287
  def testEmpty(self):
288
    self.assertEqual(objects.Node().ToDict(), {})
289
    self.assertTrue(isinstance(objects.Node.FromDict({}), objects.Node))
290

  
291
  def testHvState(self):
292
    node = objects.Node(name="node18157.example.com", hv_state={
293
      constants.HT_XEN_HVM: objects.NodeHvState(cpu_total=64),
294
      constants.HT_KVM: objects.NodeHvState(cpu_node=1),
295
      })
296

  
297
    node2 = objects.Node.FromDict(node.ToDict())
298

  
299
    # Make sure nothing can reference it anymore
300
    del node
301

  
302
    self.assertEqual(node2.name, "node18157.example.com")
303
    self.assertEqual(frozenset(node2.hv_state), frozenset([
304
      constants.HT_XEN_HVM,
305
      constants.HT_KVM,
306
      ]))
307
    self.assertEqual(node2.hv_state[constants.HT_KVM].cpu_node, 1)
308
    self.assertEqual(node2.hv_state[constants.HT_XEN_HVM].cpu_total, 64)
309

  
310
  def testDiskState(self):
311
    node = objects.Node(name="node32087.example.com", disk_state={
312
      constants.LD_LV: {
313
        "lv32352": objects.NodeDiskState(total=128),
314
        "lv2082": objects.NodeDiskState(total=512),
315
        },
316
      })
317

  
318
    node2 = objects.Node.FromDict(node.ToDict())
319

  
320
    # Make sure nothing can reference it anymore
321
    del node
322

  
323
    self.assertEqual(node2.name, "node32087.example.com")
324
    self.assertEqual(frozenset(node2.disk_state), frozenset([
325
      constants.LD_LV,
326
      ]))
327
    self.assertEqual(frozenset(node2.disk_state[constants.LD_LV]), frozenset([
328
      "lv32352",
329
      "lv2082",
330
      ]))
331
    self.assertEqual(node2.disk_state[constants.LD_LV]["lv2082"].total, 512)
332
    self.assertEqual(node2.disk_state[constants.LD_LV]["lv32352"].total, 128)
333

  
334

  
286 335
if __name__ == '__main__':
287 336
  testutils.GanetiTestProgram()

Also available in: Unified diff