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