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