NSB "Prox" : Cleanup duplicated traffic profile
[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
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.traffic_profile import TrexProfile
33     from yardstick.network_services.traffic_profile.traffic_profile import SRC
34     from yardstick.network_services.traffic_profile.traffic_profile import DST
35     from yardstick.network_services.traffic_profile.traffic_profile import ETHERNET
36     from yardstick.network_services.traffic_profile.traffic_profile import IP
37     from yardstick.network_services.traffic_profile.traffic_profile import IPv6
38     from yardstick.network_services.traffic_profile.traffic_profile import UDP
39     from yardstick.network_services.traffic_profile.traffic_profile import SRC_PORT
40     from yardstick.network_services.traffic_profile.traffic_profile import DST_PORT
41     from yardstick.network_services.traffic_profile.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 = {'description': 'Traffic profile to run RFC2544 latency',
60                'name': 'rfc2544',
61                'traffic_profile': {'traffic_type': 'RFC2544Profile',
62                                    'frame_rate': 100},
63                TrafficProfile.DOWNLINK: {'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: {'ipv4':
81                            {'outer_l2': {'framesize':
82                                          {'64B': '100', '1518B': '0',
83                                           '128B': '0', '1400B': '0',
84                                           '256B': '0', '373b': '0',
85                                           '570B': '0'},
86                                          "srcmac": "00:00:00:00:00:01",
87                                          "dstmac": "00:00:00:00:00:02"},
88                             'outer_l3v4': {'dstip4': '9.9.1.1-90.105.255.255',
89                                            'proto': 'udp',
90                                            'srcip4': '1.1.1.1-1.15.255.255',
91                                            'dscp': 0, 'ttl': 32, 'count': 1},
92                             'outer_l4': {'dstport': '2001',
93                                          'srcport': '1234',
94                                          'count': 1}}},
95                'schema': 'isb:traffic_profile:0.1'}
96     PROFILE_v6 = {'description': 'Traffic profile to run RFC2544 latency',
97                   'name': 'rfc2544',
98                   'traffic_profile': {'traffic_type': 'RFC2544Profile',
99                                       'frame_rate': 100},
100                   TrafficProfile.DOWNLINK: {'ipv6': {'outer_l2': {'framesize':
101                                                    {'64B': '100', '1518B': '0',
102                                                     '128B': '0', '1400B': '0',
103                                                     '256B': '0', '373b': '0',
104                                                     '570B': '0'},
105                                                    "srcmac": "00:00:00:00:00:02",
106                                                    "dstmac": "00:00:00:00:00:01"},
107                                                      'outer_l3v4': {'dstip6': '0064:ff9b:0:0:0:0:9810:6414-0064:ff9b:0:0:0:0:9810:6420',
108                                                      'proto': 'udp',
109                                                      'srcip6': '0064:ff9b:0:0:0:0:9810:2814-0064:ff9b:0:0:0:0:9810:2820',
110                                                      'dscp': 0, 'ttl': 32,
111                                                      'count': 1},
112                                                      'outer_l4': {'srcport': '2001',
113                                                    'dsrport': '1234',
114                                                    'count': 1}}},
115                   TrafficProfile.UPLINK:
116                   {'ipv6': {'outer_l2': {'framesize':
117                                          {'64B': '100', '1518B': '0',
118                                           '128B': '0', '1400B': '0',
119                                           '256B': '0', '373b': '0',
120                                           '570B': '0'},
121                                          "srcmac": "00:00:00:00:00:01",
122                                          "dstmac": "00:00:00:00:00:02"},
123                             'outer_l3v4': {'dstip6': '0064:ff9b:0:0:0:0:9810:2814-0064:ff9b:0:0:0:0:9810:2820',
124                                            'proto': 'udp',
125                                            'srcip6': '0064:ff9b:0:0:0:0:9810:6414-0064:ff9b:0:0:0:0:9810:6420',
126                                            'dscp': 0, 'ttl': 32,
127                                            'count': 1},
128                             'outer_l4': {'dstport': '2001',
129                                          'srcport': '1234',
130                                          'count': 1}}},
131                   'schema': 'isb:traffic_profile:0.1'}
132
133     def test___init__(self):
134         TrafficProfile.params = self.PROFILE
135         trex_profile = \
136             TrexProfile(TrafficProfile)
137         self.assertEqual(trex_profile.pps, 100)
138
139     def test_qinq(self):
140         qinq = {"S-VLAN": {"id": 128, "priority": 0, "cfi": 0},
141                 "C-VLAN": {"id": 512, "priority": 0, "cfi": 0}}
142
143         trex_profile = \
144             TrexProfile(TrafficProfile)
145         self.assertEqual(None, trex_profile.set_qinq(qinq))
146
147         qinq = {"S-VLAN": {"id": "128-130", "priority": 0, "cfi": 0},
148                 "C-VLAN": {"id": "512-515", "priority": 0, "cfi": 0}}
149         self.assertEqual(None, trex_profile.set_qinq(qinq))
150
151     def test__set_outer_l2_fields(self):
152         trex_profile = \
153             TrexProfile(TrafficProfile)
154         qinq = {"S-VLAN": {"id": 128, "priority": 0, "cfi": 0},
155                 "C-VLAN": {"id": 512, "priority": 0, "cfi": 0}}
156         outer_l2 = self.PROFILE[TrafficProfile.UPLINK]['ipv4']['outer_l2']
157         outer_l2['QinQ'] = qinq
158         self.assertEqual(None, trex_profile._set_outer_l2_fields(outer_l2))
159
160     def test__set_outer_l3v4_fields(self):
161         trex_profile = \
162             TrexProfile(TrafficProfile)
163         outer_l3v4 = self.PROFILE[TrafficProfile.UPLINK]['ipv4']['outer_l3v4']
164         outer_l3v4['proto'] = 'tcp'
165         self.assertEqual(None, trex_profile._set_outer_l3v4_fields(outer_l3v4))
166
167     def test__set_outer_l3v6_fields(self):
168         trex_profile = \
169             TrexProfile(TrafficProfile)
170         outer_l3v6 = self.PROFILE_v6[TrafficProfile.UPLINK]['ipv6']['outer_l3v4']
171         outer_l3v6['proto'] = 'tcp'
172         outer_l3v6['tc'] = 1
173         outer_l3v6['hlim'] = 10
174         self.assertEqual(None, trex_profile._set_outer_l3v6_fields(outer_l3v6))
175
176     def test__set_outer_l4_fields(self):
177         trex_profile = \
178             TrexProfile(TrafficProfile)
179         outer_l4 = self.PROFILE[TrafficProfile.UPLINK]['ipv4']['outer_l4']
180         self.assertEqual(None, trex_profile._set_outer_l4_fields(outer_l4))
181
182     def test_get_streams(self):
183         trex_profile = \
184             TrexProfile(TrafficProfile)
185         trex_profile.params = self.PROFILE
186         profile_data = self.PROFILE[TrafficProfile.UPLINK]
187         self.assertIsNotNone(trex_profile.get_streams(profile_data))
188         trex_profile.pg_id = 1
189         self.assertIsNotNone(trex_profile.get_streams(profile_data))
190         trex_profile.params = self.PROFILE_v6
191         trex_profile.profile_data = self.PROFILE_v6[TrafficProfile.UPLINK]
192         self.assertIsNotNone(trex_profile.get_streams(profile_data))
193         trex_profile.pg_id = 1
194         self.assertIsNotNone(trex_profile.get_streams(profile_data))
195
196     def test_generate_packets(self):
197         trex_profile = \
198             TrexProfile(TrafficProfile)
199         trex_profile.fsize = 10
200         trex_profile.base_pkt = [10]
201         self.assertIsNone(trex_profile.generate_packets())
202
203     def test_generate_imix_data_error(self):
204         trex_profile = \
205             TrexProfile(TrafficProfile)
206         self.assertEqual({}, trex_profile.generate_imix_data(False))
207
208     def test__get_start_end_ipv6(self):
209         trex_profile = \
210             TrexProfile(TrafficProfile)
211         self.assertRaises(SystemExit, trex_profile._get_start_end_ipv6,
212                           "1.1.1.3", "1.1.1.1")
213
214     def test__general_single_action_partial(self):
215         trex_profile = TrexProfile(TrafficProfile)
216
217         trex_profile._general_single_action_partial(ETHERNET)(SRC)(self.EXAMPLE_ETHERNET_ADDR)
218         self.assertEqual(self.EXAMPLE_ETHERNET_ADDR, trex_profile.ether_packet.src)
219
220         trex_profile._general_single_action_partial(IP)(DST)(self.EXAMPLE_IP_ADDR)
221         self.assertEqual(self.EXAMPLE_IP_ADDR, trex_profile.ip_packet.dst)
222
223         trex_profile._general_single_action_partial(IPv6)(DST)(self.EXAMPLE_IPv6_ADDR)
224         self.assertEqual(self.EXAMPLE_IPv6_ADDR, trex_profile.ip6_packet.dst)
225
226         trex_profile._general_single_action_partial(UDP)(SRC_PORT)(5060)
227         self.assertEqual(5060, trex_profile.udp_packet.sport)
228
229         trex_profile._general_single_action_partial(IP)(TYPE_OF_SERVICE)(0)
230         self.assertEqual(0, trex_profile.ip_packet.tos)
231
232     def test__set_proto_addr(self):
233         trex_profile = TrexProfile(TrafficProfile)
234
235         ether_range = "00:00:00:00:00:01-00:00:00:00:00:02"
236         ip_range = "1.1.1.2-1.1.1.10"
237         ipv6_range = '0064:ff9b:0:0:0:0:9810:6414-0064:ff9b:0:0:0:0:9810:6420'
238
239         trex_profile._set_proto_addr(ETHERNET, SRC, ether_range)
240         trex_profile._set_proto_addr(ETHERNET, DST, ether_range)
241         trex_profile._set_proto_addr(IP, SRC, ip_range)
242         trex_profile._set_proto_addr(IP, DST, ip_range)
243         trex_profile._set_proto_addr(IPv6, SRC, ipv6_range)
244         trex_profile._set_proto_addr(IPv6, DST, ipv6_range)
245         trex_profile._set_proto_addr(UDP, SRC_PORT, "5060-5090")
246         trex_profile._set_proto_addr(UDP, DST_PORT, "5060")