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:
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 - tc_num - Start of testcase name, example: "tc04".
33 :type template_string: str
35 self.template = Template(template_string)
37 def generate(self, num, frame_size, phy_cores, clients, streams,
39 """Return string of test case code with placeholders filled.
41 Fail if there are placeholders left unfilled.
42 It is not required for all placeholders to be present in template.
44 :param num: Test case number. Example value: 4.
45 :param frame_size: Imix string or numeric frame size. Example: 74.
46 :param phy_cores: Number of physical cores to use. Example: 2.
47 :param clients: Number of clients used by test program. Example: 4.
48 :param streams: Number of streams used by test program. Example: 10.
49 :param bytes_str: Size in bytes of stream sent by test program.
52 :type frame_size: str or int
53 :type phy_cores: int or str
57 :returns: Filled template, usable as test case code.
61 fsize = int(frame_size)
63 u"frame_num": f"${{{fsize:d}}}",
64 u"frame_str": f"{fsize:d}B"
66 except ValueError: # Assuming an IMIX string.
68 u"frame_num": str(frame_size),
71 cores_str = str(phy_cores)
72 cores_num = int(cores_str)
75 u"cores_num": f"${{{cores_num:d}}}",
76 u"cores_str": phy_cores,
77 u"tc_num": f"tc{num:02d}",
78 u"clients_num": f"${{{clients:d}}}",
79 u"clients_str": str(clients),
80 u"streams_num": f"${{{streams:d}}}",
81 u"streams_str": str(streams),
82 u"bytes_str": bytes_str,
85 return self.template.substitute(subst_dict)
88 def default(cls, suite_id):
89 """Factory method for creating "default" testcase objects.
91 Testcase name will contain both frame size and core count.
92 Used for most performance tests, except TCP ones.
94 :param suite_id: Part of suite name to distinguish from other suites.
96 :returns: Instance for generating testcase text of this type.
99 template_string = f'''
100 | ${{tc_num}}-${{frame_str}}-${{cores_str}}c-{suite_id}
101 | | [Tags] | ${{frame_str}} | ${{cores_str}}C
102 | | frame_size=${{frame_num}} | phy_cores=${{cores_num}}
104 return cls(template_string)
107 def tcp(cls, suite_id):
108 """Factory method for creating "tcp" testcase objects.
110 Testcase name will contain core count, but not frame size.
112 :param suite_id: Part of suite name to distinguish from other suites.
114 :returns: Instance for generating testcase text of this type.
117 # TODO: Choose a better frame size identifier for streamed protocols
118 # (TCP, QUIC, SCTP, ...) where DUT (not TG) decides frame size.
119 if u"tcphttp" in suite_id:
120 template_string = f'''
121 | ${{tc_num}}-IMIX-${{cores_str}}c-{suite_id}
122 | | [Tags] | ${{cores_str}}C
123 | | phy_cores=${{cores_num}}
125 elif u"iperf3" in suite_id:
126 template_string = f'''
127 | ${{tc_num}}-9000B-${{cores_str}}c-{suite_id}
128 | | [Tags] | ${{cores_str}}C | ${{clients_str}}CLIENT | ${{streams_str}}STREAM
129 | | phy_cores=${{cores_num}} | clients=${{clients_num}}'''
130 template_string += f" | streams=${{streams_num}}\n"
132 template_string = f'''
133 | ${{tc_num}}-9000B-${{cores_str}}c-{suite_id}
134 | | [Tags] | ${{cores_str}}C | ${{clients_str}}CLIENT | ${{streams_str}}STREAM
135 | | phy_cores=${{cores_num}} | clients=${{clients_num}}'''
136 template_string += f" | streams=${{streams_num}}" \
137 f" | bytes=${{bytes_str}}\n"
138 return cls(template_string)