45 from typing
import Any, Callable, Dict, Iterable, NamedTuple
47 import scenario_config
51 LanguageConfig = NamedTuple(
'LanguageConfig', [(
'category', str),
53 (
'client_language', str),
54 (
'server_language', str)])
58 """Converts a list of categories into a single string for counting."""
60 return category
if category
in categories
else ''
62 main_categories = (
'scalable',
'smoketest')
65 c = [m
for m
in main_categories
if m
in s]
66 s.difference_update(main_categories)
72 """Generates tuples containing the languages specified in each scenario."""
73 for language
in scenario_config.LANGUAGES:
74 for scenario
in scenario_config.LANGUAGES[language].
scenarios():
75 client_language = scenario.get(
'CLIENT_LANGUAGE',
'')
76 server_language = scenario.get(
'SERVER_LANGUAGE',
'')
77 categories = scenario.get(
'CATEGORIES', [])
78 if category !=
'all' and category
not in categories:
83 client_language=client_language,
84 server_language=server_language)
88 scenario_name_regex: str =
'.*',
89 category: str =
'all',
90 client_language: str =
'',
91 server_language: str =
'',
92 ) -> Callable[[Dict[str, Any]], bool]:
93 """Returns a function to filter scenarios to process."""
95 def filter_scenario(scenario: Dict[str, Any]) -> bool:
96 """Filters scenarios that match specified criteria."""
97 if not re.search(scenario_name_regex, scenario[
"name"]):
102 scenario_categories = scenario.get(
'CATEGORIES',
103 [
'scalable',
'smoketest'])
104 if category
not in scenario_categories
and category !=
'all':
107 scenario_client_language = scenario.get(
'CLIENT_LANGUAGE',
'')
108 if client_language != scenario_client_language:
111 scenario_server_language = scenario.get(
'SERVER_LANGUAGE',
'')
112 if server_language != scenario_server_language:
117 return filter_scenario
121 language_name: str, scenario_filter_function: Callable[[Dict[str, Any]],
123 ) -> Iterable[Dict[str, Any]]:
124 """Generates scenarios that match a given filter function."""
126 scenario_config.remove_nonproto_fields,
127 filter(scenario_filter_function,
128 scenario_config.LANGUAGES[language_name].
scenarios()))
132 filename_prefix: str) ->
None:
133 """Dumps a list of scenarios to JSON files"""
135 for scenario
in scenarios:
136 filename =
'{}{}.json'.
format(filename_prefix, scenario[
'name'])
137 print(
'Writing file {}'.
format(filename), file=sys.stderr)
138 with open(filename,
'w')
as outfile:
141 json.dump({
'scenarios': [scenario]}, outfile, indent=2)
143 print(
'Wrote {} scenarios'.
format(count), file=sys.stderr)
147 language_choices = sorted(scenario_config.LANGUAGES.keys())
148 argp = argparse.ArgumentParser(description=
'Exports scenarios to files.')
149 argp.add_argument(
'--export_scenarios',
151 help=
'Export scenarios to JSON files.')
152 argp.add_argument(
'--count_scenarios',
154 help=
'Count scenarios for all test languages.')
155 argp.add_argument(
'-l',
157 choices=language_choices,
158 help=
'Language to export.')
159 argp.add_argument(
'-f',
161 default=
'scenario_dump_',
163 help=
'Prefix for exported JSON file names.')
164 argp.add_argument(
'-r',
168 help=
'Regex to select scenarios to run.')
172 choices=[
'all',
'inproc',
'scalable',
'smoketest',
'sweep'],
173 help=
'Select scenarios for a category of tests.')
177 choices=language_choices,
178 help=
'Select only scenarios with a specified client language.')
182 choices=language_choices,
183 help=
'Select only scenarios with a specified server language.')
184 args = argp.parse_args()
186 if args.export_scenarios
and not args.language:
187 print(
'Dumping scenarios requires a specified language.',
189 argp.print_usage(file=sys.stderr)
192 if args.export_scenarios:
194 category=args.category,
195 client_language=args.client_language,
196 server_language=args.server_language)
200 if args.count_scenarios:
201 print(
'Scenario count for all languages (category: {}):'.
format(
203 print(
'{:>5} {:16} {:8} {:8} {}'.
format(
'Count',
'Language',
'Client',
204 'Server',
'Categories'))
207 for ((cat, l, cl, sl), count)
in c.most_common():
208 print(
'{count:5} {l:16} {cl:8} {sl:8} {cat}'.
format(l=l,
215 print(
'\n{:>5} total scenarios (category: {})'.
format(
216 total, args.category))
219 if __name__ ==
"__main__":