Best Python code snippet using avocado_python
memory.py
Source:memory.py  
...281        numa_pattern = r"(^[\dabcdfe]+)\s+.*%s[=:](\d+)" % key282        for address, number in re.findall(numa_pattern, numa_map_info, re.M):283            numa_maps_dict[address] = number284        return numa_maps_dict285def _get_buddy_info_content():286    buddy_info_content = ''287    with open("/proc/buddyinfo") as buddy_info:  # pylint: disable=W1514288        buddy_info_content = buddy_info.read()289    return buddy_info_content290def get_buddy_info(chunk_sizes, nodes="all", zones="all"):291    """292    Get the fragment status of the host.293    It uses the same method to get the page size in buddyinfo. The expression294    to evaluate it is::295        2^chunk_size * page_size296    The chunk_sizes can be string make up by all orders that you want to check297    split with blank or a mathematical expression with ``>``, ``<`` or ``=``.298    For example:299        * The input of chunk_size could be: ``0 2 4``, and the return  will be300          ``{'0': 3, '2': 286, '4': 687}``301        * If you are using expression: ``>=9`` the return will be302          ``{'9': 63, '10': 225}``303    :param chunk_size: The order number shows in buddyinfo. This is not304                       the real page size.305    :type chunk_size: string306    :param nodes: The numa node that you want to check. Default value is all307    :type nodes: string308    :param zones: The memory zone that you want to check. Default value is all309    :type zones: string310    :return: A dict using the chunk_size as the keys311    :rtype: dict312    """313    buddy_info_content = _get_buddy_info_content()314    re_buddyinfo = r"Node\s+"315    if nodes == "all":316        re_buddyinfo += r"(\d+)"317    else:318        re_buddyinfo += "(%s)" % "|".join(nodes.split())319    if not re.findall(re_buddyinfo, buddy_info_content):320        LOG.warning("Can not find Nodes %s", nodes)321        return None322    re_buddyinfo += r".*?zone\s+"323    if zones == "all":324        re_buddyinfo += r"(\w+)"325    else:326        re_buddyinfo += "(%s)" % "|".join(zones.split())327    if not re.findall(re_buddyinfo, buddy_info_content):...test_utils_memory.py
Source:test_utils_memory.py  
1import unittest.mock2from avocado.utils import memory3class Test(unittest.TestCase):4    def test_numa_nodes_with_memory(self):5        file_values = [u"0\n", u"1-3", u"0-1,12-14\n"]6        expected_values = [[0], [1, 2, 3], [0, 1, 12, 13, 14]]7        for value, exp in zip(file_values, expected_values):8            with unittest.mock.patch('os.path.exists', return_value=True):9                with unittest.mock.patch('avocado.utils.genio.read_file',10                                         return_value=value):11                    self.assertEqual(memory.numa_nodes_with_memory(), exp)12BUDDY_INFO_RESPONSE = (13    """Node 0, zone      DMA      1      1      0      0      1      114Node 0, zone    DMA32    987    679   1004   3068   2795   143215Node 1, zone   Normal   5430   9759   9044   9751  16482   8924""")16@unittest.mock.patch('avocado.utils.memory._get_buddy_info_content',17                     return_value=BUDDY_INFO_RESPONSE)18class GetBuddyInfo(unittest.TestCase):19    def test_simple_chunk_size(self, buddy_mocked):20        chunk_size = '0'21        result = memory.get_buddy_info(chunk_size)22        self.assertEqual(result[chunk_size], 6418)23        self.assertTrue(buddy_mocked.called)24    def test_less_than_chunk_size(self, buddy_mocked):25        chunk_size = '<2'26        result = memory.get_buddy_info(chunk_size)27        self.assertEqual(result['0'], 6418)28        self.assertEqual(result['1'], 10439)29        self.assertTrue(buddy_mocked.called)30    def test_less_than_equal_chunk_size(self, buddy_mocked):31        chunk_size = '<=2'32        result = memory.get_buddy_info(chunk_size)33        self.assertEqual(result['0'], 6418)34        self.assertEqual(result['1'], 10439)35        self.assertEqual(result['2'], 10048)36        self.assertTrue(buddy_mocked.called)37    def test_greater_than_chunk_size(self, buddy_mocked):38        chunk_size = '>3'39        result = memory.get_buddy_info(chunk_size)40        self.assertEqual(result['4'], 19278)41        self.assertEqual(result['5'], 10357)42        self.assertTrue(buddy_mocked.called)43    def test_greater_than_equal_chunk_size(self, buddy_mocked):44        chunk_size = '>=3'45        result = memory.get_buddy_info(chunk_size)46        self.assertEqual(result['3'], 12819)47        self.assertEqual(result['4'], 19278)48        self.assertEqual(result['5'], 10357)49        self.assertTrue(buddy_mocked.called)50    def test_multiple_chunk_size(self, buddy_mocked):51        chunk_size = '2 4'52        result = memory.get_buddy_info(chunk_size)53        self.assertEqual(result['2'], 10048)54        self.assertEqual(result['4'], 19278)55        self.assertTrue(buddy_mocked.called)56    def test_multiple_chunk_size_filtering_simple(self, buddy_mocked):57        chunk_size = '>2 <4'58        result = memory.get_buddy_info(chunk_size)59        self.assertEqual(result['3'], 12819)60        self.assertTrue(buddy_mocked.called)61    def test_multiple_chunk_size_filtering(self, buddy_mocked):62        chunk_size = '>=2 <=4'63        result = memory.get_buddy_info(chunk_size)64        self.assertEqual(result['2'], 10048)65        self.assertEqual(result['3'], 12819)66        self.assertEqual(result['4'], 19278)67        self.assertTrue(buddy_mocked.called)68    def test_multiple_chunk_size_filtering_invalid(self, buddy_mocked):69        chunk_size = '>2 <2'70        result = memory.get_buddy_info(chunk_size)71        self.assertEqual(result, {})72        self.assertTrue(buddy_mocked.called)73    def test_filtering_node(self, buddy_mocked):74        chunk_size = '0'75        result = memory.get_buddy_info(chunk_size, nodes='1')76        self.assertEqual(result[chunk_size], 5430)77        self.assertTrue(buddy_mocked.called)78    def test_filtering_zone(self, buddy_mocked):79        chunk_size = '0'80        result = memory.get_buddy_info(chunk_size, zones='DMA32')81        self.assertEqual(result[chunk_size], 987)82        self.assertTrue(buddy_mocked.called)83if __name__ == '__main__':...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
