back-to-back: add tg n2n suites
[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, suite_id, file_out, tc_kwargs_list):
199     """Add trex testcases to file.
200
201     :param testcase: Testcase class.
202     :param suite_id: Suite ID.
203     :param file_out: File to write testcases to.
204     :param tc_kwargs_list: Key-value pairs used to construct testcases.
205     :type testcase: Testcase
206     :type suite_id: str
207     :type file_out: file
208     :type tc_kwargs_list: dict
209     """
210     for kwargs in tc_kwargs_list:
211         # TODO: Is there a better way to disable some combinations?
212         emit = True
213         if (
214                 u"-cps-" in suite_id
215                 or u"-pps-" in suite_id
216                 or u"-tput-" in suite_id
217         ):
218             if kwargs[u"frame_size"] not in MIN_FRAME_SIZE_VALUES:
219                 emit = False
220         if emit:
221             file_out.write(testcase.generate(**kwargs))
222
223
224 def write_default_files(in_filename, in_prolog, kwargs_list):
225     """Using given filename and prolog, write all generated suites.
226
227     :param in_filename: Template filename to derive real filenames from.
228     :param in_prolog: Template content to derive real content from.
229     :param kwargs_list: List of kwargs for add_default_testcase.
230     :type in_filename: str
231     :type in_prolog: str
232     :type kwargs_list: list of dict
233     """
234     for suite_type in Constants.PERF_TYPE_TO_KEYWORD:
235         tmp_filename = replace_defensively(
236             in_filename, u"ndrpdr", suite_type, 1,
237             u"File name should contain suite type once.", in_filename
238         )
239         tmp_prolog = replace_defensively(
240             in_prolog, u"ndrpdr".upper(), suite_type.upper(), 1,
241             u"Suite type should appear once in uppercase (as tag).",
242             in_filename
243         )
244         tmp_prolog = replace_defensively(
245             tmp_prolog,
246             u"Find NDR and PDR intervals using optimized search",
247             Constants.PERF_TYPE_TO_KEYWORD[suite_type], 1,
248             u"Main search keyword should appear once in suite.",
249             in_filename
250         )
251         tmp_prolog = replace_defensively(
252             tmp_prolog,
253             Constants.PERF_TYPE_TO_SUITE_DOC_VER[u"ndrpdr"],
254             Constants.PERF_TYPE_TO_SUITE_DOC_VER[suite_type],
255             1, u"Exact suite type doc not found.", in_filename
256         )
257         tmp_prolog = replace_defensively(
258             tmp_prolog,
259             Constants.PERF_TYPE_TO_TEMPLATE_DOC_VER[u"ndrpdr"],
260             Constants.PERF_TYPE_TO_TEMPLATE_DOC_VER[suite_type],
261             1, u"Exact template type doc not found.", in_filename
262         )
263         _, suite_id, _ = get_iface_and_suite_ids(tmp_filename)
264         testcase = Testcase.default(suite_id)
265         for nic_name in Constants.NIC_NAME_TO_CODE:
266             tmp2_filename = replace_defensively(
267                 tmp_filename, u"10ge2p1x710",
268                 Constants.NIC_NAME_TO_CODE[nic_name], 1,
269                 u"File name should contain NIC code once.", in_filename
270             )
271             tmp2_prolog = replace_defensively(
272                 tmp_prolog, u"Intel-X710", nic_name, 2,
273                 u"NIC name should appear twice (tag and variable).",
274                 in_filename
275             )
276             if tmp2_prolog.count(u"HW_") == 2:
277                 # TODO CSIT-1481: Crypto HW should be read
278                 #      from topology file instead.
279                 if nic_name in Constants.NIC_NAME_TO_CRYPTO_HW:
280                     tmp2_prolog = replace_defensively(
281                         tmp2_prolog, u"HW_DH895xcc",
282                         Constants.NIC_NAME_TO_CRYPTO_HW[nic_name], 1,
283                         u"HW crypto name should appear.", in_filename
284                     )
285             iface, old_suite_id, old_suite_tag = get_iface_and_suite_ids(
286                 tmp2_filename
287             )
288             if u"DPDK" in in_prolog:
289                 for driver in Constants.DPDK_NIC_NAME_TO_DRIVER[nic_name]:
290                     out_filename = replace_defensively(
291                         tmp2_filename, old_suite_id,
292                         Constants.DPDK_NIC_DRIVER_TO_SUITE_PREFIX[driver] \
293                             + old_suite_id,
294                         1, u"Error adding driver prefix.", in_filename
295                     )
296                     out_prolog = replace_defensively(
297                         tmp2_prolog, u"vfio-pci", driver, 1,
298                         u"Driver name should appear once.", in_filename
299                     )
300                     out_prolog = replace_defensively(
301                         out_prolog,
302                         Constants.DPDK_NIC_DRIVER_TO_TAG[u"vfio-pci"],
303                         Constants.DPDK_NIC_DRIVER_TO_TAG[driver], 1,
304                         u"Driver tag should appear once.", in_filename
305                     )
306                     iface, suite_id, suite_tag = get_iface_and_suite_ids(
307                         out_filename
308                     )
309                     # The next replace is probably a noop, but it is safer to
310                     # maintain the same structure as for other edits.
311                     out_prolog = replace_defensively(
312                         out_prolog, old_suite_tag, suite_tag, 1,
313                         f"Perf suite tag {old_suite_tag} should appear once.",
314                         in_filename
315                     )
316                     check_suite_tag(suite_tag, out_prolog)
317                     # TODO: Reorder loops so suite_id is finalized sooner.
318                     testcase = Testcase.default(suite_id)
319                     with open(out_filename, u"wt") as file_out:
320                         file_out.write(out_prolog)
321                         add_default_testcases(
322                             testcase, iface, suite_id, file_out, kwargs_list
323                         )
324                 continue
325             for driver in Constants.NIC_NAME_TO_DRIVER[nic_name]:
326                 out_filename = replace_defensively(
327                     tmp2_filename, old_suite_id,
328                     Constants.NIC_DRIVER_TO_SUITE_PREFIX[driver] + old_suite_id,
329                     1, u"Error adding driver prefix.", in_filename
330                 )
331                 out_prolog = replace_defensively(
332                     tmp2_prolog, u"vfio-pci", driver, 1,
333                     u"Driver name should appear once.", in_filename
334                 )
335                 out_prolog = replace_defensively(
336                     out_prolog, Constants.NIC_DRIVER_TO_TAG[u"vfio-pci"],
337                     Constants.NIC_DRIVER_TO_TAG[driver], 1,
338                     u"Driver tag should appear once.", in_filename
339                 )
340                 out_prolog = replace_defensively(
341                     out_prolog, Constants.NIC_DRIVER_TO_PLUGINS[u"vfio-pci"],
342                     Constants.NIC_DRIVER_TO_PLUGINS[driver], 1,
343                     u"Driver plugin should appear once.", in_filename
344                 )
345                 out_prolog = replace_defensively(
346                     out_prolog, Constants.NIC_DRIVER_TO_VFS[u"vfio-pci"],
347                     Constants.NIC_DRIVER_TO_VFS[driver], 1,
348                     u"NIC VFs argument should appear once.", in_filename
349                 )
350                 iface, suite_id, suite_tag = get_iface_and_suite_ids(
351                     out_filename
352                 )
353                 # The next replace is probably a noop, but it is safer to
354                 # maintain the same structure as for other edits.
355                 out_prolog = replace_defensively(
356                     out_prolog, old_suite_tag, suite_tag, 1,
357                     f"Perf suite tag {old_suite_tag} should appear once.",
358                     in_filename
359                 )
360                 check_suite_tag(suite_tag, out_prolog)
361                 # TODO: Reorder loops so suite_id is finalized sooner.
362                 testcase = Testcase.default(suite_id)
363                 with open(out_filename, u"wt") as file_out:
364                     file_out.write(out_prolog)
365                     add_default_testcases(
366                         testcase, iface, suite_id, file_out, kwargs_list
367                     )
368
369
370 def write_reconf_files(in_filename, in_prolog, kwargs_list):
371     """Using given filename and prolog, write all generated reconf suites.
372
373     Use this for suite type reconf, as its local template
374     is incompatible with mrr/ndrpdr/soak local template,
375     while test cases are compatible.
376
377     :param in_filename: Template filename to derive real filenames from.
378     :param in_prolog: Template content to derive real content from.
379     :param kwargs_list: List of kwargs for add_testcase.
380     :type in_filename: str
381     :type in_prolog: str
382     :type kwargs_list: list of dict
383     """
384     _, suite_id, _ = get_iface_and_suite_ids(in_filename)
385     testcase = Testcase.default(suite_id)
386     for nic_name in Constants.NIC_NAME_TO_CODE:
387         tmp_filename = replace_defensively(
388             in_filename, u"10ge2p1x710",
389             Constants.NIC_NAME_TO_CODE[nic_name], 1,
390             u"File name should contain NIC code once.", in_filename
391         )
392         tmp_prolog = replace_defensively(
393             in_prolog, u"Intel-X710", nic_name, 2,
394             u"NIC name should appear twice (tag and variable).",
395             in_filename
396         )
397         if tmp_prolog.count(u"HW_") == 2:
398             # TODO CSIT-1481: Crypto HW should be read
399             #      from topology file instead.
400             if nic_name in Constants.NIC_NAME_TO_CRYPTO_HW.keys():
401                 tmp_prolog = replace_defensively(
402                     tmp_prolog, u"HW_DH895xcc",
403                     Constants.NIC_NAME_TO_CRYPTO_HW[nic_name], 1,
404                     u"HW crypto name should appear.", in_filename
405                 )
406         iface, old_suite_id, old_suite_tag = get_iface_and_suite_ids(
407             tmp_filename
408         )
409         for driver in Constants.NIC_NAME_TO_DRIVER[nic_name]:
410             out_filename = replace_defensively(
411                 tmp_filename, old_suite_id,
412                 Constants.NIC_DRIVER_TO_SUITE_PREFIX[driver] + old_suite_id,
413                 1, u"Error adding driver prefix.", in_filename
414             )
415             out_prolog = replace_defensively(
416                 tmp_prolog, u"vfio-pci", driver, 1,
417                 u"Driver name should appear once.", in_filename
418             )
419             out_prolog = replace_defensively(
420                 out_prolog, Constants.NIC_DRIVER_TO_TAG[u"vfio-pci"],
421                 Constants.NIC_DRIVER_TO_TAG[driver], 1,
422                 u"Driver tag should appear once.", in_filename
423             )
424             out_prolog = replace_defensively(
425                 out_prolog, Constants.NIC_DRIVER_TO_PLUGINS[u"vfio-pci"],
426                 Constants.NIC_DRIVER_TO_PLUGINS[driver], 1,
427                 u"Driver plugin should appear once.", in_filename
428             )
429             out_prolog = replace_defensively(
430                 out_prolog, Constants.NIC_DRIVER_TO_VFS[u"vfio-pci"],
431                 Constants.NIC_DRIVER_TO_VFS[driver], 1,
432                 u"NIC VFs argument should appear once.", in_filename
433             )
434             iface, suite_id, suite_tag = get_iface_and_suite_ids(out_filename)
435             out_prolog = replace_defensively(
436                 out_prolog, old_suite_tag, suite_tag, 1,
437                 u"Perf suite tag should appear once.", in_filename
438             )
439             check_suite_tag(suite_tag, out_prolog)
440             # TODO: Reorder loops so suite_id is finalized sooner.
441             testcase = Testcase.default(suite_id)
442             with open(out_filename, u"wt") as file_out:
443                 file_out.write(out_prolog)
444                 add_default_testcases(
445                     testcase, iface, suite_id, file_out, kwargs_list
446                 )
447
448
449 def write_tcp_files(in_filename, in_prolog, kwargs_list):
450     """Using given filename and prolog, write all generated tcp suites.
451
452     TODO: Suport drivers.
453
454     :param in_filename: Template filename to derive real filenames from.
455     :param in_prolog: Template content to derive real content from.
456     :param kwargs_list: List of kwargs for add_default_testcase.
457     :type in_filename: str
458     :type in_prolog: str
459     :type kwargs_list: list of dict
460     """
461     # TODO: Generate rps from cps? There are subtle differences.
462     _, suite_id, suite_tag = get_iface_and_suite_ids(in_filename)
463     testcase = Testcase.tcp(suite_id)
464     for nic_name in Constants.NIC_NAME_TO_CODE:
465         out_filename = replace_defensively(
466             in_filename, u"10ge2p1x710",
467             Constants.NIC_NAME_TO_CODE[nic_name], 1,
468             u"File name should contain NIC code once.", in_filename
469         )
470         out_prolog = replace_defensively(
471             in_prolog, u"Intel-X710", nic_name, 2,
472             u"NIC name should appear twice (tag and variable).",
473             in_filename
474         )
475         check_suite_tag(suite_tag, out_prolog)
476         with open(out_filename, u"wt") as file_out:
477             file_out.write(out_prolog)
478             add_tcp_testcases(testcase, file_out, kwargs_list)
479
480
481 def write_iperf3_files(in_filename, in_prolog, kwargs_list):
482     """Using given filename and prolog, write all generated iperf3 suites.
483
484     :param in_filename: Template filename to derive real filenames from.
485     :param in_prolog: Template content to derive real content from.
486     :param kwargs_list: List of kwargs for add_default_testcase.
487     :type in_filename: str
488     :type in_prolog: str
489     :type kwargs_list: list of dict
490     """
491     _, suite_id, suite_tag = get_iface_and_suite_ids(in_filename)
492     testcase = Testcase.iperf3(suite_id)
493     out_filename = replace_defensively(
494         in_filename, u"10ge2p1x710",
495         Constants.NIC_NAME_TO_CODE[u"Intel-X710"], 1,
496         u"File name should contain NIC code once.", in_filename
497     )
498     out_prolog = replace_defensively(
499         in_prolog, u"Intel-X710", u"Intel-X710", 2,
500         u"NIC name should appear twice (tag and variable).",
501         in_filename
502     )
503     check_suite_tag(suite_tag, out_prolog)
504     with open(out_filename, u"wt") as file_out:
505         file_out.write(out_prolog)
506         add_iperf3_testcases(testcase, file_out, kwargs_list)
507
508
509 def write_trex_files(in_filename, in_prolog, kwargs_list):
510     """Using given filename and prolog, write all generated trex suites.
511
512     :param in_filename: Template filename to derive real filenames from.
513     :param in_prolog: Template content to derive real content from.
514     :param kwargs_list: List of kwargs for add_trex_testcase.
515     :type in_filename: str
516     :type in_prolog: str
517     :type kwargs_list: list of dict
518     """
519     for suite_type in Constants.PERF_TYPE_TO_KEYWORD:
520         tmp_filename = replace_defensively(
521             in_filename, u"ndrpdr", suite_type, 1,
522             u"File name should contain suite type once.", in_filename
523         )
524         tmp_prolog = replace_defensively(
525             in_prolog, u"ndrpdr".upper(), suite_type.upper(), 1,
526             u"Suite type should appear once in uppercase (as tag).",
527             in_filename
528         )
529         tmp_prolog = replace_defensively(
530             tmp_prolog,
531             u"Find NDR and PDR intervals using optimized search",
532             Constants.PERF_TYPE_TO_KEYWORD[suite_type], 1,
533             u"Main search keyword should appear once in suite.",
534             in_filename
535         )
536         tmp_prolog = replace_defensively(
537             tmp_prolog,
538             Constants.PERF_TYPE_TO_SUITE_DOC_VER[u"ndrpdr"],
539             Constants.PERF_TYPE_TO_SUITE_DOC_VER[suite_type],
540             1, u"Exact suite type doc not found.", in_filename
541         )
542         tmp_prolog = replace_defensively(
543             tmp_prolog,
544             Constants.PERF_TYPE_TO_TEMPLATE_DOC_VER[u"ndrpdr"],
545             Constants.PERF_TYPE_TO_TEMPLATE_DOC_VER[suite_type],
546             1, u"Exact template type doc not found.", in_filename
547         )
548         _, suite_id, suite_tag = get_iface_and_suite_ids(tmp_filename)
549         testcase = Testcase.trex(suite_id)
550         for nic_name in Constants.NIC_NAME_TO_CODE:
551             out_filename = replace_defensively(
552                 tmp_filename, u"10ge2p1x710",
553                 Constants.NIC_NAME_TO_CODE[nic_name], 1,
554                 u"File name should contain NIC code once.", in_filename
555             )
556             out_prolog = replace_defensively(
557                 tmp_prolog, u"Intel-X710", nic_name, 2,
558                 u"NIC name should appear twice (tag and variable).",
559                 in_filename
560             )
561             check_suite_tag(suite_tag, out_prolog)
562             with open(out_filename, u"wt") as file_out:
563                 file_out.write(out_prolog)
564                 add_trex_testcases(testcase, suite_id, file_out, kwargs_list)
565
566
567 def write_device_files(in_filename, in_prolog, kwargs_list):
568     """Using given filename and prolog, write all generated suites.
569
570     :param in_filename: Template filename to derive real filenames from.
571     :param in_prolog: Template content to derive real content from.
572     :param kwargs_list: List of kwargs for add_default_testcase.
573     :type in_filename: str
574     :type in_prolog: str
575     :type kwargs_list: list of dict
576     """
577     for suite_type in Constants.DEVICE_TYPE_TO_KEYWORD:
578         tmp_filename = replace_defensively(
579             in_filename, u"scapy", suite_type, 1,
580             u"File name should contain suite type once.", in_filename
581         )
582         _, suite_id, _ = get_iface_and_suite_ids(tmp_filename)
583         testcase = Testcase.default(suite_id)
584         for nic_name in Constants.NIC_NAME_TO_CODE:
585             tmp2_filename = replace_defensively(
586                 tmp_filename, u"10ge2p1x710",
587                 Constants.NIC_NAME_TO_CODE[nic_name], 1,
588                 u"File name should contain NIC code once.", in_filename
589             )
590             tmp2_prolog = replace_defensively(
591                 in_prolog, u"Intel-X710", nic_name, 2,
592                 u"NIC name should appear twice (tag and variable).",
593                 in_filename
594             )
595             iface, old_suite_id, _ = get_iface_and_suite_ids(
596                 tmp2_filename
597             )
598             for driver in Constants.NIC_NAME_TO_DRIVER[nic_name]:
599                 out_filename = replace_defensively(
600                     tmp2_filename, old_suite_id,
601                     Constants.NIC_DRIVER_TO_SUITE_PREFIX[driver] + old_suite_id,
602                     1, u"Error adding driver prefix.", in_filename
603                 )
604                 out_prolog = replace_defensively(
605                     tmp2_prolog, u"vfio-pci", driver, 1,
606                     u"Driver name should appear once.", in_filename
607                 )
608                 out_prolog = replace_defensively(
609                     out_prolog, Constants.NIC_DRIVER_TO_TAG[u"vfio-pci"],
610                     Constants.NIC_DRIVER_TO_TAG[driver], 1,
611                     u"Driver tag should appear once.", in_filename
612                 )
613                 out_prolog = replace_defensively(
614                     out_prolog, Constants.NIC_DRIVER_TO_PLUGINS[u"vfio-pci"],
615                     Constants.NIC_DRIVER_TO_PLUGINS[driver], 1,
616                     u"Driver plugin should appear once.", in_filename
617                 )
618                 out_prolog = replace_defensively(
619                     out_prolog, Constants.NIC_DRIVER_TO_VFS[u"vfio-pci"],
620                     Constants.NIC_DRIVER_TO_VFS[driver], 1,
621                     u"NIC VFs argument should appear once.", in_filename
622                 )
623                 iface, suite_id, suite_tag = get_iface_and_suite_ids(
624                     out_filename
625                 )
626                 check_suite_tag(suite_tag, out_prolog)
627                 # TODO: Reorder loops so suite_id is finalized sooner.
628                 testcase = Testcase.default(suite_id)
629                 with open(out_filename, u"wt") as file_out:
630                     file_out.write(out_prolog)
631                     add_default_testcases(
632                         testcase, iface, suite_id, file_out, kwargs_list
633                     )
634
635
636 class Regenerator:
637     """Class containing file generating methods."""
638
639     def __init__(self, quiet=True):
640         """Initialize the instance.
641
642         :param quiet: Reduce log prints (to stderr) when True (default).
643         :type quiet: boolean
644         """
645         self.quiet = quiet
646
647     def regenerate_glob(self, pattern, protocol=u"ip4"):
648         """Regenerate files matching glob pattern based on arguments.
649
650         In the current working directory, find all files matching
651         the glob pattern. Use testcase template to regenerate test cases
652         according to suffix, governed by protocol, autonumbering them.
653         Also generate suites for other NICs and drivers.
654
655         Log-like prints are emitted to sys.stderr.
656
657         :param pattern: Glob pattern to select files. Example: *-ndrpdr.robot
658         :param protocol: String determining minimal frame size. Default: "ip4"
659         :type pattern: str
660         :type protocol: str
661         :raises RuntimeError: If invalid source suite is encountered.
662         """
663         if not self.quiet:
664             print(f"Regenerator starts at {getcwd()}", file=sys.stderr)
665
666         min_frame_size = PROTOCOL_TO_MIN_FRAME_SIZE[protocol]
667         default_kwargs_list = [
668             {u"frame_size": min_frame_size, u"phy_cores": 1},
669             {u"frame_size": min_frame_size, u"phy_cores": 2},
670             {u"frame_size": min_frame_size, u"phy_cores": 4},
671             {u"frame_size": 1518, u"phy_cores": 1},
672             {u"frame_size": 1518, u"phy_cores": 2},
673             {u"frame_size": 1518, u"phy_cores": 4},
674             {u"frame_size": 9000, u"phy_cores": 1},
675             {u"frame_size": 9000, u"phy_cores": 2},
676             {u"frame_size": 9000, u"phy_cores": 4},
677             {u"frame_size": u"IMIX_v4_1", u"phy_cores": 1},
678             {u"frame_size": u"IMIX_v4_1", u"phy_cores": 2},
679             {u"frame_size": u"IMIX_v4_1", u"phy_cores": 4}
680         ]
681         hs_bps_kwargs_list = [
682             {u"frame_size": 1460, u"phy_cores": 1},
683         ]
684         hs_quic_kwargs_list = [
685             {u"frame_size": 1280, u"phy_cores": 1},
686         ]
687         iperf3_kwargs_list = [
688             {u"frame_size": 128000, u"phy_cores": 1},
689             {u"frame_size": 128000, u"phy_cores": 2},
690             {u"frame_size": 128000, u"phy_cores": 4}
691         ]
692         # List for tests with one dataplane core
693         # (and variable number of other cores).
694         dp1_kwargs_list = [
695             {u"frame_size": min_frame_size, u"phy_cores": 2},
696             {u"frame_size": min_frame_size, u"phy_cores": 3},
697             {u"frame_size": min_frame_size, u"phy_cores": 4},
698             {u"frame_size": 1518, u"phy_cores": 2},
699             {u"frame_size": 1518, u"phy_cores": 3},
700             {u"frame_size": 1518, u"phy_cores": 4},
701             {u"frame_size": 9000, u"phy_cores": 2},
702             {u"frame_size": 9000, u"phy_cores": 3},
703             {u"frame_size": 9000, u"phy_cores": 4},
704             {u"frame_size": u"IMIX_v4_1", u"phy_cores": 2},
705             {u"frame_size": u"IMIX_v4_1", u"phy_cores": 3},
706             {u"frame_size": u"IMIX_v4_1", u"phy_cores": 4}
707         ]
708
709         http_kwargs_list = [
710             {u"frame_size": 0, u"phy_cores": 1},
711             {u"frame_size": 0, u"phy_cores": 2},
712             {u"frame_size": 64, u"phy_cores": 1},
713             {u"frame_size": 64, u"phy_cores": 2},
714             {u"frame_size": 1024, u"phy_cores": 1},
715             {u"frame_size": 1024, u"phy_cores": 2},
716             {u"frame_size": 2048, u"phy_cores": 1},
717             {u"frame_size": 2048, u"phy_cores": 2}
718         ]
719
720         device_kwargs_list = [
721             {u"frame_size": min_frame_size, u"phy_cores": 0}
722         ]
723
724         trex_kwargs_list = [
725             {u"frame_size": min_frame_size},
726             {u"frame_size": 1518},
727             {u"frame_size": 9000},
728             {u"frame_size": u"IMIX_v4_1"}
729         ]
730
731         for in_filename in glob(pattern):
732             if not self.quiet:
733                 print(
734                     u"Regenerating in_filename:", in_filename, file=sys.stderr
735                 )
736             iface, _, _ = get_iface_and_suite_ids(in_filename)
737             if not iface.endswith(u"10ge2p1x710"):
738                 raise RuntimeError(
739                     f"Error in {in_filename}: non-primary NIC found."
740                 )
741             for prefix in Constants.FORBIDDEN_SUITE_PREFIX_LIST:
742                 if prefix in in_filename:
743                     raise RuntimeError(
744                         f"Error in {in_filename}: non-primary driver found."
745                     )
746             with open(in_filename, u"rt") as file_in:
747                 in_prolog = u"".join(
748                     file_in.read().partition(u"*** Test Cases ***")[:-1]
749                 )
750             if "-tg" in in_filename:
751                 write_trex_files(in_filename, in_prolog, trex_kwargs_list)
752                 continue
753             if in_filename.endswith(u"-ndrpdr.robot"):
754                 if u"scheduler" in in_filename:
755                     write_default_files(
756                         in_filename, in_prolog, dp1_kwargs_list
757                     )
758                 else:
759                     write_default_files(
760                         in_filename, in_prolog, default_kwargs_list
761                     )
762             elif in_filename.endswith(u"-reconf.robot"):
763                 write_reconf_files(in_filename, in_prolog, default_kwargs_list)
764             elif in_filename.endswith(u"-rps.robot") \
765                     or in_filename.endswith(u"-cps.robot"):
766                 write_tcp_files(in_filename, in_prolog, http_kwargs_list)
767             elif in_filename.endswith(u"-bps.robot"):
768                 hoststack_kwargs_list = \
769                     hs_quic_kwargs_list if u"quic" in in_filename \
770                     else hs_bps_kwargs_list
771                 write_tcp_files(in_filename, in_prolog, hoststack_kwargs_list)
772             elif in_filename.endswith(u"-iperf3-mrr.robot"):
773                 write_iperf3_files(in_filename, in_prolog, iperf3_kwargs_list)
774             elif in_filename.endswith(u"-scapy.robot"):
775                 write_device_files(in_filename, in_prolog, device_kwargs_list)
776             else:
777                 raise RuntimeError(
778                     f"Error in {in_filename}: non-primary suite type found."
779                 )
780         if not self.quiet:
781             print(u"Regenerator ends.", file=sys.stderr)
782         print(file=sys.stderr)  # To make autogen check output more readable.