OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """ | 6 """ |
7 Unit tests for the contents of device_utils.py (mostly DeviceUtils). | 7 Unit tests for the contents of device_utils.py (mostly DeviceUtils). |
8 """ | 8 """ |
9 | 9 |
10 # pylint: disable=protected-access | 10 # pylint: disable=protected-access |
(...skipping 1104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1115 ['something'], | 1115 ['something'], |
1116 self.device.RunShellCommand(cmd, shell=True, check_return=True)) | 1116 self.device.RunShellCommand(cmd, shell=True, check_return=True)) |
1117 | 1117 |
1118 | 1118 |
1119 class DeviceUtilsRunPipedShellCommandTest(DeviceUtilsTest): | 1119 class DeviceUtilsRunPipedShellCommandTest(DeviceUtilsTest): |
1120 | 1120 |
1121 def testRunPipedShellCommand_success(self): | 1121 def testRunPipedShellCommand_success(self): |
1122 with self.assertCall( | 1122 with self.assertCall( |
1123 self.call.device.RunShellCommand( | 1123 self.call.device.RunShellCommand( |
1124 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"', | 1124 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"', |
1125 shell=True, check_return=True), | 1125 shell=True), |
1126 ['This line contains foo', 'PIPESTATUS: 0 0']): | 1126 ['This line contains foo', 'PIPESTATUS: 0 0']): |
1127 self.assertEquals(['This line contains foo'], | 1127 self.assertEquals(['This line contains foo'], |
1128 self.device._RunPipedShellCommand('ps | grep foo')) | 1128 self.device._RunPipedShellCommand('ps | grep foo')) |
1129 | 1129 |
1130 def testRunPipedShellCommand_firstCommandFails(self): | 1130 def testRunPipedShellCommand_firstCommandFails(self): |
1131 with self.assertCall( | 1131 with self.assertCall( |
1132 self.call.device.RunShellCommand( | 1132 self.call.device.RunShellCommand( |
1133 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"', | 1133 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"', |
1134 shell=True, check_return=True), | 1134 shell=True), |
1135 ['PIPESTATUS: 1 0']): | 1135 ['PIPESTATUS: 1 0']): |
1136 with self.assertRaises(device_errors.AdbShellCommandFailedError) as ec: | 1136 with self.assertRaises(device_errors.AdbShellCommandFailedError) as ec: |
1137 self.device._RunPipedShellCommand('ps | grep foo') | 1137 self.device._RunPipedShellCommand('ps | grep foo') |
1138 self.assertEquals([1, 0], ec.exception.status) | 1138 self.assertEquals([1, 0], ec.exception.status) |
1139 | 1139 |
1140 def testRunPipedShellCommand_secondCommandFails(self): | 1140 def testRunPipedShellCommand_secondCommandFails(self): |
1141 with self.assertCall( | 1141 with self.assertCall( |
1142 self.call.device.RunShellCommand( | 1142 self.call.device.RunShellCommand( |
1143 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"', | 1143 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"', |
1144 shell=True, check_return=True), | 1144 shell=True), |
1145 ['PIPESTATUS: 0 1']): | 1145 ['PIPESTATUS: 0 1']): |
1146 with self.assertRaises(device_errors.AdbShellCommandFailedError) as ec: | 1146 with self.assertRaises(device_errors.AdbShellCommandFailedError) as ec: |
1147 self.device._RunPipedShellCommand('ps | grep foo') | 1147 self.device._RunPipedShellCommand('ps | grep foo') |
1148 self.assertEquals([0, 1], ec.exception.status) | 1148 self.assertEquals([0, 1], ec.exception.status) |
1149 | 1149 |
1150 def testRunPipedShellCommand_outputCutOff(self): | 1150 def testRunPipedShellCommand_outputCutOff(self): |
1151 with self.assertCall( | 1151 with self.assertCall( |
1152 self.call.device.RunShellCommand( | 1152 self.call.device.RunShellCommand( |
1153 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"', | 1153 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"', |
1154 shell=True, check_return=True), | 1154 shell=True), |
1155 ['foo.bar'] * 256 + ['foo.ba']): | 1155 ['foo.bar'] * 256 + ['foo.ba']): |
1156 with self.assertRaises(device_errors.AdbShellCommandFailedError) as ec: | 1156 with self.assertRaises(device_errors.AdbShellCommandFailedError) as ec: |
1157 self.device._RunPipedShellCommand('ps | grep foo') | 1157 self.device._RunPipedShellCommand('ps | grep foo') |
1158 self.assertIs(None, ec.exception.status) | 1158 self.assertIs(None, ec.exception.status) |
1159 | 1159 |
| 1160 def testRunPipedShellCommand_noCheckReturn(self): |
| 1161 with self.assertCall( |
| 1162 self.call.device.RunShellCommand( |
| 1163 'ps | grep foo; echo "PIPESTATUS: ${PIPESTATUS[@]}"', |
| 1164 shell=True), |
| 1165 ['foo.exe', 'PIPESTATUS: 1 0']): |
| 1166 out = self.device._RunPipedShellCommand( |
| 1167 'ps | grep foo', check_return=False) |
| 1168 self.assertEquals(['foo.exe'], out) |
| 1169 |
1160 | 1170 |
1161 @mock.patch('time.sleep', mock.Mock()) | 1171 @mock.patch('time.sleep', mock.Mock()) |
1162 class DeviceUtilsKillAllTest(DeviceUtilsTest): | 1172 class DeviceUtilsKillAllTest(DeviceUtilsTest): |
1163 | 1173 |
1164 def testKillAll_noMatchingProcessesFailure(self): | 1174 def testKillAll_noMatchingProcessesFailure(self): |
1165 with self.assertCall(self.call.device.GetPids('test_process'), {}): | 1175 with self.assertCall(self.call.device.GetPids('test_process'), {}): |
1166 with self.assertRaises(device_errors.CommandFailedError): | 1176 with self.assertRaises(device_errors.CommandFailedError): |
1167 self.device.KillAll('test_process') | 1177 self.device.KillAll('test_process') |
1168 | 1178 |
1169 def testKillAll_noMatchingProcessesQuiet(self): | 1179 def testKillAll_noMatchingProcessesQuiet(self): |
(...skipping 1825 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2995 (self.call.device.GetProp('ro.build.version.sdk', cache=True), '24'), | 3005 (self.call.device.GetProp('ro.build.version.sdk', cache=True), '24'), |
2996 (self.call.adb.Shell('service call iphonesubinfo 1'), | 3006 (self.call.adb.Shell('service call iphonesubinfo 1'), |
2997 self.ShellError())): | 3007 self.ShellError())): |
2998 with self.assertRaises(device_errors.CommandFailedError): | 3008 with self.assertRaises(device_errors.CommandFailedError): |
2999 self.device.GetIMEI() | 3009 self.device.GetIMEI() |
3000 | 3010 |
3001 | 3011 |
3002 if __name__ == '__main__': | 3012 if __name__ == '__main__': |
3003 logging.getLogger().setLevel(logging.DEBUG) | 3013 logging.getLogger().setLevel(logging.DEBUG) |
3004 unittest.main(verbosity=2) | 3014 unittest.main(verbosity=2) |
OLD | NEW |