1 # Copyright (c) 2016-2017 Intel Corporation
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from six.moves import range
19 from yardstick.common.yaml_loader import yaml_load
20 from yardstick.network_services.vnf_generic import vnfdgen
26 TREX_VNFD_TEMPLATE = """
29 - id: TrexTrafficGen # ISB class mapping
32 description: TRex stateless traffic generator for RFC2544
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
44 - id: trexgen-baremetal
45 name: trexgen-baremetal
46 description: TRex stateless traffic generator for RFC2544
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 }}'
57 vnfd-connection-point-ref: xe0
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 }}'
67 vnfd-connection-point-ref: xe1
68 routing_table: {{ routing_table }}
69 nd_route_tbl: {{ nd_route_tbl }}
77 - tx_throughput_pc_linerate
78 - rx_throughput_pc_linerate
84 COMPLETE_TREX_VNFD = \
93 'tx_throughput_pc_linerate',
94 'rx_throughput_pc_linerate',
98 'connection-point': [{'name': 'xe0',
102 'description': 'TRex stateless traffic generator for RFC2544',
103 'id': 'TrexTrafficGen',
104 'mgmt-interface': {'ip': '1.1.1.1',
107 'vdu-id': 'trexgen-baremetal'},
109 'short-name': 'trexgen',
110 'vdu': [{'description': 'TRex stateless traffic generator for RFC2544',
111 'external-interface':
113 'virtual-interface': {'bandwidth': '10 Gbps',
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'},
122 'virtual-interface': {'bandwidth': '10 Gbps',
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',
134 'network': '0064:ff9b:0:0:0:0:9810:6414'},
135 {'gateway': '0064:ff9b:0:0:0:0:9810:2814',
138 'network': '0064:ff9b:0:0:0:0:9810:2814'}],
139 'routing_table': [{'gateway': '152.16.100.20',
141 'netmask': '255.255.255.0',
142 'network': '152.16.100.20'},
143 {'gateway': '152.16.40.20',
145 'netmask': '255.255.255.0',
146 'network': '152.16.40.20'}],
147 'name': 'trexgen-baremetal'}]}]}}
149 NODE_CFG = {'ip': '1.1.1.1',
152 'role': 'TrafficGen',
154 'interfaces': {'xe0': {'dpdk_port_num': 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,
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',
169 u'network': u'0064:ff9b:0:0:0:0:9810:6414'},
170 {u'gateway': u'0064:ff9b:0:0:0:0:9810:2814',
173 u'network': u'0064:ff9b:0:0:0:0:9810:2814'}],
174 'routing_table': [{u'gateway': u'152.16.100.20',
176 u'netmask': u'255.255.255.0',
177 u'network': u'152.16.100.20'},
178 {u'gateway': u'152.16.40.20',
180 u'netmask': u'255.255.255.0',
181 u'network': u'152.16.40.20'}],
185 # need to template, but can't use {} so use %s
186 TRAFFIC_PROFILE_TPL = """
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) }}"
201 UPLINK: [{"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'~')
215 yaml_load(vnfdgen.render(tmpl, routing_table=None)))
217 def test_render_unicode_dict(self):
219 tmpl = "{{ routing_table }}"
220 self.assertEqual(yaml_load(vnfdgen.render(
221 tmpl, **NODE_CFG)), NODE_CFG["routing_table"])
224 class TestVnfdGen(unittest.TestCase):
225 """ Class to verify VNFS testcases """
227 def test_generate_vnfd(self):
228 """ Function to verify vnfd generation based on template """
230 generated_vnfd = vnfdgen.generate_vnfd(TREX_VNFD_TEMPLATE, NODE_CFG)
231 self.assertDictEqual(COMPLETE_TREX_VNFD, generated_vnfd)
233 def test_generate_tp_no_vars(self):
234 """ Function to verify traffic profile generation without imix """
237 generated_tp = vnfdgen.generate_vnfd(TRAFFIC_PROFILE_TPL, {"imix": {}})
238 self.assertDictEqual(TRAFFIC_PROFILE, generated_tp)
240 def test_deepgetitem(self):
242 self.assertEqual(vnfdgen.deepgetitem(d, "a"), 1)
244 def test_dict_flatten_int(self):
246 self.assertEqual(vnfdgen.deepgetitem(d, "a"), 1)
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)
252 def test_dict_flatten_int_key_fallback(self):
254 self.assertEqual(vnfdgen.deepgetitem(d, "0"), 1)
256 def test_dict_flatten_list(self):
257 d = {'a': 1, 'b': list(range(2))}
258 self.assertEqual(vnfdgen.deepgetitem(d, "b.0"), 0)
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)
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)
268 def test_generate_tp_single_var(self):
269 """ Function to verify traffic profile generation with imix """
272 vnfdgen.generate_vnfd(TRAFFIC_PROFILE_TPL,
273 {"imix": {UPLINK: {"imix_small": '20'}}})
275 tp2 = dict(TRAFFIC_PROFILE)
276 tp2[UPLINK][0]["ipv4"]["outer_l2"]["framesize"]["64B"] = '20'
277 self.assertDictEqual(tp2, generated_tp)