Skip to content

Commit

Permalink
chore: simplfied load_config
Browse files Browse the repository at this point in the history
  • Loading branch information
penglei0 committed Jul 31, 2024
1 parent e3f9ab7 commit 47d5f7d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/config/predefined.node_config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node:
node_config:
- name: n_hop
node_img: gitlab.app.n-hop.com:5005/n-hop/bats-protocol-framework/bats_protocol:dev
node_vols:
Expand Down
14 changes: 7 additions & 7 deletions src/containernet/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ class TopologyConfig:
json_description: Optional[str] = field(default=None)


supported_config_keys = ["node", "topology"]
supported_config_keys = ["node_config", "topology"]


class INodeConfig(ABC):
class IConfig(ABC):
@staticmethod
def is_supported_config_key(config_key: str):
return config_key in supported_config_keys

@staticmethod
def load_config_reference(yaml_config_file: str,
config_name: str, config_key: str):
if not INodeConfig.is_supported_config_key(config_key):
if not IConfig.is_supported_config_key(config_key):
logging.error(
f"load_config_reference: key %s is not supported.",
config_key)
Expand Down Expand Up @@ -123,7 +123,7 @@ def load_config_reference(yaml_config_file: str,
f"load_config_reference: %s is not defined in %s",
config_name, yaml_config_file)
return None
if config_key == "node":
if config_key == "node_config":
return NodeConfig(**loaded_config)
if config_key == "topology":
return TopologyConfig(**loaded_config)
Expand All @@ -133,7 +133,7 @@ def load_config_reference(yaml_config_file: str,
def load_yaml_config(yaml_description: str, config_key: str):
# load it directly from the yaml_description or
# load it from another yaml file.
if not INodeConfig.is_supported_config_key(config_key):
if not IConfig.is_supported_config_key(config_key):
logging.error(
f"load_yaml_config: key %s is not supported.",
config_key)
Expand All @@ -142,13 +142,13 @@ def load_yaml_config(yaml_description: str, config_key: str):
is_load_from_file = ["config_file", "config_name"]
if all(key in yaml_description for key in is_load_from_file):
# load from the yaml file `config_file`
config_data = INodeConfig.load_config_reference(
config_data = IConfig.load_config_reference(
yaml_description['config_file'],
yaml_description['config_name'], config_key)
else:
# load directly from the yaml_description
logging.info('load_yaml_config: %s', yaml_description)
if config_key == "node":
if config_key == "node_config":
config_data = NodeConfig(**yaml_description)
if config_key == "topology":
config_data = TopologyConfig(**yaml_description)
Expand Down
35 changes: 21 additions & 14 deletions src/run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from containernet.linear_topology import LinearTopology
from containernet.network import Network
from containernet.config import (
INodeConfig, NodeConfig, TopologyConfig)
IConfig, NodeConfig, TopologyConfig, supported_config_keys)
from containernet.test_suites.test import (TestType, TestConfig)
from containernet.test_suites.test_iperf import IperfTest
from containernet.test_suites.test_ping import PingTest
Expand Down Expand Up @@ -53,19 +53,26 @@ def load_test(test_yaml_file: str):


def load_config(test_case_yaml) -> Tuple[NodeConfig, TopologyConfig]:
local_net_top_yaml = test_case_yaml['topology']
local_node_conf_yaml = test_case_yaml['node_config']
logging.info(f"Test: top_yaml %s, node_conf_yaml %s",
local_net_top_yaml, local_node_conf_yaml)
if local_net_top_yaml is None or local_node_conf_yaml is None:
logging.error("Error: top_yaml or node_conf_yaml is None.")
return None, None
node_config = INodeConfig.load_yaml_config(local_node_conf_yaml, 'node')
top_config = INodeConfig.load_yaml_config(local_net_top_yaml, 'topology')
if top_config is None or node_config is None:
logging.error("Error: topology/node configuration is None.")
return None, None
return node_config, top_config
configs = []
for key in supported_config_keys:
if key not in test_case_yaml:
logging.error(
f"Error: missing key %s in the test case yaml.",
key)
return None, None
local_yaml = test_case_yaml[key]
logging.info(f"Test: local_yaml %s",
local_yaml)
if local_yaml is None:
logging.error(f"Error: content of %s is None.", key)
return None, None
loaded_conf = IConfig.load_yaml_config(
local_yaml, key)
if loaded_conf is None:
logging.error("Error: loaded_conf of %s is None.", key)
return None, None
configs.append(loaded_conf)
return configs[0], configs[1]


def build_topology(top_config: TopologyConfig):
Expand Down

0 comments on commit 47d5f7d

Please sign in to comment.