def test_sftp_transfer(self): """ test sftp_transfer function """ protocol = SFTPProtocol( "localhost", 22, "", "", 2.0 ) ssh = OpenSSH(protocol) # from host to target data_remote = [DataItem(self.localdir, self.remotedir, "remote")] self.assertFalse(ssh.sftp_transfer(data_remote, \ self._from_host_to_target)) # remove local data self.assertTrue(os.path.isdir(self.localdir)) shutil.rmtree(self.localdir) # from target to host data_local = [DataItem(self.remotedir, self.localdir, "local")] self.assertFalse(ssh.sftp_transfer(data_local, \ self._from_target_to_host)) # remove remote data self.assertFalse(ssh.sftp_remove([self.remotedir], \ self._test_removing_dir)) self.assertFalse(os.path.exists(self.remotedir))
def run(self): """ Run the collect stage if defined. """ if not self._collect_exists: self._logger.debug("collect is not defined. Skipping run") return self.events.collectStarted() dataitems = [] for item in self._collect_data: data = item.get_data() dataitems.append(data) # sftp is defined if isinstance(self._collect_protocol, SFTPProtocol): self._logger.debug("transfer data using sftp protocol") openssh = OpenSSH(self._collect_protocol) openssh.sftp_transfer(dataitems, \ self.events.dataTransfer, \ self.events.dataTransferProgress) else: raise NotImplementedError(\ "'%s' protocol is not implemented"%\ self._collect_protocol.name()) self.events.collectCompleted()
def run(self): """ Run the execute stage if defined. """ if not self._execute_exists: self._logger.debug("execute is not defined. Skipping run") return self.events.executeStarted() stream = StreamForwarder(self.events, self._execute_cmds) # loop through commands # TODO: optimize commands execution, using lists for commands # having the same protocol for item in self._execute_cmds: if isinstance(item.protocol, OpenSSHProtocol): # ssh is defined self._logger.debug("execute commands using ssh protocol") openssh = OpenSSH(item.protocol) openssh.ssh_execute([item.command], stream) elif isinstance(item.protocol, SerialProtocol): # serial is defined self._logger.debug("execute commands using serial protocol") serial = Serial(item.protocol) serial.send_command([item.command], stream) else: raise NotImplementedError(\ "'%s' protocol is not implemented"%item.protocol.name()) self.events.executeCompleted()
def test_ssh_user_error(self): """ test a SSH user wrong assignment """ protocol = OpenSSHProtocol( "localhost", 22, "this_user_does_not_exist", "", 2.0 ) ssh = OpenSSH(protocol) with self.assertRaises(SSHConnectionError): ssh.ssh_execute("ls")
def test_ssh_port_error(self): """ test a SSH port wrong assignment """ protocol = OpenSSHProtocol( "localhost", 2200, "", "", 2.0 ) ssh = OpenSSH(protocol) with self.assertRaises(SSHConnectionError): ssh.ssh_execute("ls")
def test_ssh_execute(self): """ test ssh_execute function """ protocol = OpenSSHProtocol( "localhost", 22, "", "", 2.0 ) ssh = OpenSSH(protocol) results = ["0", "1"] stream = self._create_stream(self.commands, results) self.assertEqual(ssh.ssh_execute(self.commands, stream), results)
def cleanup(self): """ Remove the transferred files from target """ if not self._deploy_exists: self._logger.debug("deploy is not defined. Skipping cleanup") return self.events.cleanupTargetStarted() paths = [] for item in self._deploy_data: paths.append(item.dst) if isinstance(self._deploy_protocol, SFTPProtocol): openssh = OpenSSH(self._deploy_protocol) openssh.sftp_remove(paths, \ self.events.cleanupTargetPath) else: raise NotImplementedError(\ "'%s' protocol is not implemented"%self._deploy_protocol.name()) self.events.cleanupTargetCompleted()
def test_sftp_path_not_exist_error(self): """ test transferring a path that does not exist """ protocol = SFTPProtocol( "localhost", 22, "", "", 2.0 ) ssh = OpenSSH(protocol) with self.assertRaises(LocalPathNotExistError): item = DataItem("this_src_does_not_exist", "", "remote") ssh.sftp_transfer([item]) with self.assertRaises(RemotePathNotExistError): item = DataItem("this_src_does_not_exist", "", "local") ssh.sftp_transfer([item])