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