def test_create_channel(self, mock_client): test_width = 600 test_height = 800 mock_channel = mock.Mock() mock_client.invoke_shell.return_value = mock_channel utils.create_channel(mock_client, test_width, test_height) mock_client.invoke_shell.assert_called_once() mock_channel.resize_pty.assert_called_once_with(test_width, test_height)
def test_create_channel(self, mock_client): test_width = 600 test_height = 800 mock_channel = mock.Mock() mock_client.invoke_shell.return_value = mock_channel utils.create_channel(mock_client, test_width, test_height) mock_client.invoke_shell.assert_called_once_with() mock_channel.resize_pty.assert_called_once_with( test_width, test_height)
def test_create_channel(self): client = paramiko.SSHClient() channel = paramiko.Channel(123) self.mox.StubOutWithMock(client, 'invoke_shell') self.mox.StubOutWithMock(channel, 'resize_pty') client.invoke_shell().AndReturn(channel) channel.resize_pty(600, 800) self.mox.ReplayAll() utils.create_channel(client, 600, 800) self.mox.VerifyAll()
def _execute_cli(self, cmd): """Build SSH connection and execute CLI commands. If the connection to first controller timeout, try to connect to the other controller. """ LOG.debug(_("CLI command: %s") % cmd) connect_times = 1 ip0 = self.login_info["ControllerIP0"] ip1 = self.login_info["ControllerIP1"] user = self.login_info["UserName"] pwd = self.login_info["UserPassword"] if not self.ssh_pool: self.ssh_pool = utils.SSHPool(ip0, 22, 30, user, pwd, max_size=2) ssh_client = None while True: try: if connect_times == 2: # Switch to the other controller. with self.lock_ip: if ssh_client: if ssh_client.server_ip == self.ssh_pool.ip: self.ssh_pool.ip = ip1 if self.ssh_pool.ip == ip0 else ip0 old_ip = ssh_client.server_ip # Create a new client to replace the old one. if getattr(ssh_client, "chan", None): ssh_client.chan.close() ssh_client.close() ssh_client = self.ssh_pool.create() self._reset_transport_timeout(ssh_client, 0.1) else: self.ssh_pool.ip = ip1 old_ip = ip0 LOG.info( _("_execute_cli: Can not connect to IP " "%(old)s, try to connect to the other " "IP %(new)s.") % {"old": old_ip, "new": self.ssh_pool.ip} ) if not ssh_client: # Get an SSH client from SSH pool. ssh_client = self.ssh_pool.get() self._reset_transport_timeout(ssh_client, 0.1) # "server_ip" shows the IP of SSH server. if not getattr(ssh_client, "server_ip", None): with self.lock_ip: setattr(ssh_client, "server_ip", self.ssh_pool.ip) # An SSH client owns one "chan". if not getattr(ssh_client, "chan", None): setattr(ssh_client, "chan", utils.create_channel(ssh_client, 600, 800)) while True: ssh_client.chan.send(cmd + "\n") out = ssh_read(user, ssh_client.chan, cmd, 20) if out.find("(y/n)") > -1: cmd = "y" else: # Put SSH client back into SSH pool. self.ssh_pool.put(ssh_client) return out except Exception as err: if connect_times < 2: connect_times += 1 continue else: if ssh_client: self.ssh_pool.remove(ssh_client) raise err