1 # Copyright (c) 2018 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."""
19 from .DefaultTestcase import DefaultTestcase
22 class Regenerator(object):
23 """Class containing file generating methods."""
25 def __init__(self, testcase_class=DefaultTestcase):
26 """Initialize Testcase class to use.
28 TODO: See the type doc for testcase_class?
29 It implies the design is wrong. Fix it.
30 Easiest: Hardcode Regenerator to use DefaultTestcase only.
32 :param testcase_class: Subclass of DefaultTestcase for generation.
33 Default: DefaultTestcase
34 :type testcase_class: subclass of DefaultTestcase accepting suite_id
36 self.testcase_class = testcase_class
38 def regenerate_glob(self, pattern, protocol="ip4", tc_kwargs_list=None):
39 """Regenerate files matching glob pattern based on arguments.
41 In the current working directory, find all files matching
42 the glob pattern. Use testcase template (from init) to regenerate
43 test cases, autonumbering them, taking arguments from list.
44 If the list is None, use default list, which depends on ip6 usage.
46 :param pattern: Glob pattern to select files. Example: *-ndrpdr.robot
47 :param is_ip6: Flag determining minimal frame size. Default: False
48 :param tc_kwargs_list: Arguments defining the testcases. Default: None
49 When None, default list is used.
50 List item is a dict, argument names are keys.
51 The initialized testcase_class should accept those, and "num".
52 DefaultTestcase accepts "framesize" and "phy_cores".
55 :type tc_kwargs_list: list of tuple or None
58 protocol_to_min_framesize = {
61 "vxlan+ip4": 114 # What is the real minimum for latency stream?
64 def get_iface_and_suite_id(filename):
65 """Get interface and suite ID.
67 :param filename: Suite file.
69 :returns: Interface ID, Suite ID.
72 dash_split = filename.split("-", 1)
73 if len(dash_split[0]) <= 4:
74 # It was something like "2n1l", we need one more split.
75 dash_split = dash_split[1].split("-", 1)
76 return dash_split[0], dash_split[1].split(".", 1)[0]
78 def add_testcase(testcase, iface, suite_id, file_out, num, **kwargs):
79 """Add testcase to file.
81 :param testcase: Testcase class.
82 :param iface: Interface.
83 :param suite_id: Suite ID.
84 :param file_out: File to write testcases to.
85 :param num: Testcase number.
86 :param kwargs: Key-value pairs used to construct testcase.
87 :type testcase: Testcase
93 :returns: Next testcase number.
96 # TODO: Is there a better way to disable some combinations?
97 if kwargs["framesize"] == 9000 and "vic1227" in iface:
98 # Not supported in HW.
100 elif kwargs["framesize"] == 9000 and "avf" in suite_id:
101 # Not supported by AVF driver.
102 # https://git.fd.io/vpp/tree/src/plugins/avf/README.md
104 elif ("soak" in suite_id and
105 (kwargs["phy_cores"] != 1
106 or kwargs["framesize"] in (1518, 9000, "IMIX_v4_1"))):
107 # Soak test take too long, do not risk other than tc01.
110 file_out.write(testcase.generate(num=num, **kwargs))
113 def add_testcases(testcase, iface, suite_id, file_out, tc_kwargs_list):
114 """Add testcases to file.
116 :param testcase: Testcase class.
117 :param iface: Interface.
118 :param suite_id: Suite ID.
119 :param file_out: File to write testcases to.
120 :param tc_kwargs_list: Key-value pairs used to construct testcases.
121 :type testcase: Testcase
125 :type tc_kwargs_list: dict
128 for tc_kwargs in tc_kwargs_list:
129 num = add_testcase(testcase, iface, suite_id, file_out, num,
132 print "Regenerator starts at {cwd}".format(cwd=getcwd())
133 min_framesize = protocol_to_min_framesize[protocol]
134 kwargs_list = tc_kwargs_list if tc_kwargs_list else [
135 {"framesize": min_framesize, "phy_cores": 1},
136 {"framesize": min_framesize, "phy_cores": 2},
137 {"framesize": min_framesize, "phy_cores": 4},
138 {"framesize": 1518, "phy_cores": 1},
139 {"framesize": 1518, "phy_cores": 2},
140 {"framesize": 1518, "phy_cores": 4},
141 {"framesize": 9000, "phy_cores": 1},
142 {"framesize": 9000, "phy_cores": 2},
143 {"framesize": 9000, "phy_cores": 4},
144 {"framesize": "IMIX_v4_1", "phy_cores": 1},
145 {"framesize": "IMIX_v4_1", "phy_cores": 2},
146 {"framesize": "IMIX_v4_1", "phy_cores": 4}
148 for filename in glob(pattern):
149 print "Regenerating filename:", filename
150 with open(filename, "r") as file_in:
151 text = file_in.read()
152 text_prolog = "".join(text.partition("*** Test Cases ***")[:-1])
153 iface, suite_id = get_iface_and_suite_id(filename)
154 testcase = self.testcase_class(suite_id)
155 with open(filename, "w") as file_out:
156 file_out.write(text_prolog)
157 add_testcases(testcase, iface, suite_id, file_out, kwargs_list)
158 print "Regenerator ends."
159 print # To make autogen check output more readable.