1 # Copyright (c) 2020 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 for kwargs in tc_kwargs_list:
134 # TODO: Is there a better way to disable some combinations?
136 if kwargs[u"frame_size"] == 9000:
137 if u"vic1227" in iface:
138 # Not supported in HW.
140 if u"vic1385" in iface:
141 # Not supported in HW.
143 if u"ipsec" in suite_id:
144 # IPsec code does not support chained buffers.
145 # Tracked by Jira ticket VPP-1207.
147 if u"-16vm2t-" in suite_id or u"-16dcr2t-" in suite_id:
148 if kwargs[u"phy_cores"] > 3:
149 # CSIT lab only has 28 (physical) core processors,
150 # so these test would fail when attempting to assign cores.
152 if u"-24vm1t-" in suite_id or u"-24dcr1t-" in suite_id:
153 if kwargs[u"phy_cores"] > 3:
154 # CSIT lab only has 28 (physical) core processors,
155 # so these test would fail when attempting to assign cores.
157 if u"soak" in suite_id:
158 # Soak test take too long, do not risk other than tc01.
159 if kwargs[u"phy_cores"] != 1:
161 if kwargs[u"frame_size"] not in MIN_FRAME_SIZE_VALUES:
164 file_out.write(testcase.generate(**kwargs))
167 def add_tcp_testcases(testcase, file_out, tc_kwargs_list):
168 """Add TCP testcases to file.
170 :param testcase: Testcase class.
171 :param file_out: File to write testcases to.
172 :param tc_kwargs_list: Key-value pairs used to construct testcases.
173 :type testcase: Testcase
175 :type tc_kwargs_list: dict
177 for kwargs in tc_kwargs_list:
178 file_out.write(testcase.generate(**kwargs))
181 def write_default_files(in_filename, in_prolog, kwargs_list):
182 """Using given filename and prolog, write all generated suites.
184 :param in_filename: Template filename to derive real filenames from.
185 :param in_prolog: Template content to derive real content from.
186 :param kwargs_list: List of kwargs for add_default_testcase.
187 :type in_filename: str
189 :type kwargs_list: list of dict
191 for suite_type in Constants.PERF_TYPE_TO_KEYWORD:
192 tmp_filename = replace_defensively(
193 in_filename, u"ndrpdr", suite_type, 1,
194 u"File name should contain suite type once.", in_filename
196 tmp_prolog = replace_defensively(
197 in_prolog, u"ndrpdr".upper(), suite_type.upper(), 1,
198 u"Suite type should appear once in uppercase (as tag).",
201 tmp_prolog = replace_defensively(
203 u"Find NDR and PDR intervals using optimized search",
204 Constants.PERF_TYPE_TO_KEYWORD[suite_type], 1,
205 u"Main search keyword should appear once in suite.",
208 tmp_prolog = replace_defensively(
210 Constants.PERF_TYPE_TO_SUITE_DOC_VER[u"ndrpdr"],
211 Constants.PERF_TYPE_TO_SUITE_DOC_VER[suite_type],
212 1, u"Exact suite type doc not found.", in_filename
214 tmp_prolog = replace_defensively(
216 Constants.PERF_TYPE_TO_TEMPLATE_DOC_VER[u"ndrpdr"],
217 Constants.PERF_TYPE_TO_TEMPLATE_DOC_VER[suite_type],
218 1, u"Exact template type doc not found.", in_filename
220 _, suite_id, _ = get_iface_and_suite_ids(tmp_filename)
221 testcase = Testcase.default(suite_id)
222 for nic_name in Constants.NIC_NAME_TO_CODE:
223 tmp2_filename = replace_defensively(
224 tmp_filename, u"10ge2p1x710",
225 Constants.NIC_NAME_TO_CODE[nic_name], 1,
226 u"File name should contain NIC code once.", in_filename
228 tmp2_prolog = replace_defensively(
229 tmp_prolog, u"Intel-X710", nic_name, 2,
230 u"NIC name should appear twice (tag and variable).",
233 if tmp2_prolog.count(u"HW_") == 2:
234 # TODO CSIT-1481: Crypto HW should be read
235 # from topology file instead.
236 if nic_name in Constants.NIC_NAME_TO_CRYPTO_HW:
237 tmp2_prolog = replace_defensively(
238 tmp2_prolog, u"HW_DH895xcc",
239 Constants.NIC_NAME_TO_CRYPTO_HW[nic_name], 1,
240 u"HW crypto name should appear.", in_filename
242 iface, old_suite_id, old_suite_tag = get_iface_and_suite_ids(
245 if u"DPDK" in in_prolog:
246 for driver in Constants.DPDK_NIC_NAME_TO_DRIVER[nic_name]:
247 out_filename = replace_defensively(
248 tmp2_filename, old_suite_id,
249 Constants.DPDK_NIC_DRIVER_TO_SUITE_PREFIX[driver] \
251 1, u"Error adding driver prefix.", in_filename
253 out_prolog = replace_defensively(
254 tmp2_prolog, u"vfio-pci", driver, 1,
255 u"Driver name should appear once.", in_filename
257 out_prolog = replace_defensively(
259 Constants.DPDK_NIC_DRIVER_TO_TAG[u"vfio-pci"],
260 Constants.DPDK_NIC_DRIVER_TO_TAG[driver], 1,
261 u"Driver tag should appear once.", in_filename
263 iface, suite_id, suite_tag = get_iface_and_suite_ids(
266 # The next replace is probably a noop, but it is safer to
267 # maintain the same structure as for other edits.
268 out_prolog = replace_defensively(
269 out_prolog, old_suite_tag, suite_tag, 1,
270 f"Perf suite tag {old_suite_tag} should appear once.",
273 check_suite_tag(suite_tag, out_prolog)
274 # TODO: Reorder loops so suite_id is finalized sooner.
275 testcase = Testcase.default(suite_id)
276 with open(out_filename, u"wt") as file_out:
277 file_out.write(out_prolog)
278 add_default_testcases(
279 testcase, iface, suite_id, file_out, kwargs_list
282 for driver in Constants.NIC_NAME_TO_DRIVER[nic_name]:
283 out_filename = replace_defensively(
284 tmp2_filename, old_suite_id,
285 Constants.NIC_DRIVER_TO_SUITE_PREFIX[driver] + old_suite_id,
286 1, u"Error adding driver prefix.", in_filename
288 out_prolog = replace_defensively(
289 tmp2_prolog, u"vfio-pci", driver, 1,
290 u"Driver name should appear once.", in_filename
292 out_prolog = replace_defensively(
293 out_prolog, Constants.NIC_DRIVER_TO_TAG[u"vfio-pci"],
294 Constants.NIC_DRIVER_TO_TAG[driver], 1,
295 u"Driver tag should appear once.", in_filename
297 out_prolog = replace_defensively(
298 out_prolog, Constants.NIC_DRIVER_TO_PLUGINS[u"vfio-pci"],
299 Constants.NIC_DRIVER_TO_PLUGINS[driver], 1,
300 u"Driver plugin should appear once.", in_filename
302 out_prolog = replace_defensively(
303 out_prolog, Constants.NIC_DRIVER_TO_VFS[u"vfio-pci"],
304 Constants.NIC_DRIVER_TO_VFS[driver], 1,
305 u"NIC VFs argument should appear once.", in_filename
307 iface, suite_id, suite_tag = get_iface_and_suite_ids(
310 # The next replace is probably a noop, but it is safer to
311 # maintain the same structure as for other edits.
312 out_prolog = replace_defensively(
313 out_prolog, old_suite_tag, suite_tag, 1,
314 f"Perf suite tag {old_suite_tag} should appear once.",
317 check_suite_tag(suite_tag, out_prolog)
318 # TODO: Reorder loops so suite_id is finalized sooner.
319 testcase = Testcase.default(suite_id)
320 with open(out_filename, u"wt") as file_out:
321 file_out.write(out_prolog)
322 add_default_testcases(
323 testcase, iface, suite_id, file_out, kwargs_list
327 def write_reconf_files(in_filename, in_prolog, kwargs_list):
328 """Using given filename and prolog, write all generated reconf suites.
330 Use this for suite type reconf, as its local template
331 is incompatible with mrr/ndrpdr/soak local template,
332 while test cases are compatible.
334 :param in_filename: Template filename to derive real filenames from.
335 :param in_prolog: Template content to derive real content from.
336 :param kwargs_list: List of kwargs for add_testcase.
337 :type in_filename: str
339 :type kwargs_list: list of dict
341 _, suite_id, _ = get_iface_and_suite_ids(in_filename)
342 testcase = Testcase.default(suite_id)
343 for nic_name in Constants.NIC_NAME_TO_CODE:
344 tmp_filename = replace_defensively(
345 in_filename, u"10ge2p1x710",
346 Constants.NIC_NAME_TO_CODE[nic_name], 1,
347 u"File name should contain NIC code once.", in_filename
349 tmp_prolog = replace_defensively(
350 in_prolog, u"Intel-X710", nic_name, 2,
351 u"NIC name should appear twice (tag and variable).",
354 if tmp_prolog.count(u"HW_") == 2:
355 # TODO CSIT-1481: Crypto HW should be read
356 # from topology file instead.
357 if nic_name in Constants.NIC_NAME_TO_CRYPTO_HW.keys():
358 tmp_prolog = replace_defensively(
359 tmp_prolog, u"HW_DH895xcc",
360 Constants.NIC_NAME_TO_CRYPTO_HW[nic_name], 1,
361 u"HW crypto name should appear.", in_filename
363 iface, old_suite_id, old_suite_tag = get_iface_and_suite_ids(
366 for driver in Constants.NIC_NAME_TO_DRIVER[nic_name]:
367 out_filename = replace_defensively(
368 tmp_filename, old_suite_id,
369 Constants.NIC_DRIVER_TO_SUITE_PREFIX[driver] + old_suite_id,
370 1, u"Error adding driver prefix.", in_filename
372 out_prolog = replace_defensively(
373 tmp_prolog, u"vfio-pci", driver, 1,
374 u"Driver name should appear once.", in_filename
376 out_prolog = replace_defensively(
377 out_prolog, Constants.NIC_DRIVER_TO_TAG[u"vfio-pci"],
378 Constants.NIC_DRIVER_TO_TAG[driver], 1,
379 u"Driver tag should appear once.", in_filename
381 out_prolog = replace_defensively(
382 out_prolog, Constants.NIC_DRIVER_TO_PLUGINS[u"vfio-pci"],
383 Constants.NIC_DRIVER_TO_PLUGINS[driver], 1,
384 u"Driver plugin should appear once.", in_filename
386 out_prolog = replace_defensively(
387 out_prolog, Constants.NIC_DRIVER_TO_VFS[u"vfio-pci"],
388 Constants.NIC_DRIVER_TO_VFS[driver], 1,
389 u"NIC VFs argument should appear once.", in_filename
391 iface, suite_id, suite_tag = get_iface_and_suite_ids(out_filename)
392 out_prolog = replace_defensively(
393 out_prolog, old_suite_tag, suite_tag, 1,
394 u"Perf suite tag should appear once.", in_filename
396 check_suite_tag(suite_tag, out_prolog)
397 # TODO: Reorder loops so suite_id is finalized sooner.
398 testcase = Testcase.default(suite_id)
399 with open(out_filename, u"wt") as file_out:
400 file_out.write(out_prolog)
401 add_default_testcases(
402 testcase, iface, suite_id, file_out, kwargs_list
406 def write_tcp_files(in_filename, in_prolog, kwargs_list):
407 """Using given filename and prolog, write all generated tcp suites.
409 TODO: Suport drivers.
411 :param in_filename: Template filename to derive real filenames from.
412 :param in_prolog: Template content to derive real content from.
413 :param kwargs_list: List of kwargs for add_default_testcase.
414 :type in_filename: str
416 :type kwargs_list: list of dict
418 # TODO: Generate rps from cps? There are subtle differences.
419 _, suite_id, suite_tag = get_iface_and_suite_ids(in_filename)
420 testcase = Testcase.tcp(suite_id)
421 for nic_name in Constants.NIC_NAME_TO_CODE:
422 out_filename = replace_defensively(
423 in_filename, u"10ge2p1x710",
424 Constants.NIC_NAME_TO_CODE[nic_name], 1,
425 u"File name should contain NIC code once.", in_filename
427 out_prolog = replace_defensively(
428 in_prolog, u"Intel-X710", nic_name, 2,
429 u"NIC name should appear twice (tag and variable).",
432 check_suite_tag(suite_tag, out_prolog)
433 with open(out_filename, u"wt") as file_out:
434 file_out.write(out_prolog)
435 add_tcp_testcases(testcase, file_out, kwargs_list)
439 """Class containing file generating methods."""
441 def __init__(self, quiet=True):
442 """Initialize the instance.
444 :param quiet: Reduce log prints (to stderr) when True (default).
449 def regenerate_glob(self, pattern, protocol=u"ip4"):
450 """Regenerate files matching glob pattern based on arguments.
452 In the current working directory, find all files matching
453 the glob pattern. Use testcase template to regenerate test cases
454 according to suffix, governed by protocol, autonumbering them.
455 Also generate suites for other NICs and drivers.
457 Log-like prints are emitted to sys.stderr.
459 :param pattern: Glob pattern to select files. Example: *-ndrpdr.robot
460 :param protocol: String determining minimal frame size. Default: "ip4"
463 :raises RuntimeError: If invalid source suite is encountered.
466 print(f"Regenerator starts at {getcwd()}", file=sys.stderr)
468 min_frame_size = PROTOCOL_TO_MIN_FRAME_SIZE[protocol]
469 default_kwargs_list = [
470 {u"frame_size": min_frame_size, u"phy_cores": 1},
471 {u"frame_size": min_frame_size, u"phy_cores": 2},
472 {u"frame_size": min_frame_size, u"phy_cores": 4},
473 {u"frame_size": 1518, u"phy_cores": 1},
474 {u"frame_size": 1518, u"phy_cores": 2},
475 {u"frame_size": 1518, u"phy_cores": 4},
476 {u"frame_size": 9000, u"phy_cores": 1},
477 {u"frame_size": 9000, u"phy_cores": 2},
478 {u"frame_size": 9000, u"phy_cores": 4},
479 {u"frame_size": u"IMIX_v4_1", u"phy_cores": 1},
480 {u"frame_size": u"IMIX_v4_1", u"phy_cores": 2},
481 {u"frame_size": u"IMIX_v4_1", u"phy_cores": 4}
483 hs_bps_kwargs_list = [
484 {u"frame_size": 1460, u"phy_cores": 1},
486 hs_quic_kwargs_list = [
487 {u"frame_size": 1280, u"phy_cores": 1},
490 for in_filename in glob(pattern):
493 u"Regenerating in_filename:", in_filename, file=sys.stderr
495 iface, _, _ = get_iface_and_suite_ids(in_filename)
496 if not iface.endswith(u"10ge2p1x710"):
498 f"Error in {in_filename}: non-primary NIC found."
500 for prefix in Constants.FORBIDDEN_SUITE_PREFIX_LIST:
501 if prefix in in_filename:
503 f"Error in {in_filename}: non-primary driver found."
505 with open(in_filename, u"rt") as file_in:
506 in_prolog = u"".join(
507 file_in.read().partition(u"*** Test Cases ***")[:-1]
509 if in_filename.endswith(u"-ndrpdr.robot"):
510 write_default_files(in_filename, in_prolog, default_kwargs_list)
511 elif in_filename.endswith(u"-reconf.robot"):
512 write_reconf_files(in_filename, in_prolog, default_kwargs_list)
513 elif in_filename.endswith(u"-bps.robot"):
514 hoststack_kwargs_list = \
515 hs_quic_kwargs_list if u"quic" in in_filename \
516 else hs_bps_kwargs_list
517 write_tcp_files(in_filename, in_prolog, hoststack_kwargs_list)
520 f"Error in {in_filename}: non-primary suite type found."
523 print(u"Regenerator ends.", file=sys.stderr)
524 print(file=sys.stderr) # To make autogen check output more readable.