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