From patchwork Tue Nov 8 09:23:01 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: 622640 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp2805841pvb; Tue, 8 Nov 2022 01:24:02 -0800 (PST) X-Google-Smtp-Source: AMsMyM4cBCwXZIFB9Kt2rt6kcRTRNvy5M/NYcJdAJNrR5yabFmbNEwcwn1PE44OqUGslRwm5rnME X-Received: by 2002:a05:620a:294f:b0:6cf:920e:4f3a with SMTP id n15-20020a05620a294f00b006cf920e4f3amr38453098qkp.66.1667899442563; Tue, 08 Nov 2022 01:24:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667899442; cv=none; d=google.com; s=arc-20160816; b=eDy9p8gE/zTLVcySHp6MP2lbcau4MIWplaQZ0myQPKzKPDJHynHmPYeuD4KL6pXcXK dEuGc3vxyCL5NKeQciEqjAJiT1K2MzJADspkMDFcdKxC2/mhFE+pl0IKicp9rG/lqXyk 1oQ+3ERA1rGwkw5fKMvwnyPmPYewAqMfaDFieWI2IDKg8JGn+PVe5OQAk+9z3z1wnEMh fqFkYqd/eIorakVw6w8Yz62N0HhbUdOYMUxRrxGAO6qvAOqzqGiuGOShkzxr9+NtOefW P9dJuwnOKzabp2jFJvDln591hMkNaXr4Nrqwxl7yGjNsM3IM2jR/LYQdcEVLYCE2qlcj wN8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=uigyf6qq9Am7cRVFYV/7zGs4tD3SV3rZRcR/12kbTNw=; b=ZxIap3QAhgujf7drkD+mm5Lb/hCsz1fFJmolnqt3LmEXTJUE31NC8F3sCCn4EjD+RE vgiTlZUBYPAtxHQITwN/GxoEO9mSTeH1L/X07aBuk/RrayTkiDPmSDuXquU5DMR6xVK+ XMWB65+OKJySwMFWDfdm3AZvBgRi1+J+8Ztl5bfyqWXrJL/OtJyIgK9MDmwB7ynKFAmp jmzN17TPk1iaMx6rUOfAYKYvNAtqMmTeMO0lGojet0GqOo7OyQZIJrRauAdp5TWdyM5e XrDPkl8StdyqdygZpCe/Fq0iLPdHcFRI6b88ZlZfloCsqBPMxTgAD6dtcwXyRh9Cpxig meHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=gMVXpVuw; 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 bs43-20020a05620a472b00b006fa2e7dc051si6283000qkb.482.2022.11.08.01.24.02 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Nov 2022 01:24:02 -0800 (PST) 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=gMVXpVuw; 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 1osKpF-0007EO-Nf; Tue, 08 Nov 2022 04:23:21 -0500 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 1osKp9-0007Cx-GL for qemu-devel@nongnu.org; Tue, 08 Nov 2022 04:23:16 -0500 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1osKp6-0005oA-9l for qemu-devel@nongnu.org; Tue, 08 Nov 2022 04:23:15 -0500 Received: by mail-wr1-x435.google.com with SMTP id l14so19990880wrw.2 for ; Tue, 08 Nov 2022 01:23:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uigyf6qq9Am7cRVFYV/7zGs4tD3SV3rZRcR/12kbTNw=; b=gMVXpVuwCKTklzwH67pRe0XXCetjVfuv/THCWKn8zhrIJXv6T3rrflI0NRVuy+dbFV ainMGfNgtL6qEnDWLiUdEf1eXS78Oc3JQpjd5QYklFsfmJ0sLj6UqtGpP/DhpGnJWUsf wSKjBASh56XrOpRGD/W3/LP/SOzMS5vTPTX9jedtN3RgDFglVY94G5Okx4jkdxkslDqD G5+zijTfy93EFleRpTwt57PY6CRYGy+7F8NNKQb8Ut4ZNLNNts/QU4bWA/Xl679NrG1f ni4aNMNlI1O43bRmRlfLZ5rqC3muUpRu+cjb2BdaU8odP2ZkAV2oOX8attLnOlbp2RwI Kspg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :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=ueEPZCWjSggFGqYOFg7wRMGkw8dliE8mthxBKn9oBWIWhRLdZzg9F4B9LOzWaZJcJC IlfcvtofpGurHEd84DODLK7pwrwLFauKRVfd3LnZxZZPOKUNsmrs245UScmOyc70xHAn ZjhbNl3+sAVeJ3cQjU7O7wmRhIi0qeXmHBChFdVN/IZR3r18SU29Hh9wY1jpPBsQhnIo qTvLqyoeWSz52dLulhdHlIVchAG5U1e/AaZakQk1TN0CPqsvE9vmVJ/OdzXG0hRYJoep fd/ji2Nu7QVCwF1G7yGmqbeG54QhYEhiTEVsqWbmwacAECFTnYTZ1Q2wwZ0ndb+1yW7H 7+iw== X-Gm-Message-State: ACrzQf1J34mYk/L4RcDdNRTbWFXYjbmxqWKr3VdrsXjx+ZR3B25PZLm2 3yUi2H4t/BG6M0qzly6qLL9+bQ== X-Received: by 2002:adf:e40e:0:b0:236:b896:8f55 with SMTP id g14-20020adfe40e000000b00236b8968f55mr32824547wrm.281.1667899390697; Tue, 08 Nov 2022 01:23:10 -0800 (PST) Received: from zen.linaroharston ([185.81.254.11]) by smtp.gmail.com with ESMTPSA id fc15-20020a05600c524f00b003cf57329221sm15459328wmb.14.2022.11.08.01.23.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 01:23:09 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 5434D1FFBA; Tue, 8 Nov 2022 09:23:08 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: fam@euphon.net, berrange@redhat.com, f4bug@amsat.org, aurelien@aurel32.net, pbonzini@redhat.com, stefanha@redhat.com, crosa@redhat.com, =?utf-8?q?Alex_Benn=C3=A9e?= , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Wainer dos Santos Moschetta , Beraldo Leal Subject: [PATCH v1 2/9] tests/avocado: improve behaviour waiting for login prompts Date: Tue, 8 Nov 2022 09:23:01 +0000 Message-Id: <20221108092308.1717426-3-alex.bennee@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221108092308.1717426-1-alex.bennee@linaro.org> References: <20221108092308.1717426-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::435; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x435.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: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: 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 Reviewed-by: Philippe Mathieu-Daudé --- 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