3 # Copyright (c) 2016-2017 Intel Corporation
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 # Unittest for yardstick.network_services.vnf_generic.vnfdgen
20 from __future__ import absolute_import
22 from six.moves import range
24 from yardstick.common.yaml_loader import yaml_load
25 from yardstick.network_services.vnf_generic import vnfdgen
27 TREX_VNFD_TEMPLATE = """
30 - id: TrexTrafficGen # ISB class mapping
33 description: TRex stateless traffic generator for RFC2544
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
45 - id: trexgen-baremetal
46 name: trexgen-baremetal
47 description: TRex stateless traffic generator for RFC2544
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 }}'
58 vnfd-connection-point-ref: xe0
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 }}'
68 vnfd-connection-point-ref: xe1
69 routing_table: {{ routing_table }}
70 nd_route_tbl: {{ nd_route_tbl }}
78 - tx_throughput_pc_linerate
79 - rx_throughput_pc_linerate
85 COMPLETE_TREX_VNFD = \
94 'tx_throughput_pc_linerate',
95 'rx_throughput_pc_linerate',
99 'connection-point': [{'name': 'xe0',
103 'description': 'TRex stateless traffic generator for RFC2544',
104 'id': 'TrexTrafficGen',
105 'mgmt-interface': {'ip': '1.1.1.1',
108 'vdu-id': 'trexgen-baremetal'},
110 'short-name': 'trexgen',
111 'vdu': [{'description': 'TRex stateless traffic generator for RFC2544',
112 'external-interface':
114 'virtual-interface': {'bandwidth': '10 Gbps',
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'},
123 'virtual-interface': {'bandwidth': '10 Gbps',
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',
135 'network': '0064:ff9b:0:0:0:0:9810:6414'},
136 {'gateway': '0064:ff9b:0:0:0:0:9810:2814',
139 'network': '0064:ff9b:0:0:0:0:9810:2814'}],
140 'routing_table': [{'gateway': '152.16.100.20',
142 'netmask': '255.255.255.0',
143 'network': '152.16.100.20'},
144 {'gateway': '152.16.40.20',
146 'netmask': '255.255.255.0',
147 'network': '152.16.40.20'}],
148 'name': 'trexgen-baremetal'}]}]}}
150 NODE_CFG = {'ip': '1.1.1.1',
153 'role': 'TrafficGen',
155 'interfaces': {'xe0': {'dpdk_port_num': 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,
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',
170 u'network': u'0064:ff9b:0:0:0:0:9810:6414'},
171 {u'gateway': u'0064:ff9b:0:0:0:0:9810:2814',
174 u'network': u'0064:ff9b:0:0:0:0:9810:2814'}],
175 'routing_table': [{u'gateway': u'152.16.100.20',
177 u'netmask': u'255.255.255.0',
178 u'network': u'152.16.100.20'},
179 {u'gateway': u'152.16.40.20',
181 u'netmask': u'255.255.255.0',
182 u'network': u'152.16.40.20'}],
186 TRAFFIC_PROFILE_TPL = """
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) }}"
201 "private": [{"ipv4": {"outer_l2":
202 {"framesize": {"64B": '10', "128B": '10',
203 "256B": '10', "373B": '10',
204 "570B": '10', "1400B": '10',
208 class TestRender(unittest.TestCase):
210 def test_render_none(self):
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)
216 def test_render_unicode_dict(self):
218 tmpl = "{{ routing_table }}"
219 self.assertEqual(yaml_load(vnfdgen.render(tmpl, **NODE_CFG)), NODE_CFG["routing_table"])
222 class TestVnfdGen(unittest.TestCase):
223 """ Class to verify VNFS testcases """
225 def test_generate_vnfd(self):
226 """ Function to verify vnfd generation based on template """
228 generated_vnfd = vnfdgen.generate_vnfd(TREX_VNFD_TEMPLATE, NODE_CFG)
229 self.assertDictEqual(COMPLETE_TREX_VNFD, generated_vnfd)
231 def test_generate_tp_no_vars(self):
232 """ Function to verify traffic profile generation without imix """
235 generated_tp = vnfdgen.generate_vnfd(TRAFFIC_PROFILE_TPL, {"imix": {}})
236 self.assertDictEqual(TRAFFIC_PROFILE, generated_tp)
238 def test_deepgetitem(self):
240 self.assertEqual(vnfdgen.deepgetitem(d, "a"), 1)
242 def test_dict_flatten_int(self):
244 self.assertEqual(vnfdgen.deepgetitem(d, "a"), 1)
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)
250 def test_dict_flatten_int_key_fallback(self):
252 self.assertEqual(vnfdgen.deepgetitem(d, "0"), 1)
254 def test_dict_flatten_list(self):
255 d = {'a': 1, 'b': list(range(2))}
256 self.assertEqual(vnfdgen.deepgetitem(d, "b.0"), 0)
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)
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)
267 def test_generate_tp_single_var(self):
268 """ Function to verify traffic profile generation with imix """
271 vnfdgen.generate_vnfd(TRAFFIC_PROFILE_TPL,
272 {"imix": {"private": {"imix_small": '20'}}})
274 tp2 = dict(TRAFFIC_PROFILE)
275 tp2["private"][0]["ipv4"]["outer_l2"]["framesize"]["64B"] = '20'
276 self.assertDictEqual(tp2, generated_tp)