Beispiel #1
0
def test_expect_value(app_pidfile_env):
    """Test that expect value is being changed to the pattern."""
    env = {'BOARD': 'board', 'APPLICATION': './echo.py'}
    env.update(app_pidfile_env)

    ctrl = riotctrl.ctrl.RIOTCtrl(APPLICATIONS_DIR, env)
    ctrl.TERM_STARTED_DELAY = 1

    with ctrl.run_term(logfile=sys.stdout) as child:
        child.expect_exact('Starting RIOT Ctrl')

        # Exception is 'exc_info.value' and pattern is in 'exc.value'
        child.sendline('lowercase')
        with pytest.raises(pexpect.TIMEOUT) as exc_info:
            child.expect('UPPERCASE', timeout=0.5)
        assert str(exc_info.value) == 'UPPERCASE'

        # value updated and old value saved
        assert exc_info.value.value == 'UPPERCASE'
        assert exc_info.value.pexpect_value.startswith('Timeout exceeded.')

        # check the context is removed (should be only 2 levels)
        assert len(exc_info.traceback) == 2

        child.sendline('lowercase')
        with pytest.raises(pexpect.TIMEOUT) as exc_info:
            child.expect_exact('UPPERCASE', timeout=0.5)
        assert str(exc_info.value) == 'UPPERCASE'
Beispiel #2
0
def test_shell_interaction_cmd(app_pidfile_env):
    """Test basic functionalities with the 'shell' application."""
    ctrl = init_ctrl(app_pidfile_env)
    with ctrl.run_term(logfile=sys.stdout, reset=False):
        shell = riotctrl.shell.ShellInteraction(ctrl)
        res = shell.cmd('foobar')
        assert 'foobar' in res
        res = shell.cmd('snafoo')
        assert 'snafoo' in res
Beispiel #3
0
def test_shell_interaction_cmd_first_prompt_missing(app_pidfile_env):
    """Test basic functionalities with the 'shell' application when first
    prompt is missing."""
    ctrl = init_ctrl(app_pidfile_env, skip_first_prompt=True)
    with ctrl.run_term(logfile=sys.stdout, reset=False):
        shell = riotctrl.shell.ShellInteraction(ctrl)
        res = shell.cmd('foobar')
        assert 'foobar' in res
        res = shell.cmd('snafoo')
        assert 'snafoo' in res
Beispiel #4
0
def test_running_term_without_reset(app_pidfile_env):
    """Test not resetting ctrl on run_term."""
    env = {'BOARD': 'board'}
    env.update(app_pidfile_env)
    env['APPLICATION'] = './hello.py'

    ctrl = riotctrl.ctrl.RIOTCtrl(APPLICATIONS_DIR, env)
    ctrl.TERM_STARTED_DELAY = 1

    with ctrl.run_term(reset=False, logfile=sys.stdout) as child:
        child.expect_exact('Starting RIOT Ctrl')
        child.expect_exact('Hello World')
        # Firmware should start only once
        with pytest.raises(pexpect.exceptions.TIMEOUT):
            child.expect_exact('Starting RIOT Ctrl', timeout=1)
Beispiel #5
0
def test_running_term_with_reset(app_pidfile_env):
    """Test that ctrl resets on run_term."""
    env = {'BOARD': 'board'}
    env.update(app_pidfile_env)
    env['APPLICATION'] = './hello.py'

    ctrl = riotctrl.ctrl.RIOTCtrl(APPLICATIONS_DIR, env)
    ctrl.TERM_STARTED_DELAY = 1

    with ctrl.run_term(logfile=sys.stdout) as child:
        # Firmware should have started twice
        child.expect_exact('Starting RIOT Ctrl')
        child.expect_exact('Hello World')
        child.expect_exact('Starting RIOT Ctrl')
        child.expect_exact('Hello World')
