aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/network_services/vnf_generic/test_vnfdgen.py
blob: 2ab14129bbf2fd4987cd6f7a5b8bcbe6b8e62066 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Copyright (c) 2016-2017 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Unittest for yardstick.network_services.vnf_generic.vnfdgen

from __future__ import absolute_import
import unittest
from six.moves import range

from yardstick.common.yaml_loader import yaml_load
from yardstick.network_services.vnf_generic import vnfdgen


UPLINK = "uplink"
DOWNLINK = "downlink"

TREX_VNFD_TEMPLATE = """
vnfd:vnfd-catalog:
    vnfd:
    -   id: TrexTrafficGen  # ISB class mapping
        name: trexgen
        short-name: trexgen
        description: TRex stateless traffic generator for RFC2544
        mgmt-interface:
            vdu-id: trexgen-baremetal
            user: {{user}}  # Value filled by vnfdgen
            password: {{password}}  # Value filled by vnfdgen
            ip: {{ip}}  # Value filled by vnfdgen
        connection-point:
        -   name: xe0
            type: VPORT
        -   name: xe1
            type: VPORT
        vdu:
        -   id: trexgen-baremetal
            name: trexgen-baremetal
            description: TRex stateless traffic generator for RFC2544
            external-interface:
            -   name: xe0
                virtual-interface:
                    type: PCI-PASSTHROUGH
                    vpci: '{{ interfaces.xe0.vpci}}'
                    local_ip: '{{ interfaces.xe0.local_ip }}'
                    dst_ip: '{{ interfaces.xe0.dst_ip }}'
                    local_mac: '{{ interfaces.xe0.local_mac }}'
                    dst_mac: '{{ interfaces.xe0.dst_mac }}'
                    bandwidth: 10 Gbps
                vnfd-connection-point-ref: xe0
            -   name: xe1
                virtual-interface:
                    type: PCI-PASSTHROUGH
                    vpci: '{{ interfaces.xe1.vpci }}'
                    local_ip: '{{ interfaces.xe1.local_ip }}'
                    dst_ip: '{{ interfaces.xe1.dst_ip }}'
                    local_mac: '{{ interfaces.xe1.local_mac }}'
                    dst_mac: '{{ interfaces.xe1.dst_mac }}'
                    bandwidth: 10 Gbps
                vnfd-connection-point-ref: xe1
            routing_table: {{ routing_table }}
            nd_route_tbl: {{ nd_route_tbl }}

        benchmark:
            kpi:
                - rx_throughput_fps
                - tx_throughput_fps
                - tx_throughput_mbps
                - rx_throughput_mbps
                - tx_throughput_pc_linerate
                - rx_throughput_pc_linerate
                - min_latency
                - max_latency
                - avg_latency
"""

COMPLETE_TREX_VNFD = \
    {'vnfd:vnfd-catalog':
     {'vnfd':
      [{'benchmark':
        {'kpi':
         ['rx_throughput_fps',
          'tx_throughput_fps',
          'tx_throughput_mbps',
          'rx_throughput_mbps',
          'tx_throughput_pc_linerate',
          'rx_throughput_pc_linerate',
          'min_latency',
          'max_latency',
          'avg_latency']},
        'connection-point': [{'name': 'xe0',
                              'type': 'VPORT'},
                             {'name': 'xe1',
                              'type': 'VPORT'}],
        'description': 'TRex stateless traffic generator for RFC2544',
        'id': 'TrexTrafficGen',
        'mgmt-interface': {'ip': '1.1.1.1',
                           'password': 'berta',
                           'user': 'berta',
                           'vdu-id': 'trexgen-baremetal'},
        'name': 'trexgen',
        'short-name': 'trexgen',
        'vdu': [{'description': 'TRex stateless traffic generator for RFC2544',
                 'external-interface':
                 [{'name': 'xe0',
                   'virtual-interface': {'bandwidth': '10 Gbps',
                                         'dst_ip': '1.1.1.1',
                                         'dst_mac': '00:01:02:03:04:05',
                                         'local_ip': '1.1.1.2',
                                         'local_mac': '00:01:02:03:05:05',
                                         'type': 'PCI-PASSTHROUGH',
                                         'vpci': '0000:00:10.2'},
                   'vnfd-connection-point-ref': 'xe0'},
                  {'name': 'xe1',
                   'virtual-interface': {'bandwidth': '10 Gbps',
                                         'dst_ip': '2.1.1.1',
                                         'dst_mac': '00:01:02:03:04:06',
                                         'local_ip': '2.1.1.2',
                                         'local_mac': '00:01:02:03:05:06',
                                         'type': 'PCI-PASSTHROUGH',
                                         'vpci': '0000:00:10.1'},
                   'vnfd-connection-point-ref': 'xe1'}],
                 'id': 'trexgen-baremetal',
                 'nd_route_tbl': [{'gateway': '0064:ff9b:0:0:0:0:9810:6414',
                                   'if': 'xe0',
                                   'netmask': '112',
                                   'network': '0064:ff9b:0:0:0:0:9810:6414'},
                                  {'gateway': '0064:ff9b:0:0:0:0:9810:2814',
                                   'if': 'xe1',
                                   'netmask': '112',
                                   'network': '0064:ff9b:0:0:0:0:9810:2814'}],
                 'routing_table': [{'gateway': '152.16.100.20',
                                    'if': 'xe0',
                                    'netmask': '255.255.255.0',
                                    'network': '152.16.100.20'},
                                   {'gateway': '152.16.40.20',
                                    'if': 'xe1',
                                    'netmask': '255.255.255.0',
                                    'network': '152.16.40.20'}],
                 'name': 'trexgen-baremetal'}]}]}}

