blob: 134004412320a46b33048ea6429d4804d2e07148 [file] [log] [blame]
Gwendal Grignou427c7b22017-10-26 17:39:29 -07001#!/usr/bin/python
2
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
Gwendal Grignou427c7b22017-10-26 17:39:29 -07006import unittest
7
8from autotest_lib.client.bin import utils
9
10class TestUtils(unittest.TestCase):
11 """Test utils functions."""
12
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +010013 # 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))
Gwendal Grignou427c7b22017-10-26 17:39:29 -070023
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
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +010029 # 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\n'
36 usage = utils.get_cpu_usage()
37 self.assertEquals({
38 'user': 254544,
39 'nice': 9,
40 'system': 254768,
41 'idle': 2859878,
42 }, usage)
Gwendal Grignou427c7b22017-10-26 17:39:29 -070043
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +010044 def test_compute_active_cpu_time(self):
45 start_usage = {
46 'user': 900,
47 'nice': 10,
48 'system': 90,
49 'idle': 10000,
50 }
51 end_usage = {
52 'user': 1800,
53 'nice': 20,
54 'system': 180,
55 'idle': 11000,
56 }
57 usage = utils.compute_active_cpu_time(start_usage, end_usage)
58 self.assert_is_close(usage, 0.5)
59
60 def test_compute_active_cpu_time_idle(self):
61 start_usage = {
62 'user': 900,
63 'nice': 10,
64 'system': 90,
65 'idle': 10000,
66 }
67 end_usage = {
68 'user': 900,
69 'nice': 10,
70 'system': 90,
71 'idle': 11000,
72 }
73 usage = utils.compute_active_cpu_time(start_usage, end_usage)
74 self.assert_is_close(usage, 0)
75
76 def test_get_mem_total(self):
Kristoffer Erlandssonb84da2b2017-11-01 21:29:24 +010077 self.fake_file_text = ('MemTotal: 2048000 kB\n'
78 'MemFree: 307200 kB\n'
79 'Buffers: 102400 kB\n'
80 'Cached: 204800 kB\n')
81 self.assert_is_close(utils.get_mem_total(), 2000)
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +010082
83 def test_get_mem_free(self):
Kristoffer Erlandssonb84da2b2017-11-01 21:29:24 +010084 self.fake_file_text = ('MemTotal: 2048000 kB\n'
85 'MemFree: 307200 kB\n'
86 'Buffers: 102400 kB\n'
87 'Cached: 204800 kB\n')
88 self.assert_is_close(utils.get_mem_free(), 300)
89
90 def test_get_mem_free_plus_buffers_and_cached(self):
91 self.fake_file_text = ('MemTotal: 2048000 kB\n'
92 'MemFree: 307200 kB\n'
93 'Buffers: 102400 kB\n'
94 'Cached: 204800 kB\n')
95 self.assert_is_close(utils.get_mem_free_plus_buffers_and_cached(), 600)
Kristoffer Erlandsson1ad7db02017-11-01 11:28:44 +010096
97 def assert_is_close(self, a, b, allowed_delta = 0.0000001):
98 """
99 Asserts that two floats are within the allowed delta of each other.
100 @param allowed_delta: The allowed delta between the two floats.
101 """
102 self.assertTrue(abs(a - b) < allowed_delta,
103 "%f and %f are not within %f of each other"
104 % (a, b, allowed_delta))
Gwendal Grignou427c7b22017-10-26 17:39:29 -0700105