1 # Copyright (c) 2021 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 testcase autogeneration."""
16 from string import Template
20 """Class containing a template string and a substitution method."""
22 def __init__(self, template_string):
23 """Construct instance by storing template given by string.
25 :param template_string: Template string to generate test case code with.
26 See string.Template documentation for template string syntax.
27 Only the following placeholders are supported:
28 - cores_num - Number of cores as robot number, example: "${2}".
29 - cores_str - Number of physical cores to use, example: "2".
30 - frame_num - Framesize as a number, example: "${74}".
31 - frame_str - Framesize in upper case, example: "74B".
32 :type template_string: str
34 self.template = Template(template_string)
36 def generate(self, frame_size, phy_cores):
37 """Return string of test case code with placeholders filled.
39 Fail if there are placeholders left unfilled.
40 It is not required for all placeholders to be present in template.
42 :param frame_size: Imix string or numeric frame size. Example: 74.
43 :param phy_cores: Number of physical cores to use. Example: 2.
44 :type frame_size: str or int
45 :type phy_cores: int or str
46 :returns: Filled template, usable as test case code.
50 fsize = int(frame_size)
52 u"frame_num": f"${{{fsize:d}}}",
53 u"frame_str": f"{fsize:d}B"
55 except ValueError: # Assuming an IMIX string.
57 u"frame_num": str(frame_size),
60 cores_str = str(phy_cores)
61 cores_num = int(cores_str)
64 u"cores_num": f"${{{cores_num:d}}}",
65 u"cores_str": phy_cores,
68 return self.template.substitute(subst_dict)
71 def default(cls, suite_id):
72 """Factory method for creating "default" testcase objects.
74 Testcase name will contain both frame size and core count.
75 Used for most performance tests, except TCP ones.
77 :param suite_id: Part of suite name to distinguish from other suites.
79 :returns: Instance for generating testcase text of this type.
82 template_string = f'''
83 | ${{frame_str}}-${{cores_str}}c-{suite_id}
84 | | [Tags] | ${{frame_str}} | ${{cores_str}}C
85 | | frame_size=${{frame_num}} | phy_cores=${{cores_num}}
87 return cls(template_string)
90 def tcp(cls, suite_id):
91 """Factory method for creating "tcp" testcase objects.
93 Testcase name will contain core count, but not frame size.
95 :param suite_id: Part of suite name to distinguish from other suites.
97 :returns: Instance for generating testcase text of this type.
100 # TODO: Choose a better frame size identifier for streamed protocols
101 # (TCP, QUIC, SCTP, ...) where DUT (not TG) decides frame size.
102 if u"tcphttp" in suite_id:
103 template_string = f'''
104 | IMIX-${{cores_str}}c-{suite_id}
105 | | [Tags] | ${{cores_str}}C
106 | | phy_cores=${{cores_num}}
109 template_string = f'''
110 | ${{frame_str}}-${{cores_str}}c-{suite_id[:-4]}-{suite_id[-3:]}
111 | | [Tags] | ${{cores_str}}C\n| | phy_cores=${{cores_num}}
113 return cls(template_string)
116 def iperf3(cls, suite_id):
117 """Factory method for creating "iperf3" testcase objects.
119 Testcase name will contain core count, but not frame size.
121 :param suite_id: Part of suite name to distinguish from other suites.
123 :returns: Instance for generating testcase text of this type.
126 template_string = f'''
127 | 128KB-${{cores_str}}c-{suite_id}
128 | | [Tags] | 128KB | ${{cores_str}}C
129 | | frame_size=${{frame_num}} | phy_cores=${{cores_num}}
131 return cls(template_string)