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