From mboxrd@z Thu Jan 1 00:00:00 1970 X-GM-THRID: 7231085468566159360 X-Received: by 2002:ac2:4553:0:b0:4e0:822f:9500 with SMTP id j19-20020ac24553000000b004e0822f9500mr438146lfm.12.1683618287473; Tue, 09 May 2023 00:44:47 -0700 (PDT) X-BeenThere: isar-users@googlegroups.com Received: by 2002:a05:651c:c87:b0:2ac:812a:6a80 with SMTP id bz7-20020a05651c0c8700b002ac812a6a80ls294618ljb.0.-pod-prod-08-eu; Tue, 09 May 2023 00:44:46 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6YRIGW0kA0KOYDl4rnW5zt42PB6REdiE+u6lhGZYWsDfPT/WNWm4pYMLI2fQ91Toj2bsQB X-Received: by 2002:a2e:9985:0:b0:2ac:98be:fa55 with SMTP id w5-20020a2e9985000000b002ac98befa55mr570889lji.27.1683618286052; Tue, 09 May 2023 00:44:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1683618286; cv=pass; d=google.com; s=arc-20160816; b=KpfZdnjBkCuvw942AYnSnXHVdpGYQd9adEVQdwcVnDduUYgvJnDWhmCNq4Oxp6be75 WLngPQWsa7s40WzO9FhsnepKmA1doNR5osk1V2H+oBvpkTTfJDl0Th+QFjw2dumU2MWT wr79sLurY1TI6z4LfhgSwqgtDVx44mAb52KIFfKu0N8HsjXrr4XiG+PNDNgCAHJf2Pvf h5jNEf1BALaVr6jPSSvrKu37z8uiDa0yQS67DFSRs3N8k/F2LcnYkHyeDa8vLhVaLVTc 1gKU05r8l2H19aHtqIv4hUKwgc2QwHRoHkdOjZRHpeGDgw1TRUpA5xjbXAx27FgErqoC JD+Q== 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=DF45QIdXR00WGXQ+FMKNtYrJoybkUr2VeP9dHQu6dXzrlCBt0G20GTHwyswDGoZd4D IGe4/BujPxkiQAt81mLoPeyyCCAcVRhBhJ+iCLmgqUW2GmBwTLKa0wXOV4iNqERbOUD7 GBDczpGWWRPNce8ljVFZEPAoFUx1XAwUHwBPmQn0ZbiUQaAJdrj6kJEKYrMrCH+JOVYr fbFdmJ+Uso0Zt8UH7J06qcnLTQ/QnIbK2003O6mbqbIa4928JWVwrSdV1XKY6UcVzWne OCmGJeVGsD5TbpVY57t2B8KFuMrsviVyUUFKg8Ks+4UVhfbYqMaShMl+LuKJh5Nwe25p auwA== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@siemens.com header.s=selector2 header.b=ywyxNiat; 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:fe13::61f as permitted sender) smtp.mailfrom=tobias.schaffner@siemens.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=siemens.com Return-Path: Received: from EUR02-AM0-obe.outbound.protection.outlook.com (mail-am0eur02on2061f.outbound.protection.outlook.com. [2a01:111:f400:fe13::61f]) by gmr-mx.google.com with ESMTPS id bz16-20020a05651c0c9000b002a8b2891ba7si575826ljb.1.2023.05.09.00.44.45 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 09 May 2023 00:44:45 -0700 (PDT) Received-SPF: pass (google.com: domain of tobias.schaffner@siemens.com designates 2a01:111:f400:fe13::61f as permitted sender) client-ip=2a01:111:f400:fe13::61f; Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@siemens.com header.s=selector2 header.b=ywyxNiat; 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:fe13::61f as permitted sender) smtp.mailfrom=tobias.schaffner@siemens.com; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=siemens.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kS/WOOeY4nVkynGJHxl+Vv7djnEuA2+ormRf4GGJY9nXRt2575SSUqnWNJHwXHN9O6HF8muPBdgotVupn2hVhJ3ICOVuk0H7sxLl9T7VPppQ0c2iQaa/KxGHdnK70YjzmUVbszfCzaBIJk5yPmGldyUI8EzsJwrx87WsGhTWiSPphdloU7ujz5TZhTkKvzAcOq19QTVmbplDJYyjKVf/JUawXPfIknc2204HIgtskslfRKEM75BHxjrxBZ+rNvZeCTBzwJ3c8VAdJFWKojcjTQx1F1a36hDXl6KaFDZa5Q9n9r/1GkLtnT0rVRjy6p+hH/mjxiilUrUhDrq0l9U8yg== 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=NGlRSlxwE65l83zUKSWbEPhvxO4q4qqzci8yHFB9Xm8bU8YIsZxBlNb3NfYazyzkU39vzmNCSb872YEcOOSeI8l14HJODsHDv+/K+7E0f8Hp6jw7k34DD5TKI11LpWYaJxKQBrgOwliKQg7W6/0eT+scpyvg9M1RoPG0gVZEw3awOm5VqqyS32m3XG24nPo2s6txSjabp3Kypq8m37glHgmJD1sR1gkTnWMaNN4F0wgtEKlPJWQR4ZVspSmma6jdZj9yOW1KLs1XV4/oVNPJLjAj0VMbSkFoG2V75tIPzsTiMXkO0AYIGf/D067w5z1DDoUCcwrRJJlOKmdaCy1ehw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 194.138.21.75) smtp.rcpttodomain=googlegroups.com smtp.mailfrom=siemens.com; dmarc=pass (p=reject sp=reject 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=ywyxNiatB4h3Bp51ce1AdYTc5dvTl/bxw/JoFhxZu8NNGPhm4wcoRbWjBYY3U/O1S4OXn/AFQFQ+KN+hTSStD2lF7wYC0Erjwjssyz3jlFqhB72CP8f2i+b5lLVcNfvB4n0TUXBwoPZV/00taQCOHj4TnJz1tODwStlr+NWVfRKOtnnowtNs6WJRzPJ7tg8ApYOtBRd5SaavrAzE7PiFBPsVype5yrng06XmBgFl7FmJGLxqDsvwHQv+9aSCMkd8GlkvUHrvrrfemwHpbNf/6uStFfaf9rsy4MLo0MBuQul9vSDZcrlNsQSiv0XXfMVtO2M9U8X0urznOpPKs4XQDQ== Received: from GV3P280CA0011.SWEP280.PROD.OUTLOOK.COM (2603:10a6:150:b::7) by DU0PR10MB5703.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:10:310::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.33; Tue, 9 May 2023 07:44:44 +0000 Received: from HE1EUR01FT053.eop-EUR01.prod.protection.outlook.com (2603:10a6:150:b:cafe::a2) by GV3P280CA0011.outlook.office365.com (2603:10a6:150:b::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.33 via Frontend Transport; Tue, 9 May 2023 07:44:43 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 194.138.21.75) 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.75 as permitted sender) receiver=protection.outlook.com; client-ip=194.138.21.75; helo=hybrid.siemens.com; pr=C Received: from hybrid.siemens.com (194.138.21.75) by HE1EUR01FT053.mail.protection.outlook.com (10.152.1.73) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6387.18 via Frontend Transport; Tue, 9 May 2023 07:44:43 +0000 Received: from DEMCHDC8WAA.ad011.siemens.net (139.25.226.104) by DEMCHDC8VRA.ad011.siemens.net (194.138.21.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Tue, 9 May 2023 09:44:43 +0200 Received: from L15-Gen2.fritz.box (139.22.36.202) by DEMCHDC8WAA.ad011.siemens.net (139.25.226.104) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Tue, 9 May 2023 09:44:41 +0200 From: "T. Schaffner" To: CC: , , "Tobias Schaffner" Subject: [PATCH v2 2/4] create a minimal python unittest infrastructure Date: Tue, 9 May 2023 09:44:10 +0200 Message-ID: <20230509074412.86392-3-tobias.schaffner@siemens.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230509074412.86392-1-tobias.schaffner@siemens.com> References: <20230509074412.86392-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.22.36.202] X-ClientProxiedBy: DEMCHDC8WBA.ad011.siemens.net (139.25.226.105) To DEMCHDC8WAA.ad011.siemens.net (139.25.226.104) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: HE1EUR01FT053:EE_|DU0PR10MB5703:EE_ X-MS-Office365-Filtering-Correlation-Id: 37dc7ef2-7260-457a-77c2-08db50614117 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 5aLG3VTwnrdvFFfuGNQwzyuLEiindCFeHI0++6YU2kcYpPfUnUNK8cCBjdgMhJPAFSUPLb7sbEbVRBEy/wZbV6BI3R/A9tA1+78yNXDiWtQLFch6HNsk4kSkv6tVhvnq+7XtiVo51ss0VrRArSlaAegGJCuO+PbmT+qOxt4nWQiLCax/+dj0z/5uVSOlkn0RXa5Gj1Ca4nhiUbbLWStRtrzVqZDaQE697pu3f9LIUd/rPlBbKxXFdBIn8m6ZMqI8djiHAGdiFJerOTyA7hD4JEnufr46BXMBzjAI1huP6KHzfVdQv0boqdAZ3ZKhFPYZo0wIJnD3Bgx6QPkmEprLBxKcJivnA9hvoTmxnTkM1dkichIpWXwoZU0PN6dxC/qVITSNe3OjswnC9l7lJG3QBGl4U/hWURH+nNeyFj3gBWIU40uWHPDfA8IBBXY5uoAVmSJ8iKehhaSecAnIO82WeGNk/OYE25DydUyVJ9XxHKxWoZPUW+dAjKYc0KnELj61RmgYlTnBzen92ogW3tQ6uSnsLgcq/67T+oh77G5s5tBRKn0W6xsBK0f3dDp44Edxd568xaSR7u3ECYjqoa3Y3eaVizXZVJeMq27nwdmtwLBAEd0JpsRiGehbSXWvgHe9IDY5aLOPW+CKsc4vzXRBJwwudZ2GnySOvzI47Ah6wAVDGQn3Y2sNzoKBUwyrvjnDZMiAm+ULiQnl7gdFbQtBEAYiC3q9XkEDOVljN1GZe+u1zzqEnfYzp28Z5vlHqg0uMqpm2O0gf7gRcCcikYymdA== X-Forefront-Antispam-Report: CIP:194.138.21.75;CTRY:DE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:hybrid.siemens.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(376002)(396003)(136003)(346002)(451199021)(40470700004)(46966006)(36840700001)(36860700001)(336012)(47076005)(316002)(70586007)(4326008)(6916009)(478600001)(70206006)(16526019)(956004)(6666004)(2616005)(107886003)(1076003)(26005)(54906003)(186003)(86362001)(40480700001)(2906002)(40460700003)(36756003)(82740400003)(82960400001)(41300700001)(82310400005)(81166007)(356005)(5660300002)(8676002)(8936002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: siemens.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 May 2023 07:44:43.6135 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 37dc7ef2-7260-457a-77c2-08db50614117 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.75];Helo=[hybrid.siemens.com] X-MS-Exchange-CrossTenant-AuthSource: HE1EUR01FT053.eop-EUR01.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU0PR10MB5703 X-TUID: G57ED4MQhs2F 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