Skip to content

Commit

Permalink
move test scenarios to subdirectory and add optional --source_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Sep 28, 2024
1 parent 8009e44 commit f5641e0
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 14 deletions.
3 changes: 3 additions & 0 deletions resources/scenarios/ln_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,6 @@ def funded_lnnodes():

def main():
LNInit().main()

if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions resources/scenarios/miner_std.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,6 @@ def run_test(self):

def main():
MinerStd().main()

if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions resources/scenarios/reconnaissance.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,6 @@ def run_test(self):

def main():
Reconnaissance().main()

if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions resources/scenarios/signet_miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,6 @@ def get_args(parser):

def main():
SignetMinerScenario().main()

if __name__ == "__main__":
main()
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ def run_test(self):

def main():
Failure().main()

if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,6 @@ def assert_connection(self, connector, connectee_index, connection_type: Connect

def main():
ConnectDag().main()

if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ def run_test(self):

def main():
GetdataTest().main()

if __name__ == "__main__":
main()
4 changes: 4 additions & 0 deletions resources/scenarios/tx_flood.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ def run_test(self):

def main():
TXFlood().main()


if __name__ == "__main__":
main()
24 changes: 17 additions & 7 deletions src/warnet/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,15 @@ def get_active_network(namespace):

@click.command(context_settings={"ignore_unknown_options": True})
@click.argument("scenario_file", type=click.Path(exists=True, file_okay=True, dir_okay=False))
@click.option("--source_dir", type=click.Path(exists=True, file_okay=False, dir_okay=True), required=False)
@click.argument("additional_args", nargs=-1, type=click.UNPROCESSED)
def run(scenario_file: str, additional_args: tuple[str]):
def run(scenario_file: str, source_dir, additional_args: tuple[str]):
"""
Run a scenario from a file.
Pass `-- --help` to get individual scenario help
"""
scenario_path = Path(scenario_file).resolve()
scenario_dir = scenario_path.parent
scenario_dir = scenario_path.parent if not source_dir else Path(source_dir).resolve()
scenario_name = scenario_path.stem

if additional_args and ("--help" in additional_args or "-h" in additional_args):
Expand Down Expand Up @@ -203,15 +204,24 @@ def run(scenario_file: str, additional_args: tuple[str]):
def filter(path):
if any(needle in str(path) for needle in [".pyc", ".csv", ".DS_Store"]):
return False
return any(
needle in str(path) for needle in ["commander.py", "test_framework", scenario_name]
)

if any(needle in str(path) for needle in ["__init__.py", "commander.py", "test_framework", scenario_path.name]):
print(f"Including: {path}")
return True
return False

# In case the scenario file is not in the root of the archive directory,
# we need to specify its relative path as a submodule
# First get the path of the file relative to the source directory
relative_path = scenario_path.relative_to(scenario_dir)
# Remove the '.py' extension
relative_name = relative_path.with_suffix("")
# Replace path separators with dots and pray the user included __init__.py
module_name = ".".join(relative_name.parts)
# Compile python archive
zipapp.create_archive(
source=scenario_dir,
target=archive_buffer,
main=f"{scenario_name}:main",
main=f"{module_name}:main",
compressed=True,
filter=filter,
)
Expand Down
2 changes: 1 addition & 1 deletion src/warnet/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def copy_scenario_defaults(directory: Path):
directory,
SCENARIOS_DIR.name,
SCENARIOS_DIR,
["__pycache__", "testscenario_*.py"],
["__pycache__", "test_scenarios"],
)


Expand Down
4 changes: 2 additions & 2 deletions test/dag_connection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def setup_network(self):
self.wait_for_all_edges()

def run_connect_dag_scenario(self):
scenario_file = self.scen_dir / "testscenario_connect_dag.py"
scenario_file = self.scen_dir / "test_scenarios" / "connect_dag.py"
self.log.info(f"Running scenario from: {scenario_file}")
self.warnet(f"run {scenario_file}")
self.warnet(f"run {scenario_file} --source_dir={self.scen_dir}")
self.wait_for_all_scenarios()


Expand Down
8 changes: 4 additions & 4 deletions test/scenarios_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ def run_and_check_miner_scenario_from_file(self):
self.stop_scenario()

def run_and_check_scenario_from_file(self):
scenario_file = self.scen_dir / "testscenario_p2p_interface.py"
scenario_file = self.scen_dir / "test_scenarios" / "p2p_interface.py"
self.log.info(f"Running scenario from: {scenario_file}")
self.warnet(f"run {scenario_file}")
self.warnet(f"run {scenario_file} --source_dir={self.scen_dir}")
self.wait_for_predicate(self.check_scenario_clean_exit)

def check_regtest_recon(self):
Expand All @@ -95,9 +95,9 @@ def check_regtest_recon(self):
self.wait_for_predicate(self.check_scenario_clean_exit)

def check_active_count(self):
scenario_file = self.scen_dir / "testscenario_buggy_failure.py"
scenario_file = self.scen_dir / "test_scenarios" / "buggy_failure.py"
self.log.info(f"Running scenario from: {scenario_file}")
self.warnet(f"run {scenario_file}")
self.warnet(f"run {scenario_file} --source_dir={self.scen_dir}")

def two_pass_one_fail():
deployed = scenarios_deployed()
Expand Down

0 comments on commit f5641e0

Please sign in to comment.