Merge "Replace assertEqual(x, True|False) with assert[True|False](x)"
[yardstick.git] / tests / unit / network_services / vnf_generic / vnf / test_tg_rfc2544_ixia.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 import os
19 import unittest
20 import mock
21
22 from tests.unit import STL_MOCKS
23
24 STLClient = mock.MagicMock()
25 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
26 stl_patch.start()
27
28 if stl_patch:
29     from yardstick.network_services.vnf_generic.vnf.tg_rfc2544_ixia import IxiaTrafficGen
30     from yardstick.network_services.vnf_generic.vnf.tg_rfc2544_ixia import IxiaRfc2544Helper
31     from yardstick.network_services.vnf_generic.vnf.tg_rfc2544_ixia import IxiaResourceHelper
32     from yardstick.network_services.traffic_profile.base import TrafficProfile
33
34 TEST_FILE_YAML = 'nsb_test_case.yaml'
35
36 NAME = "tg__1"
37
38
39 @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_rfc2544_ixia.IxNextgen")
40 class TestIxiaResourceHelper(unittest.TestCase):
41     def test___init___with_custom_rfc_helper(self, *args):
42         class MyRfcHelper(IxiaRfc2544Helper):
43             pass
44
45         ixia_resource_helper = IxiaResourceHelper(mock.Mock(), MyRfcHelper)
46         self.assertIsInstance(ixia_resource_helper.rfc_helper, MyRfcHelper)
47
48     def test_stop_collect_with_client(self, *args):
49         mock_client = mock.Mock()
50
51         ixia_resource_helper = IxiaResourceHelper(mock.Mock())
52
53         ixia_resource_helper.client = mock_client
54         ixia_resource_helper.stop_collect()
55         self.assertEqual(mock_client.ix_stop_traffic.call_count, 1)
56
57
58 @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_rfc2544_ixia.IxNextgen")
59 class TestIXIATrafficGen(unittest.TestCase):
60     VNFD = {'vnfd:vnfd-catalog':
61                 {'vnfd':
62                      [{'short-name': 'VpeVnf',
63                        'vdu':
64                            [{'routing_table':
65                                  [{'network': '152.16.100.20',
66                                    'netmask': '255.255.255.0',
67                                    'gateway': '152.16.100.20',
68                                    'if': 'xe0'},
69                                   {'network': '152.16.40.20',
70                                    'netmask': '255.255.255.0',
71                                    'gateway': '152.16.40.20',
72                                    'if': 'xe1'}],
73                              'description': 'VPE approximation using DPDK',
74                              'name': 'vpevnf-baremetal',
75                              'nd_route_tbl':
76                                  [{'network': '0064:ff9b:0:0:0:0:9810:6414',
77                                    'netmask': '112',
78                                    'gateway': '0064:ff9b:0:0:0:0:9810:6414',
79                                    'if': 'xe0'},
80                                   {'network': '0064:ff9b:0:0:0:0:9810:2814',
81                                    'netmask': '112',
82                                    'gateway': '0064:ff9b:0:0:0:0:9810:2814',
83                                    'if': 'xe1'}],
84                              'id': 'vpevnf-baremetal',
85                              'external-interface':
86                                  [{'virtual-interface':
87                                        {'dst_mac': '00:00:00:00:00:04',
88                                         'vpci': '0000:05:00.0',
89                                         'local_ip': '152.16.100.19',
90                                         'type': 'PCI-PASSTHROUGH',
91                                         'netmask': '255.255.255.0',
92                                         'dpdk_port_num': 0,
93                                         'bandwidth': '10 Gbps',
94                                         'driver': "i40e",
95                                         'dst_ip': '152.16.100.20',
96                                         'local_iface_name': 'xe0',
97                                         'local_mac': '00:00:00:00:00:02'},
98                                    'vnfd-connection-point-ref': 'xe0',
99                                    'name': 'xe0'},
100                                   {'virtual-interface':
101                                        {'dst_mac': '00:00:00:00:00:03',
102                                         'vpci': '0000:05:00.1',
103                                         'local_ip': '152.16.40.19',
104                                         'type': 'PCI-PASSTHROUGH',
105                                         'driver': "i40e",
106                                         'netmask': '255.255.255.0',
107                                         'dpdk_port_num': 1,
108                                         'bandwidth': '10 Gbps',
109                                         'dst_ip': '152.16.40.20',
110                                         'local_iface_name': 'xe1',
111                                         'local_mac': '00:00:00:00:00:01'},
112                                    'vnfd-connection-point-ref': 'xe1',
113                                    'name': 'xe1'}]}],
114                        'description': 'Vpe approximation using DPDK',
115                        'mgmt-interface':
116                            {'vdu-id': 'vpevnf-baremetal',
117                             'host': '1.1.1.1',
118                             'password': 'r00t',
119                             'user': 'root',
120                             'ip': '1.1.1.1'},
121                        'benchmark':
122                            {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
123                        'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
124                                             {'type': 'VPORT', 'name': 'xe1'}],
125                        'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'}]}}
126
127     TRAFFIC_PROFILE = {
128         "schema": "isb:traffic_profile:0.1",
129         "name": "fixed",
130         "description": "Fixed traffic profile to run UDP traffic",
131         "traffic_profile": {
132             "traffic_type": "FixedTraffic",
133             "frame_rate": 100,  # pps
134             "flow_number": 10,
135             "frame_size": 64}}
136
137     TC_YAML = {'scenarios': [{'tc_options':
138                                   {'rfc2544': {'allowed_drop_rate': '0.8 - 1'}},
139                               'runner': {'duration': 400,
140                                          'interval': 35, 'type': 'Duration'},
141                               'traffic_options':
142                                   {'flow': 'ipv4_1flow_Packets_vpe.yaml',
143                                    'imix': 'imix_voice.yaml'},
144                               'vnf_options': {'vpe': {'cfg': 'vpe_config'}},
145                               'traffic_profile': 'ipv4_throughput_vpe.yaml',
146                               'type': 'NSPerf',
147                               'nodes': {'tg__1': 'trafficgen_1.yardstick',
148                                         'vnf__1': 'vnf.yardstick'},
149                               'topology': 'vpe_vnf_topology.yaml'}],
150                'context': {'nfvi_type': 'baremetal', 'type': 'Node',
151                            'name': 'yardstick',
152                            'file': '/etc/yardstick/nodes/pod.yaml'},
153                'schema': 'yardstick:task:0.1'}
154
155     def test___init__(self, *args):
156         with mock.patch("yardstick.ssh.SSH") as ssh:
157             ssh_mock = mock.Mock(autospec=ssh.SSH)
158             ssh_mock.execute = \
159                 mock.Mock(return_value=(0, "", ""))
160             ssh.from_node.return_value = ssh_mock
161             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
162             # NOTE(ralonsoh): check the object returned.
163             IxiaTrafficGen(NAME, vnfd)
164
165     def test_listen_traffic(self, *args):
166         with mock.patch("yardstick.ssh.SSH") as ssh:
167             ssh_mock = mock.Mock(autospec=ssh.SSH)
168             ssh_mock.execute = \
169                 mock.Mock(return_value=(0, "", ""))
170             ssh.from_node.return_value = ssh_mock
171             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
172             ixnet_traffic_gen = IxiaTrafficGen(NAME, vnfd)
173             self.assertEqual(None, ixnet_traffic_gen.listen_traffic({}))
174
175     def test_instantiate(self, *args):
176         with mock.patch("yardstick.ssh.SSH") as ssh:
177             ssh_mock = mock.Mock(autospec=ssh.SSH)
178             ssh_mock.execute = \
179                 mock.Mock(return_value=(0, "", ""))
180             ssh_mock.run = \
181                 mock.Mock(return_value=(0, "", ""))
182             ssh.from_node.return_value = ssh_mock
183             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
184             ixnet_traffic_gen = IxiaTrafficGen(NAME, vnfd)
185             scenario_cfg = {'tc': "nsb_test_case", "topology": "",
186                             'ixia_profile': "ixload.cfg"}
187             scenario_cfg.update({'options': {'packetsize': 64,
188                                              'traffic_type': 4,
189                                              'rfc2544': {'allowed_drop_rate': '0.8 - 1'},
190                                              'vnf__1': {'rules': 'acl_1rule.yaml',
191                                                         'vnf_config': {'lb_config': 'SW',
192                                                                        'lb_count': 1,
193                                                                        'worker_config':
194                                                                            '1C/1T',
195                                                                        'worker_threads': 1}}
196                                              }})
197             ixnet_traffic_gen.topology = ""
198             ixnet_traffic_gen.get_ixobj = mock.MagicMock()
199             ixnet_traffic_gen._ixia_traffic_gen = mock.MagicMock()
200             ixnet_traffic_gen._ixia_traffic_gen._connect = mock.Mock()
201             self.assertRaises(
202                 IOError,
203                 ixnet_traffic_gen.instantiate(scenario_cfg, {}))
204
205     def test_collect_kpi(self, *args):
206         with mock.patch("yardstick.ssh.SSH") as ssh:
207             ssh_mock = mock.Mock(autospec=ssh.SSH)
208             ssh_mock.execute = \
209                 mock.Mock(return_value=(0, "", ""))
210             ssh.from_node.return_value = ssh_mock
211             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
212             ixnet_traffic_gen = IxiaTrafficGen(NAME, vnfd)
213             ixnet_traffic_gen.data = {}
214             restult = ixnet_traffic_gen.collect_kpi()
215             self.assertEqual({}, restult)
216
217     def test_terminate(self, *args):
218         with mock.patch("yardstick.ssh.SSH") as ssh:
219             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
220             ssh_mock = mock.Mock(autospec=ssh.SSH)
221             ssh_mock.execute = \
222                 mock.Mock(return_value=(0, "", ""))
223             ssh.from_node.return_value = ssh_mock
224             ixnet_traffic_gen = IxiaTrafficGen(NAME, vnfd)
225             ixnet_traffic_gen._terminated = mock.MagicMock()
226             ixnet_traffic_gen._terminated.value = 0
227             ixnet_traffic_gen._ixia_traffic_gen = mock.MagicMock()
228             ixnet_traffic_gen._ixia_traffic_gen.ix_stop_traffic = mock.Mock()
229             ixnet_traffic_gen._traffic_process = mock.MagicMock()
230             ixnet_traffic_gen._traffic_process.terminate = mock.Mock()
231             self.assertEqual(None, ixnet_traffic_gen.terminate())
232
233     def _get_file_abspath(self, filename):
234         curr_path = os.path.dirname(os.path.abspath(__file__))
235         file_path = os.path.join(curr_path, filename)
236         return file_path
237
238     def test__check_status(self, *args):
239         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
240         sut = IxiaTrafficGen('vnf1', vnfd)
241         sut._check_status()
242
243     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_rfc2544_ixia.time")
244     @mock.patch("yardstick.ssh.SSH")
245     def test_traffic_runner(self, mock_ssh, *args):
246         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
247         mock_traffic_profile.get_traffic_definition.return_value = "64"
248         mock_traffic_profile.params = self.TRAFFIC_PROFILE
249         # traffic_profile.ports is standardized on port_num
250         mock_traffic_profile.ports = [0, 1]
251
252         mock_ssh_instance = mock.Mock(autospec=mock_ssh.SSH)
253         mock_ssh_instance.execute.return_value = 0, "", ""
254         mock_ssh_instance.run.return_value = 0, "", ""
255
256         mock_ssh.from_node.return_value = mock_ssh_instance
257
258         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
259         vnfd["mgmt-interface"].update({
260             'tg-config': {
261                 "ixchassis": "1.1.1.1",
262                 "py_bin_path": "/root",
263             }
264         })
265
266         samples = {}
267         name = ''
268         for ifname in range(1):
269             name = "xe{}".format(ifname)
270             samples[name] = {
271                 "Rx_Rate_Kbps": 20,
272                 "Tx_Rate_Kbps": 20,
273                 "Rx_Rate_Mbps": 10,
274                 "Tx_Rate_Mbps": 10,
275                 "RxThroughput": 10,
276                 "TxThroughput": 10,
277                 "Valid_Frames_Rx": 1000,
278                 "Frames_Tx": 1000,
279                 "in_packets": 1000,
280                 "out_packets": 1000,
281             }
282
283         samples.update({"CurrentDropPercentage": 0.0})
284
285         last_res = [
286             0,
287             {
288                 "Rx_Rate_Kbps": [20, 20],
289                 "Tx_Rate_Kbps": [20, 20],
290                 "Rx_Rate_Mbps": [10, 10],
291                 "Tx_Rate_Mbps": [10, 10],
292                 "CurrentDropPercentage": [0, 0],
293                 "RxThroughput": [10, 10],
294                 "TxThroughput": [10, 10],
295                 "Frames_Tx": [1000, 1000],
296                 "in_packets": [1000, 1000],
297                 "Valid_Frames_Rx": [1000, 1000],
298                 "out_packets": [1000, 1000],
299             },
300         ]
301
302         mock_traffic_profile.execute_traffic.return_value = ['Completed', samples]
303         mock_traffic_profile.get_drop_percentage.return_value = ['Completed', samples]
304
305         sut = IxiaTrafficGen(name, vnfd)
306         sut.vnf_port_pairs = [[[0], [1]]]
307         sut.tc_file_name = self._get_file_abspath(TEST_FILE_YAML)
308         sut.topology = ""
309
310         sut.ssh_helper = mock.Mock()
311         sut._traffic_process = mock.MagicMock()
312         sut.generate_port_pairs = mock.Mock()
313
314         sut._ixia_traffic_gen = mock.MagicMock()
315         sut._ixia_traffic_gen.ix_get_statistics.return_value = last_res
316
317         sut.resource_helper.client = mock.MagicMock()
318         sut.resource_helper.client_started = mock.MagicMock()
319         sut.resource_helper.client_started.value = 1
320         sut.resource_helper.rfc_helper.iteration.value = 11
321
322         sut.scenario_helper.scenario_cfg = {
323             'options': {
324                 'packetsize': 64,
325                 'traffic_type': 4,
326                 'rfc2544': {
327                     'allowed_drop_rate': '0.8 - 1',
328                     'latency': True
329                 },
330                 'vnf__1': {
331                     'rules': 'acl_1rule.yaml',
332                     'vnf_config': {
333                         'lb_config': 'SW',
334                         'lb_count': 1,
335                         'worker_config': '1C/1T',
336                         'worker_threads': 1,
337                     },
338                 },
339             },
340             'ixia_profile': '/path/to/profile',
341             'task_path': '/path/to/task'
342         }
343
344         @mock.patch('yardstick.benchmark.scenarios.networking.vnf_generic.open', create=True)
345         @mock.patch('yardstick.network_services.vnf_generic.vnf.tg_rfc2544_ixia.open',
346                     mock.mock_open(), create=True)
347         @mock.patch('yardstick.network_services.vnf_generic.vnf.tg_rfc2544_ixia.LOG.exception')
348         def _traffic_runner(*args):
349             result = sut._traffic_runner(mock_traffic_profile)
350             self.assertIsNone(result)
351
352         _traffic_runner()