NODE_CFG = {'ip': '1.1.1.1',
            'name': 'demeter',
            'password': 'berta',
            'role': 'TrafficGen',
            'user': 'berta',
            'interfaces': {'xe0': {'dpdk_port_num': 1,
                                   'dst_ip': '1.1.1.1',
                                   'dst_mac': '00:01:02:03:04:05',
                                   'local_ip': '1.1.1.2',
                                   'local_mac': '00:01:02:03:05:05',
                                   'vpci': '0000:00:10.2'},
                           'xe1': {'dpdk_port_num': 0,
                                   'dst_ip': '2.1.1.1',
                                   'dst_mac': '00:01:02:03:04:06',
                                   'local_ip': '2.1.1.2',
                                   'local_mac': '00:01:02:03:05:06',
                                   'vpci': '0000:00:10.1'}},
            'nd_route_tbl': [{u'gateway': u'0064:ff9b:0:0:0:0:9810:6414',
                              u'if': u'xe0',
                              u'netmask': u'112',
                              u'network': u'0064:ff9b:0:0:0:0:9810:6414'},
                             {u'gateway': u'0064:ff9b:0:0:0:0:9810:2814',
                              u'if': u'xe1',
                              u'netmask': u'112',
                              u'network': u'0064:ff9b:0:0:0:0:9810:2814'}],
            'routing_table': [{u'gateway': u'152.16.100.20',
                               u'if': u'xe0',
                               u'netmask': u'255.255.255.0',
                               u'network': u'152.16.100.20'},
                              {u'gateway': u'152.16.40.20',
                               u'if': u'xe1',
                               u'netmask': u'255.255.255.0',
                               u'network': u'152.16.40.20'}],
            }


# need to template, but can't use {} so use %s
TRAFFIC_PROFILE_TPL = """
%(0)s:
    - ipv4:
        outer_l2:
            framesize:
                64B: "{{ get(imix, '%(0)s.imix_small', 10) }}"
                128B: "{{ get(imix, '%(0)s.imix_128B', 10) }}"
                256B: "{{ get(imix, '%(0)s.imix_256B', 10) }}"
                373B: "{{ get(imix, '%(0)s.imix_373B', 10) }}"
                570B: "{{get(imix, '%(0)s.imix_570B', 10) }}"
                1400B: "{{get(imix, '%(0)s.imix_1400B', 10) }}"
                1518B: "{{get(imix, '%(0)s.imix_1500B', 40) }}"
""" % {"0": UPLINK}

TRAFFIC_PROFILE = {
    UPLINK: [{"ipv4": {"outer_l2":
                          {"framesize": {"64B": '10', "128B": '10',
                                         "256B": '10', "373B": '10',
                                         "570B": '10', "1400B": '10',
                                         "1518B": '40'}}}}]}


class TestRender(unittest.TestCase):

    def test_render_none(self):

        tmpl = "{{ routing_table }}"
        self.assertEqual(vnfdgen.render(tmpl, routing_table=None), u'~')
        self.assertEqual(yaml_load(vnfdgen.render(tmpl, routing_table=None)), None)

    def test_render_unicode_dict(self):

        tmpl = "{{ routing_table }}"
        self.assertEqual(yaml_load(vnfdgen.render(tmpl, **NODE_CFG)), NODE_CFG["routing_table"])


class TestVnfdGen(unittest.TestCase):
    """ Class to verify VNFS testcases """

    def test_generate_vnfd(self):
        """ Function to verify vnfd generation based on template """
        self.maxDiff = None
        generated_vnfd = vnfdgen.generate_vnfd(TREX_VNFD_TEMPLATE, NODE_CFG)
        self.assertDictEqual(COMPLETE_TREX_VNFD, generated_vnfd)

    def test_generate_tp_no_vars(self):
        """ Function to verify traffic profile generation without imix """

        self.maxDiff = None
        generated_tp = vnfdgen.generate_vnfd(TRAFFIC_PROFILE_TPL, {"imix": {}})
        self.assertDictEqual(TRAFFIC_PROFILE, generated_tp)

    def test_deepgetitem(self):
        d = {'a': 1, 'b': 2}
        self.assertEqual(vnfdgen.deepgetitem(d, "a"), 1)

    def test_dict_flatten_int(self):
        d = {'a': 1, 'b': 2}
        self.assertEqual(vnfdgen.deepgetitem(d, "a"), 1)

    def test_dict_flatten_str_int_key_first(self):
        d = {'0': 1, 0: 24, 'b': 2}
        self.assertEqual(vnfdgen.deepgetitem(d, "0"), 1)

    def test_dict_flatten_int_key_fallback(self):
        d = {0: 1, 'b': 2}
        self.assertEqual(vnfdgen.deepgetitem(d, "0"), 1)

    def test_dict_flatten_list(self):
        d = {'a': 1, 'b': list(range(2))}
        self.assertEqual(vnfdgen.deepgetitem(d, "b.0"), 0)

    def test_dict_flatten_dict(self):
        d = {'a': 1, 'b': {x: x for x in list(range(2))}}
        self.assertEqual(vnfdgen.deepgetitem(d, "b.0"), 0)

    def test_dict_flatten_only_str_key(self):
        d = {'0': 1, 0: 24, 'b': 2}
        self.assertRaises(AttributeError, vnfdgen.deepgetitem, d, 0)


    def test_generate_tp_single_var(self):
        """ Function to verify traffic profile generation with imix """

        generated_tp = \
            vnfdgen.generate_vnfd(TRAFFIC_PROFILE_TPL,
                                  {"imix": {UPLINK: {"imix_small": '20'}}})
        self.maxDiff = None
        tp2 = dict(TRAFFIC_PROFILE)
        tp2[UPLINK][0]["ipv4"]["outer_l2"]["framesize"]["64B"] = '20'
        self.assertDictEqual(tp2, generated_tp)