Exemple #1
0
 def test_init(self, ssh_client_cls):
     # Arrange
     host = "localhost"
     port = 22
     username = None
     password = None
     key_filename = None
     # Act
     ssh = SSHEntity(host, port, username, password, key_filename)
     ssh._ssh_cls = ssh_client_cls
     client = ssh.ssh_client
     # Assert
     self.assertEquals(ssh.host, host)
     self.assertEquals(ssh.port, port)
     self.assertEquals(ssh.username, username)
     self.assertEquals(ssh.password, password)
     self.assertEquals(ssh.key_filename, key_filename)
     self.assertEquals(ssh.key_filename, key_filename)
     self.assertEquals(ssh.ssh_cls, ssh_client_cls)
     self.assertEquals(client.set_missing_host_key_policy.call_count, 1)
     client.connect.assert_called_once_with(hostname=host,
                                            port=port,
                                            username=username,
                                            password=password,
                                            key_filename=key_filename)
Exemple #2
0
    def test_execute_command(self, ssh_client_cls):
        # Arrange
        host = "localhost"
        cmd = "ls"
        ssh = SSHEntity(host)
        ssh._ssh_cls = ssh_client_cls

        session_mock = mock.Mock()
        session_mock.recv_ready.return_value = True
        session_mock.exit_status_ready.return_value = True
        session_mock.recv.return_value = "dummy"
        stream_mock = mock.Mock()
        stream_mock.channel = session_mock
        ssh.ssh_client.exec_command.return_value = None, stream_mock, None

        def change_status(y):
            session_mock.recv_ready.return_value = False
            return "dummy"

        session_mock.recv.side_effect = change_status
        ssh.get_new_session = lambda: session_mock
        # Act
        ssh.execute_command(cmd)
        # Assert
        ssh.ssh_client.exec_command.assert_called_once_with(cmd)
        self.assertTrue(session_mock.recv.call_count >= 1)
Exemple #3
0
 def test_session(self):
     # Arrange
     host, port, username, password = get_ssh_config()
     # Act
     ssh = SSHEntity(host, port, username, password)
     session = ssh.get_new_session()
     # Assert
     self.assertIsNotNone(session)
Exemple #4
0
def get_executor(address, username='******', password='******', cwd='ONOS'):
    ssh = SSHEntity(address,
                    username=username,
                    password=password,
                    cwd=cwd,
                    label=address,
                    redirect_output=True)
    return ssh
Exemple #5
0
 def test_get_new_session(self, ssh_client_cls):
     # Arrange
     host = "localhost"
     ssh = SSHEntity(host)
     ssh._ssh_client = ssh_client_cls()
     # Act
     session = ssh.get_new_session()
     # Assert
     self.assertEquals(ssh._ssh_client.get_transport.call_count, 1)
Exemple #6
0
 def get_executor(self):
     address = '192.168.56.11'
     ssh = SSHEntity(address,
                     username='******',
                     password='******',
                     cwd='ONOS',
                     label='ONOSDEV',
                     redirect_output=True)
     return ssh
Exemple #7
0
 def test_connect(self):
     # Arrange
     host, port, username, password = get_ssh_config()
     # Act
     ssh = SSHEntity(host, port, username, password)
     ssh_client = ssh.ssh_client
     # Assert
     self.assertIsNotNone(ssh_client)
     # clean up
     ssh_client.close()
Exemple #8
0
 def test_execute_command_with_redirect(self):
     # Arrange
     host, port, username, password = get_ssh_config()
     home_dir = os.getcwd()
     # Act
     ssh = SSHEntity(host, port, username, password, redirect_output=True)
     ret = ssh.execute_command("ls " + home_dir)
     # Assert
     self.assertIsInstance(ret, basestring)
     self.assertEquals(ret, "")
Exemple #9
0
 def test_execute_command_with_redirect(self, ssh_client_cls):
     # Arrange
     host = "localhost"
     cmd = "ls"
     ssh = SSHEntity(host, redirect_output=True)
     ssh._ssh_cls = ssh_client_cls
     ssh.ssh_client.exec_command.return_value = (StringIO(), StringIO(),
                                                 StringIO())
     # Act
     ssh.execute_command(cmd)
     # Assert
     ssh.ssh_client.exec_command.assert_called_once_with(cmd)
Exemple #10
0
 def test_execute_command(self):
     # Arrange
     host, port, username, password = get_ssh_config()
     home_dir = os.getcwd()
     # Act
     ssh = SSHEntity(host, port, username, password)
     ret = ssh.execute_command("ls -a " + home_dir)
     # Assert
     self.assertIsInstance(ret, basestring)
     ret_list = ret.split()
     ret_list.sort()
     # ls -a returns . and .. but os.listdir doesn't
     ret_list.remove('.')
     ret_list.remove('..')
     current_list = os.listdir('.')
     current_list.sort()
     self.assertEquals(ret_list, current_list)
Exemple #11
0
 def __init__(self,
              controller_config,
              cmd_executor=None,
              sync_connection_manager=None,
              snapshot_service=None,
              username=None,
              password=None):
     """
 Args:
   controller_config: see ControllerConfig
   cmd_executer: a class that has execute_command method. If not specified
                 SSHEntity will be used
                 See SSHEntity and LocalEntity
   username: overrides the username specified in controller_config (if any)
   password: overrides the password specified in controller_config (if any)
 """
     Controller.__init__(self, controller_config, sync_connection_manager,
                         snapshot_service)
     self.username = username
     self.password = password
     if self.username is None and hasattr(self.config, 'username'):
         self.username = self.config.username
     if self.password is None and hasattr(self.config, 'password'):
         self.password = self.config.password
     self.cmd_executor = cmd_executor
     if self.cmd_executor is None and hasattr(self.config, 'cmd_executor'):
         self.cmd_executor = self.config.cmd_executer
     if self.cmd_executor is None:
         key_filename = getattr(self.config, 'key_filename', None)
         self.cmd_executor = SSHEntity(controller_config.address,
                                       username=self.username,
                                       password=self.password,
                                       key_filename=key_filename,
                                       cwd=getattr(self.config, "cwd",
                                                   None),
                                       redirect_output=True,
                                       block=True)
     assert hasattr(self.cmd_executor, "execute_command")
     self.commands = {}
     self.populate_commands()
     self.welcome_msg = " =====> Starting VM Controller <===== "
     self.alive_status_string = ""  # subclass dependent