Merge "Proposing Abhijit Sinha as a committer in Yardstick"
[yardstick.git] / tests / unit / network_services / vnf_generic / vnf / test_tg_ixload.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016-2017 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 from __future__ import absolute_import
19
20 import unittest
21 import mock
22 import subprocess
23
24 from tests.unit import STL_MOCKS
25
26
27 STLClient = mock.MagicMock()
28 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
29 stl_patch.start()
30
31 if stl_patch:
32     from yardstick.network_services.vnf_generic.vnf.tg_ixload import IxLoadTrafficGen
33     from yardstick.network_services.vnf_generic.vnf.tg_ixload import IxLoadResourceHelper
34     from yardstick.network_services.traffic_profile.base import TrafficProfile
35
36
37 NAME = "tg__1"
38
39
40 class TestIxLoadTrafficGen(unittest.TestCase):
41     VNFD = {'vnfd:vnfd-catalog':
42             {'vnfd':
43              [{'short-name': 'VpeVnf',
44                'vdu':
45                [{'routing_table':
46                  [{'network': '152.16.100.20',
47                    'netmask': '255.255.255.0',
48                    'gateway': '152.16.100.20',
49                    'if': 'xe0'},
50                   {'network': '152.16.40.20',
51                    'netmask': '255.255.255.0',
52                    'gateway': '152.16.40.20',
53                    'if': 'xe1'}],
54                  'description': 'VPE approximation using DPDK',
55                  'name': 'vpevnf-baremetal',
56                  'nd_route_tbl':
57                  [{'network': '0064:ff9b:0:0:0:0:9810:6414',
58                    'netmask': '112',
59                    'gateway': '0064:ff9b:0:0:0:0:9810:6414',
60                    'if': 'xe0'},
61                   {'network': '0064:ff9b:0:0:0:0:9810:2814',
62                    'netmask': '112',
63                    'gateway': '0064:ff9b:0:0:0:0:9810:2814',
64                    'if': 'xe1'}],
65                  'id': 'vpevnf-baremetal',
66                  'external-interface':
67                  [{'virtual-interface':
68                    {'dst_mac': '00:00:00:00:00:04',
69                     'vpci': '0000:05:00.0',
70                     'local_ip': '152.16.100.19',
71                     'type': 'PCI-PASSTHROUGH',
72                     'netmask': '255.255.255.0',
73                     'dpdk_port_num': 0,
74                     'bandwidth': '10 Gbps',
75                     'driver': "i40e",
76                     'dst_ip': '152.16.100.20',
77                     'local_iface_name': 'xe0',
78                     'local_mac': '00:00:00:00:00:02'},
79                    'vnfd-connection-point-ref': 'xe0',
80                    'name': 'xe0'},
81                   {'virtual-interface':
82                    {'dst_mac': '00:00:00:00:00:03',
83                     'vpci': '0000:05:00.1',
84                     'local_ip': '152.16.40.19',
85                     'type': 'PCI-PASSTHROUGH',
86                     'driver': "i40e",
87                     'netmask': '255.255.255.0',
88                     'dpdk_port_num': 1,
89                     'bandwidth': '10 Gbps',
90                     'dst_ip': '152.16.40.20',
91                     'local_iface_name': 'xe1',
92                     'local_mac': '00:00:00:00:00:01'},
93                    'vnfd-connection-point-ref': 'xe1',
94                    'name': 'xe1'}]}],
95                'description': 'Vpe approximation using DPDK',
96                'mgmt-interface':
97                    {'vdu-id': 'vpevnf-baremetal',
98                     'host': '1.1.1.1',
99                     'password': 'r00t',
100                     'user': 'root',
101                     'ip': '1.1.1.1'},
102                'benchmark':
103                    {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
104                'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
105                                     {'type': 'VPORT', 'name': 'xe1'}],
106                'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'}]}}
107
108     TRAFFIC_PROFILE = {
109         "schema": "isb:traffic_profile:0.1",
110         "name": "fixed",
111         "description": "Fixed traffic profile to run UDP traffic",
112         "traffic_profile": {
113             "traffic_type": "FixedTraffic",
114             "frame_rate": 100,  # pps
115             "flow_number": 10,
116             "frame_size": 64}}
117
118     def test___init__(self):
119         with mock.patch("yardstick.ssh.SSH") as ssh:
120             ssh_mock = mock.Mock(autospec=ssh.SSH)
121             ssh_mock.execute = \
122                 mock.Mock(return_value=(0, "", ""))
123             ssh.from_node.return_value = ssh_mock
124             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
125             ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd)
126             self.assertIsNone(ixload_traffic_gen.resource_helper.data)
127
128     def test_collect_kpi(self):
129         with mock.patch("yardstick.ssh.SSH") as ssh:
130             ssh_mock = mock.Mock(autospec=ssh.SSH)
131             ssh_mock.execute = \
132                 mock.Mock(return_value=(0, "", ""))
133             ssh.from_node.return_value = ssh_mock
134             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
135             ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd)
136             ixload_traffic_gen.data = {}
137             restult = ixload_traffic_gen.collect_kpi()
138             self.assertEqual({}, restult)
139
140     def test_listen_traffic(self):
141         with mock.patch("yardstick.ssh.SSH") as ssh:
142             ssh_mock = mock.Mock(autospec=ssh.SSH)
143             ssh_mock.execute = \
144                 mock.Mock(return_value=(0, "", ""))
145             ssh.from_node.return_value = ssh_mock
146             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
147             ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd)
148             self.assertEqual(None, ixload_traffic_gen.listen_traffic({}))
149
150     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.makedirs")
151     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call")
152     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil")
153     def test_instantiate(self, call, shutil, mock_makedirs):
154         # pylint: disable=unused-argument
155         with mock.patch("yardstick.ssh.SSH") as ssh:
156             ssh_mock = mock.Mock(autospec=ssh.SSH)
157             ssh_mock.execute = \
158                 mock.Mock(return_value=(0, "", ""))
159             ssh_mock.run = \
160                 mock.Mock(return_value=(0, "", ""))
161             ssh.from_node.return_value = ssh_mock
162             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
163             ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd)
164             scenario_cfg = {'tc': "nsb_test_case",
165                             'ixia_profile': "ixload.cfg",
166                             'task_path': "/path/to/task"}
167             ixload_traffic_gen.RESULTS_MOUNT = "/tmp/result"
168             shutil.copy = mock.Mock()
169             scenario_cfg.update({'options': {'packetsize': 64, 'traffic_type': 4,
170                                              'rfc2544': {'allowed_drop_rate': '0.8 - 1'},
171                                              'vnf__1': {'rules': 'acl_1rule.yaml',
172                                                         'vnf_config': {'lb_config': 'SW',
173                                                                        'lb_count': 1,
174                                                                        'worker_config':
175                                                                        '1C/1T',
176                                                                        'worker_threads': 1}}
177                                              }})
178             with mock.patch('yardstick.benchmark.scenarios.networking.vnf_generic.open',
179                             create=True) as mock_open:
180                 mock_open.return_value = mock.MagicMock()
181                 ixload_traffic_gen.instantiate(scenario_cfg, {})
182
183     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call")
184     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil")
185     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.open")
186     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.min")
187     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.max")
188     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.len")
189     def test_run_traffic(self, call, shutil, main_open, min, max, len):
190         # pylint: disable=unused-argument
191         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
192         mock_traffic_profile.get_traffic_definition.return_value = "64"
193         mock_traffic_profile.params = self.TRAFFIC_PROFILE
194         with mock.patch("yardstick.ssh.SSH") as ssh:
195             ssh_mock = mock.Mock(autospec=ssh.SSH)
196             ssh_mock.execute = \
197                 mock.Mock(return_value=(0, "", ""))
198             ssh_mock.run = \
199                 mock.Mock(return_value=(0, "", ""))
200             ssh.from_node.return_value = ssh_mock
201             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
202             vnfd["mgmt-interface"].update({"tg-config": {}})
203             vnfd["mgmt-interface"]["tg-config"].update({"ixchassis":
204                                                         "1.1.1.1"})
205             vnfd["mgmt-interface"]["tg-config"].update({"py_bin_path":
206                                                         "/root"})
207             sut = IxLoadTrafficGen(NAME, vnfd)
208             sut.connection = mock.Mock()
209             sut.connection.run = mock.Mock()
210             sut._traffic_runner = mock.Mock(return_value=0)
211             shutil.copy = mock.Mock()
212             result = sut.run_traffic(mock_traffic_profile)
213             self.assertIsNone(result)
214
215     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call")
216     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.shutil")
217     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.open")
218     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.min")
219     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.max")
220     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.len")
221     def test_run_traffic_csv(self, call, shutil, main_open, min, max, len):
222         # pylint: disable=unused-argument
223         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
224         mock_traffic_profile.get_traffic_definition.return_value = "64"
225         mock_traffic_profile.params = self.TRAFFIC_PROFILE
226         with mock.patch("yardstick.ssh.SSH") as ssh:
227             ssh_mock = mock.Mock(autospec=ssh.SSH)
228             ssh_mock.execute = \
229                 mock.Mock(return_value=(0, "", ""))
230             ssh_mock.run = \
231                 mock.Mock(return_value=(0, "", ""))
232             ssh.from_node.return_value = ssh_mock
233             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
234             vnfd["mgmt-interface"].update({"tg-config": {}})
235             vnfd["mgmt-interface"]["tg-config"].update({"ixchassis":
236                                                         "1.1.1.1"})
237             vnfd["mgmt-interface"]["tg-config"].update({"py_bin_path":
238                                                         "/root"})
239             sut = IxLoadTrafficGen(NAME, vnfd)
240             sut.connection = mock.Mock()
241             sut.connection.run = mock.Mock()
242             sut._traffic_runner = mock.Mock(return_value=0)
243             shutil.copy = mock.Mock()
244             subprocess.call(["touch", "/tmp/1.csv"])
245             sut.rel_bin_path = mock.Mock(return_value="/tmp/*.csv")
246             result = sut.run_traffic(mock_traffic_profile)
247             self.assertIsNone(result)
248
249     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call")
250     def test_terminate(self, *args):
251         with mock.patch("yardstick.ssh.SSH") as ssh:
252             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
253             ssh_mock = mock.Mock(autospec=ssh.SSH)
254             ssh_mock.execute = \
255                 mock.Mock(return_value=(0, "", ""))
256             ssh.from_node.return_value = ssh_mock
257             ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd)
258             self.assertEqual(None, ixload_traffic_gen.terminate())
259
260     @mock.patch("yardstick.ssh.SSH")
261     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call")
262     def test_parse_csv_read(self, mock_call, mock_ssh):
263         # pylint: disable=unused-argument
264         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
265         kpi_data = {
266             'HTTP Total Throughput (Kbps)': 1,
267             'HTTP Simulated Users': 2,
268             'HTTP Concurrent Connections': '3',
269             'HTTP Connection Rate': 4.3,
270             'HTTP Transaction Rate': True,
271         }
272         http_reader = [kpi_data]
273
274         mock_ssh_type = mock.Mock(autospec=mock_ssh.SSH)
275         mock_ssh_type.execute.return_value = 0, "", ""
276         mock_ssh.from_node.return_value = mock_ssh_type
277
278         ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd)
279         result = ixload_traffic_gen.resource_helper.result
280
281         ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)
282         for key_left, key_right in IxLoadResourceHelper.KPI_LIST.items():
283             self.assertEqual(result[key_left][-1], int(kpi_data[key_right]))
284
285     @mock.patch("yardstick.ssh.SSH")
286     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call")
287     def test_parse_csv_read_value_error(self, mock_call, mock_ssh):
288         # pylint: disable=unused-argument
289         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
290         http_reader = [{
291             'HTTP Total Throughput (Kbps)': 1,
292             'HTTP Simulated Users': 2,
293             'HTTP Concurrent Connections': "not a number",
294             'HTTP Connection Rate': 4,
295             'HTTP Transaction Rate': 5,
296         }]
297
298         mock_ssh_type = mock.Mock(autospec=mock_ssh.SSH)
299         mock_ssh_type.execute.return_value = 0, "", ""
300         mock_ssh.from_node.return_value = mock_ssh_type
301
302         ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd)
303         init_value = ixload_traffic_gen.resource_helper.result
304
305         ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)
306         self.assertDictEqual(ixload_traffic_gen.resource_helper.result, init_value)
307
308     @mock.patch("yardstick.ssh.SSH")
309     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ixload.call")
310     def test_parse_csv_read_error(self, mock_call, mock_ssh):
311         # pylint: disable=unused-argument
312         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
313         http_reader = [{
314             'HTTP Total Throughput (Kbps)': 1,
315             'HTTP Simulated Users': 2,
316             'HTTP Concurrent Connections': 3,
317             'HTTP Transaction Rate': 5,
318         }]
319
320         mock_ssh_type = mock.Mock(autospec=mock_ssh.SSH)
321         mock_ssh_type.execute.return_value = 0, "", ""
322         mock_ssh.from_node.return_value = mock_ssh_type
323
324         ixload_traffic_gen = IxLoadTrafficGen(NAME, vnfd)
325
326         with self.assertRaises(KeyError):
327             ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)