Add "duration" parameter to test case definition
[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 mock
18 import six
19 import unittest
20
21 from yardstick.common import exceptions as y_exc
22 from yardstick.tests import STL_MOCKS
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 setUp(self):
143         self.trex_profile = TrexProfile(self.PROFILE)
144
145     def test___init__(self):
146         self.assertEqual(self.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         self.assertIsNone(self.trex_profile.set_qinq(qinq))
153
154         qinq = {"S-VLAN": {"id": "128-130", "priority": 0, "cfi": 0},
155                 "C-VLAN": {"id": "512-515", "priority": 0, "cfi": 0}}
156         self.assertIsNone(self.trex_profile.set_qinq(qinq))
157
158     def test__set_outer_l2_fields(self):
159         qinq = {"S-VLAN": {"id": 128, "priority": 0, "cfi": 0},
160                 "C-VLAN": {"id": 512, "priority": 0, "cfi": 0}}
161         outer_l2 = self.PROFILE[TrafficProfile.UPLINK]['ipv4']['outer_l2']
162         outer_l2['QinQ'] = qinq
163         self.assertIsNone(self.trex_profile._set_outer_l2_fields(outer_l2))
164
165     def test__set_outer_l3v4_fields(self):
166         outer_l3v4 = self.PROFILE[TrafficProfile.UPLINK]['ipv4']['outer_l3v4']
167         outer_l3v4['proto'] = 'tcp'
168         self.assertIsNone(self.trex_profile._set_outer_l3v4_fields(outer_l3v4))
169
170     def test__set_outer_l3v6_fields(self):
171         outer_l3v6 = self.PROFILE_v6[TrafficProfile.UPLINK]['ipv6']['outer_l3v4']
172         outer_l3v6['proto'] = 'tcp'
173         outer_l3v6['tc'] = 1
174         outer_l3v6['hlim'] = 10
175         self.assertIsNone(self.trex_profile._set_outer_l3v6_fields(outer_l3v6))
176
177     def test__set_outer_l4_fields(self):
178         outer_l4 = self.PROFILE[TrafficProfile.UPLINK]['ipv4']['outer_l4']
179         self.assertIsNone(self.trex_profile._set_outer_l4_fields(outer_l4))
180
181     def test_get_streams(self):
182         profile_data = self.PROFILE[TrafficProfile.UPLINK]
183         self.assertIsNotNone(self.trex_profile.get_streams(profile_data))
184         self.trex_profile.pg_id = 1
185         self.assertIsNotNone(self.trex_profile.get_streams(profile_data))
186         self.trex_profile.params = self.PROFILE_v6
187         self.trex_profile.profile_data = self.PROFILE_v6[TrafficProfile.UPLINK]
188         self.assertIsNotNone(self.trex_profile.get_streams(profile_data))
189         self.trex_profile.pg_id = 1
190         self.assertIsNotNone(self.trex_profile.get_streams(profile_data))
191
192     def test_generate_packets(self):
193         self.trex_profile.fsize = 10
194         self.trex_profile.base_pkt = [10]
195         self.assertIsNone(self.trex_profile.generate_packets())
196
197     def test_generate_imix_data_error(self):
198         self.assertEqual({}, self.trex_profile.generate_imix_data(False))
199
200     def test__count_ip_ipv4(self):
201         start, end, count = TrexProfile._count_ip('1.1.1.1', '1.2.3.4')
202         self.assertEqual('1.1.1.1', str(start))
203         self.assertEqual('1.2.3.4', str(end))
204         diff = (int(ipaddress.IPv4Address(six.u('1.2.3.4'))) -
205                 int(ipaddress.IPv4Address(six.u('1.1.1.1'))))
206         self.assertEqual(diff, count)
207
208     def test__count_ip_ipv6(self):
209         start_ip = '0064:ff9b:0:0:0:0:9810:6414'
210         end_ip = '0064:ff9b:0:0:0:0:9810:6420'
211         start, end, count = TrexProfile._count_ip(start_ip, end_ip)
212         self.assertEqual(0x98106414, start)
213         self.assertEqual(0x98106420, end)
214         self.assertEqual(0x98106420 - 0x98106414, count)
215
216     def test__count_ip_ipv6_exception(self):
217         start_ip = '0064:ff9b:0:0:0:0:9810:6420'
218         end_ip = '0064:ff9b:0:0:0:0:9810:6414'
219         with self.assertRaises(y_exc.IPv6RangeError):
220             TrexProfile._count_ip(start_ip, end_ip)
221
222     def test__dscp_range_action_partial_actual_count_zero(self):
223         traffic_profile = TrexProfile(self.PROFILE)
224         dscp_partial = traffic_profile._dscp_range_action_partial()
225
226         flow_vars_initial_length = len(traffic_profile.vm_flow_vars)
227         dscp_partial('1', '1', 'unneeded')
228         self.assertEqual(len(traffic_profile.vm_flow_vars), flow_vars_initial_length + 2)
229
230     def test__dscp_range_action_partial_count_greater_than_actual(self):
231         traffic_profile = TrexProfile(self.PROFILE)
232         dscp_partial = traffic_profile._dscp_range_action_partial()
233
234         flow_vars_initial_length = len(traffic_profile.vm_flow_vars)
235         dscp_partial('1', '10', '100')
236         self.assertEqual(len(traffic_profile.vm_flow_vars), flow_vars_initial_length + 2)
237
238     def test__udp_range_action_partial_actual_count_zero(self):
239         traffic_profile = TrexProfile(self.PROFILE)
240         traffic_profile.udp['field1'] = 'value1'
241         udp_partial = traffic_profile._udp_range_action_partial('field1')
242
243         flow_vars_initial_length = len(traffic_profile.vm_flow_vars)
244         udp_partial('1', '1', 'unneeded')
245         self.assertEqual(len(traffic_profile.vm_flow_vars), flow_vars_initial_length + 2)
246
247     def test__udp_range_action_partial_count_greater_than_actual(self):
248         traffic_profile = TrexProfile(self.PROFILE)
249         traffic_profile.udp['field1'] = 'value1'
250         udp_partial = traffic_profile._udp_range_action_partial('field1', 'not_used_count')
251
252         flow_vars_initial_length = len(traffic_profile.vm_flow_vars)
253         udp_partial('1', '10', '100')
254         self.assertEqual(len(traffic_profile.vm_flow_vars), flow_vars_initial_length + 2)
255
256     def test__general_single_action_partial(self):
257         self.trex_profile._general_single_action_partial(ETHERNET)(SRC)(
258             self.EXAMPLE_ETHERNET_ADDR)
259         self.assertEqual(self.EXAMPLE_ETHERNET_ADDR,
260                          self.trex_profile.ether_packet.src)
261
262         self.trex_profile._general_single_action_partial(IP)(DST)(
263             self.EXAMPLE_IP_ADDR)
264         self.assertEqual(self.EXAMPLE_IP_ADDR, self.trex_profile.ip_packet.dst)
265
266         self.trex_profile._general_single_action_partial(IPv6)(DST)(
267             self.EXAMPLE_IPv6_ADDR)
268         self.assertEqual(self.EXAMPLE_IPv6_ADDR,
269                          self.trex_profile.ip6_packet.dst)
270
271         self.trex_profile._general_single_action_partial(UDP)(SRC_PORT)(5060)
272         self.assertEqual(5060, self.trex_profile.udp_packet.sport)
273
274         self.trex_profile._general_single_action_partial(
275             IP)(TYPE_OF_SERVICE)(0)
276         self.assertEqual(0, self.trex_profile.ip_packet.tos)
277
278     def test__set_proto_addr(self):
279         ether_range = "00:00:00:00:00:01-00:00:00:00:00:02"
280         ip_range = "1.1.1.2-1.1.1.10"
281         ipv6_range = '0064:ff9b:0:0:0:0:9810:6414-0064:ff9b:0:0:0:0:9810:6420'
282
283         self.trex_profile._set_proto_addr(ETHERNET, SRC, ether_range)
284         self.trex_profile._set_proto_addr(ETHERNET, DST, ether_range)
285         self.trex_profile._set_proto_addr(IP, SRC, ip_range)
286         self.trex_profile._set_proto_addr(IP, DST, ip_range)
287         self.trex_profile._set_proto_addr(IPv6, SRC, ipv6_range)
288         self.trex_profile._set_proto_addr(IPv6, DST, ipv6_range)
289         self.trex_profile._set_proto_addr(UDP, SRC_PORT, '5060-5090')
290         self.trex_profile._set_proto_addr(UDP, DST_PORT, '5060')