GSO: TAP/VHOST use case
[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):
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.
44         :type frame_size: str or int
45         :type phy_cores: int or str
46         :returns: Filled template, usable as test case code.
47         :rtype: str
48         """
49         try:
50             fsize = int(frame_size)
51             subst_dict = {
52                 u"frame_num": f"${{{fsize:d}}}",
53                 u"frame_str": f"{fsize:d}B"
54             }
55         except ValueError:  # Assuming an IMIX string.
56             subst_dict = {
57                 u"frame_num": str(frame_size),
58                 u"frame_str": u"IMIX"
59             }
60         cores_str = str(phy_cores)
61         cores_num = int(cores_str)
62         subst_dict.update(
63             {
64                 u"cores_num": f"${{{cores_num:d}}}",
65                 u"cores_str": phy_cores,
66             }
67         )
68         return self.template.substitute(subst_dict)
69
70     @classmethod
71     def default(cls, suite_id):
72         """Factory method for creating "default" testcase objects.
73
74         Testcase name will contain both frame size and core count.
75         Used for most performance tests, except TCP ones.
76
77         :param suite_id: Part of suite name to distinguish from other suites.
78         :type suite_id: str
79         :returns: Instance for generating testcase text of this type.
80         :rtype: Testcase
81         """
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}}
86 '''
87         return cls(template_string)
88
89     @classmethod
90     def tcp(cls, suite_id):
91         """Factory method for creating "tcp" testcase objects.
92
93         Testcase name will contain core count, but not frame size.
94
95         :param suite_id: Part of suite name to distinguish from other suites.
96         :type suite_id: str
97         :returns: Instance for generating testcase text of this type.
98         :rtype: Testcase
99         """
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}}
107 '''
108         else:
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}}
112 '''
113         return cls(template_string)
114
115     @classmethod
116     def iperf3(cls, suite_id):
117         """Factory method for creating "iperf3" testcase objects.
118
119         Testcase name will contain core count, but not frame size.
120
121         :param suite_id: Part of suite name to distinguish from other suites.
122         :type suite_id: str
123         :returns: Instance for generating testcase text of this type.
124         :rtype: Testcase
125         """
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}}
130 '''
131         return cls(template_string)