OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
ghost stip (do not use)
2015/09/08 21:46:36
you should append a LICENSE directive at the top.
bpastene
2015/09/09 00:02:19
Done.
| |
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( | |
ghost stip (do not use)
2015/09/08 21:46:37
I wonder if you can use https://chromium.googlesou
bpastene
2015/09/09 00:02:19
It couldn't find unittests/test_env
| |
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 | |
ghost stip (do not use)
2015/09/08 21:46:37
two spaces for top-level import
bpastene
2015/09/09 00:02:19
Done.
| |
29 class MainFuncTest(auto_stub.TestCase): | |
30 | |
ghost stip (do not use)
2015/09/08 21:46:37
remove spaces
bpastene
2015/09/09 00:02:19
Done.
| |
31 def setUp(self): | |
32 # Collect calls to 'subprocess.Popen', which calls send_ts_mon_values.py | |
ghost stip (do not use)
2015/09/08 21:46:36
period after comment
bpastene
2015/09/09 00:02:19
Done.
| |
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" | |
71 else: | |
72 self.fail('Unexpected adb command: %s' % (' '.join(args))) | |
73 | |
74 self.mock( | |
75 spawn_device_temp_monitor.subprocess, | |
76 'check_output', | |
77 mocked_adb_calls) | |
78 spawn_device_temp_monitor.main( | |
79 '/some/adb/path', | |
80 '["device_serial_1"]', | |
81 'metric_prefix') | |
82 | |
83 # Should build args to send_ts_mon_values correctly | |
84 expected_cmd = [spawn_device_temp_monitor._RUN_PY, | |
85 'infra.tools.send_ts_mon_values', | |
86 '--float', | |
87 '{"name": "metric_prefix/device_serial_1/cpu_temp", "value": 123}', | |
88 '--float', | |
89 '{"name": "metric_prefix/device_serial_1/battery_temp", "value": 456}'] | |
90 self.assertEquals(expected_cmd, self.send_ts_mon_call) | |
91 | |
92 def test_main_unresponsive_device(self): | |
93 # Collect calls to 'subprocess.check_output', which calls adb, and | |
94 # simulate an unresponsive device | |
95 adb_calls = [] | |
96 def mocked_adb_calls(args): | |
97 adb_calls.append(args) | |
98 raise subprocess.CalledProcessError | |
99 | |
100 self.mock( | |
101 spawn_device_temp_monitor.subprocess, | |
102 'check_output', | |
103 mocked_adb_calls) | |
104 spawn_device_temp_monitor.main( | |
105 '/some/adb/path', | |
106 '["device_serial_1"]', | |
107 'metric_prefix') | |
108 | |
109 # Should build args to send_ts_mon_values without any metrics | |
110 self.assertEquals(2, len(self.send_ts_mon_call)) | |
111 self.assertEquals( | |
112 spawn_device_temp_monitor._RUN_PY, | |
113 self.send_ts_mon_call[0]) | |
114 self.assertEquals( | |
115 'infra.tools.send_ts_mon_values', | |
116 self.send_ts_mon_call[1]) | |
117 | |
ghost stip (do not use)
2015/09/08 21:46:37
two spaces for top-level declarations
bpastene
2015/09/09 00:02:19
Done.
| |
118 if __name__ == '__main__': | |
119 unittest.main() | |
OLD | NEW |