Skip to content

Commit

Permalink
test: added unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
penglei0 committed Dec 7, 2024
1 parent b09d77d commit 9ede224
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 45 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/unittest.yaml
Original file line number Diff line number Diff line change
@@ -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"
23 changes: 14 additions & 9 deletions src/containernet/containernet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 25 additions & 17 deletions src/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 13 additions & 7 deletions src/core/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 13 additions & 12 deletions src/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
File renamed without changes.

0 comments on commit 9ede224

Please sign in to comment.