|
| 1 | +# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""Tests scenario for the Firecracker serial console.""" |
| 4 | +import os |
| 5 | +import time |
| 6 | +import select |
| 7 | + |
| 8 | + |
| 9 | +# Too few public methods (1/2) (too-few-public-methods) |
| 10 | +# pylint: disable=R0903 |
| 11 | +class MatchStaticString: |
| 12 | + """Match a static string versus input.""" |
| 13 | + |
| 14 | + # Prevent state objects from being collected by pytest. |
| 15 | + __test__ = False |
| 16 | + |
| 17 | + def __init__(self, match_string): |
| 18 | + """Initialize using specified match string.""" |
| 19 | + self._string = match_string |
| 20 | + self._input = "" |
| 21 | + |
| 22 | + def match(self, input_char) -> bool: |
| 23 | + """ |
| 24 | + Check if `_input` matches the match `_string`. |
| 25 | +
|
| 26 | + Process one char at a time and build `_input` string. |
| 27 | + Preserve built `_input` if partially matches `_string`. |
| 28 | + Return True when `_input` is the same as `_string`. |
| 29 | + """ |
| 30 | + self._input += str(input_char) |
| 31 | + if self._input == self._string[:len(self._input)]: |
| 32 | + if len(self._input) == len(self._string): |
| 33 | + self._input = "" |
| 34 | + return True |
| 35 | + return False |
| 36 | + |
| 37 | + self._input = self._input[1:] |
| 38 | + return False |
| 39 | + |
| 40 | + |
| 41 | +class TestState(MatchStaticString): |
| 42 | + """Generic test state object.""" |
| 43 | + |
| 44 | + # Prevent state objects from being collected by pytest. |
| 45 | + __test__ = False |
| 46 | + |
| 47 | + def __init__(self, match_string=''): |
| 48 | + """Initialize state fields.""" |
| 49 | + MatchStaticString.__init__(self, match_string) |
| 50 | + print('\n*** Current test state: ', str(self), end='') |
| 51 | + |
| 52 | + def handle_input(self, microvm, input_char): |
| 53 | + """Handle input event and return next state.""" |
| 54 | + |
| 55 | + def __repr__(self): |
| 56 | + """Leverages the __str__ method to describe the TestState.""" |
| 57 | + return self.__str__() |
| 58 | + |
| 59 | + def __str__(self): |
| 60 | + """Return state name.""" |
| 61 | + return self.__class__.__name__ |
| 62 | + |
| 63 | + |
| 64 | +class WaitLogin(TestState): |
| 65 | + """Initial state when we wait for the login prompt.""" |
| 66 | + |
| 67 | + def handle_input(self, microvm, input_char) -> TestState: |
| 68 | + """Handle input and return next state.""" |
| 69 | + if self.match(input_char): |
| 70 | + # Send login username. |
| 71 | + microvm.serial_input("root") |
| 72 | + return WaitPasswordPrompt("Password:") |
| 73 | + return self |
| 74 | + |
| 75 | + |
| 76 | +class WaitPasswordPrompt(TestState): |
| 77 | + """Wait for the password prompt to be shown.""" |
| 78 | + |
| 79 | + def handle_input(self, microvm, input_char) -> TestState: |
| 80 | + """Handle input and return next state.""" |
| 81 | + if self.match(input_char): |
| 82 | + microvm.serial_input("root") |
| 83 | + # Wait 1 second for shell |
| 84 | + time.sleep(1) |
| 85 | + microvm.serial_input("id") |
| 86 | + return WaitIDResult("uid=0(root) gid=0(root) groups=0(root)") |
| 87 | + return self |
| 88 | + |
| 89 | + |
| 90 | +class WaitIDResult(TestState): |
| 91 | + """Wait for the console to show the result of the 'id' shell command.""" |
| 92 | + |
| 93 | + def handle_input(self, microvm, input_char) -> TestState: |
| 94 | + """Handle input and return next state.""" |
| 95 | + if self.match(input_char): |
| 96 | + return TestFinished() |
| 97 | + return self |
| 98 | + |
| 99 | + |
| 100 | +class TestFinished(TestState): |
| 101 | + """Test complete and successful.""" |
| 102 | + |
| 103 | + def handle_input(self, microvm, input_char) -> TestState: |
| 104 | + """Return self since the test is about to end.""" |
| 105 | + return self |
| 106 | + |
| 107 | + |
| 108 | +def test_serial_console_login(test_microvm_with_ssh): |
| 109 | + """Test serial console login.""" |
| 110 | + microvm = test_microvm_with_ssh |
| 111 | + microvm.jailer.daemonize = False |
| 112 | + microvm.spawn() |
| 113 | + |
| 114 | + # We don't need to monitor the memory for this test because we are |
| 115 | + # just rebooting and the process dies before pmap gets the RSS. |
| 116 | + microvm.memory_events_queue = None |
| 117 | + |
| 118 | + # Set up the microVM with 1 vCPU and a serial console. |
| 119 | + microvm.basic_config(vcpu_count=1, |
| 120 | + boot_args='console=ttyS0 reboot=k panic=1 pci=off') |
| 121 | + |
| 122 | + microvm.start() |
| 123 | + |
| 124 | + # Screen stdout log |
| 125 | + screen_log = "/tmp/screen.log" |
| 126 | + |
| 127 | + # Open the screen log file. |
| 128 | + screen_log_fd = os.open(screen_log, os.O_RDONLY) |
| 129 | + poller = select.poll() |
| 130 | + |
| 131 | + # Set initial state - wait for 'login:' prompt |
| 132 | + current_state = WaitLogin("login:") |
| 133 | + |
| 134 | + poller.register(screen_log_fd, select.POLLIN | select.POLLHUP) |
| 135 | + |
| 136 | + while not isinstance(current_state, TestFinished): |
| 137 | + result = poller.poll(0.1) |
| 138 | + for fd, flag in result: |
| 139 | + if flag & select.POLLIN: |
| 140 | + output_char = str(os.read(fd, 1), |
| 141 | + encoding='utf-8', |
| 142 | + errors='ignore') |
| 143 | + # [DEBUG] Uncomment to see the serial console output. |
| 144 | + # print(output_char, end='') |
| 145 | + current_state = current_state.handle_input( |
| 146 | + microvm, output_char) |
| 147 | + elif flag & select.POLLHUP: |
| 148 | + assert False, "Oh! The console vanished before test completed." |
| 149 | + os.close(screen_log_fd) |
0 commit comments