From 339f2df1ddd741070e340ec01d6882dd1eee617c Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Thu, 6 May 2021 00:44:42 -0400 Subject: ssh: rewrite proxy management for multiprocessing usage We changed sync to use multiprocessing for parallel work. This broke the ssh proxy code as it's all based on threads. Rewrite the logic to be multiprocessing safe. Now instead of the module acting as a stateful object, callers have to instantiate a new ProxyManager class that holds all the state, an pass that down to any users. Bug: https://crbug.com/gerrit/12389 Change-Id: I4b1af116f7306b91e825d3c56fb4274c9b033562 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/305486 Tested-by: Mike Frysinger Reviewed-by: Chris Mcdonald --- tests/test_ssh.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'tests/test_ssh.py') diff --git a/tests/test_ssh.py b/tests/test_ssh.py index 5a4f27e4..ffb5cb94 100644 --- a/tests/test_ssh.py +++ b/tests/test_ssh.py @@ -14,6 +14,8 @@ """Unittests for the ssh.py module.""" +import multiprocessing +import subprocess import unittest from unittest import mock @@ -39,14 +41,34 @@ class SshTests(unittest.TestCase): with mock.patch('ssh._run_ssh_version', return_value='OpenSSH_1.2\n'): self.assertEqual(ssh.version(), (1, 2)) + def test_context_manager_empty(self): + """Verify context manager with no clients works correctly.""" + with multiprocessing.Manager() as manager: + with ssh.ProxyManager(manager): + pass + + def test_context_manager_child_cleanup(self): + """Verify orphaned clients & masters get cleaned up.""" + with multiprocessing.Manager() as manager: + with ssh.ProxyManager(manager) as ssh_proxy: + client = subprocess.Popen(['sleep', '964853320']) + ssh_proxy.add_client(client) + master = subprocess.Popen(['sleep', '964853321']) + ssh_proxy.add_master(master) + # If the process still exists, these will throw timeout errors. + client.wait(0) + master.wait(0) + def test_ssh_sock(self): """Check sock() function.""" + manager = multiprocessing.Manager() + proxy = ssh.ProxyManager(manager) with mock.patch('tempfile.mkdtemp', return_value='/tmp/foo'): # old ssh version uses port with mock.patch('ssh.version', return_value=(6, 6)): - self.assertTrue(ssh.sock().endswith('%p')) - ssh._ssh_sock_path = None + self.assertTrue(proxy.sock().endswith('%p')) + + proxy._sock_path = None # new ssh version uses hash with mock.patch('ssh.version', return_value=(6, 7)): - self.assertTrue(ssh.sock().endswith('%C')) - ssh._ssh_sock_path = None + self.assertTrue(proxy.sock().endswith('%C')) -- cgit v1.2.3-54-g00ecf