summaryrefslogtreecommitdiffstats
path: root/tests/test_ssh.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_ssh.py')
-rw-r--r--tests/test_ssh.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/test_ssh.py b/tests/test_ssh.py
new file mode 100644
index 00000000..ffb5cb94
--- /dev/null
+++ b/tests/test_ssh.py
@@ -0,0 +1,74 @@
1# Copyright 2019 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Unittests for the ssh.py module."""
16
17import multiprocessing
18import subprocess
19import unittest
20from unittest import mock
21
22import ssh
23
24
25class SshTests(unittest.TestCase):
26 """Tests the ssh functions."""
27
28 def test_parse_ssh_version(self):
29 """Check _parse_ssh_version() handling."""
30 ver = ssh._parse_ssh_version('Unknown\n')
31 self.assertEqual(ver, ())
32 ver = ssh._parse_ssh_version('OpenSSH_1.0\n')
33 self.assertEqual(ver, (1, 0))
34 ver = ssh._parse_ssh_version('OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.13, OpenSSL 1.0.1f 6 Jan 2014\n')
35 self.assertEqual(ver, (6, 6, 1))
36 ver = ssh._parse_ssh_version('OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n 7 Dec 2017\n')
37 self.assertEqual(ver, (7, 6))
38
39 def test_version(self):
40 """Check version() handling."""
41 with mock.patch('ssh._run_ssh_version', return_value='OpenSSH_1.2\n'):
42 self.assertEqual(ssh.version(), (1, 2))
43
44 def test_context_manager_empty(self):
45 """Verify context manager with no clients works correctly."""
46 with multiprocessing.Manager() as manager:
47 with ssh.ProxyManager(manager):
48 pass
49
50 def test_context_manager_child_cleanup(self):
51 """Verify orphaned clients & masters get cleaned up."""
52 with multiprocessing.Manager() as manager:
53 with ssh.ProxyManager(manager) as ssh_proxy:
54 client = subprocess.Popen(['sleep', '964853320'])
55 ssh_proxy.add_client(client)
56 master = subprocess.Popen(['sleep', '964853321'])
57 ssh_proxy.add_master(master)
58 # If the process still exists, these will throw timeout errors.
59 client.wait(0)
60 master.wait(0)
61
62 def test_ssh_sock(self):
63 """Check sock() function."""
64 manager = multiprocessing.Manager()
65 proxy = ssh.ProxyManager(manager)
66 with mock.patch('tempfile.mkdtemp', return_value='/tmp/foo'):
67 # old ssh version uses port
68 with mock.patch('ssh.version', return_value=(6, 6)):
69 self.assertTrue(proxy.sock().endswith('%p'))
70
71 proxy._sock_path = None
72 # new ssh version uses hash
73 with mock.patch('ssh.version', return_value=(6, 7)):
74 self.assertTrue(proxy.sock().endswith('%C'))