back-to-back tests: add TG tests
[csit.git] / resources / libraries / python / autogen / Testcase.py
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:
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 testcase autogeneration."""
15
16 from string import Template
17
18
19 class Testcase:
20     """Class containing a template string and a substitution method."""
21
22     def __init__(self, template_string):
23         """Construct instance by storing template given by string.
24
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
33         """
34         self.template = Template(template_string)
35
36     def generate(self, frame_size, phy_cores=None):
37         """Return string of test case code with placeholders filled.
38
39         Fail if there are placeholders left unfilled.
40         It is not required for all placeholders to be present in template.
41
42         :param frame_size: Imix string or numeric frame size. Example: 74.
43         :param phy_cores: Number of physical cores to use. Example: 2. It can
44             be None in n2n testcases.
45         :type frame_size: str or int
46         :type phy_cores: int, str or None
47         :returns: Filled template, usable as test case code.
48         :rtype: str
49         """
50         try:
51             fsize = int(frame_size)
52             subst_dict = {
53                 u"frame_num": f"${{{fsize:d}}}",
54                 u"frame_str": f"{fsize:d}B"
55             }
56         except ValueError:  # Assuming an IMIX string.
57             subst_dict = {
58                 u"frame_num": str(frame_size),
59                 u"frame_str": u"IMIX"
60             }
61         if phy_cores is None:
62             return self.template.substitute(subst_dict)
63         cores_str = str(phy_cores)
64         cores_num = int(cores_str)
65         subst_dict.update(
66             {
67                 u"cores_num": f"${{{cores_num:d}}}",
68                 u"cores_str": phy_cores,
69             }
70         )
71         return self.template.substitute(subst_dict)
72
73     @classmethod
74     def default(cls, suite_id):
75         """Factory method for creating "default" testcase objects.
76
77         Testcase name will contain both frame size and core count.
78         Used for most performance tests, except TCP ones.
79
80         :param suite_id: Part of suite name to distinguish from other suites.
81         :type suite_id: str
82         :returns: Instance for generating testcase text of this type.
83         :rtype: Testcase
84         """
85         template_string = f'''
86 | ${{frame_str}}-${{cores_str}}c-{suite_id}
87 | | [Tags] | ${{frame_str}} | ${{cores_str}}C
88 | | frame_size=${{frame_num}} | phy_cores=${{cores_num}}
89 '''
90         return cls(template_string)
91
92     @classmethod
93     def tcp(cls, suite_id):
94         """Factory method for creating "tcp" testcase objects.
95
96         Testcase name will contain core count, but not frame size.
97
98         :param suite_id: Part of suite name to distinguish from other suites.
99         :type suite_id: str
100         :returns: Instance for generating testcase text of this type.
101         :rtype: Testcase
102         """
103         # TODO: Choose a better frame size identifier for streamed protocols
104         # (TCP, QUIC, SCTP, ...) where DUT (not TG) decides frame size.
105         if u"tcphttp" in suite_id:
106             if u"rps" or u"cps" in suite_id:
107                 template_string = f'''
108 | ${{frame_str}}-${{cores_str}}c-{suite_id}
109 | | [Tags] | ${{frame_str}} | ${{cores_str}}C
110 | | frame_size=${{frame_num}} | phy_cores=${{cores_num}}
111 '''
112             else:
113                 template_string = f'''
114 | IMIX-${{cores_str}}c-{suite_id}
115 | | [Tags] | ${{cores_str}}C
116 | | phy_cores=${{cores_num}}
117 '''
118         else:
119             template_string = f'''
120 | ${{frame_str}}-${{cores_str}}c-{suite_id[:-4]}-{suite_id[-3:]}
121 | | [Tags] | ${{cores_str}}C\n| | phy_cores=${{cores_num}}
122 '''
123         return cls(template_string)
124
125     @classmethod
126     def iperf3(cls, suite_id):
127         """Factory method for creating "iperf3" testcase objects.
128
129         Testcase name will contain core count, but not frame size.
130
131         :param suite_id: Part of suite name to distinguish from other suites.
132         :type suite_id: str
133         :returns: Instance for generating testcase text of this type.
134         :rtype: Testcase
135         """
136         template_string = f'''
137 | 128KB-${{cores_str}}c-{suite_id}
138 | | [Tags] | 128KB | ${{cores_str}}C
139 | | frame_size=${{frame_num}} | phy_cores=${{cores_num}}
140 '''
141         return cls(template_string)
142
143     @classmethod
144     def trex(cls, suite_id):
145         """Factory method for creating "trex" testcase objects.
146
147         Testcase name will contain frame size, but not core count.
148
149         :param suite_id: Part of suite name to distinguish from other suites.
150         :type suite_id: str
151         :returns: Instance for generating testcase text of this type.
152         :rtype: Testcase
153         """
154         template_string = f'''
155 | ${{frame_str}}--{suite_id}
156 | | [Tags] | ${{frame_str}}
157 | | frame_size=${{frame_num}}
158 '''
159         return cls(template_string)