From mboxrd@z Thu Jan 1 00:00:00 1970 X-GM-THRID: 7216294651790098432 X-Received: by 2002:a17:907:a80d:b0:947:335f:5a10 with SMTP id vo13-20020a170907a80d00b00947335f5a10mr1483920ejc.12.1680174529091; Thu, 30 Mar 2023 04:08:49 -0700 (PDT) X-BeenThere: isar-users@googlegroups.com Received: by 2002:a05:6402:42cb:b0:4ad:73cb:b525 with SMTP id i11-20020a05640242cb00b004ad73cbb525ls1731364edc.3.-pod-prod-gmail; Thu, 30 Mar 2023 04:08:47 -0700 (PDT) X-Google-Smtp-Source: AKy350ZBW5jv/JsGD+fWjG4zOR8OCXAnSJZdjZK95gRDmo3tGR9TBGQ4B+KeA0JQbXG4G+uCBGds X-Received: by 2002:aa7:d806:0:b0:4ac:b69a:2f06 with SMTP id v6-20020aa7d806000000b004acb69a2f06mr22760655edq.0.1680174527740; Thu, 30 Mar 2023 04:08:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1680174527; cv=pass; d=google.com; s=arc-20160816; b=LUYKoL3JmXlQYZwip1wHBTJYQ2h9fbmRfnYFS0AeqO6zKHa+cBQyV8eByk1VjgF/iH UxB/Slp/7jqAjzA0xGpwDWejQqPj8eTsgCoeFG5i12DEU0JedKiiKwIVAWFymrsYxXul hgzVbjGqEN9V5236XphZWKnAhm4pH5FsoY6msP+BHjHTf+IMFGBGG5fJj98aLqP7rZjv V9oiJOmNzMmVtVV1RqZfFIUXBR+yl5AUt+l5/EwUGfLFKnCuU3Q0cmSLfCIq7WGzKnNK 4uuoB84pB1POUedmMxcjwIHtN47Anqn/lOf39+ZcuKr1yyL7KnqNBLEUnbLHheqR6EIk MVSA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=Cyz4pf25ub2ZI5LaoEn0HCNrbYRL1G5mREaDYfAtdEQ=; b=lBX1qJ74QLIh8f9RwYQINVuIeVVtbr6KdOihbWzYO97IuDFvuXRBr7uMUnabxdVvZm K9i3dVYszrN7GlpQPs/gs3horVmkjW+i29fRBHpz7eBnFmC1flNrkd5uwoo//FtaXAwb rdK6j2D4loe5jl/TCxCm2Ts8JiZaX2ncp+rGk5KRwb5yIvTHEzXKI8QyCUUmsZ87j1AT OGsL6nyqdmklzxfJc0fo3+AjcIJcL+VFyHHJmbC8DwTabQfpzII2qHqYQPBwFZ7s9D33 bnjWLELiPYt87JXpK1Fr1pxwjF6WtYIdTOjAlcVI53OvwuS77il2yJ8rV9Wy5HPLuHe1 3L/g== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@siemens.com header.s=selector2 header.b=wKdeMSmH; arc=pass (i=1 spf=pass spfdomain=siemens.com dmarc=pass fromdomain=siemens.com); spf=pass (google.com: domain of tobias.schaffner@siemens.com designates 2a01:111:f400:7e1a::617 as permitted sender) smtp.mailfrom=tobias.schaffner@siemens.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siemens.com Return-Path: Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05on20617.outbound.protection.outlook.com. [2a01:111:f400:7e1a::617]) by gmr-mx.google.com with ESMTPS id w7-20020a056402268700b005002c03a8absi1467577edd.0.2023.03.30.04.08.47 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Mar 2023 04:08:47 -0700 (PDT) Received-SPF: pass (google.com: domain of tobias.schaffner@siemens.com designates 2a01:111:f400:7e1a::617 as permitted sender) client-ip=2a01:111:f400:7e1a::617; Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@siemens.com header.s=selector2 header.b=wKdeMSmH; arc=pass (i=1 spf=pass spfdomain=siemens.com dmarc=pass fromdomain=siemens.com); spf=pass (google.com: domain of tobias.schaffner@siemens.com designates 2a01:111:f400:7e1a::617 as permitted sender) smtp.mailfrom=tobias.schaffner@siemens.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siemens.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OsyB88/Plnb/FXd7O6yBbkP8C2KVRJYD1ehczVcTiLU1TTCKsPD+suviI2UgPunmPlWYQOPbsV7RBCJA4TehZwmISi++wlBG00MUyIPf3YgbCyP38DDh/bXptrwUoiZTSTpKPjOg3I6++OxPVRa9OgakSkpjN7dgejgAO9JHZdpyfYNbRv8ZLB+ucUiB/rzvO9lmjgTZArt78DEsArBvbKpR02vC1+WcKiRu2fVPnZHVqkk5KaXSM6iyY2uDQVd7P1C8NioPyqDgz3I6BXqRcNbNFgI3qE3LI6KoX5XKpV+kL34+T1f+z0sgsgGvf+eLpuFkSlJ0ffDjWlB20t9zLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Cyz4pf25ub2ZI5LaoEn0HCNrbYRL1G5mREaDYfAtdEQ=; b=YYnFleh/GBBwIjmONbDym/KLcJZfXnvKnZt2LrhWNnGMZ1h4OVVfJT86mH4a0E6SiOglwVCLmburTZ85ihkpNXFrLRfURqlHpcGc1yUSoAsTBstIHf0aSoTdPDAZtGGMYpdGXtelmNfuk/6bjBPwk8RtsucWaxtP8TCsAlmZMXR29cMiuei3Zfef/0crQPH4hy4YBkjufhhZyPLv60wbmbHDyoUnPG78bQqGJY6CIse4UdUTsbmOESBxHpIsnkslCqHuuQfDJl4MA7OqGDgyphYhgnYh+nli9d5TaLgeJ3ICWRgyg0jvOBSmGedebHdeAapSMCn12jH0gg5isN50TA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 194.138.21.76) smtp.rcpttodomain=googlegroups.com smtp.mailfrom=siemens.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=siemens.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=siemens.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Cyz4pf25ub2ZI5LaoEn0HCNrbYRL1G5mREaDYfAtdEQ=; b=wKdeMSmHrIMkxsbLtxPVP7aCYQ7xaWdiTpgCSUcz030e0mqUivKaiQSUxxQzfeoQt6muJWsYktWhfjTKhryov/0+Z2ax7RBlQgBZzFp682ztAabK729WoP3btcsWCA197uchGTg1TK+zSzl4XQAPbCLOaQeS1jYauNLkNWxzO/W241aeOG8kkJnoG4xDc1bgHea3qHdo3QkA+3SX+x8ziOQntE5jVhzTGvJ+Ic1qj1YUCZTQXNWSmJXtApHCZofmJmu/TtrYP+1Kf7Op/NDcY1jJ+a05TU54HFev75L9S2sxJXxQ7RFznkZyqGsl2PcKothwz1IilkSz6AYQaKqmig== Received: from DB6PR0301CA0035.eurprd03.prod.outlook.com (2603:10a6:4:3e::45) by AS4PR10MB5524.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:20b:4ce::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.20; Thu, 30 Mar 2023 11:08:46 +0000 Received: from DB5EUR01FT029.eop-EUR01.prod.protection.outlook.com (2603:10a6:4:3e:cafe::9e) by DB6PR0301CA0035.outlook.office365.com (2603:10a6:4:3e::45) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.42 via Frontend Transport; Thu, 30 Mar 2023 11:08:46 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 194.138.21.76) smtp.mailfrom=siemens.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=siemens.com; Received-SPF: Pass (protection.outlook.com: domain of siemens.com designates 194.138.21.76 as permitted sender) receiver=protection.outlook.com; client-ip=194.138.21.76; helo=hybrid.siemens.com; pr=C Received: from hybrid.siemens.com (194.138.21.76) by DB5EUR01FT029.mail.protection.outlook.com (10.152.4.241) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6254.22 via Frontend Transport; Thu, 30 Mar 2023 11:08:46 +0000 Received: from DEMCHDC8WBA.ad011.siemens.net (139.25.226.105) by DEMCHDC8VSA.ad011.siemens.net (194.138.21.76) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Thu, 30 Mar 2023 13:08:44 +0200 Received: from L15-Gen2.fritz.box (139.25.0.85) by DEMCHDC8WBA.ad011.siemens.net (139.25.226.105) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Thu, 30 Mar 2023 13:08:44 +0200 From: "T. Schaffner" To: CC: , , "Tobias Schaffner" Subject: [PATCH 2/4] create a minimal python unittest infrastructure Date: Thu, 30 Mar 2023 13:08:02 +0200 Message-ID: <20230330110804.1016614-3-tobias.schaffner@siemens.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230330110804.1016614-1-tobias.schaffner@siemens.com> References: <20230330110804.1016614-1-tobias.schaffner@siemens.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain Return-Path: tobias.schaffner@siemens.com X-Originating-IP: [139.25.0.85] X-ClientProxiedBy: DEMCHDC8WAA.ad011.siemens.net (139.25.226.104) To DEMCHDC8WBA.ad011.siemens.net (139.25.226.105) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB5EUR01FT029:EE_|AS4PR10MB5524:EE_ X-MS-Office365-Filtering-Correlation-Id: 0ca39bdc-fb23-483e-b54d-08db310f21dc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zTjbsn3XQwgylQjrx5XlKO3N4WZa0QOH0SBvIg1I6ti/zWHxBLrmvUCZU3Qf8R3fpnRN+0ll51gt9Byuj0sZ610G8MqpmD2NY0tiaWILqhrMM+/8SDWLIFRK2A8aJG4PmwHlEND0kRbKbbunsXPQjj8AG3TM6/EuRoZVJsSZnRo5cCgj/16oPIRyAFG08dqifjkcTTRcE32e6ZUTm9LvTKMK8JoPCCLavLyQzqWdTuycQA19Xdq1UrpjQ9u++DfCwoaSB0iSJWTEWmWr0ycsVSci7XzLOxCqkEpW+Vj62BorszThE1chNwp3rHSAOC8KC4r1A1k3kjjcRpc6NAh9B1d32LdS+ecO8WA4jJ3f6giyyjnzPoA6fIzUhpdpmZ3wlRG3autEfbYf1US37GI73JHqdj50xZZJG03X/oWcPvshGK3mfM9GojfY2sBZ7xwoSoev8g7HgYT1r5t+q7HQB03YzS0Qooxc9HrB6BS64wMrBpE1xJ64RfHOQTI/bBh4B09wo1a+y5kskp5Xevc0uQfiTRxC2nQo3DqgirvjWAGppQ3mErRGP/QaBP8EVOtbZi9UNQJ3PfVPICF0o5uEM+vwNDeds0sQIriYZ7LfjPC4TNBifx1/Z93adlieCappxdCqn6rEgWFbYCdhVt55BvIDmeg1SC/vWn05BL/5s0RQNLoeyMG97tSs7e1sjXxegw6KjS5Cb+xSgz3vLrljC9jjpBBW4caq9UOY3LsCxyA9+x46oyhDy0RphaAX41753f2u4n1spAeKSKh2hIJZYueDQaUmlJ/YCOlveO7WaLI= X-Forefront-Antispam-Report: CIP:194.138.21.76;CTRY:DE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:hybrid.siemens.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(346002)(376002)(136003)(39860400002)(396003)(451199021)(36840700001)(40470700004)(46966006)(82960400001)(81166007)(82310400005)(82740400003)(40460700003)(356005)(70586007)(478600001)(86362001)(36756003)(8676002)(40480700001)(70206006)(34020700004)(36860700001)(1076003)(336012)(47076005)(16526019)(26005)(6666004)(956004)(107886003)(186003)(6916009)(54906003)(8936002)(2616005)(316002)(41300700001)(5660300002)(4326008)(2906002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: siemens.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Mar 2023 11:08:46.4658 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0ca39bdc-fb23-483e-b54d-08db310f21dc X-MS-Exchange-CrossTenant-Id: 38ae3bcd-9579-4fd4-adda-b42e1495d55a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=38ae3bcd-9579-4fd4-adda-b42e1495d55a;Ip=[194.138.21.76];Helo=[hybrid.siemens.com] X-MS-Exchange-CrossTenant-AuthSource: DB5EUR01FT029.eop-EUR01.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS4PR10MB5524 X-TUID: wog7KbUb49XI From: Tobias Schaffner Add some some infrastructure for python unittesting. The unittest_isar module exposes a function that uses the bb.parse module to import python functions that are defined in bitbake files. Signed-off-by: Tobias Schaffner --- testsuite/unittests/README.md | 28 +++++++++++++++++++++ testsuite/unittests/bitbake.py | 37 ++++++++++++++++++++++++++++ testsuite/unittests/rootfs.py | 45 ++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 testsuite/unittests/README.md create mode 100644 testsuite/unittests/bitbake.py create mode 100644 testsuite/unittests/rootfs.py diff --git a/testsuite/unittests/README.md b/testsuite/unittests/README.md new file mode 100644 index 00000000..75a3bb01 --- /dev/null +++ b/testsuite/unittests/README.md @@ -0,0 +1,28 @@ +# Isar Unittests + +The unittest python module adds some simple infrastructure that allows to +unittest python functions defined in bitbake files. + +## Running the tests + +You can run the tests using avocado with `avocado --show=app,test run testsuite/unittests/` +or by using the buildin module with `python3 -m unittest discover testsuite/unittests/` + +## Creating new tests + +See the [unittest documentation](https://docs.python.org/3/library/unittest.html) +on how to create a test module and name it test_*bitbake_module_name*.py + +Use the function `load_function(file_name: str, function_name: str) -> Callable` +in the bitbake module to load the function. + +Example: +```python +from bitbake import load_function + +my_function = load_function("meta/classes/my_module.bbclass", "my_function") +my_function(arg1, arg2) +``` + +Use the [unittest.mock](https://docs.python.org/3/library/unittest.mock.html) +library to mock the bb modules as needed. diff --git a/testsuite/unittests/bitbake.py b/testsuite/unittests/bitbake.py new file mode 100644 index 00000000..1e2f685a --- /dev/null +++ b/testsuite/unittests/bitbake.py @@ -0,0 +1,37 @@ +# This software is a part of ISAR. +# Copyright (C) Siemens AG, 2023 +# +# SPDX-License-Identifier: MIT + +import sys +import pathlib +from typing import Callable + +location = pathlib.Path(__file__).parent.resolve() +sys.path.insert(0, "{}/../../bitbake/lib".format(location)) + +from bb.parse import handle +from bb.data import init + +# Modules added for reimport from testfiles +from bb.data_smart import DataSmart + + +def load_function(file_name: str, function_name: str) -> Callable: + """Load a python function defined in a bitbake file. + + Args: + file_name (str): The path to the file e.g. `meta/classes/my_special.bbclass`. + function_name (str): The name of the python function without braces e.g. `my_special_function` + + Returns: + Callable: The loaded function. + """ + d = init() + parse = handle("{}/../../{}".format(location, file_name), d) + if function_name not in parse: + raise KeyError("Function {} does not exist in {}".format( + function_name, file_name)) + namespace = {} + exec(parse[function_name], namespace) + return namespace[function_name] diff --git a/testsuite/unittests/rootfs.py b/testsuite/unittests/rootfs.py new file mode 100644 index 00000000..6c511493 --- /dev/null +++ b/testsuite/unittests/rootfs.py @@ -0,0 +1,45 @@ +# This software is a part of ISAR. +# Copyright (C) Siemens AG, 2023 +# +# SPDX-License-Identifier: MIT + +import tempfile +import pathlib +import shutil +import atexit + +temp_dirs = [] + + +class TemporaryRootfs: + """ A temporary rootfs folder that will be removed after the testrun. """ + + def __init__(self): + self._rootfs_path = tempfile.mkdtemp() + temp_dirs.append(self._rootfs_path) + + def path(self) -> str: + return self._rootfs_path + + def create_file(self, path: str, content: str) -> None: + """ Create a file with the given content. + + Args: + path (str): The path to the file e.g. `/etc/hostname`. + content (str): The content of the file e.g. `my_special_host` + + Returns: + None + """ + pathlib.Path(self._rootfs_path + + path).parent.mkdir(parents=True, exist_ok=True) + with open(self._rootfs_path + path, 'w') as file: + file.write(content) + + +def cleanup(): + for temp_dir in temp_dirs: + shutil.rmtree(temp_dir) + + +atexit.register(cleanup) -- 2.34.1