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