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