Home | History | Annotate | Download | only in bin
      1 #!/usr/bin/python
      2 
      3 __author__ = "kerl (at] google.com, gwendal (at] google.com (Gwendal Grignou)"
      4 
      5 import io
      6 import unittest
      7 
      8 from autotest_lib.client.bin import utils
      9 
     10 class TestUtils(unittest.TestCase):
     11     """Test utils functions."""
     12 
     13     # Test methods, disable missing-docstring
     14     # pylint: disable=missing-docstring
     15     def setUp(self):
     16         utils._open_file = self.fake_open
     17         # Files opened with utils._open_file will contain this string.
     18         self.fake_file_text = ''
     19 
     20     def fake_open(self, path):
     21         # Use BytesIO instead of StringIO to support with statements.
     22         return io.BytesIO(bytes(self.fake_file_text))
     23 
     24     def test_concat_partition(self):
     25         self.assertEquals("nvme0n1p3", utils.concat_partition("nvme0n1", 3))
     26         self.assertEquals("mmcblk1p3", utils.concat_partition("mmcblk1", 3))
     27         self.assertEquals("sda3", utils.concat_partition("sda", 3))
     28 
     29     # The columns in /proc/stat are:
     30     # user nice system idle iowait irq softirq steal guest guest_nice
     31     #
     32     # Although older kernel versions might not contain all of them.
     33     # Unit is 1/100ths of a second.
     34     def test_get_cpu_usage(self):
     35         self.fake_file_text = 'cpu 254544 9 254768 2859878 1 2 3 4 5 6\n'
     36         usage = utils.get_cpu_usage()
     37         self.assertEquals({
     38             'user': 254544,
     39             'nice': 9,
     40             'system': 254768,
     41             'idle': 2859878,
     42             'iowait': 1,
     43             'irq': 2,
     44             'softirq': 3,
     45             'steal': 4,
     46             'guest': 5,
     47             'guest_nice': 6
     48         }, usage)
     49 
     50     def test_get_cpu_missing_columns(self):
     51         self.fake_file_text = 'cpu 254544 9 254768 2859878\n'
     52         usage = utils.get_cpu_usage()
     53         self.assertEquals({
     54             'user': 254544,
     55             'nice': 9,
     56             'system': 254768,
     57             'idle': 2859878,
     58             'iowait': 0,
     59             'irq': 0,
     60             'softirq': 0,
     61             'steal': 0,
     62             'guest': 0,
     63             'guest_nice': 0
     64         }, usage)
     65 
     66     def test_compute_active_cpu_time(self):
     67         start_usage = {
     68             'user': 900,
     69             'nice': 10,
     70             'system': 90,
     71             'idle': 10000,
     72             'iowait': 500,
     73             'irq': 100,
     74             'softirq': 50,
     75             'steal': 150,
     76             'guest': 170,
     77             'guest_nice': 30
     78         }
     79         end_usage = {
     80             'user': 1800,
     81             'nice': 20,
     82             'system': 180,
     83             'idle': 13000,
     84             'iowait': 2000,
     85             'irq': 200,
     86             'softirq': 100,
     87             'steal': 300,
     88             'guest': 340,
     89             'guest_nice': 60
     90         }
     91         usage = utils.compute_active_cpu_time(start_usage, end_usage)
     92         self.assertAlmostEqual(usage, 0.25)
     93 
     94     def test_compute_active_cpu_time_idle(self):
     95         start_usage = {
     96             'user': 900,
     97             'nice': 10,
     98             'system': 90,
     99             'idle': 10000,
    100             'iowait': 500,
    101             'irq': 100,
    102             'softirq': 50,
    103             'steal': 150,
    104             'guest': 170,
    105             'guest_nice':30
    106         }
    107         end_usage = {
    108             'user': 900,
    109             'nice': 10,
    110             'system': 90,
    111             'idle': 11000,
    112             'iowait': 1000,
    113             'irq': 100,
    114             'softirq': 50,
    115             'steal': 150,
    116             'guest': 170,
    117             'guest_nice':30
    118         }
    119         usage = utils.compute_active_cpu_time(start_usage, end_usage)
    120         self.assertAlmostEqual(usage, 0)
    121 
    122     def test_get_mem_total(self):
    123         self.fake_file_text = ('MemTotal:  2048000 kB\n'
    124                                'MemFree:  307200 kB\n'
    125                                'Buffers:  102400 kB\n'
    126                                'Cached:   204800 kB\n')
    127         self.assertAlmostEqual(utils.get_mem_total(), 2000)
    128 
    129     def test_get_mem_free(self):
    130         self.fake_file_text = ('MemTotal:  2048000 kB\n'
    131                                'MemFree:  307200 kB\n'
    132                                'Buffers:  102400 kB\n'
    133                                'Cached:   204800 kB\n')
    134         self.assertAlmostEqual(utils.get_mem_free(), 300)
    135 
    136     def test_get_mem_free_plus_buffers_and_cached(self):
    137         self.fake_file_text = ('MemTotal:  2048000 kB\n'
    138                                'MemFree:  307200 kB\n'
    139                                'Buffers:  102400 kB\n'
    140                                'Cached:   204800 kB\n')
    141         self.assertAlmostEqual(utils.get_mem_free_plus_buffers_and_cached(),
    142                                600)
    143 
    144     def test_get_num_allocated_file_handles(self):
    145         self.fake_file_text = '123 0 456\n'
    146         self.assertEqual(utils.get_num_allocated_file_handles(), 123)
    147 
    148