From nobody Fri Jan 13 21:25:29 2023 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4NtvbF4xJmz2ql5T; Fri, 13 Jan 2023 21:25:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4NtvbF4JzJz42fP; Fri, 13 Jan 2023 21:25:29 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673645129; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6sctq7sXYhKxZowQ+WE1qQPLC9P1Ujhimh+m2VQUlQI=; b=nZq5PoiDRaGzAunqskPALOJaUntqO1o42avqDofRfE0L1ryMBKiScmQj60tvbCRU5+gg61 k4rHhKddnEYXgxl3iZpubWXc+Kit27WClN5mTA5CzAotbMx9S7/PK+mW5sderLOiqRTr79 UsTfJlvf5X/Pi979nMN/nzsCYiWxxcvdeir4QUUfPaPYEEZp4AsNOAMumtrILOkyaI+j5r IyeYSQUksdT+5vhnyeGVpzfvdTtCZ9uGa4b3MVZIBANC41OF1IaA3G5NWoSvmZp87jE5tS EM1pBzO/LeWDmDcW6jQ9cyWmb1I5UsaLN/StO+BNvwANXg3axa/Det7VQYtfXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1673645129; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6sctq7sXYhKxZowQ+WE1qQPLC9P1Ujhimh+m2VQUlQI=; b=LjRsJKUuYvcfAyny1dRJUEza69JxXVs6k4U9c7Y8/S3cECU9PYv7404aagAI8UYAaMnIKN k1pHHEL/YY9axJk5u8rXPLhIUkNSjB5DfZD1V4VJX+cWb/v6S8cU1N4+gLvYjF/YVespJK GyAbPoSBQiQdiDxNDKk4nWUGYip2wUQ69FXHdzr93n55o3Y3ZiezCWzEk1Ti5EeyagvlEO TFdNQvdWYdNRhkPFvfzUMJQ91KXBXejh06VY6mp8IrBbhV+153kyx8zZ0RoIBn6uP+26oM PhoWk3dwu5T6e6j/RfWOxBEVBG3z4fVgrd5TITGycSJv4rMjqxo5/1S5l/fgvQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1673645129; a=rsa-sha256; cv=none; b=TtSN94+cym+fw+vmF/uZOUiWU00a68PIw9zVRVoUq215H1TrUDQv9pFOWKMnWRUSajxCmM 4sgyyIPLnG6wRHY2B71QBlVmX4g9J2MrluDSryj1WfEVRv3XeXQqHaLsFaMw/C76HNOipX 32mCZGs8CAJBo4gIXR5K2V0mbaFmsheUlcjYd7gEcXG9LZu4Pw8aTLYU7BLrp7y1dTEt8Y T9IZX3taBcu66h7BF0+7JSLy/5Ua9rCRbQaxLnk4qUMtsfSFs6EyRqAjsYjBBw6ENGkxyj bdHRRloT6CX+2BkA8TOoL2f1G67S0zatPtRl20zFs4Gznxp9J4Mi5P5wUUSn6Q== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4NtvbF3DYczMrZ; Fri, 13 Jan 2023 21:25:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 30DLPTQi041917; Fri, 13 Jan 2023 21:25:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30DLPTjk041916; Fri, 13 Jan 2023 21:25:29 GMT (envelope-from git) Date: Fri, 13 Jan 2023 21:25:29 GMT Message-Id: <202301132125.30DLPTjk041916@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: cdd12f62eb32 - stable/13 - testing: pass ATF vars to pytest via env instead of arguments. List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: melifaro X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: cdd12f62eb32a3d6f72d5980af538870ef54637e Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=cdd12f62eb32a3d6f72d5980af538870ef54637e commit cdd12f62eb32a3d6f72d5980af538870ef54637e Author: Alexander V. Chernikov AuthorDate: 2022-06-28 10:49:41 +0000 Commit: Alexander V. Chernikov CommitDate: 2023-01-13 21:24:10 +0000 testing: pass ATF vars to pytest via env instead of arguments. This change is a continuation of 9c42645a1e4d workaround. Apparently pytest argument parser is not happy when parsing values with spaces or just more than one --atf-var argument. Switch wrapper to send these kv pairs as env variables. Specifically, use _ATF_VAR_key=value format to distinguish from the other vars. Add the `atf_vars` fixture returning all passed kv pairs as a dict. Reviewed by: lwhsu Differential Revision: https://reviews.freebsd.org/D35625 MFC after: 2 weeks (cherry picked from commit 513ce835b55831d343185e03a51efa2901405ac8) --- .../atf/atf-pytest-wrapper/atf_pytest_wrapper.cpp | 29 ++++++++++++++++------ tests/atf_python/atf_pytest.py | 6 +++++ tests/conftest.py | 7 +++++- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/libexec/atf/atf-pytest-wrapper/atf_pytest_wrapper.cpp b/libexec/atf/atf-pytest-wrapper/atf_pytest_wrapper.cpp index bc7eec3b851d..6baa85999070 100644 --- a/libexec/atf/atf-pytest-wrapper/atf_pytest_wrapper.cpp +++ b/libexec/atf/atf-pytest-wrapper/atf_pytest_wrapper.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -10,6 +11,7 @@ class Handler { const std::string kPytestName = "pytest"; const std::string kCleanupSuffix = ":cleanup"; const std::string kPythonPathEnv = "PYTHONPATH"; + const std::string kAtfVar = "_ATF_VAR_"; public: // Test listing requested bool flag_list = false; @@ -28,7 +30,7 @@ class Handler { // Name of the test to run (provided by ATF) std::string test_name; // kv pairs (provided by ATF) - std::vector kv_list; + std::map kv_map; // our binary name std::string binary_name; @@ -102,7 +104,14 @@ class Handler { dst_file = std::string(optarg); break; case 'v': - kv_list.emplace_back(std::string(optarg)); + { + std::string kv = std::string(optarg); + size_t splitter = kv.find("="); + if (splitter == std::string::npos) { + Usage("Unknown variable: " + kv, true); + } + kv_map[kv.substr(0, splitter)] = kv.substr(splitter + 1); + } break; default: Usage("Unknown option -" + std::string(1, static_cast(c)), true); @@ -147,16 +156,12 @@ class Handler { if (!dst_file.empty()) { args.push_back("--atf-file=" + dst_file); } - for (auto &pair: kv_list) { - args.push_back("--atf-var"); - args.push_back(pair); - } // Create nodeid from the test path &name args.push_back(script_path + "::" + test_name); return args; } - void SetEnv() { + void SetPythonPath() { if (!python_path.empty()) { char *env_path = getenv(kPythonPathEnv.c_str()); if (env_path != nullptr) { @@ -166,6 +171,16 @@ class Handler { } } + void SetEnv() { + SetPythonPath(); + + // Pass ATF kv pairs as env variables to avoid dealing with + // pytest parser + for (auto [k, v]: kv_map) { + setenv((kAtfVar + k).c_str(), v.c_str(), 1); + } + } + int Run(std::string binary, std::vector args) { if (flag_debug) { PrintVector("OUT", args); diff --git a/tests/atf_python/atf_pytest.py b/tests/atf_python/atf_pytest.py index 89c0e3a515b9..f72122fb740e 100644 --- a/tests/atf_python/atf_pytest.py +++ b/tests/atf_python/atf_pytest.py @@ -6,6 +6,7 @@ from typing import NamedTuple from typing import Tuple import pytest +import os class ATFCleanupItem(pytest.Item): @@ -216,3 +217,8 @@ class ATFHandler(object): line = "{}: {}".format(test.state, test.reason) with open(path, mode="w") as f: print(line, file=f) + + @staticmethod + def get_atf_vars() -> Dict[str, str]: + px = "_ATF_VAR_" + return {k[len(px):]: v for k, v in os.environ.items() if k.startswith(px)} diff --git a/tests/conftest.py b/tests/conftest.py index 193d2adfb5e0..65c8bf5f0d01 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ import pytest from atf_python.atf_pytest import ATFHandler +from typing import Dict PLUGIN_ENABLED = False @@ -17,7 +18,6 @@ def pytest_addoption(parser): """Add file output""" # Add meta-values group = parser.getgroup("general", "Running and selection options") - group.addoption("--atf-var", dest="atf_vars", action="append", default=[]) group.addoption( "--atf-source-dir", type=str, @@ -46,6 +46,11 @@ def pytest_addoption(parser): ) +@pytest.fixture(autouse=True, scope="session") +def atf_vars() -> Dict[str, str]: + return ATFHandler.get_atf_vars() + + @pytest.mark.trylast def pytest_configure(config): if config.option.help: