4f92e6c2e108ffaa52a93a4cbafc3cbfec62d211
[csit.git] / resources / libraries / python / autogen / Testcase.py
1 # Copyright (c) 2018 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(object):
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             - tc_num - Start of testcase name, example: "tc04".
33         :type template_string: str
34         """
35         self.template = Template(template_string)
36
37     def generate(self, num, framesize, phy_cores):
38         """Return string of test case code with placeholders filled.
39
40         Fail if there are placeholders left unfilled.
41
42         :param num: Test case number. Example value: 4.
43         :param framesize: Imix string or numeric frame size. Example: 74.
44         :param phy_cores: Number of physical cores to use. Example: 2.
45         :type num: int
46         :type framesize: str or int
47         :type phy_cores: int or str
48         :returns: Filled template, usable as test case code.
49         :rtype: str
50         """
51         try:
52             fs = int(framesize)
53             subst_dict = {
54                 "frame_num": "${%d}" % fs,
55                 "frame_str": "%dB" % fs
56             }
57         except ValueError:  # Assuming an IMIX string.
58             subst_dict = {
59                 "frame_num": str(framesize),
60                 "frame_str": "IMIX"
61             }
62         cores_str = str(phy_cores)
63         cores_num = int(cores_str)
64         subst_dict.update(
65             {
66                 "cores_num": "${%d}" % cores_num,
67                 "cores_str": phy_cores,
68                 "tc_num": "tc{num:02d}".format(num=num)
69             })
70         return self.template.substitute(subst_dict)