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