• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2
3__author__ = "kerl@google.com, gwendal@google.com (Gwendal Grignou)"
4
5import io
6import mock
7import unittest
8
9from autotest_lib.client.bin import utils
10
11_IOSTAT_OUTPUT = (
12    'Linux 3.8.11 (localhost)   02/19/19        _x86_64_        (4 CPU)\n'
13    '\n'
14    'Device            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn\n'
15    'ALL              4.45        10.33       292.40     665582     188458\n'
16    '\n')
17
18class TestUtils(unittest.TestCase):
19    """Test utils functions."""
20
21    # Test methods, disable missing-docstring
22    # pylint: disable=missing-docstring
23    def setUp(self):
24        utils._open_file = self.fake_open
25        # Files opened with utils._open_file will contain this string.
26        self.fake_file_text = ''
27
28    def fake_open(self, path):
29        # Use BytesIO instead of StringIO to support with statements.
30        return io.BytesIO(bytes(self.fake_file_text))
31
32    def test_concat_partition(self):
33        self.assertEquals("nvme0n1p3", utils.concat_partition("nvme0n1", 3))
34        self.assertEquals("mmcblk1p3", utils.concat_partition("mmcblk1", 3))
35        self.assertEquals("sda3", utils.concat_partition("sda", 3))
36
37    # The columns in /proc/stat are:
38    # user nice system idle iowait irq softirq steal guest guest_nice
39    #
40    # Although older kernel versions might not contain all of them.
41    # Unit is 1/100ths of a second.
42    def test_get_cpu_usage(self):
43        self.fake_file_text = 'cpu 254544 9 254768 2859878 1 2 3 4 5 6\n'
44        usage = utils.get_cpu_usage()
45        self.assertEquals({
46            'user': 254544,
47            'nice': 9,
48            'system': 254768,
49            'idle': 2859878,
50            'iowait': 1,
51            'irq': 2,
52            'softirq': 3,
53            'steal': 4,
54            'guest': 5,
55            'guest_nice': 6
56        }, usage)
57
58    def test_get_cpu_missing_columns(self):
59        self.fake_file_text = 'cpu 254544 9 254768 2859878\n'
60        usage = utils.get_cpu_usage()
61        self.assertEquals({
62            'user': 254544,
63            'nice': 9,
64            'system': 254768,
65            'idle': 2859878,
66            'iowait': 0,
67            'irq': 0,
68            'softirq': 0,
69            'steal': 0,
70            'guest': 0,
71            'guest_nice': 0
72        }, usage)
73
74    def test_compute_active_cpu_time(self):
75        start_usage = {
76            'user': 900,
77            'nice': 10,
78            'system': 90,
79            'idle': 10000,
80            'iowait': 500,
81            'irq': 100,
82            'softirq': 50,
83            'steal': 150,
84            'guest': 170,
85            'guest_nice': 30
86        }
87        end_usage = {
88            'user': 1800,
89            'nice': 20,
90            'system': 180,
91            'idle': 13000,
92            'iowait': 2000,
93            'irq': 200,
94            'softirq': 100,
95            'steal': 300,
96            'guest': 340,
97            'guest_nice': 60
98        }
99        usage = utils.compute_active_cpu_time(start_usage, end_usage)
100        self.assertAlmostEqual(usage, 0.25)
101
102    def test_compute_active_cpu_time_idle(self):
103        start_usage = {
104            'user': 900,
105            'nice': 10,
106            'system': 90,
107            'idle': 10000,
108            'iowait': 500,
109            'irq': 100,
110            'softirq': 50,
111            'steal': 150,
112            'guest': 170,
113            'guest_nice':30
114        }
115        end_usage = {
116            'user': 900,
117            'nice': 10,
118            'system': 90,
119            'idle': 11000,
120            'iowait': 1000,
121            'irq': 100,
122            'softirq': 50,
123            'steal': 150,
124            'guest': 170,
125            'guest_nice':30
126        }
127        usage = utils.compute_active_cpu_time(start_usage, end_usage)
128        self.assertAlmostEqual(usage, 0)
129
130    def test_get_mem_total(self):
131        self.fake_file_text = ('MemTotal:  2048000 kB\n'
132                               'MemFree:  307200 kB\n'
133                               'Buffers:  102400 kB\n'
134                               'Cached:   204800 kB\n')
135        self.assertAlmostEqual(utils.get_mem_total(), 2000)
136
137    def test_get_mem_free(self):
138        self.fake_file_text = ('MemTotal:  2048000 kB\n'
139                               'MemFree:  307200 kB\n'
140                               'Buffers:  102400 kB\n'
141                               'Cached:   204800 kB\n')
142        self.assertAlmostEqual(utils.get_mem_free(), 300)
143
144    def test_get_mem_free_plus_buffers_and_cached(self):
145        self.fake_file_text = ('MemTotal:  2048000 kB\n'
146                               'MemFree:  307200 kB\n'
147                               'Buffers:  102400 kB\n'
148                               'Cached:   204800 kB\n')
149        self.assertAlmostEqual(utils.get_mem_free_plus_buffers_and_cached(),
150                               600)
151
152    def test_get_meminfo(self):
153        self.fake_file_text = ('MemTotal:      2048000 kB\n'
154                               'MemFree:        307200 kB\n'
155                               'Buffers:        102400 kB\n'
156                               'Cached:         204800 kB\n'
157                               'Active(anon):   409600 kB')
158        meminfo = utils.get_meminfo()
159        self.assertEqual(meminfo.MemTotal, 2048000)
160        self.assertEqual(meminfo.Active_anon, 409600)
161
162    def test_get_num_allocated_file_handles(self):
163        self.fake_file_text = '123 0 456\n'
164        self.assertEqual(utils.get_num_allocated_file_handles(), 123)
165
166    @mock.patch('autotest_lib.client.common_lib.utils.system_output')
167    def test_get_storage_statistics(self, system_output_mock):
168        system_output_mock.return_value = _IOSTAT_OUTPUT
169        statistics = utils.get_storage_statistics()
170        self.assertEqual({
171            'read_kb': 665582.0,
172            'written_kb_per_s': 292.4,
173            'read_kb_per_s': 10.33,
174            'transfers_per_s': 4.45,
175            'written_kb': 188458.0,
176        }, statistics)
177