Best Python code snippet using tempest_python
test_ssh.py
Source:test_ssh.py
...36 # a sentinel isn't a basestring...37 ssh.Client('localhost', 'root', pkey=pkey)38 self.assertEqual(0, rsa_mock.call_count)39 self.assertEqual(0, cs_mock.call_count)40 def _set_ssh_connection_mocks(self):41 client_mock = mock.MagicMock()42 client_mock.connect.return_value = True43 return (self.patch('paramiko.SSHClient'),44 self.patch('paramiko.AutoAddPolicy'),45 client_mock)46 def test_get_ssh_connection(self):47 c_mock, aa_mock, client_mock = self._set_ssh_connection_mocks()48 s_mock = self.patch('time.sleep')49 c_mock.return_value = client_mock50 aa_mock.return_value = mock.sentinel.aa51 # Test normal case for successful connection on first try52 client = ssh.Client('localhost', 'root', timeout=2)53 client._get_ssh_connection(sleep=1)54 aa_mock.assert_called_once_with()55 client_mock.set_missing_host_key_policy.assert_called_once_with(56 mock.sentinel.aa)57 expected_connect = [mock.call(58 'localhost',59 username='root',60 pkey=None,61 key_filename=None,62 look_for_keys=False,63 timeout=10.0,64 password=None65 )]66 self.assertEqual(expected_connect, client_mock.connect.mock_calls)67 self.assertEqual(0, s_mock.call_count)68 def test_get_ssh_connection_two_attemps(self):69 c_mock, aa_mock, client_mock = self._set_ssh_connection_mocks()70 c_mock.return_value = client_mock71 client_mock.connect.side_effect = [72 socket.error,73 mock.MagicMock()74 ]75 client = ssh.Client('localhost', 'root', timeout=1)76 start_time = int(time.time())77 client._get_ssh_connection(sleep=1)78 end_time = int(time.time())79 self.assertLess((end_time - start_time), 4)80 self.assertGreater((end_time - start_time), 1)81 def test_get_ssh_connection_timeout(self):82 c_mock, aa_mock, client_mock = self._set_ssh_connection_mocks()83 c_mock.return_value = client_mock84 client_mock.connect.side_effect = [85 socket.error,86 socket.error,87 socket.error,88 ]89 client = ssh.Client('localhost', 'root', timeout=2)90 start_time = int(time.time())91 with testtools.ExpectedException(exceptions.SSHTimeout):92 client._get_ssh_connection()93 end_time = int(time.time())94 self.assertLess((end_time - start_time), 5)95 self.assertGreaterEqual((end_time - start_time), 2)96 def test_exec_command(self):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!