未验证 提交 66982e73 编写于 作者: C Cleber Rosa

Merge remote-tracking branch 'cacarrara/add-tests-for-memory-get-buddy-info-v3'

Signed-off-by: NCleber Rosa <crosa@redhat.com>
...@@ -350,6 +350,13 @@ def read_from_numa_maps(pid, key): ...@@ -350,6 +350,13 @@ def read_from_numa_maps(pid, key):
return numa_maps_dict return numa_maps_dict
def _get_buddy_info_content():
buddy_info_content = ''
with open("/proc/buddyinfo") as buddy_info:
buddy_info_content = buddy_info.read()
return buddy_info_content
def get_buddy_info(chunk_sizes, nodes="all", zones="all"): def get_buddy_info(chunk_sizes, nodes="all", zones="all"):
""" """
Get the fragement status of the host. Get the fragement status of the host.
...@@ -378,8 +385,7 @@ def get_buddy_info(chunk_sizes, nodes="all", zones="all"): ...@@ -378,8 +385,7 @@ def get_buddy_info(chunk_sizes, nodes="all", zones="all"):
:return: A dict using the chunk_size as the keys :return: A dict using the chunk_size as the keys
:rtype: dict :rtype: dict
""" """
with open("/proc/buddyinfo") as buddy_info: buddy_info_content = _get_buddy_info_content()
buddy_info_content = buddy_info.read()
re_buddyinfo = r"Node\s+" re_buddyinfo = r"Node\s+"
if nodes == "all": if nodes == "all":
......
...@@ -20,5 +20,80 @@ class UtilsMemoryTest(unittest.TestCase): ...@@ -20,5 +20,80 @@ class UtilsMemoryTest(unittest.TestCase):
self.assertEqual(memory.numa_nodes_with_memory(), exp) self.assertEqual(memory.numa_nodes_with_memory(), exp)
BUDDY_INFO_RESPONSE = '\n'.join([
'Node 0, zone DMA 1 1 0 0 1 1',
'Node 0, zone DMA32 987 679 1004 3068 2795 1432',
'Node 1, zone Normal 5430 9759 9044 9751 16482 8924',
])
@mock.patch('avocado.utils.memory._get_buddy_info_content', return_value=BUDDY_INFO_RESPONSE)
class UtilsMemoryTestGetBuddyInfo(unittest.TestCase):
def test_get_buddy_info_simple_chunk_size(self, buddy_info_content_mocked):
chunk_size = '0'
result = memory.get_buddy_info(chunk_size)
self.assertEqual(result[chunk_size], 6418)
def test_get_buddy_info_less_than_chunk_size(self, buddy_info_content_mocked):
chunk_size = '<2'
result = memory.get_buddy_info(chunk_size)
self.assertEqual(result['0'], 6418)
self.assertEqual(result['1'], 10439)
def test_get_buddy_info_less_than_equal_chunk_size(self, buddy_info_content_mocked):
chunk_size = '<=2'
result = memory.get_buddy_info(chunk_size)
self.assertEqual(result['0'], 6418)
self.assertEqual(result['1'], 10439)
self.assertEqual(result['2'], 10048)
def test_get_buddy_info_greater_than_chunk_size(self, buddy_info_content_mocked):
chunk_size = '>3'
result = memory.get_buddy_info(chunk_size)
self.assertEqual(result['4'], 19278)
self.assertEqual(result['5'], 10357)
def test_get_buddy_info_greater_than_equal_chunk_size(self, buddy_info_content_mocked):
chunk_size = '>=3'
result = memory.get_buddy_info(chunk_size)
self.assertEqual(result['3'], 12819)
self.assertEqual(result['4'], 19278)
self.assertEqual(result['5'], 10357)
def test_get_buddy_info_multiple_chunk_size(self, buddy_info_content_mocked):
chunk_size = '2 4'
result = memory.get_buddy_info(chunk_size)
self.assertEqual(result['2'], 10048)
self.assertEqual(result['4'], 19278)
def test_get_buddy_info_multiple_chunk_size_filtering_simple(self, buddy_info_content_mocked):
chunk_size = '>2 <4'
result = memory.get_buddy_info(chunk_size)
self.assertEqual(result['3'], 12819)
def test_get_buddy_info_multiple_chunk_size_filtering(self, buddy_info_content_mocked):
chunk_size = '>=2 <=4'
result = memory.get_buddy_info(chunk_size)
self.assertEqual(result['2'], 10048)
self.assertEqual(result['3'], 12819)
self.assertEqual(result['4'], 19278)
def test_get_buddy_info_multiple_chunk_size_filtering_invalid(self, buddy_info_content_mocked):
chunk_size = '>2 <2'
result = memory.get_buddy_info(chunk_size)
self.assertEqual(result, {})
def test_get_buddy_info_filtering_node(self, buddy_info_content_mocked):
chunk_size = '0'
result = memory.get_buddy_info(chunk_size, nodes='1')
self.assertEqual(result[chunk_size], 5430)
def test_get_buddy_info_filtering_zone(self, buddy_info_content_mocked):
chunk_size = '0'
result = memory.get_buddy_info(chunk_size, zones='DMA32')
self.assertEqual(result[chunk_size], 987)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册