Merge "Create docker image for Ubuntu 18.04"
[yardstick.git] / yardstick / tests / unit / network_services / vnf_generic / vnf / test_tg_rfc2544_ixia.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import os
16
17 import mock
18 import six
19 import unittest
20 import ipaddress
21
22 from yardstick.common import utils
23 from yardstick.common import exceptions
24 from yardstick.benchmark import contexts
25 from yardstick.benchmark.contexts import base as ctx_base
26 from yardstick.network_services.libs.ixia_libs.ixnet import ixnet_api
27 from yardstick.network_services.traffic_profile import base as tp_base
28 from yardstick.network_services.vnf_generic.vnf import tg_rfc2544_ixia
29
30
31 TEST_FILE_YAML = 'nsb_test_case.yaml'
32
33 NAME = "tg__1"
34
35
36 class TestIxiaResourceHelper(unittest.TestCase):
37
38     def setUp(self):
39         self._mock_IxNextgen = mock.patch.object(ixnet_api, 'IxNextgen')
40         self.mock_IxNextgen = self._mock_IxNextgen.start()
41         self.addCleanup(self._stop_mocks)
42
43     def _stop_mocks(self):
44         self._mock_IxNextgen.stop()
45
46     def test___init___with_custom_rfc_helper(self):
47         class MyRfcHelper(tg_rfc2544_ixia.IxiaRfc2544Helper):
48             pass
49
50         ixia_resource_helper = tg_rfc2544_ixia.IxiaResourceHelper(
51             mock.Mock(), MyRfcHelper)
52         self.assertIsInstance(ixia_resource_helper.rfc_helper, MyRfcHelper)
53
54     def test__init_ix_scenario(self):
55         mock_scenario = mock.Mock()
56         mock_scenario_helper = mock.Mock()
57         mock_scenario_helper.scenario_cfg = {'ixia_config': 'TestScenario',
58                                              'options': 'scenario_options'}
59         mock_setup_helper = mock.Mock(scenario_helper=mock_scenario_helper)
60         ixia_resource_helper = tg_rfc2544_ixia.IxiaResourceHelper(mock_setup_helper)
61         ixia_resource_helper._ixia_scenarios = {'TestScenario': mock_scenario}
62         ixia_resource_helper.client = 'client'
63         ixia_resource_helper.context_cfg = 'context'
64         ixia_resource_helper._init_ix_scenario()
65         mock_scenario.assert_called_once_with('client', 'context', 'scenario_options')
66
67     def test__init_ix_scenario_not_supported_cfg_type(self):
68         mock_scenario_helper = mock.Mock()
69         mock_scenario_helper.scenario_cfg = {'ixia_config': 'FakeScenario',
70                                              'options': 'scenario_options'}
71         mock_setup_helper = mock.Mock(scenario_helper=mock_scenario_helper)
72         ixia_resource_helper = tg_rfc2544_ixia.IxiaResourceHelper(mock_setup_helper)
73         ixia_resource_helper._ixia_scenarios = {'TestScenario': mock.Mock()}
74         with self.assertRaises(RuntimeError):
75             ixia_resource_helper._init_ix_scenario()
76
77     @mock.patch.object(tg_rfc2544_ixia.IxiaResourceHelper, '_init_ix_scenario')
78     def test_setup(self, mock__init_ix_scenario):
79         ixia_resource_helper = tg_rfc2544_ixia.IxiaResourceHelper(mock.Mock())
80         ixia_resource_helper.setup()
81         mock__init_ix_scenario.assert_called_once()
82
83     def test_stop_collect_with_client(self):
84         mock_client = mock.Mock()
85         ixia_resource_helper = tg_rfc2544_ixia.IxiaResourceHelper(mock.Mock())
86         ixia_resource_helper.client = mock_client
87         ixia_resource_helper._ix_scenario = mock.Mock()
88         ixia_resource_helper.stop_collect()
89         self.assertEqual(1, ixia_resource_helper._terminated.value)
90         ixia_resource_helper._ix_scenario.stop_protocols.assert_called_once()
91
92     def test_run_traffic(self):
93         mock_tprofile = mock.Mock()
94         mock_tprofile.config.duration = 10
95         mock_tprofile.get_drop_percentage.return_value = True, 'fake_samples'
96         ixia_rhelper = tg_rfc2544_ixia.IxiaResourceHelper(mock.Mock())
97         ixia_rhelper.rfc_helper = mock.Mock()
98         ixia_rhelper.vnfd_helper = mock.Mock()
99         ixia_rhelper._ix_scenario = mock.Mock()
100         ixia_rhelper.vnfd_helper.port_pairs.all_ports = []
101         with mock.patch.object(ixia_rhelper, 'generate_samples'), \
102                 mock.patch.object(ixia_rhelper, '_build_ports'), \
103                 mock.patch.object(ixia_rhelper, '_initialize_client'), \
104                 mock.patch.object(utils, 'wait_until_true'):
105             ixia_rhelper.run_traffic(mock_tprofile)
106
107         self.assertEqual('fake_samples', ixia_rhelper._queue.get())
108
109
110 @mock.patch.object(tg_rfc2544_ixia, 'ixnet_api')
111 class TestIXIATrafficGen(unittest.TestCase):
112     VNFD = {'vnfd:vnfd-catalog':
113             {'vnfd':
114              [{'short-name': 'VpeVnf',
115                'vdu':
116                [{'routing_table':
117                              [{'network': '152.16.100.20',
118                                'netmask': '255.255.255.0',
119                                'gateway': '152.16.100.20',
120                                'if': 'xe0'},
121                               {'network': '152.16.40.20',
122                                'netmask': '255.255.255.0',
123                                'gateway': '152.16.40.20',
124                                'if': 'xe1'}],
125                              'description': 'VPE approximation using DPDK',
126                              'name': 'vpevnf-baremetal',
127                              'nd_route_tbl':
128                                  [{'network': '0064:ff9b:0:0:0:0:9810:6414',
129                                    'netmask': '112',
130                                    'gateway': '0064:ff9b:0:0:0:0:9810:6414',
131                                    'if': 'xe0'},
132                                   {'network': '0064:ff9b:0:0:0:0:9810:2814',
133                                    'netmask': '112',
134                                    'gateway': '0064:ff9b:0:0:0:0:9810:2814',
135                                    'if': 'xe1'}],
136                              'id': 'vpevnf-baremetal',
137                              'external-interface':
138                                  [{'virtual-interface':
139                                    {'dst_mac': '00:00:00:00:00:04',
140                                     'vpci': '0000:05:00.0',
141                                     'local_ip': '152.16.100.19',
142                                     'type': 'PCI-PASSTHROUGH',
143                                     'netmask': '255.255.255.0',
144                                     'dpdk_port_num': 0,
145                                     'bandwidth': '10 Gbps',
146                                     'driver': "i40e",
147                                     'dst_ip': '152.16.100.20',
148                                     'local_iface_name': 'xe0',
149                                     'local_mac': '00:00:00:00:00:02'},
150                                    'vnfd-connection-point-ref': 'xe0',
151                                    'name': 'xe0'},
152                                   {'virtual-interface':
153                                    {'dst_mac': '00:00:00:00:00:03',
154                                     'vpci': '0000:05:00.1',
155                                     'local_ip': '152.16.40.19',
156                                     'type': 'PCI-PASSTHROUGH',
157                                     'driver': "i40e",
158                                     'netmask': '255.255.255.0',
159                                     'dpdk_port_num': 1,
160                                     'bandwidth': '10 Gbps',
161                                     'dst_ip': '152.16.40.20',
162                                     'local_iface_name': 'xe1',
163                                     'local_mac': '00:00:00:00:00:01'},
164                                    'vnfd-connection-point-ref': 'xe1',
165                                    'name': 'xe1'}]}],
166                'description': 'Vpe approximation using DPDK',
167                'mgmt-interface':
168                {'vdu-id': 'vpevnf-baremetal',
169                 'host': '1.1.1.1',
170                 'password': 'r00t',
171                             'user': 'root',
172                             'ip': '1.1.1.1'},
173                'benchmark':
174                {'kpi': ['packets_in', 'packets_fwd',
175                         'packets_dropped']},
176                'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
177                                     {'type': 'VPORT', 'name': 'xe1'}],
178                'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'}]}}
179
180     TRAFFIC_PROFILE = {
181         "schema": "isb:traffic_profile:0.1",
182         "name": "fixed",
183         "description": "Fixed traffic profile to run UDP traffic",
184         "traffic_profile": {
185             "traffic_type": "FixedTraffic",
186             "frame_rate": 100,  # pps
187             "flow_number": 10,
188             "frame_size": 64}}
189
190     TC_YAML = {'scenarios': [{'tc_options':
191                               {'rfc2544': {'allowed_drop_rate': '0.8 - 1'}},
192                               'runner': {'duration': 400,
193                                          'interval': 35, 'type': 'Duration'},
194                               'traffic_options':
195                                   {'flow': 'ipv4_1flow_Packets_vpe.yaml',
196                                    'imix': 'imix_voice.yaml'},
197                               'vnf_options': {'vpe': {'cfg': 'vpe_config'}},
198                               'traffic_profile': 'ipv4_throughput_vpe.yaml',
199                               'type': 'NSPerf',
200                               'nodes': {'tg__1': 'trafficgen_1.yardstick',
201                                         'vnf__1': 'vnf.yardstick'},
202                               'topology': 'vpe_vnf_topology.yaml'}],
203                'context': {'nfvi_type': 'baremetal',
204                            'type': contexts.CONTEXT_NODE,
205                            'name': 'yardstick',
206                            'file': '/etc/yardstick/nodes/pod.yaml'},
207                'schema': 'yardstick:task:0.1'}
208
209     def test___init__(self, *args):
210         with mock.patch("yardstick.ssh.SSH") as ssh:
211             ssh_mock = mock.Mock(autospec=ssh.SSH)
212             ssh_mock.execute = \
213                 mock.Mock(return_value=(0, "", ""))
214             ssh.from_node.return_value = ssh_mock
215             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
216             # NOTE(ralonsoh): check the object returned.
217             tg_rfc2544_ixia.IxiaTrafficGen(NAME, vnfd, 'task_id')
218
219     def test_listen_traffic(self, *args):
220         with mock.patch("yardstick.ssh.SSH") as ssh:
221             ssh_mock = mock.Mock(autospec=ssh.SSH)
222             ssh_mock.execute = \
223                 mock.Mock(return_value=(0, "", ""))
224             ssh.from_node.return_value = ssh_mock
225             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
226             ixnet_traffic_gen = tg_rfc2544_ixia.IxiaTrafficGen(NAME, vnfd,
227                                                                'task_id')
228             self.assertIsNone(ixnet_traffic_gen.listen_traffic({}))
229
230     @mock.patch.object(ctx_base.Context, 'get_context_from_server', return_value='fake_context')
231     def test_instantiate(self, *args):
232         with mock.patch("yardstick.ssh.SSH") as ssh:
233             ssh_mock = mock.Mock(autospec=ssh.SSH)
234             ssh_mock.execute = \
235                 mock.Mock(return_value=(0, "", ""))
236             ssh_mock.run = \
237                 mock.Mock(return_value=(0, "", ""))
238             ssh.from_node.return_value = ssh_mock
239             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
240             ixnet_traffic_gen = tg_rfc2544_ixia.IxiaTrafficGen(NAME, vnfd,
241                                                                'task_id')
242             scenario_cfg = {'tc': "nsb_test_case",
243                             "topology": ""}
244             scenario_cfg.update(
245                 {
246                     'options': {
247                         'packetsize': 64,
248                         'traffic_type': 4,
249                         'rfc2544': {
250                             'allowed_drop_rate': '0.8 - 1'},
251                         'vnf__1': {
252                             'rules': 'acl_1rule.yaml',
253                             'vnf_config': {
254                                 'lb_config': 'SW',
255                                 'lb_count': 1,
256                                 'worker_config': '1C/1T',
257                                 'worker_threads': 1}}}})
258             scenario_cfg.update({
259                 'nodes': {ixnet_traffic_gen.name: "mock"}
260             })
261             ixnet_traffic_gen.topology = ""
262             ixnet_traffic_gen.get_ixobj = mock.MagicMock()
263             ixnet_traffic_gen._ixia_traffic_gen = mock.MagicMock()
264             ixnet_traffic_gen._ixia_traffic_gen._connect = mock.Mock()
265             self.assertRaises(
266                 IOError,
267                 ixnet_traffic_gen.instantiate(scenario_cfg, {}))
268
269     @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server', return_value='mock_node')
270     def test_collect_kpi(self, *args):
271         with mock.patch("yardstick.ssh.SSH") as ssh:
272             ssh_mock = mock.Mock(autospec=ssh.SSH)
273             ssh_mock.execute = \
274                 mock.Mock(return_value=(0, "", ""))
275             ssh.from_node.return_value = ssh_mock
276
277             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
278             ixnet_traffic_gen = tg_rfc2544_ixia.IxiaTrafficGen(NAME, vnfd,
279                                                                'task_id')
280             ixnet_traffic_gen.scenario_helper.scenario_cfg = {
281                 'nodes': {ixnet_traffic_gen.name: "mock"}
282             }
283             ixnet_traffic_gen.data = {}
284             restult = ixnet_traffic_gen.collect_kpi()
285
286             expected = {'collect_stats': {},
287                         'physical_node': 'mock_node'}
288
289             self.assertEqual(expected, restult)
290
291     def test_terminate(self, *args):
292         with mock.patch("yardstick.ssh.SSH") as ssh:
293             vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
294             ssh_mock = mock.Mock(autospec=ssh.SSH)
295             ssh_mock.execute = \
296                 mock.Mock(return_value=(0, "", ""))
297             ssh.from_node.return_value = ssh_mock
298             ixnet_traffic_gen = tg_rfc2544_ixia.IxiaTrafficGen(
299                 NAME, vnfd, 'task_id', resource_helper_type=mock.Mock())
300             ixnet_traffic_gen._terminated = mock.MagicMock()
301             ixnet_traffic_gen._terminated.value = 0
302             ixnet_traffic_gen._ixia_traffic_gen = mock.MagicMock()
303             ixnet_traffic_gen._ixia_traffic_gen.ix_stop_traffic = mock.Mock()
304             ixnet_traffic_gen._traffic_process = mock.MagicMock()
305             ixnet_traffic_gen._traffic_process.terminate = mock.Mock()
306             self.assertIsNone(ixnet_traffic_gen.terminate())
307
308     def _get_file_abspath(self, filename):
309         curr_path = os.path.dirname(os.path.abspath(__file__))
310         file_path = os.path.join(curr_path, filename)
311         return file_path
312
313     def test__check_status(self, *args):
314         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
315         sut = tg_rfc2544_ixia.IxiaTrafficGen('vnf1', vnfd, 'task_id')
316         sut._check_status()
317
318     @mock.patch("yardstick.ssh.SSH")
319     def test_traffic_runner(self, mock_ssh, *args):
320         mock_traffic_profile = mock.Mock(autospec=tp_base.TrafficProfile)
321         mock_traffic_profile.get_traffic_definition.return_value = "64"
322         mock_traffic_profile.params = self.TRAFFIC_PROFILE
323         # traffic_profile.ports is standardized on port_num
324         mock_traffic_profile.ports = [0, 1]
325
326         mock_ssh_instance = mock.Mock(autospec=mock_ssh.SSH)
327         mock_ssh_instance.execute.return_value = 0, "", ""
328         mock_ssh_instance.run.return_value = 0, "", ""
329
330         mock_ssh.from_node.return_value = mock_ssh_instance
331
332         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
333         vnfd["mgmt-interface"].update({
334             'tg-config': {
335                 "ixchassis": "1.1.1.1",
336                 "py_bin_path": "/root",
337             }
338         })
339
340         samples = {}
341         name = ''
342         for ifname in range(1):
343             name = "xe{}".format(ifname)
344             samples[name] = {
345                 "Rx_Rate_Kbps": 20,
346                 "Tx_Rate_Kbps": 20,
347                 "Rx_Rate_Mbps": 10,
348                 "Tx_Rate_Mbps": 10,
349                 "RxThroughput": 10,
350                 "TxThroughput": 10,
351                 "Valid_Frames_Rx": 1000,
352                 "Frames_Tx": 1000,
353                 "in_packets": 1000,
354                 "out_packets": 1000,
355             }
356
357         samples.update({"CurrentDropPercentage": 0.0})
358
359         last_res = [
360             0,
361             {
362                 "Rx_Rate_Kbps": [20, 20],
363                 "Tx_Rate_Kbps": [20, 20],
364                 "Rx_Rate_Mbps": [10, 10],
365                 "Tx_Rate_Mbps": [10, 10],
366                 "CurrentDropPercentage": [0, 0],
367                 "RxThroughput": [10, 10],
368                 "TxThroughput": [10, 10],
369                 "Frames_Tx": [1000, 1000],
370                 "in_packets": [1000, 1000],
371                 "Valid_Frames_Rx": [1000, 1000],
372                 "out_packets": [1000, 1000],
373             },
374         ]
375
376         mock_traffic_profile.execute_traffic.return_value = [
377             'Completed', samples]
378         mock_traffic_profile.get_drop_percentage.return_value = [
379             'Completed', samples]
380
381         sut = tg_rfc2544_ixia.IxiaTrafficGen(name, vnfd, 'task_id')
382         sut.vnf_port_pairs = [[[0], [1]]]
383         sut.tc_file_name = self._get_file_abspath(TEST_FILE_YAML)
384         sut.topology = ""
385
386         sut.ssh_helper = mock.Mock()
387         sut._traffic_process = mock.MagicMock()
388         sut.generate_port_pairs = mock.Mock()
389
390         sut._ixia_traffic_gen = mock.MagicMock()
391         sut._ixia_traffic_gen.ix_get_statistics.return_value = last_res
392
393         sut.resource_helper.client = mock.MagicMock()
394         sut.resource_helper.client_started = mock.MagicMock()
395         sut.resource_helper.client_started.value = 1
396         sut.resource_helper.rfc_helper.iteration.value = 11
397         sut.resource_helper._ix_scenario = mock.Mock()
398
399         sut.scenario_helper.scenario_cfg = {
400             'options': {
401                 'packetsize': 64,
402                 'traffic_type': 4,
403                 'rfc2544': {
404                     'allowed_drop_rate': '0.8 - 1',
405                     'latency': True
406                 },
407                 'vnf__1': {
408                     'rules': 'acl_1rule.yaml',
409                     'vnf_config': {
410                         'lb_config': 'SW',
411                         'lb_count': 1,
412                         'worker_config': '1C/1T',
413                         'worker_threads': 1,
414                     },
415                 },
416             },
417             'task_path': '/path/to/task'
418         }
419
420         @mock.patch.object(six.moves.builtins, 'open', create=True)
421         @mock.patch('yardstick.network_services.vnf_generic.vnf.tg_rfc2544_ixia.open',
422                     mock.mock_open(), create=True)
423         @mock.patch('yardstick.network_services.vnf_generic.vnf.tg_rfc2544_ixia.LOG.exception')
424         def _traffic_runner(*args):
425             sut._setup_mq_producer = mock.Mock(return_value='mq_producer')
426             result = sut._traffic_runner(mock_traffic_profile, mock.ANY)
427             self.assertIsNone(result)
428
429         _traffic_runner()
430
431
432 class TestIxiaBasicScenario(unittest.TestCase):
433
434     def setUp(self):
435         self._mock_IxNextgen = mock.patch.object(ixnet_api, 'IxNextgen')
436         self.mock_IxNextgen = self._mock_IxNextgen.start()
437         self.context_cfg = mock.Mock()
438         self.ixia_cfg = mock.Mock()
439         self.scenario = tg_rfc2544_ixia.IxiaBasicScenario(self.mock_IxNextgen,
440                                                           self.context_cfg,
441                                                           self.ixia_cfg)
442         self.addCleanup(self._stop_mocks)
443
444     def _stop_mocks(self):
445         self._mock_IxNextgen.stop()
446
447     def test___init___(self):
448         self.assertIsInstance(self.scenario, tg_rfc2544_ixia.IxiaBasicScenario)
449         self.assertEqual(self.scenario.client, self.mock_IxNextgen)
450
451     def test_apply_config(self):
452         self.assertIsNone(self.scenario.apply_config())
453
454     def test_create_traffic_model(self):
455         self.mock_IxNextgen.get_vports.return_value = [1, 2, 3, 4]
456         self.scenario.create_traffic_model()
457         self.scenario.client.get_vports.assert_called_once()
458         self.scenario.client.create_traffic_model.assert_called_once_with([1, 3], [2, 4])
459
460     def test_run_protocols(self):
461         self.assertIsNone(self.scenario.run_protocols())
462
463     def test_stop_protocols(self):
464         self.assertIsNone(self.scenario.stop_protocols())
465
466
467 class TestIxiaPppoeClientScenario(unittest.TestCase):
468
469     IXIA_CFG = {
470         'pppoe_client': {
471             'sessions_per_port': 4,
472             'sessions_per_svlan': 1,
473             's_vlan': 10,
474             'c_vlan': 20,
475             'ip': ['10.3.3.1', '10.4.4.1']
476         },
477         'ipv4_client': {
478             'sessions_per_port': 1,
479             'sessions_per_vlan': 1,
480             'vlan': 101,
481             'gateway_ip': ['10.1.1.1', '10.2.2.1'],
482             'ip': ['10.1.1.1', '10.2.2.1'],
483             'prefix': ['24', '24']
484         }
485     }
486
487     CONTEXT_CFG = {
488         'nodes': {'tg__0': {
489             'interfaces': {'xe0': {
490                 'local_ip': '10.1.1.1',
491                 'netmask': '255.255.255.0'
492                 }}}}}
493
494     def setUp(self):
495         self._mock_IxNextgen = mock.patch.object(ixnet_api, 'IxNextgen')
496         self.mock_IxNextgen = self._mock_IxNextgen.start()
497         self.scenario = tg_rfc2544_ixia.IxiaPppoeClientScenario(
498             self.mock_IxNextgen, self.CONTEXT_CFG, self.IXIA_CFG)
499         tg_rfc2544_ixia.WAIT_PROTOCOLS_STARTED = 2
500         self.addCleanup(self._stop_mocks)
501
502     def _stop_mocks(self):
503         self._mock_IxNextgen.stop()
504
505     def test___init___(self):
506         self.assertIsInstance(self.scenario, tg_rfc2544_ixia.IxiaPppoeClientScenario)
507         self.assertEqual(self.scenario.client, self.mock_IxNextgen)
508
509     @mock.patch.object(tg_rfc2544_ixia.IxiaPppoeClientScenario,
510                        '_fill_ixia_config')
511     @mock.patch.object(tg_rfc2544_ixia.IxiaPppoeClientScenario,
512                        '_apply_access_network_config')
513     @mock.patch.object(tg_rfc2544_ixia.IxiaPppoeClientScenario,
514                        '_apply_core_network_config')
515     def test_apply_config(self, mock_apply_core_net_cfg,
516                           mock_apply_access_net_cfg,
517                           mock_fill_ixia_config):
518         self.mock_IxNextgen.get_vports.return_value = [1, 2, 3, 4]
519         self.scenario.apply_config()
520         self.scenario.client.get_vports.assert_called_once()
521         self.assertEqual(self.scenario._uplink_vports, [1, 3])
522         self.assertEqual(self.scenario._downlink_vports, [2, 4])
523         mock_fill_ixia_config.assert_called_once()
524         mock_apply_core_net_cfg.assert_called_once()
525         mock_apply_access_net_cfg.assert_called_once()
526
527     def test_create_traffic_model(self):
528         self.scenario._access_topologies = 'access'
529         self.scenario._core_topologies = 'core'
530         self.scenario.create_traffic_model()
531         self.scenario.client.create_ipv4_traffic_model.assert_called_once_with(
532             'access', 'core')
533
534     def test_run_protocols(self):
535         self.scenario.client.is_protocols_running.return_value = True
536         self.scenario.run_protocols()
537         self.scenario.client.start_protocols.assert_called_once()
538
539     def test_run_protocols_timeout_exception(self):
540         self.scenario.client.is_protocols_running.return_value = False
541         with self.assertRaises(exceptions.WaitTimeout):
542             self.scenario.run_protocols()
543         self.scenario.client.start_protocols.assert_called_once()
544
545     def test_stop_protocols(self):
546         self.scenario.stop_protocols()
547         self.scenario.client.stop_protocols.assert_called_once()
548
549     def test__get_intf_addr_str_type_input(self):
550         intf = '192.168.10.2/24'
551         ip, mask = self.scenario._get_intf_addr(intf)
552         self.assertEqual(ip, '192.168.10.2')
553         self.assertEqual(mask, 24)
554
555     def test__get_intf_addr_dict_type_input(self):
556         intf = {'tg__0': 'xe0'}
557         ip, mask = self.scenario._get_intf_addr(intf)
558         self.assertEqual(ip, '10.1.1.1')
559         self.assertEqual(mask, 24)
560
561     @mock.patch.object(tg_rfc2544_ixia.IxiaPppoeClientScenario, '_get_intf_addr')
562     def test__fill_ixia_config(self, mock_get_intf_addr):
563
564         ixia_cfg = {
565             'pppoe_client': {
566                 'sessions_per_port': 4,
567                 'sessions_per_svlan': 1,
568                 's_vlan': 10,
569                 'c_vlan': 20,
570                 'ip': ['10.3.3.1/24', '10.4.4.1/24']
571             },
572             'ipv4_client': {
573                 'sessions_per_port': 1,
574                 'sessions_per_vlan': 1,
575                 'vlan': 101,
576                 'gateway_ip': ['10.1.1.1/24', '10.2.2.1/24'],
577                 'ip': ['10.1.1.1/24', '10.2.2.1/24']
578             }
579         }
580
581         mock_get_intf_addr.side_effect = [
582             ('10.3.3.1', '24'),
583             ('10.4.4.1', '24'),
584             ('10.1.1.1', '24'),
585             ('10.2.2.1', '24'),
586             ('10.1.1.1', '24'),
587             ('10.2.2.1', '24')
588         ]
589         self.scenario._ixia_cfg = ixia_cfg
590         self.scenario._fill_ixia_config()
591         self.assertEqual(mock_get_intf_addr.call_count, 6)
592         self.assertEqual(self.scenario._ixia_cfg['pppoe_client']['ip'],
593                          ['10.3.3.1', '10.4.4.1'])
594         self.assertEqual(self.scenario._ixia_cfg['ipv4_client']['ip'],
595                          ['10.1.1.1', '10.2.2.1'])
596         self.assertEqual(self.scenario._ixia_cfg['ipv4_client']['prefix'],
597                          ['24', '24'])
598
599     @mock.patch('yardstick.network_services.libs.ixia_libs.ixnet.ixnet_api.Vlan')
600     def test__apply_access_network_config_pap_auth(self, mock_vlan):
601         _ixia_cfg = {
602             'pppoe_client': {
603                 'sessions_per_port': 4,
604                 'sessions_per_svlan': 1,
605                 's_vlan': 10,
606                 'c_vlan': 20,
607                 'pap_user': 'test_pap',
608                 'pap_password': 'pap'
609                 }}
610         pap_user = _ixia_cfg['pppoe_client']['pap_user']
611         pap_passwd = _ixia_cfg['pppoe_client']['pap_password']
612         self.scenario._ixia_cfg = _ixia_cfg
613         self.scenario._uplink_vports = [0, 2]
614         self.scenario.client.add_topology.side_effect = ['Topology 1', 'Topology 2']
615         self.scenario.client.add_device_group.side_effect = ['Dg1', 'Dg2', 'Dg3',
616                                                              'Dg4', 'Dg5', 'Dg6',
617                                                              'Dg7', 'Dg8']
618         self.scenario.client.add_ethernet.side_effect = ['Eth1', 'Eth2', 'Eth3',
619                                                          'Eth4', 'Eth5', 'Eth6',
620                                                          'Eth7', 'Eth8']
621         self.scenario._apply_access_network_config()
622         self.assertEqual(self.scenario.client.add_topology.call_count, 2)
623         self.assertEqual(self.scenario.client.add_device_group.call_count, 8)
624         self.assertEqual(self.scenario.client.add_ethernet.call_count, 8)
625         self.assertEqual(mock_vlan.call_count, 16)
626         self.assertEqual(self.scenario.client.add_vlans.call_count, 8)
627         self.assertEqual(self.scenario.client.add_pppox_client.call_count, 8)
628         self.scenario.client.add_topology.assert_has_calls([
629             mock.call('Topology access 0', 0),
630             mock.call('Topology access 1', 2)
631         ])
632         self.scenario.client.add_device_group.assert_has_calls([
633             mock.call('Topology 1', 'SVLAN 10', 1),
634             mock.call('Topology 1', 'SVLAN 11', 1),
635             mock.call('Topology 1', 'SVLAN 12', 1),
636             mock.call('Topology 1', 'SVLAN 13', 1),
637             mock.call('Topology 2', 'SVLAN 14', 1),
638             mock.call('Topology 2', 'SVLAN 15', 1),
639             mock.call('Topology 2', 'SVLAN 16', 1),
640             mock.call('Topology 2', 'SVLAN 17', 1)
641         ])
642         self.scenario.client.add_ethernet.assert_has_calls([
643             mock.call('Dg1', 'Ethernet'),
644             mock.call('Dg2', 'Ethernet'),
645             mock.call('Dg3', 'Ethernet'),
646             mock.call('Dg4', 'Ethernet'),
647             mock.call('Dg5', 'Ethernet'),
648             mock.call('Dg6', 'Ethernet'),
649             mock.call('Dg7', 'Ethernet'),
650             mock.call('Dg8', 'Ethernet')
651         ])
652         mock_vlan.assert_has_calls([
653             mock.call(vlan_id=10),
654             mock.call(vlan_id=20, vlan_id_step=1),
655             mock.call(vlan_id=11),
656             mock.call(vlan_id=20, vlan_id_step=1),
657             mock.call(vlan_id=12),
658             mock.call(vlan_id=20, vlan_id_step=1),
659             mock.call(vlan_id=13),
660             mock.call(vlan_id=20, vlan_id_step=1),
661             mock.call(vlan_id=14),
662             mock.call(vlan_id=20, vlan_id_step=1),
663             mock.call(vlan_id=15),
664             mock.call(vlan_id=20, vlan_id_step=1),
665             mock.call(vlan_id=16),
666             mock.call(vlan_id=20, vlan_id_step=1),
667             mock.call(vlan_id=17),
668             mock.call(vlan_id=20, vlan_id_step=1)
669         ])
670         self.scenario.client.add_pppox_client.assert_has_calls([
671             mock.call('Eth1', 'pap', pap_user, pap_passwd),
672             mock.call('Eth2', 'pap', pap_user, pap_passwd),
673             mock.call('Eth3', 'pap', pap_user, pap_passwd),
674             mock.call('Eth4', 'pap', pap_user, pap_passwd),
675             mock.call('Eth5', 'pap', pap_user, pap_passwd),
676             mock.call('Eth6', 'pap', pap_user, pap_passwd),
677             mock.call('Eth7', 'pap', pap_user, pap_passwd),
678             mock.call('Eth8', 'pap', pap_user, pap_passwd)
679         ])
680
681     def test__apply_access_network_config_chap_auth(self):
682         _ixia_cfg = {
683             'pppoe_client': {
684                 'sessions_per_port': 4,
685                 'sessions_per_svlan': 1,
686                 's_vlan': 10,
687                 'c_vlan': 20,
688                 'chap_user': 'test_chap',
689                 'chap_password': 'chap'
690             }}
691         chap_user = _ixia_cfg['pppoe_client']['chap_user']
692         chap_passwd = _ixia_cfg['pppoe_client']['chap_password']
693         self.scenario._ixia_cfg = _ixia_cfg
694         self.scenario._uplink_vports = [0, 2]
695         self.scenario.client.add_ethernet.side_effect = ['Eth1', 'Eth2', 'Eth3',
696                                                          'Eth4', 'Eth5', 'Eth6',
697                                                          'Eth7', 'Eth8']
698         self.scenario._apply_access_network_config()
699         self.assertEqual(self.scenario.client.add_pppox_client.call_count, 8)
700         self.scenario.client.add_pppox_client.assert_has_calls([
701             mock.call('Eth1', 'chap', chap_user, chap_passwd),
702             mock.call('Eth2', 'chap', chap_user, chap_passwd),
703             mock.call('Eth3', 'chap', chap_user, chap_passwd),
704             mock.call('Eth4', 'chap', chap_user, chap_passwd),
705             mock.call('Eth5', 'chap', chap_user, chap_passwd),
706             mock.call('Eth6', 'chap', chap_user, chap_passwd),
707             mock.call('Eth7', 'chap', chap_user, chap_passwd),
708             mock.call('Eth8', 'chap', chap_user, chap_passwd)
709         ])
710
711     @mock.patch('yardstick.network_services.libs.ixia_libs.ixnet.ixnet_api.Vlan')
712     def test__apply_core_network_config_no_bgp_proto(self, mock_vlan):
713         self.scenario._downlink_vports = [1, 3]
714         self.scenario.client.add_topology.side_effect = ['Topology 1', 'Topology 2']
715         self.scenario.client.add_device_group.side_effect = ['Dg1', 'Dg2']
716         self.scenario.client.add_ethernet.side_effect = ['Eth1', 'Eth2']
717         self.scenario._apply_core_network_config()
718         self.assertEqual(self.scenario.client.add_topology.call_count, 2)
719         self.assertEqual(self.scenario.client.add_device_group.call_count, 2)
720         self.assertEqual(self.scenario.client.add_ethernet.call_count, 2)
721         self.assertEqual(mock_vlan.call_count, 2)
722         self.assertEqual(self.scenario.client.add_vlans.call_count, 2)
723         self.assertEqual(self.scenario.client.add_ipv4.call_count, 2)
724         self.scenario.client.add_topology.assert_has_calls([
725             mock.call('Topology core 0', 1),
726             mock.call('Topology core 1', 3)
727         ])
728         self.scenario.client.add_device_group.assert_has_calls([
729             mock.call('Topology 1', 'Core port 0', 1),
730             mock.call('Topology 2', 'Core port 1', 1)
731         ])
732         self.scenario.client.add_ethernet.assert_has_calls([
733             mock.call('Dg1', 'Ethernet'),
734             mock.call('Dg2', 'Ethernet')
735         ])
736         mock_vlan.assert_has_calls([
737             mock.call(vlan_id=101),
738             mock.call(vlan_id=102)
739         ])
740         self.scenario.client.add_ipv4.assert_has_calls([
741             mock.call('Eth1', name='ipv4', addr=ipaddress.IPv4Address('10.1.1.2'),
742                       addr_step='0.0.0.1', prefix='24', gateway='10.1.1.1'),
743             mock.call('Eth2', name='ipv4', addr=ipaddress.IPv4Address('10.2.2.2'),
744                       addr_step='0.0.0.1', prefix='24', gateway='10.2.2.1')
745         ])
746         self.scenario.client.add_bgp.assert_not_called()
747
748     def test__apply_core_network_config_with_bgp_proto(self):
749         bgp_params = {
750             'bgp': {
751                 'bgp_type': 'external',
752                 'dut_ip': '10.0.0.1',
753                 'as_number': 65000
754             }
755         }
756         self.scenario._ixia_cfg['ipv4_client'].update(bgp_params)
757         self.scenario._downlink_vports = [1, 3]
758         self.scenario.client.add_ipv4.side_effect = ['ipv4_1', 'ipv4_2']
759         self.scenario._apply_core_network_config()
760         self.assertEqual(self.scenario.client.add_bgp.call_count, 2)
761         self.scenario.client.add_bgp.assert_has_calls([
762             mock.call('ipv4_1', dut_ip=bgp_params["bgp"]["dut_ip"],
763                       local_as=bgp_params["bgp"]["as_number"],
764                       bgp_type=bgp_params["bgp"]["bgp_type"]),
765             mock.call('ipv4_2', dut_ip=bgp_params["bgp"]["dut_ip"],
766                       local_as=bgp_params["bgp"]["as_number"],
767                       bgp_type=bgp_params["bgp"]["bgp_type"])
768         ])