edd0ff796ea7e94bf32df69dd4088957a5ad9dcb
[yardstick.git] / yardstick / tests / unit / network_services / vnf_generic / vnf / test_router_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 import unittest
17 import mock
18
19 from yardstick.tests import STL_MOCKS
20 from yardstick.tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
21 from yardstick.benchmark.contexts import base as ctx_base
22
23
24 STLClient = mock.MagicMock()
25 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
26 stl_patch.start()
27
28 if stl_patch:
29     from yardstick.network_services.vnf_generic.vnf.router_vnf import RouterVNF
30
31
32 TEST_FILE_YAML = 'nsb_test_case.yaml'
33 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
34
35
36 name = 'vnf__1'
37
38
39 class TestRouterVNF(unittest.TestCase):
40     VNFD = {'vnfd:vnfd-catalog':
41             {'vnfd':
42              [{'short-name': 'RouterVNF',
43                'vdu':
44                [{'routing_table': [],
45                  'description': 'RouterVNF',
46                  'name': 'router-baremetal',
47                  'nd_route_tbl': [],
48                  'id': 'router-baremetal',
49                  'external-interface':
50                  [{'virtual-interface':
51                    {'dst_mac': '00:00:00:00:00:04',
52                     'vpci': '0000:05:00.0',
53                     'local_ip': '152.16.100.19',
54                     'type': 'PCI-PASSTHROUGH',
55                     'netmask': '255.255.255.0',
56                     'dpdk_port_num': 0,
57                     'bandwidth': '10 Gbps',
58                     'driver': "i40e",
59                     'dst_ip': '152.16.100.20',
60                     'local_iface_name': 'xe0',
61                     'local_mac': '00:00:00:00:00:02'},
62                    'vnfd-connection-point-ref': 'xe0',
63                    'name': 'xe0'},
64                   {'virtual-interface':
65                    {'dst_mac': '00:00:00:00:00:03',
66                     'vpci': '0000:05:00.1',
67                     'local_ip': '152.16.40.19',
68                     'type': 'PCI-PASSTHROUGH',
69                     'driver': "i40e",
70                     'netmask': '255.255.255.0',
71                     'dpdk_port_num': 1,
72                     'bandwidth': '10 Gbps',
73                     'dst_ip': '152.16.40.20',
74                     'local_iface_name': 'xe1',
75                     'local_mac': '00:00:00:00:00:01'},
76                    'vnfd-connection-point-ref': 'xe1',
77                    'name': 'xe1'}]}],
78                'description': 'RouterVNF',
79                'mgmt-interface':
80                    {'vdu-id': 'router-baremetal',
81                     'host': '1.2.1.1',
82                     'password': 'r00t',
83                     'user': 'root',
84                     'ip': '1.2.1.1'},
85                'benchmark':
86                    {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
87                'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
88                                     {'type': 'VPORT', 'name': 'xe1'}],
89                'id': 'RouterVNF', 'name': 'VPEVnfSsh'}]}}
90
91     scenario_cfg = {'nodes': {'cpt__0': 'compute_0.compute_nodes',
92                               'tg__0': 'trafficgen_1.baremetal',
93                               'vnf__0': 'vnf.yardstick'},
94                     'options': {'flow': {'count': 128000,
95                                          'dst_ip': ['10.0.3.26-10.0.3.105'],
96                                          'dst_port': ['2001-2004'],
97                                          'src_ip': ['10.0.2.26-10.0.2.105'],
98                                          'src_port': ['1234-1238']},
99                                 'framesize': {'downlink': {'1024B': 100},
100                                               'uplink': {'1024B': 100}},
101                                 'rfc2544': {'allowed_drop_rate': '0.0001 - 0.1'},
102                                 'tg__0': {'queues_per_port': 7},
103                                 'traffic_type': 4,
104                                 'vnf__0': {'nfvi_enable': True}},
105                     'runner': {'interval': 35,
106                                'iterations': 10,
107                                'type': 'Iteration'},
108                     'topology': 'router-tg-topology.yaml',
109                     'traffic_profile': '../../traffic_profiles/ipv4_throughput.yaml',
110                     'type': 'NSPerf'}
111
112     context_cfg = {'nodes': {'tg__1':
113                              {'member-vnf-index': '1',
114                               'role': 'TrafficGen',
115                               'name': 'trafficgen_1.yardstick',
116                               'vnfd-id-ref': 'tg__1',
117                               'ip': '1.2.1.1',
118                               'interfaces':
119                               {'xe0': {'local_iface_name': 'ens785f0',
120                                        'vld_id': RouterVNF.UPLINK,
121                                        'netmask': '255.255.255.0',
122                                        'local_ip': '152.16.100.20',
123                                        'dst_mac': '00:00:00:00:00:02',
124                                        'local_mac': '00:00:00:00:00:04',
125                                        'dst_ip': '152.16.100.19',
126                                        'driver': 'i40e',
127                                        'vpci': '0000:05:00.0',
128                                        'dpdk_port_num': 0},
129                                'xe1': {'local_iface_name': 'ens785f1',
130                                        'netmask': '255.255.255.0',
131                                        'local_ip': '152.16.100.21',
132                                        'local_mac': '00:00:00:00:00:01',
133                                        'driver': 'i40e',
134                                        'vpci': '0000:05:00.1',
135                                        'dpdk_port_num': 1}},
136                               'password': 'r00t',
137                               'VNF model': 'tg_rfc2544_tpl.yaml',
138                               'user': 'root'},
139                              'vnf__1':
140                              {'name': 'vnf.yardstick',
141                               'vnfd-id-ref': 'vnf__1',
142                               'ip': '1.2.1.1',
143                               'interfaces':
144                               {'xe0': {'local_iface_name': 'ens786f0',
145                                        'vld_id': RouterVNF.UPLINK,
146                                        'netmask': '255.255.255.0',
147                                        'local_ip': '152.16.100.19',
148                                        'dst_mac': '00:00:00:00:00:04',
149                                        'local_mac': '00:00:00:00:00:02',
150                                        'dst_ip': '152.16.100.20',
151                                        'driver': 'i40e',
152                                        'vpci': '0000:05:00.0',
153                                        'dpdk_port_num': 0},
154                                'xe1': {'local_iface_name': 'ens786f1',
155                                        'vld_id': RouterVNF.DOWNLINK,
156                                        'netmask': '255.255.255.0',
157                                        'local_ip': '152.16.40.19',
158                                        'dst_mac': '00:00:00:00:00:03',
159                                        'local_mac': '00:00:00:00:00:01',
160                                        'dst_ip': '152.16.40.20',
161                                        'driver': 'i40e',
162                                        'vpci': '0000:05:00.1',
163                                        'dpdk_port_num': 1}},
164                               'routing_table': [],
165                               'member-vnf-index': '2',
166                               'host': '1.2.1.1',
167                               'role': 'vnf',
168                               'user': 'root',
169                               'nd_route_tbl': [],
170                               'password': 'r00t',
171                               'VNF model': 'router_vnf.yaml'}}}
172
173     IP_SHOW_STATS_OUTPUT = """\
174 2: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
175     link/ether d4:c9:ef:52:7c:4d brd ff:ff:ff:ff:ff:ff
176     RX: bytes  packets  errors  dropped overrun mcast
177     2781945429 3202213  0       0       0       30131
178     RX errors: length  crc     frame   fifo    missed
179                0        0       0       0       0
180     TX: bytes  packets  errors  dropped carrier collsns
181     646221183  2145799  0       0       0       0
182     TX errors: aborted fifo    window  heartbeat
183                0        0       0       0
184 """
185     STATS = {
186         'RX:bytes': '2781945429',
187         'RX:dropped': '0',
188         'RX:errors': '0',
189         'RX:mcast': '30131',
190         'RX:overrun': '0',
191         'RX:packets': '3202213',
192         'RX errors:length': '0',
193         'RX errors:crc': '0',
194         'RX errors:frame': '0',
195         'RX errors:fifo': '0',
196         'RX errors:missed': '0',
197         'TX:bytes': '646221183',
198         'TX:carrier': '0',
199         'TX:collsns': '0',
200         'TX:dropped': '0',
201         'TX:errors': '0',
202         'TX:packets': '2145799',
203         'TX errors:aborted': '0',
204         'TX errors:fifo': '0',
205         'TX errors:window': '0',
206         'TX errors:heartbeat': '0',
207     }
208
209     def test___init__(self):
210         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
211         router_vnf = RouterVNF(name, vnfd)
212         self.assertIsNone(router_vnf._vnf_process)
213
214     def test_get_stats(self):
215         stats = RouterVNF.get_stats(self.IP_SHOW_STATS_OUTPUT)
216         self.assertDictEqual(stats, self.STATS)
217
218     @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server', return_value='mock_node')
219     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
220     @mock.patch(SSH_HELPER)
221     def test_collect_kpi(self, ssh, *args):
222         m = mock_ssh(ssh)
223
224         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
225         router_vnf = RouterVNF(name, vnfd)
226         router_vnf.scenario_helper.scenario_cfg = {
227             'nodes': {router_vnf.name: "mock"}
228         }
229         router_vnf.ssh_helper = m
230         result = {
231             'physical_node': 'mock_node',
232             'packets_dropped': 0,
233             'packets_fwd': 0,
234             'packets_in': 0,
235             'link_stats': {}
236         }
237         self.assertEqual(result, router_vnf.collect_kpi())
238
239     @mock.patch(SSH_HELPER)
240     def test_run_router(self, ssh):
241         mock_ssh(ssh)
242
243         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
244         router_vnf = RouterVNF(name, vnfd)
245         router_vnf.scenario_helper.scenario_cfg = self.scenario_cfg
246         router_vnf._run()
247         router_vnf.ssh_helper.drop_connection.assert_called_once()
248
249     @mock.patch.object(ctx_base, 'Context')
250     @mock.patch(SSH_HELPER)
251     def test_instantiate(self, ssh, *args):
252         mock_ssh(ssh)
253
254         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
255         router_vnf = RouterVNF(name, vnfd)
256         router_vnf.WAIT_TIME = 0
257         router_vnf.INTERFACE_WAIT = 0
258         self.scenario_cfg.update({"nodes": {"vnf__1": ""}})
259         self.assertIsNone(router_vnf.instantiate(self.scenario_cfg,
260                                                  self.context_cfg))
261
262     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
263     @mock.patch(SSH_HELPER)
264     def test_terminate(self, ssh, _):
265         mock_ssh(ssh)
266
267         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
268         router_vnf = RouterVNF(name, vnfd)
269         router_vnf._vnf_process = mock.MagicMock()
270         router_vnf._vnf_process.terminate = mock.Mock()
271         self.assertIsNone(router_vnf.terminate())