Skip to content

Commit

Permalink
Merge branch '80-cfg overwrite and missing interface' into 'main'
Browse files Browse the repository at this point in the history
Resolve "cfg overwrite and missing interface"

Closes #80

See merge request dependencies/oasis!83
  • Loading branch information
penglei0 committed Nov 5, 2024
2 parents 8bfab89 + 2b612f0 commit f59435d
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/containernet/containernet_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def cmd(self, command: str) -> str:
"""
return self.containernet_host.cmd(command)

def cmdPrint(self, command: str) -> str:
"""Execute a command on the host and print the output.
"""
return self.containernet_host.cmdPrint(command)

def name(self) -> str:
"""Get the name of the host.
"""
Expand Down
6 changes: 4 additions & 2 deletions src/core/network_mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ def build_networks(self, node_config: NodeConfig,
node_config.bind_port = False
route_strategy = RoutingFactory().create_routing(
route_string_to_enum[route])
self.networks.append(ContainerizedNetwork(
node_config, topology, route_strategy))
net = ContainerizedNetwork(
node_config, topology, route_strategy)
net.id = i
self.networks.append(net)
elif cur_net_num > net_num:
# stop the extra networks
for i in range(net_num, cur_net_num):
Expand Down
17 changes: 10 additions & 7 deletions src/core/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,7 @@ def execute_tests(self):
for i in range(self.net_num):
p = multiprocessing.Process(target=self._perform_test_in_process,
args=(networks[i],
test_name,
i, process_shared_dict))
test_name, process_shared_dict))
processes.append(p)
p.start()

Expand Down Expand Up @@ -450,9 +449,11 @@ def handle_test_results(self, top_index):
"Test %s results analysis not passed, and failure is ignored.", test_name)
# 5.2 move results(logs, diagrams) to "{cur_results_path}/{top_index}"
cur_results_path = f"{g_root_path}test_results/{test_name}/"
logging.info("cur_results_path %s", cur_results_path)
archive_dir = f"{cur_results_path}topology-{top_index}"
logging.info("cur_results_path %s, archive_dir %s",
cur_results_path, archive_dir)
if not os.path.exists(archive_dir):
logging.info("Create archive directory %s", archive_dir)
os.makedirs(archive_dir)
# move all files and folders to the archive directory except folder which start with "topology-*"
for root, dirs, files in os.walk(cur_results_path):
Expand All @@ -463,11 +464,15 @@ def handle_test_results(self, top_index):
if dir_name.startswith("topology-"):
continue
os.system(f"mv {root}/{dir_name} {archive_dir}")
logging.info("Move %s to %s", dir_name, archive_dir)
for file_name in files:
os.system(f"mv {root}/{file_name} {archive_dir}")
logging.info("Move %s to %s", file_name, archive_dir)
# 5.3 save top_description
with open(f"{archive_dir}/topology_description.txt", 'w', encoding='utf-8') as f:
f.write(f"{self.top_description}")
logging.debug("Save topology description to %s",
f"{archive_dir}/topology_description.txt")
return True

def cleanup(self):
Expand All @@ -492,13 +497,11 @@ def _load_protocols(self):
return False
return True

def _perform_test_in_process(self, network, test_name, id, result_dict):
def _perform_test_in_process(self, network, test_name, result_dict):
"""Execute the test in a separate process,
then store the results in the shared dictionary.
Args:
id (int): The id of the process.
"""
id = network.get_id()
logging.info(
"########## Oasis process %d Performing the test for %s", id, test_name)
network.perform_test()
Expand Down
6 changes: 6 additions & 0 deletions src/interfaces/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

class INetwork(ABC):
def __init__(self):
self.id = 0
self.test_suites = []
self.proto_suites = []
self.test_results = {}
Expand All @@ -20,6 +21,9 @@ def __init__(self):
def is_accessible(self):
return self.is_accessible_flag

def get_id(self):
return self.id

@abstractmethod
def start(self):
pass
Expand Down Expand Up @@ -76,6 +80,8 @@ def perform_test(self):
# Combination of protocol and test
for proto in self.proto_suites:
# start the protocol
logging.info("Starting protocol %s on network %s",
proto.get_config().name, self.get_id())
if proto.start(self) is False:
logging.error("Protocol %s failed to start",
proto.get_config().name)
Expand Down
21 changes: 14 additions & 7 deletions src/protosuites/bats/bats_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def pre_run(self, network: INetwork):
# Init the license file for bats, otherwise it will not run.
hosts = network.get_hosts()
if hosts is None:
logging.error("No host found in the network")
return False
net_id = network.get_id()
host_num = len(hosts)
# prepare the bats protocol config files
hosts_ip_range = network.get_host_ip_range()
Expand All @@ -43,17 +45,19 @@ def pre_run(self, network: INetwork):
cfg_template_path = os.path.join(
self.config.config_base_path, self.config.config_file)
else:
logging.error("Config base path or config file is not set.")
logging.debug(
f"########################## BATSProtocol Source path: %s", self.source_path)
logging.error(
"%s Config base path or config file is not set.", net_id)
# configurations are separated by network
logging.info(
f"########################## BATSProtocol Source path: %s, %s", self.source_path, net_id)
generate_cfg_files(host_num, hosts_ip_range,
self.virtual_ip_prefix, self.source_path, cfg_template_path)
self.virtual_ip_prefix, f'{self.source_path}/{net_id}', cfg_template_path)
# generate some error log if the license file is not correct
self._verify_license()
for i in range(host_num):
hosts[i].cmd(f'iptables -F -t nat')
self._init_tun(hosts[i])
self._init_config(hosts[i])
self._init_config(hosts[i], net_id)
logging.info(
f"############### Oasis install bats protocol config files on "
"%s ###############",
Expand Down Expand Up @@ -88,6 +92,9 @@ def run(self, network: INetwork):
res = hosts[host_idx].cmd(f'ls /tmp | grep "{running_flag}"')
if res.find(running_flag) == -1:
unready_idx.append(host_idx)
else:
logging.info("Bats protocol is running on %s",
hosts[host_idx].name())
host_range = unready_idx

if len(host_range) > 0:
Expand Down Expand Up @@ -140,7 +147,7 @@ def _verify_license(self) -> bool:
return False
return True

def _init_config(self, host: IHost):
def _init_config(self, host: IHost, id: int):
# {host.name()} = h0, h1, h2, ... the last character is the index of the host
host_idx = int(host.name()[-1])
host.cmd(
Expand All @@ -150,7 +157,7 @@ def _init_config(self, host: IHost):
host.cmd(
f'mkdir -p /etc/bats-protocol')
host.cmd(
f'cp {self.source_path}/h{host_idx}.ini /etc/bats-protocol/bats-protocol-settings.ini')
f'cp {self.source_path}/{id}/h{host_idx}.ini /etc/bats-protocol/bats-protocol-settings.ini')
return True

def _get_ip_from_host(self, host: IHost, dev: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion src/testsuites/test_scp.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _run_test(self, network: INetwork, proto_info: IProtoInfo):
for file in self.scp_files:
scp_cmd += f' {file}'
scp_cmd += f' root@{receiver_ip}:/tmp/'
hosts[i].cmdPrint(
hosts[i].cmd(
f'script -c \'{scp_cmd}\' | tee {self.result.record} ')
return True
# Run ping test from client to server
Expand Down
3 changes: 3 additions & 0 deletions src/tools/cfg_generator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import argparse
import logging
from mininet.util import ipStr, netParse
Expand Down Expand Up @@ -189,6 +190,8 @@ def generate_cfg_files(num_nodes, node_ip_range="10.0.0.0/8",
virtual_ip_prefix="1.0.0.",
output_dir="/tmp",
config_file_template=None):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
generator = ConfigGenerator(
node_ip_range, output_dir, config_file_template)
generator.generate_cfg(num_nodes, virtual_ip_prefix)
Expand Down

0 comments on commit f59435d

Please sign in to comment.