CSIT-1186: Add srv6 NDRPDR suites
[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 .Testcase import Testcase
20 from .DefaultTestcase import DefaultTestcase
21
22
23 class Regenerator(object):
24     """Class containing file generating methods."""
25
26     def __init__(self, testcase_class=DefaultTestcase):
27         """Initialize Testcase class to use.
28
29         TODO: See the type doc for testcase_class?
30         It implies the design is wrong. Fix it.
31         Easiest: Hardcode Regenerator to use DefaultTestcase only.
32
33         :param testcase_class: Subclass of DefaultTestcase for generation.
34             Default: DefaultTestcase
35         :type testcase_class: subclass of DefaultTestcase accepting suite_id
36         """
37         self.testcase_class = testcase_class
38
39     def regenerate_glob(self, pattern, is_ip6=False, tc_kwargs_list=None):
40         """Regenerate files matching glob pattern based on arguments.
41
42         In the current working directory, find all files matching
43         the glob pattern. Use testcase template (from init) to regenerate
44         test cases, autonumbering them, taking arguments from list.
45         If the list is None, use default list, which depends on ip6 usage.
46
47         :param pattern: Glob pattern to select files. Example: *-ndrpdr.robot
48         :param is_ip6: Flag determining minimal frame size. Default: False
49         :param tc_kwargs_list: Arguments defining the testcases. Default: None
50             When None, default list is used.
51             List item is a dict, argument names are keys.
52             The initialized testcase_class should accept those, and "num".
53             DefaultTestcase accepts "framesize" and "phy_cores".
54         :type pattern: str
55         :type is_ip6: boolean
56         :type tc_kwargs_list: list of tuple or None
57         """
58
59         def add_testcase(file_out, num, **kwargs):
60             file_out.write(testcase.generate(num=num, **kwargs))
61             return num + 1
62
63         def add_testcases(file_out, tc_kwargs_list):
64             num = 1
65             for tc_kwargs in tc_kwargs_list:
66                 num = add_testcase(file_out, num, **tc_kwargs)
67
68         print "Regenerator starts at {cwd}".format(cwd=getcwd())
69         min_framesize = 78 if is_ip6 else 64
70         kwargs_list = tc_kwargs_list if tc_kwargs_list is not None else [
71             {"framesize": min_framesize, "phy_cores": 1},
72             {"framesize": min_framesize, "phy_cores": 2},
73             {"framesize": min_framesize, "phy_cores": 4},
74             {"framesize": 1518, "phy_cores": 1},
75             {"framesize": 1518, "phy_cores": 2},
76             {"framesize": 1518, "phy_cores": 4},
77             {"framesize": 9000, "phy_cores": 1},
78             {"framesize": 9000, "phy_cores": 2},
79             {"framesize": 9000, "phy_cores": 4},
80             {"framesize": "IMIX_v4_1", "phy_cores": 1},
81             {"framesize": "IMIX_v4_1", "phy_cores": 2},
82             {"framesize": "IMIX_v4_1", "phy_cores": 4}
83         ]
84         for filename in glob(pattern):
85             with open(filename, "r") as file_in:
86                 text = file_in.read()
87                 text_prolog = "".join(text.partition("*** Test Cases ***")[:-1])
88             # TODO: Make the following work for 2n suites.
89             suite_id = filename.split("-", 1)[1].split(".", 1)[0]
90             print "Regenerating suite_id:", suite_id
91             testcase = self.testcase_class(suite_id)
92             with open(filename, "w") as file_out:
93                 file_out.write(text_prolog)
94                 add_testcases(file_out, kwargs_list)
95         print "Regenerator ends."
96         print  # To make autogen check output more readable.