Gwendal Grignou | 427c7b2 | 2017-10-26 17:39:29 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Kristoffer Erlandsson | 1ad7db0 | 2017-11-01 11:28:44 +0100 | [diff] [blame] | 3 | __author__ = "kerl@google.com, gwendal@google.com (Gwendal Grignou)" |
Gwendal Grignou | 427c7b2 | 2017-10-26 17:39:29 -0700 | [diff] [blame] | 4 | |
Kristoffer Erlandsson | 1ad7db0 | 2017-11-01 11:28:44 +0100 | [diff] [blame] | 5 | import io |
Gwendal Grignou | 427c7b2 | 2017-10-26 17:39:29 -0700 | [diff] [blame] | 6 | import unittest |
| 7 | |
| 8 | from autotest_lib.client.bin import utils |
| 9 | |
| 10 | class TestUtils(unittest.TestCase): |
| 11 | """Test utils functions.""" |
| 12 | |
Kristoffer Erlandsson | 1ad7db0 | 2017-11-01 11:28:44 +0100 | [diff] [blame] | 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)) |
Gwendal Grignou | 427c7b2 | 2017-10-26 17:39:29 -0700 | [diff] [blame] | 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 | |
Kristoffer Erlandsson | 1ad7db0 | 2017-11-01 11:28:44 +0100 | [diff] [blame] | 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\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 Grignou | 427c7b2 | 2017-10-26 17:39:29 -0700 | [diff] [blame] | 43 | |
Kristoffer Erlandsson | 1ad7db0 | 2017-11-01 11:28:44 +0100 | [diff] [blame] | 44 | 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 Erlandsson | b84da2b | 2017-11-01 21:29:24 +0100 | [diff] [blame^] | 77 | 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 Erlandsson | 1ad7db0 | 2017-11-01 11:28:44 +0100 | [diff] [blame] | 82 | |
| 83 | def test_get_mem_free(self): |
Kristoffer Erlandsson | b84da2b | 2017-11-01 21:29:24 +0100 | [diff] [blame^] | 84 | 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 Erlandsson | 1ad7db0 | 2017-11-01 11:28:44 +0100 | [diff] [blame] | 96 | |
| 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 Grignou | 427c7b2 | 2017-10-26 17:39:29 -0700 | [diff] [blame] | 105 | |