Add tox.ini and few checker scripts
[csit.git] / resources / libraries / python / autogen / Regenerator.py
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:
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 __future__ import print_function
17
18 from glob import glob
19 from os import getcwd
20 import sys
21
22 from .DefaultTestcase import DefaultTestcase
23
24
25 # Copied from https://stackoverflow.com/a/14981125
26 def eprint(*args, **kwargs):
27     """Print to stderr."""
28     print(*args, file=sys.stderr, **kwargs)
29
30
31 class Regenerator(object):
32     """Class containing file generating methods."""
33
34     def __init__(self, testcase_class=DefaultTestcase, quiet=True):
35         """Initialize Testcase class to use.
36
37         TODO: See the type doc for testcase_class?
38         It implies the design is wrong. Fix it.
39         Easiest: Hardcode Regenerator to use DefaultTestcase only.
40
41         :param testcase_class: Subclass of DefaultTestcase for generation.
42             Default: DefaultTestcase
43         :param quiet: Reduce log prints (to stderr) when True (default).
44         :type testcase_class: subclass of DefaultTestcase accepting suite_id
45         :type quiet: boolean
46         """
47         self.testcase_class = testcase_class
48         self.quiet = quiet
49
50     def regenerate_glob(self, pattern, protocol="ip4", tc_kwargs_list=None):
51         """Regenerate files matching glob pattern based on arguments.
52
53         In the current working directory, find all files matching
54         the glob pattern. Use testcase template (from init) to regenerate
55         test cases, autonumbering them, taking arguments from list.
56         If the list is None, use default list, which depends on ip6 usage.
57
58         Log-like prints are emited to sys.stderr.
59
60         :param pattern: Glob pattern to select files. Example: *-ndrpdr.robot
61         :param is_ip6: Flag determining minimal frame size. Default: False
62         :param tc_kwargs_list: Arguments defining the testcases. Default: None
63             When None, default list is used.
64             List item is a dict, argument names are keys.
65             The initialized testcase_class should accept those, and "num".
66             DefaultTestcase accepts "framesize" and "phy_cores".
67         :type pattern: str
68         :type is_ip6: boolean
69         :type tc_kwargs_list: list of tuple or None
70         """
71
72         protocol_to_min_framesize = {
73             "ip4": 64,
74             "ip6": 78,
75             "vxlan+ip4": 114  # What is the real minimum for latency stream?
76         }
77         min_framesize_values = protocol_to_min_framesize.values()
78
79         def get_iface_and_suite_id(filename):
80             """Get interface and suite ID.
81
82             :param filename: Suite file.
83             :type filename: str
84             :returns: Interface ID, Suite ID.
85             :rtype: tuple
86             """
87             dash_split = filename.split("-", 1)
88             if len(dash_split[0]) <= 4:
89                 # It was something like "2n1l", we need one more split.
90                 dash_split = dash_split[1].split("-", 1)
91             return dash_split[0], dash_split[1].split(".", 1)[0]
92
93         def add_testcase(testcase, iface, suite_id, file_out, num, **kwargs):
94             """Add testcase to file.
95
96             :param testcase: Testcase class.
97             :param iface: Interface.
98             :param suite_id: Suite ID.
99             :param file_out: File to write testcases to.
100             :param num: Testcase number.
101             :param kwargs: Key-value pairs used to construct testcase.
102             :type testcase: Testcase
103             :type iface: str
104             :type suite_id: str
105             :type file_out: file
106             :type num: int
107             :type kwargs: dict
108             :returns: Next testcase number.
109             :rtype: int
110             """
111             # TODO: Is there a better way to disable some combinations?
112             emit = True
113             if kwargs["framesize"] == 9000:
114                 if "vic1227" in iface:
115                     # Not supported in HW.
116                     emit = False
117                 if "avf" in suite_id:
118                     # Not supported by AVF driver.
119                     # https://git.fd.io/vpp/tree/src/plugins/avf/README.md
120                     emit = False
121             if "-16vm-" in suite_id or "-16dcr-" in suite_id:
122                 if kwargs["phy_cores"] > 3:
123                     # CSIT lab only has 28 (physical) core processors,
124                     # so these test would fail when attempting to assign cores.
125                     emit = False
126             if "soak" in suite_id:
127                 # Soak test take too long, do not risk other than tc01.
128                 if kwargs["phy_cores"] != 1:
129                     emit = False
130                 if kwargs["framesize"] not in min_framesize_values:
131                     emit = False
132             if emit:
133                 file_out.write(testcase.generate(num=num, **kwargs))
134             # We bump tc number in any case, so that future enables/disables
135             # do not affect the numbering of other test cases.
136             return num + 1
137
138         def add_testcases(testcase, iface, suite_id, file_out, tc_kwargs_list):
139             """Add testcases to file.
140
141             :param testcase: Testcase class.
142             :param iface: Interface.
143             :param suite_id: Suite ID.
144             :param file_out: File to write testcases to.
145             :param tc_kwargs_list: Key-value pairs used to construct testcases.
146             :type testcase: Testcase
147             :type iface: str
148             :type suite_id: str
149             :type file_out: file
150             :type tc_kwargs_list: dict
151             """
152             num = 1
153             for tc_kwargs in tc_kwargs_list:
154                 num = add_testcase(testcase, iface, suite_id, file_out, num,
155                                    **tc_kwargs)
156
157         if not self.quiet:
158             eprint("Regenerator starts at {cwd}".format(cwd=getcwd()))
159         min_framesize = protocol_to_min_framesize[protocol]
160         kwargs_list = tc_kwargs_list if tc_kwargs_list else [
161             {"framesize": min_framesize, "phy_cores": 1},
162             {"framesize": min_framesize, "phy_cores": 2},
163             {"framesize": min_framesize, "phy_cores": 4},
164             {"framesize": 1518, "phy_cores": 1},
165             {"framesize": 1518, "phy_cores": 2},
166             {"framesize": 1518, "phy_cores": 4},
167             {"framesize": 9000, "phy_cores": 1},
168             {"framesize": 9000, "phy_cores": 2},
169             {"framesize": 9000, "phy_cores": 4},
170             {"framesize": "IMIX_v4_1", "phy_cores": 1},
171             {"framesize": "IMIX_v4_1", "phy_cores": 2},
172             {"framesize": "IMIX_v4_1", "phy_cores": 4}
173         ]
174         for filename in glob(pattern):
175             if not self.quiet:
176                 eprint("Regenerating filename:", filename)
177             with open(filename, "r") as file_in:
178                 text = file_in.read()
179                 text_prolog = "".join(text.partition("*** Test Cases ***")[:-1])
180             iface, suite_id = get_iface_and_suite_id(filename)
181             testcase = self.testcase_class(suite_id)
182             with open(filename, "w") as file_out:
183                 file_out.write(text_prolog)
184                 add_testcases(testcase, iface, suite_id, file_out, kwargs_list)
185         if not self.quiet:
186             eprint("Regenerator ends.")
187         eprint()  # To make autogen check output more readable.