Merge "Adding Test Cases for Prox PktTouch Standalone SRIOV"
[yardstick.git] / yardstick / tests / unit / network_services / vnf_generic / vnf / test_tg_ixload.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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
15 import subprocess
16
17 import mock
18 import six
19
20 from yardstick import ssh
21 from yardstick.benchmark.contexts import base as ctx_base
22 from yardstick.common import utils
23 from yardstick.network_services.vnf_generic.vnf import tg_ixload
24 from yardstick.network_services.traffic_profile import base as tp_base
25 from yardstick.tests.unit import base as ut_base
26
27
28 NAME = "tg__1"
29
30
31 class TestIxLoadTrafficGen(ut_base.BaseUnitTestCase):
32     VNFD = {'vnfd:vnfd-catalog':
33             {'vnfd':
34              [{'short-name': 'VpeVnf',
35                'vdu':
36                [{'routing_table':
37                  [{'network': '152.16.100.20',
38                    'netmask': '255.255.255.0',
39                    'gateway': '152.16.100.20',
40                    'if': 'xe0'},
41                   {'network': '152.16.40.20',
42                    'netmask': '255.255.255.0',
43                    'gateway': '152.16.40.20',
44                    'if': 'xe1'}],
45                  'description': 'VPE approximation using DPDK',
46                  'name': 'vpevnf-baremetal',
47                  'nd_route_tbl':
48                  [{'network': '0064:ff9b:0:0:0:0:9810:6414',
49                    'netmask': '112',
50                    'gateway': '0064:ff9b:0:0:0:0:9810:6414',
51                    'if': 'xe0'},
52                   {'network': '0064:ff9b:0:0:0:0:9810:2814',
53                    'netmask': '112',
54                    'gateway': '0064:ff9b:0:0:0:0:9810:2814',
55                    'if': 'xe1'}],
56                  'id': 'vpevnf-baremetal',
57                  'external-interface':
58                  [{'virtual-interface':
59                    {'dst_mac': '00:00:00:00:00:04',
60                     'vld_id': 'uplink_0',
61                     'vpci': '0000:05:00.0',
62                     'local_ip': '152.16.100.19',
63                     'type': 'PCI-PASSTHROUGH',
64                     'netmask': '255.255.255.0',
65                     'dpdk_port_num': 0,
66                     'bandwidth': '10 Gbps',
67                     'driver': "i40e",
68                     'dst_ip': '152.16.100.20',
69                     'local_iface_name': 'xe0',
70                     'local_mac': '00:00:00:00:00:02'},
71                    'vnfd-connection-point-ref': 'xe0',
72                    'name': 'xe0'},
73                   {'virtual-interface':
74                    {'dst_mac': '00:00:00:00:00:03',
75                     'vld_id': 'downlink_0',
76                     'vpci': '0000:05:00.1',
77                     'local_ip': '152.16.40.19',
78                     'type': 'PCI-PASSTHROUGH',
79                     'driver': "i40e",
80                     'netmask': '255.255.255.0',
81                     'dpdk_port_num': 1,
82                     'bandwidth': '10 Gbps',
83                     'dst_ip': '152.16.40.20',
84                     'local_iface_name': 'xe1',
85                     'local_mac': '00:00:00:00:00:01'},
86                    'vnfd-connection-point-ref': 'xe1',
87                    'name': 'xe1'}]}],
88                'description': 'Vpe approximation using DPDK',
89                'mgmt-interface':
90                    {'vdu-id': 'vpevnf-baremetal',
91                     'host': '1.1.1.1',
92                     'password': 'r00t',
93                     'user': 'root',
94                     'ip': '1.1.1.1'},
95                'benchmark':
96                    {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
97                'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
98                                     {'type': 'VPORT', 'name': 'xe1'}],
99                'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'}]}}
100
101     TRAFFIC_PROFILE = {
102         "schema": "isb:traffic_profile:0.1",
103         "name": "fixed",
104         "description": "Fixed traffic profile to run UDP traffic",
105         "traffic_profile": {
106             "traffic_type": "FixedTraffic",
107             "frame_rate": 100,  # pps
108             "flow_number": 10,
109             "frame_size": 64}}
110
111     def setUp(self):
112         self._mock_call = mock.patch.object(subprocess, 'call')
113         self.mock_call = self._mock_call.start()
114         self._mock_open = mock.patch.object(tg_ixload, 'open')
115         self.mock_open = self._mock_open.start()
116         self._mock_ssh = mock.patch.object(ssh, 'SSH')
117         self.mock_ssh = self._mock_ssh.start()
118         ssh_obj_mock = mock.Mock(autospec=ssh.SSH)
119         ssh_obj_mock.execute = mock.Mock(return_value=(0, '', ''))
120         ssh_obj_mock.run = mock.Mock(return_value=(0, '', ''))
121         self.mock_ssh.from_node.return_value = ssh_obj_mock
122         self.addCleanup(self._stop_mock)
123
124     def _stop_mock(self):
125         self._mock_call.stop()
126         self._mock_open.stop()
127         self._mock_ssh.stop()
128
129     def test___init__(self):
130         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
131         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
132         self.assertIsNone(ixload_traffic_gen.resource_helper.data)
133
134     def test_update_gateways(self):
135         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
136         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
137         links = {'uplink_0': {'ip': {}},
138                  'downlink_1': {'ip': {}}}
139
140         ixload_traffic_gen.update_gateways(links)
141
142         self.assertEqual("152.16.100.20", links["uplink_0"]["ip"]["gateway"])
143         self.assertEqual("0.0.0.0", links["downlink_1"]["ip"]["gateway"])
144
145     @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server',
146                        return_value='mock_node')
147     def test_collect_kpi(self, *args):
148         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
149         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
150         ixload_traffic_gen.scenario_helper.scenario_cfg = {
151             'nodes': {ixload_traffic_gen.name: "mock"}
152         }
153         ixload_traffic_gen.data = {}
154         result = ixload_traffic_gen.collect_kpi()
155
156         expected = {
157             'physical_node': 'mock_node',
158             'collect_stats': {}}
159         self.assertEqual(expected, result)
160
161     def test_listen_traffic(self):
162         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
163         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
164         self.assertIsNone(ixload_traffic_gen.listen_traffic({}))
165
166     @mock.patch.object(utils, 'find_relative_file')
167     @mock.patch.object(utils, 'makedirs')
168     @mock.patch.object(ctx_base.Context, 'get_context_from_server')
169     @mock.patch.object(tg_ixload, 'shutil')
170     def test_instantiate(self, mock_shutil, *args):
171         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
172         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
173         scenario_cfg = {'tc': "nsb_test_case",
174                         'ixia_profile': "ixload.cfg",
175                         'task_path': "/path/to/task"}
176         ixload_traffic_gen.RESULTS_MOUNT = "/tmp/result"
177         mock_shutil.copy = mock.Mock()
178         scenario_cfg.update(
179             {'options':
180                  {'packetsize': 64, 'traffic_type': 4,
181                   'rfc2544': {'allowed_drop_rate': '0.8 - 1'},
182                   'vnf__1': {'rules': 'acl_1rule.yaml',
183                              'vnf_config': {'lb_config': 'SW',
184                                             'lb_count': 1,
185                                             'worker_config':
186                                                 '1C/1T',
187                                             'worker_threads': 1}}
188                   }
189              }
190         )
191         scenario_cfg.update({'nodes': {ixload_traffic_gen.name: "mock"}})
192         with mock.patch.object(six.moves.builtins, 'open',
193                                create=True) as mock_open:
194             mock_open.return_value = mock.MagicMock()
195             ixload_traffic_gen.instantiate(scenario_cfg, {})
196
197     @mock.patch.object(tg_ixload, 'open')
198     @mock.patch.object(tg_ixload, 'min')
199     @mock.patch.object(tg_ixload, 'max')
200     @mock.patch.object(tg_ixload, 'len')
201     @mock.patch.object(tg_ixload, 'shutil')
202     def test_run_traffic(self, *args):
203         mock_traffic_profile = mock.Mock(autospec=tp_base.TrafficProfile)
204         mock_traffic_profile.get_traffic_definition.return_value = '64'
205         mock_traffic_profile.get_links_param.return_value = {
206             'uplink_0': {'ip': {}}}
207         mock_traffic_profile.params = self.TRAFFIC_PROFILE
208         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
209         vnfd['mgmt-interface'].update({'tg-config': {}})
210         vnfd['mgmt-interface']['tg-config'].update({'ixchassis': '1.1.1.1'})
211         vnfd['mgmt-interface']['tg-config'].update({'py_bin_path': '/root'})
212         sut = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
213         sut.connection = mock.Mock()
214         sut._traffic_runner = mock.Mock(return_value=0)
215         result = sut.run_traffic(mock_traffic_profile)
216         self.assertIsNone(result)
217
218     @mock.patch.object(tg_ixload, 'open')
219     @mock.patch.object(tg_ixload, 'min')
220     @mock.patch.object(tg_ixload, 'max')
221     @mock.patch.object(tg_ixload, 'len')
222     @mock.patch.object(tg_ixload, 'shutil')
223     def test_run_traffic_csv(self, *args):
224         mock_traffic_profile = mock.Mock(autospec=tp_base.TrafficProfile)
225         mock_traffic_profile.get_traffic_definition.return_value = '64'
226         mock_traffic_profile.get_links_param.return_value = {
227             'uplink_0': {'ip': {}}}
228         mock_traffic_profile.params = self.TRAFFIC_PROFILE
229         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
230         vnfd['mgmt-interface'].update({'tg-config': {}})
231         vnfd['mgmt-interface']['tg-config'].update({'ixchassis': '1.1.1.1'})
232         vnfd['mgmt-interface']['tg-config'].update({'py_bin_path': '/root'})
233         sut = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
234         sut.connection = mock.Mock()
235         sut._traffic_runner = mock.Mock(return_value=0)
236         subprocess.call(['touch', '/tmp/1.csv'])
237         sut.rel_bin_path = mock.Mock(return_value='/tmp/*.csv')
238         result = sut.run_traffic(mock_traffic_profile)
239         self.assertIsNone(result)
240
241     def test_terminate(self):
242         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
243         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
244         self.assertIsNone(ixload_traffic_gen.terminate())
245
246     def test_parse_csv_read(self):
247         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
248         kpi_data = {
249             'HTTP Total Throughput (Kbps)': 1,
250             'HTTP Simulated Users': 2,
251             'HTTP Concurrent Connections': '3',
252             'HTTP Connection Rate': 4.3,
253             'HTTP Transaction Rate': True,
254         }
255         http_reader = [kpi_data]
256         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
257         result = ixload_traffic_gen.resource_helper.result
258         ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)
259         for k_left, k_right in tg_ixload.IxLoadResourceHelper.KPI_LIST.items():
260             self.assertEqual(result[k_left][-1], int(kpi_data[k_right]))
261
262     def test_parse_csv_read_value_error(self):
263         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
264         http_reader = [{
265             'HTTP Total Throughput (Kbps)': 1,
266             'HTTP Simulated Users': 2,
267             'HTTP Concurrent Connections': "not a number",
268             'HTTP Connection Rate': 4,
269             'HTTP Transaction Rate': 5,
270         }]
271         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
272         init_value = ixload_traffic_gen.resource_helper.result
273         ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)
274         self.assertDictEqual(ixload_traffic_gen.resource_helper.result,
275                              init_value)
276
277     def test_parse_csv_read_error(self,):
278         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
279         http_reader = [{
280             'HTTP Total Throughput (Kbps)': 1,
281             'HTTP Simulated Users': 2,
282             'HTTP Concurrent Connections': 3,
283             'HTTP Transaction Rate': 5,
284         }]
285         ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
286
287         with self.assertRaises(KeyError):
288             ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)