Chromium Code Reviews| Index: scripts/slave/recipe_modules/chromium_android/tests/spawn_device_temp_monitor_test.py |
| diff --git a/scripts/slave/recipe_modules/chromium_android/tests/spawn_device_temp_monitor_test.py b/scripts/slave/recipe_modules/chromium_android/tests/spawn_device_temp_monitor_test.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..309c3324ae95a615efc3a4f98b70efcab507acb8 |
| --- /dev/null |
| +++ b/scripts/slave/recipe_modules/chromium_android/tests/spawn_device_temp_monitor_test.py |
| @@ -0,0 +1,119 @@ |
| +#!/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.
|
| + |
| +import json |
| +import os |
| +import shutil |
| +import signal |
| +import subprocess |
| +import sys |
| +import tempfile |
| +import unittest |
| + |
| +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| + |
| +# For 'test_env'. |
| +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
|
| + 0, os.path.abspath(os.path.join(THIS_DIR, '..', '..', '..', 'unittests'))) |
| +# For 'spawn_device_temp_monitor.py'. |
| +sys.path.insert( |
| + 0, os.path.abspath(os.path.join(THIS_DIR, '..', 'resources'))) |
| + |
| +# Imported for side effects on sys.path. |
| +import test_env |
| +import mock |
| + |
| +# In depot_tools/ |
| +from testing_support import auto_stub |
| +import spawn_device_temp_monitor |
| + |
|
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.
|
| +class MainFuncTest(auto_stub.TestCase): |
| + |
|
ghost stip (do not use)
2015/09/08 21:46:37
remove spaces
bpastene
2015/09/09 00:02:19
Done.
|
| + def setUp(self): |
| + # 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.
|
| + self.send_ts_mon_call = [] |
| + def mocked_ts_mon_calls(args): |
| + self.send_ts_mon_call = args |
| + self.mock( |
| + spawn_device_temp_monitor.subprocess, |
| + 'Popen', |
| + mocked_ts_mon_calls) |
| + |
| + # Make sleep throw sigterm to simulate a kill and break out of loop |
| + def mocked_sleep_call(duration): |
| + self.assertEquals(30, duration) |
| + raise spawn_device_temp_monitor.SigtermError() |
| + self.mock( |
| + spawn_device_temp_monitor.time, |
| + 'sleep', |
| + mocked_sleep_call) |
| + |
| + # Collect calls to os.kill |
| + self.kill_calls = [] |
| + def mocked_kill_call(pid, thrown_signal): |
| + self.kill_calls.append([pid, thrown_signal]) |
| + self.mock( |
| + spawn_device_temp_monitor.os, |
| + 'kill', |
| + mocked_kill_call) |
| + |
| + def test_main_responsive_device(self): |
| + # Collect calls to 'subprocess.check_output', which calls adb, and |
| + # simulate a responsive device |
| + adb_calls = [] |
| + def mocked_adb_calls(args): |
| + adb_calls.append(args) |
| + if args[4].startswith('grep'): |
| + return "some_thermal_file_name" |
| + elif args[4].startswith('cat'): |
| + return "123" |
| + elif args[4].startswith('dumpsys'): |
| + return "temperature: 456" |
| + else: |
| + self.fail('Unexpected adb command: %s' % (' '.join(args))) |
| + |
| + self.mock( |
| + spawn_device_temp_monitor.subprocess, |
| + 'check_output', |
| + mocked_adb_calls) |
| + spawn_device_temp_monitor.main( |
| + '/some/adb/path', |
| + '["device_serial_1"]', |
| + 'metric_prefix') |
| + |
| + # Should build args to send_ts_mon_values correctly |
| + expected_cmd = [spawn_device_temp_monitor._RUN_PY, |
| + 'infra.tools.send_ts_mon_values', |
| + '--float', |
| + '{"name": "metric_prefix/device_serial_1/cpu_temp", "value": 123}', |
| + '--float', |
| + '{"name": "metric_prefix/device_serial_1/battery_temp", "value": 456}'] |
| + self.assertEquals(expected_cmd, self.send_ts_mon_call) |
| + |
| + def test_main_unresponsive_device(self): |
| + # Collect calls to 'subprocess.check_output', which calls adb, and |
| + # simulate an unresponsive device |
| + adb_calls = [] |
| + def mocked_adb_calls(args): |
| + adb_calls.append(args) |
| + raise subprocess.CalledProcessError |
| + |
| + self.mock( |
| + spawn_device_temp_monitor.subprocess, |
| + 'check_output', |
| + mocked_adb_calls) |
| + spawn_device_temp_monitor.main( |
| + '/some/adb/path', |
| + '["device_serial_1"]', |
| + 'metric_prefix') |
| + |
| + # Should build args to send_ts_mon_values without any metrics |
| + self.assertEquals(2, len(self.send_ts_mon_call)) |
| + self.assertEquals( |
| + spawn_device_temp_monitor._RUN_PY, |
| + self.send_ts_mon_call[0]) |
| + self.assertEquals( |
| + 'infra.tools.send_ts_mon_values', |
| + self.send_ts_mon_call[1]) |
| + |
|
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.
|
| +if __name__ == '__main__': |
| + unittest.main() |