1 # Copyright 2015 Intel Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """TestCase base class
21 from collections import OrderedDict
23 import core.component_factory as component_factory
24 from core.loader import Loader
26 class TestCase(object):
27 """TestCase base class
29 In this basic form runs RFC2544 throughput test
31 def __init__(self, cfg, results_dir):
32 """Pull out fields from test config
34 :param cfg: A dictionary of string-value pairs describing the test
35 configuration. Both the key and values strings use well-known
37 :param results_dir: Where the csv formatted results are written.
39 self._logger = logging.getLogger(__name__)
40 self.name = cfg['Name']
41 self.desc = cfg.get('Description', 'No description given.')
42 self._traffic_type = cfg['Traffic Type']
43 self._deployment = cfg['Deployment']
44 self._collector = cfg['Collector']
45 self._bidir = cfg['biDirectional']
46 self._frame_mod = cfg.get('Frame Modification', None)
48 self._frame_mod = self._frame_mod.lower()
49 self._results_dir = results_dir
50 self._multistream = cfg.get('MultiStream', 0)
55 All setup and teardown through controllers is included.
57 self._logger.debug(self.name)
59 self._logger.debug("Controllers:")
61 traffic_ctl = component_factory.create_traffic(
63 loader.get_trafficgen_class())
64 vnf_ctl = component_factory.create_vnf(
66 loader.get_vnf_class())
67 vswitch_ctl = component_factory.create_vswitch(
69 loader.get_vswitch_class())
70 collector_ctl = component_factory.create_collector(
72 loader.get_collector_class())
75 self._logger.debug("Setup:")
76 collector_ctl.log_cpu_stats()
80 traffic = {'traffic_type': self._traffic_type,
82 'multistream': self._multistream}
83 vswitch = vswitch_ctl.get_vswitch()
84 if self._frame_mod == "vlan":
85 flow = {'table':'2', 'priority':'1000', 'metadata':'2', 'actions': ['push_vlan:0x8100','goto_table:3']}
86 vswitch.add_flow('br0', flow)
87 flow = {'table':'2', 'priority':'1000', 'metadata':'1', 'actions': ['push_vlan:0x8100','goto_table:3']}
88 vswitch.add_flow('br0', flow)
91 traffic_ctl.send_traffic(traffic)
94 self._logger.debug("Traffic Results:")
95 traffic_ctl.print_results()
97 self._logger.debug("Collector Results:")
98 self._logger.debug(collector_ctl.get_results())
101 output_file = "result_" + self.name + "_" + self._deployment +".csv"
103 self._write_result_to_file(
104 traffic_ctl.get_results(),
105 os.path.join(self._results_dir, output_file))
108 def _write_result_to_file(results, output):
109 """Write list of dictionaries to a CSV file.
111 Each element on list will create separate row in output file.
112 If output file already exists, data will be appended at the end,
113 otherwise it will be created.
115 :param results: list of dictionaries.
116 :param output: path to output file.
118 with open(output, 'a') as csvfile:
120 logging.info("Write results to file: " + output)
121 fieldnames = TestCase._get_unique_keys(results)
123 writer = csv.DictWriter(csvfile, fieldnames)
125 if not csvfile.tell(): # file is now empty
128 for result in results:
129 writer.writerow(result)
133 def _get_unique_keys(list_of_dicts):
134 """Gets unique key values as ordered list of strings in given dicts
136 :param list_of_dicts: list of dictionaries.
138 :returns: list of unique keys(strings).
140 result = OrderedDict()
141 for item in list_of_dicts:
142 for key in item.keys():
145 return list(result.keys())