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