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