From patchwork Thu Nov 3 14:13:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 621069 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp355143pvb; Thu, 3 Nov 2022 07:14:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4lATR96j0W5RUVEj1GHyA7FowhF3ulqq7Mc2Y1EMO9uZ+Kf7wLanaB32GfvkVnVnNlafvj X-Received: by 2002:a05:600c:2150:b0:3cf:6c05:b4ab with SMTP id v16-20020a05600c215000b003cf6c05b4abmr17127425wml.161.1667484891104; Thu, 03 Nov 2022 07:14:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667484891; cv=none; d=google.com; s=arc-20160816; b=FLYFtYpaBC3yE6E0yTfUmbB1b3rh35OdKPHbubQZ97AGatxWR7IYBSCBwPwkspjKSj JTHJiMaL7gxW6knHCe5933YIwx5noKc1G/08tIX43t0d8cmSwTg0qejQoraIjYYoKXaw D475h/gt3sHDwktEZ0jNWJYMmKS2PE5h+ODsnn+h4G7XhpllHVOUQOZNlBR1NmWc3TvB EL7DhjpZeQoIu3lR591Lk3YouznfFJAZithPjvpN7Kn7DHAPEpecOfktWOA53ffIUIMn JkKM3tNLgCpY1MjCZkQPxGD/3/72hheG56RfQpst1wzUy8a04L9KP1a353J0yzwgvIeB 9ZGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=uigyf6qq9Am7cRVFYV/7zGs4tD3SV3rZRcR/12kbTNw=; b=RHWIRBcyXKshj3LpbvXiqdv8tbr3u21/eQ3SRbcLDLaYTUXl4pgdSLgFsceD0nwcJ9 idn7A8qUTJycsaUjuKFwR8yA+djJyTtg3b+QKCiDnP0E1iMm+Zk4uiLT7ME5YttJdcVc AlwIbeNk5SBqTjbcP9LqPLI8XFVu4qjif2hFsetfXK58MLRvZ4DI4hRyM8C3UrjOAXW9 BV/WpLfN3JaSkr+32iFikYekBeQ3opP/gfJXOez4MBAs49qxeiY+oKbB+9QmKarGVGSe yi4M3MphaWuzwVsZEaECYdOT+UWPT0IoE1srGQW6aBP+ok9pU54esXiycOSy7p+OK7dS uKqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GJ4cRsqv; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id e9-20020a5d6d09000000b0022e3ed2ab26si1076169wrq.345.2022.11.03.07.14.50 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 03 Nov 2022 07:14:51 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=GJ4cRsqv; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oqayd-0000al-Ly; Thu, 03 Nov 2022 10:13:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oqayb-0000aF-8j for qemu-devel@nongnu.org; Thu, 03 Nov 2022 10:13:49 -0400 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oqayY-0006to-59 for qemu-devel@nongnu.org; Thu, 03 Nov 2022 10:13:48 -0400 Received: by mail-wm1-x333.google.com with SMTP id p16so1220979wmc.3 for ; Thu, 03 Nov 2022 07:13:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=uigyf6qq9Am7cRVFYV/7zGs4tD3SV3rZRcR/12kbTNw=; b=GJ4cRsqvbT4tQCqvNXQlRyFWa4VxsyhIAMYz6nPN7zu6+f5I8kY1M/AsTDozUwKJZI kT6Pd4kSZZWgR1xpaLToiyIdn/wyG84k4kW/VJFzNL6ElMbykEooV8WelPrp2ShRUYuK eqk+0+xFsGHT4hoxuId8QL6gzUDZb4I6HRFJrbdb/xQHhsq6IYeMcJhArgv3EhbAI6Nk jP2kOTJAfdLgt6CtfGvOldvzLWom+R3no7Gg7zrk5r0EIr2Ls5sJp4cKFOMv3utxVCww dB7P4pKP6xSQMbL+58UNecoUwzGOVyMkCGtDKHXn/lKL/hb3bLPOJCcPVuvT+hIaYicl QCWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=uigyf6qq9Am7cRVFYV/7zGs4tD3SV3rZRcR/12kbTNw=; b=YIeUagQO10xkHHk5UtWWMnYMV05iyqNga6CA5M8YSn0no/q0uxG4qd1QvImumtovi1 SSOqQDZlcmewQGPfgW7xe6hmY3ClKioBf0NGS09Wjlfb6OC4KhZ52h7ZFcpSwg9u5OYC oh/DKoSzGeX8zylWb85P8W1m9b7HIv9jQ489jtboSy1PrJkKsn7oItXDfC+3GGjZ2PyA nrUbh7TZQ2Szv1y0IdsuZkZT5b0GuimySVWI2Efk1nOZy0EdAiP86u4IJB49jFD+9Zgw u2K3pRZQ8Ipl4miP30vNQxkNvjytSK4pDsLP+THqxEv6JiP66ucRvqgaFCJ3sMd+qaBf 27Tg== X-Gm-Message-State: ACrzQf3uGutC/ksM5haJMQKPuR9c8NCvn5D2ymEB/LpVLPErJ6q3Uc21 MQlwJGIPRdT76QhAAXdGGIxBYw== X-Received: by 2002:a1c:35c9:0:b0:3c3:d9a:385f with SMTP id c192-20020a1c35c9000000b003c30d9a385fmr19505105wma.139.1667484824427; Thu, 03 Nov 2022 07:13:44 -0700 (PDT) Received: from zen.linaroharston ([185.81.254.11]) by smtp.gmail.com with ESMTPSA id bj9-20020a0560001e0900b002365cd93d05sm1026291wrb.102.2022.11.03.07.13.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 07:13:43 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 31B481FFB7; Thu, 3 Nov 2022 14:13:43 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: jsnow@redhat.com, =?utf-8?q?Alex_Benn=C3=A9e?= , Cleber Rosa , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Wainer dos Santos Moschetta , Beraldo Leal Subject: [RFC PATCH] tests/avocado: improve behaviour waiting for login prompts Date: Thu, 3 Nov 2022 14:13:30 +0000 Message-Id: <20221103141330.2705821-1-alex.bennee@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::333; envelope-from=alex.bennee@linaro.org; helo=mail-wm1-x333.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Qemu-devel" Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org This attempts to deal with the problem of login prompts not being guaranteed to be terminated with a newline. The solution to this is to peek at the incoming data looking to see if we see an up-coming match before we fall back to the old readline() logic. The reason to mostly rely on readline is because I am occasionally seeing the peek stalling despite data being there. This seems kinda hacky and gross so I'm open to alternative approaches and cleaner python code. Signed-off-by: Alex Bennée --- tests/avocado/avocado_qemu/__init__.py | 89 +++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/tests/avocado/avocado_qemu/__init__.py b/tests/avocado/avocado_qemu/__init__.py index 910f3ba1ea..d6ff68e171 100644 --- a/tests/avocado/avocado_qemu/__init__.py +++ b/tests/avocado/avocado_qemu/__init__.py @@ -131,6 +131,58 @@ def pick_default_qemu_bin(bin_prefix='qemu-system-', arch=None): return path return None +def _peek_ahead(console, min_match, success_message): + """ + peek ahead in the console stream keeping an eye out for the + success message. + + Returns some message to process or None, indicating the normal + readline should occur. + """ + console_logger = logging.getLogger('console') + peek_len = 0 + retries = 0 + + while True: + try: + old_peek_len = peek_len + peek_ahead = console.peek(min_match).decode() + peek_len = len(peek_ahead) + + # if we get stuck too long lets just fallback to readline + if peek_len <= old_peek_len: + retries += 1 + if retries > 10: + return None + + # if we see a newline in the peek we can let safely bail + # and let the normal readline() deal with it + if peek_ahead.endswith(('\n', '\r', '\r\n')): + return None + + # if we haven't seen enough for the whole message but the + # first part matches lets just loop again + if len(peek_ahead) < min_match and \ + success_message[:peek_len] in peek_ahead: + continue + + # if we see the whole success_message we are done, consume + # it and pass back so we can exit to the user + if success_message in peek_ahead: + return console.read(peek_len).decode() + + # of course if we've seen enough then this line probably + # doesn't contain what we are looking for, fallback + if peek_len > min_match: + return None + + except UnicodeDecodeError: + console_logger.log("error in decode of peek") + return None + + # we should never get here + return None + def _console_interaction(test, success_message, failure_message, send_string, keep_sending=False, vm=None): @@ -139,17 +191,52 @@ def _console_interaction(test, success_message, failure_message, vm = test.vm console = vm.console_socket.makefile(mode='rb', encoding='utf-8') console_logger = logging.getLogger('console') + + # Establish the minimum number of bytes we would need to + # potentially match against success_message + if success_message is not None: + min_match = len(success_message) + else: + min_match = 0 + + console_logger.debug("looking for %d:(%s), sending %s (always=%s)", + min_match, success_message, send_string, keep_sending) + while True: + msg = None + + # First send our string, optionally repeating the send next + # cycle. if send_string: vm.console_socket.sendall(send_string.encode()) if not keep_sending: send_string = None # send only once + + # If the console has no data to read we briefly + # sleep before continuing. + if not console.readable(): + time.sleep(0.1) + continue + try: - msg = console.readline().decode().strip() + + # First we shall peek ahead for a potential match to cover waiting + # for lines without any newlines. + if min_match > 0: + msg = _peek_ahead(console, min_match, success_message) + + # otherwise we block here for a full line + if not msg: + msg = console.readline().decode().strip() + except UnicodeDecodeError: + console_logger.debug("skipped unicode error") msg = None + + # if nothing came out we continue and try again if not msg: continue + console_logger.debug(msg) if success_message is None or success_message in msg: break