From patchwork Tue Sep 3 14:35:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 824892 Delivered-To: patch@linaro.org Received: by 2002:a5d:48c1:0:b0:367:895a:4699 with SMTP id p1csp2442950wrs; Tue, 3 Sep 2024 07:36:40 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVoZnHUgG+h1V82I1md2jcZMtWa3XC9YcbsVyRz373qDBp9nTGX7jEbv4PbVXmpCRJ3JPoBhg==@linaro.org X-Google-Smtp-Source: AGHT+IETUgCLhBK5oH9NU7Uks0FRO7QxXzf0zXVSvq4wJT85delwB116NChsYHEg9WdYcHYe4+SB X-Received: by 2002:a05:622a:1e8b:b0:456:80ea:d390 with SMTP id d75a77b69052e-45756a8f40dmr124981721cf.40.1725374200312; Tue, 03 Sep 2024 07:36:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1725374200; cv=none; d=google.com; s=arc-20160816; b=oZ2toZQ0ikfsKVG53I9TXTyxt5ENNKrrNmg67r1vLrvNSpnjgZk79ej+uB4ZVXWAIy VKjH7WOw1w41RbD6lebza/AgeZGpL9bcz0lguL64Br5LUs3HGmaJ+8zq2pmxOaV870dk sGputCSj2vudvKMDO9bqyS9E0UWWEJDJubD78fLNVNiPpSsZA9HJwN8g2iSHrd9/Hxaf X6dVFgQ6jpPUjR1iJdv4Gwk3d4tw6sOAJf9N/+oM5UCNFd3CXjf7E4H+Zr3iaHKwfC3f B1Jav+KiaUeOsQ/9h+zTFMe3sDXnCGjqg/HdVsNXVT103D7Y/QR5IQ3gpkKNI+8NLziS 4bcA== 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:message-id:date:subject:cc:to:from:dkim-signature; bh=vvTFKjkeH7vpHb4iuRT03tIFxEtrLKBKwsGS/P4sub4=; fh=Yyj8iSBPqWg+AKce12uRzyUAk25qCbcmqJzx9/sgKkE=; b=rtCO1M32hDmqAgJLyEMcMfCPXpy95tuMb6O08dX7Z9K/ksfHveh/tGfwtCGZwHhx36 Y7LGTZnkEqRz0QqSiVO6ACz87BiF54RP8GUhKOLsJCyMxuBESAXo5bSY5TorY4ypt/1A 3u54Cqh7Av/fZDhL/cf/d/hzc6+qEgZ5j0AelV7ad86B6PiTc84BAit6Kn5ItRlTAB6u r7iP7R6g6LDdpxMzVaifVzB0jxUjRNlm42R9M++OlNVLFUjb6JkCqp3hsNlW5qNFg0Tc jXZYk95/et7k8KdBfPAh086Xm6KuXhqvu+Iyqk47eucClLIfKL0O6w5RmNKWtiDz/ONc spzA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Pn6NqatG; 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; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id d75a77b69052e-45682c86898si130717041cf.143.2024.09.03.07.36.40 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 03 Sep 2024 07:36:40 -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=Pn6NqatG; 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; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1slUdU-0003a7-2B; Tue, 03 Sep 2024 10:36:00 -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 1slUdS-0003ZL-FO for qemu-devel@nongnu.org; Tue, 03 Sep 2024 10:35:58 -0400 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1slUdQ-00042a-R0 for qemu-devel@nongnu.org; Tue, 03 Sep 2024 10:35:58 -0400 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-42bb81e795bso44486235e9.1 for ; Tue, 03 Sep 2024 07:35:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1725374155; x=1725978955; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=vvTFKjkeH7vpHb4iuRT03tIFxEtrLKBKwsGS/P4sub4=; b=Pn6NqatGeOZZTm4+YAu3eq1yVCpI+5Nw70LN4oI6OZfFAcObEf6P0M1MBwRWSHtdx0 NV5fkHpe2NiHldTfYLfvCpXennsgGhhRmEeN7ifOpqI+7Fc74G6V9k05+qS2H6gM7keu wmauu3Nunp3ETnKDPTrJ5gtnvJdUFuWcQ93dTBoiWlP31MxJG6q5d0cOSnT7CJqLGfAT tpiaSkPlqK4ZtiMj+B5ozrv9ZKBlurKq+tt+uDIt7Hy7n9oJGt9X4N3l3vExmTJhHt4r H3py97BqPIQi18RGeDc1WIA6inBn1flc2B0YtOcISf97FEQ7/HTf8oktzj5kiXqgVcQh PvbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725374155; x=1725978955; 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=vvTFKjkeH7vpHb4iuRT03tIFxEtrLKBKwsGS/P4sub4=; b=o5Fv6RTGALmsGrenBdFdSJ606VgGj8q2y6i48EgLZiiykwq4iSOgZcXGU5r8cOYPtF jRQlgpwbjqUzbY8nZtVQ/mxTC25GdWwHgPPKd8/UCpelrzXDWhwdt3t6W1RkbkvNsymR 9iWvBwr6C6PFNdE+8EyBrPh9qyFag/tGGQ3TNQNSh5jbbA4fjUd6/lt2GaC+rUrCpcr5 x33UIvQ2aFrCobJQwiL0rF5zWhDdjH2Em43hIObNlX8qWQbqneXb5AVdYa3p8ZC3tOVH NvPhJzpXcZC3u+4edrPBOWNWn3KZLfKAdYRVyxxMO5lSt0c9aoxB/Ow4VS4PORzmLHcf hIWw== X-Forwarded-Encrypted: i=1; AJvYcCWUVBdQv1fJ/1vGv2pZDrOF2cuapDRZMKkZ8kV6kiL3KJw4+ArZ7beaCdkxB8ikGHLFY2bAcO+dL7pb@nongnu.org X-Gm-Message-State: AOJu0YwsZxZ6dR+OG1OSECb1ahUJssVoecvnWLxfZQN3KG+zctBnqb5f 7uDnntgan2U9BJWN0f9QTvUDq1HNaLeL0vJdhVQIp1A394LMOC/zF8An85vjQWs= X-Received: by 2002:adf:eace:0:b0:374:c324:eab5 with SMTP id ffacd0b85a97d-374c324eb64mr5766805f8f.41.1725374154662; Tue, 03 Sep 2024 07:35:54 -0700 (PDT) Received: from localhost.localdomain ([78.196.4.158]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42bb37f7849sm179802065e9.7.2024.09.03.07.35.53 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 03 Sep 2024 07:35:54 -0700 (PDT) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: Thomas Huth , qemu-devel@nongnu.org Cc: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= Subject: [PATCH] tests/functional/test_vnc: Reduce raciness in find_free_ports() Date: Tue, 3 Sep 2024 16:35:53 +0200 Message-ID: <20240903143553.16877-1-philmd@linaro.org> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32e; envelope-from=philmd@linaro.org; helo=mail-wm1-x32e.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, T_SCC_BODY_TEXT_LINE=-0.01 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 Pass the port range as argument. In order to reduce races when looking for free ports, use a per-target per-process base port (based on the target built-in hash). Signed-off-by: Philippe Mathieu-Daudé --- Based-on: <20240830133841.142644-33-thuth@redhat.com> --- tests/functional/test_vnc.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/functional/test_vnc.py b/tests/functional/test_vnc.py index b769d3b268..508db0709d 100755 --- a/tests/functional/test_vnc.py +++ b/tests/functional/test_vnc.py @@ -10,6 +10,7 @@ # This work is licensed under the terms of the GNU GPL, version 2 or # later. See the COPYING file in the top-level directory. +import os import socket from typing import List @@ -18,7 +19,6 @@ VNC_ADDR = '127.0.0.1' VNC_PORT_START = 32768 -VNC_PORT_END = VNC_PORT_START + 1024 def check_bind(port: int) -> bool: @@ -41,9 +41,10 @@ def check_connect(port: int) -> bool: return True -def find_free_ports(count: int) -> List[int]: +# warning, racy function +def find_free_ports(portrange, count: int) -> List[int]: result = [] - for port in range(VNC_PORT_START, VNC_PORT_END): + for port in portrange: if check_bind(port): result.append(port) if len(result) >= count: @@ -91,7 +92,10 @@ def test_change_password(self): password='new_password') def test_change_listen(self): - a, b, c = find_free_ports(3) + per_arch_port_base = abs((os.getpid() + hash(self.arch)) % (10 ** 4)) + port_start = VNC_PORT_START + per_arch_port_base + port_stop = port_start + 100 + a, b, c = find_free_ports(range(port_start, port_stop), 3) self.assertFalse(check_connect(a)) self.assertFalse(check_connect(b)) self.assertFalse(check_connect(c))