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