Merge "Refactor "utils.parse_ini_file" testing"
[yardstick.git] / yardstick / tests / unit / network_services / vnf_generic / vnf / test_vpe_vnf.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
16 from multiprocessing import Process, Queue
17 import os
18 import time
19
20 import mock
21 from six.moves import configparser
22 import unittest
23
24 from yardstick.tests import STL_MOCKS
25 from yardstick.tests.unit.network_services.vnf_generic.vnf.test_base import FileAbsPath
26 from yardstick.tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
27 from yardstick.network_services.vnf_generic.vnf.base import QueueFileWrapper
28 from yardstick.network_services.vnf_generic.vnf.base import VnfdHelper
29
30
31 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
32
33 STLClient = mock.MagicMock()
34 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
35 stl_patch.start()
36
37 if stl_patch:
38     from yardstick.network_services.vnf_generic.vnf.vpe_vnf import ConfigCreate
39     from yardstick.network_services.nfvi.resource import ResourceProfile
40     from yardstick.network_services.vnf_generic.vnf.vpe_vnf import \
41         VpeApproxVnf, VpeApproxSetupEnvHelper
42
43
44 TEST_FILE_YAML = 'nsb_test_case.yaml'
45
46 NAME = 'vnf_1'
47
48 PING_OUTPUT_1 = "Pkts in: 101\r\n\tPkts dropped by AH: 100\r\n\tPkts dropped by other: 100"
49
50 MODULE_PATH = FileAbsPath(__file__)
51 get_file_abspath = MODULE_PATH.get_path
52
53
54 class TestConfigCreate(unittest.TestCase):
55
56     VNFD_0 = {
57         'short-name': 'VpeVnf',
58         'vdu': [
59             {
60                 'routing_table': [
61                     {
62                         'network': '152.16.100.20',
63                         'netmask': '255.255.255.0',
64                         'gateway': '152.16.100.20',
65                         'if': 'xe0'
66                     },
67                     {
68                         'network': '152.16.40.20',
69                         'netmask': '255.255.255.0',
70                         'gateway': '152.16.40.20',
71                         'if': 'xe1'
72                     },
73                 ],
74                 'description': 'VPE approximation using DPDK',
75                 'name': 'vpevnf-baremetal',
76                 'nd_route_tbl': [
77                     {
78                         'network': '0064:ff9b:0:0:0:0:9810:6414',
79                         'netmask': '112',
80                         'gateway': '0064:ff9b:0:0:0:0:9810:6414',
81                         'if': 'xe0'
82                     },
83                     {
84                         'network': '0064:ff9b:0:0:0:0:9810:2814',
85                         'netmask': '112',
86                         'gateway': '0064:ff9b:0:0:0:0:9810:2814',
87                         'if': 'xe1'
88                     },
89                 ],
90                 'id': 'vpevnf-baremetal',
91                 'external-interface': [
92                     {
93                         'virtual-interface': {
94                             'dst_mac': '00:00:00:00:00:03',
95                             'vpci': '0000:05:00.0',
96                             'local_ip': '152.16.100.19',
97                             'type': 'PCI-PASSTHROUGH',
98                             'netmask': '255.255.255.0',
99                             'dpdk_port_num': 0,
100                             'bandwidth': '10 Gbps',
101                             'dst_ip': '152.16.100.20',
102                             'local_mac': '00:00:00:00:00:01',
103                             'vld_id': 'uplink_0',
104                             'ifname': 'xe0',
105                         },
106                         'vnfd-connection-point-ref': 'xe0',
107                         'name': 'xe0'
108                     },
109                     {
110                         'virtual-interface': {
111                             'dst_mac': '00:00:00:00:00:04',
112                             'vpci': '0000:05:00.1',
113                             'local_ip': '152.16.40.19',
114                             'type': 'PCI-PASSTHROUGH',
115                             'netmask': '255.255.255.0',
116                             'dpdk_port_num': 1,
117                             'bandwidth': '10 Gbps',
118                             'dst_ip': '152.16.40.20',
119                             'local_mac': '00:00:00:00:00:02',
120                             'vld_id': 'downlink_0',
121                             'ifname': 'xe1',
122                         },
123                         'vnfd-connection-point-ref': 'xe1',
124                         'name': 'xe1'
125                     },
126                 ],
127             },
128         ],
129         'description': 'Vpe approximation using DPDK',
130         'mgmt-interface': {
131             'vdu-id': 'vpevnf-baremetal',
132             'host': '1.1.1.1',
133             'password': 'r00t',
134             'user': 'root',
135             'ip': '1.1.1.1'
136         },
137         'benchmark': {
138             'kpi': [
139                 'packets_in',
140                 'packets_fwd',
141                 'packets_dropped',
142             ],
143         },
144         'connection-point': [
145             {
146                 'type': 'VPORT',
147                 'name': 'xe0',
148             },
149             {
150                 'type': 'VPORT',
151                 'name': 'xe1',
152             },
153         ],
154         'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'
155     }
156
157     def test___init__(self):
158         vnfd_helper = VnfdHelper(self.VNFD_0)
159         config_create = ConfigCreate(vnfd_helper, 2)
160         self.assertEqual(config_create.uplink_ports, ['xe0'])
161         self.assertEqual(config_create.downlink_ports, ['xe1'])
162         self.assertEqual(config_create.socket, 2)
163
164     def test_dpdk_port_to_link_id(self):
165         vnfd_helper = VnfdHelper(self.VNFD_0)
166         config_create = ConfigCreate(vnfd_helper, 2)
167         self.assertEqual(config_create.dpdk_port_to_link_id_map, {'xe0': 0, 'xe1': 1})
168
169     def test_vpe_initialize(self):
170         vnfd_helper = VnfdHelper(self.VNFD_0)
171         config_create = ConfigCreate(vnfd_helper, 2)
172         config = configparser.ConfigParser()
173         config_create.vpe_initialize(config)
174         self.assertEqual(config.get('EAL', 'log_level'), '0')
175         self.assertEqual(config.get('PIPELINE0', 'type'), 'MASTER')
176         self.assertEqual(config.get('PIPELINE0', 'core'), 's2C0')
177         self.assertEqual(config.get('MEMPOOL0', 'pool_size'), '256K')
178         self.assertEqual(config.get('MEMPOOL1', 'pool_size'), '2M')
179
180     def test_vpe_rxq(self):
181         vnfd_helper = VnfdHelper(self.VNFD_0)
182         config_create = ConfigCreate(vnfd_helper, 2)
183         config = configparser.ConfigParser()
184         config_create.downlink_ports = ['xe0']
185         config_create.vpe_rxq(config)
186         self.assertEqual(config.get('RXQ0.0', 'mempool'), 'MEMPOOL1')
187
188     def test_get_sink_swq(self):
189         vnfd_helper = VnfdHelper(self.VNFD_0)
190         config_create = ConfigCreate(vnfd_helper, 2)
191         config = configparser.ConfigParser()
192         config.add_section('PIPELINE0')
193         config.set('PIPELINE0', 'key1', 'value1')
194         config.set('PIPELINE0', 'key2', 'value2 SINK')
195         config.set('PIPELINE0', 'key3', 'TM value3')
196         config.set('PIPELINE0', 'key4', 'value4')
197         config.set('PIPELINE0', 'key5', 'the SINK value5')
198
199         self.assertEqual(config_create.get_sink_swq(config, 'PIPELINE0', 'key1', 5), 'SWQ-1')
200         self.assertEqual(config_create.get_sink_swq(config, 'PIPELINE0', 'key2', 5), 'SWQ-1 SINK0')
201         self.assertEqual(config_create.get_sink_swq(config, 'PIPELINE0', 'key3', 5), 'SWQ-1 TM5')
202         config_create.sw_q += 1
203         self.assertEqual(config_create.get_sink_swq(config, 'PIPELINE0', 'key4', 5), 'SWQ0')
204         self.assertEqual(config_create.get_sink_swq(config, 'PIPELINE0', 'key5', 5), 'SWQ0 SINK1')
205
206     def test_generate_vpe_script(self):
207         vnfd_helper = VnfdHelper(self.VNFD_0)
208         vpe_config_vnf = ConfigCreate(vnfd_helper, 2)
209         intf = [
210             {
211                 "name": 'xe1',
212                 "virtual-interface": {
213                     "dst_ip": "1.1.1.1",
214                     "dst_mac": "00:00:00:00:00:00:02",
215                 },
216             },
217             {
218                 "name": 'xe2',
219                 "virtual-interface": {
220                     "dst_ip": "1.1.1.1",
221                     "dst_mac": "00:00:00:00:00:00:02",
222                 },
223             },
224         ]
225         vpe_config_vnf.downlink_ports = ['xe1']
226         vpe_config_vnf.uplink_ports = ['xe2']
227         result = vpe_config_vnf.generate_vpe_script(intf)
228         self.assertIsInstance(result, str)
229         self.assertNotEqual(result, '')
230
231     def test_create_vpe_config(self):
232         vnfd_helper = VnfdHelper(self.VNFD_0)
233         config_create = ConfigCreate(vnfd_helper, 23)
234         config_create.downlink_ports = ['xe1']
235         config_create.uplink_ports = ['xe1']
236         curr_path = os.path.dirname(os.path.abspath(__file__))
237         vpe_cfg = "samples/vnf_samples/nsut/vpe/vpe_config"
238         vnf_cfg = os.path.join(curr_path, "../../../../..", vpe_cfg)
239         config_create.create_vpe_config(vnf_cfg)
240         os.system("git checkout -- %s" % vnf_cfg)
241
242
243 class TestVpeApproxVnf(unittest.TestCase):
244
245     VNFD_0 = {
246         'short-name': 'VpeVnf',
247         'vdu': [
248             {
249                 'routing_table': [
250                     {
251                         'network': '152.16.100.20',
252                         'netmask': '255.255.255.0',
253                         'gateway': '152.16.100.20',
254                         'if': 'xe0',
255                     },
256                     {
257                         'network': '152.16.40.20',
258                         'netmask': '255.255.255.0',
259                         'gateway': '152.16.40.20',
260                         'if': 'xe1',
261                     },
262                 ],
263                 'description': 'VPE approximation using DPDK',
264                 'name': 'vpevnf-baremetal',
265                 'nd_route_tbl': [
266                     {
267                         'network': '0064:ff9b:0:0:0:0:9810:6414',
268                         'netmask': '112',
269                         'gateway': '0064:ff9b:0:0:0:0:9810:6414',
270                         'if': 'xe0',
271                     },
272                     {
273                         'network': '0064:ff9b:0:0:0:0:9810:2814',
274                         'netmask': '112',
275                         'gateway': '0064:ff9b:0:0:0:0:9810:2814',
276                         'if': 'xe1',
277                     },
278                 ],
279                 'id': 'vpevnf-baremetal',
280                 'external-interface': [
281                     {
282                         'virtual-interface': {
283                             'dst_mac': '00:00:00:00:00:04',
284                             'vpci': '0000:05:00.0',
285                             'local_ip': '152.16.100.19',
286                             'type': 'PCI-PASSTHROUGH',
287                             'netmask': '255.255.255.0',
288                             'dpdk_port_num': 0,
289                             'bandwidth': '10 Gbps',
290                             'driver': "i40e",
291                             'dst_ip': '152.16.100.20',
292                             'local_iface_name': 'xe0',
293                             'local_mac': '00:00:00:00:00:02',
294                             'vld_id': 'uplink_0',
295                             'ifname': 'xe0',
296                         },
297                         'vnfd-connection-point-ref': 'xe0',
298                         'name': 'xe0',
299                     },
300                     {
301                         'virtual-interface': {
302                             'dst_mac': '00:00:00:00:00:03',
303                             'vpci': '0000:05:00.1',
304                             'local_ip': '152.16.40.19',
305                             'type': 'PCI-PASSTHROUGH',
306                             'driver': "i40e",
307                             'netmask': '255.255.255.0',
308                             'dpdk_port_num': 1,
309                             'bandwidth': '10 Gbps',
310                             'dst_ip': '152.16.40.20',
311                             'local_iface_name': 'xe1',
312                             'local_mac': '00:00:00:00:00:01',
313                             'vld_id': 'downlink_0',
314                             'ifname': 'xe1',
315                         },
316                         'vnfd-connection-point-ref': 'xe1',
317                         'name': 'xe1',
318                     },
319                 ],
320             },
321         ],
322         'description': 'Vpe approximation using DPDK',
323         'mgmt-interface': {
324             'vdu-id': 'vpevnf-baremetal',
325             'host': '1.2.1.1',
326             'password': 'r00t',
327             'user': 'root',
328             'ip': '1.2.1.1',
329         },
330         'benchmark': {
331             'kpi': [
332                 'packets_in',
333                 'packets_fwd',
334                 'packets_dropped',
335             ],
336         },
337         'connection-point': [
338             {
339                 'type': 'VPORT',
340                 'name': 'xe0',
341             },
342             {
343                 'type': 'VPORT',
344                 'name': 'xe1',
345             },
346         ],
347         'id': 'VpeApproxVnf',
348         'name': 'VPEVnfSsh',
349     }
350
351     VNFD = {
352         'vnfd:vnfd-catalog': {
353             'vnfd': [
354                 VNFD_0,
355             ],
356         },
357     }
358
359     SCENARIO_CFG = {
360         'options': {
361             'packetsize': 64,
362             'traffic_type': 4,
363             'rfc2544': {
364                 'allowed_drop_rate': '0.8 - 1',
365             },
366             'vnf__1': {
367                 'cfg': 'acl_1rule.yaml',
368                 'vnf_config': {
369                     'lb_config': 'SW',
370                     'lb_count': 1,
371                     'worker_config':
372                     '1C/1T',
373                     'worker_threads': 1,
374                 },
375             }
376         },
377         'task_id': 'a70bdf4a-8e67-47a3-9dc1-273c14506eb7',
378         'tc': 'tc_ipv4_1Mflow_64B_packetsize',
379         'runner': {
380             'object': 'NetworkServiceTestCase',
381             'interval': 35,
382             'output_filename': '/tmp/yardstick.out',
383             'runner_id': 74476,
384             'duration': 400,
385             'type': 'Duration',
386         },
387         'traffic_profile': 'ipv4_throughput_vpe.yaml',
388         'traffic_options': {
389             'flow': 'ipv4_Packets_vpe.yaml',
390             'imix': 'imix_voice.yaml',
391         },
392         'type': 'ISB',
393         'nodes': {
394             'tg__2': 'trafficgen_2.yardstick',
395             'tg__1': 'trafficgen_1.yardstick',
396             'vnf__1': 'vnf.yardstick',
397         },
398         'topology': 'vpe-tg-topology-baremetal.yaml',
399     }
400
401     CONTEXT_CFG = {
402         'nodes': {
403             'tg__2': {
404                 'member-vnf-index': '3',
405                 'role': 'TrafficGen',
406                 'name': 'trafficgen_2.yardstick',
407                 'vnfd-id-ref': 'tg__2',
408                 'ip': '1.2.1.1',
409                 'interfaces': {
410                     'xe0': {
411                         'local_iface_name': 'ens513f0',
412                         'vld_id': VpeApproxVnf.DOWNLINK,
413                         'netmask': '255.255.255.0',
414                         'local_ip': '152.16.40.20',
415                         'dst_mac': '00:00:00:00:00:01',
416                         'local_mac': '00:00:00:00:00:03',
417                         'dst_ip': '152.16.40.19',
418                         'driver': 'ixgbe',
419                         'vpci': '0000:02:00.0',
420                         'dpdk_port_num': 0,
421                     },
422                     'xe1': {
423                         'local_iface_name': 'ens513f1',
424                         'netmask': '255.255.255.0',
425                         'network': '202.16.100.0',
426                         'local_ip': '202.16.100.20',
427                         'local_mac': '00:1e:67:d0:60:5d',
428                         'driver': 'ixgbe',
429                         'vpci': '0000:02:00.1',
430                         'dpdk_port_num': 1,
431                     },
432                 },
433                 'password': 'r00t',
434                 'VNF model': 'l3fwd_vnf.yaml',
435                 'user': 'root',
436             },
437             'tg__1': {
438                 'member-vnf-index': '1',
439                 'role': 'TrafficGen',
440                 'name': 'trafficgen_1.yardstick',
441                 'vnfd-id-ref': 'tg__1',
442                 'ip': '1.2.1.1',
443                 'interfaces': {
444                     'xe0': {
445                         'local_iface_name': 'ens785f0',
446                         'vld_id': VpeApproxVnf.UPLINK,
447                         'netmask': '255.255.255.0',
448                         'local_ip': '152.16.100.20',
449                         'dst_mac': '00:00:00:00:00:02',
450                         'local_mac': '00:00:00:00:00:04',
451                         'dst_ip': '152.16.100.19',
452                         'driver': 'i40e',
453                         'vpci': '0000:05:00.0',
454                         'dpdk_port_num': 0,
455                     },
456                     'xe1': {
457                         'local_iface_name': 'ens785f1',
458                         'netmask': '255.255.255.0',
459                         'local_ip': '152.16.100.21',
460                         'local_mac': '00:00:00:00:00:01',
461                         'driver': 'i40e',
462                         'vpci': '0000:05:00.1',
463                         'dpdk_port_num': 1,
464                     },
465                 },
466                 'password': 'r00t',
467                 'VNF model': 'tg_rfc2544_tpl.yaml',
468                 'user': 'root',
469             },
470             'vnf__1': {
471                 'name': 'vnf.yardstick',
472                 'vnfd-id-ref': 'vnf__1',
473                 'ip': '1.2.1.1',
474                 'interfaces': {
475                     'xe0': {
476                         'local_iface_name': 'ens786f0',
477                         'vld_id': VpeApproxVnf.UPLINK,
478                         'netmask': '255.255.255.0',
479                         'local_ip': '152.16.100.19',
480                         'dst_mac': '00:00:00:00:00:04',
481                         'local_mac': '00:00:00:00:00:02',
482                         'dst_ip': '152.16.100.20',
483                         'driver': 'i40e',
484                         'vpci': '0000:05:00.0',
485                         'dpdk_port_num': 0,
486                     },
487                     'xe1': {
488                         'local_iface_name': 'ens786f1',
489                         'vld_id': VpeApproxVnf.DOWNLINK,
490                         'netmask': '255.255.255.0',
491                         'local_ip': '152.16.40.19',
492                         'dst_mac': '00:00:00:00:00:03',
493                         'local_mac': '00:00:00:00:00:01',
494                         'dst_ip': '152.16.40.20',
495                         'driver': 'i40e',
496                         'vpci': '0000:05:00.1',
497                         'dpdk_port_num': 1,
498                     },
499                 },
500                 'routing_table': [
501                     {
502                         'netmask': '255.255.255.0',
503                         'gateway': '152.16.100.20',
504                         'network': '152.16.100.20',
505                         'if': 'xe0',
506                     },
507                     {
508                         'netmask': '255.255.255.0',
509                         'gateway': '152.16.40.20',
510                         'network': '152.16.40.20',
511                         'if': 'xe1',
512                     },
513                 ],
514                 'member-vnf-index': '2',
515                 'host': '1.2.1.1',
516                 'role': 'vnf',
517                 'user': 'root',
518                 'nd_route_tbl': [
519                     {
520                         'netmask': '112',
521                         'gateway': '0064:ff9b:0:0:0:0:9810:6414',
522                         'network': '0064:ff9b:0:0:0:0:9810:6414',
523                         'if': 'xe0',
524                     },
525                     {
526                         'netmask': '112',
527                         'gateway': '0064:ff9b:0:0:0:0:9810:2814',
528                         'network': '0064:ff9b:0:0:0:0:9810:2814',
529                         'if': 'xe1',
530                     },
531                 ],
532                 'password': 'r00t',
533                 'VNF model': 'vpe_vnf.yaml',
534             },
535         },
536     }
537
538     def setUp(self):
539         self._mock_time_sleep = mock.patch.object(time, 'sleep')
540         self.mock_time_sleep = self._mock_time_sleep.start()
541         self.addCleanup(self._stop_mocks)
542
543     def _stop_mocks(self):
544         self._mock_time_sleep.stop()
545
546     def test___init__(self):
547         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
548         self.assertIsNone(vpe_approx_vnf._vnf_process)
549
550     @mock.patch(SSH_HELPER)
551     def test_collect_kpi_sa_not_running(self, ssh):
552         mock_ssh(ssh)
553
554         resource = mock.Mock(autospec=ResourceProfile)
555         resource.check_if_system_agent_running.return_value = 1, ''
556         resource.amqp_collect_nfvi_kpi.return_value = {'foo': 234}
557         resource.check_if_system_agent_running.return_value = (1, None)
558
559         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
560         vpe_approx_vnf.q_in = mock.MagicMock()
561         vpe_approx_vnf.q_out = mock.MagicMock()
562         vpe_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
563         vpe_approx_vnf.resource_helper.resource = resource
564
565         expected = {
566             'pkt_in_down_stream': 0,
567             'pkt_in_up_stream': 0,
568             'pkt_drop_down_stream': 0,
569             'pkt_drop_up_stream': 0,
570             'collect_stats': {'core': {}},
571         }
572         self.assertEqual(vpe_approx_vnf.collect_kpi(), expected)
573
574     @mock.patch(SSH_HELPER)
575     def test_collect_kpi_sa_running(self, ssh):
576         mock_ssh(ssh)
577
578         resource = mock.Mock(autospec=ResourceProfile)
579         resource.check_if_system_agent_running.return_value = 0, '1234'
580         resource.amqp_collect_nfvi_kpi.return_value = {'foo': 234}
581
582         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
583         vpe_approx_vnf.q_in = mock.MagicMock()
584         vpe_approx_vnf.q_out = mock.MagicMock()
585         vpe_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
586         vpe_approx_vnf.resource_helper.resource = resource
587
588         expected = {
589             'pkt_in_down_stream': 0,
590             'pkt_in_up_stream': 0,
591             'pkt_drop_down_stream': 0,
592             'pkt_drop_up_stream': 0,
593             'collect_stats': {'core': {'foo': 234}},
594         }
595         self.assertEqual(vpe_approx_vnf.collect_kpi(), expected)
596
597     @mock.patch(SSH_HELPER)
598     def test_vnf_execute(self, ssh):
599         mock_ssh(ssh)
600         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
601         vpe_approx_vnf.q_in = mock.MagicMock()
602         vpe_approx_vnf.q_out = mock.MagicMock()
603         vpe_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
604         self.assertEqual(vpe_approx_vnf.vnf_execute("quit", 0), '')
605
606     @mock.patch(SSH_HELPER)
607     def test_run_vpe(self, ssh):
608         mock_ssh(ssh)
609
610         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
611         vpe_approx_vnf.tc_file_name = get_file_abspath(TEST_FILE_YAML)
612         vpe_approx_vnf.vnf_cfg = {
613             'lb_config': 'SW',
614             'lb_count': 1,
615             'worker_config': '1C/1T',
616             'worker_threads': 1,
617         }
618         vpe_approx_vnf.scenario_helper.scenario_cfg = {
619             'options': {
620                 NAME: {
621                     'traffic_type': '4',
622                     'topology': 'nsb_test_case.yaml',
623                     'vnf_config': 'vpe_config',
624                 }
625             }
626         }
627         vpe_approx_vnf.topology = "nsb_test_case.yaml"
628         vpe_approx_vnf.nfvi_type = "baremetal"
629         vpe_approx_vnf._provide_config_file = mock.Mock()
630         vpe_approx_vnf._build_config = mock.MagicMock()
631
632         self.assertIsInstance(vpe_approx_vnf.ssh_helper, mock.Mock)
633         self.assertIsInstance(vpe_approx_vnf.ssh_helper, mock.Mock)
634         self.assertIsNone(vpe_approx_vnf._run())
635
636     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.MultiPortConfig")
637     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Context")
638     @mock.patch("yardstick.network_services.vnf_generic.vnf.vpe_vnf.ConfigCreate")
639     @mock.patch("six.moves.builtins.open")
640     @mock.patch(SSH_HELPER)
641     def test_build_config(self, ssh, *args):
642         mock_ssh(ssh)
643         vpe_approx_vnf = VpeApproxSetupEnvHelper(mock.MagicMock(),
644                                                  mock.MagicMock(), mock.MagicMock())
645         vpe_approx_vnf.tc_file_name = get_file_abspath(TEST_FILE_YAML)
646         vpe_approx_vnf.generate_port_pairs = mock.Mock()
647         vpe_approx_vnf.vnf_cfg = {
648             'lb_config': 'SW',
649             'lb_count': 1,
650             'worker_config': '1C/1T',
651             'worker_threads': 1,
652         }
653         vpe_approx_vnf.scenario_helper.scenario_cfg = {
654             'options': {
655                 NAME: {
656                     'traffic_type': '4',
657                     'topology': 'nsb_test_case.yaml',
658                     'vnf_config': 'vpe_config',
659                 }
660             }
661         }
662         vpe_approx_vnf.topology = "nsb_test_case.yaml"
663         vpe_approx_vnf.nfvi_type = "baremetal"
664         vpe_approx_vnf._provide_config_file = mock.Mock()
665
666         vpe_approx_vnf.ssh_helper = mock.MagicMock()
667         vpe_approx_vnf.scenario_helper = mock.MagicMock()
668         vpe_approx_vnf.ssh_helper.bin_path = mock.Mock()
669         vpe_approx_vnf.ssh_helper.upload_config_file = mock.MagicMock()
670         self.assertIsNone(vpe_approx_vnf._build_vnf_ports())
671
672         vpe_approx_vnf.ssh_helper.provision_tool = mock.Mock(return_value='tool_path')
673         vpe_approx_vnf.ssh_helper.all_ports = mock.Mock()
674         vpe_approx_vnf.vnfd_helper.port_nums = mock.Mock(return_value=[0, 1])
675         vpe_approx_vnf.scenario_helper.vnf_cfg = {'lb_config': 'HW'}
676
677         expected = 'sudo tool_path -p 0x3 -f /tmp/vpe_config -s /tmp/vpe_script  --hwlb 3'
678         self.assertEqual(vpe_approx_vnf.build_config(), expected)
679
680     @mock.patch(SSH_HELPER)
681     def test_wait_for_instantiate(self, ssh):
682         mock_ssh(ssh)
683
684         mock_process = mock.Mock(autospec=Process)
685         mock_process.is_alive.return_value = True
686         mock_process.exitcode = 432
687
688         mock_q_out = mock.Mock(autospec=Queue)
689         mock_q_out.get.side_effect = iter(["pipeline>"])
690         mock_q_out.qsize.side_effect = range(1, -1, -1)
691
692         mock_resource = mock.MagicMock()
693
694         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
695         vpe_approx_vnf._vnf_process = mock_process
696         vpe_approx_vnf.q_out = mock_q_out
697         vpe_approx_vnf.queue_wrapper = mock.Mock(autospec=QueueFileWrapper)
698         vpe_approx_vnf.resource_helper.resource = mock_resource
699
700         vpe_approx_vnf.q_out.put("pipeline>")
701         self.assertEqual(vpe_approx_vnf.wait_for_instantiate(), 432)
702
703     @mock.patch(SSH_HELPER)
704     def test_wait_for_instantiate_fragmented(self, ssh):
705         mock_ssh(ssh)
706
707         mock_process = mock.Mock(autospec=Process)
708         mock_process.is_alive.return_value = True
709         mock_process.exitcode = 432
710
711         # test that fragmented pipeline prompt is recognized
712         mock_q_out = mock.Mock(autospec=Queue)
713         mock_q_out.get.side_effect = iter(["wow pipel", "ine>"])
714         mock_q_out.qsize.side_effect = range(2, -1, -1)
715
716         mock_resource = mock.MagicMock()
717
718         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
719         vpe_approx_vnf._vnf_process = mock_process
720         vpe_approx_vnf.q_out = mock_q_out
721         vpe_approx_vnf.queue_wrapper = mock.Mock(autospec=QueueFileWrapper)
722         vpe_approx_vnf.resource_helper.resource = mock_resource
723
724         self.assertEqual(vpe_approx_vnf.wait_for_instantiate(), 432)
725
726     @mock.patch(SSH_HELPER)
727     def test_wait_for_instantiate_crash(self, ssh):
728         mock_ssh(ssh, exec_result=(1, "", ""))
729
730         mock_process = mock.Mock(autospec=Process)
731         mock_process.is_alive.return_value = False
732         mock_process.exitcode = 432
733
734         mock_resource = mock.MagicMock()
735
736         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
737         vpe_approx_vnf._vnf_process = mock_process
738         vpe_approx_vnf.resource_helper.resource = mock_resource
739
740         with self.assertRaises(RuntimeError) as raised:
741             vpe_approx_vnf.wait_for_instantiate()
742
743         self.assertIn('VNF process died', str(raised.exception))
744
745     @mock.patch(SSH_HELPER)
746     def test_wait_for_instantiate_panic(self, ssh):
747         mock_ssh(ssh, exec_result=(1, "", ""))
748
749         mock_process = mock.Mock(autospec=Process)
750         mock_process.is_alive.return_value = True
751         mock_process.exitcode = 432
752
753         mock_resource = mock.MagicMock()
754
755         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
756         vpe_approx_vnf._vnf_process = mock_process
757         vpe_approx_vnf.resource_helper.resource = mock_resource
758
759         vpe_approx_vnf.q_out.put("PANIC")
760         with self.assertRaises(RuntimeError) as raised:
761             vpe_approx_vnf.wait_for_instantiate()
762
763         self.assertIn('Error starting', str(raised.exception))
764
765     @mock.patch(SSH_HELPER)
766     def test_wait_for_instantiate_panic_fragmented(self, ssh):
767         mock_ssh(ssh, exec_result=(1, "", ""))
768
769         mock_process = mock.Mock(autospec=Process)
770         mock_process.is_alive.return_value = True
771         mock_process.exitcode = 432
772
773         # test that fragmented PANIC is recognized
774         mock_q_out = mock.Mock(autospec=Queue)
775         mock_q_out.get.side_effect = iter(["omg PA", "NIC this is bad"])
776         mock_q_out.qsize.side_effect = range(2, -1, -1)
777
778         mock_resource = mock.MagicMock()
779
780         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
781         vpe_approx_vnf._vnf_process = mock_process
782         vpe_approx_vnf.q_out = mock_q_out
783         vpe_approx_vnf.resource_helper.resource = mock_resource
784
785         with self.assertRaises(RuntimeError) as raised:
786             vpe_approx_vnf.wait_for_instantiate()
787
788         self.assertIn('Error starting', str(raised.exception))
789
790     @mock.patch(SSH_HELPER)
791     def test_terminate(self, ssh):
792         mock_ssh(ssh)
793
794         vpe_approx_vnf = VpeApproxVnf(NAME, self.VNFD_0)
795         vpe_approx_vnf._vnf_process = mock.MagicMock()
796         vpe_approx_vnf._resource_collect_stop = mock.Mock()
797         vpe_approx_vnf.resource_helper = mock.MagicMock()
798
799         self.assertIsNone(vpe_approx_vnf.terminate())