- Handle de-serialization correctly when pool is not defined - Don't serialize when the attribute is None (this should never happen in reality as the attribute is always set when de-serializing) - Add tests
Signed-off-by: Michael Hanselmann <[email protected]> --- lib/objects.py | 9 +++++++-- test/py/ganeti.objects_unittest.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/objects.py b/lib/objects.py index f8667e3..3332714 100644 --- a/lib/objects.py +++ b/lib/objects.py @@ -1623,7 +1623,8 @@ class Cluster(TaggableObject): """ mydict = super(Cluster, self).ToDict() - mydict["tcpudp_port_pool"] = list(self.tcpudp_port_pool) + if self.tcpudp_port_pool is not None: + mydict["tcpudp_port_pool"] = list(self.tcpudp_port_pool) return mydict @classmethod @@ -1632,8 +1633,12 @@ class Cluster(TaggableObject): """ obj = super(Cluster, cls).FromDict(val) - if not isinstance(obj.tcpudp_port_pool, set): + + if obj.tcpudp_port_pool is None: + obj.tcpudp_port_pool = set() + elif not isinstance(obj.tcpudp_port_pool, set): obj.tcpudp_port_pool = set(obj.tcpudp_port_pool) + return obj def SimpleFillDP(self, diskparams): diff --git a/test/py/ganeti.objects_unittest.py b/test/py/ganeti.objects_unittest.py index c29f83e..dc40c9a 100755 --- a/test/py/ganeti.objects_unittest.py +++ b/test/py/ganeti.objects_unittest.py @@ -216,6 +216,35 @@ class TestClusterObject(unittest.TestCase): self.assertEqual(self.fake_cl.primary_hypervisor, constants.HT_CHROOT) +class TestClusterObjectTcpUdpPortPool(unittest.TestCase): + def testNewCluster(self): + self.assertTrue(objects.Cluster().tcpudp_port_pool is None) + + def testSerializingEmpty(self): + self.assertEqual(objects.Cluster().ToDict(), {}) + + def testSerializing(self): + cluster = objects.Cluster.FromDict({}) + self.assertEqual(cluster.tcpudp_port_pool, set()) + + cluster.tcpudp_port_pool.add(3546) + cluster.tcpudp_port_pool.add(62511) + + data = cluster.ToDict() + self.assertEqual(data.keys(), ["tcpudp_port_pool"]) + self.assertEqual(sorted(data["tcpudp_port_pool"]), sorted([3546, 62511])) + + def testDeserializingEmpty(self): + cluster = objects.Cluster.FromDict({}) + self.assertEqual(cluster.tcpudp_port_pool, set()) + + def testDeserialize(self): + cluster = objects.Cluster.FromDict({ + "tcpudp_port_pool": [26214, 10039, 267], + }) + self.assertEqual(cluster.tcpudp_port_pool, set([26214, 10039, 267])) + + class TestOS(unittest.TestCase): ALL_DATA = [ "debootstrap", -- 1.8.1
