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