Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 1 | # Copyright 2014 OpenStack Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # 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, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | from io import StringIO |
| 16 | import socket |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 17 | |
| 18 | import mock |
| 19 | import six |
| 20 | import testtools |
| 21 | |
| 22 | from tempest.lib.common import ssh |
| 23 | from tempest.lib import exceptions |
Matthew Treinish | ffad78a | 2016-04-16 14:39:52 -0400 | [diff] [blame] | 24 | from tempest.tests import base |
Jordan Pittier | 0e53b61 | 2016-03-03 14:23:17 +0100 | [diff] [blame] | 25 | import tempest.tests.utils as utils |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 26 | |
| 27 | |
| 28 | class TestSshClient(base.TestCase): |
| 29 | |
| 30 | SELECT_POLLIN = 1 |
| 31 | |
| 32 | @mock.patch('paramiko.RSAKey.from_private_key') |
| 33 | @mock.patch('six.StringIO') |
| 34 | def test_pkey_calls_paramiko_RSAKey(self, cs_mock, rsa_mock): |
| 35 | cs_mock.return_value = mock.sentinel.csio |
| 36 | pkey = 'mykey' |
| 37 | ssh.Client('localhost', 'root', pkey=pkey) |
| 38 | rsa_mock.assert_called_once_with(mock.sentinel.csio) |
| 39 | cs_mock.assert_called_once_with('mykey') |
| 40 | rsa_mock.reset_mock() |
| 41 | cs_mock.reset_mock() |
| 42 | pkey = mock.sentinel.pkey |
| 43 | # Shouldn't call out to load a file from RSAKey, since |
| 44 | # a sentinel isn't a basestring... |
| 45 | ssh.Client('localhost', 'root', pkey=pkey) |
| 46 | self.assertEqual(0, rsa_mock.call_count) |
| 47 | self.assertEqual(0, cs_mock.call_count) |
| 48 | |
| 49 | def _set_ssh_connection_mocks(self): |
| 50 | client_mock = mock.MagicMock() |
| 51 | client_mock.connect.return_value = True |
| 52 | return (self.patch('paramiko.SSHClient'), |
| 53 | self.patch('paramiko.AutoAddPolicy'), |
| 54 | client_mock) |
| 55 | |
| 56 | def test_get_ssh_connection(self): |
| 57 | c_mock, aa_mock, client_mock = self._set_ssh_connection_mocks() |
| 58 | s_mock = self.patch('time.sleep') |
| 59 | |
| 60 | c_mock.return_value = client_mock |
| 61 | aa_mock.return_value = mock.sentinel.aa |
| 62 | |
| 63 | # Test normal case for successful connection on first try |
| 64 | client = ssh.Client('localhost', 'root', timeout=2) |
| 65 | client._get_ssh_connection(sleep=1) |
| 66 | |
| 67 | aa_mock.assert_called_once_with() |
| 68 | client_mock.set_missing_host_key_policy.assert_called_once_with( |
| 69 | mock.sentinel.aa) |
| 70 | expected_connect = [mock.call( |
| 71 | 'localhost', |
Masayuki Igawa | 55b4cfd | 2016-08-30 10:29:46 +0900 | [diff] [blame^] | 72 | port=22, |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 73 | username='root', |
| 74 | pkey=None, |
| 75 | key_filename=None, |
| 76 | look_for_keys=False, |
| 77 | timeout=10.0, |
| 78 | password=None |
| 79 | )] |
| 80 | self.assertEqual(expected_connect, client_mock.connect.mock_calls) |
| 81 | self.assertEqual(0, s_mock.call_count) |
| 82 | |
Jordan Pittier | 0e53b61 | 2016-03-03 14:23:17 +0100 | [diff] [blame] | 83 | @mock.patch('time.sleep') |
| 84 | def test_get_ssh_connection_two_attemps(self, sleep_mock): |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 85 | c_mock, aa_mock, client_mock = self._set_ssh_connection_mocks() |
| 86 | |
| 87 | c_mock.return_value = client_mock |
| 88 | client_mock.connect.side_effect = [ |
| 89 | socket.error, |
| 90 | mock.MagicMock() |
| 91 | ] |
| 92 | |
| 93 | client = ssh.Client('localhost', 'root', timeout=1) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 94 | client._get_ssh_connection(sleep=1) |
Jordan Pittier | 0e53b61 | 2016-03-03 14:23:17 +0100 | [diff] [blame] | 95 | # We slept 2 seconds: because sleep is "1" and backoff is "1" too |
| 96 | sleep_mock.assert_called_once_with(2) |
| 97 | self.assertEqual(2, client_mock.connect.call_count) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 98 | |
| 99 | def test_get_ssh_connection_timeout(self): |
| 100 | c_mock, aa_mock, client_mock = self._set_ssh_connection_mocks() |
| 101 | |
Jordan Pittier | 0e53b61 | 2016-03-03 14:23:17 +0100 | [diff] [blame] | 102 | timeout = 2 |
| 103 | time_mock = self.patch('time.time') |
| 104 | time_mock.side_effect = utils.generate_timeout_series(timeout + 1) |
| 105 | |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 106 | c_mock.return_value = client_mock |
| 107 | client_mock.connect.side_effect = [ |
| 108 | socket.error, |
| 109 | socket.error, |
| 110 | socket.error, |
| 111 | ] |
| 112 | |
Jordan Pittier | 0e53b61 | 2016-03-03 14:23:17 +0100 | [diff] [blame] | 113 | client = ssh.Client('localhost', 'root', timeout=timeout) |
| 114 | # We need to mock LOG here because LOG.info() calls time.time() |
| 115 | # in order to preprend a timestamp. |
| 116 | with mock.patch.object(ssh, 'LOG'): |
| 117 | self.assertRaises(exceptions.SSHTimeout, |
| 118 | client._get_ssh_connection) |
| 119 | |
| 120 | # time.time() should be called twice, first to start the timer |
| 121 | # and then to compute the timedelta |
| 122 | self.assertEqual(2, time_mock.call_count) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 123 | |
| 124 | @mock.patch('select.POLLIN', SELECT_POLLIN, create=True) |
| 125 | def test_timeout_in_exec_command(self): |
| 126 | chan_mock, poll_mock, _ = self._set_mocks_for_select([0, 0, 0], True) |
| 127 | |
| 128 | # Test for a timeout condition immediately raised |
| 129 | client = ssh.Client('localhost', 'root', timeout=2) |
| 130 | with testtools.ExpectedException(exceptions.TimeoutException): |
| 131 | client.exec_command("test") |
| 132 | |
| 133 | chan_mock.fileno.assert_called_once_with() |
| 134 | chan_mock.exec_command.assert_called_once_with("test") |
| 135 | chan_mock.shutdown_write.assert_called_once_with() |
| 136 | |
| 137 | poll_mock.register.assert_called_once_with( |
| 138 | chan_mock, self.SELECT_POLLIN) |
| 139 | poll_mock.poll.assert_called_once_with(10) |
| 140 | |
| 141 | @mock.patch('select.POLLIN', SELECT_POLLIN, create=True) |
| 142 | def test_exec_command(self): |
| 143 | chan_mock, poll_mock, select_mock = ( |
| 144 | self._set_mocks_for_select([[1, 0, 0]], True)) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 145 | |
| 146 | chan_mock.recv_exit_status.return_value = 0 |
| 147 | chan_mock.recv.return_value = b'' |
| 148 | chan_mock.recv_stderr.return_value = b'' |
| 149 | |
| 150 | client = ssh.Client('localhost', 'root', timeout=2) |
| 151 | client.exec_command("test") |
| 152 | |
| 153 | chan_mock.fileno.assert_called_once_with() |
| 154 | chan_mock.exec_command.assert_called_once_with("test") |
| 155 | chan_mock.shutdown_write.assert_called_once_with() |
| 156 | |
| 157 | select_mock.assert_called_once_with() |
| 158 | poll_mock.register.assert_called_once_with( |
| 159 | chan_mock, self.SELECT_POLLIN) |
| 160 | poll_mock.poll.assert_called_once_with(10) |
| 161 | chan_mock.recv_ready.assert_called_once_with() |
| 162 | chan_mock.recv.assert_called_once_with(1024) |
| 163 | chan_mock.recv_stderr_ready.assert_called_once_with() |
| 164 | chan_mock.recv_stderr.assert_called_once_with(1024) |
| 165 | chan_mock.recv_exit_status.assert_called_once_with() |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 166 | |
| 167 | def _set_mocks_for_select(self, poll_data, ito_value=False): |
| 168 | gsc_mock = self.patch('tempest.lib.common.ssh.Client.' |
| 169 | '_get_ssh_connection') |
| 170 | ito_mock = self.patch('tempest.lib.common.ssh.Client._is_timed_out') |
| 171 | csp_mock = self.patch( |
| 172 | 'tempest.lib.common.ssh.Client._can_system_poll') |
| 173 | csp_mock.return_value = True |
| 174 | |
| 175 | select_mock = self.patch('select.poll', create=True) |
| 176 | client_mock = mock.MagicMock() |
| 177 | tran_mock = mock.MagicMock() |
| 178 | chan_mock = mock.MagicMock() |
| 179 | poll_mock = mock.MagicMock() |
| 180 | |
| 181 | select_mock.return_value = poll_mock |
| 182 | gsc_mock.return_value = client_mock |
| 183 | ito_mock.return_value = ito_value |
| 184 | client_mock.get_transport.return_value = tran_mock |
Lucas Alvares Gomes | 68c197e | 2016-04-19 18:18:05 +0100 | [diff] [blame] | 185 | tran_mock.open_session().__enter__.return_value = chan_mock |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 186 | if isinstance(poll_data[0], list): |
| 187 | poll_mock.poll.side_effect = poll_data |
| 188 | else: |
| 189 | poll_mock.poll.return_value = poll_data |
| 190 | |
| 191 | return chan_mock, poll_mock, select_mock |
| 192 | |
| 193 | _utf8_string = six.unichr(1071) |
| 194 | _utf8_bytes = _utf8_string.encode("utf-8") |
| 195 | |
| 196 | @mock.patch('select.POLLIN', SELECT_POLLIN, create=True) |
| 197 | def test_exec_good_command_output(self): |
| 198 | chan_mock, poll_mock, _ = self._set_mocks_for_select([1, 0, 0]) |
| 199 | closed_prop = mock.PropertyMock(return_value=True) |
| 200 | type(chan_mock).closed = closed_prop |
| 201 | |
| 202 | chan_mock.recv_exit_status.return_value = 0 |
| 203 | chan_mock.recv.side_effect = [self._utf8_bytes[0:1], |
| 204 | self._utf8_bytes[1:], b'R', b''] |
| 205 | chan_mock.recv_stderr.return_value = b'' |
| 206 | |
| 207 | client = ssh.Client('localhost', 'root', timeout=2) |
| 208 | out_data = client.exec_command("test") |
| 209 | self.assertEqual(self._utf8_string + 'R', out_data) |
| 210 | |
| 211 | @mock.patch('select.POLLIN', SELECT_POLLIN, create=True) |
| 212 | def test_exec_bad_command_output(self): |
| 213 | chan_mock, poll_mock, _ = self._set_mocks_for_select([1, 0, 0]) |
| 214 | closed_prop = mock.PropertyMock(return_value=True) |
| 215 | type(chan_mock).closed = closed_prop |
| 216 | |
| 217 | chan_mock.recv_exit_status.return_value = 1 |
| 218 | chan_mock.recv.return_value = b'' |
| 219 | chan_mock.recv_stderr.side_effect = [b'R', self._utf8_bytes[0:1], |
| 220 | self._utf8_bytes[1:], b''] |
| 221 | |
| 222 | client = ssh.Client('localhost', 'root', timeout=2) |
| 223 | exc = self.assertRaises(exceptions.SSHExecCommandFailed, |
| 224 | client.exec_command, "test") |
| 225 | self.assertIn('R' + self._utf8_string, six.text_type(exc)) |
| 226 | |
| 227 | def test_exec_command_no_select(self): |
| 228 | gsc_mock = self.patch('tempest.lib.common.ssh.Client.' |
| 229 | '_get_ssh_connection') |
| 230 | csp_mock = self.patch( |
| 231 | 'tempest.lib.common.ssh.Client._can_system_poll') |
| 232 | csp_mock.return_value = False |
| 233 | |
| 234 | select_mock = self.patch('select.poll', create=True) |
| 235 | client_mock = mock.MagicMock() |
| 236 | tran_mock = mock.MagicMock() |
| 237 | chan_mock = mock.MagicMock() |
| 238 | |
| 239 | # Test for proper reading of STDOUT and STDERROR |
| 240 | |
| 241 | gsc_mock.return_value = client_mock |
| 242 | client_mock.get_transport.return_value = tran_mock |
Lucas Alvares Gomes | 68c197e | 2016-04-19 18:18:05 +0100 | [diff] [blame] | 243 | tran_mock.open_session().__enter__.return_value = chan_mock |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 244 | chan_mock.recv_exit_status.return_value = 0 |
| 245 | |
| 246 | std_out_mock = mock.MagicMock(StringIO) |
| 247 | std_err_mock = mock.MagicMock(StringIO) |
| 248 | chan_mock.makefile.return_value = std_out_mock |
| 249 | chan_mock.makefile_stderr.return_value = std_err_mock |
| 250 | |
| 251 | client = ssh.Client('localhost', 'root', timeout=2) |
| 252 | client.exec_command("test") |
| 253 | |
| 254 | chan_mock.makefile.assert_called_once_with('rb', 1024) |
| 255 | chan_mock.makefile_stderr.assert_called_once_with('rb', 1024) |
| 256 | std_out_mock.read.assert_called_once_with() |
| 257 | std_err_mock.read.assert_called_once_with() |
| 258 | self.assertFalse(select_mock.called) |