1 # Copyright (c) 2019 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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """Module defining utilities for test directory regeneration.
16 TODO: How can we check each suite id is unique,
17 when currently the suite generation is run on each directory separately?
27 from resources.libraries.python.Constants import Constants
28 from resources.libraries.python.autogen.Testcase import Testcase
31 PROTOCOL_TO_MIN_FRAME_SIZE = {
34 u"ethip4vxlan": 114, # What is the real minimum for latency stream?
37 MIN_FRAME_SIZE_VALUES = list(PROTOCOL_TO_MIN_FRAME_SIZE.values())
40 def replace_defensively(
41 whole, to_replace, replace_with, how_many, msg, in_filename):
42 """Replace substrings while checking the number of occurrences.
44 Return edited copy of the text. Assuming "whole" is really a string,
45 or something else with .replace not affecting it.
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.
55 :type replace_with: str
58 :type in_filename: str
59 :returns: The whole text after replacements are done.
61 :raises ValueError: If number of occurrences does not match.
63 found = whole.count(to_replace)
65 raise ValueError(f"{in_filename}: {msg}")
66 return whole.replace(to_replace, replace_with)
69 def get_iface_and_suite_ids(filename):
70 """Get NIC code, suite ID and suite tag.
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.
78 :param filename: Suite file.
80 :returns: NIC code, suite ID, suite tag.
81 :rtype: 3-tuple of str
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".", 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
96 def check_suite_tag(suite_tag, prolog):
97 """Verify suite tag occurres once in prolog.
99 Call this after all edits are done,
100 to confirm the (edited) suite tag still matches the (edited) suite name.
102 Currently, the edited suite tag is expect to be identical
103 to the primary suite tag, but having a function is more flexible.
105 The occurences are counted including "| " prefix,
106 to lower the chance to match a comment.
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.
112 :raises ValueError: If suite_tag not found exactly once.
114 found = prolog.count(u"| " + suite_tag)
116 raise ValueError(f"Suite tag found {found} times for {suite_tag}")
119 def add_default_testcases(testcase, iface, suite_id, file_out, tc_kwargs_list):
120 """Add default testcases to file.
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
131 :type tc_kwargs_list: dict
133 # We bump tc number in any case, so that future enables/disables
134 # do not affect the numbering of other test cases.
135 for num, kwargs in enumerate(tc_kwargs_list, start=1):
136 # TODO: Is there a better way to disable some combinations?
138 if kwargs[u"frame_size"] == 9000:
139 if u"vic1227" in iface:
140 # Not supported in HW.
142 if u"vic1385" in iface:
143 # Not supported in HW.
145 if u"ipsec" in suite_id:
146 # IPsec code does not support chained buffers.
147 # Tracked by Jira ticket VPP-1207.
149 if u"-16vm2t-" in suite_id or u"-16dcr2t-" in suite_id:
150 if kwargs[u"phy_cores"] > 3:
151 # CSIT lab only has 28 (physical) core processors,
152 # so these test would fail when attempting to assign cores.
154 if u"-24vm1t-" in suite_id or u"-24dcr1t-" in suite_id:
155 if kwargs[u"phy_cores"] > 3:
156 # CSIT lab only has 28 (physical) core processors,
157 # so these test would fail when attempting to assign cores.
159 if u"soak" in suite_id:
160 # Soak test take too long, do not risk other than tc01.
161 if kwargs[u"phy_cores"] != 1:
163 if kwargs[u"frame_size"] not in MIN_FRAME_SIZE_VALUES:
166 file_out.write(testcase.generate(num=num, **kwargs))
169 def add_tcp_testcases(testcase, file_out, tc_kwargs_list):
170 """Add TCP testcases to file.
172 :param testcase: Testcase class.
173 :param file_out: File to write testcases to.
174 :param tc_kwargs_list: Key-value pairs used to construct testcases.
175 :type testcase: Testcase
177 :type tc_kwargs_list: dict
179 for num, kwargs in enumerate(tc_kwargs_list, start=1):
180 file_out.write(testcase.generate(num=num, **kwargs))
183 def write_default_files(in_filename, in_prolog, kwargs_list):
184 """Using given filename and prolog, write all generated suites.
186 :param in_filename: Template filename to derive real filenames from.
187 :param in_prolog: Template content to derive real content from.
188 :param kwargs_list: List of kwargs for add_default_testcase.
189 :type in_filename: str
191 :type kwargs_list: list of dict
193 for suite_type in Constants.PERF_TYPE_TO_KEYWORD:
194 tmp_filename = replace_defensively(
195 in_filename, u"ndrpdr", suite_type, 1,
196 u"File name should contain suite type once.", in_filename
198 tmp_prolog = replace_defensively(
199 in_prolog, u"ndrpdr".upper(), suite_type.upper(), 1,
200 u"Suite type should appear once in uppercase (as tag).",
203 tmp_prolog = replace_defensively(
205 u"Find NDR and PDR intervals using optimized search",
206 Constants.PERF_TYPE_TO_KEYWORD[suite_type], 1,
207 u"Main search keyword should appear once in suite.",
210 tmp_prolog = replace_defensively(
212 Constants.PERF_TYPE_TO_SUITE_DOC_VER[u"ndrpdr"],
213 Constants.PERF_TYPE_TO_SUITE_DOC_VER[suite_type],
214 1, u"Exact suite type doc not found.", in_filename
216 tmp_prolog = replace_defensively(
218 Constants.PERF_TYPE_TO_TEMPLATE_DOC_VER[u"ndrpdr"],
219 Constants.PERF_TYPE_TO_TEMPLATE_DOC_VER[suite_type],
220 1, u"Exact template type doc not found.", in_filename
222 _, suite_id, _ = get_iface_and_suite_ids(tmp_filename)
223 testcase = Testcase.default(suite_id)
224 for nic_name in Constants.NIC_NAME_TO_CODE:
225 tmp2_filename = replace_defensively(
226 tmp_filename, u"10ge2p1x710",
227 Constants.NIC_NAME_TO_CODE[nic_name], 1,
228 u"File name should contain NIC code once.", in_filename
230 tmp2_prolog = replace_defensively(
231 tmp_prolog, u"Intel-X710", nic_name, 2,
232 u"NIC name should appear twice (tag and variable).",
235 if tmp2_prolog.count(u"HW_") == 2:
236 # TODO CSIT-1481: Crypto HW should be read
237 # from topology file instead.
238 if nic_name in Constants.NIC_NAME_TO_CRYPTO_HW:
239 tmp2_prolog = replace_defensively(
240 tmp2_prolog, u"HW_DH895xcc",
241 Constants.NIC_NAME_TO_CRYPTO_HW[nic_name], 1,
242 u"HW crypto name should appear.", in_filename
244 iface, old_suite_id, old_suite_tag = get_iface_and_suite_ids(
247 if u"DPDK" in in_prolog:
248 check_suite_tag(old_suite_tag, tmp2_prolog)
249 with open(tmp2_filename, u"wt") as file_out:
250 file_out.write(tmp2_prolog)
251 add_default_testcases(
252 testcase, iface, old_suite_id, file_out, kwargs_list
255 for driver in Constants.NIC_NAME_TO_DRIVER[nic_name]:
256 out_filename = replace_defensively(
257 tmp2_filename, old_suite_id,
258 Constants.NIC_DRIVER_TO_SUITE_PREFIX[driver] + old_suite_id,
259 1, u"Error adding driver prefix.", in_filename
261 out_prolog = replace_defensively(
262 tmp2_prolog, u"vfio-pci", driver, 1,
263 u"Driver name should appear once.", in_filename
265 out_prolog = replace_defensively(
266 out_prolog, Constants.NIC_DRIVER_TO_TAG[u"vfio-pci"],
267 Constants.NIC_DRIVER_TO_TAG[driver], 1,
268 u"Driver tag should appear once.", in_filename
270 out_prolog = replace_defensively(
271 out_prolog, Constants.NIC_DRIVER_TO_PLUGINS[u"vfio-pci"],
272 Constants.NIC_DRIVER_TO_PLUGINS[driver], 1,
273 u"Driver plugin should appear once.", in_filename
275 out_prolog = replace_defensively(
276 out_prolog, Constants.NIC_DRIVER_TO_SETUP_ARG[u"vfio-pci"],
277 Constants.NIC_DRIVER_TO_SETUP_ARG[driver], 1,
278 u"Perf setup argument should appear once.", in_filename
280 iface, suite_id, suite_tag = get_iface_and_suite_ids(
283 # The next replace is probably a noop, but it is safer to
284 # maintain the same structure as for other edits.
285 out_prolog = replace_defensively(
286 out_prolog, old_suite_tag, suite_tag, 1,
287 f"Perf suite tag {old_suite_tag} should appear once.",
290 check_suite_tag(suite_tag, out_prolog)
291 # TODO: Reorder loops so suite_id is finalized sooner.
292 testcase = Testcase.default(suite_id)
293 with open(out_filename, u"wt") as file_out:
294 file_out.write(out_prolog)
295 add_default_testcases(
296 testcase, iface, suite_id, file_out, kwargs_list
300 def write_reconf_files(in_filename, in_prolog, kwargs_list):
301 """Using given filename and prolog, write all generated reconf suites.
303 Use this for suite type reconf, as its local template
304 is incompatible with mrr/ndrpdr/soak local template,
305 while test cases are compatible.
307 :param in_filename: Template filename to derive real filenames from.
308 :param in_prolog: Template content to derive real content from.
309 :param kwargs_list: List of kwargs for add_testcase.
310 :type in_filename: str
312 :type kwargs_list: list of dict
314 _, suite_id, _ = get_iface_and_suite_ids(in_filename)
315 testcase = Testcase.default(suite_id)
316 for nic_name in Constants.NIC_NAME_TO_CODE:
317 tmp_filename = replace_defensively(
318 in_filename, u"10ge2p1x710",
319 Constants.NIC_NAME_TO_CODE[nic_name], 1,
320 u"File name should contain NIC code once.", in_filename
322 tmp_prolog = replace_defensively(
323 in_prolog, u"Intel-X710", nic_name, 2,
324 u"NIC name should appear twice (tag and variable).",
327 if tmp_prolog.count(u"HW_") == 2:
328 # TODO CSIT-1481: Crypto HW should be read
329 # from topology file instead.
330 if nic_name in Constants.NIC_NAME_TO_CRYPTO_HW.keys():
331 tmp_prolog = replace_defensively(
332 tmp_prolog, u"HW_DH895xcc",
333 Constants.NIC_NAME_TO_CRYPTO_HW[nic_name], 1,
334 u"HW crypto name should appear.", in_filename
336 iface, old_suite_id, old_suite_tag = get_iface_and_suite_ids(
339 for driver in Constants.NIC_NAME_TO_DRIVER[nic_name]:
340 out_filename = replace_defensively(
341 tmp_filename, old_suite_id,
342 Constants.NIC_DRIVER_TO_SUITE_PREFIX[driver] + old_suite_id,
343 1, u"Error adding driver prefix.", in_filename
345 out_prolog = replace_defensively(
346 tmp_prolog, u"vfio-pci", driver, 1,
347 u"Driver name should appear once.", in_filename
349 out_prolog = replace_defensively(
350 out_prolog, Constants.NIC_DRIVER_TO_TAG[u"vfio-pci"],
351 Constants.NIC_DRIVER_TO_TAG[driver], 1,
352 u"Driver tag should appear once.", in_filename
354 out_prolog = replace_defensively(
355 out_prolog, Constants.NIC_DRIVER_TO_PLUGINS[u"vfio-pci"],
356 Constants.NIC_DRIVER_TO_PLUGINS[driver], 1,
357 u"Driver plugin should appear once.", in_filename
359 out_prolog = replace_defensively(
360 out_prolog, Constants.NIC_DRIVER_TO_SETUP_ARG[u"vfio-pci"],
361 Constants.NIC_DRIVER_TO_SETUP_ARG[driver], 1,
362 u"Perf setup argument should appear once.", in_filename
364 iface, suite_id, suite_tag = get_iface_and_suite_ids(out_filename)
365 out_prolog = replace_defensively(
366 out_prolog, old_suite_tag, suite_tag, 1,
367 u"Perf suite tag should appear once.", in_filename
369 check_suite_tag(suite_tag, out_prolog)
370 # TODO: Reorder loops so suite_id is finalized sooner.
371 testcase = Testcase.default(suite_id)
372 with open(out_filename, u"wt") as file_out:
373 file_out.write(out_prolog)
374 add_default_testcases(
375 testcase, iface, suite_id, file_out, kwargs_list
379 def write_tcp_files(in_filename, in_prolog, kwargs_list):
380 """Using given filename and prolog, write all generated tcp suites.
382 TODO: Suport drivers.
384 :param in_filename: Template filename to derive real filenames from.
385 :param in_prolog: Template content to derive real content from.
386 :param kwargs_list: List of kwargs for add_default_testcase.
387 :type in_filename: str
389 :type kwargs_list: list of dict
391 # TODO: Generate rps from cps? There are subtle differences.
392 _, suite_id, suite_tag = get_iface_and_suite_ids(in_filename)
393 testcase = Testcase.tcp(suite_id)
394 for nic_name in Constants.NIC_NAME_TO_CODE:
395 out_filename = replace_defensively(
396 in_filename, u"10ge2p1x710",
397 Constants.NIC_NAME_TO_CODE[nic_name], 1,
398 u"File name should contain NIC code once.", in_filename
400 out_prolog = replace_defensively(
401 in_prolog, u"Intel-X710", nic_name, 2,
402 u"NIC name should appear twice (tag and variable).",
405 check_suite_tag(suite_tag, out_prolog)
406 with open(out_filename, u"wt") as file_out:
407 file_out.write(out_prolog)
408 add_tcp_testcases(testcase, file_out, kwargs_list)
412 """Class containing file generating methods."""
414 def __init__(self, quiet=True):
415 """Initialize the instance.
417 :param quiet: Reduce log prints (to stderr) when True (default).
422 def regenerate_glob(self, pattern, protocol=u"ip4"):
423 """Regenerate files matching glob pattern based on arguments.
425 In the current working directory, find all files matching
426 the glob pattern. Use testcase template to regenerate test cases
427 according to suffix, governed by protocol, autonumbering them.
428 Also generate suites for other NICs and drivers.
430 Log-like prints are emitted to sys.stderr.
432 :param pattern: Glob pattern to select files. Example: *-ndrpdr.robot
433 :param protocol: String determining minimal frame size. Default: "ip4"
436 :raises RuntimeError: If invalid source suite is encountered.
439 print(f"Regenerator starts at {getcwd()}", file=sys.stderr)
441 min_frame_size = PROTOCOL_TO_MIN_FRAME_SIZE[protocol]
442 default_kwargs_list = [
443 {u"frame_size": min_frame_size, u"phy_cores": 1, u"clients": 1,
444 u"streams": 1, u"bytes_str": u"1G"},
445 {u"frame_size": min_frame_size, u"phy_cores": 2, u"clients": 1,
446 u"streams": 1, u"bytes_str": u"1G"},
447 {u"frame_size": min_frame_size, u"phy_cores": 4, u"clients": 1,
448 u"streams": 1, u"bytes_str": u"1G"},
449 {u"frame_size": 1518, u"phy_cores": 1, u"clients": 1,
450 u"streams": 1, u"bytes_str": u"1G"},
451 {u"frame_size": 1518, u"phy_cores": 2, u"clients": 1,
452 u"streams": 1, u"bytes_str": u"1G"},
453 {u"frame_size": 1518, u"phy_cores": 4, u"clients": 1,
454 u"streams": 1, u"bytes_str": u"1G"},
455 {u"frame_size": 9000, u"phy_cores": 1, u"clients": 1,
456 u"streams": 1, u"bytes_str": u"1G"},
457 {u"frame_size": 9000, u"phy_cores": 2, u"clients": 1,
458 u"streams": 1, u"bytes_str": u"1G"},
459 {u"frame_size": 9000, u"phy_cores": 4, u"clients": 1,
460 u"streams": 1, u"bytes_str": u"1G"},
461 {u"frame_size": u"IMIX_v4_1", u"phy_cores": 1, u"clients": 1,
462 u"streams": 1, u"bytes_str": u"1G"},
463 {u"frame_size": u"IMIX_v4_1", u"phy_cores": 2, u"clients": 1,
464 u"streams": 1, u"bytes_str": u"1G"},
465 {u"frame_size": u"IMIX_v4_1", u"phy_cores": 4, u"clients": 1,
466 u"streams": 1, u"bytes_str": u"1G"}
468 hoststack_wrk_kwargs_list = [
469 {u"frame_size": 0, u"phy_cores": i, u"clients": 1,
470 u"streams": 1, u"bytes_str": u"1G"} for i in (1, 2, 4)
472 hoststack_iperf3_kwargs_list = [
473 {u"frame_size": 0, u"phy_cores": 1, u"clients": 1,
474 u"streams": 1, u"bytes_str": u"1G"},
475 {u"frame_size": 0, u"phy_cores": 1, u"clients": 1,
476 u"streams": 10, u"bytes_str": u"1G"},
478 hoststack_quic_kwargs_list = [
479 {u"frame_size": 0, u"phy_cores": 1, u"clients": 1,
480 u"streams": 1, u"bytes_str": u"100M"},
481 {u"frame_size": 0, u"phy_cores": 1, u"clients": 1,
482 u"streams": 10, u"bytes_str": u"100M"},
483 {u"frame_size": 0, u"phy_cores": 1, u"clients": 10,
484 u"streams": 1, u"bytes_str": u"100M"},
485 {u"frame_size": 0, u"phy_cores": 1, u"clients": 10,
486 u"streams": 10, u"bytes_str": u"100M"},
489 for in_filename in glob(pattern):
492 u"Regenerating in_filename:", in_filename, file=sys.stderr
494 iface, _, _ = get_iface_and_suite_ids(in_filename)
495 if not iface.endswith(u"10ge2p1x710"):
497 f"Error in {in_filename}: non-primary NIC found."
499 for prefix in Constants.FORBIDDEN_SUITE_PREFIX_LIST:
500 if prefix in in_filename:
502 f"Error in {in_filename}: non-primary driver found."
504 with open(in_filename, u"rt") as file_in:
505 in_prolog = u"".join(
506 file_in.read().partition(u"*** Test Cases ***")[:-1]
508 if in_filename.endswith(u"-ndrpdr.robot"):
509 write_default_files(in_filename, in_prolog, default_kwargs_list)
510 elif in_filename.endswith(u"-reconf.robot"):
511 write_reconf_files(in_filename, in_prolog, default_kwargs_list)
512 elif in_filename[-10:] in (u"-cps.robot", u"-rps.robot"):
513 write_tcp_files(in_filename, in_prolog,
514 hoststack_wrk_kwargs_list)
515 elif in_filename[-10:] in u"-bps.robot":
516 write_tcp_files(in_filename, in_prolog,
517 hoststack_iperf3_kwargs_list if u"iperf3"
518 in in_filename else hoststack_quic_kwargs_list)
521 f"Error in {in_filename}: non-primary suite type found."
524 print(u"Regenerator ends.", file=sys.stderr)
525 print(file=sys.stderr) # To make autogen check output more readable.