From patchwork Thu Oct 13 10:35:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Juraj_Linke=C5=A1?= X-Patchwork-Id: 118129 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 2970CA00C2; Thu, 13 Oct 2022 12:35:44 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 6508642E89; Thu, 13 Oct 2022 12:35:27 +0200 (CEST) Received: from lb.pantheon.sk (lb.pantheon.sk [46.229.239.20]) by mails.dpdk.org (Postfix) with ESMTP id EFEAD42E6F for ; Thu, 13 Oct 2022 12:35:24 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by lb.pantheon.sk (Postfix) with ESMTP id 82C98165617; Thu, 13 Oct 2022 12:35:23 +0200 (CEST) X-Virus-Scanned: amavisd-new at siecit.sk Received: from lb.pantheon.sk ([127.0.0.1]) by localhost (lb.pantheon.sk [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id d8Ay5an1LdAk; Thu, 13 Oct 2022 12:35:21 +0200 (CEST) Received: from entguard.lab.pantheon.local (unknown [46.229.239.141]) by lb.pantheon.sk (Postfix) with ESMTP id 2ABEA165610; Thu, 13 Oct 2022 12:35:19 +0200 (CEST) From: =?utf-8?q?Juraj_Linke=C5=A1?= To: thomas@monjalon.net, Honnappa.Nagarahalli@arm.com, ohilyard@iol.unh.edu, lijuan.tu@intel.com, kda@semihalf.com, bruce.richardson@intel.com Cc: dev@dpdk.org, =?utf-8?q?Juraj_Linke=C5=A1?= Subject: [PATCH v6 03/10] dts: add config parser module Date: Thu, 13 Oct 2022 10:35:10 +0000 Message-Id: <20221013103517.3443997-4-juraj.linkes@pantheon.tech> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221013103517.3443997-1-juraj.linkes@pantheon.tech> References: <20221013103517.3443997-1-juraj.linkes@pantheon.tech> MIME-Version: 1.0 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org From: Owen Hilyard The configuration is split into two parts, one defining the parameters of the test run and the other defining the topology to be used. The format of the configuration is YAML. It is validated according to a json schema which also server as detailed documentation of the various configuration fields. This means that the complete set of allowed values are tied to the schema as a source of truth. This enables making changes to parts of DTS that interface with config files without a high risk of breaking someone's configuration. This configuration system uses immutable objects to represent the configuration, making IDE/LSP autocomplete work properly. There are two ways to specify the configuration file path, an environment variable or a command line argument, applied in that order. Signed-off-by: Owen Hilyard Signed-off-by: Juraj Linkeš --- dts/conf.yaml | 6 ++ dts/framework/config/__init__.py | 100 +++++++++++++++++++++ dts/framework/config/conf_yaml_schema.json | 65 ++++++++++++++ dts/framework/settings.py | 84 +++++++++++++++++ 4 files changed, 255 insertions(+) create mode 100644 dts/conf.yaml create mode 100644 dts/framework/config/__init__.py create mode 100644 dts/framework/config/conf_yaml_schema.json create mode 100644 dts/framework/settings.py diff --git a/dts/conf.yaml b/dts/conf.yaml new file mode 100644 index 0000000000..75947dc234 --- /dev/null +++ b/dts/conf.yaml @@ -0,0 +1,6 @@ +executions: + - system_under_test: "SUT 1" +nodes: + - name: "SUT 1" + hostname: sut1.change.me.localhost + user: root diff --git a/dts/framework/config/__init__.py b/dts/framework/config/__init__.py new file mode 100644 index 0000000000..47f7a8e3ff --- /dev/null +++ b/dts/framework/config/__init__.py @@ -0,0 +1,100 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2010-2021 Intel Corporation +# Copyright(c) 2022 University of New Hampshire +# + +""" +Generic port and topology nodes configuration file load function +""" + +import json +import os.path +import pathlib +from dataclasses import dataclass +from typing import Any, Optional + +import warlock +import yaml + +from framework.settings import SETTINGS + + +# Slots enables some optimizations, by pre-allocating space for the defined +# attributes in the underlying data structure. +# +# Frozen makes the object immutable. This enables further optimizations, +# and makes it thread safe should we every want to move in that direction. +@dataclass(slots=True, frozen=True) +class NodeConfiguration: + name: str + hostname: str + user: str + password: Optional[str] + + @staticmethod + def from_dict(d: dict) -> "NodeConfiguration": + return NodeConfiguration( + name=d["name"], + hostname=d["hostname"], + user=d["user"], + password=d.get("password"), + ) + + +@dataclass(slots=True, frozen=True) +class ExecutionConfiguration: + system_under_test: NodeConfiguration + + @staticmethod + def from_dict(d: dict, node_map: dict) -> "ExecutionConfiguration": + sut_name = d["system_under_test"] + assert sut_name in node_map, f"Unknown SUT {sut_name} in execution {d}" + + return ExecutionConfiguration( + system_under_test=node_map[sut_name], + ) + + +@dataclass(slots=True, frozen=True) +class Configuration: + executions: list[ExecutionConfiguration] + + @staticmethod + def from_dict(d: dict) -> "Configuration": + nodes: list[NodeConfiguration] = list( + map(NodeConfiguration.from_dict, d["nodes"]) + ) + assert len(nodes) > 0, "There must be a node to test" + + node_map = {node.name: node for node in nodes} + assert len(nodes) == len(node_map), "Duplicate node names are not allowed" + + executions: list[ExecutionConfiguration] = list( + map( + ExecutionConfiguration.from_dict, d["executions"], [node_map for _ in d] + ) + ) + + return Configuration(executions=executions) + + +def load_config() -> Configuration: + """ + Loads the configuration file and the configuration file schema, + validates the configuration file, and creates a configuration object. + """ + with open(SETTINGS.config_file_path, "r") as f: + config_data = yaml.safe_load(f) + + schema_path = os.path.join( + pathlib.Path(__file__).parent.resolve(), "conf_yaml_schema.json" + ) + + with open(schema_path, "r") as f: + schema = json.load(f) + config: dict[str, Any] = warlock.model_factory(schema, name="_Config")(config_data) + config_obj: Configuration = Configuration.from_dict(dict(config)) + return config_obj + + +CONFIGURATION = load_config() diff --git a/dts/framework/config/conf_yaml_schema.json b/dts/framework/config/conf_yaml_schema.json new file mode 100644 index 0000000000..6b8d6ccd05 --- /dev/null +++ b/dts/framework/config/conf_yaml_schema.json @@ -0,0 +1,65 @@ +{ + "$schema": "https://json-schema.org/draft-07/schema", + "title": "DTS Config Schema", + "definitions": { + "node_name": { + "type": "string", + "description": "A unique identifier for a node" + } + }, + "type": "object", + "properties": { + "nodes": { + "type": "array", + "items": { + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "A unique identifier for this node" + }, + "hostname": { + "type": "string", + "description": "A hostname from which the node running DTS can access this node. This can also be an IP address." + }, + "user": { + "type": "string", + "description": "The user to access this node with." + }, + "password": { + "type": "string", + "description": "The password to use on this node. Use only as a last resort. SSH keys are STRONGLY preferred." + } + }, + "additionalProperties": false, + "required": [ + "name", + "hostname", + "user" + ] + }, + "minimum": 1 + }, + "executions": { + "type": "array", + "items": { + "type": "object", + "properties": { + "system_under_test": { + "$ref": "#/definitions/node_name" + } + }, + "additionalProperties": false, + "required": [ + "system_under_test" + ] + }, + "minimum": 1 + } + }, + "required": [ + "executions", + "nodes" + ], + "additionalProperties": false +} diff --git a/dts/framework/settings.py b/dts/framework/settings.py new file mode 100644 index 0000000000..d1a955502b --- /dev/null +++ b/dts/framework/settings.py @@ -0,0 +1,84 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Copyright(c) 2010-2021 Intel Corporation +# Copyright(c) 2022 PANTHEON.tech s.r.o. +# Copyright(c) 2022 University of New Hampshire +# + +import argparse +import os +from dataclasses import dataclass +from typing import Any, Callable, Iterable, Optional, Sequence, TypeVar + +_T = TypeVar("_T") + + +def _env_arg(env_var: str) -> Any: + class _EnvironmentArgument(argparse.Action): + def __init__( + self, + option_strings: Sequence[str], + dest: str, + nargs: Optional[str | int] = None, + const: Optional[str] = None, + default: str = None, + type: Callable[[str], Optional[_T | argparse.FileType]] = None, + choices: Optional[Iterable[_T]] = None, + required: bool = True, + help: Optional[str] = None, + metavar: Optional[str | tuple[str, ...]] = None, + ) -> None: + env_var_value = os.environ.get(env_var) + default = env_var_value or default + super(_EnvironmentArgument, self).__init__( + option_strings, + dest, + nargs=nargs, + const=const, + default=default, + type=type, + choices=choices, + required=required, + help=help, + metavar=metavar, + ) + + def __call__( + self, + parser: argparse.ArgumentParser, + namespace: argparse.Namespace, + values: Any, + option_string: str = None, + ) -> None: + setattr(namespace, self.dest, values) + + return _EnvironmentArgument + + +@dataclass(slots=True, frozen=True) +class _Settings: + config_file_path: str + + +def _get_parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser(description="DPDK test framework.") + + parser.add_argument( + "--config-file", + action=_env_arg("DTS_CFG_FILE"), + default="conf.yaml", + required=False, + help="[DTS_CFG_FILE] configuration file that describes the test cases, SUTs " + "and targets.", + ) + + return parser + + +def _get_settings() -> _Settings: + parsed_args = _get_parser().parse_args() + return _Settings( + config_file_path=parsed_args.config_file, + ) + + +SETTINGS: _Settings = _get_settings()