Skip to content

Commit

Permalink
refactor: defined Test
Browse files Browse the repository at this point in the history
  • Loading branch information
penglei0 committed Dec 7, 2024
1 parent 567729b commit b09d77d
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 142 deletions.
2 changes: 1 addition & 1 deletion src/containernet/containernet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import yaml
from tools.util import is_same_path
from var.global_var import g_root_path
from .config import (NestedConfig)
from core.config import (NestedConfig)


def load_nested_config(nested_config_file: str,
Expand Down
4 changes: 2 additions & 2 deletions src/containernet/containernet_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import os
from mininet.net import Containernet # type: ignore
from mininet.util import ipStr, netParse
from containernet.topology import (ITopology, MatrixType)
from core.config import (NodeConfig)
from core.topology import (ITopology, MatrixType)
from containernet.containernet_host import ContainernetHostAdapter
from interfaces.network import INetwork
from interfaces.routing import IRoutingStrategy
from .config import (NodeConfig)


def subnets(base_ip, parent_ip):
Expand Down
126 changes: 86 additions & 40 deletions src/containernet/config.py → src/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,12 @@
from abc import ABC
from dataclasses import dataclass, field
from typing import Optional, List, Dict, Any
from enum import IntEnum
import logging
import os
import yaml


class MatrixType(IntEnum):
# Adjacency matrix to describe the network topology
ADJACENCY_MATRIX = 0
# Bandwidth matrix to describe the network bandwidth link-by-link
BANDW_MATRIX = 1
# Loss matrix to describe the network loss link-by-link
LOSS_MATRIX = 2
# Latency matrix to describe the network latency link-by-link
LATENCY_MATRIX = 3
# Jitter matrix to describe the network jitter link-by-link
JITTER_MATRIX = 4


class TopologyType(IntEnum):
linear = 0 # Linear chain topology
star = 1 # Star topology
tree = 2 # Complete Binary Tree
butterfly = 3 # Butterfly topology
mesh = 5 # Random Mesh topology
from core.topology import (ITopology, TopologyConfig)
from core.linear_topology import LinearTopology


@dataclass
Expand Down Expand Up @@ -58,25 +39,6 @@ class NestedConfig:
mounts: Optional[List[str]] = field(default=None)


@dataclass
class Parameter:
name: str
init_value: List[int]


@dataclass
class TopologyConfig:
"""Configuration for the network topology.
"""
name: str
nodes: int
topology_type: TopologyType
# @array_description: the array description of the topology
array_description: Optional[List[Parameter]] = field(default=None)
# @json_description: the json description of the topology
json_description: Optional[str] = field(default=None)


supported_config_keys = ["topology", "node_config"]


Expand Down Expand Up @@ -155,3 +117,87 @@ def load_yaml_config(config_base_path: str, yaml_description: Dict[str, Any], co
if config_key == "topology":
return TopologyConfig(**yaml_description)
return None


class Test:
"""Class to hold the test configuration(yaml) for one cases
"""

def __init__(self, test_yaml: Dict[str, Any], name: str):
self.name = name
self.test_yaml = test_yaml

def yaml(self):
return self.test_yaml

def is_active(self):
if self.test_yaml is None:
return False
if "if" in self.test_yaml and not self.test_yaml["if"]:
return False
return True

def load_topology(self, config_base_path) -> Optional[ITopology]:
"""Load network related configuration from the yaml file.
"""
if 'topology' not in self.test_yaml:
logging.error("Error: missing key topology in the test case yaml.")
return None
local_yaml = self.test_yaml['topology']
logging.info(f"Test: local_yaml %s",
local_yaml)
if local_yaml is None:
logging.error("Error: content of topology is None.")
return None
loaded_conf = IConfig.load_yaml_config(config_base_path,
local_yaml,
'topology')
if loaded_conf is None:
logging.error("Error: loaded_conf of topology is None.")
return None
if not isinstance(loaded_conf, TopologyConfig):
logging.error("Error: loaded_conf of topology is None.")
return None
if loaded_conf.topology_type == "linear":
return LinearTopology(loaded_conf)
logging.error("Error: unsupported topology type.")
return None


def load_all_tests(test_yaml_file: str, test_name: str = "all") -> List[Test]:
"""
Load the test case configuration from a yaml file.
"""
logging.info(
"########################## Oasis Loading Tests "
"##########################")
# List of active cases.
test_cases = None
with open(test_yaml_file, 'r', encoding='utf-8') as stream:
try:
yaml_content = yaml.safe_load(stream)
test_cases = yaml_content["tests"]
except yaml.YAMLError as exc:
logging.error(exc)
return []
# ------------------------------------------------
if test_cases is None:
logging.error("No test cases are loaded from %s", test_yaml_file)
return []
active_test_list = []
if test_name in ("all", "All", "ALL"):
test_name = ""
for name in test_cases.keys():
if test_name not in ("", name):
logging.debug("Oasis skips the test case %s", name)
continue
test_cases[name]["name"] = name
test = Test(test_cases[name], name)
if test.is_active():
active_test_list.append(test)
logging.info(f"case %s is enabled!", name)
else:
logging.info(f"case %s is disabled!", name)
if len(active_test_list) == 0:
logging.info(f"No active test case in %s", test_yaml_file)
return active_test_list
File renamed without changes.
4 changes: 2 additions & 2 deletions src/core/network_mgr.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging

from containernet.config import NodeConfig
from core.config import NodeConfig
from core.topology import ITopology
from containernet.containernet_network import ContainerizedNetwork
from containernet.topology import ITopology
from routing.routing_factory import RoutingFactory, route_string_to_enum
from interfaces.network_mgr import (INetworkManager, NetworkType)

Expand Down
2 changes: 1 addition & 1 deletion src/core/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from interfaces.network_mgr import INetworkManager
from interfaces.network import INetwork
from containernet.topology import ITopology
from core.topology import ITopology
from testsuites.test import (TestType, TestConfig)
from testsuites.test_iperf import IperfTest
from testsuites.test_ping import PingTest
Expand Down
2 changes: 1 addition & 1 deletion src/core/testbed_mgr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from containernet.topology import ITopology
from core.topology import ITopology
from interfaces.network_mgr import (INetworkManager, NetworkType)
from testbed.config import HostConfig

Expand Down
44 changes: 43 additions & 1 deletion src/containernet/topology.py → src/core/topology.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from abc import ABC, abstractmethod
from enum import IntEnum

from dataclasses import dataclass, field
from typing import Optional, List
import logging
import os
import json
from .config import (TopologyConfig, MatrixType)


class LinkAttr(IntEnum):
Expand All @@ -14,6 +16,19 @@ class LinkAttr(IntEnum):
link_bandwidth_backward = 4


class MatrixType(IntEnum):
# Adjacency matrix to describe the network topology
ADJACENCY_MATRIX = 0
# Bandwidth matrix to describe the network bandwidth link-by-link
BANDW_MATRIX = 1
# Loss matrix to describe the network loss link-by-link
LOSS_MATRIX = 2
# Latency matrix to describe the network latency link-by-link
LATENCY_MATRIX = 3
# Jitter matrix to describe the network jitter link-by-link
JITTER_MATRIX = 4


# mapping MatrixType to the link attribute except for the adjacency matrix
MatType2LinkAttr = {
MatrixType.LOSS_MATRIX: LinkAttr.link_loss,
Expand All @@ -23,6 +38,33 @@ class LinkAttr(IntEnum):
}


class TopologyType(IntEnum):
linear = 0 # Linear chain topology
star = 1 # Star topology
tree = 2 # Complete Binary Tree
butterfly = 3 # Butterfly topology
mesh = 5 # Random Mesh topology


@dataclass
class Parameter:
name: str
init_value: List[int]


@dataclass
class TopologyConfig:
"""Configuration for the network topology.
"""
name: str
nodes: int
topology_type: TopologyType
# @array_description: the array description of the topology
array_description: Optional[List[Parameter]] = field(default=None)
# @json_description: the json description of the topology
json_description: Optional[str] = field(default=None)


class ITopology(ABC):
def __init__(self, top: TopologyConfig, init_all_mat: bool = True):
self.all_mats = {}
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import copy
from abc import ABC, abstractmethod
from typing import List
from containernet.topology import (ITopology)
from core.topology import (ITopology)
from protosuites.proto import IProtoSuite
from interfaces.routing import IRoutingStrategy
from interfaces.host import IHost
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/network_mgr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABC, abstractmethod
from enum import IntEnum
from containernet.topology import ITopology
from core.topology import ITopology

# alphabet table
alphabet = ['h', 'i', 'j', 'k', 'l', 'm',
Expand Down
Loading

0 comments on commit b09d77d

Please sign in to comment.