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