blob: 8d59f8231fb9e3c7751fe0ece25526ba0de0678c [file] [log] [blame]
Mike Frysingerd03e6b52019-08-03 12:49:01 -04001#!/usr/bin/python2
Gwendal Grignou427c7b22017-10-26 17:39:29 -07002
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +01003__author__ = "kerl@google.com, gwendal@google.com (Gwendal Grignou)"
Gwendal Grignou427c7b22017-10-26 17:39:29 -07004
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +01005import io
Emil Lundmarkae094912019-02-19 16:25:52 +01006import mock
Gwendal Grignou427c7b22017-10-26 17:39:29 -07007import unittest
8
9from autotest_lib.client.bin import utils
10
Emil Lundmarkae094912019-02-19 16:25:52 +010011_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
Gwendal Grignou427c7b22017-10-26 17:39:29 -070018class TestUtils(unittest.TestCase):
19 """Test utils functions."""
20
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +010021 # 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))
Gwendal Grignou427c7b22017-10-26 17:39:29 -070031
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
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +010037 # 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):
Kristoffer Erlandsson82b9f322017-11-07 12:20:38 +010043 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):
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +010059 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,
Kristoffer Erlandsson82b9f322017-11-07 12:20:38 +010066 'iowait': 0,
67 'irq': 0,
68 'softirq': 0,
69 'steal': 0,
70 'guest': 0,
71 'guest_nice': 0
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +010072 }, usage)
Gwendal Grignou427c7b22017-10-26 17:39:29 -070073
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +010074 def test_compute_active_cpu_time(self):
75 start_usage = {
76 'user': 900,
77 'nice': 10,
78 'system': 90,
79 'idle': 10000,
Kristoffer Erlandsson82b9f322017-11-07 12:20:38 +010080 'iowait': 500,
81 'irq': 100,
82 'softirq': 50,
83 'steal': 150,
84 'guest': 170,
85 'guest_nice': 30
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +010086 }
87 end_usage = {
88 'user': 1800,
89 'nice': 20,
90 'system': 180,
Kristoffer Erlandsson82b9f322017-11-07 12:20:38 +010091 'idle': 13000,
92 'iowait': 2000,
93 'irq': 200,
94 'softirq': 100,
95 'steal': 300,
96 'guest': 340,
97 'guest_nice': 60
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +010098 }
99 usage = utils.compute_active_cpu_time(start_usage, end_usage)
Kristoffer Erlandssona6928852017-11-09 10:04:31 +0100100 self.assertAlmostEqual(usage, 0.25)
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +0100101
102 def test_compute_active_cpu_time_idle(self):
103 start_usage = {
104 'user': 900,
105 'nice': 10,
106 'system': 90,
107 'idle': 10000,
Kristoffer Erlandsson82b9f322017-11-07 12:20:38 +0100108 'iowait': 500,
109 'irq': 100,
110 'softirq': 50,
111 'steal': 150,
112 'guest': 170,
113 'guest_nice':30
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +0100114 }
115 end_usage = {
116 'user': 900,
117 'nice': 10,
118 'system': 90,
119 'idle': 11000,
Kristoffer Erlandsson82b9f322017-11-07 12:20:38 +0100120 'iowait': 1000,
121 'irq': 100,
122 'softirq': 50,
123 'steal': 150,
124 'guest': 170,
125 'guest_nice':30
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +0100126 }
127 usage = utils.compute_active_cpu_time(start_usage, end_usage)
Kristoffer Erlandssona6928852017-11-09 10:04:31 +0100128 self.assertAlmostEqual(usage, 0)
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +0100129
130 def test_get_mem_total(self):
Kristoffer Erlandssonb84da2b2017-11-01 21:29:24 +0100131 self.fake_file_text = ('MemTotal: 2048000 kB\n'
132 'MemFree: 307200 kB\n'
133 'Buffers: 102400 kB\n'
134 'Cached: 204800 kB\n')
Kristoffer Erlandssona6928852017-11-09 10:04:31 +0100135 self.assertAlmostEqual(utils.get_mem_total(), 2000)
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +0100136
137 def test_get_mem_free(self):
Kristoffer Erlandssonb84da2b2017-11-01 21:29:24 +0100138 self.fake_file_text = ('MemTotal: 2048000 kB\n'
139 'MemFree: 307200 kB\n'
140 'Buffers: 102400 kB\n'
141 'Cached: 204800 kB\n')
Kristoffer Erlandssona6928852017-11-09 10:04:31 +0100142 self.assertAlmostEqual(utils.get_mem_free(), 300)
Kristoffer Erlandssonb84da2b2017-11-01 21:29:24 +0100143
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')
Kristoffer Erlandssona6928852017-11-09 10:04:31 +0100149 self.assertAlmostEqual(utils.get_mem_free_plus_buffers_and_cached(),
150 600)
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +0100151
Kuo-Hsin Yang9cd699c2018-03-23 12:13:27 +0800152 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
Kristoffer Erlandsson58776682017-11-06 11:32:49 +0100162 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
Emil Lundmarkae094912019-02-19 16:25:52 +0100166 @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)
Jack Rosenthald8f92732023-01-31 21:14:04 -0700177
178 def test_ec_version_from_chardev_contents_ro(self):
179 contents = "1.0.0\nRO_VERSION\nRW_VERSION\nread-only\n"
180 self.assertEqual(
181 utils.get_ec_version_from_chardev_contents(contents),
182 "RO_VERSION",
183 )
184
185 def test_ec_version_from_chardev_contents_rw(self):
186 contents = "1.0.0\nRO_VERSION\nRW_VERSION\nread-write\n"
187 self.assertEqual(
188 utils.get_ec_version_from_chardev_contents(contents),
189 "RW_VERSION",
190 )
191
192 def test_ec_version_from_chardev_contents_garbage(self):
193 contents = "garbage\n"
194 self.assertEqual(
195 utils.get_ec_version_from_chardev_contents(contents),
196 "",
197 )