aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Lin (Tzu Hsiang Lin) <ericth@google.com>2021-12-28 12:26:40 +0800
committerGitHub <noreply@github.com>2021-12-28 12:26:40 +0800
commitd2efcf909824e4f1f457dc57530395c84dbc255f (patch)
treeb89a0fd87a6387224417d04c04fd7668f3ee11c6
parent1d1b57295b014fe50733d6665f7f9ae29e2164f3 (diff)
downloadmobly-d2efcf909824e4f1f457dc57530395c84dbc255f.tar.gz
Improve test coverage in Mobly utils. (#789)
-rwxr-xr-xtests/mobly/utils_test.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/tests/mobly/utils_test.py b/tests/mobly/utils_test.py
index ed69e4f..16204c9 100755
--- a/tests/mobly/utils_test.py
+++ b/tests/mobly/utils_test.py
@@ -149,6 +149,69 @@ class UtilsTest(unittest.TestCase):
self.assertListEqual(pid_list, [780, 791, 799, 888, 890, 913, 999])
+ @mock.patch.object(os, 'kill')
+ @mock.patch.object(utils, '_collect_process_tree')
+ def test_kill_process_tree_on_unix_succeeds(self, mock_collect_process_tree,
+ mock_os_kill):
+ mock_collect_process_tree.return_value = [799, 888, 890]
+ mock_proc = mock.MagicMock()
+ mock_proc.pid = 123
+
+ with mock.patch.object(os, 'name', new='posix'):
+ utils._kill_process_tree(mock_proc)
+
+ mock_os_kill.assert_has_calls([
+ mock.call(799, signal.SIGTERM),
+ mock.call(888, signal.SIGTERM),
+ mock.call(890, signal.SIGTERM),
+ ])
+ mock_proc.kill.assert_called_once()
+
+ @mock.patch.object(os, 'kill')
+ @mock.patch.object(utils, '_collect_process_tree')
+ def test_kill_process_tree_on_unix_kill_children_failed_throws_error(
+ self, mock_collect_process_tree, mock_os_kill):
+ mock_collect_process_tree.return_value = [799, 888, 890]
+ mock_os_kill.side_effect = [None, OSError(), None]
+ mock_proc = mock.MagicMock()
+ mock_proc.pid = 123
+
+ with mock.patch.object(os, 'name', new='posix'):
+ with self.assertRaises(utils.Error):
+ utils._kill_process_tree(mock_proc)
+
+ mock_proc.kill.assert_called_once()
+
+ @mock.patch.object(utils, '_collect_process_tree')
+ def test_kill_process_tree_on_unix_kill_proc_failed_throws_error(
+ self, mock_collect_process_tree):
+ mock_collect_process_tree.return_value = []
+ mock_proc = mock.MagicMock()
+ mock_proc.pid = 123
+ mock_proc.kill.side_effect = subprocess.SubprocessError()
+
+ with mock.patch.object(os, 'name', new='posix'):
+ with self.assertRaises(utils.Error):
+ utils._kill_process_tree(mock_proc)
+
+ mock_proc.kill.assert_called_once()
+
+ @mock.patch('subprocess.check_output')
+ def test_kill_process_tree_on_windows_calls_taskkill(self, mock_check_output):
+ mock_proc = mock.MagicMock()
+ mock_proc.pid = 123
+
+ with mock.patch.object(os, 'name', new='nt'):
+ utils._kill_process_tree(mock_proc)
+
+ mock_check_output.assert_called_once_with([
+ 'taskkill',
+ '/F',
+ '/T',
+ '/PID',
+ '123',
+ ])
+
def test_run_command(self):
ret, _, _ = utils.run_command(self.sleep_cmd(0.01))