From 9ede224de74c124ce115341459657dca7b91c2eb Mon Sep 17 00:00:00 2001 From: Peng LEI Date: Sat, 7 Dec 2024 22:15:33 +0800 Subject: [PATCH] test: added unittest --- .github/workflows/unittest.yaml | 23 +++++++++++ src/containernet/containernet.py | 23 ++++++----- src/core/config.py | 42 ++++++++++++-------- src/core/runner.py | 20 ++++++---- src/run_test.py | 25 ++++++------ src/tools/{test_util.py => util_unittest.py} | 0 6 files changed, 88 insertions(+), 45 deletions(-) create mode 100644 .github/workflows/unittest.yaml rename src/tools/{test_util.py => util_unittest.py} (100%) diff --git a/.github/workflows/unittest.yaml b/.github/workflows/unittest.yaml new file mode 100644 index 0000000..637ef6a --- /dev/null +++ b/.github/workflows/unittest.yaml @@ -0,0 +1,23 @@ +name: Unittest + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8"] + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pylint + - name: Run unittest + run: | + python3 -m unittest discover -s src -p "*_unittest.py" diff --git a/src/containernet/containernet.py b/src/containernet/containernet.py index 80dc3e5..0c63582 100644 --- a/src/containernet/containernet.py +++ b/src/containernet/containernet.py @@ -15,18 +15,23 @@ def load_nested_config(nested_config_file: str, """ if nested_config_file == "" or containernet_name == "": return NestedConfig(image="") - containernet_list = [] logging.info( f"process yaml file: %s", {nested_config_file}) - with open(nested_config_file, 'r', encoding='utf-8') as stream: - try: + try: + with open(nested_config_file, 'r', encoding='utf-8') as stream: nested_config = yaml.safe_load(stream) - logging.info( - f"loaded nested_config: %s", nested_config) - containernet_list = nested_config["containernet"] - except yaml.YAMLError as exc: - logging.error(exc) - return NestedConfig(image="") + except FileNotFoundError: + logging.error( + "YAML file '%s' not found.", nested_config_file) + return NestedConfig(image="") + except yaml.YAMLError as exc: + logging.error("Error parsing YAML file: %s", exc) + return NestedConfig(image="") + if not nested_config or 'containernet' not in nested_config: + logging.error("No containernet found in the YAML file.") + return NestedConfig(image="") + logging.info(f"loaded nested_config: %s", nested_config) + containernet_list = nested_config["containernet"] containernet_names = containernet_list.keys() logging.info( f"loaded containernet: %s", containernet_list) diff --git a/src/core/config.py b/src/core/config.py index 38c0331..cf35d9e 100644 --- a/src/core/config.py +++ b/src/core/config.py @@ -9,8 +9,8 @@ import os import yaml -from core.topology import (ITopology, TopologyConfig) -from core.linear_topology import LinearTopology +from .topology import (ITopology, TopologyConfig) +from .linear_topology import LinearTopology @dataclass @@ -62,13 +62,17 @@ def load_config_reference(config_base_path: str, yaml_config_file: str, f"load_config_reference: file %s does not exist.", full_yaml_config_file) return None - loaded_yaml_config = [] - with open(full_yaml_config_file, 'r', encoding='utf-8') as stream: - try: + try: + with open(full_yaml_config_file, 'r', encoding='utf-8') as stream: loaded_yaml_config = yaml.safe_load(stream) - except yaml.YAMLError as exc: - logging.error(exc) - return None + except FileNotFoundError: + logging.error( + "YAML file '%s' not found.", full_yaml_config_file) + return None + except yaml.YAMLError as exc: + logging.error("Error parsing YAML file: %s", exc) + return None + # check key 'config_key' in the yaml content if loaded_yaml_config is None or config_key not in loaded_yaml_config: logging.error( @@ -131,7 +135,7 @@ def yaml(self): return self.test_yaml def is_active(self): - if self.test_yaml is None: + if not self.test_yaml: return False if "if" in self.test_yaml and not self.test_yaml["if"]: return False @@ -171,15 +175,19 @@ def load_all_tests(test_yaml_file: str, test_name: str = "all") -> List[Test]: logging.info( "########################## Oasis Loading Tests " "##########################") - # List of active cases. - test_cases = None - with open(test_yaml_file, 'r', encoding='utf-8') as stream: - try: + try: + with open(test_yaml_file, 'r', encoding='utf-8') as stream: yaml_content = yaml.safe_load(stream) - test_cases = yaml_content["tests"] - except yaml.YAMLError as exc: - logging.error(exc) - return [] + except FileNotFoundError: + logging.error("Test YAML file '%s' not found.", test_yaml_file) + return [] + except yaml.YAMLError as exc: + logging.error("Error parsing YAML file: %s", exc) + return [] + if not yaml_content or 'tests' not in yaml_content: + logging.error("No tests found in the YAML file.") + return [] + test_cases = yaml_content["tests"] # ------------------------------------------------ if test_cases is None: logging.error("No test cases are loaded from %s", test_yaml_file) diff --git a/src/core/runner.py b/src/core/runner.py index f4d55a4..128e370 100644 --- a/src/core/runner.py +++ b/src/core/runner.py @@ -144,14 +144,20 @@ def load_predefined_protocols(config_base_path): """ Load predefined protocols from the yaml file. """ - predefined_protocols = None - with open(f'{config_base_path}/predefined.protocols.yaml', 'r', encoding='utf-8') as stream: - try: + try: + with open(f'{config_base_path}/predefined.protocols.yaml', 'r', encoding='utf-8') as stream: yaml_content = yaml.safe_load(stream) - predefined_protocols = yaml_content['protocols'] - except yaml.YAMLError as exc: - logging.error(exc) - return None + except FileNotFoundError: + logging.error( + "YAML file '%s'/predefined.protocols.yaml not found.", config_base_path) + return None + except yaml.YAMLError as exc: + logging.error("Error parsing YAML file: %s", exc) + return None + if not yaml_content or 'protocols' not in yaml_content: + logging.error("No protocols found in the YAML file.") + return None + predefined_protocols = yaml_content['protocols'] predefined_proto_conf_dict = {} for protocol in predefined_protocols: if 'protocols' not in protocol: diff --git a/src/run_test.py b/src/run_test.py index ac17cc3..8aed969 100755 --- a/src/run_test.py +++ b/src/run_test.py @@ -17,20 +17,21 @@ def containernet_node_config(config_base_path, file_path) -> NodeConfig: """Load node related configuration from the yaml file. """ - node_config_yaml = None - with open(file_path, 'r', encoding='utf-8') as stream: - try: + try: + with open(file_path, 'r', encoding='utf-8') as stream: yaml_content = yaml.safe_load(stream) - if yaml_content['containernet'] is None: - logging.error("Error: no containernet node config.") - return NodeConfig(name="", img="") - node_config_yaml = yaml_content['containernet']["node_config"] - except yaml.YAMLError as exc: - logging.error(exc) - return NodeConfig(name="", img="") - if node_config_yaml is None: - logging.error("Error: no containernet node config.") + except FileNotFoundError: + logging.error( + "YAML file '%s' not found.", file_path) + return NodeConfig(name="", img="") + except yaml.YAMLError as exc: + logging.error("Error parsing YAML file: %s", exc) + return NodeConfig(name="", img="") + + if not yaml_content or 'containernet' not in yaml_content: + logging.error("No containernet node config found in the YAML file.") return NodeConfig(name="", img="") + node_config_yaml = yaml_content['containernet']["node_config"] loaded_conf = IConfig.load_yaml_config(config_base_path, node_config_yaml, 'node_config') if isinstance(loaded_conf, NodeConfig): diff --git a/src/tools/test_util.py b/src/tools/util_unittest.py similarity index 100% rename from src/tools/test_util.py rename to src/tools/util_unittest.py