Add equivalence operators to NUMACell and NUMAPagesTopology

This makes the next patch in the series cleaner.

Change-Id: Idf1e6ed8fcd36a61d7c361023bbf6eef45b02ea0
This commit is contained in:
Dan Smith 2015-03-05 10:12:42 -08:00
parent 5390aa9b7e
commit d30605f730
2 changed files with 108 additions and 0 deletions

View File

@ -20,6 +20,20 @@ from nova.objects import fields
from nova.virt import hardware
def all_things_equal(obj_a, obj_b):
for name in obj_a.fields:
set_a = obj_a.obj_attr_is_set(name)
set_b = obj_b.obj_attr_is_set(name)
if set_a != set_b:
return False
elif not set_a:
continue
if getattr(obj_a, name) != getattr(obj_b, name):
return False
return True
# TODO(berrange): Remove NovaObjectDictCompat
class NUMACell(base.NovaObject,
base.NovaObjectDictCompat):
@ -43,6 +57,12 @@ class NUMACell(base.NovaObject,
'mempages': [('1.2', '1.0')]
}
def __eq__(self, other):
return all_things_equal(self, other)
def __ne__(self, other):
return not (self == other)
@property
def free_cpus(self):
return self.cpuset - self.pinned_cpus or set()
@ -122,6 +142,12 @@ class NUMAPagesTopology(base.NovaObject,
'used': fields.IntegerField(default=0),
}
def __eq__(self, other):
return all_things_equal(self, other)
def __ne__(self, other):
return not (self == other)
@property
def free(self):
"""Returns the number of avail pages."""

View File

@ -111,6 +111,88 @@ class _TestNUMA(object):
cell = objects.NUMATopology.obj_from_primitive(prim).cells[0]
self.assertEqual(cell_ver, cell.VERSION)
def test_numa_pages_equivalent(self):
pt1 = objects.NUMAPagesTopology(size_kb=1024, total=32, used=0)
pt2 = objects.NUMAPagesTopology(size_kb=1024, total=32, used=0)
self.assertEqual(pt1, pt2)
def test_numa_pages_not_equivalent(self):
pt1 = objects.NUMAPagesTopology(size_kb=1024, total=32, used=0)
pt2 = objects.NUMAPagesTopology(size_kb=1024, total=33, used=0)
self.assertNotEqual(pt1, pt2)
def test_numa_pages_not_equivalent_missing_a(self):
pt1 = objects.NUMAPagesTopology(size_kb=1024, used=0)
pt2 = objects.NUMAPagesTopology(size_kb=1024, total=32, used=0)
self.assertNotEqual(pt1, pt2)
def test_numa_pages_not_equivalent_missing_b(self):
pt1 = objects.NUMAPagesTopology(size_kb=1024, total=32, used=0)
pt2 = objects.NUMAPagesTopology(size_kb=1024, used=0)
self.assertNotEqual(pt1, pt2)
def test_numa_cell_equivalent(self):
cell1 = objects.NUMACell(id=1, cpuset=set([1, 2]), memory=32,
cpu_usage=10, pinned_cpus=set([3, 4]),
siblings=[set([5, 6])])
cell2 = objects.NUMACell(id=1, cpuset=set([1, 2]), memory=32,
cpu_usage=10, pinned_cpus=set([3, 4]),
siblings=[set([5, 6])])
self.assertEqual(cell1, cell2)
def test_numa_cell_not_equivalent(self):
cell1 = objects.NUMACell(id=1, cpuset=set([1, 2]), memory=32,
cpu_usage=10, pinned_cpus=set([3, 4]),
siblings=[set([5, 6])])
cell2 = objects.NUMACell(id=2, cpuset=set([1, 2]), memory=32,
cpu_usage=10, pinned_cpus=set([3, 4]),
siblings=[set([5, 6])])
self.assertNotEqual(cell1, cell2)
def test_numa_cell_not_equivalent_missing_a(self):
cell1 = objects.NUMACell(id=1, cpuset=set([1, 2]), memory=32,
pinned_cpus=set([3, 4]),
siblings=[set([5, 6])])
cell2 = objects.NUMACell(id=2, cpuset=set([1, 2]), memory=32,
cpu_usage=10, pinned_cpus=set([3, 4]),
siblings=[set([5, 6])])
self.assertNotEqual(cell1, cell2)
def test_numa_cell_not_equivalent_missing_b(self):
cell1 = objects.NUMACell(id=1, cpuset=set([1, 2]), memory=32,
cpu_usage=10, pinned_cpus=set([3, 4]),
siblings=[set([5, 6])])
cell2 = objects.NUMACell(id=2, cpuset=set([1, 2]), memory=32,
pinned_cpus=set([3, 4]),
siblings=[set([5, 6])])
self.assertNotEqual(cell1, cell2)
def test_numa_cell_equivalent_different_pages(self):
pt1 = objects.NUMAPagesTopology(size_kb=1024, total=32, used=0)
pt2 = objects.NUMAPagesTopology(size_kb=1024, total=32, used=0)
cell1 = objects.NUMACell(id=1, cpuset=set([1, 2]), memory=32,
cpu_usage=10, pinned_cpus=set([3, 4]),
siblings=[set([5, 6])],
mempages=[pt1])
cell2 = objects.NUMACell(id=1, cpuset=set([1, 2]), memory=32,
cpu_usage=10, pinned_cpus=set([3, 4]),
siblings=[set([5, 6])],
mempages=[pt2])
self.assertEqual(cell1, cell2)
def test_numa_cell_not_equivalent_different_pages(self):
pt1 = objects.NUMAPagesTopology(size_kb=1024, total=32, used=0)
pt2 = objects.NUMAPagesTopology(size_kb=1024, total=32, used=1)
cell1 = objects.NUMACell(id=1, cpuset=set([1, 2]), memory=32,
cpu_usage=10, pinned_cpus=set([3, 4]),
siblings=[set([5, 6])],
mempages=[pt1])
cell2 = objects.NUMACell(id=1, cpuset=set([1, 2]), memory=32,
cpu_usage=10, pinned_cpus=set([3, 4]),
siblings=[set([5, 6])],
mempages=[pt2])
self.assertNotEqual(cell1, cell2)
class TestNUMA(test_objects._LocalTest,
_TestNUMA):