Standardized TRex client library path
[yardstick.git] / tests / unit / network_services / traffic_profile / test_ixia_rfc2544.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 from __future__ import division
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.traffic_profile \
31         import TrexProfile
32     from yardstick.network_services.traffic_profile.ixia_rfc2544 import \
33         IXIARFC2544Profile
34     from yardstick.network_services.traffic_profile import ixia_rfc2544
35
36
37 class TestIXIARFC2544Profile(unittest.TestCase):
38     TRAFFIC_PROFILE = {
39         "schema": "isb:traffic_profile:0.1",
40         "name": "fixed",
41         "description": "Fixed traffic profile to run UDP traffic",
42         "traffic_profile": {
43             "traffic_type": "FixedTraffic",
44             "frame_rate": 100,  # pps
45             "flow_number": 10,
46             "frame_size": 64}}
47
48     PROFILE = {'description': 'Traffic profile to run RFC2544 latency',
49                'name': 'rfc2544',
50                'traffic_profile': {'traffic_type': 'IXIARFC2544Profile',
51                                    'frame_rate': 100},
52                'public': {'ipv4':
53                           {'outer_l2': {'framesize':
54                                         {'64B': '100', '1518B': '0',
55                                          '128B': '0', '1400B': '0',
56                                          '256B': '0', '373b': '0',
57                                          '570B': '0'}},
58                            'outer_l3v4': {'dstip4': '1.1.1.1-1.15.255.255',
59                                           'proto': 'udp',
60                                           'srcip4': '90.90.1.1-90.105.255.255',
61                                           'dscp': 0, 'ttl': 32},
62                            'outer_l4': {'srcport': '2001',
63                                         'dsrport': '1234'}}},
64                'private': {'ipv4':
65                            {'outer_l2': {'framesize':
66                                          {'64B': '100', '1518B': '0',
67                                           '128B': '0', '1400B': '0',
68                                           '256B': '0', '373b': '0',
69                                           '570B': '0'}},
70                             'outer_l3v4': {'dstip4': '9.9.1.1-90.105.255.255',
71                                            'proto': 'udp',
72                                            'srcip4': '1.1.1.1-1.15.255.255',
73                                            'dscp': 0, 'ttl': 32},
74                             'outer_l4': {'dstport': '2001',
75                                          'srcport': '1234'}}},
76                'schema': 'isb:traffic_profile:0.1'}
77
78     def test_get_ixia_traffic_profile_error(self):
79         traffic_generator = mock.Mock(autospec=TrexProfile)
80         traffic_generator.my_ports = [0, 1]
81         traffic_generator.priv_ports = [-1]
82         traffic_generator.pub_ports = [1]
83         traffic_generator.client = \
84             mock.Mock(return_value=True)
85         STATIC_TRAFFIC = {
86             "private": {
87                 "id": 1,
88                 "bidir": "False",
89                 "duration": 60,
90                 "iload": "100",
91                 "outer_l2": {
92                     "dstmac": "00:00:00:00:00:03",
93                     "framesPerSecond": True,
94                     "framesize": 64,
95                     "srcmac": "00:00:00:00:00:01"
96                 },
97                 "outer_l3": {
98                     "dscp": 0,
99                     "dstip4": "152.16.40.20",
100                     "proto": "udp",
101                     "srcip4": "152.16.100.20",
102                     "ttl": 32
103                 },
104                 "outer_l3v4": {
105                     "dscp": 0,
106                     "dstip4": "152.16.40.20",
107                     "proto": "udp",
108                     "srcip4": "152.16.100.20",
109                     "ttl": 32
110                 },
111                 "outer_l3v6": {
112                     "count": 1024,
113                     "dscp": 0,
114                     "dstip4": "152.16.100.20",
115                     "proto": "udp",
116                     "srcip4": "152.16.40.20",
117                     "ttl": 32
118                 },
119                 "outer_l4": {
120                     "dstport": "2001",
121                     "srcport": "1234"
122                 },
123                 "traffic_type": "continuous"
124             },
125             "public": {
126                 "id": 2,
127                 "bidir": "False",
128                 "duration": 60,
129                 "iload": "100",
130                 "outer_l2": {
131                     "dstmac": "00:00:00:00:00:04",
132                     "framesPerSecond": True,
133                     "framesize": 64,
134                     "srcmac": "00:00:00:00:00:01"
135                 },
136                 "outer_l3": {
137                     "count": 1024,
138                     "dscp": 0,
139                     "dstip4": "152.16.100.20",
140                     "proto": "udp",
141                     "srcip4": "152.16.40.20",
142                     "ttl": 32
143                 },
144                 "outer_l3v4": {
145                     "count": 1024,
146                     "dscp": 0,
147                     "dstip4": "152.16.100.20",
148                     "proto": "udp",
149                     "srcip4": "152.16.40.20",
150                     "ttl": 32
151                 },
152                 "outer_l3v6": {
153                     "count": 1024,
154                     "dscp": 0,
155                     "dstip4": "152.16.100.20",
156                     "proto": "udp",
157                     "srcip4": "152.16.40.20",
158                     "ttl": 32
159                 },
160                 "outer_l4": {
161                     "dstport": "1234",
162                     "srcport": "2001"
163                 },
164                 "traffic_type": "continuous"
165             }
166         }
167         ixia_rfc2544.STATIC_TRAFFIC = STATIC_TRAFFIC
168
169         r_f_c2544_profile = IXIARFC2544Profile(self.TRAFFIC_PROFILE)
170         r_f_c2544_profile.rate = 100
171         mac = {"src_mac_0": "00:00:00:00:00:01",
172                "src_mac_1": "00:00:00:00:00:02",
173                "src_mac_2": "00:00:00:00:00:02",
174                "dst_mac_0": "00:00:00:00:00:03",
175                "dst_mac_1": "00:00:00:00:00:04",
176                "dst_mac_2": "00:00:00:00:00:04"}
177         self.assertRaises(IOError, r_f_c2544_profile._get_ixia_traffic_profile,
178                           self.PROFILE, mac, xfile="tmp",
179                           static_traffic=STATIC_TRAFFIC)
180
181
182     @mock.patch("yardstick.network_services.traffic_profile.ixia_rfc2544.open")
183     def test_get_ixia_traffic_profile(self, mock_open):
184         traffic_generator = mock.Mock(autospec=TrexProfile)
185         traffic_generator.my_ports = [0, 1]
186         traffic_generator.priv_ports = [-1]
187         traffic_generator.pub_ports = [1]
188         traffic_generator.client = \
189             mock.Mock(return_value=True)
190         STATIC_TRAFFIC = {
191             "private": {
192                 "id": 1,
193                 "bidir": "False",
194                 "duration": 60,
195                 "iload": "100",
196                 "outer_l2": {
197                     "dstmac": "00:00:00:00:00:03",
198                     "framesPerSecond": True,
199                     "framesize": 64,
200                     "srcmac": "00:00:00:00:00:01"
201                 },
202                 "outer_l3": {
203                     "dscp": 0,
204                     "dstip4": "152.16.40.20",
205                     "proto": "udp",
206                     "srcip4": "152.16.100.20",
207                     "ttl": 32
208                 },
209                 "outer_l3v4": {
210                     "dscp": 0,
211                     "dstip4": "152.16.40.20",
212                     "proto": "udp",
213                     "srcip4": "152.16.100.20",
214                     "ttl": 32
215                 },
216                 "outer_l3v6": {
217                     "count": 1024,
218                     "dscp": 0,
219                     "dstip4": "152.16.100.20",
220                     "proto": "udp",
221                     "srcip4": "152.16.40.20",
222                     "ttl": 32
223                 },
224                 "outer_l4": {
225                     "dstport": "2001",
226                     "srcport": "1234"
227                 },
228                 "traffic_type": "continuous"
229             },
230             "public": {
231                 "id": 2,
232                 "bidir": "False",
233                 "duration": 60,
234                 "iload": "100",
235                 "outer_l2": {
236                     "dstmac": "00:00:00:00:00:04",
237                     "framesPerSecond": True,
238                     "framesize": 64,
239                     "srcmac": "00:00:00:00:00:01"
240                 },
241                 "outer_l3": {
242                     "count": 1024,
243                     "dscp": 0,
244                     "dstip4": "152.16.100.20",
245                     "proto": "udp",
246                     "srcip4": "152.16.40.20",
247                     "ttl": 32
248                 },
249                 "outer_l3v4": {
250                     "count": 1024,
251                     "dscp": 0,
252                     "dstip4": "152.16.100.20",
253                     "proto": "udp",
254                     "srcip4": "152.16.40.20",
255                     "ttl": 32
256                 },
257                 "outer_l3v6": {
258                     "count": 1024,
259                     "dscp": 0,
260                     "dstip4": "152.16.100.20",
261                     "proto": "udp",
262                     "srcip4": "152.16.40.20",
263                     "ttl": 32
264                 },
265                 "outer_l4": {
266                     "dstport": "1234",
267                     "srcport": "2001"
268                 },
269                 "traffic_type": "continuous"
270             }
271         }
272         ixia_rfc2544.STATIC_TRAFFIC = STATIC_TRAFFIC
273
274         r_f_c2544_profile = IXIARFC2544Profile(self.TRAFFIC_PROFILE)
275         r_f_c2544_profile.rate = 100
276         mac = {"src_mac_0": "00:00:00:00:00:01",
277                "src_mac_1": "00:00:00:00:00:02",
278                "src_mac_2": "00:00:00:00:00:02",
279                "dst_mac_0": "00:00:00:00:00:03",
280                "dst_mac_1": "00:00:00:00:00:04",
281                "dst_mac_2": "00:00:00:00:00:04"}
282         result = r_f_c2544_profile._get_ixia_traffic_profile(
283             self.PROFILE, mac, xfile="tmp", static_traffic=STATIC_TRAFFIC)
284         self.assertIsNotNone(result)
285
286     @mock.patch("yardstick.network_services.traffic_profile.ixia_rfc2544.open")
287     def test_get_ixia_traffic_profile_v6(self, mock_open):
288         traffic_generator = mock.Mock(autospec=TrexProfile)
289         traffic_generator.my_ports = [0, 1]
290         traffic_generator.priv_ports = [-1]
291         traffic_generator.pub_ports = [1]
292         traffic_generator.client = \
293             mock.Mock(return_value=True)
294         STATIC_TRAFFIC = {
295             "private": {
296                 "id": 1,
297                 "bidir": "False",
298                 "duration": 60,
299                 "iload": "100",
300                 "outer_l2": {
301                     "dstmac": "00:00:00:00:00:03",
302                     "framesPerSecond": True,
303                     "framesize": 64,
304                     "srcmac": "00:00:00:00:00:01"
305                 },
306                 "outer_l3": {
307                     "dscp": 0,
308                     "dstip4": "152.16.40.20",
309                     "proto": "udp",
310                     "srcip4": "152.16.100.20",
311                     "ttl": 32
312                 },
313                 "outer_l3v4": {
314                     "dscp": 0,
315                     "dstip4": "152.16.40.20",
316                     "proto": "udp",
317                     "srcip4": "152.16.100.20",
318                     "ttl": 32
319                 },
320                 "outer_l3v6": {
321                     "count": 1024,
322                     "dscp": 0,
323                     "dstip4": "152.16.100.20",
324                     "proto": "udp",
325                     "srcip4": "152.16.40.20",
326                     "ttl": 32
327                 },
328                 "outer_l4": {
329                     "dstport": "2001",
330                     "srcport": "1234"
331                 },
332                 "traffic_type": "continuous"
333             },
334             "public": {
335                 "id": 2,
336                 "bidir": "False",
337                 "duration": 60,
338                 "iload": "100",
339                 "outer_l2": {
340                     "dstmac": "00:00:00:00:00:04",
341                     "framesPerSecond": True,
342                     "framesize": 64,
343                     "srcmac": "00:00:00:00:00:01"
344                 },
345                 "outer_l3": {
346                     "count": 1024,
347                     "dscp": 0,
348                     "dstip4": "152.16.100.20",
349                     "proto": "udp",
350                     "srcip4": "152.16.40.20",
351                     "ttl": 32
352                 },
353                 "outer_l3v4": {
354                     "count": 1024,
355                     "dscp": 0,
356                     "dstip4": "152.16.100.20",
357                     "proto": "udp",
358                     "srcip4": "152.16.40.20",
359                     "ttl": 32
360                 },
361                 "outer_l3v6": {
362                     "count": 1024,
363                     "dscp": 0,
364                     "dstip4": "152.16.100.20",
365                     "proto": "udp",
366                     "srcip4": "152.16.40.20",
367                     "ttl": 32
368                 },
369                 "outer_l4": {
370                     "dstport": "1234",
371                     "srcport": "2001"
372                 },
373                 "traffic_type": "continuous"
374             }
375         }
376         ixia_rfc2544.STATIC_TRAFFIC = STATIC_TRAFFIC
377
378         r_f_c2544_profile = IXIARFC2544Profile(self.TRAFFIC_PROFILE)
379         r_f_c2544_profile.rate = 100
380         mac = {"src_mac_0": "00:00:00:00:00:01",
381                "src_mac_1": "00:00:00:00:00:02",
382                "src_mac_2": "00:00:00:00:00:02",
383                "dst_mac_0": "00:00:00:00:00:03",
384                "dst_mac_1": "00:00:00:00:00:04",
385                "dst_mac_2": "00:00:00:00:00:04"}
386         profile_data = {'description': 'Traffic profile to run RFC2544',
387                         'name': 'rfc2544',
388                         'traffic_profile':
389                         {'traffic_type': 'IXIARFC2544Profile',
390                          'frame_rate': 100},
391                         'public':
392                         {'ipv4':
393                          {'outer_l2': {'framesize':
394                                        {'64B': '100', '1518B': '0',
395                                         '128B': '0', '1400B': '0',
396                                         '256B': '0', '373b': '0',
397                                         '570B': '0'}},
398                           'outer_l3v6': {'dstip6': '1.1.1.1-1.15.255.255',
399                                          'proto': 'udp',
400                                          'srcip6': '90.90.1.1-90.105.255.255',
401                                          'dscp': 0, 'ttl': 32},
402                           'outer_l4': {'srcport': '2001',
403                                        'dsrport': '1234'}}},
404                         'private': {'ipv4':
405                                     {'outer_l2': {'framesize':
406                                                   {'64B': '100', '1518B': '0',
407                                                    '128B': '0', '1400B': '0',
408                                                    '256B': '0', '373b': '0',
409                                                    '570B': '0'}},
410                                      'outer_l3v6':
411                                      {'dstip6': '9.9.1.1-90.105.255.255',
412                                       'proto': 'udp',
413                                       'srcip6': '1.1.1.1-1.15.255.255',
414                                       'dscp': 0, 'ttl': 32},
415                                      'outer_l4': {'dstport': '2001',
416                                                   'srcport': '1234'}}},
417                         'schema': 'isb:traffic_profile:0.1'}
418         result = r_f_c2544_profile._get_ixia_traffic_profile(
419             profile_data, mac, static_traffic=STATIC_TRAFFIC)
420         self.assertIsNotNone(result)
421
422     def test__ixia_traffic_generate(self):
423         traffic_generator = mock.Mock(autospec=TrexProfile)
424         traffic_generator.my_ports = [0, 1]
425         traffic_generator.priv_ports = [-1]
426         traffic_generator.pub_ports = [1]
427         traffic_generator.client = \
428             mock.Mock(return_value=True)
429         traffic = {"public": {'iload': 10},
430                    "private": {'iload': 10}}
431         ixia_obj = mock.MagicMock()
432         r_f_c2544_profile = IXIARFC2544Profile(self.TRAFFIC_PROFILE)
433         r_f_c2544_profile.rate = 100
434         result = r_f_c2544_profile._ixia_traffic_generate(traffic_generator,
435                                                           traffic, ixia_obj)
436         self.assertIsNone(result)
437
438     def test_execute(self):
439         traffic_generator = mock.Mock(autospec=TrexProfile)
440         traffic_generator.my_ports = [0, 1]
441         traffic_generator.priv_ports = [-1]
442         traffic_generator.pub_ports = [1]
443         traffic_generator.client = \
444             mock.Mock(return_value=True)
445         r_f_c2544_profile = IXIARFC2544Profile(self.TRAFFIC_PROFILE)
446         r_f_c2544_profile.first_run = True
447         r_f_c2544_profile.params = {"public": {'iload': 10},
448                                     "private": {'iload': 10}}
449
450         r_f_c2544_profile.get_streams = mock.Mock()
451         r_f_c2544_profile.full_profile = {}
452         r_f_c2544_profile._get_ixia_traffic_profile = mock.Mock()
453         r_f_c2544_profile.get_multiplier = mock.Mock()
454         r_f_c2544_profile._ixia_traffic_generate = mock.Mock()
455         ixia_obj = mock.MagicMock()
456         self.assertEqual(None, r_f_c2544_profile.execute(traffic_generator,
457                                                          ixia_obj))
458
459     def test_get_drop_percentage(self):
460         traffic_generator = mock.Mock(autospec=TrexProfile)
461         traffic_generator.my_ports = [0, 1]
462         traffic_generator.priv_ports = [0]
463         traffic_generator.pub_ports = [1]
464         traffic_generator.client = \
465             mock.Mock(return_value=True)
466         r_f_c2544_profile = IXIARFC2544Profile(self.TRAFFIC_PROFILE)
467         r_f_c2544_profile.params = self.PROFILE
468         ixia_obj = mock.MagicMock()
469         r_f_c2544_profile.execute = mock.Mock()
470         r_f_c2544_profile._get_ixia_traffic_profile = mock.Mock()
471         r_f_c2544_profile._ixia_traffic_generate = mock.Mock()
472         r_f_c2544_profile.get_multiplier = mock.Mock()
473         r_f_c2544_profile.tmp_throughput = 0
474         r_f_c2544_profile.tmp_drop = 0
475         r_f_c2544_profile.full_profile = {}
476         samples = {}
477         for ifname in range(1):
478             name = "xe{}".format(ifname)
479             samples[name] = {"rx_throughput_fps": 20,
480                              "tx_throughput_fps": 20,
481                              "rx_throughput_mbps": 10,
482                              "tx_throughput_mbps": 10,
483                              "RxThroughput": 10,
484                              "TxThroughput": 10,
485                              "in_packets": 1000,
486                              "out_packets": 1000}
487         tol_min = 100.0
488         tolerance = 0.0
489         self.assertIsNotNone(r_f_c2544_profile.get_drop_percentage(
490                              traffic_generator, samples,
491                              tol_min, tolerance, ixia_obj))
492
493     def test_get_drop_percentage_update(self):
494         traffic_generator = mock.Mock(autospec=TrexProfile)
495         traffic_generator.my_ports = [0, 1]
496         traffic_generator.priv_ports = [0]
497         traffic_generator.pub_ports = [1]
498         traffic_generator.client = \
499             mock.Mock(return_value=True)
500         r_f_c2544_profile = IXIARFC2544Profile(self.TRAFFIC_PROFILE)
501         r_f_c2544_profile.params = self.PROFILE
502         ixia_obj = mock.MagicMock()
503         r_f_c2544_profile.execute = mock.Mock()
504         r_f_c2544_profile._get_ixia_traffic_profile = mock.Mock()
505         r_f_c2544_profile._ixia_traffic_generate = mock.Mock()
506         r_f_c2544_profile.get_multiplier = mock.Mock()
507         r_f_c2544_profile.tmp_throughput = 0
508         r_f_c2544_profile.tmp_drop = 0
509         r_f_c2544_profile.full_profile = {}
510         samples = {}
511         for ifname in range(1):
512             name = "xe{}".format(ifname)
513             samples[name] = {"rx_throughput_fps": 20,
514                              "tx_throughput_fps": 20,
515                              "rx_throughput_mbps": 10,
516                              "tx_throughput_mbps": 10,
517                              "RxThroughput": 10,
518                              "TxThroughput": 10,
519                              "in_packets": 1000,
520                              "out_packets": 1002}
521         tol_min = 0.0
522         tolerance = 1.0
523         self.assertIsNotNone(r_f_c2544_profile.get_drop_percentage(
524                              traffic_generator, samples,
525                              tol_min, tolerance, ixia_obj))
526
527     def test_get_drop_percentage_div_zero(self):
528         traffic_generator = mock.Mock(autospec=TrexProfile)
529         traffic_generator.my_ports = [0, 1]
530         traffic_generator.priv_ports = [0]
531         traffic_generator.pub_ports = [1]
532         traffic_generator.client = \
533             mock.Mock(return_value=True)
534         r_f_c2544_profile = IXIARFC2544Profile(self.TRAFFIC_PROFILE)
535         r_f_c2544_profile.params = self.PROFILE
536         ixia_obj = mock.MagicMock()
537         r_f_c2544_profile.execute = mock.Mock()
538         r_f_c2544_profile._get_ixia_traffic_profile = mock.Mock()
539         r_f_c2544_profile._ixia_traffic_generate = mock.Mock()
540         r_f_c2544_profile.get_multiplier = mock.Mock()
541         r_f_c2544_profile.tmp_throughput = 0
542         r_f_c2544_profile.tmp_drop = 0
543         r_f_c2544_profile.full_profile = {}
544         samples = {}
545         for ifname in range(1):
546             name = "xe{}".format(ifname)
547             samples[name] = {"rx_throughput_fps": 20,
548                              "tx_throughput_fps": 20,
549                              "rx_throughput_mbps": 10,
550                              "tx_throughput_mbps": 10,
551                              "RxThroughput": 10,
552                              "TxThroughput": 10,
553                              "in_packets": 1000,
554                              "out_packets": 0}
555         tol_min = 0.0
556         tolerance = 0.0
557         r_f_c2544_profile.tmp_throughput = 0
558         self.assertIsNotNone(r_f_c2544_profile.get_drop_percentage(
559                              traffic_generator, samples,
560                              tol_min, tolerance, ixia_obj))
561
562     def test_get_multiplier(self):
563         r_f_c2544_profile = IXIARFC2544Profile(self.TRAFFIC_PROFILE)
564         r_f_c2544_profile.max_rate = 100
565         r_f_c2544_profile.min_rate = 100
566         self.assertEqual("1.0", r_f_c2544_profile.get_multiplier())
567
568     def test_start_ixia_latency(self):
569         traffic_generator = mock.Mock(autospec=TrexProfile)
570         traffic_generator.my_ports = [0, 1]
571         traffic_generator.priv_ports = [0]
572         traffic_generator.pub_ports = [1]
573         traffic_generator.client = \
574             mock.Mock(return_value=True)
575         r_f_c2544_profile = IXIARFC2544Profile(self.TRAFFIC_PROFILE)
576         r_f_c2544_profile.max_rate = 100
577         r_f_c2544_profile.min_rate = 100
578         ixia_obj = mock.MagicMock()
579         r_f_c2544_profile._get_ixia_traffic_profile = \
580             mock.Mock(return_value={})
581         r_f_c2544_profile.full_profile = {}
582         r_f_c2544_profile._ixia_traffic_generate = mock.Mock()
583         self.assertEqual(
584             None,
585             r_f_c2544_profile.start_ixia_latency(traffic_generator,
586                                                  ixia_obj))
587
588
589 if __name__ == '__main__':
590     unittest.main()