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:
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
19 class Testcase(object):
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, framesize, phy_cores):
38 """Return string of test case code with placeholders filled.
40 Fail if there are placeholders left unfilled.
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.
46 :type framesize: str or int
47 :type phy_cores: int or str
48 :returns: Filled template, usable as test case code.
52 fsize = int(framesize)
54 "frame_num": "${%d}" % fsize,
55 "frame_str": "%dB" % fsize
57 except ValueError: # Assuming an IMIX string.
59 "frame_num": str(framesize),
62 cores_str = str(phy_cores)
63 cores_num = int(cores_str)
66 "cores_num": "${%d}" % cores_num,
67 "cores_str": phy_cores,
68 "tc_num": "tc{num:02d}".format(num=num)
70 return self.template.substitute(subst_dict)