Merge "Extend TRex RFC2544 test case collected stats"
[yardstick.git] / yardstick / tests / unit / network_services / vnf_generic / vnf / test_tg_rfc2544_ixia.py
1 # Copyright (c) 2016-2019 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 import ipaddress
21 import time
22 from collections import OrderedDict
23
24 from yardstick.common import utils
25 from yardstick.common import exceptions
26 from yardstick.benchmark import contexts
27 from yardstick.benchmark.contexts import base as ctx_base
28 from yardstick.network_services.libs.ixia_libs.ixnet import ixnet_api
29 from yardstick.network_services.traffic_profile import base as tp_base
30 from yardstick.network_services.vnf_generic.vnf import tg_rfc2544_ixia
31 from yardstick.network_services.traffic_profile import ixia_rfc2544
32
33
34 TEST_FILE_YAML = 'nsb_test_case.yaml'
35
36 NAME = "tg__1"
37
38
39 class TestIxiaResourceHelper(unittest.TestCase):
40
41     def setUp(self):
42         self._mock_IxNextgen = mock.patch.object(ixnet_api, 'IxNextgen')
43         self.mock_IxNextgen = self._mock_IxNextgen.start()
44         self.addCleanup(self._stop_mocks)
45
46     def _stop_mocks(self):
47         self._mock_IxNextgen.stop()
48
49     def test___init___with_custom_rfc_helper(self):
50         class MyRfcHelper(tg_rfc2544_ixia.IxiaRfc2544Helper):
51             pass
52
53         ixia_resource_helper = tg_rfc2544_ixia.IxiaResourceHelper(
54             mock.Mock(), MyRfcHelper)
55         self.assertIsInstance(ixia_resource_helper.rfc_helper, MyRfcHelper)
56
57     def test__init_ix_scenario(self):
58         mock_scenario = mock.Mock()
59         mock_scenario_helper = mock.Mock()
60         mock_scenario_helper.scenario_cfg = {'ixia_config': 'TestScenario',
61                                              'options': 'scenario_options'}
62         mock_setup_helper = mock.Mock(scenario_helper=mock_scenario_helper)
63         ixia_resource_helper = tg_rfc2544_ixia.IxiaResourceHelper(mock_setup_helper)
64         ixia_resource_helper._ixia_scenarios = {'TestScenario': mock_scenario}
65         ixia_resource_helper.client = 'client'
66         ixia_resource_helper.context_cfg = 'context'
67         ixia_resource_helper._init_ix_scenario()
68         mock_scenario.assert_called_once_with('client', 'context', 'scenario_options')
69
70     def test__init_ix_scenario_not_supported_cfg_type(self):
71         mock_scenario_helper = mock.Mock()
72         mock_scenario_helper.scenario_cfg = {'ixia_config': 'FakeScenario',
73                                              'options': 'scenario_options'}
74         mock_setup_helper = mock.Mock(scenario_helper=mock_scenario_helper)
75         ixia_resource_helper = tg_rfc2544_ixia.IxiaResourceHelper(mock_setup_helper)
76         ixia_resource_helper._ixia_scenarios = {'TestScenario': mock.Mock()}
77         with self.assertRaises(RuntimeError):
78             ixia_resource_helper._init_ix_scenario()
79
80     @mock.patch.object(tg_rfc2544_ixia.IxiaResourceHelper, '_init_ix_scenario')
81     def test_setup(self, mock__init_ix_scenario):
82         ixia_resource_helper = tg_rfc2544_ixia.IxiaResourceHelper(mock.Mock())
83         ixia_resource_helper.setup()
84         mock__init_ix_scenario.assert_called_once()
85
86     def test_stop_collect_with_client(self):
87         mock_client = mock.Mock()
88         ixia_resource_helper = tg_rfc2544_ixia.IxiaResourceHelper(mock.Mock())
89         ixia_resource_helper.client = mock_client
90         ixia_resource_helper._ix_scenario = mock.Mock()
91         ixia_resource_helper.stop_collect()
92         self.assertEqual(1, ixia_resource_helper._terminated.value)
93         ixia_resource_helper._ix_scenario.stop_protocols.assert_called_once()
94
95     def test_run_traffic(self):
96         mock_tprofile = mock.Mock()
97         mock_tprofile.config.duration = 10
98         mock_tprofile.get_drop_percentage.return_value = True, 'fake_samples'
99         ixia_rhelper = tg_rfc2544_ixia.IxiaResourceHelper(mock.Mock())
100         ixia_rhelper.rfc_helper = mock.Mock()
101         ixia_rhelper.vnfd_helper = mock.Mock()
102         ixia_rhelper._ix_scenario = mock.Mock()
103         ixia_rhelper.vnfd_helper.port_pairs.all_ports = []
104         with mock.patch.object(ixia_rhelper, 'generate_samples'), \
105                 mock.patch.object(ixia_rhelper, '_build_ports'), \
106                 mock.patch.object(ixia_rhelper, '_initialize_client'), \
107                 mock.patch.object(utils, 'wait_until_true'):
108             ixia_rhelper.run_traffic(mock_tprofile)
109
110         self.assertEqual('fake_samples', ixia_rhelper._queue.get())
111         mock_tprofile.update_traffic_profile.assert_called_once()
112
113     def test_run_test(self):
114         expected_result = {'test': 'fake_samples', 'Iteration': 1}
115         mock_tprofile = mock.Mock()
116         mock_tprofile.config.duration = 10
117         mock_tprofile.get_drop_percentage.return_value = \
118             True, {'test': 'fake_samples', 'Iteration': 1}
119         ixia_rhelper = tg_rfc2544_ixia.IxiaResourceHelper(mock.Mock())
120         tasks_queue = mock.Mock()
121         tasks_queue.get.return_value = 'RUN_TRAFFIC'
122         results_queue = mock.Mock()
123         ixia_rhelper.rfc_helper = mock.Mock()
124         ixia_rhelper.vnfd_helper = mock.Mock()
125         ixia_rhelper._ix_scenario = mock.Mock()
126         ixia_rhelper.vnfd_helper.port_pairs.all_ports = []
127         with mock.patch.object(ixia_rhelper, 'generate_samples'), \
128                 mock.patch.object(ixia_rhelper, '_build_ports'), \
129                 mock.patch.object(ixia_rhelper, '_initialize_client'), \
130                 mock.patch.object(utils, 'wait_until_true'):
131             ixia_rhelper.run_test(mock_tprofile, tasks_queue, results_queue)
132
133         self.assertEqual(expected_result, ixia_rhelper._queue.get())
134         mock_tprofile.update_traffic_profile.assert_called_once()
135         tasks_queue.task_done.assert_called_once()
136         results_queue.put.assert_called_once_with('COMPLETE')
137
138
139 @mock.patch.object(tg_rfc2544_ixia, 'ixnet_api')
140 class TestIXIATrafficGen(unittest.TestCase):
141     VNFD = {'vnfd:vnfd-catalog':
142             {'vnfd':
143              [{'short-name': 'VpeVnf',
144                'vdu':
145                [{'routing_table':
146                              [{'network': '152.16.100.20',
147                                'netmask': '255.255.255.0',
148                                'gateway': '152.16.100.20',
149                                'if': 'xe0'},
150                               {'network': '152.16.40.20',
151                                'netmask': '255.255.255.0',
152                                'gateway': '152.16.40.20',
153                                'if': 'xe1'}],
154                              'description': 'VPE approximation using DPDK',
155                              'name': 'vpevnf-baremetal',
156                              'nd_route_tbl':
157                                  [{'network': '0064:ff9b:0:0:0:0:9810:6414',
158                                    'netmask': '112',
159                                    'gateway': '0064:ff9b:0:0:0:0:9810:6414',
160                                    'if': 'xe0'},
161                                   {'network': '0064:ff9b:0:0:0:0:9810:2814',
162                                    'netmask': '112',
163                                    'gateway': '0064:ff9b:0:0:0:0:9810:2814',
164                                    'if': 'xe1'}],
165                              'id': 'vpevnf-baremetal',
166                              'external-interface':
167                                  [{'virtual-interface':
168                                    {'dst_mac': '00:00:00:00:00:04',
169                                     'vpci': '0000:05:00.0',
170                                     'local_ip': '152.16.100.19',
171                                     'type': 'PCI-PASSTHROUGH',
172                                     'netmask': '255.255.255.0',
173                                     'dpdk_port_num': 0,
174                                     'bandwidth': '10 Gbps',
175                                     'driver': "i40e",
176                                     'dst_ip': '152.16.100.20',
177                                     'local_iface_name': 'xe0',
178                                     'local_mac': '00:00:00:00:00:02'},
179                                    'vnfd-connection-point-ref': 'xe0',
180                                    'name': 'xe0'},
181                                   {'virtual-interface':
182                                    {'dst_mac': '00:00:00:00:00:03',
183                                     'vpci': '0000:05:00.1',
184                                     'local_ip': '152.16.40.19',
185                                     'type': 'PCI-PASSTHROUGH',
186                                     'driver': "i40e",
187                                     'netmask': '255.255.255.0',
188                                     'dpdk_port_num': 1,
189                                     'bandwidth': '10 Gbps',
190                                     'dst_ip': '152.16.40.20',
191                                     'local_iface_name': 'xe1',
192                                     'local_mac': '00:00:00:00:00:01'},
193                                    'vnfd-connection-point-ref': 'xe1',
194                                    'name': 'xe1'}]}],
195                'description': 'Vpe approximation using DPDK',
196                'mgmt-interface':
197                {'vdu-id': 'vpevnf-baremetal',
198                 'host': '1.1.1.1',
199                 'password': 'r00t',
200                             'user': 'root',
201                             'ip': '1.1.1.1'},
202                'benchmark':
203                {'kpi': ['packets_in', 'packets_fwd',
204                         'packets_dropped']},
205                'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
206                                     {'type': 'VPORT', 'name': 'xe1'}],
207                'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'}]}}
208
209     TRAFFIC_PROFILE = {
210         "schema": "isb:traffic_profile:0.1",
211         "name": "fixed",
212         "description": "Fixed traffic profile to run UDP traffic",
213         "traffic_profile": {
214             "traffic_type": "FixedTraffic",
215             "frame_rate": 100,  # pps
216             "flow_number": 10,
217             "frame_size": 64}}
218
219     TC_YAML = {'scenarios': [{'tc_options':
220                               {'rfc2544': {'allowed_drop_rate': '0.8 - 1'}},
221                               'runner': {'duration': 400,
222                                          'interval': 35, 'type': 'Duration'},
223                               'traffic_options':
224                                   {'flow': 'ipv4_1flow_Packets_vpe.yaml',
225                                    'imix': 'imix_voice.yaml'},
226                               'vnf_options': {'vpe': {'cfg': 'vpe_config'}},
227                               'traffic_profile': 'ipv4_throughput_vpe.yaml',
228                               'type': 'NSPerf',
229                               'nodes': {'tg__1': 'trafficgen_1.yardstick',
230                                         'vnf__1': 'vnf.yardstick'},
231                               'topology': 'vpe_vnf_topology.yaml'}],
232                'context': {'nfvi_type': 'baremetal',
233                            'type': contexts.CONTEXT_NODE,
234                            'name': 'yardstick',
235                            'file': '/etc/yardstick/nodes/pod.yaml'},
236                'schema': 'yardstick:task:0.1'}
237
238     def test___init__(self, *args):
239         with mock.patch("yardstick.ssh.SSH") as ssh:
240             ssh_mock = mock.Mock(autospec=ssh.SSH)
241             ssh_mock.execute = \
242                 mock.Mock(return_value=(0, "", ""))
243             ssh.from_node.return_value = ssh_mock
244             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
245             # NOTE(ralonsoh): check the object returned.
246             tg_rfc2544_ixia.IxiaTrafficGen(NAME, vnfd)
247
248     def test_listen_traffic(self, *args):
249         with mock.patch("yardstick.ssh.SSH") as ssh:
250             ssh_mock = mock.Mock(autospec=ssh.SSH)
251             ssh_mock.execute = \
252                 mock.Mock(return_value=(0, "", ""))
253             ssh.from_node.return_value = ssh_mock
254             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
255             ixnet_traffic_gen = tg_rfc2544_ixia.IxiaTrafficGen(NAME, vnfd)
256             self.assertIsNone(ixnet_traffic_gen.listen_traffic({}))
257
258     @mock.patch.object(ctx_base.Context, 'get_context_from_server', return_value='fake_context')
259     def test_instantiate(self, *args):
260         with mock.patch("yardstick.ssh.SSH") as ssh:
261             ssh_mock = mock.Mock(autospec=ssh.SSH)
262             ssh_mock.execute = \
263                 mock.Mock(return_value=(0, "", ""))
264             ssh_mock.run = \
265                 mock.Mock(return_value=(0, "", ""))
266             ssh.from_node.return_value = ssh_mock
267             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
268             ixnet_traffic_gen = tg_rfc2544_ixia.IxiaTrafficGen(NAME, vnfd)
269             scenario_cfg = {'tc': "nsb_test_case",
270                             "topology": ""}
271             scenario_cfg.update(
272                 {
273                     'options': {
274                         'packetsize': 64,
275                         'traffic_type': 4,
276                         'rfc2544': {
277                             'allowed_drop_rate': '0.8 - 1'},
278                         'vnf__1': {
279                             'rules': 'acl_1rule.yaml',
280                             'vnf_config': {
281                                 'lb_config': 'SW',
282                                 'lb_count': 1,
283                                 'worker_config': '1C/1T',
284                                 'worker_threads': 1}}}})
285             scenario_cfg.update({
286                 'nodes': {ixnet_traffic_gen.name: "mock"}
287             })
288             ixnet_traffic_gen.topology = ""
289             ixnet_traffic_gen.get_ixobj = mock.MagicMock()
290             ixnet_traffic_gen._ixia_traffic_gen = mock.MagicMock()
291             ixnet_traffic_gen._ixia_traffic_gen._connect = mock.Mock()
292             self.assertRaises(
293                 IOError,
294                 ixnet_traffic_gen.instantiate(scenario_cfg, {}))
295
296     @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server', return_value='mock_node')
297     def test_collect_kpi(self, *args):
298         with mock.patch("yardstick.ssh.SSH") as ssh:
299             ssh_mock = mock.Mock(autospec=ssh.SSH)
300             ssh_mock.execute = \
301                 mock.Mock(return_value=(0, "", ""))
302             ssh.from_node.return_value = ssh_mock
303
304             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
305             ixnet_traffic_gen = tg_rfc2544_ixia.IxiaTrafficGen(NAME, vnfd)
306             ixnet_traffic_gen.scenario_helper.scenario_cfg = {
307                 'nodes': {ixnet_traffic_gen.name: "mock"}
308             }
309             ixnet_traffic_gen.data = {}
310             restult = ixnet_traffic_gen.collect_kpi()
311
312             expected = {'collect_stats': {},
313                         'physical_node': 'mock_node'}
314
315             self.assertEqual(expected, restult)
316
317     def test_terminate(self, *args):
318         with mock.patch("yardstick.ssh.SSH") as ssh:
319             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
320             ssh_mock = mock.Mock(autospec=ssh.SSH)
321             ssh_mock.execute = \
322                 mock.Mock(return_value=(0, "", ""))
323             ssh.from_node.return_value = ssh_mock
324             ixnet_traffic_gen = tg_rfc2544_ixia.IxiaTrafficGen(
325                 NAME, vnfd, resource_helper_type=mock.Mock())
326             ixnet_traffic_gen._terminated = mock.MagicMock()
327             ixnet_traffic_gen._terminated.value = 0
328             ixnet_traffic_gen._ixia_traffic_gen = mock.MagicMock()
329             ixnet_traffic_gen._ixia_traffic_gen.ix_stop_traffic = mock.Mock()
330             ixnet_traffic_gen._traffic_process = mock.MagicMock()
331             ixnet_traffic_gen._traffic_process.terminate = mock.Mock()
332             self.assertIsNone(ixnet_traffic_gen.terminate())
333
334     def _get_file_abspath(self, filename):
335         curr_path = os.path.dirname(os.path.abspath(__file__))
336         file_path = os.path.join(curr_path, filename)
337         return file_path
338
339     def test__check_status(self, *args):
340         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
341         sut = tg_rfc2544_ixia.IxiaTrafficGen('vnf1', vnfd)
342         sut._check_status()
343
344     @mock.patch("yardstick.ssh.SSH")
345     def test_traffic_runner(self, mock_ssh, *args):
346         mock_traffic_profile = mock.Mock(autospec=tp_base.TrafficProfile)
347         mock_traffic_profile.get_traffic_definition.return_value = "64"
348         mock_traffic_profile.params = self.TRAFFIC_PROFILE
349         # traffic_profile.ports is standardized on port_num
350         mock_traffic_profile.ports = [0, 1]
351
352         mock_ssh_instance = mock.Mock(autospec=mock_ssh.SSH)
353         mock_ssh_instance.execute.return_value = 0, "", ""
354         mock_ssh_instance.run.return_value = 0, "", ""
355
356         mock_ssh.from_node.return_value = mock_ssh_instance
357
358         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
359         vnfd["mgmt-interface"].update({
360             'tg-config': {
361                 "ixchassis": "1.1.1.1",
362                 "py_bin_path": "/root",
363             }
364         })
365
366         samples = {}
367         name = ''
368         for ifname in range(1):
369             name = "xe{}".format(ifname)
370             samples[name] = {
371                 "Rx_Rate_Kbps": 20,
372                 "Tx_Rate_Kbps": 20,
373                 "Rx_Rate_Mbps": 10,
374                 "Tx_Rate_Mbps": 10,
375                 "RxThroughput": 10,
376                 "TxThroughput": 10,
377                 "Valid_Frames_Rx": 1000,
378                 "Frames_Tx": 1000,
379                 "in_packets": 1000,
380                 "out_packets": 1000,
381             }
382
383         samples.update({"CurrentDropPercentage": 0.0})
384
385         last_res = [
386             0,
387             {
388                 "Rx_Rate_Kbps": [20, 20],
389                 "Tx_Rate_Kbps": [20, 20],
390                 "Rx_Rate_Mbps": [10, 10],
391                 "Tx_Rate_Mbps": [10, 10],
392                 "CurrentDropPercentage": [0, 0],
393                 "RxThroughput": [10, 10],
394                 "TxThroughput": [10, 10],
395                 "Frames_Tx": [1000, 1000],
396                 "in_packets": [1000, 1000],
397                 "Valid_Frames_Rx": [1000, 1000],
398                 "out_packets": [1000, 1000],
399             },
400         ]
401
402         mock_traffic_profile.execute_traffic.return_value = [
403             'Completed', samples]
404         mock_traffic_profile.get_drop_percentage.return_value = [
405             'Completed', samples]
406
407         sut = tg_rfc2544_ixia.IxiaTrafficGen(name, vnfd)
408         sut.vnf_port_pairs = [[[0], [1]]]
409         sut.tc_file_name = self._get_file_abspath(TEST_FILE_YAML)
410         sut.topology = ""
411
412         sut.ssh_helper = mock.Mock()
413         sut._traffic_process = mock.MagicMock()
414         sut.generate_port_pairs = mock.Mock()
415
416         sut._ixia_traffic_gen = mock.MagicMock()
417         sut._ixia_traffic_gen.ix_get_statistics.return_value = last_res
418
419         sut.resource_helper.client = mock.MagicMock()
420         sut.resource_helper.client_started = mock.MagicMock()
421         sut.resource_helper.client_started.value = 1
422         sut.resource_helper.rfc_helper.iteration.value = 11
423         sut.resource_helper._ix_scenario = mock.Mock()
424
425         sut.scenario_helper.scenario_cfg = {
426             'options': {
427                 'packetsize': 64,
428                 'traffic_type': 4,
429                 'rfc2544': {
430                     'allowed_drop_rate': '0.8 - 1',
431                     'latency': True
432                 },
433                 'vnf__1': {
434                     'rules': 'acl_1rule.yaml',
435                     'vnf_config': {
436                         'lb_config': 'SW',
437                         'lb_count': 1,
438                         'worker_config': '1C/1T',
439                         'worker_threads': 1,
440                     },
441                 },
442             },
443             'task_path': '/path/to/task'
444         }
445
446         @mock.patch.object(six.moves.builtins, 'open', create=True)
447         @mock.patch('yardstick.network_services.vnf_generic.vnf.tg_rfc2544_ixia.open',
448                     mock.mock_open(), create=True)
449         @mock.patch('yardstick.network_services.vnf_generic.vnf.tg_rfc2544_ixia.LOG.exception')
450         def _traffic_runner(*args):
451             result = sut._traffic_runner(mock_traffic_profile)
452             self.assertIsNone(result)
453
454         _traffic_runner()
455
456     def test_run_traffic_once(self, *args):
457         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
458         sut = tg_rfc2544_ixia.IxiaTrafficGen('vnf1', vnfd)
459         sut._init_traffic_process = mock.Mock()
460         sut._tasks_queue.put = mock.Mock()
461         sut.resource_helper.client_started.value = 0
462         sut.run_traffic_once(self.TRAFFIC_PROFILE)
463         sut._tasks_queue.put.assert_called_once_with("RUN_TRAFFIC")
464         sut._init_traffic_process.assert_called_once_with(self.TRAFFIC_PROFILE)
465
466     def test__test_runner(self, *args):
467         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
468         sut = tg_rfc2544_ixia.IxiaTrafficGen('vnf1', vnfd)
469         tasks = 'tasks'
470         results = 'results'
471         sut.resource_helper = mock.Mock()
472         sut._test_runner(self.TRAFFIC_PROFILE, tasks, results)
473         sut.resource_helper.run_test.assert_called_once_with(self.TRAFFIC_PROFILE,
474                                                              tasks, results)
475
476     @mock.patch.object(time, 'sleep', return_value=0)
477     def test__init_traffic_process(self, *args):
478         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
479         sut = tg_rfc2544_ixia.IxiaTrafficGen('vnf1', vnfd)
480         sut._test_runner = mock.Mock(return_value=0)
481         sut.resource_helper = mock.Mock()
482         sut.resource_helper.client_started.value = 0
483         sut._init_traffic_process(self.TRAFFIC_PROFILE)
484
485     def test_wait_on_traffic(self, *args):
486         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
487         sut = tg_rfc2544_ixia.IxiaTrafficGen('vnf1', vnfd)
488         sut._tasks_queue.join = mock.Mock(return_value=0)
489         sut._result_queue.get = mock.Mock(return_value='COMPLETE')
490         result = sut.wait_on_traffic()
491         sut._tasks_queue.join.assert_called_once()
492         sut._result_queue.get.assert_called_once()
493         self.assertEqual(result, 'COMPLETE')
494
495
496 class TestIxiaBasicScenario(unittest.TestCase):
497
498     STATS = {'stat_name': ['Card01/Port01',
499                            'Card02/Port02'],
500              'port_name': ['Ethernet - 001', 'Ethernet - 002'],
501              'Frames_Tx': ['150', '150'],
502              'Valid_Frames_Rx': ['150', '150'],
503              'Frames_Tx_Rate': ['0.0', '0.0'],
504              'Valid_Frames_Rx_Rate': ['0.0', '0.0'],
505              'Bytes_Rx': ['9600', '9600'],
506              'Bytes_Tx': ['9600', '9600'],
507              'Tx_Rate_Kbps': ['0.0', '0.0'],
508              'Rx_Rate_Mbps': ['0.0', '0.0'],
509              'Tx_Rate_Mbps': ['0.0', '0.0'],
510              'Rx_Rate_Kbps': ['0.0', '0.0'],
511              'Store-Forward_Max_latency_ns': ['100', '200'],
512              'Store-Forward_Min_latency_ns': ['100', '200'],
513              'Store-Forward_Avg_latency_ns': ['100', '200']}
514
515     def setUp(self):
516         self._mock_IxNextgen = mock.patch.object(ixnet_api, 'IxNextgen')
517         self.mock_IxNextgen = self._mock_IxNextgen.start()
518         self.context_cfg = mock.Mock()
519         self.ixia_cfg = mock.Mock()
520         self.scenario = tg_rfc2544_ixia.IxiaBasicScenario(self.mock_IxNextgen,
521                                                           self.context_cfg,
522                                                           self.ixia_cfg)
523         self.addCleanup(self._stop_mocks)
524
525     def _stop_mocks(self):
526         self._mock_IxNextgen.stop()
527
528     def test___init___(self):
529         self.assertIsInstance(self.scenario, tg_rfc2544_ixia.IxiaBasicScenario)
530         self.assertEqual(self.scenario.client, self.mock_IxNextgen)
531
532     def test_create_traffic_model(self):
533         self.mock_IxNextgen.get_vports.return_value = [1, 2, 3, 4]
534         yaml_data = {'traffic_profile': {}
535                     }
536         traffic_profile = ixia_rfc2544.IXIARFC2544Profile(yaml_data)
537         self.scenario.create_traffic_model(traffic_profile)
538         self.scenario.client.get_vports.assert_called_once()
539         self.scenario.client.create_traffic_model.assert_called_once_with(
540             [1, 3], [2, 4], traffic_profile)
541
542     def test_apply_config(self):
543         self.assertIsNone(self.scenario.apply_config())
544
545     def test_run_protocols(self):
546         self.assertIsNone(self.scenario.run_protocols())
547
548     def test_stop_protocols(self):
549         self.assertIsNone(self.scenario.stop_protocols())
550
551     def test__get_stats(self):
552         self.scenario._get_stats()
553         self.scenario.client.get_statistics.assert_called_once()
554
555     @mock.patch.object(tg_rfc2544_ixia.IxiaBasicScenario, '_get_stats')
556     def test_generate_samples(self, mock_get_stats):
557
558         expected_samples = {'xe0': {
559                                 'in_packets': 150,
560                                 'out_packets': 150,
561                                 'in_bytes': 9600,
562                                 'out_bytes': 9600,
563                                 'rx_throughput_mbps': 0.0,
564                                 'rx_throughput_kps': 0.0,
565                                 'RxThroughput': 5.0,
566                                 'TxThroughput': 5.0,
567                                 'RxThroughputBps': 320.0,
568                                 'TxThroughputBps': 320.0,
569                                 'tx_throughput_mbps': 0.0,
570                                 'tx_throughput_kps': 0.0,
571                                 'Store-Forward_Max_latency_ns': 100,
572                                 'Store-Forward_Min_latency_ns': 100,
573                                 'Store-Forward_Avg_latency_ns': 100},
574                             'xe1': {
575                                 'in_packets': 150,
576                                 'out_packets': 150,
577                                 'in_bytes': 9600,
578                                 'out_bytes': 9600,
579                                 'rx_throughput_mbps': 0.0,
580                                 'rx_throughput_kps': 0.0,
581                                 'RxThroughput': 5.0,
582                                 'TxThroughput': 5.0,
583                                 'RxThroughputBps': 320.0,
584                                 'TxThroughputBps': 320.0,
585                                 'tx_throughput_mbps': 0.0,
586                                 'tx_throughput_kps': 0.0,
587                                 'Store-Forward_Max_latency_ns': 200,
588                                 'Store-Forward_Min_latency_ns': 200,
589                                 'Store-Forward_Avg_latency_ns': 200}}
590
591         res_helper = mock.Mock()
592         res_helper.vnfd_helper.find_interface_by_port.side_effect = \
593             [{'name': 'xe0'}, {'name': 'xe1'}]
594         ports = [0, 1]
595         duration = 30
596         mock_get_stats.return_value = self.STATS
597         samples = self.scenario.generate_samples(res_helper, ports, duration)
598         mock_get_stats.assert_called_once()
599         self.assertEqual(samples, expected_samples)
600
601
602 class TestIxiaL3Scenario(TestIxiaBasicScenario):
603     IXIA_CFG = {
604         'flow': {
605             'src_ip': ['192.168.0.1-192.168.0.50'],
606             'dst_ip': ['192.168.1.1-192.168.1.150']
607         }
608     }
609
610     CONTEXT_CFG = {
611         'nodes': {
612             'tg__0': {
613                 'role': 'IxNet',
614                 'interfaces': {
615                     'xe0': {
616                         'vld_id': 'uplink_0',
617                         'local_ip': '10.1.1.1',
618                         'local_mac': 'aa:bb:cc:dd:ee:ff',
619                         'ifname': 'xe0'
620                     },
621                     'xe1': {
622                         'vld_id': 'downlink_0',
623                         'local_ip': '20.2.2.2',
624                         'local_mac': 'bb:bb:cc:dd:ee:ee',
625                         'ifname': 'xe1'
626                     }
627                 },
628                 'routing_table': [{
629                     'network': "152.16.100.20",
630                     'netmask': '255.255.0.0',
631                     'gateway': '152.16.100.21',
632                     'if': 'xe0'
633                 }]
634             }
635         }
636     }
637
638     def setUp(self):
639         super(TestIxiaL3Scenario, self).setUp()
640         self.ixia_cfg = self.IXIA_CFG
641         self.context_cfg = self.CONTEXT_CFG
642         self.scenario = tg_rfc2544_ixia.IxiaL3Scenario(self.mock_IxNextgen,
643                                                        self.context_cfg,
644                                                        self.ixia_cfg)
645
646     def test___init___(self):
647         self.assertIsInstance(self.scenario, tg_rfc2544_ixia.IxiaL3Scenario)
648         self.assertEqual(self.scenario.client, self.mock_IxNextgen)
649
650     def test_create_traffic_model(self):
651         self.mock_IxNextgen.get_vports.return_value = ['1', '2']
652         traffic_profile = 'fake_profile'
653         self.scenario.create_traffic_model(traffic_profile)
654         self.scenario.client.get_vports.assert_called_once()
655         self.scenario.client.create_ipv4_traffic_model.\
656             assert_called_once_with(['1/protocols/static'],
657                                     ['2/protocols/static'],
658                                     'fake_profile')
659
660     def test_apply_config(self):
661         self.scenario._add_interfaces = mock.Mock()
662         self.scenario._add_static_ips = mock.Mock()
663         self.assertIsNone(self.scenario.apply_config())
664
665     def test__add_static(self):
666         self.mock_IxNextgen.get_vports.return_value = ['1', '2']
667         self.mock_IxNextgen.get_static_interface.side_effect = ['intf1',
668                                                                 'intf2']
669
670         self.scenario._add_static_ips()
671
672         self.mock_IxNextgen.get_static_interface.assert_any_call('1')
673         self.mock_IxNextgen.get_static_interface.assert_any_call('2')
674
675         self.scenario.client.add_static_ipv4.assert_any_call(
676             'intf1', '1', '192.168.0.1', 49, '32')
677         self.scenario.client.add_static_ipv4.assert_any_call(
678             'intf2', '2', '192.168.1.1', 149, '32')
679
680     def test__add_interfaces(self):
681         self.mock_IxNextgen.get_vports.return_value = ['1', '2']
682
683         self.scenario._add_interfaces()
684
685         self.mock_IxNextgen.add_interface.assert_any_call('1',
686                                                           '10.1.1.1',
687                                                           'aa:bb:cc:dd:ee:ff',
688                                                           '152.16.100.21')
689         self.mock_IxNextgen.add_interface.assert_any_call('2',
690                                                           '20.2.2.2',
691                                                           'bb:bb:cc:dd:ee:ee',
692                                                           None)
693
694
695 class TestIxiaPppoeClientScenario(unittest.TestCase):
696
697     IXIA_CFG = {
698         'pppoe_client': {
699             'sessions_per_port': 4,
700             'sessions_per_svlan': 1,
701             's_vlan': 10,
702             'c_vlan': 20,
703             'ip': ['10.3.3.1', '10.4.4.1']
704         },
705         'ipv4_client': {
706             'sessions_per_port': 1,
707             'sessions_per_vlan': 1,
708             'vlan': 101,
709             'gateway_ip': ['10.1.1.1', '10.2.2.1'],
710             'ip': ['10.1.1.1', '10.2.2.1'],
711             'prefix': ['24', '24']
712         },
713         'priority': {
714             'tos': {'precedence': [0, 4]}
715         }
716     }
717
718     CONTEXT_CFG = {
719         'nodes': {'tg__0': {
720             'interfaces': {'xe0': {
721                 'local_ip': '10.1.1.1',
722                 'netmask': '255.255.255.0'
723                 }}}}}
724
725     def setUp(self):
726         self._mock_IxNextgen = mock.patch.object(ixnet_api, 'IxNextgen')
727         self.mock_IxNextgen = self._mock_IxNextgen.start()
728         self.scenario = tg_rfc2544_ixia.IxiaPppoeClientScenario(
729             self.mock_IxNextgen, self.CONTEXT_CFG, self.IXIA_CFG)
730         tg_rfc2544_ixia.WAIT_PROTOCOLS_STARTED = 2
731         self.addCleanup(self._stop_mocks)
732
733     def _stop_mocks(self):
734         self._mock_IxNextgen.stop()
735
736     def test___init___(self):
737         self.assertIsInstance(self.scenario, tg_rfc2544_ixia.IxiaPppoeClientScenario)
738         self.assertEqual(self.scenario.client, self.mock_IxNextgen)
739
740     @mock.patch.object(tg_rfc2544_ixia.IxiaPppoeClientScenario,
741                        '_fill_ixia_config')
742     @mock.patch.object(tg_rfc2544_ixia.IxiaPppoeClientScenario,
743                        '_apply_access_network_config')
744     @mock.patch.object(tg_rfc2544_ixia.IxiaPppoeClientScenario,
745                        '_apply_core_network_config')
746     def test_apply_config(self, mock_apply_core_net_cfg,
747                           mock_apply_access_net_cfg,
748                           mock_fill_ixia_config):
749         self.mock_IxNextgen.get_vports.return_value = [1, 2, 3, 4]
750         self.scenario.apply_config()
751         self.scenario.client.get_vports.assert_called_once()
752         self.assertEqual(self.scenario._uplink_vports, [1, 3])
753         self.assertEqual(self.scenario._downlink_vports, [2, 4])
754         mock_fill_ixia_config.assert_called_once()
755         mock_apply_core_net_cfg.assert_called_once()
756         mock_apply_access_net_cfg.assert_called_once()
757
758     @mock.patch.object(tg_rfc2544_ixia.IxiaPppoeClientScenario,
759                        '_get_endpoints_src_dst_id_pairs')
760     @mock.patch.object(tg_rfc2544_ixia.IxiaPppoeClientScenario,
761                        '_get_endpoints_src_dst_obj_pairs')
762     def test_create_traffic_model(self, mock_obj_pairs, mock_id_pairs):
763         uplink_endpoints = ['group1', 'group2']
764         downlink_endpoints = ['group3', 'group3']
765         mock_id_pairs.return_value = ['xe0', 'xe1', 'xe0', 'xe1']
766         mock_obj_pairs.return_value = ['group1', 'group3', 'group2', 'group3']
767         mock_tp = mock.Mock()
768         mock_tp.full_profile = {'uplink_0': 'data',
769                                 'downlink_0': 'data',
770                                 'uplink_1': 'data',
771                                 'downlink_1': 'data'
772                                 }
773         self.scenario.create_traffic_model(mock_tp)
774         mock_id_pairs.assert_called_once_with(mock_tp.full_profile)
775         mock_obj_pairs.assert_called_once_with(['xe0', 'xe1', 'xe0', 'xe1'])
776         self.scenario.client.create_ipv4_traffic_model.assert_called_once_with(
777             uplink_endpoints, downlink_endpoints, mock_tp)
778
779     @mock.patch.object(tg_rfc2544_ixia.IxiaPppoeClientScenario,
780                        '_get_endpoints_src_dst_id_pairs')
781     @mock.patch.object(tg_rfc2544_ixia.IxiaPppoeClientScenario,
782                        '_get_endpoints_src_dst_obj_pairs')
783     def test_create_traffic_model_topology_based_flows(self, mock_obj_pairs,
784                                                        mock_id_pairs):
785         uplink_topologies = ['topology1', 'topology3']
786         downlink_topologies = ['topology2', 'topology4']
787         mock_id_pairs.return_value = []
788         mock_obj_pairs.return_value = []
789         mock_tp = mock.Mock()
790         mock_tp.full_profile = {'uplink_0': 'data',
791                                 'downlink_0': 'data',
792                                 'uplink_1': 'data',
793                                 'downlink_1': 'data'
794                                 }
795         self.scenario._access_topologies = ['topology1', 'topology3']
796         self.scenario._core_topologies = ['topology2', 'topology4']
797         self.scenario.create_traffic_model(mock_tp)
798         mock_id_pairs.assert_called_once_with(mock_tp.full_profile)
799         mock_obj_pairs.assert_called_once_with([])
800         self.scenario.client.create_ipv4_traffic_model.assert_called_once_with(
801             uplink_topologies, downlink_topologies, mock_tp)
802
803     def test__get_endpoints_src_dst_id_pairs(self):
804         full_tp = OrderedDict([
805             ('uplink_0', {'ipv4': {'port': 'xe0'}}),
806             ('downlink_0', {'ipv4': {'port': 'xe1'}}),
807             ('uplink_1', {'ipv4': {'port': 'xe0'}}),
808             ('downlink_1', {'ipv4': {'port': 'xe3'}})])
809         endpoints_src_dst_pairs = ['xe0', 'xe1', 'xe0', 'xe3']
810         res = self.scenario._get_endpoints_src_dst_id_pairs(full_tp)
811         self.assertEqual(res, endpoints_src_dst_pairs)
812
813     def test__get_endpoints_src_dst_id_pairs_wrong_flows_number(self):
814         full_tp = OrderedDict([
815             ('uplink_0', {'ipv4': {'port': 'xe0'}}),
816             ('downlink_0', {'ipv4': {'port': 'xe1'}}),
817             ('uplink_1', {'ipv4': {'port': 'xe0'}})])
818         with self.assertRaises(RuntimeError):
819             self.scenario._get_endpoints_src_dst_id_pairs(full_tp)
820
821     def test__get_endpoints_src_dst_id_pairs_no_port_key(self):
822         full_tp = OrderedDict([
823             ('uplink_0', {'ipv4': {'id': 1}}),
824             ('downlink_0', {'ipv4': {'id': 2}})])
825         self.assertEqual(
826             self.scenario._get_endpoints_src_dst_id_pairs(full_tp), [])
827
828     def test__get_endpoints_src_dst_obj_pairs_tp_with_port_key(self):
829         endpoints_id_pairs = ['xe0', 'xe1',
830                               'xe0', 'xe1',
831                               'xe0', 'xe3',
832                               'xe0', 'xe3']
833         ixia_cfg = {
834             'pppoe_client': {
835                 'sessions_per_port': 4,
836                 'sessions_per_svlan': 1
837             },
838             'flow': {
839                 'src_ip': [{'tg__0': 'xe0'}, {'tg__0': 'xe2'}],
840                 'dst_ip': [{'tg__0': 'xe1'}, {'tg__0': 'xe3'}]
841             }
842         }
843
844         expected_result = ['tp1_dg1', 'tp3_dg1', 'tp1_dg2', 'tp3_dg1',
845                            'tp1_dg3', 'tp4_dg1', 'tp1_dg4', 'tp4_dg1']
846
847         self.scenario._ixia_cfg = ixia_cfg
848         self.scenario._access_topologies = ['topology1', 'topology2']
849         self.scenario._core_topologies = ['topology3', 'topology4']
850         self.mock_IxNextgen.get_topology_device_groups.side_effect = \
851             [['tp1_dg1', 'tp1_dg2', 'tp1_dg3', 'tp1_dg4'],
852              ['tp2_dg1', 'tp2_dg2', 'tp2_dg3', 'tp2_dg4'],
853              ['tp3_dg1'],
854              ['tp4_dg1']]
855         res = self.scenario._get_endpoints_src_dst_obj_pairs(
856             endpoints_id_pairs)
857         self.assertEqual(res, expected_result)
858
859     def test__get_endpoints_src_dst_obj_pairs_default_flows_mapping(self):
860         endpoints_id_pairs = []
861         ixia_cfg = {
862             'pppoe_client': {
863                 'sessions_per_port': 4,
864                 'sessions_per_svlan': 1
865             },
866             'flow': {
867                 'src_ip': [{'tg__0': 'xe0'}, {'tg__0': 'xe2'}],
868                 'dst_ip': [{'tg__0': 'xe1'}, {'tg__0': 'xe3'}]
869             }
870         }
871
872         self.scenario._ixia_cfg = ixia_cfg
873         res = self.scenario._get_endpoints_src_dst_obj_pairs(
874             endpoints_id_pairs)
875         self.assertEqual(res, [])
876
877     def test_run_protocols(self):
878         self.scenario.client.is_protocols_running.return_value = True
879         self.scenario.run_protocols()
880         self.scenario.client.start_protocols.assert_called_once()
881
882     def test_run_protocols_timeout_exception(self):
883         self.scenario.client.is_protocols_running.return_value = False
884         with self.assertRaises(exceptions.WaitTimeout):
885             self.scenario.run_protocols()
886         self.scenario.client.start_protocols.assert_called_once()
887
888     def test_stop_protocols(self):
889         self.scenario.stop_protocols()
890         self.scenario.client.stop_protocols.assert_called_once()
891
892     def test__get_intf_addr_str_type_input(self):
893         intf = '192.168.10.2/24'
894         ip, mask = self.scenario._get_intf_addr(intf)
895         self.assertEqual(ip, '192.168.10.2')
896         self.assertEqual(mask, 24)
897
898     def test__get_intf_addr_dict_type_input(self):
899         intf = {'tg__0': 'xe0'}
900         ip, mask = self.scenario._get_intf_addr(intf)
901         self.assertEqual(ip, '10.1.1.1')
902         self.assertEqual(mask, 24)
903
904     @mock.patch.object(tg_rfc2544_ixia.IxiaPppoeClientScenario, '_get_intf_addr')
905     def test__fill_ixia_config(self, mock_get_intf_addr):
906
907         ixia_cfg = {
908             'pppoe_client': {
909                 'sessions_per_port': 4,
910                 'sessions_per_svlan': 1,
911                 's_vlan': 10,
912                 'c_vlan': 20,
913                 'ip': ['10.3.3.1/24', '10.4.4.1/24']
914             },
915             'ipv4_client': {
916                 'sessions_per_port': 1,
917                 'sessions_per_vlan': 1,
918                 'vlan': 101,
919                 'gateway_ip': ['10.1.1.1/24', '10.2.2.1/24'],
920                 'ip': ['10.1.1.1/24', '10.2.2.1/24']
921             }
922         }
923
924         mock_get_intf_addr.side_effect = [
925             ('10.3.3.1', '24'),
926             ('10.4.4.1', '24'),
927             ('10.1.1.1', '24'),
928             ('10.2.2.1', '24'),
929             ('10.1.1.1', '24'),
930             ('10.2.2.1', '24')
931         ]
932         self.scenario._ixia_cfg = ixia_cfg
933         self.scenario._fill_ixia_config()
934         self.assertEqual(mock_get_intf_addr.call_count, 6)
935         self.assertEqual(self.scenario._ixia_cfg['pppoe_client']['ip'],
936                          ['10.3.3.1', '10.4.4.1'])
937         self.assertEqual(self.scenario._ixia_cfg['ipv4_client']['ip'],
938                          ['10.1.1.1', '10.2.2.1'])
939         self.assertEqual(self.scenario._ixia_cfg['ipv4_client']['prefix'],
940                          ['24', '24'])
941
942     @mock.patch('yardstick.network_services.libs.ixia_libs.ixnet.ixnet_api.Vlan')
943     def test__apply_access_network_config_pap_auth(self, mock_vlan):
944         _ixia_cfg = {
945             'pppoe_client': {
946                 'sessions_per_port': 4,
947                 'sessions_per_svlan': 1,
948                 's_vlan': 10,
949                 'c_vlan': 20,
950                 'pap_user': 'test_pap',
951                 'pap_password': 'pap'
952                 }}
953         pap_user = _ixia_cfg['pppoe_client']['pap_user']
954         pap_passwd = _ixia_cfg['pppoe_client']['pap_password']
955         self.scenario._ixia_cfg = _ixia_cfg
956         self.scenario._uplink_vports = [0, 2]
957         self.scenario.client.add_topology.side_effect = ['Topology 1', 'Topology 2']
958         self.scenario.client.add_device_group.side_effect = ['Dg1', 'Dg2', 'Dg3',
959                                                              'Dg4', 'Dg5', 'Dg6',
960                                                              'Dg7', 'Dg8']
961         self.scenario.client.add_ethernet.side_effect = ['Eth1', 'Eth2', 'Eth3',
962                                                          'Eth4', 'Eth5', 'Eth6',
963                                                          'Eth7', 'Eth8']
964         self.scenario._apply_access_network_config()
965         self.assertEqual(self.scenario.client.add_topology.call_count, 2)
966         self.assertEqual(self.scenario.client.add_device_group.call_count, 8)
967         self.assertEqual(self.scenario.client.add_ethernet.call_count, 8)
968         self.assertEqual(mock_vlan.call_count, 16)
969         self.assertEqual(self.scenario.client.add_vlans.call_count, 8)
970         self.assertEqual(self.scenario.client.add_pppox_client.call_count, 8)
971         self.scenario.client.add_topology.assert_has_calls([
972             mock.call('Topology access 0', 0),
973             mock.call('Topology access 1', 2)
974         ])
975         self.scenario.client.add_device_group.assert_has_calls([
976             mock.call('Topology 1', 'SVLAN 10', 1),
977             mock.call('Topology 1', 'SVLAN 11', 1),
978             mock.call('Topology 1', 'SVLAN 12', 1),
979             mock.call('Topology 1', 'SVLAN 13', 1),
980             mock.call('Topology 2', 'SVLAN 14', 1),
981             mock.call('Topology 2', 'SVLAN 15', 1),
982             mock.call('Topology 2', 'SVLAN 16', 1),
983             mock.call('Topology 2', 'SVLAN 17', 1)
984         ])
985         self.scenario.client.add_ethernet.assert_has_calls([
986             mock.call('Dg1', 'Ethernet'),
987             mock.call('Dg2', 'Ethernet'),
988             mock.call('Dg3', 'Ethernet'),
989             mock.call('Dg4', 'Ethernet'),
990             mock.call('Dg5', 'Ethernet'),
991             mock.call('Dg6', 'Ethernet'),
992             mock.call('Dg7', 'Ethernet'),
993             mock.call('Dg8', 'Ethernet')
994         ])
995         mock_vlan.assert_has_calls([
996             mock.call(vlan_id=10),
997             mock.call(vlan_id=20, vlan_id_step=1),
998             mock.call(vlan_id=11),
999             mock.call(vlan_id=20, vlan_id_step=1),
1000             mock.call(vlan_id=12),
1001             mock.call(vlan_id=20, vlan_id_step=1),
1002             mock.call(vlan_id=13),
1003             mock.call(vlan_id=20, vlan_id_step=1),
1004             mock.call(vlan_id=14),
1005             mock.call(vlan_id=20, vlan_id_step=1),
1006             mock.call(vlan_id=15),
1007             mock.call(vlan_id=20, vlan_id_step=1),
1008             mock.call(vlan_id=16),
1009             mock.call(vlan_id=20, vlan_id_step=1),
1010             mock.call(vlan_id=17),
1011             mock.call(vlan_id=20, vlan_id_step=1)
1012         ])
1013         self.scenario.client.add_pppox_client.assert_has_calls([
1014             mock.call('Eth1', 'pap', pap_user, pap_passwd),
1015             mock.call('Eth2', 'pap', pap_user, pap_passwd),
1016             mock.call('Eth3', 'pap', pap_user, pap_passwd),
1017             mock.call('Eth4', 'pap', pap_user, pap_passwd),
1018             mock.call('Eth5', 'pap', pap_user, pap_passwd),
1019             mock.call('Eth6', 'pap', pap_user, pap_passwd),
1020             mock.call('Eth7', 'pap', pap_user, pap_passwd),
1021             mock.call('Eth8', 'pap', pap_user, pap_passwd)
1022         ])
1023
1024     def test__apply_access_network_config_chap_auth(self):
1025         _ixia_cfg = {
1026             'pppoe_client': {
1027                 'sessions_per_port': 4,
1028                 'sessions_per_svlan': 1,
1029                 's_vlan': 10,
1030                 'c_vlan': 20,
1031                 'chap_user': 'test_chap',
1032                 'chap_password': 'chap'
1033             }}
1034         chap_user = _ixia_cfg['pppoe_client']['chap_user']
1035         chap_passwd = _ixia_cfg['pppoe_client']['chap_password']
1036         self.scenario._ixia_cfg = _ixia_cfg
1037         self.scenario._uplink_vports = [0, 2]
1038         self.scenario.client.add_ethernet.side_effect = ['Eth1', 'Eth2', 'Eth3',
1039                                                          'Eth4', 'Eth5', 'Eth6',
1040                                                          'Eth7', 'Eth8']
1041         self.scenario._apply_access_network_config()
1042         self.assertEqual(self.scenario.client.add_pppox_client.call_count, 8)
1043         self.scenario.client.add_pppox_client.assert_has_calls([
1044             mock.call('Eth1', 'chap', chap_user, chap_passwd),
1045             mock.call('Eth2', 'chap', chap_user, chap_passwd),
1046             mock.call('Eth3', 'chap', chap_user, chap_passwd),
1047             mock.call('Eth4', 'chap', chap_user, chap_passwd),
1048             mock.call('Eth5', 'chap', chap_user, chap_passwd),
1049             mock.call('Eth6', 'chap', chap_user, chap_passwd),
1050             mock.call('Eth7', 'chap', chap_user, chap_passwd),
1051             mock.call('Eth8', 'chap', chap_user, chap_passwd)
1052         ])
1053
1054     @mock.patch('yardstick.network_services.libs.ixia_libs.ixnet.ixnet_api.Vlan')
1055     def test__apply_core_network_config_no_bgp_proto(self, mock_vlan):
1056         self.scenario._downlink_vports = [1, 3]
1057         self.scenario.client.add_topology.side_effect = ['Topology 1', 'Topology 2']
1058         self.scenario.client.add_device_group.side_effect = ['Dg1', 'Dg2']
1059         self.scenario.client.add_ethernet.side_effect = ['Eth1', 'Eth2']
1060         self.scenario._apply_core_network_config()
1061         self.assertEqual(self.scenario.client.add_topology.call_count, 2)
1062         self.assertEqual(self.scenario.client.add_device_group.call_count, 2)
1063         self.assertEqual(self.scenario.client.add_ethernet.call_count, 2)
1064         self.assertEqual(mock_vlan.call_count, 2)
1065         self.assertEqual(self.scenario.client.add_vlans.call_count, 2)
1066         self.assertEqual(self.scenario.client.add_ipv4.call_count, 2)
1067         self.scenario.client.add_topology.assert_has_calls([
1068             mock.call('Topology core 0', 1),
1069             mock.call('Topology core 1', 3)
1070         ])
1071         self.scenario.client.add_device_group.assert_has_calls([
1072             mock.call('Topology 1', 'Core port 0', 1),
1073             mock.call('Topology 2', 'Core port 1', 1)
1074         ])
1075         self.scenario.client.add_ethernet.assert_has_calls([
1076             mock.call('Dg1', 'Ethernet'),
1077             mock.call('Dg2', 'Ethernet')
1078         ])
1079         mock_vlan.assert_has_calls([
1080             mock.call(vlan_id=101),
1081             mock.call(vlan_id=102)
1082         ])
1083         self.scenario.client.add_ipv4.assert_has_calls([
1084             mock.call('Eth1', name='ipv4', addr=ipaddress.IPv4Address('10.1.1.2'),
1085                       addr_step='0.0.0.1', prefix='24', gateway='10.1.1.1'),
1086             mock.call('Eth2', name='ipv4', addr=ipaddress.IPv4Address('10.2.2.2'),
1087                       addr_step='0.0.0.1', prefix='24', gateway='10.2.2.1')
1088         ])
1089         self.scenario.client.add_bgp.assert_not_called()
1090
1091     def test__apply_core_network_config_with_bgp_proto(self):
1092         bgp_params = {
1093             'bgp': {
1094                 'bgp_type': 'external',
1095                 'dut_ip': '10.0.0.1',
1096                 'as_number': 65000
1097             }
1098         }
1099         self.scenario._ixia_cfg['ipv4_client'].update(bgp_params)
1100         self.scenario._downlink_vports = [1, 3]
1101         self.scenario.client.add_ipv4.side_effect = ['ipv4_1', 'ipv4_2']
1102         self.scenario._apply_core_network_config()
1103         self.assertEqual(self.scenario.client.add_bgp.call_count, 2)
1104         self.scenario.client.add_bgp.assert_has_calls([
1105             mock.call('ipv4_1', dut_ip=bgp_params["bgp"]["dut_ip"],
1106                       local_as=bgp_params["bgp"]["as_number"],
1107                       bgp_type=bgp_params["bgp"]["bgp_type"]),
1108             mock.call('ipv4_2', dut_ip=bgp_params["bgp"]["dut_ip"],
1109                       local_as=bgp_params["bgp"]["as_number"],
1110                       bgp_type=bgp_params["bgp"]["bgp_type"])
1111         ])
1112
1113     def test_update_tracking_options_raw_priority(self):
1114         raw_priority = {'raw': 4}
1115         self.scenario._ixia_cfg['priority'] = raw_priority
1116         self.scenario.update_tracking_options()
1117         self.scenario.client.set_flow_tracking.assert_called_once_with(
1118             ['flowGroup0', 'vlanVlanId0', 'ipv4Raw0'])
1119
1120     def test_update_tracking_options_tos_priority(self):
1121         tos_priority = {'tos': {'precedence': [4, 7]}}
1122         self.scenario._ixia_cfg['priority'] = tos_priority
1123         self.scenario.update_tracking_options()
1124         self.scenario.client.set_flow_tracking.assert_called_once_with(
1125             ['flowGroup0', 'vlanVlanId0', 'ipv4Precedence0'])
1126
1127     def test_update_tracking_options_dscp_priority(self):
1128         dscp_priority = {'dscp': {'defaultPHB': [4, 7]}}
1129         self.scenario._ixia_cfg['priority'] = dscp_priority
1130         self.scenario.update_tracking_options()
1131         self.scenario.client.set_flow_tracking.assert_called_once_with(
1132             ['flowGroup0', 'vlanVlanId0', 'ipv4DefaultPhb0'])
1133
1134     def test_update_tracking_options_invalid_priority_data(self):
1135         invalid_priority = {'tos': {'inet-precedence': [4, 7]}}
1136         self.scenario._ixia_cfg['priority'] = invalid_priority
1137         self.scenario.update_tracking_options()
1138         self.scenario.client.set_flow_tracking.assert_called_once_with(
1139             ['flowGroup0', 'vlanVlanId0', 'ipv4Precedence0'])
1140
1141     def test_get_tc_rfc2544_options(self):
1142         rfc2544_tc_opts = {'allowed_drop_rate': '0.0001 - 0.0001'}
1143         self.scenario._ixia_cfg['rfc2544'] = rfc2544_tc_opts
1144         res = self.scenario.get_tc_rfc2544_options()
1145         self.assertEqual(res, rfc2544_tc_opts)
1146
1147     def test__get_stats(self):
1148         self.scenario._get_stats()
1149         self.scenario.client.get_pppoe_scenario_statistics.assert_called_once()
1150
1151     def test_get_flow_id_data(self):
1152         stats = [{'id': 1, 'in_packets': 10, 'out_packets': 20}]
1153         key = "in_packets"
1154         flow_id = 1
1155         res = self.scenario.get_flow_id_data(stats, flow_id, key)
1156         self.assertEqual(res, 10)
1157
1158     @mock.patch.object(tg_rfc2544_ixia.IxiaPppoeClientScenario, '_get_stats')
1159     @mock.patch.object(tg_rfc2544_ixia.IxiaPppoeClientScenario,
1160                        'get_priority_flows_stats')
1161     def test_generate_samples(self, mock_prio_flow_statistics,
1162                               mock_get_stats):
1163         ixia_stats = {
1164             'flow_statistic': [
1165                 {'Flow_Group': 'RFC2544-1 - Flow Group 0001',
1166                  'Frames_Delta': '0',
1167                  'IP_Priority': '0',
1168                  'Rx_Frames': '3000',
1169                  'Tx_Frames': '3000',
1170                  'VLAN-ID': '100',
1171                  'Tx_Port': 'Ethernet - 001',
1172                  'Store-Forward_Avg_latency_ns': '2',
1173                  'Store-Forward_Min_latency_ns': '2',
1174                  'Store-Forward_Max_latency_ns': '2'},
1175                 {'Flow_Group': 'RFC2544-2 - Flow Group 0001',
1176                  'Frames_Delta': '0',
1177                  'IP_Priority': '0',
1178                  'Rx_Frames': '3000',
1179                  'Tx_Frames': '3000',
1180                  'VLAN-ID': '101',
1181                  'Tx_Port': 'Ethernet - 002',
1182                  'Store-Forward_Avg_latency_ns': '2',
1183                  'Store-Forward_Min_latency_ns': '2',
1184                  'Store-Forward_Max_latency_ns': '2'
1185                  }],
1186             'port_statistics': [
1187                 {'Frames_Tx': '3000',
1188                  'Valid_Frames_Rx': '3000',
1189                  'Bytes_Rx': '192000',
1190                  'Bytes_Tx': '192000',
1191                  'Rx_Rate_Kbps': '0.0',
1192                  'Tx_Rate_Kbps': '0.0',
1193                  'Rx_Rate_Mbps': '0.0',
1194                  'Tx_Rate_Mbps': '0.0',
1195                  'port_name': 'Ethernet - 001'},
1196                 {'Frames_Tx': '3000',
1197                  'Valid_Frames_Rx': '3000',
1198                  'Bytes_Rx': '192000',
1199                  'Bytes_Tx': '192000',
1200                  'Rx_Rate_Kbps': '0.0',
1201                  'Tx_Rate_Kbps': '0.0',
1202                  'Rx_Rate_Mbps': '0.0',
1203                  'Tx_Rate_Mbps': '0.0',
1204                  'port_name': 'Ethernet - 002'}],
1205             'pppox_client_per_port': [
1206                 {'Sessions_Down': '0',
1207                  'Sessions_Not_Started': '0',
1208                  'Sessions_Total': '1',
1209                  'Sessions_Up': '1',
1210                  'subs_port': 'Ethernet - 001'}]}
1211
1212         prio_flows_stats = {
1213             '0': {
1214                 'in_packets': 6000,
1215                 'out_packets': 6000,
1216                 'RxThroughput': 200.0,
1217                 'TxThroughput': 200.0,
1218                 'avg_latency_ns': 2,
1219                 'max_latency_ns': 2,
1220                 'min_latency_ns': 2
1221             }
1222         }
1223
1224         expected_result = {'priority_stats': {
1225             '0': {'RxThroughput': 200.0,
1226                   'TxThroughput': 200.0,
1227                   'avg_latency_ns': 2,
1228                   'max_latency_ns': 2,
1229                   'min_latency_ns': 2,
1230                   'in_packets': 6000,
1231                   'out_packets': 6000}},
1232             'xe0': {'RxThroughput': 100.0,
1233                     'Store-Forward_Avg_latency_ns': 2,
1234                     'Store-Forward_Max_latency_ns': 2,
1235                     'Store-Forward_Min_latency_ns': 2,
1236                     'TxThroughput': 100.0,
1237                     'in_packets': 3000,
1238                     'out_packets': 3000,
1239                     'in_bytes': 192000,
1240                     'out_bytes': 192000,
1241                     'RxThroughputBps': 6400.0,
1242                     'TxThroughputBps': 6400.0,
1243                     'rx_throughput_kps': 0.0,
1244                     'rx_throughput_mbps': 0.0,
1245                     'sessions_down': 0,
1246                     'sessions_not_started': 0,
1247                     'sessions_total': 1,
1248                     'sessions_up': 1,
1249                     'tx_throughput_kps': 0.0,
1250                     'tx_throughput_mbps': 0.0},
1251             'xe1': {'RxThroughput': 100.0,
1252                     'Store-Forward_Avg_latency_ns': 2,
1253                     'Store-Forward_Max_latency_ns': 2,
1254                     'Store-Forward_Min_latency_ns': 2,
1255                     'TxThroughput': 100.0,
1256                     'in_packets': 3000,
1257                     'out_packets': 3000,
1258                     'in_bytes': 192000,
1259                     'out_bytes': 192000,
1260                     'RxThroughputBps': 6400.0,
1261                     'TxThroughputBps': 6400.0,
1262                     'rx_throughput_kps': 0.0,
1263                     'rx_throughput_mbps': 0.0,
1264                     'tx_throughput_kps': 0.0,
1265                     'tx_throughput_mbps': 0.0}}
1266
1267         mock_get_stats.return_value = ixia_stats
1268         mock_prio_flow_statistics.return_value = prio_flows_stats
1269         ports = [0, 1]
1270         port_names = [{'name': 'xe0'}, {'name': 'xe1'}]
1271         duration = 30
1272         res_helper = mock.Mock()
1273         res_helper.vnfd_helper.find_interface_by_port.side_effect = \
1274             port_names
1275         samples = self.scenario.generate_samples(res_helper, ports, duration)
1276         self.assertIsNotNone(samples)
1277         self.assertIsNotNone(samples.get('xe0'))
1278         self.assertIsNotNone(samples.get('xe1'))
1279         self.assertEqual(samples, expected_result)
1280         mock_get_stats.assert_called_once()
1281         mock_prio_flow_statistics.assert_called_once()