def test_SerialConsole_login_finds_user_match_sends_correct_username(serial_console_proxy): user_match = 'Enter username: '******'Foo' nonblocking( serial_console_proxy.console.login, username_match=user_match, username=username) # Expect line break, to force printing the prompt received = serial_console_proxy.read_serial_output() expected = f'{serial_console_proxy.console.linesep}' assert received == expected for i in range(0, 10): serial_console_proxy.fake_reception( f'Nonsense non matching line {i}...') time.sleep(0.01) serial_console_proxy.fake_reception(user_match) received = serial_console_proxy.read_serial_output() expected = f'{username}{serial_console_proxy.console.linesep}' assert received == expected
def test_SerialConsole_login_success_with_no_password(serial_console_proxy): user_match = 'Enter username: '******'Foo' async_result = nonblocking( serial_console_proxy.console.login, username_match=user_match, username=username) # Expect line break, used to force printing the prompt received = serial_console_proxy.read_serial_output() expected = f'{serial_console_proxy.console.linesep}' assert received == expected for i in range(0, 10): serial_console_proxy.fake_reception( f'Nonsense non matching line {i}...') time.sleep(0.01) serial_console_proxy.fake_reception(user_match) received = serial_console_proxy.read_serial_output() expected = f'{username}{serial_console_proxy.console.linesep}' assert received == expected # Wait for console.login() to finish async_result.get() # If we've gotten to here without error then success assert True
def test_SerialConsole_check_alive_returns_true_when_target_not_responds(serial_console_proxy): async_result = nonblocking( serial_console_proxy.console.check_alive, timeout=1) # Send no response to console assert async_result.get() is False
def test_SerialConsole_check_alive_returns_true_when_target_responds(serial_console_proxy): async_result = nonblocking( serial_console_proxy.console.check_alive) # Send console a newline char serial_console_proxy.fake_reception(serial_console_proxy.console.linesep) assert async_result.get() is True
def test_SerialConsole_send_returns_received_false_when_no_match_available(serial_console_proxy): msg = loremipsum wont_match = 'Baz' async_result = nonblocking(serial_console_proxy.console.send, match=wont_match, timeout=0.5) serial_console_proxy.fake_reception(msg) __, matched = async_result.get() assert matched is False
def test_SerialConsole_send_and_expect_returns_received_if_no_match(serial_console_proxy): expected_received = 'Not really matching.' regex = 'A regex' send_and_expect_result = nonblocking(serial_console_proxy.console.send_and_expect, cmd='abc', match=regex, timeout=0.5) serial_console_proxy.fake_reception(expected_received) received, matched = send_and_expect_result.get() assert received == expected_received assert matched is None
def test_SerialConsole_send_matches_regex(serial_console_proxy): msg = 'Hello World! 123FooBarBaz' regex = '[0-3]+Foo' expected_match = '123Foo' async_result = nonblocking(serial_console_proxy.console.send, match=regex) serial_console_proxy.fake_reception(msg) __, matched = async_result.get() assert matched == expected_match
def test_SerialConsole_send_and_expect_returns_after_timeout(serial_console_proxy, timeout): start_time = time.time() send_and_expect_result = nonblocking(serial_console_proxy.console.send_and_expect, cmd='abc', match='NoFoo', timeout=timeout) serial_console_proxy.fake_reception('abc\ndef') send_and_expect_result.get() total_duration = time.time() - start_time assert 0.8 * timeout < total_duration < 1.2*timeout
def test_SerialConsole_send_and_expect_ignores_previous_content(serial_console_proxy): expected_received = 'Not really matching.' regex = '[0-3]+Match' serial_console_proxy.fake_reception(expected_received) send_and_expect_result = nonblocking(serial_console_proxy.console.send_and_expect, cmd='abc', match=regex, timeout=0.5) received, matched = send_and_expect_result.get() assert received == received assert matched is None
def test_SerialConsole_send_returns_data_when_receive_arg_true(serial_console_proxy): msg = 'Bar' async_result = nonblocking(serial_console_proxy.console.send, receive=True) # Wait short time for function to start time.sleep(0.1) serial_console_proxy.fake_reception(msg) received, __ = async_result.get() assert received == msg
def test_SerialConsole_send_and_expect_matches_regex(serial_console_proxy): expected_match = '123Match' expected_received = f'Multiline content\n and {expected_match}' regex = '[0-3]+Match' send_and_expect_result = nonblocking(serial_console_proxy.console.send_and_expect, cmd='abc', match=regex) serial_console_proxy.fake_reception(expected_received+'Trailing content') received, matched = send_and_expect_result.get() assert received == expected_received assert matched == expected_match
def test_SerialConsole_send_returns_received_when_match_available(serial_console_proxy): before_match, to_match = 'Foo', 'Bar' msg = before_match + to_match async_result = nonblocking(serial_console_proxy.console.send, match=to_match) # Wait short time for function to start time.sleep(0.1) serial_console_proxy.fake_reception(msg) received, __ = async_result.get() assert received == before_match
def test_WaitForPatternAction_should_succeed_when_matched_proxy_console( mock_board, serial_console_proxy): mock_board = Board("board", console=serial_console_proxy.console) expected_pattern = 'abc' action = WaitForPatternAction(mock_board, pattern=expected_pattern) async_action = nonblocking(action.execute) # Wait to make sure what we write isn't discarded time.sleep(0.1) serial_console_proxy.proxy.write('some content') serial_console_proxy.proxy.write('abc\n') async_action.get()
def test_ConsoleBase_send_and_expect_match_multiline_reception(basic_console): match_result = MatchResult(regex_matched='de*f', text_matched='abc', text_received='abc\ndef') basic_console.engine.wait_for_match = MagicMock(return_value=match_result) async_result = nonblocking(basic_console.send_and_expect, cmd='', match='anything', timeout=0.2) received, matched = async_result.get() assert received == match_result.text_received assert matched == match_result.text_matched
def test_SerialConsole_login_except_on_wrong_success_match(serial_console_proxy): user_match = 'Enter username: '******'Enter password: '******'command prompt >>' username = '******' password = '******' async_result = nonblocking( serial_console_proxy.console.login, username_match=user_match, password_match=pass_match, username=username, password=password, success_match=success_match) # Wait short time for function to start time.sleep(0.1) # Expect line break, used to force printing the prompt received = serial_console_proxy.read_serial_output() expected = f'{serial_console_proxy.console.linesep}' assert received == expected for i in range(0, 10): serial_console_proxy.fake_reception( f'Nonsense non matching line {i}...') time.sleep(0.01) serial_console_proxy.fake_reception(user_match) received = serial_console_proxy.read_serial_output() expected = f'{username}{serial_console_proxy.console.linesep}' assert received == expected serial_console_proxy.fake_reception(pass_match) received = serial_console_proxy.read_serial_output() expected = f'{password}{serial_console_proxy.console.linesep}' assert received == expected serial_console_proxy.fake_reception( 'This is not the success_match you are looking for') with pytest.raises(ConsoleLoginFailedError): # Wait for console.login() to finish async_result.get()
def test_ConsoleBase_send_and_expect_returns_received_and_none_when_no_match_available( basic_console): match_result = MatchResult(regex_matched=None, text_matched=None, text_received='abc\ndef') basic_console.engine.wait_for_match = MagicMock(return_value=match_result) async_result = nonblocking(basic_console.send_and_expect, cmd='', match='anything', timeout=0.2) received, matched = async_result.get() assert received == match_result.text_received assert matched is None
def test_SerialConsole_send_and_expect_matches_regex_with_previous_content( serial_console_proxy): expected_match = '123Match' expected_received = f'Multiline content\n and {expected_match}' regex = '[0-3]+Match' serial_console_proxy.fake_reception('Some content sent before trying to match') send_and_expect_result = nonblocking(serial_console_proxy.console.send_and_expect, cmd='abc', match=regex) serial_console_proxy.fake_reception(expected_received) received, matched = send_and_expect_result.get() assert received == expected_received assert matched == expected_match
def _request_term_info(request, until, out_stream=None, in_stream=None, timeout=0.5): _out = out_stream or sys.stdout _in = in_stream or sys.stdin with raw(_in): with nonblocking(_in): _out.write(request) _out.flush() resp = c = '' start = time() while not resp.endswith(until): c = _in.read(1) if c: resp += c elif (time() - start) > timeout: raise Exception("Request timed out") return resp
def test_WaitForPatternAction_should_fail_when_no_matched_proxy_console( mock_board, serial_console_proxy): mock_board = Board("board", console=serial_console_proxy.console) expected_pattern = 'abc' action = WaitForPatternAction(mock_board, pattern=expected_pattern, timeout=1) async_action = nonblocking(action.execute) # Wait to make sure what we write isn't discarded time.sleep(0.1) serial_console_proxy.proxy.write('some content') serial_console_proxy.proxy.write('and more\n') with pytest.raises(TaskFailed): async_action.get()
def test_ConsoleBase_wait_for_quiet_should_return_when_quiet( basic_console, quiet_time, non_quiet_time): start = time.time() async_result = nonblocking(basic_console.wait_for_quiet, quiet=quiet_time, timeout=5) data_end = start + non_quiet_time while (time.time() < data_end): basic_console.engine.received += 'abc' time.sleep(0.025) success = async_result.get() elapsed = time.time() - start assert success is True total_time = non_quiet_time + quiet_time assert 0.8 * total_time < elapsed < 1.2 * total_time
def test_SerialConsole_login_except_on_wrong_username_match(serial_console_proxy): user_match = 'Enter username: '******'Foo' async_result = nonblocking( serial_console_proxy.console.login, username_match=user_match, username=username) for i in range(0, 10): serial_console_proxy.fake_reception( f'Nonsense non matching line {i}...') serial_console_proxy.fake_reception( 'This is not the username_match you are looking for') with pytest.raises(ConsoleLoginFailedError): # Wait for console.login() to finish async_result.get()
def test_ConsoleBase_wait_for_quiet_should_return_when_quiet(serial_console_proxy, quiet_time, non_quiet_time): serial_console_proxy.console.open() start = time.time() async_result = nonblocking(serial_console_proxy.console.wait_for_quiet, quiet=quiet_time, timeout=5) data_start = time.time() data_end = data_start + non_quiet_time while(time.time() < data_end): serial_console_proxy.fake_reception('abc', wait_time=0.02) success = async_result.get() elapsed = time.time() - start assert success is True total_time = non_quiet_time+quiet_time assert 0.8*total_time < elapsed < 1.2*total_time