diff options
Diffstat (limited to 'resources/libraries/python/autogen')
-rw-r--r-- | resources/libraries/python/autogen/Copyright.py | 29 | ||||
-rw-r--r-- | resources/libraries/python/autogen/DefaultTestcase.py | 34 | ||||
-rw-r--r-- | resources/libraries/python/autogen/Regenerator.py | 96 | ||||
-rw-r--r-- | resources/libraries/python/autogen/Testcase.py | 70 | ||||
-rw-r--r-- | resources/libraries/python/autogen/__init__.py | 16 |
5 files changed, 245 insertions, 0 deletions
diff --git a/resources/libraries/python/autogen/Copyright.py b/resources/libraries/python/autogen/Copyright.py new file mode 100644 index 0000000000..e8c72f0624 --- /dev/null +++ b/resources/libraries/python/autogen/Copyright.py @@ -0,0 +1,29 @@ +# Copyright (c) 2018 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Module defining sole constant holding copyright text for current year.""" + + +COPYRIGHT = '''# Copyright (c) 2018 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +'''
\ No newline at end of file diff --git a/resources/libraries/python/autogen/DefaultTestcase.py b/resources/libraries/python/autogen/DefaultTestcase.py new file mode 100644 index 0000000000..9878289bd1 --- /dev/null +++ b/resources/libraries/python/autogen/DefaultTestcase.py @@ -0,0 +1,34 @@ +# Copyright (c) 2018 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Module with utilities for autogeneration of non-customizable testcases.""" + +from Testcase import Testcase + + +class DefaultTestcase(Testcase): + """Testcase subclass with a rigid template string.""" + + def __init__(self, suite_id): + """Construct instance for identified suite. + + :param suite_id: Suite identifier, without NIC prefix and .robot suffix. + Example: ethip6srhip6-ip6base-srv6enc2sids-nodecaps-ndrpdr + :type suite_id: str + """ + template_string = r''' +| ${tc_num}-${frame_str}-${cores_str}c-''' + suite_id + r''' +| | [Tags] | ${frame_str} | ${cores_str}C +| | framesize=${frame_num} | phy_cores=${cores_num} +''' + super(DefaultTestcase, self).__init__(template_string) diff --git a/resources/libraries/python/autogen/Regenerator.py b/resources/libraries/python/autogen/Regenerator.py new file mode 100644 index 0000000000..85e8b60dd5 --- /dev/null +++ b/resources/libraries/python/autogen/Regenerator.py @@ -0,0 +1,96 @@ +# Copyright (c) 2018 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Module defining utilities for test directory regeneration.""" + +from glob import glob +from os import getcwd + +from .Testcase import Testcase +from .DefaultTestcase import DefaultTestcase + + +class Regenerator(object): + """Class containing file generating methods.""" + + def __init__(self, testcase_class=DefaultTestcase): + """Initialize Testcase class to use. + + TODO: See the type doc for testcase_class? + It implies the design is wrong. Fix it. + Easiest: Hardcode Regenerator to use DefaultTestcase only. + + :param testcase_class: Subclass of DefaultTestcase for generation. + Default: DefaultTestcase + :type testcase_class: subclass of DefaultTestcase accepting suite_id + """ + self.testcase_class = testcase_class + + def regenerate_glob(self, pattern, is_ip6=False, tc_kwargs_list=None): + """Regenerate files matching glob pattern based on arguments. + + In the current working directory, find all files matching + the glob pattern. Use testcase template (from init) to regenerate + test cases, autonumbering them, taking arguments from list. + If the list is None, use default list, which depends on ip6 usage. + + :param pattern: Glob pattern to select files. Example: *-ndrpdr.robot + :param is_ip6: Flag determining minimal frame size. Default: False + :param tc_kwargs_list: Arguments defining the testcases. Default: None + When None, default list is used. + List item is a dict, argument names are keys. + The initialized testcase_class should accept those, and "num". + DefaultTestcase accepts "framesize" and "phy_cores". + :type pattern: str + :type is_ip6: boolean + :type tc_kwargs_list: list of tuple or None + """ + + def add_testcase(file_out, num, **kwargs): + file_out.write(testcase.generate(num=num, **kwargs)) + return num + 1 + + def add_testcases(file_out, tc_kwargs_list): + num = 1 + for tc_kwargs in tc_kwargs_list: + num = add_testcase(file_out, num, **tc_kwargs) + + print "Regenerator starts at {cwd}".format(cwd=getcwd()) + min_framesize = 78 if is_ip6 else 64 + kwargs_list = tc_kwargs_list if tc_kwargs_list is not None else [ + {"framesize": min_framesize, "phy_cores": 1}, + {"framesize": min_framesize, "phy_cores": 2}, + {"framesize": min_framesize, "phy_cores": 4}, + {"framesize": 1518, "phy_cores": 1}, + {"framesize": 1518, "phy_cores": 2}, + {"framesize": 1518, "phy_cores": 4}, + {"framesize": 9000, "phy_cores": 1}, + {"framesize": 9000, "phy_cores": 2}, + {"framesize": 9000, "phy_cores": 4}, + {"framesize": "IMIX_v4_1", "phy_cores": 1}, + {"framesize": "IMIX_v4_1", "phy_cores": 2}, + {"framesize": "IMIX_v4_1", "phy_cores": 4} + ] + for filename in glob(pattern): + with open(filename, "r") as file_in: + text = file_in.read() + text_prolog = "".join(text.partition("*** Test Cases ***")[:-1]) + # TODO: Make the following work for 2n suites. + suite_id = filename.split("-", 1)[1].split(".", 1)[0] + print "Regenerating suite_id:", suite_id + testcase = self.testcase_class(suite_id) + with open(filename, "w") as file_out: + file_out.write(text_prolog) + add_testcases(file_out, kwargs_list) + print "Regenerator ends." + print # To make autogen check output more readable. diff --git a/resources/libraries/python/autogen/Testcase.py b/resources/libraries/python/autogen/Testcase.py new file mode 100644 index 0000000000..4f92e6c2e1 --- /dev/null +++ b/resources/libraries/python/autogen/Testcase.py @@ -0,0 +1,70 @@ +# Copyright (c) 2018 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Module defining utilities for testcase autogeneration.""" + +from string import Template + + +class Testcase(object): + """Class containing a template string and a substitution method.""" + + def __init__(self, template_string): + """Construct instance by storing template given by string. + + :param template_string: Template string to generate test case code with. + See string.Template documentation for template string syntax. + Only the following placeholders are supported: + - cores_num - Number of cores as robot number, example: "${2}". + - cores_str - Number of physical cores to use, example: "2". + - frame_num - Framesize as a number, example: "${74}". + - frame_str - Framesize in upper case, example: "74B". + - tc_num - Start of testcase name, example: "tc04". + :type template_string: str + """ + self.template = Template(template_string) + + def generate(self, num, framesize, phy_cores): + """Return string of test case code with placeholders filled. + + Fail if there are placeholders left unfilled. + + :param num: Test case number. Example value: 4. + :param framesize: Imix string or numeric frame size. Example: 74. + :param phy_cores: Number of physical cores to use. Example: 2. + :type num: int + :type framesize: str or int + :type phy_cores: int or str + :returns: Filled template, usable as test case code. + :rtype: str + """ + try: + fs = int(framesize) + subst_dict = { + "frame_num": "${%d}" % fs, + "frame_str": "%dB" % fs + } + except ValueError: # Assuming an IMIX string. + subst_dict = { + "frame_num": str(framesize), + "frame_str": "IMIX" + } + cores_str = str(phy_cores) + cores_num = int(cores_str) + subst_dict.update( + { + "cores_num": "${%d}" % cores_num, + "cores_str": phy_cores, + "tc_num": "tc{num:02d}".format(num=num) + }) + return self.template.substitute(subst_dict) diff --git a/resources/libraries/python/autogen/__init__.py b/resources/libraries/python/autogen/__init__.py new file mode 100644 index 0000000000..dfb2255a09 --- /dev/null +++ b/resources/libraries/python/autogen/__init__.py @@ -0,0 +1,16 @@ +# Copyright (c) 2018 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +__init__ file for directory resources/libraries/python/autogen +""" |