64bcea917d75935be797e5873cc0210b1278ee4d
[csit.git] / resources / libraries / python / autogen / Regenerator.py
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:
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 from glob import glob
17 from os import getcwd
18
19 from .DefaultTestcase import DefaultTestcase
20
21
22 class Regenerator(object):
23     """Class containing file generating methods."""
24
25     def __init__(self, testcase_class=DefaultTestcase):
26         """Initialize Testcase class to use.
27
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.
31
32         :param testcase_class: Subclass of DefaultTestcase for generation.
33             Default: DefaultTestcase
34         :type testcase_class: subclass of DefaultTestcase accepting suite_id
35         """
36         self.testcase_class = testcase_class
37
38     def regenerate_glob(self, pattern, protocol="ip4", tc_kwargs_list=None):
39         """Regenerate files matching glob pattern based on arguments.
40
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.
45
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".
53         :type pattern: str
54         :type is_ip6: boolean
55         :type tc_kwargs_list: list of tuple or None
56         """
57
58         protocol_to_min_framesize = {
59             "ip4": 64,
60             "ip6": 78,
61             "vxlan+ip4": 114  # What is the real minimum for latency stream?
62         }
63         min_framesize_values = protocol_to_min_framesize.values()
64
65         def get_iface_and_suite_id(filename):
66             """Get interface and suite ID.
67
68             :param filename: Suite file.
69             :type filename: str
70             :returns: Interface ID, Suite ID.
71             :rtype: tuple
72             """
73             dash_split = filename.split("-", 1)
74             if len(dash_split[0]) <= 4:
75                 # It was something like "2n1l", we need one more split.
76                 dash_split = dash_split[1].split("-", 1)
77             return dash_split[0], dash_split[1].split(".", 1)[0]
78
79         def add_testcase(testcase, iface, suite_id, file_out, num, **kwargs):
80             """Add testcase to file.
81
82             :param testcase: Testcase class.
83             :param iface: Interface.
84             :param suite_id: Suite ID.
85             :param file_out: File to write testcases to.
86             :param num: Testcase number.
87             :param kwargs: Key-value pairs used to construct testcase.
88             :type testcase: Testcase
89             :type iface: str
90             :type suite_id: str
91             :type file_out: file
92             :type num: int
93             :type kwargs: dict
94             :returns: Next testcase number.
95             :rtype: int
96             """
97             # TODO: Is there a better way to disable some combinations?
98             emit = True
99             if kwargs["framesize"] == 9000:
100                 if "vic1227" in iface:
101                     # Not supported in HW.
102                     emit = False
103                 if "avf" in suite_id:
104                     # Not supported by AVF driver.
105                     # https://git.fd.io/vpp/tree/src/plugins/avf/README.md
106                     emit = False
107             if "-16vm-" in suite_id or "-16dcr-" in suite_id:
108                 if kwargs["phy_cores"] > 3:
109                     # CSIT lab only has 28 (physical) core processors,
110                     # so these test would fail when attempting to assign cores.
111                     emit = False
112             if "soak" in suite_id:
113                 # Soak test take too long, do not risk other than tc01.
114                 if kwargs["phy_cores"] != 1:
115                     emit = False
116                 if kwargs["framesize"] not in min_framesize_values:
117                     emit = False
118             if emit:
119                 file_out.write(testcase.generate(num=num, **kwargs))
120             # We bump tc number in any case, so that future enables/disables
121             # do not affect the numbering of other test cases.
122             return num + 1
123
124         def add_testcases(testcase, iface, suite_id, file_out, tc_kwargs_list):
125             """Add testcases to file.
126
127             :param testcase: Testcase class.
128             :param iface: Interface.
129             :param suite_id: Suite ID.
130             :param file_out: File to write testcases to.
131             :param tc_kwargs_list: Key-value pairs used to construct testcases.
132             :type testcase: Testcase
133             :type iface: str
134             :type suite_id: str
135             :type file_out: file
136             :type tc_kwargs_list: dict
137             """
138             num = 1
139             for tc_kwargs in tc_kwargs_list:
140                 num = add_testcase(testcase, iface, suite_id, file_out, num,
141                                    **tc_kwargs)
142
143         print "Regenerator starts at {cwd}".format(cwd=getcwd())
144         min_framesize = protocol_to_min_framesize[protocol]
145         kwargs_list = tc_kwargs_list if tc_kwargs_list else [
146             {"framesize": min_framesize, "phy_cores": 1},
147             {"framesize": min_framesize, "phy_cores": 2},
148             {"framesize": min_framesize, "phy_cores": 4},
149             {"framesize": 1518, "phy_cores": 1},
150             {"framesize": 1518, "phy_cores": 2},
151             {"framesize": 1518, "phy_cores": 4},
152             {"framesize": 9000, "phy_cores": 1},
153             {"framesize": 9000, "phy_cores": 2},
154             {"framesize": 9000, "phy_cores": 4},
155             {"framesize": "IMIX_v4_1", "phy_cores": 1},
156             {"framesize": "IMIX_v4_1", "phy_cores": 2},
157             {"framesize": "IMIX_v4_1", "phy_cores": 4}
158         ]
159         for filename in glob(pattern):
160             print "Regenerating filename:", filename
161             with open(filename, "r") as file_in:
162                 text = file_in.read()
163                 text_prolog = "".join(text.partition("*** Test Cases ***")[:-1])
164             iface, suite_id = get_iface_and_suite_id(filename)
165             testcase = self.testcase_class(suite_id)
166             with open(filename, "w") as file_out:
167                 file_out.write(text_prolog)
168                 add_testcases(testcase, iface, suite_id, file_out, kwargs_list)
169         print "Regenerator ends."
170         print  # To make autogen check output more readable.