[v1,6/8] dts: add config parser module
Checks
Commit Message
The module uses Python's configparser module, which supports an ini-like
format with sections.
The configuration is split into two parts, one defining the parameters
of the test run and the other defining the topology to be used.
Signed-off-by: Juraj Linkeš <juraj.linkes@pantheon.tech>
---
dts/conf/topology.cfg | 9 +++++
dts/execution.cfg | 2 +
dts/framework/config.py | 81 ++++++++++++++++++++++++++++++++++++++
dts/framework/exception.py | 13 ++++++
dts/framework/settings.py | 34 ++++++++++++++++
5 files changed, 139 insertions(+)
create mode 100644 dts/conf/topology.cfg
create mode 100644 dts/execution.cfg
create mode 100644 dts/framework/config.py
new file mode 100644
@@ -0,0 +1,9 @@
+#Topology Configuration
+#[SUT IP]
+# sut_ip: SUT ip address
+# sut_user: SUT username
+# sut_passwd: SUT password
+[SUT IP1]
+sut_ip=xxx.xxx.xxx.xxx
+sut_user=root
+sut_passwd=
new file mode 100644
@@ -0,0 +1,2 @@
+[Execution1]
+sut=<SUT IP Address>
new file mode 100644
@@ -0,0 +1,81 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2010-2021 Intel Corporation
+#
+
+"""
+Generic port and topology nodes configuration file load function
+"""
+import configparser # config parse module
+
+from .exception import ConfigParseException
+from .settings import CONFIG_ROOT_PATH
+
+TOPOCONF = "%s/topology.cfg" % CONFIG_ROOT_PATH
+
+
+class UserConf:
+ def __init__(self, config):
+ self.conf = configparser.SafeConfigParser()
+ load_files = self.conf.read(config)
+ if load_files == []:
+ self.conf = None
+ raise ConfigParseException(config)
+
+ def get_sections(self):
+ if self.conf is None:
+ return []
+
+ return self.conf.sections()
+
+ def load_section(self, section):
+ if self.conf is None:
+ return None
+
+ items = None
+ for conf_sect in self.conf.sections():
+ if conf_sect == section:
+ items = self.conf.items(section)
+
+ return items
+
+
+class TopologyConf(UserConf):
+ TOPO_DEFAULTS = {
+ "IP": "",
+ "user": "",
+ "pass": "",
+ }
+
+ def __init__(self, topo_conf=TOPOCONF):
+ self.config_file = topo_conf
+ self.nodes = []
+ try:
+ self.topo_conf = UserConf(self.config_file)
+ except ConfigParseException:
+ self.topo_conf = None
+ raise ConfigParseException
+
+ def load_topo_config(self):
+ sections = self.topo_conf.get_sections()
+ if not sections:
+ return self.nodes
+
+ for node_name in sections:
+ node = self.TOPO_DEFAULTS.copy()
+ node["section"] = node_name
+ node_conf = self.topo_conf.load_section(node_name)
+ if not node_conf:
+ continue
+
+ # convert file configuration to dts node configuration
+ for key, value in node_conf:
+ if key == "sut_ip":
+ node["IP"] = value
+ elif key == "sut_user":
+ node["user"] = value
+ elif key == "sut_passwd":
+ node["pass"] = value
+
+ self.nodes.append(node)
+ return self.nodes
+
@@ -46,3 +46,16 @@ def __init__(self, host):
def __str__(self):
return "SSH session with %s has been dead" % self.host
+
+
+class ConfigParseException(Exception):
+
+ """
+ Configuration file parse failure exception.
+ """
+
+ def __init__(self, conf_file):
+ self.config = conf_file
+
+ def __str__(self):
+ return "Failed to parse config file [%s]" % (self.config)
@@ -2,7 +2,41 @@
# Copyright(c) 2010-2021 Intel Corporation
#
+import os
+import re
+
"""
Default session timeout.
"""
TIMEOUT = 15
+
+"""
+DTS global environment variables
+"""
+DTS_ENV_PAT = r"DTS_*"
+DTS_CFG_FOLDER = "DTS_CFG_FOLDER"
+
+
+def load_global_setting(key):
+ """
+ Load DTS global setting
+ """
+ if re.match(DTS_ENV_PAT, key):
+ env_key = key
+ else:
+ env_key = "DTS_" + key
+
+ if env_key in list(os.environ.keys()):
+ return os.environ[env_key]
+ else:
+ return ""
+
+
+"""
+The root path of framework configs.
+"""
+dts_cfg_folder = load_global_setting(DTS_CFG_FOLDER)
+if dts_cfg_folder != "":
+ CONFIG_ROOT_PATH = dts_cfg_folder
+else:
+ CONFIG_ROOT_PATH = "./conf"