77
88from warnet .control import stop_scenario
99from warnet .process import run_command
10- from warnet .status import _get_active_scenarios as scenarios_active
10+ from warnet .status import _get_deployed_scenarios as scenarios_deployed
1111
1212
1313class ScenariosTest (TestBase ):
@@ -18,7 +18,10 @@ def __init__(self):
1818 def run_test (self ):
1919 try :
2020 self .setup_network ()
21- self .test_scenarios ()
21+ self .run_and_check_miner_scenario_from_file ()
22+ self .run_and_check_scenario_from_file ()
23+ self .check_regtest_recon ()
24+ self .check_active_count ()
2225 finally :
2326 self .cleanup ()
2427
@@ -28,49 +31,36 @@ def setup_network(self):
2831 self .wait_for_all_tanks_status (target = "running" )
2932 self .wait_for_all_edges ()
3033
31- def test_scenarios (self ):
32- self .run_and_check_miner_scenario_from_file ()
33- self .run_and_check_scenario_from_file ()
34- self .check_regtest_recon ()
35-
3634 def scenario_running (self , scenario_name : str ):
3735 """Check that we are only running a single scenario of the correct name"""
38- active = scenarios_active ()
39- assert len (active ) == 1
40- return scenario_name in active [0 ]["name" ]
41-
42- def run_and_check_scenario_from_file (self ):
43- scenario_file = "test/data/scenario_p2p_interface.py"
44- self .log .info (f"Running scenario from: { scenario_file } " )
45- self .warnet (f"run { scenario_file } " )
46- self .wait_for_predicate (self .check_scenario_clean_exit )
47-
48- def run_and_check_miner_scenario_from_file (self ):
49- scenario_file = "resources/scenarios/miner_std.py"
50- self .log .info (f"Running scenario from file: { scenario_file } " )
51- self .warnet (f"run { scenario_file } --allnodes --interval=1" )
52- start = int (self .warnet ("bitcoin rpc tank-0000 getblockcount" ))
53- self .wait_for_predicate (lambda : self .scenario_running ("commander-minerstd" ))
54- self .wait_for_predicate (lambda : self .check_blocks (2 , start = start ))
55- self .stop_scenario ()
36+ deployed = scenarios_deployed ()
37+ assert len (deployed ) == 1
38+ return scenario_name in deployed [0 ]["name" ]
5639
57- def check_regtest_recon (self ):
58- scenario_file = "resources/scenarios/reconnaissance.py"
59- self .log .info (f"Running scenario from file: { scenario_file } " )
60- self .warnet (f"run { scenario_file } " )
61- self .wait_for_predicate (self .check_scenario_clean_exit )
40+ def check_scenario_stopped (self ):
41+ running = scenarios_deployed ()
42+ self .log .debug (f"Checking if scenario stopped. Running scenarios: { len (running )} " )
43+ return len (running ) == 0
6244
6345 def check_scenario_clean_exit (self ):
64- active = scenarios_active ()
65- return all (scenario ["status" ] == "succeeded" for scenario in active )
46+ deployed = scenarios_deployed ()
47+ return all (scenario ["status" ] == "succeeded" for scenario in deployed )
48+
49+ def stop_scenario (self ):
50+ self .log .info ("Stopping running scenario" )
51+ running = scenarios_deployed ()
52+ assert len (running ) == 1 , f"Expected one running scenario, got { len (running )} "
53+ assert running [0 ]["status" ] == "running" , "Scenario should be running"
54+ stop_scenario (running [0 ]["name" ])
55+ self .wait_for_predicate (self .check_scenario_stopped )
6656
6757 def check_blocks (self , target_blocks , start : int = 0 ):
6858 count = int (self .warnet ("bitcoin rpc tank-0000 getblockcount" ))
6959 self .log .debug (f"Current block count: { count } , target: { start + target_blocks } " )
7060
7161 try :
72- active = scenarios_active ()
73- commander = active [0 ]["commander" ]
62+ deployed = scenarios_deployed ()
63+ commander = deployed [0 ]["commander" ]
7464 command = f"kubectl logs { commander } "
7565 print ("\n commander output:" )
7666 print (run_command (command ))
@@ -80,18 +70,43 @@ def check_blocks(self, target_blocks, start: int = 0):
8070
8171 return count >= start + target_blocks
8272
83- def stop_scenario (self ):
84- self .log .info ("Stopping running scenario" )
85- running = scenarios_active ()
86- assert len (running ) == 1 , f"Expected one running scenario, got { len (running )} "
87- assert running [0 ]["status" ] == "running" , "Scenario should be running"
88- stop_scenario (running [0 ]["name" ])
89- self .wait_for_predicate (self .check_scenario_stopped )
73+ def run_and_check_miner_scenario_from_file (self ):
74+ scenario_file = "resources/scenarios/miner_std.py"
75+ self .log .info (f"Running scenario from file: { scenario_file } " )
76+ self .warnet (f"run { scenario_file } --allnodes --interval=1" )
77+ start = int (self .warnet ("bitcoin rpc tank-0000 getblockcount" ))
78+ self .wait_for_predicate (lambda : self .scenario_running ("commander-minerstd" ))
79+ self .wait_for_predicate (lambda : self .check_blocks (2 , start = start ))
80+ table = self .warnet ("status" )
81+ assert "Active Scenarios: 1" in table
82+ self .stop_scenario ()
9083
91- def check_scenario_stopped (self ):
92- running = scenarios_active ()
93- self .log .debug (f"Checking if scenario stopped. Running scenarios: { len (running )} " )
94- return len (running ) == 0
84+ def run_and_check_scenario_from_file (self ):
85+ scenario_file = "test/data/scenario_p2p_interface.py"
86+ self .log .info (f"Running scenario from: { scenario_file } " )
87+ self .warnet (f"run { scenario_file } " )
88+ self .wait_for_predicate (self .check_scenario_clean_exit )
89+
90+ def check_regtest_recon (self ):
91+ scenario_file = "resources/scenarios/reconnaissance.py"
92+ self .log .info (f"Running scenario from file: { scenario_file } " )
93+ self .warnet (f"run { scenario_file } " )
94+ self .wait_for_predicate (self .check_scenario_clean_exit )
95+
96+ def check_active_count (self ):
97+ scenario_file = "test/data/scenario_buggy_failure.py"
98+ self .log .info (f"Running scenario from: { scenario_file } " )
99+ self .warnet (f"run { scenario_file } " )
100+
101+ def two_pass_one_fail ():
102+ deployed = scenarios_deployed ()
103+ if len ([s for s in deployed if s ["status" ] == "succeeded" ]) != 2 :
104+ return False
105+ return len ([s for s in deployed if s ["status" ] == "failed" ]) == 1
106+
107+ self .wait_for_predicate (two_pass_one_fail )
108+ table = self .warnet ("status" )
109+ assert "Active Scenarios: 0" in table
95110
96111
97112if __name__ == "__main__" :
0 commit comments