Merge "Setup hugepages on SA host(sriov, ovs_dpdk)"
[yardstick.git] / yardstick / tests / unit / network_services / vnf_generic / test_vnfdgen.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 six.moves import range
17 import unittest
18
19 from yardstick.common.yaml_loader import yaml_load
20 from yardstick.network_services.vnf_generic import vnfdgen
21
22
23 UPLINK = "uplink"
24 DOWNLINK = "downlink"
25
26 TREX_VNFD_TEMPLATE = """
27 vnfd:vnfd-catalog:
28     vnfd:
29     -   id: TrexTrafficGen  # ISB class mapping
30         name: trexgen
31         short-name: trexgen
32         description: TRex stateless traffic generator for RFC2544
33         mgmt-interface:
34             vdu-id: trexgen-baremetal
35             user: {{user}}  # Value filled by vnfdgen
36             password: {{password}}  # Value filled by vnfdgen
37             ip: {{ip}}  # Value filled by vnfdgen
38         connection-point:
39         -   name: xe0
40             type: VPORT
41         -   name: xe1
42             type: VPORT
43         vdu:
44         -   id: trexgen-baremetal
45             name: trexgen-baremetal
46             description: TRex stateless traffic generator for RFC2544
47             external-interface:
48             -   name: xe0
49                 virtual-interface:
50                     type: PCI-PASSTHROUGH
51                     vpci: '{{ interfaces.xe0.vpci}}'
52                     local_ip: '{{ interfaces.xe0.local_ip }}'
53                     dst_ip: '{{ interfaces.xe0.dst_ip }}'
54                     local_mac: '{{ interfaces.xe0.local_mac }}'
55                     dst_mac: '{{ interfaces.xe0.dst_mac }}'
56                     bandwidth: 10 Gbps
57                 vnfd-connection-point-ref: xe0
58             -   name: xe1
59                 virtual-interface:
60                     type: PCI-PASSTHROUGH
61                     vpci: '{{ interfaces.xe1.vpci }}'
62                     local_ip: '{{ interfaces.xe1.local_ip }}'
63                     dst_ip: '{{ interfaces.xe1.dst_ip }}'
64                     local_mac: '{{ interfaces.xe1.local_mac }}'
65                     dst_mac: '{{ interfaces.xe1.dst_mac }}'
66                     bandwidth: 10 Gbps
67                 vnfd-connection-point-ref: xe1
68             routing_table: {{ routing_table }}
69             nd_route_tbl: {{ nd_route_tbl }}
70
71         benchmark:
72             kpi:
73                 - rx_throughput_fps
74                 - tx_throughput_fps
75                 - tx_throughput_mbps
76                 - rx_throughput_mbps
77                 - tx_throughput_pc_linerate
78                 - rx_throughput_pc_linerate
79                 - min_latency
80                 - max_latency
81                 - avg_latency
82 """
83
84 COMPLETE_TREX_VNFD = \
85     {'vnfd:vnfd-catalog':
86      {'vnfd':
87       [{'benchmark':
88         {'kpi':
89          ['rx_throughput_fps',
90           'tx_throughput_fps',
91           'tx_throughput_mbps',
92           'rx_throughput_mbps',
93           'tx_throughput_pc_linerate',
94           'rx_throughput_pc_linerate',
95           'min_latency',
96           'max_latency',
97           'avg_latency']},
98         'connection-point': [{'name': 'xe0',
99                               'type': 'VPORT'},
100                              {'name': 'xe1',
101                               'type': 'VPORT'}],
102         'description': 'TRex stateless traffic generator for RFC2544',
103         'id': 'TrexTrafficGen',
104         'mgmt-interface': {'ip': '1.1.1.1',
105                            'password': 'berta',
106                            'user': 'berta',
107                            'vdu-id': 'trexgen-baremetal'},
108         'name': 'trexgen',
109         'short-name': 'trexgen',
110         'vdu': [{'description': 'TRex stateless traffic generator for RFC2544',
111                  'external-interface':
112                  [{'name': 'xe0',
113                    'virtual-interface': {'bandwidth': '10 Gbps',
114                                          'dst_ip': '1.1.1.1',
115                                          'dst_mac': '00:01:02:03:04:05',
116                                          'local_ip': '1.1.1.2',
117                                          'local_mac': '00:01:02:03:05:05',
118                                          'type': 'PCI-PASSTHROUGH',
119                                          'vpci': '0000:00:10.2'},
120                    'vnfd-connection-point-ref': 'xe0'},
121                   {'name': 'xe1',
122                    'virtual-interface': {'bandwidth': '10 Gbps',
123                                          'dst_ip': '2.1.1.1',
124                                          'dst_mac': '00:01:02:03:04:06',
125                                          'local_ip': '2.1.1.2',
126                                          'local_mac': '00:01:02:03:05:06',
127                                          'type': 'PCI-PASSTHROUGH',
128                                          'vpci': '0000:00:10.1'},
129                    'vnfd-connection-point-ref': 'xe1'}],
130                  'id': 'trexgen-baremetal',
131                  'nd_route_tbl': [{'gateway': '0064:ff9b:0:0:0:0:9810:6414',
132                                    'if': 'xe0',
133                                    'netmask': '112',
134                                    'network': '0064:ff9b:0:0:0:0:9810:6414'},
135                                   {'gateway': '0064:ff9b:0:0:0:0:9810:2814',
136                                    'if': 'xe1',
137                                    'netmask': '112',
138                                    'network': '0064:ff9b:0:0:0:0:9810:2814'}],
139                  'routing_table': [{'gateway': '152.16.100.20',
140                                     'if': 'xe0',
141                                     'netmask': '255.255.255.0',
142                                     'network': '152.16.100.20'},
143                                    {'gateway': '152.16.40.20',
144                                     'if': 'xe1',
145                                     'netmask': '255.255.255.0',
146                                     'network': '152.16.40.20'}],
147                  'name': 'trexgen-baremetal'}]}]}}
148
149 NODE_CFG = {'ip': '1.1.1.1',
150             'name': 'demeter',
151             'password': 'berta',
152             'role': 'TrafficGen',
153             'user': 'berta',
154             'interfaces': {'xe0': {'dpdk_port_num': 1,
155                                    'dst_ip': '1.1.1.1',
156                                    'dst_mac': '00:01:02:03:04:05',
157                                    'local_ip': '1.1.1.2',
158                                    'local_mac': '00:01:02:03:05:05',
159                                    'vpci': '0000:00:10.2'},
160                            'xe1': {'dpdk_port_num': 0,
161                                    'dst_ip': '2.1.1.1',
162                                    'dst_mac': '00:01:02:03:04:06',
163                                    'local_ip': '2.1.1.2',
164                                    'local_mac': '00:01:02:03:05:06',
165                                    'vpci': '0000:00:10.1'}},
166             'nd_route_tbl': [{u'gateway': u'0064:ff9b:0:0:0:0:9810:6414',
167                               u'if': u'xe0',
168                               u'netmask': u'112',
169                               u'network': u'0064:ff9b:0:0:0:0:9810:6414'},
170                              {u'gateway': u'0064:ff9b:0:0:0:0:9810:2814',
171                               u'if': u'xe1',
172                               u'netmask': u'112',
173                               u'network': u'0064:ff9b:0:0:0:0:9810:2814'}],
174             'routing_table': [{u'gateway': u'152.16.100.20',
175                                u'if': u'xe0',
176                                u'netmask': u'255.255.255.0',
177                                u'network': u'152.16.100.20'},
178                               {u'gateway': u'152.16.40.20',
179                                u'if': u'xe1',
180                                u'netmask': u'255.255.255.0',
181                                u'network': u'152.16.40.20'}],
182             }
183
184
185 # need to template, but can't use {} so use %s
186 TRAFFIC_PROFILE_TPL = """
187 %(0)s:
188     - ipv4:
189         outer_l2:
190             framesize:
191                 64B: "{{ get(imix, '%(0)s.imix_small', 10) }}"
192                 128B: "{{ get(imix, '%(0)s.imix_128B', 10) }}"
193                 256B: "{{ get(imix, '%(0)s.imix_256B', 10) }}"
194                 373B: "{{ get(imix, '%(0)s.imix_373B', 10) }}"
195                 570B: "{{get(imix, '%(0)s.imix_570B', 10) }}"
196                 1400B: "{{get(imix, '%(0)s.imix_1400B', 10) }}"
197                 1518B: "{{get(imix, '%(0)s.imix_1500B', 40) }}"
198 """ % {"0": UPLINK}
199
200 TRAFFIC_PROFILE = {
201     UPLINK: [{"ipv4": {"outer_l2":
202                        {"framesize": {"64B": '10', "128B": '10',
203                                       "256B": '10', "373B": '10',
204                                       "570B": '10', "1400B": '10',
205                                       "1518B": '40'}}}}]}
206
207
208 class TestRender(unittest.TestCase):
209
210     def test_render_none(self):
211
212         tmpl = "{{ routing_table }}"
213         self.assertEqual(vnfdgen.render(tmpl, routing_table=None), u'~')
214         self.assertIsNone(
215             yaml_load(vnfdgen.render(tmpl, routing_table=None)))
216
217     def test_render_unicode_dict(self):
218
219         tmpl = "{{ routing_table }}"
220         self.assertEqual(yaml_load(vnfdgen.render(
221             tmpl, **NODE_CFG)), NODE_CFG["routing_table"])
222
223
224 class TestVnfdGen(unittest.TestCase):
225     """ Class to verify VNFS testcases """
226
227     def test_generate_vnfd(self):
228         """ Function to verify vnfd generation based on template """
229         self.maxDiff = None
230         generated_vnfd = vnfdgen.generate_vnfd(TREX_VNFD_TEMPLATE, NODE_CFG)
231         self.assertDictEqual(COMPLETE_TREX_VNFD, generated_vnfd)
232
233     def test_generate_tp_no_vars(self):
234         """ Function to verify traffic profile generation without imix """
235
236         self.maxDiff = None
237         generated_tp = vnfdgen.generate_vnfd(TRAFFIC_PROFILE_TPL, {"imix": {}})
238         self.assertDictEqual(TRAFFIC_PROFILE, generated_tp)
239
240     def test_deepgetitem(self):
241         d = {'a': 1, 'b': 2}
242         self.assertEqual(vnfdgen.deepgetitem(d, "a"), 1)
243
244     def test_dict_flatten_int(self):
245         d = {'a': 1, 'b': 2}
246         self.assertEqual(vnfdgen.deepgetitem(d, "a"), 1)
247
248     def test_dict_flatten_str_int_key_first(self):
249         d = {'0': 1, 0: 24, 'b': 2}
250         self.assertEqual(vnfdgen.deepgetitem(d, "0"), 1)
251
252     def test_dict_flatten_int_key_fallback(self):
253         d = {0: 1, 'b': 2}
254         self.assertEqual(vnfdgen.deepgetitem(d, "0"), 1)
255
256     def test_dict_flatten_list(self):
257         d = {'a': 1, 'b': list(range(2))}
258         self.assertEqual(vnfdgen.deepgetitem(d, "b.0"), 0)
259
260     def test_dict_flatten_dict(self):
261         d = {'a': 1, 'b': {x: x for x in list(range(2))}}
262         self.assertEqual(vnfdgen.deepgetitem(d, "b.0"), 0)
263
264     def test_dict_flatten_only_str_key(self):
265         d = {'0': 1, 0: 24, 'b': 2}
266         self.assertRaises(AttributeError, vnfdgen.deepgetitem, d, 0)
267
268     def test_generate_tp_single_var(self):
269         """ Function to verify traffic profile generation with imix """
270
271         generated_tp = \
272             vnfdgen.generate_vnfd(TRAFFIC_PROFILE_TPL,
273                                   {"imix": {UPLINK: {"imix_small": '20'}}})
274         self.maxDiff = None
275         tp2 = dict(TRAFFIC_PROFILE)
276         tp2[UPLINK][0]["ipv4"]["outer_l2"]["framesize"]["64B"] = '20'
277         self.assertDictEqual(tp2, generated_tp)