def test_body(self):
        filepath = ''
        console = None
        temp_folder = None
        if self.run_on_host:
            console = HostConsole('sh')
            filepath = self.executable_file
        else:
            console = self.board.console
            if not console:
                raise ValueError('Current console is null, cannot copy executable')

            if self.host_file:
                self.check_console_supports_copy(console)
                filepath, temp_folder = self.deploy_file_in_tmp_folder(
                    file=self.executable_file, console=console)
            else:
                filepath = self.executable_file

        try:
            CommandRunner.run(test_name=self._test_name, command=filepath,
                              console=console, timeout=self.timeout)
        finally:
            if temp_folder:
                CommandRunner.run(test_name=self._test_name, command=f'rm -r {temp_folder}',
                                  console=console, timeout=self.timeout)
Beispiel #2
0
    def run_command(self,
                    console: ConsoleBase,
                    script: str,
                    timeout: Optional[float] = None) -> str:
        timeout = timeout or self.timeout

        if self.runs_in_shell:
            output = CommandRunner.run(test_name=self._test_name,
                                       console=console,
                                       command=script,
                                       timeout=timeout)
        else:
            output = CommandRunner.run_raw(test_name=self._test_name,
                                           console=console,
                                           command=script,
                                           timeout=timeout)

        if self.should_match_regex or self.should_not_match_regex:
            CommandRunner.check_output(test_name=self._test_name,
                                       command=script,
                                       output=output,
                                       match_regex=self.should_match_regex,
                                       error_regex=self.should_not_match_regex)

        log.log(CommandRunner.format_command_log(sent=script, output=output))
        return output
Beispiel #3
0
def test_CommandRunner_check_output_should_error(match_regex, error_regex,
                                                 output):
    with pytest.raises(TaskFailed):
        CommandRunner.check_output(test_name='test',
                                   command='cmd',
                                   output=output,
                                   match_regex=match_regex,
                                   error_regex=error_regex)
Beispiel #4
0
def test_CommandRunner_check_output_should_succeed(match_regex, error_regex,
                                                   output):

    CommandRunner.check_output(test_name='test',
                               command='cmd',
                               output=output,
                               match_regex=match_regex,
                               error_regex=error_regex)
    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
Beispiel #6
0
def test_CommandRunner_query_return_code_should_work_with_dirty_output(
        mock_console):
    retcode = 123
    mock_console.send_and_read.return_value = f'abc\r\nabcretcode={retcode} abc\nabc'

    retcode_read = CommandRunner.query_return_code(mock_console)
    assert retcode == retcode_read
Beispiel #7
0
    def test_body(self):
        console = self.board.console
        if not console:
            raise TaskFailed('No console available')

        output = CommandRunner.run(test_name=str(self),
                                   console=console,
                                   command='lsmod',
                                   timeout=3)

        missing_modules = []
        for module in self.modules:
            found = False
            # For some reason, re.MULTILINE fails to match
            for line in output.splitlines():
                if re.match(fr'^{module}\b', line):
                    found = True
                    break

            if not found:
                missing_modules.append(module)

        if missing_modules:
            raise TaskFailed(
                f'The following kernel modules are not loaded: "{missing_modules}"'
            )
    def test_body(self):
        filepath = ''
        console = None
        temp_folder = None
        if self.run_on_host:
            console = HostConsole('sh')
            filepath = self.executable_file
        else:
            console = self.board.console
            if self.host_file:
                temp_folder = self.random_folder_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(self.executable_file))
                console.copy_to_target(self.executable_file, destination)
                filepath = destination
            else:
                filepath = self.executable_file

        if not console:
            raise TaskFailed(
                f'Failed to run script test "{self}": no console available')

        try:
            CommandRunner.run(test_name=self,
                              command=filepath,
                              console=console,
                              timeout=self.timeout)
        finally:
            if temp_folder:
                CommandRunner.run(test_name=self,
                                  command=f'rm -r {temp_folder}',
                                  console=console,
                                  timeout=self.timeout)
Beispiel #9
0
def test_CommandRunner_output_matches_methods_should_match_list(
        patterns, output):
    assert CommandRunner.output_matches_any_pattern(patterns, output) is True
    assert CommandRunner.output_matches_all_patterns(patterns, output) is True
Beispiel #10
0
def test_CommandRunner_query_return_code_should_call_send_and_read(
        mock_console):
    mock_console.send_and_read.return_value = ''
    CommandRunner.query_return_code(mock_console)
    mock_console.send_and_read.assert_called_once_with('echo retcode=$?')
Beispiel #11
0
def test_CommandRunner_output_matches_all_patterns(patterns, output, expected):
    assert CommandRunner.output_matches_all_patterns(patterns,
                                                     output) is expected
Beispiel #12
0
def test_CommandRunner_query_return_code_should_return_retcode(
        mock_console, retcode):
    mock_console.send_and_read.return_value = f'retcode={retcode}'
    assert CommandRunner.query_return_code(mock_console) == retcode
Beispiel #13
0
def test_CommandRunner_query_return_code_should_return_none_if_no_match(
        mock_console, console_output):
    mock_console.send_and_read.return_value = console_output
    assert CommandRunner.query_return_code(mock_console) is None