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