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