Beispiel #6
0
def test_expect_not_matching_stdin(app_pidfile_env):
    """Test that expect does not match stdin."""
    env = {'BOARD': 'board', 'APPLICATION': './hello.py'}
    env.update(app_pidfile_env)

    ctrl = riotctrl.ctrl.RIOTCtrl(APPLICATIONS_DIR, env)
    ctrl.TERM_STARTED_DELAY = 1

    with ctrl.run_term(logfile=sys.stdout) as child:
        child.expect_exact('Starting RIOT Ctrl')
        child.expect_exact('Hello World')

        msg = "This should not be matched as it is on stdin"
        child.sendline(msg)
        matched = child.expect_exact([pexpect.TIMEOUT, msg], timeout=1)
        assert matched == 0
Beispiel #7
0
def test_running_echo_application(app_pidfile_env):
    """Test basic functionalities with the 'echo' application."""
    env = {'BOARD': 'board', 'APPLICATION': './echo.py'}
    env.update(app_pidfile_env)

    ctrl = riotctrl.ctrl.RIOTCtrl(APPLICATIONS_DIR, env)
    ctrl.TERM_STARTED_DELAY = 1

    with ctrl.run_term(logfile=sys.stdout) as child:
        child.expect_exact('Starting RIOT Ctrl')

        # Test multiple echo
        for i in range(16):
            child.sendline('Hello Test {}'.format(i))
            child.expect(r'Hello Test (\d+)', timeout=1)
            num = int(child.match.group(1))
            assert i == num
Beispiel #8
0
def test_reset():
    env = {'DEBUG_ADAPTER_ID': '', 'BOARD': 'native'}
    ctrl = riotctrl_ctrl.native.NativeRIOTCtrl(
        application_directory=os.path.normpath(
            os.path.join(os.path.abspath(__file__), '..', '..', '..', '..',
                         '..', 'examples', 'hello-world')),
        env=env,
    )
    print(ctrl.application_directory)
    assert not ctrl.env['DEBUG_ADAPTER_ID']
    ctrl.make_run(['flash'])
    with ctrl.run_term(reset=False):
        # DEBUG_ADAPTER_ID is now a PID
        assert int(ctrl.env['DEBUG_ADAPTER_ID'])
        ctrl.term.expect_exact('Hello World!')
        ctrl.reset()
        ctrl.term.expect_exact('!! REBOOT !!')
        ctrl.term.expect_exact('Hello World!')
Beispiel #9
0
def test_running_error_cases(app_pidfile_env):
    """Test basic functionalities with the 'echo' application.

    This tests:
    * stopping already stopped child
    """
    # Use only 'echo' as process to exit directly
    env = {'BOARD': 'board',
           'NODE_WRAPPER': 'echo', 'APPLICATION': 'Starting RIOT Ctrl'}
    env.update(app_pidfile_env)

    ctrl = riotctrl.ctrl.RIOTCtrl(APPLICATIONS_DIR, env)
    ctrl.TERM_STARTED_DELAY = 1

    with ctrl.run_term(logfile=sys.stdout) as child:
        child.expect_exact('Starting RIOT Ctrl')

        # Term is already finished and expect should trigger EOF
        with pytest.raises(pexpect.EOF):
            child.expect('this should eof')
Beispiel #10
0
def test_term_cleanup(app_pidfile_env):
    """Test a terminal that does a cleanup after kill.

    The term process should be able to run its cleanup.
    """
    # Always run as 'deleted=True' to deleted even on early exception
    # File must exist at the end of the context manager
    with tempfile.NamedTemporaryFile(delete=True) as tmpfile:
        env = {'BOARD': 'board'}
        env.update(app_pidfile_env)
        env['APPLICATION'] = './create_file.py %s' % tmpfile.name

        ctrl = riotctrl.ctrl.RIOTCtrl(APPLICATIONS_DIR, env)
        ctrl.TERM_STARTED_DELAY = 1
        with ctrl.run_term(logfile=sys.stdout) as child:
            child.expect_exact('Running')
            # Ensure script is started correctly
            content = open(tmpfile.name, 'r', encoding='utf-8').read()
            assert content == 'Running\n'

        # File should not exist anymore so no error to create one
        # File must exist to be cleaned by tempfile
        open(tmpfile.name, 'x')