Fix exception handling ixload
[yardstick.git] / yardstick / tests / unit / network_services / traffic_profile / test_trex_traffic_profile.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import ipaddress
16
17 import six
18 import unittest
19
20 from yardstick.common import exceptions as y_exc
21 from yardstick.network_services.traffic_profile import base as tp_base
22 from yardstick.network_services.traffic_profile import trex_traffic_profile
23
24
25 class TestTrexProfile(unittest.TestCase):
26     TRAFFIC_PROFILE = {
27         "schema": "isb:traffic_profile:0.1",
28         "name": "fixed",
29         "description": "Fixed traffic profile to run UDP traffic",
30         "traffic_profile": {
31             "traffic_type": "FixedTraffic",
32             "frame_rate": 100,  # pps
33             "flow_number": 10,
34             "frame_size": 64}}
35
36     EXAMPLE_ETHERNET_ADDR = "00:00:00:00:00:01"
37     EXAMPLE_IP_ADDR = "10.0.0.1"
38     EXAMPLE_IPv6_ADDR = "0064:ff9b:0:0:0:0:9810:6414"
39
40     PROFILE = {
41         'description': 'Traffic profile to run RFC2544 latency',
42         'name': 'rfc2544',
43         'traffic_profile': {'traffic_type': 'RFC2544Profile',
44                             'frame_rate': 100},
45         tp_base.TrafficProfile.DOWNLINK: {
46             'ipv4': {'outer_l2': {'framesize': {'64B': '100',
47                                                 '1518B': '0',
48                                                 '128B': '0',
49                                                 '1400B': '0',
50                                                 '256B': '0',
51                                                 '373b': '0',
52                                                 '570B': '0'},
53                                   "srcmac": "00:00:00:00:00:02",
54                                   "dstmac": "00:00:00:00:00:01"},
55                      'outer_l3v4': {'dstip4': '1.1.1.1-1.1.2.2',
56                                     'proto': 'udp',
57                                     'srcip4': '9.9.1.1-90.1.2.2',
58                                     'dscp': 0, 'ttl': 32,
59                                     'count': 1},
60                      'outer_l4': {'srcport': '2001',
61                                   'dsrport': '1234',
62                                   'count': 1}}},
63         tp_base.TrafficProfile.UPLINK: {
64             'ipv4':
65                 {'outer_l2': {'framesize':
66                                   {'64B': '100', '1518B': '0',
67                                    '128B': '0', '1400B': '0',
68                                    '256B': '0', '373b': '0',
69                                    '570B': '0'},
70                               "srcmac": "00:00:00:00:00:01",
71                               "dstmac": "00:00:00:00:00:02"},
72                  'outer_l3v4': {'dstip4': '9.9.1.1-90.105.255.255',
73                                 'proto': 'udp',
74                                 'srcip4': '1.1.1.1-1.15.255.255',
75                                 'dscp': 0, 'ttl': 32, 'count': 1},
76                  'outer_l4': {'dstport': '2001',
77                               'srcport': '1234',
78                               'count': 1}}},
79         'schema': 'isb:traffic_profile:0.1'}
80     PROFILE_v6 = {
81         'description': 'Traffic profile to run RFC2544 latency',
82         'name': 'rfc2544',
83         'traffic_profile': {'traffic_type': 'RFC2544Profile',
84                             'frame_rate': 100},
85         tp_base.TrafficProfile.DOWNLINK: {
86             'ipv6': {'outer_l2': {'framesize':
87                                       {'64B': '100', '1518B': '0',
88                                        '128B': '0', '1400B': '0',
89                                        '256B': '0', '373b': '0',
90                                        '570B': '0'},
91                                   "srcmac": "00:00:00:00:00:02",
92                                   "dstmac": "00:00:00:00:00:01"},
93                      'outer_l3v4': {
94                          'dstip6':
95                              '0064:ff9b:0:0:0:0:9810:6414-0064:ff9b:0:0:0:0:9810:6420',
96                          'proto': 'udp',
97                          'srcip6':
98                              '0064:ff9b:0:0:0:0:9810:2814-0064:ff9b:0:0:0:0:9810:2820',
99                          'dscp': 0, 'ttl': 32,
100                          'count': 1},
101                      'outer_l4': {'srcport': '2001',
102                                   'dsrport': '1234',
103                                   'count': 1}}},
104         tp_base.TrafficProfile.UPLINK: {
105             'ipv6': {'outer_l2': {'framesize':
106                                       {'64B': '100', '1518B': '0',
107                                        '128B': '0', '1400B': '0',
108                                        '256B': '0', '373b': '0',
109                                        '570B': '0'},
110                                   "srcmac": "00:00:00:00:00:01",
111                                   "dstmac": "00:00:00:00:00:02"},
112                      'outer_l3v4': {
113                          'dstip6':
114                              '0064:ff9b:0:0:0:0:9810:2814-0064:ff9b:0:0:0:0:9810:2820',
115                          'proto': 'udp',
116                          'srcip6':
117                              '0064:ff9b:0:0:0:0:9810:6414-0064:ff9b:0:0:0:0:9810:6420',
118                          'dscp': 0, 'ttl': 32,
119                          'count': 1},
120                      'outer_l4': {'dstport': '2001',
121                                   'srcport': '1234',
122                                   'count': 1}}},
123         'schema': 'isb:traffic_profile:0.1'}
124
125     def test___init__(self):
126         trex_profile = trex_traffic_profile.TrexProfile(self.PROFILE)
127         self.assertEqual(trex_profile.pps, 100)
128
129     def test_qinq(self):
130         trex_profile = trex_traffic_profile.TrexProfile(self.PROFILE)
131         qinq = {"S-VLAN": {"id": 128, "priority": 0, "cfi": 0},
132                 "C-VLAN": {"id": 512, "priority": 0, "cfi": 0}}
133
134         trex_profile = trex_traffic_profile.TrexProfile(self.PROFILE)
135         self.assertIsNone(trex_profile.set_qinq(qinq))
136
137         qinq = {"S-VLAN": {"id": "128-130", "priority": 0, "cfi": 0},
138                 "C-VLAN": {"id": "512-515", "priority": 0, "cfi": 0}}
139         self.assertIsNone(trex_profile.set_qinq(qinq))
140
141     def test__set_outer_l2_fields(self):
142         trex_profile = trex_traffic_profile.TrexProfile(self.PROFILE)
143         qinq = {"S-VLAN": {"id": 128, "priority": 0, "cfi": 0},
144                 "C-VLAN": {"id": 512, "priority": 0, "cfi": 0}}
145         outer_l2 = self.PROFILE[
146             tp_base.TrafficProfile.UPLINK]['ipv4']['outer_l2']
147         outer_l2['QinQ'] = qinq
148         self.assertIsNone(trex_profile._set_outer_l2_fields(outer_l2))
149
150     def test__set_outer_l3v4_fields(self):
151         trex_profile = trex_traffic_profile.TrexProfile(self.PROFILE)
152         outer_l3v4 = self.PROFILE[
153             tp_base.TrafficProfile.UPLINK]['ipv4']['outer_l3v4']
154         outer_l3v4['proto'] = 'tcp'
155         self.assertIsNone(trex_profile._set_outer_l3v4_fields(outer_l3v4))
156
157     def test__set_outer_l3v6_fields(self):
158         trex_profile = trex_traffic_profile.TrexProfile(self.PROFILE)
159         outer_l3v6 = self.PROFILE_v6[
160             tp_base.TrafficProfile.UPLINK]['ipv6']['outer_l3v4']
161         outer_l3v6['proto'] = 'tcp'
162         outer_l3v6['tc'] = 1
163         outer_l3v6['hlim'] = 10
164         self.assertIsNone(trex_profile._set_outer_l3v6_fields(outer_l3v6))
165
166     def test__set_outer_l4_fields(self):
167         trex_profile = trex_traffic_profile.TrexProfile(self.PROFILE)
168         outer_l4 = self.PROFILE[
169             tp_base.TrafficProfile.UPLINK]['ipv4']['outer_l4']
170         self.assertIsNone(trex_profile._set_outer_l4_fields(outer_l4))
171
172     def test__count_ip_ipv4(self):
173         start, end, count = trex_traffic_profile.TrexProfile._count_ip(
174             '1.1.1.1', '1.2.3.4')
175         self.assertEqual('1.1.1.1', str(start))
176         self.assertEqual('1.2.3.4', str(end))
177         diff = (int(ipaddress.IPv4Address(six.u('1.2.3.4'))) -
178                 int(ipaddress.IPv4Address(six.u('1.1.1.1'))))
179         self.assertEqual(diff, count)
180
181     def test__count_ip_ipv6(self):
182         start_ip = '0064:ff9b:0:0:0:0:9810:6414'
183         end_ip = '0064:ff9b:0:0:0:0:9810:6420'
184         start, end, count = trex_traffic_profile.TrexProfile._count_ip(
185             start_ip, end_ip)
186         self.assertEqual(0x98106414, start)
187         self.assertEqual(0x98106420, end)
188         self.assertEqual(0x98106420 - 0x98106414, count)
189
190     def test__count_ip_ipv6_exception(self):
191         start_ip = '0064:ff9b:0:0:0:0:9810:6420'
192         end_ip = '0064:ff9b:0:0:0:0:9810:6414'
193         with self.assertRaises(y_exc.IPv6RangeError):
194             trex_traffic_profile.TrexProfile._count_ip(start_ip, end_ip)
195
196     def test__dscp_range_action_partial_actual_count_zero(self):
197         traffic_profile = trex_traffic_profile.TrexProfile(self.PROFILE)
198         dscp_partial = traffic_profile._dscp_range_action_partial()
199
200         flow_vars_initial_length = len(traffic_profile.vm_flow_vars)
201         dscp_partial('1', '1', 'unneeded')
202         self.assertEqual(len(traffic_profile.vm_flow_vars), flow_vars_initial_length + 2)
203
204     def test__dscp_range_action_partial_count_greater_than_actual(self):
205         traffic_profile = trex_traffic_profile.TrexProfile(self.PROFILE)
206         dscp_partial = traffic_profile._dscp_range_action_partial()
207
208         flow_vars_initial_length = len(traffic_profile.vm_flow_vars)
209         dscp_partial('1', '10', '100')
210         self.assertEqual(len(traffic_profile.vm_flow_vars), flow_vars_initial_length + 2)
211
212     def test__udp_range_action_partial_actual_count_zero(self):
213         traffic_profile = trex_traffic_profile.TrexProfile(self.PROFILE)
214         traffic_profile.udp['field1'] = 'value1'
215         udp_partial = traffic_profile._udp_range_action_partial('field1')
216
217         flow_vars_initial_length = len(traffic_profile.vm_flow_vars)
218         udp_partial('1', '1', 'unneeded')
219         self.assertEqual(len(traffic_profile.vm_flow_vars), flow_vars_initial_length + 2)
220
221     def test__udp_range_action_partial_count_greater_than_actual(self):
222         traffic_profile = trex_traffic_profile.TrexProfile(self.PROFILE)
223         traffic_profile.udp['field1'] = 'value1'
224         udp_partial = traffic_profile._udp_range_action_partial(
225             'field1', 'not_used_count')
226         flow_vars_initial_length = len(traffic_profile.vm_flow_vars)
227         udp_partial('1', '10', '100')
228         self.assertEqual(len(traffic_profile.vm_flow_vars), flow_vars_initial_length + 2)
229
230     def test__general_single_action_partial(self):
231         trex_profile = trex_traffic_profile.TrexProfile(self.PROFILE)
232         trex_profile._general_single_action_partial(
233             trex_traffic_profile.ETHERNET)(trex_traffic_profile.SRC)(
234             self.EXAMPLE_ETHERNET_ADDR)
235         self.assertEqual(self.EXAMPLE_ETHERNET_ADDR,
236                          trex_profile.ether_packet.src)
237
238         trex_profile._general_single_action_partial(trex_traffic_profile.IP)(
239             trex_traffic_profile.DST)(self.EXAMPLE_IP_ADDR)
240         self.assertEqual(self.EXAMPLE_IP_ADDR, trex_profile.ip_packet.dst)
241
242         trex_profile._general_single_action_partial(trex_traffic_profile.IPv6)(
243             trex_traffic_profile.DST)(self.EXAMPLE_IPv6_ADDR)
244         self.assertEqual(self.EXAMPLE_IPv6_ADDR, trex_profile.ip6_packet.dst)
245
246         trex_profile._general_single_action_partial(trex_traffic_profile.UDP)(
247             trex_traffic_profile.SRC_PORT)(5060)
248         self.assertEqual(5060, trex_profile.udp_packet.sport)
249
250         trex_profile._general_single_action_partial(trex_traffic_profile.IP)(
251             trex_traffic_profile.TYPE_OF_SERVICE)(0)
252         self.assertEqual(0, trex_profile.ip_packet.tos)
253
254     def test__set_proto_addr(self):
255         trex_profile = trex_traffic_profile.TrexProfile(self.PROFILE)
256
257         ether_range = "00:00:00:00:00:01-00:00:00:00:00:02"
258         ip_range = "1.1.1.2-1.1.1.10"
259         ipv6_range = '0064:ff9b:0:0:0:0:9810:6414-0064:ff9b:0:0:0:0:9810:6420'
260
261         trex_profile._set_proto_addr(trex_traffic_profile.ETHERNET,
262                                      trex_traffic_profile.SRC, ether_range)
263         trex_profile._set_proto_addr(trex_traffic_profile.ETHERNET,
264                                      trex_traffic_profile.DST, ether_range)
265         trex_profile._set_proto_addr(trex_traffic_profile.IP,
266                                      trex_traffic_profile.SRC, ip_range)
267         trex_profile._set_proto_addr(trex_traffic_profile.IP,
268                                      trex_traffic_profile.DST, ip_range)
269         trex_profile._set_proto_addr(trex_traffic_profile.IPv6,
270                                      trex_traffic_profile.SRC, ipv6_range)
271         trex_profile._set_proto_addr(trex_traffic_profile.IPv6,
272                                      trex_traffic_profile.DST, ipv6_range)
273         trex_profile._set_proto_addr(trex_traffic_profile.UDP,
274                                      trex_traffic_profile.SRC_PORT,
275                                      '5060-5090')
276         trex_profile._set_proto_addr(trex_traffic_profile.UDP,
277                                      trex_traffic_profile.DST_PORT, '5060')