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..d6589d34ce78813786aac42816898f8c376a62e5 |
| --- /dev/null |
| +++ b/scripts/slave/recipe_modules/chromium_android/tests/spawn_device_temp_monitor_test.py |
| @@ -0,0 +1,139 @@ |
| +#!/usr/bin/env python |
| + |
| +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( |
| + 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 |
| + |
| +class MainFuncTest(auto_stub.TestCase): |
| + |
| + def setUp(self): |
| + # Collect calls to 'subprocess.Popen', which calls send_ts_mon_values.py |
| + 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" |
|
luqui
2015/09/03 01:10:41
else:
die horribly
bpastene
2015/09/04 03:47:21
Done.
|
| + |
| + 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', None) |
| + |
| + # Should build args to send_ts_mon_values correctly |
| + 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.
|
| + 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]) |
| + self.assertEquals('--float', self.send_ts_mon_call[2]) |
| + self.assertEquals( |
| + '{"name": "metric_prefix/device_serial_1/cpu_temp", "value": 123}', |
| + self.send_ts_mon_call[3]) |
| + self.assertEquals('--float', self.send_ts_mon_call[4]) |
| + self.assertEquals( |
| + '{"name": "metric_prefix/device_serial_1/battery_temp", "value": 456}', |
| + self.send_ts_mon_call[5]) |
| + |
| + 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', |
| + None) |
| + |
| + # 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]) |
| + |
| + def test_pid_file_creation(self): |
| + m_open = mock.mock_open() |
| + m_open.return_value.readline = lambda: "789" |
| + with mock.patch('__builtin__.open', m_open): |
| + spawn_device_temp_monitor.main( |
| + '/some/adb/path', |
| + '[]', |
| + 'metric_prefix', |
| + '/tmp/tmp_pid') |
| + self.assertEquals(1, len(self.kill_calls)) |
| + self.assertEquals(789, self.kill_calls[0][0]) |
| + self.assertEquals(signal.SIGTERM, self.kill_calls[0][1]) |
| + |
| +if __name__ == '__main__': |
| + unittest.main() |