Merge "Introducing Generic framework to do pre-deployment VNF & Network service testing"
[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.network_services.vnf_generic import vnfdgen
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
69         benchmark:
70             kpi:
71                 - rx_throughput_fps
72                 - tx_throughput_fps
73                 - tx_throughput_mbps
74                 - rx_throughput_mbps
75                 - tx_throughput_pc_linerate
76                 - rx_throughput_pc_linerate
77                 - min_latency
78                 - max_latency
79                 - avg_latency
80 """
81
82 COMPLETE_TREX_VNFD = \
83     {'vnfd:vnfd-catalog':
84      {'vnfd':
85       [{'benchmark':
86         {'kpi':
87          ['rx_throughput_fps',
88           'tx_throughput_fps',
89           'tx_throughput_mbps',
90           'rx_throughput_mbps',
91           'tx_throughput_pc_linerate',
92           'rx_throughput_pc_linerate',
93           'min_latency',
94           'max_latency',
95           'avg_latency']},
96         'connection-point': [{'name': 'xe0',
97                               'type': 'VPORT'},
98                              {'name': 'xe1',
99                               'type': 'VPORT'}],
100         'description': 'TRex stateless traffic generator for RFC2544',
101         'id': 'TrexTrafficGen',
102         'mgmt-interface': {'ip': '1.1.1.1',
103                            'password': 'berta',
104                            'user': 'berta',
105                            'vdu-id': 'trexgen-baremetal'},
106         'name': 'trexgen',
107         'short-name': 'trexgen',
108         'vdu': [{'description': 'TRex stateless traffic generator for RFC2544',
109                  'external-interface':
110                  [{'name': 'xe0',
111                    'virtual-interface': {'bandwidth': '10 Gbps',
112                                          'dst_ip': '1.1.1.1',
113                                          'dst_mac': '00:01:02:03:04:05',
114                                          'local_ip': '1.1.1.2',
115                                          'local_mac': '00:01:02:03:05:05',
116                                          'type': 'PCI-PASSTHROUGH',
117                                          'vpci': '0000:00:10.2'},
118                    'vnfd-connection-point-ref': 'xe0'},
119                   {'name': 'xe1',
120                    'virtual-interface': {'bandwidth': '10 Gbps',
121                                          'dst_ip': '2.1.1.1',
122                                          'dst_mac': '00:01:02:03:04:06',
123                                          'local_ip': '2.1.1.2',
124                                          'local_mac': '00:01:02:03:05:06',
125                                          'type': 'PCI-PASSTHROUGH',
126                                          'vpci': '0000:00:10.1'},
127                    'vnfd-connection-point-ref': 'xe1'}],
128                  'id': 'trexgen-baremetal',
129                  'name': 'trexgen-baremetal'}]}]}}
130
131 NODE_CFG = {'ip': '1.1.1.1',
132             'name': 'demeter',
133             'password': 'berta',
134             'role': 'TrafficGen',
135             'user': 'berta',
136             'interfaces': {'xe0': {'dpdk_port_num': 1,
137                                    'dst_ip': '1.1.1.1',
138                                    'dst_mac': '00:01:02:03:04:05',
139                                    'local_ip': '1.1.1.2',
140                                    'local_mac': '00:01:02:03:05:05',
141                                    'vpci': '0000:00:10.2'},
142                            'xe1': {'dpdk_port_num': 0,
143                                    'dst_ip': '2.1.1.1',
144                                    'dst_mac': '00:01:02:03:04:06',
145                                    'local_ip': '2.1.1.2',
146                                    'local_mac': '00:01:02:03:05:06',
147                                    'vpci': '0000:00:10.1'}}}
148
149
150 TRAFFIC_PROFILE_TPL = """
151 private:
152     - ipv4:
153         outer_l2:
154             framesize:
155                 64B: "{{ get(imix, 'private.imix_small', 10) }}"
156                 128B: "{{ get(imix, 'private.imix_128B', 10) }}"
157                 256B: "{{ get(imix, 'private.imix_256B', 10) }}"
158                 373B: "{{ get(imix, 'private.imix_373B', 10) }}"
159                 570B: "{{get(imix, 'private.imix_570B', 10) }}"
160                 1400B: "{{get(imix, 'private.imix_1400B', 10) }}"
161                 1518B: "{{get(imix, 'private.imix_1500B', 40) }}"
162 """
163
164 TRAFFIC_PROFILE = {
165     "private": [{"ipv4": {"outer_l2":
166                           {"framesize": {"64B": '10', "128B": '10',
167                                          "256B": '10', "373B": '10',
168                                          "570B": '10', "1400B": '10',
169                                          "1518B": '40'}}}}]}
170
171
172 class TestVnfdGen(unittest.TestCase):
173     """ Class to verify VNFS testcases """
174
175     def test_generate_vnfd(self):
176         """ Function to verify vnfd generation based on template """
177         self.maxDiff = None
178         generated_vnfd = vnfdgen.generate_vnfd(TREX_VNFD_TEMPLATE, NODE_CFG)
179         self.assertDictEqual(COMPLETE_TREX_VNFD, generated_vnfd)
180
181     def test_generate_tp_no_vars(self):
182         """ Function to verify traffic profile generation without imix """
183
184         self.maxDiff = None
185         generated_tp = vnfdgen.generate_vnfd(TRAFFIC_PROFILE_TPL, {"imix": {}})
186         self.assertDictEqual(TRAFFIC_PROFILE, generated_tp)
187
188     def test_dict_flatten_empty_dict(self):
189         self.assertEqual(vnfdgen.dict_key_flatten({}), {})
190
191     def test_dict_flatten_int(self):
192         d = {'a': 1, 'b': 2}
193         self.assertEqual(vnfdgen.dict_key_flatten(d), d)
194
195     def test_dict_flatten_str(self):
196         d = {'a': "1", 'b': '2'}
197         self.assertEqual(vnfdgen.dict_key_flatten(d), d)
198
199     def test_dict_flatten_list(self):
200         d = {'a': 1, 'b': list(range(2))}
201         self.assertEqual(vnfdgen.dict_key_flatten(d),
202                          {'a': 1, 'b0': 0, 'b1': 1})
203
204     def test_dict_flatten_dict(self):
205         d = {'a': 1, 'b': {x: x for x in list(range(2))}}
206         self.assertEqual(vnfdgen.dict_key_flatten(d),
207                          {'a': 1, 'b.0': 0, 'b.1': 1})
208
209     def test_generate_tp_single_var(self):
210         """ Function to verify traffic profile generation with imix """
211
212         generated_tp = \
213             vnfdgen.generate_vnfd(TRAFFIC_PROFILE_TPL,
214                                   {"imix": {"private": {"imix_small": '20'}}})
215         self.maxDiff = None
216         tp2 = dict(TRAFFIC_PROFILE)
217         tp2["private"][0]["ipv4"]["outer_l2"]["framesize"]["64B"] = '20'
218         self.assertDictEqual(tp2, generated_tp)