Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 import json | |
| 4 import os | |
| 5 import shutil | |
| 6 import signal | |
| 7 import subprocess | |
| 8 import sys | |
| 9 import tempfile | |
| 10 import unittest | |
| 11 | |
| 12 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| 13 | |
| 14 # For 'test_env'. | |
| 15 sys.path.insert( | |
| 16 0, os.path.abspath(os.path.join(THIS_DIR, '..', '..', '..', 'unittests'))) | |
| 17 # For 'spawn_device_temp_monitor.py'. | |
| 18 sys.path.insert( | |
| 19 0, os.path.abspath(os.path.join(THIS_DIR, '..', 'resources'))) | |
| 20 | |
| 21 # Imported for side effects on sys.path. | |
| 22 import test_env | |
| 23 import mock | |
| 24 | |
| 25 # In depot_tools/ | |
| 26 from testing_support import auto_stub | |
| 27 import spawn_device_temp_monitor | |
| 28 | |
| 29 class MainFuncTest(auto_stub.TestCase): | |
| 30 | |
| 31 def setUp(self): | |
| 32 # Collect calls to 'subprocess.Popen', which calls send_ts_mon_values.py | |
| 33 self.send_ts_mon_call = [] | |
| 34 def mocked_ts_mon_calls(args): | |
| 35 self.send_ts_mon_call = args | |
| 36 self.mock( | |
| 37 spawn_device_temp_monitor.subprocess, | |
| 38 'Popen', | |
| 39 mocked_ts_mon_calls) | |
| 40 | |
| 41 # Make sleep throw sigterm to simulate a kill and break out of loop | |
| 42 def mocked_sleep_call(duration): | |
| 43 self.assertEquals(30, duration) | |
| 44 raise spawn_device_temp_monitor.SigtermError() | |
| 45 self.mock( | |
| 46 spawn_device_temp_monitor.time, | |
| 47 'sleep', | |
| 48 mocked_sleep_call) | |
| 49 | |
| 50 # Collect calls to os.kill | |
| 51 self.kill_calls = [] | |
| 52 def mocked_kill_call(pid, thrown_signal): | |
| 53 self.kill_calls.append([pid, thrown_signal]) | |
| 54 self.mock( | |
| 55 spawn_device_temp_monitor.os, | |
| 56 'kill', | |
| 57 mocked_kill_call) | |
| 58 | |
| 59 def test_main_responsive_device(self): | |
| 60 # Collect calls to 'subprocess.check_output', which calls adb, and | |
| 61 # simulate a responsive device | |
| 62 adb_calls = [] | |
| 63 def mocked_adb_calls(args): | |
| 64 adb_calls.append(args) | |
| 65 if args[4].startswith('grep'): | |
| 66 return "some_thermal_file_name" | |
| 67 elif args[4].startswith('cat'): | |
| 68 return "123" | |
| 69 elif args[4].startswith('dumpsys'): | |
| 70 return "temperature: 456" | |
|
luqui
2015/09/03 01:10:41
else:
die horribly
bpastene
2015/09/04 03:47:21
Done.
| |
| 71 | |
| 72 self.mock( | |
| 73 spawn_device_temp_monitor.subprocess, | |
| 74 'check_output', | |
| 75 mocked_adb_calls) | |
| 76 spawn_device_temp_monitor.main( | |
| 77 '/some/adb/path', | |
| 78 '["device_serial_1"]', | |
| 79 'metric_prefix', None) | |
| 80 | |
| 81 # Should build args to send_ts_mon_values correctly | |
| 82 self.assertEquals(6, len(self.send_ts_mon_call)) | |
|
luqui
2015/09/03 01:10:41
Woah that's a lot of asserts, why not just assert
bpastene
2015/09/04 03:47:21
Done.
| |
| 83 self.assertEquals( | |
| 84 spawn_device_temp_monitor._RUN_PY, | |
| 85 self.send_ts_mon_call[0]) | |
| 86 self.assertEquals( | |
| 87 'infra.tools.send_ts_mon_values', | |
| 88 self.send_ts_mon_call[1]) | |
| 89 self.assertEquals('--float', self.send_ts_mon_call[2]) | |
| 90 self.assertEquals( | |
| 91 '{"name": "metric_prefix/device_serial_1/cpu_temp", "value": 123}', | |
| 92 self.send_ts_mon_call[3]) | |
| 93 self.assertEquals('--float', self.send_ts_mon_call[4]) | |
| 94 self.assertEquals( | |
| 95 '{"name": "metric_prefix/device_serial_1/battery_temp", "value": 456}', | |
| 96 self.send_ts_mon_call[5]) | |
| 97 | |
| 98 def test_main_unresponsive_device(self): | |
| 99 # Collect calls to 'subprocess.check_output', which calls adb, and | |
| 100 # simulate an unresponsive device | |
| 101 adb_calls = [] | |
| 102 def mocked_adb_calls(args): | |
| 103 adb_calls.append(args) | |
| 104 raise subprocess.CalledProcessError | |
| 105 | |
| 106 self.mock( | |
| 107 spawn_device_temp_monitor.subprocess, | |
| 108 'check_output', | |
| 109 mocked_adb_calls) | |
| 110 spawn_device_temp_monitor.main( | |
| 111 '/some/adb/path', | |
| 112 '["device_serial_1"]', | |
| 113 'metric_prefix', | |
| 114 None) | |
| 115 | |
| 116 # Should build args to send_ts_mon_values without any metrics | |
| 117 self.assertEquals(2, len(self.send_ts_mon_call)) | |
| 118 self.assertEquals( | |
| 119 spawn_device_temp_monitor._RUN_PY, | |
| 120 self.send_ts_mon_call[0]) | |
| 121 self.assertEquals( | |
| 122 'infra.tools.send_ts_mon_values', | |
| 123 self.send_ts_mon_call[1]) | |
| 124 | |
| 125 def test_pid_file_creation(self): | |
| 126 m_open = mock.mock_open() | |
| 127 m_open.return_value.readline = lambda: "789" | |
| 128 with mock.patch('__builtin__.open', m_open): | |
| 129 spawn_device_temp_monitor.main( | |
| 130 '/some/adb/path', | |
| 131 '[]', | |
| 132 'metric_prefix', | |
| 133 '/tmp/tmp_pid') | |
| 134 self.assertEquals(1, len(self.kill_calls)) | |
| 135 self.assertEquals(789, self.kill_calls[0][0]) | |
| 136 self.assertEquals(signal.SIGTERM, self.kill_calls[0][1]) | |
| 137 | |
| 138 if __name__ == '__main__': | |
| 139 unittest.main() | |
| OLD | NEW |