def test_progress_log(remoterunner, intcaplog):
    ret = remoterunner.execute_command_in_target(
        'echo -n progress;echo log;>&2 echo err', progress_log=True)

    LOGGER.debug("===== test_progress_log: intcaplog.text == %s",
                 intcaplog.text)
    assert ': progresslog' in intcaplog.text
    assert ret == RunResult(status='0', stdout='progresslog', stderr='err')
def test_kill_background_execution(remoterunner):
    remoterunner.execute_background_command_in_target(
        'echo out;>&2 echo err;sleep 10', exec_id='exec_id')

    expected_result = RunResult(status=str(-signal.SIGTERM),
                                stdout='out',
                                stderr='err')

    assert remoterunner.kill_background_execution('exec_id') == expected_result
    assert remoterunner.wait_background_execution('exec_id') == expected_result
def test_create_directory_in_target(copyrunner, target, path, mode):
    with target.as_cwd():
        assert copyrunner.create_directory_in_target(
            path, mode=mode, target='target') == RunResult(status='0',
                                                           stdout='',
                                                           stderr='')
        logger.debug('current dir: %s', os.getcwd())
        m = os.stat(path).st_mode
        assert stat.S_ISDIR(m)
        assert stat.S_IMODE(m) == int(mode, 8)
Ejemplo n.º 4
0
def runner_in_target_factory(mock_terminal):
    @contextmanager
    def mock_active_terminal():
        yield mock_terminal

    r_in_target_factory = mock.Mock()
    r_in_target_factory.mock_terminal = mock_terminal
    r_in_target_factory.return_value.active_terminal = mock_active_terminal
    r_in_target_factory.return_value.run.return_value = RunResult(
        '0', 'out\n', 'err\n')
    return r_in_target_factory
def test_copy_directory_to_target(copyrunner, sourcedir, target,
                                  create_target_dir_before, target_dir, mode):
    with target.as_cwd():
        if create_target_dir_before:
            os.makedirs(target_dir)
        assert copyrunner.copy_directory_to_target(
            source_dir=sourcedir,
            target_dir=target_dir,
            mode=mode,
            target='to_target') == RunResult(status='0', stdout='', stderr='')
        dircmp = filecmp.dircmp(sourcedir, target_dir)
        dircmp.report()
        for diff in [dircmp.left_only, dircmp.right_only, dircmp.diff_files]:
            assert not diff
def test_copy_file_from_target(copyrunner, source, target, destination,
                               expected_target, intcaplog):
    with target.as_cwd():
        assert copyrunner.copy_file_from_target(target='from_target',
                                                source_file=source,
                                                destination=destination,
                                                timeout=1) == RunResult(
                                                    status='0',
                                                    stdout='',
                                                    stderr='')

        assert filecmp.cmp(source, expected_target)

    assert "write(*('sourcecontent',)" not in intcaplog.text
def test_copy_file_to_target(copyrunner, source, target, mode, destination_dir,
                             expected_target, intcaplog):
    destination_kwarg = {} if destination_dir is None else {
        'destination_dir': destination_dir
    }
    with target.as_cwd():
        assert copyrunner.copy_file_to_target(
            source_file=source,
            mode=mode,
            target='to_target',
            timeout=1,
            **destination_kwarg) == RunResult(status='0', stdout='', stderr='')

        assert filecmp.cmp(source, expected_target)
        assert oct(os.stat(expected_target).st_mode
                   & 0o777) == mode or is_windows()

    assert "write(*('sourcecontent',)" not in intcaplog.text
def test_copy_file_between_targets(copyrunner, source, target, mode,
                                   destination_dir, expected_target,
                                   intcaplog):
    with target.as_cwd():
        assert copyrunner.copy_file_between_targets(
            from_target='from_target',
            source_file=source,
            to_target='to_target',
            destination_dir=destination_dir,
            mode=mode,
            timeout=1) == RunResult(status='0', stdout='', stderr='')

        logger.debug('pwd: %s, source: %s, exptected_target: %s', os.getcwd(),
                     source, expected_target)
        assert filecmp.cmp(source, expected_target)
        assert oct(os.stat(expected_target).st_mode
                   & 0o777) == mode or is_windows()

    assert "write(*('sourcecontent',)" not in intcaplog.text
def test_execute_background_command_in_target(remoterunner):
    remoterunner.execute_background_command_in_target('echo out;>&2 echo err',
                                                      exec_id='exec_id')
    assert remoterunner.wait_background_execution('exec_id') == RunResult(
        status='0', stdout='out', stderr='err')
    remoterunner.set_default_target_property('prompt_timeout', 1)
    remoterunner.set_default_target_property('termination_timeout',
                                             termination_timeout)
    remoterunner.set_target(shelldicts=[{'shellname': 'ExampleShell'}])


def execute_timeout_command(remoterunner):
    remoterunner.execute_command_in_target("echo out; sleep 0.5", timeout=0.05)


@pytest.mark.usefixtures('mock_interactivesession')
@pytest.mark.xfail(is_windows(), reason="Windows")
@pytest.mark.parametrize(
    'ignoresignals,expected_exception,expected_args',
    [([], RunnerTimeout,
      RunResult(status=-signal.SIGTERM, stdout=b'out\n', stderr=b'')),
     ([signal.SIGTERM
       ], RunnerTimeout, RunResult(status=-9, stdout=b'out\n', stderr=b'')),
     ([signal.SIGTERM, 9], FailedToKillProcess, 'failedtokill')])
def test_execute_timeout_cleaning(remoterunner, ignoresignals,
                                  expected_exception, expected_args):
    setup_target_with_timeout(remoterunner, 0.05)
    with MockKillpg(ignoresignals=ignoresignals):
        with pytest.raises(expected_exception) as excinfo:
            execute_timeout_command(remoterunner)

    if expected_args == 'failedtokill':
        assert 'Killing of the process with pid' in str(excinfo.value)
    else:
        assert excinfo.value.args[0] == expected_args
    remoterunner.execute_command_in_target('echo out')