1+ #!/usr/bin/env python3
12import argparse
23import logging
34import os
2627 dest = "seed" ,
2728 help = "Random seed for generating configs." ,
2829)
30+ PARSER .add_argument (
31+ "--num_init_config" ,
32+ default = 1 ,
33+ dest = "num_init_config" ,
34+ help = "Number of initial configs to generate for the given P4 program." ,
35+ )
2936
3037
3138# Parse options and process argv
@@ -46,16 +53,20 @@ class Options:
4653 testdir : Path = Path ("." )
4754 # Random seed for generating configs.
4855 seed : int = 1
56+ # Number of initial configs to generate for the given P4 program.
57+ num_init_config : int = 1
4958
5059
5160def generate_config (p4rtsmith_path , seed , p4_program_path , testdir , config_file_path ):
5261 command = f"{ p4rtsmith_path } --target bmv2 --arch v1model --seed { seed } --output-dir { testdir } --generate-config { config_file_path } { p4_program_path } "
53- subprocess .run (command , shell = True )
62+ returncode = subprocess .run (command , shell = True )
63+ return returncode .returncode
5464
5565
5666def run_test (run_test_script , p4_program_path , config_file_path ):
5767 command = f"sudo -E { run_test_script } .. { p4_program_path } -tf { config_file_path } "
58- subprocess .run (command , shell = True )
68+ returncode = subprocess .run (command , shell = True )
69+ return returncode .returncode
5970
6071
6172def find_p4c_dir ():
@@ -73,16 +84,24 @@ def find_p4c_dir():
7384
7485
7586def run_tests (options : Options ) -> int :
76- config_file_path = "initial_config.txtpb"
77-
7887 seed = options .seed
7988 testdir = options .testdir
8089 p4rtsmith_path = options .p4rtsmith
8190 run_test_script = FILE_DIR / "run-bmv2-proto-test.py"
8291 p4_program_path = options .p4_file
92+ filename = os .path .splitext (os .path .basename (p4_program_path ))[0 ]
8393
84- generate_config (p4rtsmith_path , seed , p4_program_path , testdir , config_file_path )
85- run_test (run_test_script , p4_program_path , testdir / config_file_path )
94+ for i in range (options .num_init_config ):
95+ config_file_path = f"initial_config_{ filename } _{ i } .txtpb"
96+ result = generate_config (
97+ p4rtsmith_path , seed , p4_program_path , testdir , config_file_path
98+ )
99+ if result != 0 :
100+ return result
101+ result = run_test (run_test_script , p4_program_path , testdir / config_file_path )
102+ if result != 0 :
103+ return result
104+ return 0
86105
87106
88107def create_options (test_args : Any ) -> Optional [Options ]:
@@ -101,6 +120,7 @@ def create_options(test_args: Any) -> Optional[Options]:
101120 os .chmod (testdir , 0o755 )
102121 options .testdir = Path (testdir )
103122 options .seed = test_args .seed
123+ options .num_init_config = test_args .num_init_config
104124
105125 # Configure logging.
106126 logging .basicConfig (
@@ -120,4 +140,5 @@ def create_options(test_args: Any) -> Optional[Options]:
120140 if not test_options :
121141 sys .exit ()
122142
123- run_tests (test_options )
143+ test_result = run_tests (test_options )
144+ sys .exit (test_result )
0 commit comments