def deploy_file_in_tmp_folder(self, file: str,
                                  console: ConsoleBase) -> Tuple[str, str]:
        '''Deploy the file, and returns its full path and containing temporary folder'''
        temp_folder = random_dir_name()
        CommandRunner.run(test_name=self._test_name,
                          command=f'mkdir {temp_folder}',
                          console=console, timeout=self.timeout)
        destination = os.path.join(temp_folder,
                                   os.path.basename(file))
        console.copy_to_target(source=file,
                               destination=destination)

        return destination, temp_folder
Esempio n. 2
0
    def run(test_name: str, console: ConsoleBase, command: str, timeout: float = None) -> str:
        '''Run a command in a Shell context'''
        retcode_token = 'pluma-retcode='
        base_command = command
        command += f' ; echo {retcode_token}$?'
        output, matched = console.send_and_expect(
            command, timeout=10, match=retcode_token+r'-?\d+')

        if not matched:
            CommandRunner.log_error(test_name=test_name, sent=command, output=output,
                                    error='No response within timeout, or failed device'
                                    ' failed to send return code. If this is not running'
                                    ' in a shell, set "runs_in_shell" to "false".')

        output = re.sub(re.escape(matched) + r'$', '', output)
        retcode_match = re.search(retcode_token+r'(-?\d+)', matched)
        if not retcode_match:
            raise TestingException('Failed to find return code value')

        retcode = int(retcode_match.group(1))
        if retcode == 0:
            error = None
        elif retcode is None:
            error = 'failed to retrieve return code for command'
        else:
            error = f'returned with exit code {retcode}'

        if error:
            CommandRunner.log_error(test_name=test_name, sent=command, output=output,
                                    error=f'Command "{command}" {error}')

        output = CommandRunner.cleanup_command_output(command, output)
        log.log(CommandRunner.format_command_log(sent=base_command, output=output))

        return output
Esempio n. 3
0
    def query_return_code(console: ConsoleBase):
        '''Query and return the return code of the last command ran.'''
        retcode_output = console.send_and_read('echo retcode=$?')
        if not retcode_output:
            return None

        retcode_match = re.search(r'retcode=(-?\d+)\b', retcode_output,
                                  re.MULTILINE)
        if not retcode_match:
            return None

        return int(retcode_match.group(1))
Esempio n. 4
0
    def run_raw(test_name: str, console: ConsoleBase, command: str,
                timeout: int = None) -> str:
        '''Run a command with minimal assumptions regarding the context'''
        output = console.send_and_read(command, timeout=timeout, quiet_time=timeout)
        output = CommandRunner.cleanup_command_output(command, output)

        if not output:
            CommandRunner.log_error(test_name=test_name, sent=command, output=output,
                                    error='No response received after sending command')
        else:
            log.debug(CommandRunner.format_command_log(sent=command, output=output))

        return output