ddd1828ae6eba40e52a6cc9603d32437e469d871
[yardstick.git] / yardstick / tests / unit / network_services / traffic_profile / test_ixia_rfc2544.py
1 # Copyright (c) 2016-2019 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 copy
16
17 import mock
18 import unittest
19 import collections
20
21 from yardstick.network_services.traffic_profile import ixia_rfc2544
22 from yardstick.network_services.traffic_profile import trex_traffic_profile
23
24
25 class TestIXIARFC2544Profile(unittest.TestCase):
26
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     }
38
39     PROFILE = {
40         'description': 'Traffic profile to run RFC2544 latency',
41         'name': 'rfc2544',
42         'traffic_profile': {
43             'traffic_type': 'IXIARFC2544Profile',
44             'frame_rate': 100},
45         ixia_rfc2544.IXIARFC2544Profile.DOWNLINK: {
46             'ipv4': {
47                 'outer_l2': {
48                     'framesize': {
49                         '64B': '100',
50                         '1518B': '0',
51                         '128B': '0',
52                         '1400B': '0',
53                         '256B': '0',
54                         '373b': '0',
55                         '570B': '0'}},
56                 'outer_l3v4': {
57                     'dstip4': '1.1.1.1-1.15.255.255',
58                     'proto': 'udp',
59                     'count': '1',
60                     'srcip4': '90.90.1.1-90.105.255.255',
61                     'dscp': 0,
62                     'ttl': 32},
63                 'outer_l4': {
64                     'srcport': '2001',
65                     'dsrport': '1234'}}},
66         ixia_rfc2544.IXIARFC2544Profile.UPLINK: {
67             'ipv4': {
68                 'outer_l2': {
69                     'framesize': {
70                         '64B': '100',
71                         '1518B': '0',
72                         '128B': '0',
73                         '1400B': '0',
74                         '256B': '0',
75                         '373b': '0',
76                         '570B': '0'}},
77                 'outer_l3v4': {
78                     'dstip4': '9.9.1.1-90.105.255.255',
79                     'proto': 'udp',
80                     'count': '1',
81                     'srcip4': '1.1.1.1-1.15.255.255',
82                     'dscp': 0,
83                     'ttl': 32},
84                 'outer_l4': {
85                     'dstport': '2001',
86                     'srcport': '1234'}}},
87         'schema': 'isb:traffic_profile:0.1'}
88
89     def test_get_ixia_traffic_profile_error(self):
90         traffic_generator = mock.Mock(
91             autospec=trex_traffic_profile.TrexProfile)
92         traffic_generator.my_ports = [0, 1]
93         traffic_generator.uplink_ports = [-1]
94         traffic_generator.downlink_ports = [1]
95         traffic_generator.client = \
96             mock.Mock(return_value=True)
97         STATIC_TRAFFIC = {
98             ixia_rfc2544.IXIARFC2544Profile.UPLINK: {
99                 "id": 1,
100                 "bidir": "False",
101                 "duration": 60,
102                 "iload": "100",
103                 "outer_l2": {
104                     "dstmac": "00:00:00:00:00:03",
105                     "framesPerSecond": True,
106                     "framesize": 64,
107                     "srcmac": "00:00:00:00:00:01"
108                 },
109                 "outer_l3": {
110                     "dscp": 0,
111                     "dstip4": "152.16.40.20",
112                     "proto": "udp",
113                     "srcip4": "152.16.100.20",
114                     "ttl": 32
115                 },
116                 "outer_l3v4": {
117                     "dscp": 0,
118                     "dstip4": "152.16.40.20",
119                     "proto": "udp",
120                     "srcip4": "152.16.100.20",
121                     "ttl": 32
122                 },
123                 "outer_l3v6": {
124                     "count": 1024,
125                     "dscp": 0,
126                     "dstip4": "152.16.100.20",
127                     "proto": "udp",
128                     "srcip4": "152.16.40.20",
129                     "ttl": 32
130                 },
131                 "outer_l4": {
132                     "dstport": "2001",
133                     "srcport": "1234"
134                 },
135                 "traffic_type": "continuous"
136             },
137             ixia_rfc2544.IXIARFC2544Profile.DOWNLINK: {
138                 "id": 2,
139                 "bidir": "False",
140                 "duration": 60,
141                 "iload": "100",
142                 "outer_l2": {
143                     "dstmac": "00:00:00:00:00:04",
144                     "framesPerSecond": True,
145                     "framesize": 64,
146                     "srcmac": "00:00:00:00:00:01"
147                 },
148                 "outer_l3": {
149                     "count": 1024,
150                     "dscp": 0,
151                     "dstip4": "152.16.100.20",
152                     "proto": "udp",
153                     "srcip4": "152.16.40.20",
154                     "ttl": 32
155                 },
156                 "outer_l3v4": {
157                     "count": 1024,
158                     "dscp": 0,
159                     "dstip4": "152.16.100.20",
160                     "proto": "udp",
161                     "srcip4": "152.16.40.20",
162                     "ttl": 32
163                 },
164                 "outer_l3v6": {
165                     "count": 1024,
166                     "dscp": 0,
167                     "dstip4": "152.16.100.20",
168                     "proto": "udp",
169                     "srcip4": "152.16.40.20",
170                     "ttl": 32
171                 },
172                 "outer_l4": {
173                     "dstport": "1234",
174                     "srcport": "2001"
175                 },
176                 "traffic_type": "continuous"
177             }
178         }
179         ixia_rfc2544.STATIC_TRAFFIC = STATIC_TRAFFIC
180
181         r_f_c2544_profile = ixia_rfc2544.IXIARFC2544Profile(
182             self.TRAFFIC_PROFILE)
183         r_f_c2544_profile.rate = 100
184         mac = {"src_mac_0": "00:00:00:00:00:01",
185                "src_mac_1": "00:00:00:00:00:02",
186                "src_mac_2": "00:00:00:00:00:02",
187                "dst_mac_0": "00:00:00:00:00:03",
188                "dst_mac_1": "00:00:00:00:00:04",
189                "dst_mac_2": "00:00:00:00:00:04"}
190         result = r_f_c2544_profile._get_ixia_traffic_profile(self.PROFILE, mac)
191         self.assertIsNotNone(result)
192
193     def test_get_ixia_traffic_profile(self):
194         traffic_generator = mock.Mock(
195             autospec=trex_traffic_profile.TrexProfile)
196         traffic_generator.my_ports = [0, 1]
197         traffic_generator.uplink_ports = [-1]
198         traffic_generator.downlink_ports = [1]
199         traffic_generator.client = \
200             mock.Mock(return_value=True)
201         STATIC_TRAFFIC = {
202             ixia_rfc2544.IXIARFC2544Profile.UPLINK: {
203                 "id": 1,
204                 "bidir": "False",
205                 "duration": 60,
206                 "iload": "100",
207                 "outer_l2": {
208                     "dstmac": "00:00:00:00:00:03",
209                     "framesPerSecond": True,
210                     "framesize": 64,
211                     "srcmac": "00:00:00:00:00:01"
212                 },
213                 "outer_l3": {
214                     "dscp": 0,
215                     "dstip4": "152.16.40.20",
216                     "proto": "udp",
217                     "srcip4": "152.16.100.20",
218                     "ttl": 32
219                 },
220                 "outer_l3v4": {
221                     "dscp": 0,
222                     "dstip4": "152.16.40.20",
223                     "proto": "udp",
224                     "srcip4": "152.16.100.20",
225                     "ttl": 32,
226                     "count": "1"
227                 },
228                 "outer_l3v6": {
229                     "dscp": 0,
230                     "dstip4": "152.16.100.20",
231                     "proto": "udp",
232                     "srcip4": "152.16.40.20",
233                     "ttl": 32,
234                 },
235                 "outer_l4": {
236                     "dstport": "2001",
237                     "srcport": "1234",
238                     "count": "1"
239                 },
240                 "traffic_type": "continuous"
241             },
242             ixia_rfc2544.IXIARFC2544Profile.DOWNLINK: {
243                 "id": 2,
244                 "bidir": "False",
245                 "duration": 60,
246                 "iload": "100",
247                 "outer_l2": {
248                     "dstmac": "00:00:00:00:00:04",
249                     "framesPerSecond": True,
250                     "framesize": 64,
251                     "srcmac": "00:00:00:00:00:01"
252                 },
253                 "outer_l3": {
254                     "count": 1024,
255                     "dscp": 0,
256                     "dstip4": "152.16.100.20",
257                     "proto": "udp",
258                     "srcip4": "152.16.40.20",
259                     "ttl": 32
260                 },
261                 "outer_l3v4": {
262                     "dscp": 0,
263                     "dstip4": "152.16.100.20",
264                     "proto": "udp",
265                     "srcip4": "152.16.40.20",
266                     "ttl": 32,
267                 },
268                 "outer_l3v6": {
269                     "dscp": 0,
270                     "dstip4": "152.16.100.20",
271                     "proto": "udp",
272                     "srcip4": "152.16.40.20",
273                     "ttl": 32,
274                 },
275                 "outer_l4": {
276                     "dstport": "1234",
277                     "srcport": "2001",
278                     "count": "1"
279                 },
280                 "traffic_type": "continuous"
281             }
282         }
283         ixia_rfc2544.STATIC_TRAFFIC = STATIC_TRAFFIC
284
285         r_f_c2544_profile = ixia_rfc2544.IXIARFC2544Profile(
286             self.TRAFFIC_PROFILE)
287         r_f_c2544_profile.rate = 100
288         mac = {"src_mac_0": "00:00:00:00:00:01",
289                "src_mac_1": "00:00:00:00:00:02",
290                "src_mac_2": "00:00:00:00:00:02",
291                "dst_mac_0": "00:00:00:00:00:03",
292                "dst_mac_1": "00:00:00:00:00:04",
293                "dst_mac_2": "00:00:00:00:00:04"}
294         result = r_f_c2544_profile._get_ixia_traffic_profile(self.PROFILE, mac)
295         self.assertIsNotNone(result)
296
297     @mock.patch("yardstick.network_services.traffic_profile.ixia_rfc2544.open")
298     def test_get_ixia_traffic_profile_v6(self, *args):
299         traffic_generator = mock.Mock(
300             autospec=trex_traffic_profile.TrexProfile)
301         traffic_generator.my_ports = [0, 1]
302         traffic_generator.uplink_ports = [-1]
303         traffic_generator.downlink_ports = [1]
304         traffic_generator.client = \
305             mock.Mock(return_value=True)
306         STATIC_TRAFFIC = {
307             ixia_rfc2544.IXIARFC2544Profile.UPLINK: {
308                 "id": 1,
309                 "bidir": "False",
310                 "duration": 60,
311                 "iload": "100",
312                 "outer_l2": {
313                     "dstmac": "00:00:00:00:00:03",
314                     "framesPerSecond": True,
315                     "framesize": 64,
316                     "srcmac": "00:00:00:00:00:01"
317                 },
318                 "outer_l3": {
319                     "dscp": 0,
320                     "dstip4": "152.16.40.20",
321                     "proto": "udp",
322                     "srcip4": "152.16.100.20",
323                     "ttl": 32
324                 },
325                 "outer_l3v4": {
326                     "dscp": 0,
327                     "dstip4": "152.16.40.20",
328                     "proto": "udp",
329                     "srcip4": "152.16.100.20",
330                     "ttl": 32
331                 },
332                 "outer_l3v6": {
333                     "count": 1024,
334                     "dscp": 0,
335                     "dstip4": "152.16.100.20",
336                     "proto": "udp",
337                     "srcip4": "152.16.40.20",
338                     "ttl": 32
339                 },
340                 "outer_l4": {
341                     "dstport": "2001",
342                     "srcport": "1234"
343                 },
344                 "traffic_type": "continuous"
345             },
346             ixia_rfc2544.IXIARFC2544Profile.DOWNLINK: {
347                 "id": 2,
348                 "bidir": "False",
349                 "duration": 60,
350                 "iload": "100",
351                 "outer_l2": {
352                     "dstmac": "00:00:00:00:00:04",
353                     "framesPerSecond": True,
354                     "framesize": 64,
355                     "srcmac": "00:00:00:00:00:01"
356                 },
357                 "outer_l3": {
358                     "count": 1024,
359                     "dscp": 0,
360                     "dstip4": "152.16.100.20",
361                     "proto": "udp",
362                     "srcip4": "152.16.40.20",
363                     "ttl": 32
364                 },
365                 "outer_l3v4": {
366                     "count": 1024,
367                     "dscp": 0,
368                     "dstip4": "152.16.100.20",
369                     "proto": "udp",
370                     "srcip4": "152.16.40.20",
371                     "ttl": 32
372                 },
373                 "outer_l3v6": {
374                     "count": 1024,
375                     "dscp": 0,
376                     "dstip4": "152.16.100.20",
377                     "proto": "udp",
378                     "srcip4": "152.16.40.20",
379                     "ttl": 32
380                 },
381                 "outer_l4": {
382                     "dstport": "1234",
383                     "srcport": "2001"
384                 },
385                 "traffic_type": "continuous"
386             }
387         }
388         ixia_rfc2544.STATIC_TRAFFIC = STATIC_TRAFFIC
389
390         r_f_c2544_profile = ixia_rfc2544.IXIARFC2544Profile(
391             self.TRAFFIC_PROFILE)
392         r_f_c2544_profile.rate = 100
393         mac = {"src_mac_0": "00:00:00:00:00:01",
394                "src_mac_1": "00:00:00:00:00:02",
395                "src_mac_2": "00:00:00:00:00:02",
396                "dst_mac_0": "00:00:00:00:00:03",
397                "dst_mac_1": "00:00:00:00:00:04",
398                "dst_mac_2": "00:00:00:00:00:04"}
399         profile_data = {'description': 'Traffic profile to run RFC2544',
400                         'name': 'rfc2544',
401                         'traffic_profile':
402                         {'traffic_type': 'IXIARFC2544Profile',
403                          'frame_rate': 100},
404                         ixia_rfc2544.IXIARFC2544Profile.DOWNLINK:
405                         {'ipv4':
406                          {'outer_l2': {'framesize':
407                                        {'64B': '100', '1518B': '0',
408                                         '128B': '0', '1400B': '0',
409                                         '256B': '0', '373b': '0',
410                                         '570B': '0'}},
411                           'outer_l3v4': {'dstip4': '1.1.1.1-1.15.255.255',
412                                          'proto': 'udp', 'count': '1',
413                                          'srcip4': '90.90.1.1-90.105.255.255',
414                                          'dscp': 0, 'ttl': 32},
415                           'outer_l3v6': {'dstip6': '1.1.1.1-1.15.255.255',
416                                          'proto': 'udp', 'count': '1',
417                                          'srcip6': '90.90.1.1-90.105.255.255',
418                                          'dscp': 0, 'ttl': 32},
419                           'outer_l4': {'srcport': '2001',
420                                        'dsrport': '1234'}}},
421                         ixia_rfc2544.IXIARFC2544Profile.UPLINK: {'ipv4':
422                                                     {'outer_l2': {'framesize':
423                                                                   {'64B': '100', '1518B': '0',
424                                                                    '128B': '0', '1400B': '0',
425                                                                    '256B': '0', '373b': '0',
426                                                                    '570B': '0'}},
427                                                         'outer_l3v4':
428                                                         {'dstip4': '9.9.1.1-90.105.255.255',
429                                                          'proto': 'udp', 'count': '1',
430                                                          'srcip4': '1.1.1.1-1.15.255.255',
431                                                          'dscp': 0, 'ttl': 32},
432                                                         'outer_l3v6':
433                                                         {'dstip6': '9.9.1.1-90.105.255.255',
434                                                          'proto': 'udp', 'count': '1',
435                                                          'srcip6': '1.1.1.1-1.15.255.255',
436                                                          'dscp': 0, 'ttl': 32},
437
438                                                         'outer_l4': {'dstport': '2001',
439                                                                      'srcport': '1234'}}},
440                         'schema': 'isb:traffic_profile:0.1'}
441         result = r_f_c2544_profile._get_ixia_traffic_profile(profile_data, mac)
442         self.assertIsNotNone(result)
443
444     def test__init__(self):
445         t_profile_data = copy.deepcopy(self.TRAFFIC_PROFILE)
446         t_profile_data['traffic_profile']['frame_rate'] = 12345678
447         r_f_c2544_profile = ixia_rfc2544.IXIARFC2544Profile(t_profile_data)
448         self.assertEqual(12345678, r_f_c2544_profile.rate)
449
450     def test__get_ip_and_mask_range(self):
451         ip_range = '1.2.0.2-1.2.255.254'
452         r_f_c2544_profile = ixia_rfc2544.IXIARFC2544Profile(
453             self.TRAFFIC_PROFILE)
454         ip, mask = r_f_c2544_profile._get_ip_and_mask(ip_range)
455         self.assertEqual('1.2.0.2', ip)
456         self.assertEqual(16, mask)
457
458     def test__get_ip_and_mask_single(self):
459         ip_range = '192.168.1.10'
460         r_f_c2544_profile = ixia_rfc2544.IXIARFC2544Profile(
461             self.TRAFFIC_PROFILE)
462         ip, mask = r_f_c2544_profile._get_ip_and_mask(ip_range)
463         self.assertEqual('192.168.1.10', ip)
464         self.assertIsNone(mask)
465
466     def test__get_fixed_and_mask_range(self):
467         fixed_mask = '8-48'
468         r_f_c2544_profile = ixia_rfc2544.IXIARFC2544Profile(
469             self.TRAFFIC_PROFILE)
470         fixed, mask = r_f_c2544_profile._get_fixed_and_mask(fixed_mask)
471         self.assertEqual(8, fixed)
472         self.assertEqual(48, mask)
473
474     def test__get_fixed_and_mask_single(self):
475         fixed_mask = 1234
476         r_f_c2544_profile = ixia_rfc2544.IXIARFC2544Profile(
477             self.TRAFFIC_PROFILE)
478         fixed, mask = r_f_c2544_profile._get_fixed_and_mask(fixed_mask)
479         self.assertEqual(1234, fixed)
480         self.assertEqual(0, mask)
481
482     def test__get_ixia_traffic_profile_default_args(self):
483         r_f_c2544_profile = ixia_rfc2544.IXIARFC2544Profile(
484             self.TRAFFIC_PROFILE)
485
486         expected = {}
487         result = r_f_c2544_profile._get_ixia_traffic_profile({})
488         self.assertDictEqual(result, expected)
489
490     @mock.patch.object(ixia_rfc2544.IXIARFC2544Profile,
491                        '_update_traffic_tracking_options')
492     def test__ixia_traffic_generate(self, mock_upd_tracking_opts):
493         traffic_generator = mock.Mock(
494             autospec=trex_traffic_profile.TrexProfile)
495         traffic_generator.networks = {
496             "uplink_0": ["xe0"],
497             "downlink_0": ["xe1"],
498         }
499         traffic_generator.client = \
500             mock.Mock(return_value=True)
501         traffic = {ixia_rfc2544.IXIARFC2544Profile.DOWNLINK: {'iload': 10},
502                    ixia_rfc2544.IXIARFC2544Profile.UPLINK: {'iload': 10}}
503         ixia_obj = mock.MagicMock()
504         r_f_c2544_profile = ixia_rfc2544.IXIARFC2544Profile(
505             self.TRAFFIC_PROFILE)
506         r_f_c2544_profile.rate = 100
507         result = r_f_c2544_profile._ixia_traffic_generate(traffic, ixia_obj,
508                                                           traffic_generator)
509         self.assertIsNone(result)
510         mock_upd_tracking_opts.assert_called_once_with(traffic_generator)
511
512     def test__update_traffic_tracking_options(self):
513         mock_traffic_gen = mock.Mock()
514         rfc2544_profile = ixia_rfc2544.IXIARFC2544Profile(self.TRAFFIC_PROFILE)
515         rfc2544_profile._update_traffic_tracking_options(mock_traffic_gen)
516         mock_traffic_gen.update_tracking_options.assert_called_once()
517
518     def test__get_framesize(self):
519         traffic_profile = {
520             'uplink_0': {'ipv4': {'outer_l2': {'framesize': {'64B': 100}}}},
521             'downlink_0': {'ipv4': {'outer_l2': {'framesize': {'64B': 100}}}},
522             'uplink_1': {'ipv4': {'outer_l2': {'framesize': {'64B': 100}}}},
523             'downlink_1': {'ipv4': {'outer_l2': {'framesize': {'64B': 100}}}}
524         }
525         rfc2544_profile = ixia_rfc2544.IXIARFC2544Profile(self.TRAFFIC_PROFILE)
526         rfc2544_profile.params = traffic_profile
527         result = rfc2544_profile._get_framesize()
528         self.assertEqual(result, '64B')
529
530     def test__get_framesize_IMIX_traffic(self):
531         traffic_profile = {
532             'uplink_0': {'ipv4': {'outer_l2': {'framesize': {'64B': 50,
533                                                              '128B': 50}}}},
534             'downlink_0': {'ipv4': {'outer_l2': {'framesize': {'64B': 50,
535                                                                '128B': 50}}}},
536             'uplink_1': {'ipv4': {'outer_l2': {'framesize': {'64B': 50,
537                                                              '128B': 50}}}},
538             'downlink_1': {'ipv4': {'outer_l2': {'framesize': {'64B': 50,
539                                                                '128B': 50}}}}
540         }
541         rfc2544_profile = ixia_rfc2544.IXIARFC2544Profile(self.TRAFFIC_PROFILE)
542         rfc2544_profile.params = traffic_profile
543         result = rfc2544_profile._get_framesize()
544         self.assertEqual(result, 'IMIX')
545
546     def test__get_framesize_zero_pkt_size_weight(self):
547         traffic_profile = {
548             'uplink_0': {'ipv4': {'outer_l2': {'framesize': {'64B': 0}}}},
549             'downlink_0': {'ipv4': {'outer_l2': {'framesize': {'64B': 0}}}},
550             'uplink_1': {'ipv4': {'outer_l2': {'framesize': {'64B': 0}}}},
551             'downlink_1': {'ipv4': {'outer_l2': {'framesize': {'64B': 0}}}}
552         }
553         rfc2544_profile = ixia_rfc2544.IXIARFC2544Profile(self.TRAFFIC_PROFILE)
554         rfc2544_profile.params = traffic_profile
555         result = rfc2544_profile._get_framesize()
556         self.assertEqual(result, '')
557
558     def test_execute_traffic_first_run(self):
559         rfc2544_profile = ixia_rfc2544.IXIARFC2544Profile(self.TRAFFIC_PROFILE)
560         rfc2544_profile.first_run = True
561         rfc2544_profile.rate = 50
562         traffic_gen = mock.Mock()
563         traffic_gen.rfc_helper.iteration.value = 0
564         with mock.patch.object(rfc2544_profile, '_get_ixia_traffic_profile') \
565                 as mock_get_tp, \
566                 mock.patch.object(rfc2544_profile, '_ixia_traffic_generate') \
567                 as mock_tgenerate:
568             mock_get_tp.return_value = 'fake_tprofile'
569             output = rfc2544_profile.execute_traffic(traffic_gen,
570                                                      ixia_obj=mock.ANY)
571
572         self.assertTrue(output)
573         self.assertFalse(rfc2544_profile.first_run)
574         self.assertEqual(50, rfc2544_profile.max_rate)
575         self.assertEqual(0, rfc2544_profile.min_rate)
576         mock_get_tp.assert_called_once()
577         mock_tgenerate.assert_called_once()
578
579     def test_execute_traffic_not_first_run(self):
580         rfc2544_profile = ixia_rfc2544.IXIARFC2544Profile(self.TRAFFIC_PROFILE)
581         rfc2544_profile.first_run = False
582         rfc2544_profile.max_rate = 70
583         rfc2544_profile.min_rate = 0
584         traffic_gen = mock.Mock()
585         traffic_gen.rfc_helper.iteration.value = 0
586         with mock.patch.object(rfc2544_profile, '_get_ixia_traffic_profile') \
587                 as mock_get_tp, \
588                 mock.patch.object(rfc2544_profile, '_ixia_traffic_generate') \
589                 as mock_tgenerate:
590             mock_get_tp.return_value = 'fake_tprofile'
591             rfc2544_profile.full_profile = mock.ANY
592             output = rfc2544_profile.execute_traffic(traffic_gen,
593                                                      ixia_obj=mock.ANY)
594
595         self.assertFalse(output)
596         self.assertEqual(35.0, rfc2544_profile.rate)
597         mock_get_tp.assert_called_once()
598         mock_tgenerate.assert_called_once()
599
600     def test_update_traffic_profile(self):
601         traffic_generator = mock.Mock(
602             autospec=trex_traffic_profile.TrexProfile)
603         traffic_generator.networks = {
604             "uplink_0": ["xe0"],  # private, one value for intfs
605             "downlink_0": ["xe1", "xe2"],  # public, two values for intfs
606             "downlink_1": ["xe3"],  # not in TRAFFIC PROFILE
607             "tenant_0": ["xe4"],  # not public or private
608         }
609
610         ports_expected = [8, 3, 5]
611         traffic_generator.vnfd_helper.port_num.side_effect = ports_expected
612         traffic_generator.client.return_value = True
613
614         traffic_profile = copy.deepcopy(self.TRAFFIC_PROFILE)
615         traffic_profile.update({
616             "uplink_0": ["xe0"],
617             "downlink_0": ["xe1", "xe2"],
618         })
619
620         r_f_c2544_profile = ixia_rfc2544.IXIARFC2544Profile(traffic_profile)
621         r_f_c2544_profile.full_profile = {}
622         r_f_c2544_profile.get_streams = mock.Mock()
623
624         self.assertIsNone(
625             r_f_c2544_profile.update_traffic_profile(traffic_generator))
626         self.assertEqual(r_f_c2544_profile.ports, ports_expected)
627
628     def test_get_drop_percentage_completed(self):
629         samples = {'iface_name_1':
630                        {'InPackets': 1000, 'OutPackets': 1000,
631                         'InBytes': 64000, 'OutBytes': 64000,
632                         'LatencyAvg': 20,
633                         'LatencyMin': 15,
634                         'LatencyMax': 25},
635                    'iface_name_2':
636                        {'InPackets': 1005, 'OutPackets': 1007,
637                         'InBytes': 64320, 'OutBytes': 64448,
638                         'LatencyAvg': 23,
639                         'LatencyMin': 13,
640                         'LatencyMax': 28}
641                    }
642         rfc2544_profile = ixia_rfc2544.IXIARFC2544Profile(self.TRAFFIC_PROFILE)
643         rfc2544_profile.rate = 100.0
644         rfc2544_profile._get_next_rate = mock.Mock(return_value=100.0)
645         rfc2544_profile._get_framesize = mock.Mock(return_value='64B')
646         completed, samples = rfc2544_profile.get_drop_percentage(
647             samples, 0, 1, 4, 0.1)
648         self.assertTrue(completed)
649         self.assertEqual(66.9, samples['TxThroughput'])
650         self.assertEqual(66.833, samples['RxThroughput'])
651         self.assertEqual(0.099651, samples['DropPercentage'])
652         self.assertEqual(21.5, samples['LatencyAvg'])
653         self.assertEqual(13.0, samples['LatencyMin'])
654         self.assertEqual(28.0, samples['LatencyMax'])
655         self.assertEqual(100.0, samples['Rate'])
656         self.assertEqual('64B', samples['PktSize'])
657
658     def test_get_drop_percentage_over_drop_percentage(self):
659         samples = {'iface_name_1':
660                        {'InPackets': 1000, 'OutPackets': 1000,
661                         'InBytes': 64000, 'OutBytes': 64000,
662                         'LatencyAvg': 20,
663                         'LatencyMin': 15,
664                         'LatencyMax': 25},
665                    'iface_name_2':
666                        {'InPackets': 1005, 'OutPackets': 1007,
667                         'InBytes': 64320, 'OutBytes': 64448,
668                         'LatencyAvg': 20,
669                         'LatencyMin': 15,
670                         'LatencyMax': 25}
671                    }
672         rfc2544_profile = ixia_rfc2544.IXIARFC2544Profile(self.TRAFFIC_PROFILE)
673         rfc2544_profile.rate = 1000
674         rfc2544_profile._get_next_rate = mock.Mock(return_value=50.0)
675         completed, samples = rfc2544_profile.get_drop_percentage(
676             samples, 0, 0.05, 4, 0.1)
677         self.assertFalse(completed)
678         self.assertEqual(66.9, samples['TxThroughput'])
679         self.assertEqual(66.833, samples['RxThroughput'])
680         self.assertEqual(0.099651, samples['DropPercentage'])
681         self.assertEqual(rfc2544_profile.rate, rfc2544_profile.max_rate)
682
683     def test_get_drop_percentage_under_drop_percentage(self):
684         samples = {'iface_name_1':
685                        {'InPackets': 1000, 'OutPackets': 1000,
686                         'InBytes': 64000, 'OutBytes': 64000,
687                         'LatencyAvg': 20,
688                         'LatencyMin': 15,
689                         'LatencyMax': 25},
690                    'iface_name_2':
691                        {'InPackets': 1005, 'OutPackets': 1007,
692                         'InBytes': 64320, 'OutBytes': 64448,
693                         'LatencyAvg': 20,
694                         'LatencyMin': 15,
695                         'LatencyMax': 25}
696                    }
697         rfc2544_profile = ixia_rfc2544.IXIARFC2544Profile(self.TRAFFIC_PROFILE)
698         rfc2544_profile.rate = 1000
699         rfc2544_profile._get_next_rate = mock.Mock(return_value=50.0)
700         completed, samples = rfc2544_profile.get_drop_percentage(
701             samples, 0.2, 1, 4, 0.1)
702         self.assertFalse(completed)
703         self.assertEqual(66.9, samples['TxThroughput'])
704         self.assertEqual(66.833, samples['RxThroughput'])
705         self.assertEqual(0.099651, samples['DropPercentage'])
706         self.assertEqual(rfc2544_profile.rate, rfc2544_profile.min_rate)
707
708     @mock.patch.object(ixia_rfc2544.LOG, 'info')
709     def test_get_drop_percentage_not_flow(self, *args):
710         samples = {'iface_name_1':
711                        {'InPackets': 1000, 'OutPackets': 0,
712                         'InBytes': 64000, 'OutBytes': 0,
713                         'LatencyAvg': 20,
714                         'LatencyMin': 15,
715                         'LatencyMax': 25},
716                    'iface_name_2':
717                        {'InPackets': 1005, 'OutPackets': 0,
718                         'InBytes': 64320, 'OutBytes': 0,
719                         'LatencyAvg': 20,
720                         'LatencyMin': 15,
721                         'LatencyMax': 25}
722                    }
723         rfc2544_profile = ixia_rfc2544.IXIARFC2544Profile(self.TRAFFIC_PROFILE)
724         rfc2544_profile.rate = 1000
725         rfc2544_profile._get_next_rate = mock.Mock(return_value=50.0)
726         completed, samples = rfc2544_profile.get_drop_percentage(
727             samples, 0.2, 1, 4, 0.1)
728         self.assertFalse(completed)
729         self.assertEqual(0.0, samples['TxThroughput'])
730         self.assertEqual(66.833, samples['RxThroughput'])
731         self.assertEqual(100, samples['DropPercentage'])
732         self.assertEqual(rfc2544_profile.rate, rfc2544_profile.max_rate)
733
734     def test_get_drop_percentage_first_run(self):
735         samples = {'iface_name_1':
736                        {'InPackets': 1000, 'OutPackets': 1000,
737                         'InBytes': 64000, 'OutBytes': 64000,
738                         'LatencyAvg': 20,
739                         'LatencyMin': 15,
740                         'LatencyMax': 25},
741                    'iface_name_2':
742                        {'InPackets': 1005, 'OutPackets': 1007,
743                         'InBytes': 64320, 'OutBytes': 64448,
744                         'LatencyAvg': 20,
745                         'LatencyMin': 15,
746                         'LatencyMax': 25}
747                    }
748         rfc2544_profile = ixia_rfc2544.IXIARFC2544Profile(self.TRAFFIC_PROFILE)
749         rfc2544_profile._get_next_rate = mock.Mock(return_value=50.0)
750         completed, samples = rfc2544_profile.get_drop_percentage(
751             samples, 0, 1, 4, 0.1, first_run=True)
752         self.assertTrue(completed)
753         self.assertEqual(66.9, samples['TxThroughput'])
754         self.assertEqual(66.833, samples['RxThroughput'])
755         self.assertEqual(0.099651, samples['DropPercentage'])
756         self.assertEqual(33.45, rfc2544_profile.rate)
757
758     def test_get_drop_percentage_resolution(self):
759         rfc2544_profile = ixia_rfc2544.IXIARFC2544Profile(self.TRAFFIC_PROFILE)
760         rfc2544_profile._get_next_rate = mock.Mock(return_value=0.1)
761         samples = {'iface_name_1':
762                        {'InPackets': 1000, 'OutPackets': 1000,
763                         'InBytes': 64000, 'OutBytes': 64000,
764                         'LatencyAvg': 20,
765                         'LatencyMin': 15,
766                         'LatencyMax': 25},
767                    'iface_name_2':
768                        {'InPackets': 1005, 'OutPackets': 1007,
769                         'InBytes': 64320, 'OutBytes': 64448,
770                         'LatencyAvg': 20,
771                         'LatencyMin': 15,
772                         'LatencyMax': 25}
773                    }
774         rfc2544_profile.rate = 0.19
775         completed, _ = rfc2544_profile.get_drop_percentage(
776             samples, 0, 0.05, 4, 0.1)
777         self.assertTrue(completed)
778
779         samples = {'iface_name_1':
780                        {'InPackets': 1000, 'OutPackets': 1000,
781                         'InBytes': 64000, 'OutBytes': 64000,
782                         'LatencyAvg': 20,
783                         'LatencyMin': 15,
784                         'LatencyMax': 25},
785                    'iface_name_2':
786                        {'InPackets': 1005, 'OutPackets': 1007,
787                         'InBytes': 64320, 'OutBytes': 64448,
788                         'LatencyAvg': 20,
789                         'LatencyMin': 15,
790                         'LatencyMax': 25}
791                    }
792         rfc2544_profile.rate = 0.5
793         completed, _ = rfc2544_profile.get_drop_percentage(
794             samples, 0, 0.05, 4, 0.1)
795         self.assertFalse(completed)
796
797
798 class TestIXIARFC2544PppoeScenarioProfile(unittest.TestCase):
799
800     TRAFFIC_PROFILE = {
801         "schema": "nsb:traffic_profile:0.1",
802         "name": "fixed",
803         "description": "Fixed traffic profile to run UDP traffic",
804         "traffic_profile": {
805             "traffic_type": "FixedTraffic",
806             "frame_rate": 100},
807         'uplink_0': {'ipv4': {'port': 'xe0', 'id': 1}},
808         'downlink_0': {'ipv4': {'port': 'xe2', 'id': 2}},
809         'uplink_1': {'ipv4': {'port': 'xe1', 'id': 3}},
810         'downlink_1': {'ipv4': {'port': 'xe2', 'id': 4}}
811     }
812
813     def setUp(self):
814         self.ixia_tp = ixia_rfc2544.IXIARFC2544PppoeScenarioProfile(
815             self.TRAFFIC_PROFILE)
816         self.ixia_tp.rate = 100.0
817         self.ixia_tp._get_next_rate = mock.Mock(return_value=50.0)
818         self.ixia_tp._get_framesize = mock.Mock(return_value='64B')
819
820     def test___init__(self):
821         self.assertIsInstance(self.ixia_tp.full_profile,
822                               collections.OrderedDict)
823
824     def test__get_flow_groups_params(self):
825         expected_tp = collections.OrderedDict([
826             ('uplink_0', {'ipv4': {'id': 1, 'port': 'xe0'}}),
827             ('downlink_0', {'ipv4': {'id': 2, 'port': 'xe2'}}),
828             ('uplink_1', {'ipv4': {'id': 3, 'port': 'xe1'}}),
829             ('downlink_1', {'ipv4': {'id': 4, 'port': 'xe2'}})])
830
831         self.ixia_tp._get_flow_groups_params()
832         self.assertDictEqual(self.ixia_tp.full_profile, expected_tp)
833
834     @mock.patch.object(ixia_rfc2544.IXIARFC2544PppoeScenarioProfile,
835                        '_get_flow_groups_params')
836     def test_update_traffic_profile(self, mock_get_flow_groups_params):
837         networks = {
838             'uplink_0': 'data1',
839             'downlink_0': 'data2',
840             'uplink_1': 'data3',
841             'downlink_1': 'data4'
842         }
843         ports = ['xe0', 'xe1', 'xe2', 'xe3']
844         mock_traffic_gen = mock.Mock()
845         mock_traffic_gen.networks = networks
846         mock_traffic_gen.vnfd_helper.port_num.side_effect = ports
847         self.ixia_tp.update_traffic_profile(mock_traffic_gen)
848         mock_get_flow_groups_params.assert_called_once()
849         self.assertEqual(self.ixia_tp.ports, ports)
850
851     def test__get_prio_flows_drop_percentage(self):
852
853         input_stats = {
854             '0': {
855                 'InPackets': 50,
856                 'OutPackets': 100,
857                 'Store-Forward_Avg_latency_ns': 10,
858                 'Store-Forward_Min_latency_ns': 10,
859                 'Store-Forward_Max_latency_ns': 10}}
860
861         result = self.ixia_tp._get_prio_flows_drop_percentage(input_stats)
862         self.assertIsNotNone(result['0'].get('DropPercentage'))
863         self.assertEqual(result['0'].get('DropPercentage'), 50.0)
864
865     def test__get_prio_flows_drop_percentage_traffic_not_flowing(self):
866         input_stats = {
867             '0': {
868                 'InPackets': 0,
869                 'OutPackets': 0,
870                 'Store-Forward_Avg_latency_ns': 0,
871                 'Store-Forward_Min_latency_ns': 0,
872                 'Store-Forward_Max_latency_ns': 0}}
873
874         result = self.ixia_tp._get_prio_flows_drop_percentage(input_stats)
875         self.assertIsNotNone(result['0'].get('DropPercentage'))
876         self.assertEqual(result['0'].get('DropPercentage'), 100)
877
878     def test__get_summary_pppoe_subs_counters(self):
879         input_stats = {
880             'xe0': {
881                 'OutPackets': 100,
882                 'SessionsUp': 4,
883                 'SessionsDown': 0,
884                 'SessionsNotStarted': 0,
885                 'SessionsTotal': 4},
886             'xe1': {
887                 'OutPackets': 100,
888                 'SessionsUp': 4,
889                 'SessionsDown': 0,
890                 'SessionsNotStarted': 0,
891                 'SessionsTotal': 4}
892         }
893
894         expected_stats = {
895             'SessionsUp': 8,
896             'SessionsDown': 0,
897             'SessionsNotStarted': 0,
898             'SessionsTotal': 8
899         }
900
901         res = self.ixia_tp._get_summary_pppoe_subs_counters(input_stats)
902         self.assertDictEqual(res, expected_stats)
903
904     @mock.patch.object(ixia_rfc2544.IXIARFC2544PppoeScenarioProfile,
905                        '_get_prio_flows_drop_percentage')
906     @mock.patch.object(ixia_rfc2544.IXIARFC2544PppoeScenarioProfile,
907                        '_get_summary_pppoe_subs_counters')
908     def test_get_drop_percentage(self, mock_get_pppoe_subs,
909                                  mock_sum_prio_drop_rate):
910         samples = {
911             'priority_stats': {
912                 '0': {
913                     'InPackets': 100,
914                     'OutPackets': 100,
915                     'InBytes': 6400,
916                     'OutBytes': 6400,
917                     'LatencyAvg': 10,
918                     'LatencyMin': 10,
919                     'LatencyMax': 10}},
920             'xe0': {
921                 'InPackets': 100,
922                 'OutPackets': 100,
923                 'InBytes': 6400,
924                 'OutBytes': 6400,
925                 'LatencyAvg': 10,
926                 'LatencyMin': 10,
927                 'LatencyMax': 10}}
928
929         mock_get_pppoe_subs.return_value = {'SessionsUp': 1}
930         mock_sum_prio_drop_rate.return_value = {'0': {'DropPercentage': 0.0}}
931
932         self.ixia_tp._get_framesize = mock.Mock(return_value='64B')
933         status, res = self.ixia_tp.get_drop_percentage(
934             samples, tol_min=0.0, tolerance=0.0001, precision=0,
935             resolution=0.1, first_run=True)
936         self.assertIsNotNone(res.get('DropPercentage'))
937         self.assertIsNotNone(res.get('Priority'))
938         self.assertIsNotNone(res.get('SessionsUp'))
939         self.assertEqual(res['DropPercentage'], 0.0)
940         self.assertEqual(res['Rate'], 100.0)
941         self.assertEqual(res['PktSize'], '64B')
942         self.assertTrue(status)
943         mock_sum_prio_drop_rate.assert_called_once()
944         mock_get_pppoe_subs.assert_called_once()
945
946     @mock.patch.object(ixia_rfc2544.IXIARFC2544PppoeScenarioProfile,
947                        '_get_prio_flows_drop_percentage')
948     @mock.patch.object(ixia_rfc2544.IXIARFC2544PppoeScenarioProfile,
949                        '_get_summary_pppoe_subs_counters')
950     def test_get_drop_percentage_failed_status(self, mock_get_pppoe_subs,
951                                                mock_sum_prio_drop_rate):
952         samples = {
953             'priority_stats': {
954                 '0': {
955                     'InPackets': 90,
956                     'OutPackets': 100,
957                     'InBytes': 5760,
958                     'OutBytes': 6400,
959                     'LatencyAvg': 10,
960                     'LatencyMin': 10,
961                     'LatencyMax': 10}},
962             'xe0': {
963                 'InPackets': 90,
964                 'OutPackets': 100,
965                 'InBytes': 5760,
966                 'OutBytes': 6400,
967                 'LatencyAvg': 10,
968                 'LatencyMin': 10,
969                 'LatencyMax': 10}}
970
971         mock_get_pppoe_subs.return_value = {'SessionsUp': 1}
972         mock_sum_prio_drop_rate.return_value = {'0': {'DropPercentage': 0.0}}
973
974         status, res = self.ixia_tp.get_drop_percentage(
975             samples, tol_min=0.0, tolerance=0.0001, precision=0,
976             resolution=0.1, first_run=True)
977         self.assertIsNotNone(res.get('DropPercentage'))
978         self.assertIsNotNone(res.get('Priority'))
979         self.assertIsNotNone(res.get('SessionsUp'))
980         self.assertEqual(res['DropPercentage'], 10.0)
981         self.assertFalse(status)
982         mock_sum_prio_drop_rate.assert_called_once()
983         mock_get_pppoe_subs.assert_called_once()
984
985     @mock.patch.object(ixia_rfc2544.IXIARFC2544PppoeScenarioProfile,
986                        '_get_prio_flows_drop_percentage')
987     @mock.patch.object(ixia_rfc2544.IXIARFC2544PppoeScenarioProfile,
988                        '_get_summary_pppoe_subs_counters')
989     def test_get_drop_percentage_priority_flow_check(self, mock_get_pppoe_subs,
990                                                      mock_sum_prio_drop_rate):
991         samples = {
992             'priority_stats': {
993                 '0': {
994                     'InPackets': 100,
995                     'OutPackets': 100,
996                     'InBytes': 6400,
997                     'OutBytes': 6400,
998                     'LatencyAvg': 10,
999                     'LatencyMin': 10,
1000                     'LatencyMax': 10}},
1001             'xe0': {
1002                 'InPackets': 90,
1003                 'OutPackets': 100,
1004                 'InBytes': 5760,
1005                 'OutBytes': 6400,
1006                 'LatencyAvg': 10,
1007                 'LatencyMin': 10,
1008                 'LatencyMax': 10
1009         }}
1010
1011         mock_get_pppoe_subs.return_value = {'SessionsUp': 1}
1012         mock_sum_prio_drop_rate.return_value = {'0': {'DropPercentage': 0.0}}
1013
1014         tc_rfc2544_opts = {'priority': '0',
1015                            'allowed_drop_rate': '0.0001 - 0.0001'}
1016         status, res = self.ixia_tp.get_drop_percentage(
1017             samples, tol_min=15.0000, tolerance=15.0001, precision=0,
1018             resolution=0.1, first_run=True, tc_rfc2544_opts=tc_rfc2544_opts)
1019         self.assertIsNotNone(res.get('DropPercentage'))
1020         self.assertIsNotNone(res.get('Priority'))
1021         self.assertIsNotNone(res.get('SessionsUp'))
1022         self.assertTrue(status)
1023         mock_sum_prio_drop_rate.assert_called_once()
1024         mock_get_pppoe_subs.assert_called_once()