예제 #1
0
 def __getattr__(self, name):
     if name == 'spawn':
         if is_windows():
             return self._paramikospawn
         raise AttributeError(
             "{clsname} has no attribute 'spawn'".format(
                 clsname=self.__class__.__name__))
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
예제 #4
0
def test_get_exe_suffix(monkeypatch, platform, iswin):
    monkeypatch.setattr(sys, 'platform', platform)
    assert is_windows() is iswin
                                                 **kwargs)
    assert_result_success(ret)


def assert_result_success(result):
    assert_result_status(result, expected_status='0')


def assert_result_status(result, expected_status):
    assert result.status == expected_status
    assert result.stdout == 'out'
    assert result.stderr == 'err'


@pytest.mark.usefixtures('mock_interactivesession')
@pytest.mark.xfail(is_windows(), reason="Windows")
def test_execute_command_in_target_timeout(remoterunner):
    remoterunner.set_default_target_property('prompt_timeout', 0.01)
    with pytest.raises(RunnerTimeout) as excinfo:
        remoterunner.execute_command_in_target('echo out;sleep 1',
                                               timeout=0.01)

    assert excinfo.value.args[0].status == -15
    assert excinfo.value.args[0].stdout == b'out\n'
    assert excinfo.value.args[0].stderr == b''


@pytest.mark.usefixtures('mock_time_sleep')
def test_run_raises_sessioninitalizationfailed(remoterunner):
    exception = Exception('message')
    remoterunner.set_target(shelldicts=[{
import subprocess
import os
import logging

from crl.interactivesessions.pexpectplatform import is_windows

__copyright__ = 'Copyright (C) 2019, Nokia'

logger = logging.getLogger(__name__)

if is_windows():
    origpopen = subprocess.Popen

    def windowspopen(*args, **kwargs):
        kwargsmod = kwargs.copy()
        if 'preexec_fn' in kwargs:
            del kwargsmod['preexec_fn']
        if 'executable' in kwargsmod:
            del kwargsmod['executable']
        logger.debug('origpopen(args=%s, kwargs=%s)', args, kwargsmod)

        return origpopen(*args, **kwargsmod)

    os.setsid = lambda *args, **kwargs: None
    subprocess.Popen = windowspopen
import os
import signal

import pytest

from crl.interactivesessions.pexpectplatform import is_windows
from .utils import verify_kill, cmdline_should_not_be_running_or_zompie


__copyright__ = 'Copyright (C) 2019, Nokia'


@pytest.mark.xfail(is_windows(), reason='Windows pexpect cannot spawn BashShell')
def test_background_nohup(bash_remoterunner,
                          bash_remoterunner_initializer,
                          sleeptime,
                          caplog):
    caplog.set_level(7)
    bash_pid = get_bashshell_pid(bash_remoterunner)
    cmd = 'sleep {}'.format(sleeptime)

    background_pid = bash_remoterunner.execute_nohup_background_in_target(cmd)

    with verify_kill(bash_pid):
        os.kill(bash_pid, signal.SIGHUP)
    bash_remoterunner.close()
    bash_remoterunner_initializer.initializer(bash_remoterunner)
    with verify_kill(background_pid):
        bash_remoterunner.execute_command_in_target("pkill -f '{}'".format(cmd))

    cmdline_should_not_be_running_or_zompie(cmd.split())
def mock_spawn(request):
    if not is_windows():
        m = mock.patch('pexpect.spawn')
        request.addfinalizer(m.stop)
        return m.start()
def mock_shell():
    m = mock.create_autospec(InteractiveSession.Shell)
    m.start.return_value = mock.Mock()
    if is_windows():
        m.spawn = mock.Mock()
    return m