126a4a2a46fe8222ada32c7e088a2e84a18f2828
[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 import unittest
20
21 from yardstick.network_services.traffic_profile.base import TrafficProfile
22 from yardstick.network_services.traffic_profile.traffic_profile import \
23     TrexProfile
24
25
26 class TestTrexProfile(unittest.TestCase):
27     TRAFFIC_PROFILE = {
28         "schema": "isb:traffic_profile:0.1",
29         "name": "fixed",
30         "description": "Fixed traffic profile to run UDP traffic",
31         "traffic_profile": {
32             "traffic_type": "FixedTraffic",
33             "frame_rate": 100,  # pps
34             "flow_number": 10,
35             "frame_size": 64}}
36
37     PROFILE = {'description': 'Traffic profile to run RFC2544 latency',
38                'name': 'rfc2544',
39                'traffic_profile': {'traffic_type': 'RFC2544Profile',
40                                    'frame_rate': 100},
41                'public': {'ipv4': {'outer_l2': {'framesize': {'64B': '100',
42                                                               '1518B': '0',
43                                                               '128B': '0',
44                                                               '1400B': '0',
45                                                               '256B': '0',
46                                                               '373b': '0',
47                                                               '570B': '0'},
48                                                 "srcmac": "00:00:00:00:00:02",
49                                                 "dstmac": "00:00:00:00:00:01"},
50                                    'outer_l3v4': {'dstip4': '1.1.1.1-1.1.2.2',
51                                                   'proto': 'udp',
52                                                   'srcip4': '9.9.1.1-90.1.2.2',
53                                                   'dscp': 0, 'ttl': 32},
54                                    'outer_l4': {'srcport': '2001',
55                                                 'dsrport': '1234'}}},
56                'private': {'ipv4':
57                            {'outer_l2': {'framesize':
58                                          {'64B': '100', '1518B': '0',
59                                           '128B': '0', '1400B': '0',
60                                           '256B': '0', '373b': '0',
61                                           '570B': '0'},
62                                          "srcmac": "00:00:00:00:00:01",
63                                          "dstmac": "00:00:00:00:00:02"},
64                             'outer_l3v4': {'dstip4': '9.9.1.1-90.105.255.255',
65                                            'proto': 'udp',
66                                            'srcip4': '1.1.1.1-1.15.255.255',
67                                            'dscp': 0, 'ttl': 32},
68                             'outer_l4': {'dstport': '2001',
69                                          'srcport': '1234'}}},
70                'schema': 'isb:traffic_profile:0.1'}
71     PROFILE_v6 = {'description': 'Traffic profile to run RFC2544 latency',
72                   'name': 'rfc2544',
73                   'traffic_profile': {'traffic_type': 'RFC2544Profile',
74                                       'frame_rate': 100},
75                   'public': {'ipv6': {'outer_l2': {'framesize':
76                                                    {'64B': '100', '1518B': '0',
77                                                     '128B': '0', '1400B': '0',
78                                                     '256B': '0', '373b': '0',
79                                                     '570B': '0'},
80                                                    "srcmac": "00:00:00:00:00:02",
81                                                    "dstmac": "00:00:00:00:00:01"},
82                                       'outer_l3v4': {'dstip6': '0064:ff9b:0:0:0:0:9810:6414-0064:ff9b:0:0:0:0:9810:6420',
83                                                      'proto': 'udp',
84                                                      'srcip6': '0064:ff9b:0:0:0:0:9810:2814-0064:ff9b:0:0:0:0:9810:2820',
85                                                      'dscp': 0, 'ttl': 32},
86                                       'outer_l4': {'srcport': '2001',
87                                                    'dsrport': '1234'}}},
88                   'private':
89                   {'ipv6': {'outer_l2': {'framesize':
90                                          {'64B': '100', '1518B': '0',
91                                           '128B': '0', '1400B': '0',
92                                           '256B': '0', '373b': '0',
93                                           '570B': '0'},
94                                          "srcmac": "00:00:00:00:00:01",
95                                          "dstmac": "00:00:00:00:00:02"},
96                             'outer_l3v4': {'dstip6': '0064:ff9b:0:0:0:0:9810:2814-0064:ff9b:0:0:0:0:9810:2820',
97                                            'proto': 'udp',
98                                            'srcip6': '0064:ff9b:0:0:0:0:9810:6414-0064:ff9b:0:0:0:0:9810:6420',
99                                            'dscp': 0, 'ttl': 32},
100                             'outer_l4': {'dstport': '2001',
101                                          'srcport': '1234'}}},
102                   'schema': 'isb:traffic_profile:0.1'}
103
104     def test___init__(self):
105         TrafficProfile.params = self.PROFILE
106         trex_profile = \
107             TrexProfile(TrafficProfile)
108         self.assertEqual(trex_profile.pps, 100)
109
110     def test_execute(self):
111         trex_profile = \
112             TrexProfile(TrafficProfile)
113         self.assertEqual(None, trex_profile.execute({}))
114
115     def test_set_src_mac(self):
116         src_mac = "00:00:00:00:00:01"
117         trex_profile = \
118             TrexProfile(TrafficProfile)
119         self.assertEqual(None, trex_profile.set_src_mac(src_mac))
120
121         src_mac = "00:00:00:00:00:01-00:00:00:00:00:02"
122         self.assertEqual(None, trex_profile.set_src_mac(src_mac))
123
124     def test_set_dst_mac(self):
125         dst_mac = "00:00:00:00:00:03"
126         trex_profile = \
127             TrexProfile(TrafficProfile)
128         self.assertEqual(None, trex_profile.set_dst_mac(dst_mac))
129
130         dst_mac = "00:00:00:00:00:03-00:00:00:00:00:04"
131         self.assertEqual(None, trex_profile.set_dst_mac(dst_mac))
132
133     def test_set_src_ip4(self):
134         src_ipv4 = "152.16.100.20"
135         trex_profile = \
136             TrexProfile(TrafficProfile)
137         self.assertEqual(None, trex_profile.set_src_ip4(src_ipv4))
138
139         src_ipv4 = "152.16.100.20-152.16.100.30"
140         self.assertEqual(None, trex_profile.set_src_ip4(src_ipv4))
141
142     def test_set_dst_ip4(self):
143         dst_ipv4 = "152.16.100.20"
144         trex_profile = \
145             TrexProfile(TrafficProfile)
146         self.assertEqual(None, trex_profile.set_dst_ip4(dst_ipv4))
147
148         dst_ipv4 = "152.16.100.20-152.16.100.30"
149         self.assertEqual(None, trex_profile.set_dst_ip4(dst_ipv4))
150
151     def test_set_src_ip6(self):
152         src_ipv6 = "0064:ff9b:0:0:0:0:9810:6414"
153         trex_profile = \
154             TrexProfile(TrafficProfile)
155         self.assertEqual(None, trex_profile.set_src_ip6(src_ipv6))
156
157         src_ipv6 = "0064:ff9b:0:0:0:0:9810:6414-0064:ff9b:0:0:0:0:9810:6420"
158         self.assertEqual(None, trex_profile.set_src_ip6(src_ipv6))
159
160     def test_set_dst_ip6(self):
161         dst_ipv6 = "0064:ff9b:0:0:0:0:9810:6414"
162         trex_profile = \
163             TrexProfile(TrafficProfile)
164         self.assertEqual(None, trex_profile.set_dst_ip6(dst_ipv6))
165
166         dst_ipv6 = "0064:ff9b:0:0:0:0:9810:6414-0064:ff9b:0:0:0:0:9810:6420"
167         self.assertEqual(None, trex_profile.set_dst_ip6(dst_ipv6))
168
169     def test_dscp(self):
170         dscp = "0"
171         trex_profile = \
172             TrexProfile(TrafficProfile)
173         self.assertEqual(None, trex_profile.set_dscp(dscp))
174
175         dscp = "0-1"
176         self.assertEqual(None, trex_profile.set_dscp(dscp))
177
178     def test_src_port(self):
179         port = "1234"
180         trex_profile = \
181             TrexProfile(TrafficProfile)
182         self.assertEqual(None, trex_profile.set_src_port(port))
183
184         port = "1234-5678"
185         self.assertEqual(None, trex_profile.set_src_port(port))
186
187     def test_dst_port(self):
188         port = "1234"
189         trex_profile = \
190             TrexProfile(TrafficProfile)
191         self.assertEqual(None, trex_profile.set_dst_port(port))
192
193         port = "1234-5678"
194         self.assertEqual(None, trex_profile.set_dst_port(port))
195
196     def test_qinq(self):
197         qinq = {"S-VLAN": {"id": 128, "priority": 0, "cfi": 0},
198                 "C-VLAN": {"id": 512, "priority": 0, "cfi": 0}}
199
200         trex_profile = \
201             TrexProfile(TrafficProfile)
202         self.assertEqual(None, trex_profile.set_qinq(qinq))
203
204         qinq = {"S-VLAN": {"id": "128-130", "priority": 0, "cfi": 0},
205                 "C-VLAN": {"id": "512-515", "priority": 0, "cfi": 0}}
206         self.assertEqual(None, trex_profile.set_qinq(qinq))
207
208     def test_set_outer_l2_fields(self):
209         trex_profile = \
210             TrexProfile(TrafficProfile)
211         qinq = {"S-VLAN": {"id": 128, "priority": 0, "cfi": 0},
212                 "C-VLAN": {"id": 512, "priority": 0, "cfi": 0}}
213         outer_l2 = self.PROFILE['private']['ipv4']['outer_l2']
214         outer_l2['QinQ'] = qinq
215         self.assertEqual(None, trex_profile.set_outer_l2_fields(outer_l2))
216
217     def test_set_outer_l3v4_fields(self):
218         trex_profile = \
219             TrexProfile(TrafficProfile)
220         outer_l3v4 = self.PROFILE['private']['ipv4']['outer_l3v4']
221         outer_l3v4['proto'] = 'tcp'
222         self.assertEqual(None, trex_profile.set_outer_l3v4_fields(outer_l3v4))
223
224     def test_set_outer_l3v6_fields(self):
225         trex_profile = \
226             TrexProfile(TrafficProfile)
227         outer_l3v6 = self.PROFILE_v6['private']['ipv6']['outer_l3v4']
228         outer_l3v6['proto'] = 'tcp'
229         outer_l3v6['tc'] = 1
230         outer_l3v6['hlim'] = 10
231         self.assertEqual(None, trex_profile.set_outer_l3v6_fields(outer_l3v6))
232
233     def test_set_outer_l4_fields(self):
234         trex_profile = \
235             TrexProfile(TrafficProfile)
236         outer_l4 = self.PROFILE['private']['ipv4']['outer_l4']
237         self.assertEqual(None, trex_profile.set_outer_l4_fields(outer_l4))
238
239     def test_get_streams(self):
240         trex_profile = \
241             TrexProfile(TrafficProfile)
242         trex_profile.params = self.PROFILE
243         trex_profile.profile_data = self.PROFILE["private"]
244         self.assertIsNotNone(trex_profile.get_streams())
245         trex_profile.pg_id = 1
246         self.assertIsNotNone(trex_profile.get_streams())
247         trex_profile.params = self.PROFILE_v6
248         trex_profile.profile_data = self.PROFILE_v6["private"]
249         self.assertIsNotNone(trex_profile.get_streams())
250         trex_profile.pg_id = 1
251         self.assertIsNotNone(trex_profile.get_streams())
252
253     def test_generate_packets(self):
254         trex_profile = \
255             TrexProfile(TrafficProfile)
256         trex_profile.fsize = 10
257         trex_profile.base_pkt = [10]
258         self.assertIsNone(trex_profile.generate_packets())
259
260     def test_generate_imix_data_error(self):
261         trex_profile = \
262             TrexProfile(TrafficProfile)
263         self.assertEqual({}, trex_profile.generate_imix_data(False))
264
265     def test__get_start_end_ipv6(self):
266         trex_profile = \
267             TrexProfile(TrafficProfile)
268         self.assertRaises(SystemExit, trex_profile._get_start_end_ipv6,
269                           "1.1.1.3", "1.1.1.1")