class TestSshSession(TestCase): def setUp(self): self._username = "******" self._password = "******" self._hostname = "hostname" self._port = 22 self._on_session_start = Mock() def test_init_attributes(self): self._instance = SSHSession( self._hostname, self._username, self._password, port=self._port, on_session_start=self._on_session_start, ) mandatory_attributes = [ "username", "_handler", "_current_channel", "password", "_buffer_size", ] self.assertEqual( len( set(mandatory_attributes).difference( set(self._instance.__dict__.keys()))), 0, ) @patch("cloudshell.cli.session.ssh_session.ExpectSession") def test_eq(self, expect_session): self._instance = SSHSession( self._hostname, self._username, self._password, port=self._port, on_session_start=self._on_session_start, ) self.assertTrue( self._instance.__eq__( SSHSession( self._hostname, self._username, self._password, port=self._port, on_session_start=self._on_session_start, ))) self.assertFalse( self._instance.__eq__( SSHSession( self._hostname, "incorrect_username", self._password, port=self._port, on_session_start=self._on_session_start, ))) self.assertFalse( self._instance.__eq__( SSHSession( self._hostname, self._username, "incorrect_password", port=self._port, on_session_start=self._on_session_start, ))) pkey = paramiko.RSAKey.from_private_key(StringIO(KEY_WITH_PASSPHRASE), password=KEY_PASSPHRASE) self.assertFalse( self._instance.__eq__( SSHSession( self._hostname, self._username, "", port=self._port, on_session_start=self._on_session_start, pkey=pkey, ))) @patch("cloudshell.cli.session.ssh_session.ExpectSession") def test_eq_rsa(self, expect_session): pkey = paramiko.RSAKey.from_private_key(StringIO(KEY_WITH_PASSPHRASE), password=KEY_PASSPHRASE) self._instance = SSHSession( self._hostname, self._username, self._password, port=self._port, on_session_start=self._on_session_start, pkey=pkey, ) self.assertTrue( self._instance.__eq__( SSHSession( self._hostname, self._username, self._password, port=self._port, on_session_start=self._on_session_start, pkey=pkey, ))) self.assertFalse( self._instance.__eq__( SSHSession( self._hostname, self._username, self._password, port=self._port, on_session_start=self._on_session_start, ))) @patch("cloudshell.cli.session.ssh_session.paramiko") def test_intialize_session(self, mock_paramiko): # Setup mock_paramiko.SSHClient.return_value = Mock() self._instance = SSHSession( "127.0.0.1", "user0", "password0", port=22, on_session_start=self._on_session_start, ) self._instance.hardware_expect = Mock(return_value="Done") # Act self._instance._initialize_session(">", logger=Mock()) # Assert self.assertIsNotNone(self._instance._handler) self.assertEqual(self._instance._handler, mock_paramiko.SSHClient.return_value) def test_connect_actions(self): # Setup on_session_start = Mock() self._instance = SSHSession( "127.0.0.1", "user0", "password0", port=22, on_session_start=on_session_start, ) self._instance.hardware_expect = Mock(return_value="Done") self._instance._handler = Mock() # Act self._instance._connect_actions(">", logger=Mock()) # Assert self._instance.hardware_expect.assert_called_once() on_session_start.assert_called_once() def test_connect_simple(self): pkey = paramiko.RSAKey.from_private_key( StringIO(KEY_WITH_PASSPHRASE), password=KEY_PASSPHRASE) # unused server = SSHServer(user2key={"user-1", pkey}, user2password={"user0": "password0"}) self._instance = SSHSession( "127.0.0.1", "user0", "password0", port=server.port, on_session_start=self._on_session_start, ) self._instance.connect(">", logger=Mock()) self._instance.hardware_expect("dummy command", ">", Mock()) def test_upload_sftp(self): server = SSHServer(user2password={"user0": "password0"}, enable_sftp=True, enable_scp=False) self._instance = SSHSession( "127.0.0.1", "user0", "password0", port=server.port, on_session_start=self._on_session_start, ) self._instance.connect(">", logger=Mock()) self._instance.upload_sftp(StringIO(u"klmno"), "z.txt", 5, "0601") self.assertTrue( server.filename2stringio["z.txt"].getvalue() == "klmno") def test_upload_scp(self): server = SSHServer(user2password={"user0": "password0"}, enable_sftp=False, enable_scp=True) self._instance = SSHSession( "127.0.0.1", "user0", "password0", port=server.port, on_session_start=self._on_session_start, ) self._instance.connect(">", logger=Mock()) self._instance.upload_scp(StringIO(u"abcde"), "y.txt", 5, "0601") sleep(3) self.assertTrue( server.filename2stringio["y.txt"].getvalue() == "abcde") def test_connect_timeout(self): self._instance = SSHSession("bad_host", "user1", "password1", on_session_start=self._on_session_start) with self.assertRaises(SSHSessionException): self._instance.connect(">", logger=Mock()) def test_username_password(self): server = SSHServer(user2password={"user1": "password1"}) self._instance = SSHSession( "127.0.0.1", "user1", "password1", port=server.port, on_session_start=self._on_session_start, ) self._instance.connect(">", logger=Mock()) self._instance.hardware_expect("dummy command", ">", Mock()) def test_enable_exit(self): server = SSHServer(user2password={"user1": "password1"}) self._instance = SSHSession( "127.0.0.1", "user1", "password1", port=server.port, on_session_start=self._on_session_start, ) self._instance.connect(">", logger=Mock()) o = self._instance.hardware_expect("dummy command", ">", Mock()) self.assertTrue("[prompt]" in o) o = self._instance.hardware_expect("enable", ">", Mock()) self.assertTrue("[enable]" in o) o = self._instance.hardware_expect("dummy command", ">", Mock()) self.assertTrue("[enable]" in o) o = self._instance.hardware_expect("exit", ">", Mock()) self.assertTrue("[prompt]" in o) o = self._instance.hardware_expect("dummy command", ">", Mock()) self.assertTrue("[prompt]" in o) def test_rsa(self): pkey = paramiko.RSAKey.from_private_key(StringIO(KEY_WITH_PASSPHRASE), password=KEY_PASSPHRASE) server = SSHServer(user2key={"user4": pkey}) self._instance = SSHSession( "127.0.0.1", "user4", "", port=server.port, on_session_start=self._on_session_start, pkey=pkey, ) self._instance.connect(">", logger=Mock()) self._instance.hardware_expect("dummy command", ">", Mock()) def test_rsa_failure(self): pkey = paramiko.RSAKey.from_private_key(StringIO(KEY_WITH_PASSPHRASE), password=KEY_PASSPHRASE) server = SSHServer(user2key={}) with self.assertRaises(SSHSessionException): self._instance = SSHSession( "127.0.0.1", "user5", "", port=server.port, on_session_start=self._on_session_start, pkey=pkey, ) self._instance.connect(">", logger=Mock()) self._instance.hardware_expect("dummy command", ">", Mock())
class TestSshSession(TestCase): def setUp(self): self._username = '******' self._password = '******' self._hostname = 'hostname' self._port = 22 self._on_session_start = Mock() @patch('cloudshell.cli.session.ssh_session.ExpectSession') @patch('cloudshell.cli.session.ssh_session.ConnectionParams') def test_init_attributes(self, connection_params, expect_session): self._instance = SSHSession(self._hostname, self._username, self._password, port=self._port, on_session_start=self._on_session_start) mandatory_attributes = [ 'username', '_handler', '_current_channel', 'password', '_buffer_size' ] self.assertEqual( len( set(mandatory_attributes).difference( set(self._instance.__dict__.keys()))), 0) @patch('cloudshell.cli.session.ssh_session.ExpectSession') def test_eq(self, expect_session): self._instance = SSHSession(self._hostname, self._username, self._password, port=self._port, on_session_start=self._on_session_start) self.assertTrue( self._instance.__eq__( SSHSession(self._hostname, self._username, self._password, port=self._port, on_session_start=self._on_session_start))) self.assertFalse( self._instance.__eq__( SSHSession(self._hostname, 'incorrect_username', self._password, port=self._port, on_session_start=self._on_session_start))) self.assertFalse( self._instance.__eq__( SSHSession(self._hostname, self._username, 'incorrect_password', port=self._port, on_session_start=self._on_session_start))) pkey = paramiko.RSAKey.from_private_key(StringIO(KEY_WITH_PASSPHRASE), password=KEY_PASSPHRASE) self.assertFalse( self._instance.__eq__( SSHSession(self._hostname, self._username, '', port=self._port, on_session_start=self._on_session_start, pkey=pkey))) @patch('cloudshell.cli.session.ssh_session.ExpectSession') def test_eq_rsa(self, expect_session): pkey = paramiko.RSAKey.from_private_key(StringIO(KEY_WITH_PASSPHRASE), password=KEY_PASSPHRASE) self._instance = SSHSession(self._hostname, self._username, self._password, port=self._port, on_session_start=self._on_session_start, pkey=pkey) self.assertTrue( self._instance.__eq__( SSHSession(self._hostname, self._username, self._password, port=self._port, on_session_start=self._on_session_start, pkey=pkey))) self.assertFalse( self._instance.__eq__( SSHSession(self._hostname, self._username, self._password, port=self._port, on_session_start=self._on_session_start))) def test_connect_simple(self): pkey = paramiko.RSAKey.from_private_key( StringIO(KEY_WITH_PASSPHRASE), password=KEY_PASSPHRASE) # unused server = SSHServer(user2key={'user-1', pkey}, user2password={'user0': 'password0'}) self._instance = SSHSession('127.0.0.1', 'user0', 'password0', port=server.port, on_session_start=self._on_session_start) self._instance.connect('>', logger=Mock()) self._instance.hardware_expect('dummy command', '>', Mock()) def test_upload_sftp(self): server = SSHServer(user2password={'user0': 'password0'}, enable_sftp=True, enable_scp=False) self._instance = SSHSession('127.0.0.1', 'user0', 'password0', port=server.port, on_session_start=self._on_session_start) self._instance.connect('>', logger=Mock()) self._instance.upload_sftp(StringIO('klmno'), 'z.txt', 5, '0601') self.assertTrue( server.filename2stringio['z.txt'].getvalue() == 'klmno') def test_upload_scp(self): server = SSHServer(user2password={'user0': 'password0'}, enable_sftp=False, enable_scp=True) self._instance = SSHSession('127.0.0.1', 'user0', 'password0', port=server.port, on_session_start=self._on_session_start) self._instance.connect('>', logger=Mock()) self._instance.upload_scp(StringIO('abcde'), 'y.txt', 5, '0601') sleep(3) self.assertTrue( server.filename2stringio['y.txt'].getvalue() == 'abcde') def test_connect_timeout(self): self._instance = SSHSession('bad_host', 'user1', 'password1', on_session_start=self._on_session_start) with self.assertRaises(SSHSessionException): self._instance.connect('>', logger=Mock()) def test_username_password(self): server = SSHServer(user2password={'user1': 'password1'}) self._instance = SSHSession('127.0.0.1', 'user1', 'password1', port=server.port, on_session_start=self._on_session_start) self._instance.connect('>', logger=Mock()) self._instance.hardware_expect('dummy command', '>', Mock()) def test_enable_exit(self): server = SSHServer(user2password={'user1': 'password1'}) self._instance = SSHSession('127.0.0.1', 'user1', 'password1', port=server.port, on_session_start=self._on_session_start) self._instance.connect('>', logger=Mock()) o = self._instance.hardware_expect('dummy command', '>', Mock()) self.assertTrue('[prompt]' in o) o = self._instance.hardware_expect('enable', '>', Mock()) self.assertTrue('[enable]' in o) o = self._instance.hardware_expect('dummy command', '>', Mock()) self.assertTrue('[enable]' in o) o = self._instance.hardware_expect('exit', '>', Mock()) self.assertTrue('[prompt]' in o) o = self._instance.hardware_expect('dummy command', '>', Mock()) self.assertTrue('[prompt]' in o) def test_rsa(self): pkey = paramiko.RSAKey.from_private_key(StringIO(KEY_WITH_PASSPHRASE), password=KEY_PASSPHRASE) server = SSHServer(user2key={'user4': pkey}) self._instance = SSHSession('127.0.0.1', 'user4', '', port=server.port, on_session_start=self._on_session_start, pkey=pkey) self._instance.connect('>', logger=Mock()) self._instance.hardware_expect('dummy command', '>', Mock()) def test_rsa_failure(self): pkey = paramiko.RSAKey.from_private_key(StringIO(KEY_WITH_PASSPHRASE), password=KEY_PASSPHRASE) server = SSHServer(user2key={}) with self.assertRaises(SSHSessionException): self._instance = SSHSession( '127.0.0.1', 'user5', '', port=server.port, on_session_start=self._on_session_start, pkey=pkey) self._instance.connect('>', logger=Mock()) self._instance.hardware_expect('dummy command', '>', Mock())