back-to-back tests: add TG tests
[csit.git] / resources / libraries / python / autogen / Regenerator.py
1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Module defining utilities for test directory regeneration.
15
16 TODO: How can we check each suite id is unique,
17       when currently the suite generation is run on each directory separately?
18 """
19
20 import sys
21
22 from glob import glob
23 from io import open
24 from os import getcwd
25
26
27 from resources.libraries.python.Constants import Constants
28 from resources.libraries.python.autogen.Testcase import Testcase
29
30
31 PROTOCOL_TO_MIN_FRAME_SIZE = {
32     u"ip4": 64,
33     u"ip6": 78,
34     u"ethip4vxlan": 114,  # What is the real minimum for latency stream?
35     u"dot1qip4vxlan": 118
36 }
37 MIN_FRAME_SIZE_VALUES = list(PROTOCOL_TO_MIN_FRAME_SIZE.values())
38
39
40 def replace_defensively(
41         whole, to_replace, replace_with, how_many, msg, in_filename):
42     """Replace substrings while checking the number of occurrences.
43
44     Return edited copy of the text. Assuming "whole" is really a string,
45     or something else with .replace not affecting it.
46
47     :param whole: The text to perform replacements on.
48     :param to_replace: Substring occurrences of which to replace.
49     :param replace_with: Substring to replace occurrences with.
50     :param how_many: Number of occurrences to expect.
51     :param msg: Error message to raise.
52     :param in_filename: File name in which the error occurred.
53     :type whole: str
54     :type to_replace: str
55     :type replace_with: str
56     :type how_many: int
57     :type msg: str
58     :type in_filename: str
59     :returns: The whole text after replacements are done.
60     :rtype: str
61     :raises ValueError: If number of occurrences does not match.
62     """
63     found = whole.count(to_replace)
64     if found != how_many:
65         raise ValueError(f"{in_filename}: {msg}")
66     return whole.replace(to_replace, replace_with)
67
68
69 def get_iface_and_suite_ids(filename):
70     """Get NIC code, suite ID and suite tag.
71
72     NIC code is the part of suite name
73     which should be replaced for other NIC.
74     Suite ID is the part os suite name
75     which is appended to test case names.
76     Suite tag is suite ID without both test type and NIC driver parts.
77
78     :param filename: Suite file.
79     :type filename: str
80     :returns: NIC code, suite ID, suite tag.
81     :rtype: 3-tuple of str
82     """
83     dash_split = filename.split(u"-", 1)
84     if len(dash_split[0]) <= 4:
85         # It was something like "2n1l", we need one more split.
86         dash_split = dash_split[1].split(u"-", 1)
87     nic_code = dash_split[0]
88     suite_id = dash_split[1].split(u".robot", 1)[0]
89     suite_tag = suite_id.rsplit(u"-", 1)[0]
90     for prefix in Constants.FORBIDDEN_SUITE_PREFIX_LIST:
91         if suite_tag.startswith(prefix):
92             suite_tag = suite_tag[len(prefix):]
93     return nic_code, suite_id, suite_tag
94
95
96 def check_suite_tag(suite_tag, prolog):
97     """Verify suite tag occurres once in prolog.
98
99     Call this after all edits are done,
100     to confirm the (edited) suite tag still matches the (edited) suite name.
101
102     Currently, the edited suite tag is expect to be identical
103     to the primary suite tag, but having a function is more flexible.
104
105     The occurences are counted including "| " prefix,
106     to lower the chance to match a comment.
107
108     :param suite_tag: Part of suite name, between NIC driver and suite type.
109     :param prolog: The part of .robot file content without test cases.
110     :type suite_tag: str
111     :type prolog: str
112     :raises ValueError: If suite_tag not found exactly once.
113     """
114     found = prolog.count(u"| " + suite_tag)
115     if found != 1:
116         raise ValueError(f"Suite tag found {found} times for {suite_tag}")
117
118
119 def add_default_testcases(testcase, iface, suite_id, file_out, tc_kwargs_list):
120     """Add default testcases to file.
121
122     :param testcase: Testcase class.
123     :param iface: Interface.
124     :param suite_id: Suite ID.
125     :param file_out: File to write testcases to.
126     :param tc_kwargs_list: Key-value pairs used to construct testcases.
127     :type testcase: Testcase
128     :type iface: str
129     :type suite_id: str
130     :type file_out: file
131     :type tc_kwargs_list: dict
132     """
133     for kwargs in tc_kwargs_list:
134         # TODO: Is there a better way to disable some combinations?
135         emit = True
136         if kwargs[u"frame_size"] == 9000:
137             if u"vic1227" in iface:
138                 # Not supported in HW.
139                 emit = False
140             if u"vic1385" in iface:
141                 # Not supported in HW.
142                 emit = False
143         if u"-16vm2t-" in suite_id or u"-16dcr2t-" in suite_id:
144             if kwargs[u"phy_cores"] > 3:
145                 # CSIT lab only has 28 (physical) core processors,
146                 # so these test would fail when attempting to assign cores.
147                 emit = False
148         if u"-24vm1t-" in suite_id or u"-24dcr1t-" in suite_id:
149             if kwargs[u"phy_cores"] > 3:
150                 # CSIT lab only has 28 (physical) core processors,
151                 # so these test would fail when attempting to assign cores.
152                 emit = False
153         if u"soak" in suite_id:
154             # Soak test take too long, do not risk other than tc01.
155             if kwargs[u"phy_cores"] != 1:
156                 emit = False
157             if kwargs[u"frame_size"] not in MIN_FRAME_SIZE_VALUES:
158                 emit = False
159         if (
160                 u"-cps-" in suite_id
161                 or u"-pps-" in suite_id
162                 or u"-tput-" in suite_id
163         ):
164             if kwargs[u"frame_size"] not in MIN_FRAME_SIZE_VALUES:
165                 emit = False
166         if emit:
167             file_out.write(testcase.generate(**kwargs))
168
169
170 def add_tcp_testcases(testcase, file_out, tc_kwargs_list):
171     """Add TCP testcases to file.
172
173     :param testcase: Testcase class.
174     :param file_out: File to write testcases to.
175     :param tc_kwargs_list: Key-value pairs used to construct testcases.
176     :type testcase: Testcase
177     :type file_out: file
178     :type tc_kwargs_list: dict
179     """
180     for kwargs in tc_kwargs_list:
181         file_out.write(testcase.generate(**kwargs))
182
183
184 def add_iperf3_testcases(testcase, file_out, tc_kwargs_list):
185     """Add iperf3 testcases to file.
186
187     :param testcase: Testcase class.
188     :param file_out: File to write testcases to.
189     :param tc_kwargs_list: Key-value pairs used to construct testcases.
190     :type testcase: Testcase
191     :type file_out: file
192     :type tc_kwargs_list: dict
193     """
194     for kwargs in tc_kwargs_list:
195         file_out.write(testcase.generate(**kwargs))
196
197
198 def add_trex_testcases(testcase, file_out, tc_kwargs_list):
199     """Add trex testcases to file.
200
201     :param testcase: Testcase class.
202     :param file_out: File to write testcases to.
203     :param tc_kwargs_list: Key-value pairs used to construct testcases.
204     :type testcase: Testcase
205     :type file_out: file
206     :type tc_kwargs_list: dict
207     """
208     for kwargs in tc_kwargs_list:
209         file_out.write(testcase.generate(**kwargs))
210
211
212 def write_default_files(in_filename, in_prolog, kwargs_list):
213     """Using given filename and prolog, write all generated suites.
214
215     :param in_filename: Template filename to derive real filenames from.
216     :param in_prolog: Template content to derive real content from.
217     :param kwargs_list: List of kwargs for add_default_testcase.
218     :type in_filename: str
219     :type in_prolog: str
220     :type kwargs_list: list of dict
221     """
222     for suite_type in Constants.PERF_TYPE_TO_KEYWORD:
223         tmp_filename = replace_defensively(
224             in_filename, u"ndrpdr", suite_type, 1,
225             u"File name should contain suite type once.", in_filename
226         )
227         tmp_prolog = replace_defensively(
228             in_prolog, u"ndrpdr".upper(), suite_type.upper(), 1,
229             u"Suite type should appear once in uppercase (as tag).",
230             in_filename
231         )
232         tmp_prolog = replace_defensively(
233             tmp_prolog,
234             u"Find NDR and PDR intervals using optimized search",
235             Constants.PERF_TYPE_TO_KEYWORD[suite_type], 1,
236             u"Main search keyword should appear once in suite.",
237             in_filename
238         )
239         tmp_prolog = replace_defensively(
240             tmp_prolog,
241             Constants.PERF_TYPE_TO_SUITE_DOC_VER[u"ndrpdr"],
242             Constants.PERF_TYPE_TO_SUITE_DOC_VER[suite_type],
243             1, u"Exact suite type doc not found.", in_filename
244         )
245         tmp_prolog = replace_defensively(
246             tmp_prolog,
247             Constants.PERF_TYPE_TO_TEMPLATE_DOC_VER[u"ndrpdr"],
248             Constants.PERF_TYPE_TO_TEMPLATE_DOC_VER[suite_type],
249             1, u"Exact template type doc not found.", in_filename
250         )
251         _, suite_id, _ = get_iface_and_suite_ids(tmp_filename)
252         testcase = Testcase.default(suite_id)
253         for nic_name in Constants.NIC_NAME_TO_CODE:
254             tmp2_filename = replace_defensively(
255                 tmp_filename, u"10ge2p1x710",
256                 Constants.NIC_NAME_TO_CODE[nic_name], 1,
257                 u"File name should contain NIC code once.", in_filename
258             )
259             tmp2_prolog = replace_defensively(
260                 tmp_prolog, u"Intel-X710", nic_name, 2,
261                 u"NIC name should appear twice (tag and variable).",
262                 in_filename
263             )
264             if tmp2_prolog.count(u"HW_") == 2:
265                 # TODO CSIT-1481: Crypto HW should be read
266                 #      from topology file instead.
267                 if nic_name in Constants.NIC_NAME_TO_CRYPTO_HW:
268                     tmp2_prolog = replace_defensively(
269                         tmp2_prolog, u"HW_DH895xcc",
270                         Constants.NIC_NAME_TO_CRYPTO_HW[nic_name], 1,
271                         u"HW crypto name should appear.", in_filename
272                     )
273             iface, old_suite_id, old_suite_tag = get_iface_and_suite_ids(
274                 tmp2_filename
275             )
276             if u"DPDK" in in_prolog:
277                 for driver in Constants.DPDK_NIC_NAME_TO_DRIVER[nic_name]:
278                     out_filename = replace_defensively(
279                         tmp2_filename, old_suite_id,
280                         Constants.DPDK_NIC_DRIVER_TO_SUITE_PREFIX[driver] \
281                             + old_suite_id,
282                         1, u"Error adding driver prefix.", in_filename
283                     )
284                     out_prolog = replace_defensively(
285                         tmp2_prolog, u"vfio-pci", driver, 1,
286                         u"Driver name should appear once.", in_filename
287                     )
288                     out_prolog = replace_defensively(
289                         out_prolog,
290                         Constants.DPDK_NIC_DRIVER_TO_TAG[u"vfio-pci"],
291                         Constants.DPDK_NIC_DRIVER_TO_TAG[driver], 1,
292                         u"Driver tag should appear once.", in_filename
293                     )
294                     iface, suite_id, suite_tag = get_iface_and_suite_ids(
295                         out_filename
296                     )
297                     # The next replace is probably a noop, but it is safer to
298                     # maintain the same structure as for other edits.
299                     out_prolog = replace_defensively(
300                         out_prolog, old_suite_tag, suite_tag, 1,
301                         f"Perf suite tag {old_suite_tag} should appear once.",
302                         in_filename
303                     )
304                     check_suite_tag(suite_tag, out_prolog)
305                     # TODO: Reorder loops so suite_id is finalized sooner.
306                     testcase = Testcase.default(suite_id)
307                     with open(out_filename, u"wt") as file_out:
308                         file_out.write(out_prolog)
309                         add_default_testcases(
310                             testcase, iface, suite_id, file_out, kwargs_list
311                         )
312                 continue
313             for driver in Constants.NIC_NAME_TO_DRIVER[nic_name]:
314                 out_filename = replace_defensively(
315                     tmp2_filename, old_suite_id,
316                     Constants.NIC_DRIVER_TO_SUITE_PREFIX[driver] + old_suite_id,
317                     1, u"Error adding driver prefix.", in_filename
318                 )
319                 out_prolog = replace_defensively(
320                     tmp2_prolog, u"vfio-pci", driver, 1,
321                     u"Driver name should appear once.", in_filename
322                 )
323                 out_prolog = replace_defensively(
324                     out_prolog, Constants.NIC_DRIVER_TO_TAG[u"vfio-pci"],
325                     Constants.NIC_DRIVER_TO_TAG[driver], 1,
326                     u"Driver tag should appear once.", in_filename
327                 )
328                 out_prolog = replace_defensively(
329                     out_prolog, Constants.NIC_DRIVER_TO_PLUGINS[u"vfio-pci"],
330                     Constants.NIC_DRIVER_TO_PLUGINS[driver], 1,
331                     u"Driver plugin should appear once.", in_filename
332                 )
333                 out_prolog = replace_defensively(
334                     out_prolog, Constants.NIC_DRIVER_TO_VFS[u"vfio-pci"],
335                     Constants.NIC_DRIVER_TO_VFS[driver], 1,
336                     u"NIC VFs argument should appear once.", in_filename
337                 )
338                 iface, suite_id, suite_tag = get_iface_and_suite_ids(
339                     out_filename
340                 )
341                 # The next replace is probably a noop, but it is safer to
342                 # maintain the same structure as for other edits.
343                 out_prolog = replace_defensively(
344                     out_prolog, old_suite_tag, suite_tag, 1,
345                     f"Perf suite tag {old_suite_tag} should appear once.",
346                     in_filename
347                 )
348                 check_suite_tag(suite_tag, out_prolog)
349                 # TODO: Reorder loops so suite_id is finalized sooner.
350                 testcase = Testcase.default(suite_id)
351                 with open(out_filename, u"wt") as file_out:
352                     file_out.write(out_prolog)
353                     add_default_testcases(
354                         testcase, iface, suite_id, file_out, kwargs_list
355                     )
356
357
358 def write_reconf_files(in_filename, in_prolog, kwargs_list):
359     """Using given filename and prolog, write all generated reconf suites.
360
361     Use this for suite type reconf, as its local template
362     is incompatible with mrr/ndrpdr/soak local template,
363     while test cases are compatible.
364
365     :param in_filename: Template filename to derive real filenames from.
366     :param in_prolog: Template content to derive real content from.
367     :param kwargs_list: List of kwargs for add_testcase.
368     :type in_filename: str
369     :type in_prolog: str
370     :type kwargs_list: list of dict
371     """
372     _, suite_id, _ = get_iface_and_suite_ids(in_filename)
373     testcase = Testcase.default(suite_id)
374     for nic_name in Constants.NIC_NAME_TO_CODE:
375         tmp_filename = replace_defensively(
376             in_filename, u"10ge2p1x710",
377             Constants.NIC_NAME_TO_CODE[nic_name], 1,
378             u"File name should contain NIC code once.", in_filename
379         )
380         tmp_prolog = replace_defensively(
381             in_prolog, u"Intel-X710", nic_name, 2,
382             u"NIC name should appear twice (tag and variable).",
383             in_filename
384         )
385         if tmp_prolog.count(u"HW_") == 2:
386             # TODO CSIT-1481: Crypto HW should be read
387             #      from topology file instead.
388             if nic_name in Constants.NIC_NAME_TO_CRYPTO_HW.keys():
389                 tmp_prolog = replace_defensively(
390                     tmp_prolog, u"HW_DH895xcc",
391                     Constants.NIC_NAME_TO_CRYPTO_HW[nic_name], 1,
392                     u"HW crypto name should appear.", in_filename
393                 )
394         iface, old_suite_id, old_suite_tag = get_iface_and_suite_ids(
395             tmp_filename
396         )
397         for driver in Constants.NIC_NAME_TO_DRIVER[nic_name]:
398             out_filename = replace_defensively(
399                 tmp_filename, old_suite_id,
400                 Constants.NIC_DRIVER_TO_SUITE_PREFIX[driver] + old_suite_id,
401                 1, u"Error adding driver prefix.", in_filename
402             )
403             out_prolog = replace_defensively(
404                 tmp_prolog, u"vfio-pci", driver, 1,
405                 u"Driver name should appear once.", in_filename
406             )
407             out_prolog = replace_defensively(
408                 out_prolog, Constants.NIC_DRIVER_TO_TAG[u"vfio-pci"],
409                 Constants.NIC_DRIVER_TO_TAG[driver], 1,
410                 u"Driver tag should appear once.", in_filename
411             )
412             out_prolog = replace_defensively(
413                 out_prolog, Constants.NIC_DRIVER_TO_PLUGINS[u"vfio-pci"],
414                 Constants.NIC_DRIVER_TO_PLUGINS[driver], 1,
415                 u"Driver plugin should appear once.", in_filename
416             )
417             out_prolog = replace_defensively(
418                 out_prolog, Constants.NIC_DRIVER_TO_VFS[u"vfio-pci"],
419                 Constants.NIC_DRIVER_TO_VFS[driver], 1,
420                 u"NIC VFs argument should appear once.", in_filename
421             )
422             iface, suite_id, suite_tag = get_iface_and_suite_ids(out_filename)
423             out_prolog = replace_defensively(
424                 out_prolog, old_suite_tag, suite_tag, 1,
425                 u"Perf suite tag should appear once.", in_filename
426             )
427             check_suite_tag(suite_tag, out_prolog)
428             # TODO: Reorder loops so suite_id is finalized sooner.
429             testcase = Testcase.default(suite_id)
430             with open(out_filename, u"wt") as file_out:
431                 file_out.write(out_prolog)
432                 add_default_testcases(
433                     testcase, iface, suite_id, file_out, kwargs_list
434                 )
435
436
437 def write_tcp_files(in_filename, in_prolog, kwargs_list):
438     """Using given filename and prolog, write all generated tcp suites.
439
440     TODO: Suport drivers.
441
442     :param in_filename: Template filename to derive real filenames from.
443     :param in_prolog: Template content to derive real content from.
444     :param kwargs_list: List of kwargs for add_default_testcase.
445     :type in_filename: str
446     :type in_prolog: str
447     :type kwargs_list: list of dict
448     """
449     # TODO: Generate rps from cps? There are subtle differences.
450     _, suite_id, suite_tag = get_iface_and_suite_ids(in_filename)
451     testcase = Testcase.tcp(suite_id)
452     for nic_name in Constants.NIC_NAME_TO_CODE:
453         out_filename = replace_defensively(
454             in_filename, u"10ge2p1x710",
455             Constants.NIC_NAME_TO_CODE[nic_name], 1,
456             u"File name should contain NIC code once.", in_filename
457         )
458         out_prolog = replace_defensively(
459             in_prolog, u"Intel-X710", nic_name, 2,
460             u"NIC name should appear twice (tag and variable).",
461             in_filename
462         )
463         check_suite_tag(suite_tag, out_prolog)
464         with open(out_filename, u"wt") as file_out:
465             file_out.write(out_prolog)
466             add_tcp_testcases(testcase, file_out, kwargs_list)
467
468
469 def write_iperf3_files(in_filename, in_prolog, kwargs_list):
470     """Using given filename and prolog, write all generated iperf3 suites.
471
472     :param in_filename: Template filename to derive real filenames from.
473     :param in_prolog: Template content to derive real content from.
474     :param kwargs_list: List of kwargs for add_default_testcase.
475     :type in_filename: str
476     :type in_prolog: str
477     :type kwargs_list: list of dict
478     """
479     _, suite_id, suite_tag = get_iface_and_suite_ids(in_filename)
480     testcase = Testcase.iperf3(suite_id)
481     out_filename = replace_defensively(
482         in_filename, u"10ge2p1x710",
483         Constants.NIC_NAME_TO_CODE[u"Intel-X710"], 1,
484         u"File name should contain NIC code once.", in_filename
485     )
486     out_prolog = replace_defensively(
487         in_prolog, u"Intel-X710", u"Intel-X710", 2,
488         u"NIC name should appear twice (tag and variable).",
489         in_filename
490     )
491     check_suite_tag(suite_tag, out_prolog)
492     with open(out_filename, u"wt") as file_out:
493         file_out.write(out_prolog)
494         add_iperf3_testcases(testcase, file_out, kwargs_list)
495
496
497 def write_trex_files(in_filename, in_prolog, kwargs_list):
498     """Using given filename and prolog, write all generated trex suites.
499
500     :param in_filename: Template filename to derive real filenames from.
501     :param in_prolog: Template content to derive real content from.
502     :param kwargs_list: List of kwargs for add_trex_testcase.
503     :type in_filename: str
504     :type in_prolog: str
505     :type kwargs_list: list of dict
506     """
507     for suite_type in Constants.PERF_TYPE_TO_KEYWORD:
508         tmp_filename = replace_defensively(
509             in_filename, u"ndrpdr", suite_type, 1,
510             u"File name should contain suite type once.", in_filename
511         )
512         tmp_prolog = replace_defensively(
513             in_prolog, u"ndrpdr".upper(), suite_type.upper(), 1,
514             u"Suite type should appear once in uppercase (as tag).",
515             in_filename
516         )
517         tmp_prolog = replace_defensively(
518             tmp_prolog,
519             u"Find NDR and PDR intervals using optimized search",
520             Constants.PERF_TYPE_TO_KEYWORD[suite_type], 1,
521             u"Main search keyword should appear once in suite.",
522             in_filename
523         )
524         tmp_prolog = replace_defensively(
525             tmp_prolog,
526             Constants.PERF_TYPE_TO_SUITE_DOC_VER[u"ndrpdr"],
527             Constants.PERF_TYPE_TO_SUITE_DOC_VER[suite_type],
528             1, u"Exact suite type doc not found.", in_filename
529         )
530         tmp_prolog = replace_defensively(
531             tmp_prolog,
532             Constants.PERF_TYPE_TO_TEMPLATE_DOC_VER[u"ndrpdr"],
533             Constants.PERF_TYPE_TO_TEMPLATE_DOC_VER[suite_type],
534             1, u"Exact template type doc not found.", in_filename
535         )
536         _, suite_id, suite_tag = get_iface_and_suite_ids(tmp_filename)
537         testcase = Testcase.trex(suite_id)
538         for nic_name in Constants.NIC_NAME_TO_CODE:
539             out_filename = replace_defensively(
540                 tmp_filename, u"10ge2p1x710",
541                 Constants.NIC_NAME_TO_CODE[nic_name], 1,
542                 u"File name should contain NIC code once.", in_filename
543             )
544             out_prolog = replace_defensively(
545                 tmp_prolog, u"Intel-X710", nic_name, 2,
546                 u"NIC name should appear twice (tag and variable).",
547                 in_filename
548             )
549             check_suite_tag(suite_tag, out_prolog)
550             with open(out_filename, u"wt") as file_out:
551                 file_out.write(out_prolog)
552                 add_trex_testcases(testcase, file_out, kwargs_list)
553
554
555 def write_device_files(in_filename, in_prolog, kwargs_list):
556     """Using given filename and prolog, write all generated suites.
557
558     :param in_filename: Template filename to derive real filenames from.
559     :param in_prolog: Template content to derive real content from.
560     :param kwargs_list: List of kwargs for add_default_testcase.
561     :type in_filename: str
562     :type in_prolog: str
563     :type kwargs_list: list of dict
564     """
565     for suite_type in Constants.DEVICE_TYPE_TO_KEYWORD:
566         tmp_filename = replace_defensively(
567             in_filename, u"scapy", suite_type, 1,
568             u"File name should contain suite type once.", in_filename
569         )
570         _, suite_id, _ = get_iface_and_suite_ids(tmp_filename)
571         testcase = Testcase.default(suite_id)
572         for nic_name in Constants.NIC_NAME_TO_CODE:
573             tmp2_filename = replace_defensively(
574                 tmp_filename, u"10ge2p1x710",
575                 Constants.NIC_NAME_TO_CODE[nic_name], 1,
576                 u"File name should contain NIC code once.", in_filename
577             )
578             tmp2_prolog = replace_defensively(
579                 in_prolog, u"Intel-X710", nic_name, 2,
580                 u"NIC name should appear twice (tag and variable).",
581                 in_filename
582             )
583             iface, old_suite_id, _ = get_iface_and_suite_ids(
584                 tmp2_filename
585             )
586             for driver in Constants.NIC_NAME_TO_DRIVER[nic_name]:
587                 out_filename = replace_defensively(
588                     tmp2_filename, old_suite_id,
589                     Constants.NIC_DRIVER_TO_SUITE_PREFIX[driver] + old_suite_id,
590                     1, u"Error adding driver prefix.", in_filename
591                 )
592                 out_prolog = replace_defensively(
593                     tmp2_prolog, u"vfio-pci", driver, 1,
594                     u"Driver name should appear once.", in_filename
595                 )
596                 out_prolog = replace_defensively(
597                     out_prolog, Constants.NIC_DRIVER_TO_TAG[u"vfio-pci"],
598                     Constants.NIC_DRIVER_TO_TAG[driver], 1,
599                     u"Driver tag should appear once.", in_filename
600                 )
601                 out_prolog = replace_defensively(
602                     out_prolog, Constants.NIC_DRIVER_TO_PLUGINS[u"vfio-pci"],
603                     Constants.NIC_DRIVER_TO_PLUGINS[driver], 1,
604                     u"Driver plugin should appear once.", in_filename
605                 )
606                 out_prolog = replace_defensively(
607                     out_prolog, Constants.NIC_DRIVER_TO_VFS[u"vfio-pci"],
608                     Constants.NIC_DRIVER_TO_VFS[driver], 1,
609                     u"NIC VFs argument should appear once.", in_filename
610                 )
611                 iface, suite_id, suite_tag = get_iface_and_suite_ids(
612                     out_filename
613                 )
614                 check_suite_tag(suite_tag, out_prolog)
615                 # TODO: Reorder loops so suite_id is finalized sooner.
616                 testcase = Testcase.default(suite_id)
617                 with open(out_filename, u"wt") as file_out:
618                     file_out.write(out_prolog)
619                     add_default_testcases(
620                         testcase, iface, suite_id, file_out, kwargs_list
621                     )
622
623
624 class Regenerator:
625     """Class containing file generating methods."""
626
627     def __init__(self, quiet=True):
628         """Initialize the instance.
629
630         :param quiet: Reduce log prints (to stderr) when True (default).
631         :type quiet: boolean
632         """
633         self.quiet = quiet
634
635     def regenerate_glob(self, pattern, protocol=u"ip4"):
636         """Regenerate files matching glob pattern based on arguments.
637
638         In the current working directory, find all files matching
639         the glob pattern. Use testcase template to regenerate test cases
640         according to suffix, governed by protocol, autonumbering them.
641         Also generate suites for other NICs and drivers.
642
643         Log-like prints are emitted to sys.stderr.
644
645         :param pattern: Glob pattern to select files. Example: *-ndrpdr.robot
646         :param protocol: String determining minimal frame size. Default: "ip4"
647         :type pattern: str
648         :type protocol: str
649         :raises RuntimeError: If invalid source suite is encountered.
650         """
651         if not self.quiet:
652             print(f"Regenerator starts at {getcwd()}", file=sys.stderr)
653
654         min_frame_size = PROTOCOL_TO_MIN_FRAME_SIZE[protocol]
655         default_kwargs_list = [
656             {u"frame_size": min_frame_size, u"phy_cores": 1},
657             {u"frame_size": min_frame_size, u"phy_cores": 2},
658             {u"frame_size": min_frame_size, u"phy_cores": 4},
659             {u"frame_size": 1518, u"phy_cores": 1},
660             {u"frame_size": 1518, u"phy_cores": 2},
661             {u"frame_size": 1518, u"phy_cores": 4},
662             {u"frame_size": 9000, u"phy_cores": 1},
663             {u"frame_size": 9000, u"phy_cores": 2},
664             {u"frame_size": 9000, u"phy_cores": 4},
665             {u"frame_size": u"IMIX_v4_1", u"phy_cores": 1},
666             {u"frame_size": u"IMIX_v4_1", u"phy_cores": 2},
667             {u"frame_size": u"IMIX_v4_1", u"phy_cores": 4}
668         ]
669         hs_bps_kwargs_list = [
670             {u"frame_size": 1460, u"phy_cores": 1},
671         ]
672         hs_quic_kwargs_list = [
673             {u"frame_size": 1280, u"phy_cores": 1},
674         ]
675         iperf3_kwargs_list = [
676             {u"frame_size": 128000, u"phy_cores": 1},
677             {u"frame_size": 128000, u"phy_cores": 2},
678             {u"frame_size": 128000, u"phy_cores": 4}
679         ]
680         # List for tests with one dataplane core
681         # (and variable number of other cores).
682         dp1_kwargs_list = [
683             {u"frame_size": min_frame_size, u"phy_cores": 2},
684             {u"frame_size": min_frame_size, u"phy_cores": 3},
685             {u"frame_size": min_frame_size, u"phy_cores": 4},
686             {u"frame_size": 1518, u"phy_cores": 2},
687             {u"frame_size": 1518, u"phy_cores": 3},
688             {u"frame_size": 1518, u"phy_cores": 4},
689             {u"frame_size": 9000, u"phy_cores": 2},
690             {u"frame_size": 9000, u"phy_cores": 3},
691             {u"frame_size": 9000, u"phy_cores": 4},
692             {u"frame_size": u"IMIX_v4_1", u"phy_cores": 2},
693             {u"frame_size": u"IMIX_v4_1", u"phy_cores": 3},
694             {u"frame_size": u"IMIX_v4_1", u"phy_cores": 4}
695         ]
696
697         http_kwargs_list = [
698             {u"frame_size": 0, u"phy_cores": 1},
699             {u"frame_size": 0, u"phy_cores": 2},
700             {u"frame_size": 64, u"phy_cores": 1},
701             {u"frame_size": 64, u"phy_cores": 2},
702             {u"frame_size": 1024, u"phy_cores": 1},
703             {u"frame_size": 1024, u"phy_cores": 2},
704             {u"frame_size": 2048, u"phy_cores": 1},
705             {u"frame_size": 2048, u"phy_cores": 2}
706         ]
707
708         device_kwargs_list = [
709             {u"frame_size": min_frame_size, u"phy_cores": 0}
710         ]
711
712         trex_kwargs_list = [
713             {u"frame_size": min_frame_size},
714             {u"frame_size": 1518},
715             {u"frame_size": 9000},
716             {u"frame_size": u"IMIX_v4_1"}
717         ]
718
719         for in_filename in glob(pattern):
720             if not self.quiet:
721                 print(
722                     u"Regenerating in_filename:", in_filename, file=sys.stderr
723                 )
724             iface, _, _ = get_iface_and_suite_ids(in_filename)
725             if not iface.endswith(u"10ge2p1x710"):
726                 raise RuntimeError(
727                     f"Error in {in_filename}: non-primary NIC found."
728                 )
729             for prefix in Constants.FORBIDDEN_SUITE_PREFIX_LIST:
730                 if prefix in in_filename:
731                     raise RuntimeError(
732                         f"Error in {in_filename}: non-primary driver found."
733                     )
734             with open(in_filename, u"rt") as file_in:
735                 in_prolog = u"".join(
736                     file_in.read().partition(u"*** Test Cases ***")[:-1]
737                 )
738             if "-tg-" in in_filename:
739                 write_trex_files(in_filename, in_prolog, trex_kwargs_list)
740                 continue
741             if in_filename.endswith(u"-ndrpdr.robot"):
742                 if u"scheduler" in in_filename:
743                     write_default_files(
744                         in_filename, in_prolog, dp1_kwargs_list
745                     )
746                 else:
747                     write_default_files(
748                         in_filename, in_prolog, default_kwargs_list
749                     )
750             elif in_filename.endswith(u"-reconf.robot"):
751                 write_reconf_files(in_filename, in_prolog, default_kwargs_list)
752             elif in_filename.endswith(u"-rps.robot") \
753                     or in_filename.endswith(u"-cps.robot"):
754                 write_tcp_files(in_filename, in_prolog, http_kwargs_list)
755             elif in_filename.endswith(u"-bps.robot"):
756                 hoststack_kwargs_list = \
757                     hs_quic_kwargs_list if u"quic" in in_filename \
758                     else hs_bps_kwargs_list
759                 write_tcp_files(in_filename, in_prolog, hoststack_kwargs_list)
760             elif in_filename.endswith(u"-iperf3-mrr.robot"):
761                 write_iperf3_files(in_filename, in_prolog, iperf3_kwargs_list)
762             elif in_filename.endswith(u"-scapy.robot"):
763                 write_device_files(in_filename, in_prolog, device_kwargs_list)
764             else:
765                 raise RuntimeError(
766                     f"Error in {in_filename}: non-primary suite type found."
767                 )
768         if not self.quiet:
769             print(u"Regenerator ends.", file=sys.stderr)
770         print(file=sys.stderr)  # To make autogen check output more readable.