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