OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import json |
| 7 import os |
| 8 import shutil |
| 9 import signal |
| 10 import subprocess |
| 11 import sys |
| 12 import tempfile |
| 13 import unittest |
| 14 |
| 15 THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 16 |
| 17 # For 'test_env'. |
| 18 sys.path.insert( |
| 19 0, os.path.abspath(os.path.join(THIS_DIR, '..', '..', '..', 'unittests'))) |
| 20 # For 'spawn_device_temp_monitor.py'. |
| 21 sys.path.insert( |
| 22 0, os.path.abspath(os.path.join(THIS_DIR, '..', 'resources'))) |
| 23 |
| 24 # Imported for side effects on sys.path. |
| 25 import test_env |
| 26 import mock |
| 27 |
| 28 # In depot_tools/ |
| 29 from testing_support import auto_stub |
| 30 import spawn_device_temp_monitor |
| 31 |
| 32 |
| 33 class SimulatedSigterm(Exception): |
| 34 pass |
| 35 |
| 36 |
| 37 class MainFuncTest(auto_stub.TestCase): |
| 38 def setUp(self): |
| 39 # Collect calls to 'subprocess.Popen', which calls send_ts_mon_values.py. |
| 40 self.send_ts_mon_call = [] |
| 41 def mocked_ts_mon_calls(args): |
| 42 self.send_ts_mon_call = args |
| 43 self.mock( |
| 44 spawn_device_temp_monitor.subprocess, |
| 45 'Popen', |
| 46 mocked_ts_mon_calls) |
| 47 |
| 48 # Make sleep throw an exception to simulate a sigterm |
| 49 # and break out of loop. |
| 50 def mocked_sleep_call(duration): |
| 51 self.assertEquals(60, duration) |
| 52 raise SimulatedSigterm('simulated sigterm') |
| 53 self.mock( |
| 54 spawn_device_temp_monitor.time, |
| 55 'sleep', |
| 56 mocked_sleep_call) |
| 57 |
| 58 def test_main_responsive_device(self): |
| 59 # Collect calls to 'subprocess.check_output', which calls adb, and |
| 60 # simulate a responsive device. |
| 61 adb_calls = [] |
| 62 def mocked_adb_calls(args): |
| 63 adb_calls.append(args) |
| 64 if args[4].startswith('grep'): |
| 65 return "some_thermal_file_name" |
| 66 elif args[4].startswith('cat'): |
| 67 return "123" |
| 68 elif args[4].startswith('dumpsys'): |
| 69 return "temperature: 456" |
| 70 else: |
| 71 self.fail('Unexpected adb command: %s' % (' '.join(args))) |
| 72 |
| 73 self.mock( |
| 74 spawn_device_temp_monitor.subprocess, |
| 75 'check_output', |
| 76 mocked_adb_calls) |
| 77 try: |
| 78 spawn_device_temp_monitor.main( |
| 79 '/some/adb/path', |
| 80 '["device_serial_1"]', |
| 81 'some_slave_name') |
| 82 except SimulatedSigterm: |
| 83 pass |
| 84 |
| 85 # Should build args to send_ts_mon_values correctly. |
| 86 expected_cmd = [spawn_device_temp_monitor._RUN_PY, |
| 87 'infra.tools.send_ts_mon_values', |
| 88 '--float', |
| 89 '{"slave": "some_slave_name", "name": "dev/cpu/temperature", ' |
| 90 '"value": 123, "device_id": "device_serial_1"}', |
| 91 '--float', |
| 92 '{"slave": "some_slave_name", "name": "dev/battery/temperature", ' |
| 93 '"value": 456, "device_id": "device_serial_1"}'] |
| 94 self.assertEquals(expected_cmd, self.send_ts_mon_call) |
| 95 |
| 96 def test_main_unresponsive_device(self): |
| 97 # Collect calls to 'subprocess.check_output', which calls adb, and |
| 98 # simulate an unresponsive device. |
| 99 adb_calls = [] |
| 100 def mocked_adb_calls(args): |
| 101 adb_calls.append(args) |
| 102 raise subprocess.CalledProcessError |
| 103 |
| 104 self.mock( |
| 105 spawn_device_temp_monitor.subprocess, |
| 106 'check_output', |
| 107 mocked_adb_calls) |
| 108 try: |
| 109 spawn_device_temp_monitor.main( |
| 110 '/some/adb/path', |
| 111 '["device_serial_1"]', |
| 112 'some_slave_name') |
| 113 except SimulatedSigterm: |
| 114 pass |
| 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 |
| 126 if __name__ == '__main__': |
| 127 unittest.main() |
OLD | NEW |