Merge "NSB: Improve get_url in ansible scripts"
[yardstick.git] / yardstick / tests / unit / network_services / vnf_generic / vnf / test_tg_trex_vpp.py
1 # Copyright (c) 2019 Viosoft 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 unittest
16 from multiprocessing import Process
17
18 import mock
19 from trex_stl_lib.trex_stl_exceptions import STLError
20
21 from yardstick.benchmark.contexts import base as ctx_base
22 from yardstick.network_services.traffic_profile import base as tp_base
23 from yardstick.network_services.traffic_profile import rfc2544
24 from yardstick.network_services.vnf_generic.vnf import base, sample_vnf, \
25     tg_trex_vpp
26 from yardstick.tests.unit.network_services.vnf_generic.vnf.test_base import \
27     mock_ssh
28
29
30 class TestTrexVppResourceHelper(unittest.TestCase):
31     TRAFFIC_PROFILE = {
32         "schema": "isb:traffic_profile:0.1",
33         "name": "fixed",
34         "description": "Fixed traffic profile to run UDP traffic",
35         "traffic_profile": {
36             "traffic_type": "FixedTraffic",
37             "frame_rate": 100,  # pps
38             "flow_number": 10,
39             "frame_size": 64
40         },
41     }
42
43     def test_fmt_latency(self):
44         mock_setup_helper = mock.Mock()
45         vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
46         self.assertEqual('10/90/489', vpp_rfc.fmt_latency(10, 90, 489))
47
48     def test_fmt_latency_error(self):
49         mock_setup_helper = mock.Mock()
50         vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
51         self.assertEqual('-1/-1/-1', vpp_rfc.fmt_latency('err', 'err', 'err'))
52
53     def test_generate_samples(self):
54         stats = {
55             0: {
56                 "ibytes": 55549120,
57                 "ierrors": 0,
58                 "ipackets": 867955,
59                 "obytes": 55549696,
60                 "oerrors": 0,
61                 "opackets": 867964,
62                 "rx_bps": 104339032.0,
63                 "rx_bps_L1": 136944984.0,
64                 "rx_pps": 203787.2,
65                 "rx_util": 1.36944984,
66                 "tx_bps": 134126008.0,
67                 "tx_bps_L1": 176040392.0,
68                 "tx_pps": 261964.9,
69                 "tx_util": 1.7604039200000001
70             },
71             1: {
72                 "ibytes": 55549696,
73                 "ierrors": 0,
74                 "ipackets": 867964,
75                 "obytes": 55549120,
76                 "oerrors": 0,
77                 "opackets": 867955,
78                 "rx_bps": 134119648.0,
79                 "rx_bps_L1": 176032032.0,
80                 "rx_pps": 261952.4,
81                 "rx_util": 1.76032032,
82                 "tx_bps": 104338192.0,
83                 "tx_bps_L1": 136943872.0,
84                 "tx_pps": 203785.5,
85                 "tx_util": 1.36943872
86             },
87             "flow_stats": {
88                 1: {
89                     "rx_bps": {
90                         "0": 0,
91                         "1": 0,
92                         "total": 0
93                     },
94                     "rx_bps_l1": {
95                         "0": 0.0,
96                         "1": 0.0,
97                         "total": 0.0
98                     },
99                     "rx_bytes": {
100                         "0": 6400,
101                         "1": 0,
102                         "total": 6400
103                     },
104                     "rx_pkts": {
105                         "0": 100,
106                         "1": 0,
107                         "total": 100
108                     },
109                     "rx_pps": {
110                         "0": 0,
111                         "1": 0,
112                         "total": 0
113                     },
114                     "tx_bps": {
115                         "0": 0,
116                         "1": 0,
117                         "total": 0
118                     },
119                     "tx_bps_l1": {
120                         "0": 0.0,
121                         "1": 0.0,
122                         "total": 0.0
123                     },
124                     "tx_bytes": {
125                         "0": 0,
126                         "1": 6400,
127                         "total": 6400
128                     },
129                     "tx_pkts": {
130                         "0": 0,
131                         "1": 100,
132                         "total": 100
133                     },
134                     "tx_pps": {
135                         "0": 0,
136                         "1": 0,
137                         "total": 0
138                     }
139                 },
140                 2: {
141                     "rx_bps": {
142                         "0": 0,
143                         "1": 0,
144                         "total": 0
145                     },
146                     "rx_bps_l1": {
147                         "0": 0.0,
148                         "1": 0.0,
149                         "total": 0.0
150                     },
151                     "rx_bytes": {
152                         "0": 0,
153                         "1": 6464,
154                         "total": 6464
155                     },
156                     "rx_pkts": {
157                         "0": 0,
158                         "1": 101,
159                         "total": 101
160                     },
161                     "rx_pps": {
162                         "0": 0,
163                         "1": 0,
164                         "total": 0
165                     },
166                     "tx_bps": {
167                         "0": 0,
168                         "1": 0,
169                         "total": 0
170                     },
171                     "tx_bps_l1": {
172                         "0": 0.0,
173                         "1": 0.0,
174                         "total": 0.0
175                     },
176                     "tx_bytes": {
177                         "0": 6464,
178                         "1": 0,
179                         "total": 6464
180                     },
181                     "tx_pkts": {
182                         "0": 101,
183                         "1": 0,
184                         "total": 101
185                     },
186                     "tx_pps": {
187                         "0": 0,
188                         "1": 0,
189                         "total": 0
190                     }
191                 },
192                 "global": {
193                     "rx_err": {
194                         "0": 0,
195                         "1": 0
196                     },
197                     "tx_err": {
198                         "0": 0,
199                         "1": 0
200                     }
201                 }
202             },
203             "global": {
204                 "bw_per_core": 45.6,
205                 "cpu_util": 0.1494,
206                 "queue_full": 0,
207                 "rx_bps": 238458672.0,
208                 "rx_cpu_util": 4.751e-05,
209                 "rx_drop_bps": 0.0,
210                 "rx_pps": 465739.6,
211                 "tx_bps": 238464208.0,
212                 "tx_pps": 465750.4
213             },
214             "latency": {
215                 1: {
216                     "err_cntrs": {
217                         "dropped": 0,
218                         "dup": 0,
219                         "out_of_order": 0,
220                         "seq_too_high": 0,
221                         "seq_too_low": 0
222                     },
223                     "latency": {
224                         "average": 63.375,
225                         "histogram": {
226                             "20": 1,
227                             "30": 18,
228                             "40": 12,
229                             "50": 10,
230                             "60": 12,
231                             "70": 11,
232                             "80": 6,
233                             "90": 10,
234                             "100": 20
235                         },
236                         "jitter": 23,
237                         "last_max": 122,
238                         "total_max": 123,
239                         "total_min": 20
240                     }
241                 },
242                 2: {
243                     "err_cntrs": {
244                         "dropped": 0,
245                         "dup": 0,
246                         "out_of_order": 0,
247                         "seq_too_high": 0,
248                         "seq_too_low": 0
249                     },
250                     "latency": {
251                         "average": 74,
252                         "histogram": {
253                             "60": 20,
254                             "70": 10,
255                             "80": 3,
256                             "90": 4,
257                             "100": 64
258                         },
259                         "jitter": 6,
260                         "last_max": 83,
261                         "total_max": 135,
262                         "total_min": 60
263                     }
264                 },
265                 "global": {
266                     "bad_hdr": 0,
267                     "old_flow": 0
268                 }
269             },
270             "total": {
271                 "ibytes": 111098816,
272                 "ierrors": 0,
273                 "ipackets": 1735919,
274                 "obytes": 111098816,
275                 "oerrors": 0,
276                 "opackets": 1735919,
277                 "rx_bps": 238458680.0,
278                 "rx_bps_L1": 312977016.0,
279                 "rx_pps": 465739.6,
280                 "rx_util": 3.1297701599999996,
281                 "tx_bps": 238464200.0,
282                 "tx_bps_L1": 312984264.0,
283                 "tx_pps": 465750.4,
284                 "tx_util": 3.12984264
285             }
286         }
287         expected = {
288             "xe0": {
289                 "in_packets": 867955,
290                 "latency": {
291                     2: {
292                         "avg_latency": 74.0,
293                         "max_latency": 135.0,
294                         "min_latency": 60.0
295                     }
296                 },
297                 "out_packets": 867964,
298                 "rx_throughput_bps": 104339032.0,
299                 "rx_throughput_fps": 203787.2,
300                 "tx_throughput_bps": 134126008.0,
301                 "tx_throughput_fps": 261964.9
302             },
303             "xe1": {
304                 "in_packets": 867964,
305                 "latency": {
306                     1: {
307                         "avg_latency": 63.375,
308                         "max_latency": 123.0,
309                         "min_latency": 20.0
310                     }
311                 },
312                 "out_packets": 867955,
313                 "rx_throughput_bps": 134119648.0,
314                 "rx_throughput_fps": 261952.4,
315                 "tx_throughput_bps": 104338192.0,
316                 "tx_throughput_fps": 203785.5
317             }
318         }
319         mock_setup_helper = mock.Mock()
320         vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
321         vpp_rfc.vnfd_helper = base.VnfdHelper(TestTrexTrafficGenVpp.VNFD_0)
322         port_pg_id = rfc2544.PortPgIDMap()
323         port_pg_id.add_port(1)
324         port_pg_id.increase_pg_id()
325         port_pg_id.add_port(0)
326         port_pg_id.increase_pg_id()
327         self.assertEqual(expected,
328                          vpp_rfc.generate_samples(stats, [0, 1], port_pg_id,
329                                                   True))
330
331     def test_generate_samples_error(self):
332         stats = {
333             0: {
334                 "ibytes": 55549120,
335                 "ierrors": 0,
336                 "ipackets": 867955,
337                 "obytes": 55549696,
338                 "oerrors": 0,
339                 "opackets": 867964,
340                 "rx_bps": 104339032.0,
341                 "rx_bps_L1": 136944984.0,
342                 "rx_pps": 203787.2,
343                 "rx_util": 1.36944984,
344                 "tx_bps": 134126008.0,
345                 "tx_bps_L1": 176040392.0,
346                 "tx_pps": 261964.9,
347                 "tx_util": 1.7604039200000001
348             },
349             1: {
350                 "ibytes": 55549696,
351                 "ierrors": 0,
352                 "ipackets": 867964,
353                 "obytes": 55549120,
354                 "oerrors": 0,
355                 "opackets": 867955,
356                 "rx_bps": 134119648.0,
357                 "rx_bps_L1": 176032032.0,
358                 "rx_pps": 261952.4,
359                 "rx_util": 1.76032032,
360                 "tx_bps": 104338192.0,
361                 "tx_bps_L1": 136943872.0,
362                 "tx_pps": 203785.5,
363                 "tx_util": 1.36943872
364             },
365             "flow_stats": {
366                 1: {
367                     "rx_bps": {
368                         "0": 0,
369                         "1": 0,
370                         "total": 0
371                     },
372                     "rx_bps_l1": {
373                         "0": 0.0,
374                         "1": 0.0,
375                         "total": 0.0
376                     },
377                     "rx_bytes": {
378                         "0": 6400,
379                         "1": 0,
380                         "total": 6400
381                     },
382                     "rx_pkts": {
383                         "0": 100,
384                         "1": 0,
385                         "total": 100
386                     },
387                     "rx_pps": {
388                         "0": 0,
389                         "1": 0,
390                         "total": 0
391                     },
392                     "tx_bps": {
393                         "0": 0,
394                         "1": 0,
395                         "total": 0
396                     },
397                     "tx_bps_l1": {
398                         "0": 0.0,
399                         "1": 0.0,
400                         "total": 0.0
401                     },
402                     "tx_bytes": {
403                         "0": 0,
404                         "1": 6400,
405                         "total": 6400
406                     },
407                     "tx_pkts": {
408                         "0": 0,
409                         "1": 100,
410                         "total": 100
411                     },
412                     "tx_pps": {
413                         "0": 0,
414                         "1": 0,
415                         "total": 0
416                     }
417                 },
418                 2: {
419                     "rx_bps": {
420                         "0": 0,
421                         "1": 0,
422                         "total": 0
423                     },
424                     "rx_bps_l1": {
425                         "0": 0.0,
426                         "1": 0.0,
427                         "total": 0.0
428                     },
429                     "rx_bytes": {
430                         "0": 0,
431                         "1": 6464,
432                         "total": 6464
433                     },
434                     "rx_pkts": {
435                         "0": 0,
436                         "1": 101,
437                         "total": 101
438                     },
439                     "rx_pps": {
440                         "0": 0,
441                         "1": 0,
442                         "total": 0
443                     },
444                     "tx_bps": {
445                         "0": 0,
446                         "1": 0,
447                         "total": 0
448                     },
449                     "tx_bps_l1": {
450                         "0": 0.0,
451                         "1": 0.0,
452                         "total": 0.0
453                     },
454                     "tx_bytes": {
455                         "0": 6464,
456                         "1": 0,
457                         "total": 6464
458                     },
459                     "tx_pkts": {
460                         "0": 101,
461                         "1": 0,
462                         "total": 101
463                     },
464                     "tx_pps": {
465                         "0": 0,
466                         "1": 0,
467                         "total": 0
468                     }
469                 },
470                 "global": {
471                     "rx_err": {
472                         "0": 0,
473                         "1": 0
474                     },
475                     "tx_err": {
476                         "0": 0,
477                         "1": 0
478                     }
479                 }
480             },
481             "global": {
482                 "bw_per_core": 45.6,
483                 "cpu_util": 0.1494,
484                 "queue_full": 0,
485                 "rx_bps": 238458672.0,
486                 "rx_cpu_util": 4.751e-05,
487                 "rx_drop_bps": 0.0,
488                 "rx_pps": 465739.6,
489                 "tx_bps": 238464208.0,
490                 "tx_pps": 465750.4
491             },
492             "latency": {
493                 1: {
494                     "err_cntrs": {
495                         "dropped": 0,
496                         "dup": 0,
497                         "out_of_order": 0,
498                         "seq_too_high": 0,
499                         "seq_too_low": 0
500                     },
501                     "latency": {
502                         "average": "err",
503                         "histogram": {
504                             "20": 1,
505                             "30": 18,
506                             "40": 12,
507                             "50": 10,
508                             "60": 12,
509                             "70": 11,
510                             "80": 6,
511                             "90": 10,
512                             "100": 20
513                         },
514                         "jitter": 23,
515                         "last_max": 122,
516                         "total_max": "err",
517                         "total_min": "err"
518                     }
519                 },
520                 2: {
521                     "err_cntrs": {
522                         "dropped": 0,
523                         "dup": 0,
524                         "out_of_order": 0,
525                         "seq_too_high": 0,
526                         "seq_too_low": 0
527                     },
528                     "latency": {
529                         "average": 74,
530                         "histogram": {
531                             "60": 20,
532                             "70": 10,
533                             "80": 3,
534                             "90": 4,
535                             "100": 64
536                         },
537                         "jitter": 6,
538                         "last_max": 83,
539                         "total_max": 135,
540                         "total_min": 60
541                     }
542                 },
543                 "global": {
544                     "bad_hdr": 0,
545                     "old_flow": 0
546                 }
547             },
548             "total": {
549                 "ibytes": 111098816,
550                 "ierrors": 0,
551                 "ipackets": 1735919,
552                 "obytes": 111098816,
553                 "oerrors": 0,
554                 "opackets": 1735919,
555                 "rx_bps": 238458680.0,
556                 "rx_bps_L1": 312977016.0,
557                 "rx_pps": 465739.6,
558                 "rx_util": 3.1297701599999996,
559                 "tx_bps": 238464200.0,
560                 "tx_bps_L1": 312984264.0,
561                 "tx_pps": 465750.4,
562                 "tx_util": 3.12984264
563             }
564         }
565         expected = {'xe0': {'in_packets': 867955,
566                             'latency': {2: {'avg_latency': 74.0,
567                                             'max_latency': 135.0,
568                                             'min_latency': 60.0}},
569                             'out_packets': 867964,
570                             'rx_throughput_bps': 104339032.0,
571                             'rx_throughput_fps': 203787.2,
572                             'tx_throughput_bps': 134126008.0,
573                             'tx_throughput_fps': 261964.9},
574                     'xe1': {'in_packets': 867964,
575                             'latency': {1: {'avg_latency': -1.0,
576                                             'max_latency': -1.0,
577                                             'min_latency': -1.0}},
578                             'out_packets': 867955,
579                             'rx_throughput_bps': 134119648.0,
580                             'rx_throughput_fps': 261952.4,
581                             'tx_throughput_bps': 104338192.0,
582                             'tx_throughput_fps': 203785.5}}
583         mock_setup_helper = mock.Mock()
584         vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
585         vpp_rfc.vnfd_helper = base.VnfdHelper(TestTrexTrafficGenVpp.VNFD_0)
586         vpp_rfc.get_stats = mock.Mock()
587         vpp_rfc.get_stats.return_value = stats
588         port_pg_id = rfc2544.PortPgIDMap()
589         port_pg_id.add_port(1)
590         port_pg_id.increase_pg_id()
591         port_pg_id.add_port(0)
592         port_pg_id.increase_pg_id()
593         self.assertEqual(expected,
594                          vpp_rfc.generate_samples(stats=None, ports=[0, 1],
595                                                   port_pg_id=port_pg_id,
596                                                   latency=True))
597
598     def test__run_traffic_once(self):
599         mock_setup_helper = mock.Mock()
600         mock_traffic_profile = mock.Mock()
601         vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
602         vpp_rfc.TRANSIENT_PERIOD = 0
603         vpp_rfc.rfc2544_helper = mock.Mock()
604
605         self.assertTrue(vpp_rfc._run_traffic_once(mock_traffic_profile))
606         mock_traffic_profile.execute_traffic.assert_called_once_with(vpp_rfc)
607
608     def test_run_traffic(self):
609         mock_traffic_profile = mock.Mock(autospec=tp_base.TrafficProfile)
610         mock_traffic_profile.get_traffic_definition.return_value = "64"
611         mock_traffic_profile.params = self.TRAFFIC_PROFILE
612         mock_setup_helper = mock.Mock()
613         vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
614         vpp_rfc.ssh_helper = mock.Mock()
615         vpp_rfc.ssh_helper.run = mock.Mock()
616         vpp_rfc._traffic_runner = mock.Mock(return_value=0)
617         vpp_rfc._build_ports = mock.Mock()
618         vpp_rfc._connect = mock.Mock()
619         vpp_rfc.run_traffic(mock_traffic_profile)
620
621     def test_send_traffic_on_tg(self):
622         stats = {
623             0: {
624                 "ibytes": 55549120,
625                 "ierrors": 0,
626                 "ipackets": 867955,
627                 "obytes": 55549696,
628                 "oerrors": 0,
629                 "opackets": 867964,
630                 "rx_bps": 104339032.0,
631                 "rx_bps_L1": 136944984.0,
632                 "rx_pps": 203787.2,
633                 "rx_util": 1.36944984,
634                 "tx_bps": 134126008.0,
635                 "tx_bps_L1": 176040392.0,
636                 "tx_pps": 261964.9,
637                 "tx_util": 1.7604039200000001
638             },
639             1: {
640                 "ibytes": 55549696,
641                 "ierrors": 0,
642                 "ipackets": 867964,
643                 "obytes": 55549120,
644                 "oerrors": 0,
645                 "opackets": 867955,
646                 "rx_bps": 134119648.0,
647                 "rx_bps_L1": 176032032.0,
648                 "rx_pps": 261952.4,
649                 "rx_util": 1.76032032,
650                 "tx_bps": 104338192.0,
651                 "tx_bps_L1": 136943872.0,
652                 "tx_pps": 203785.5,
653                 "tx_util": 1.36943872
654             },
655             "flow_stats": {
656                 1: {
657                     "rx_bps": {
658                         "0": 0,
659                         "1": 0,
660                         "total": 0
661                     },
662                     "rx_bps_l1": {
663                         "0": 0.0,
664                         "1": 0.0,
665                         "total": 0.0
666                     },
667                     "rx_bytes": {
668                         "0": 6400,
669                         "1": 0,
670                         "total": 6400
671                     },
672                     "rx_pkts": {
673                         "0": 100,
674                         "1": 0,
675                         "total": 100
676                     },
677                     "rx_pps": {
678                         "0": 0,
679                         "1": 0,
680                         "total": 0
681                     },
682                     "tx_bps": {
683                         "0": 0,
684                         "1": 0,
685                         "total": 0
686                     },
687                     "tx_bps_l1": {
688                         "0": 0.0,
689                         "1": 0.0,
690                         "total": 0.0
691                     },
692                     "tx_bytes": {
693                         "0": 0,
694                         "1": 6400,
695                         "total": 6400
696                     },
697                     "tx_pkts": {
698                         "0": 0,
699                         "1": 100,
700                         "total": 100
701                     },
702                     "tx_pps": {
703                         "0": 0,
704                         "1": 0,
705                         "total": 0
706                     }
707                 },
708                 2: {
709                     "rx_bps": {
710                         "0": 0,
711                         "1": 0,
712                         "total": 0
713                     },
714                     "rx_bps_l1": {
715                         "0": 0.0,
716                         "1": 0.0,
717                         "total": 0.0
718                     },
719                     "rx_bytes": {
720                         "0": 0,
721                         "1": 6464,
722                         "total": 6464
723                     },
724                     "rx_pkts": {
725                         "0": 0,
726                         "1": 101,
727                         "total": 101
728                     },
729                     "rx_pps": {
730                         "0": 0,
731                         "1": 0,
732                         "total": 0
733                     },
734                     "tx_bps": {
735                         "0": 0,
736                         "1": 0,
737                         "total": 0
738                     },
739                     "tx_bps_l1": {
740                         "0": 0.0,
741                         "1": 0.0,
742                         "total": 0.0
743                     },
744                     "tx_bytes": {
745                         "0": 6464,
746                         "1": 0,
747                         "total": 6464
748                     },
749                     "tx_pkts": {
750                         "0": 101,
751                         "1": 0,
752                         "total": 101
753                     },
754                     "tx_pps": {
755                         "0": 0,
756                         "1": 0,
757                         "total": 0
758                     }
759                 },
760                 "global": {
761                     "rx_err": {
762                         "0": 0,
763                         "1": 0
764                     },
765                     "tx_err": {
766                         "0": 0,
767                         "1": 0
768                     }
769                 }
770             },
771             "global": {
772                 "bw_per_core": 45.6,
773                 "cpu_util": 0.1494,
774                 "queue_full": 0,
775                 "rx_bps": 238458672.0,
776                 "rx_cpu_util": 4.751e-05,
777                 "rx_drop_bps": 0.0,
778                 "rx_pps": 465739.6,
779                 "tx_bps": 238464208.0,
780                 "tx_pps": 465750.4
781             },
782             "latency": {
783                 1: {
784                     "err_cntrs": {
785                         "dropped": 0,
786                         "dup": 0,
787                         "out_of_order": 0,
788                         "seq_too_high": 0,
789                         "seq_too_low": 0
790                     },
791                     "latency": {
792                         "average": 63.375,
793                         "histogram": {
794                             "20": 1,
795                             "30": 18,
796                             "40": 12,
797                             "50": 10,
798                             "60": 12,
799                             "70": 11,
800                             "80": 6,
801                             "90": 10,
802                             "100": 20
803                         },
804                         "jitter": 23,
805                         "last_max": 122,
806                         "total_max": 123,
807                         "total_min": 20
808                     }
809                 },
810                 2: {
811                     "err_cntrs": {
812                         "dropped": 0,
813                         "dup": 0,
814                         "out_of_order": 0,
815                         "seq_too_high": 0,
816                         "seq_too_low": 0
817                     },
818                     "latency": {
819                         "average": 74,
820                         "histogram": {
821                             "60": 20,
822                             "70": 10,
823                             "80": 3,
824                             "90": 4,
825                             "100": 64
826                         },
827                         "jitter": 6,
828                         "last_max": 83,
829                         "total_max": 135,
830                         "total_min": 60
831                     }
832                 },
833                 "global": {
834                     "bad_hdr": 0,
835                     "old_flow": 0
836                 }
837             },
838             "total": {
839                 "ibytes": 111098816,
840                 "ierrors": 0,
841                 "ipackets": 1735919,
842                 "obytes": 111098816,
843                 "oerrors": 0,
844                 "opackets": 1735919,
845                 "rx_bps": 238458680.0,
846                 "rx_bps_L1": 312977016.0,
847                 "rx_pps": 465739.6,
848                 "rx_util": 3.1297701599999996,
849                 "tx_bps": 238464200.0,
850                 "tx_bps_L1": 312984264.0,
851                 "tx_pps": 465750.4,
852                 "tx_util": 3.12984264
853             }
854         }
855         mock_setup_helper = mock.Mock()
856         vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
857         vpp_rfc.vnfd_helper = base.VnfdHelper(TestTrexTrafficGenVpp.VNFD_0)
858         vpp_rfc.client = mock.Mock()
859         vpp_rfc.client.get_warnings.return_value = 'get_warnings'
860         vpp_rfc.client.get_stats.return_value = stats
861         port_pg_id = rfc2544.PortPgIDMap()
862         port_pg_id.add_port(1)
863         port_pg_id.increase_pg_id()
864         port_pg_id.add_port(0)
865         port_pg_id.increase_pg_id()
866         self.assertEqual(stats,
867                          vpp_rfc.send_traffic_on_tg([0, 1], port_pg_id, 30,
868                                                     10000, True))
869
870     def test_send_traffic_on_tg_error(self):
871         mock_setup_helper = mock.Mock()
872         vpp_rfc = tg_trex_vpp.TrexVppResourceHelper(mock_setup_helper)
873         vpp_rfc.vnfd_helper = base.VnfdHelper(TestTrexTrafficGenVpp.VNFD_0)
874         vpp_rfc.client = mock.Mock()
875         vpp_rfc.client.get_warnings.return_value = 'get_warnings'
876         vpp_rfc.client.get_stats.side_effect = STLError('get_stats')
877         vpp_rfc.client.wait_on_traffic.side_effect = STLError(
878             'wait_on_traffic')
879         port_pg_id = rfc2544.PortPgIDMap()
880         port_pg_id.add_port(1)
881         port_pg_id.increase_pg_id()
882         port_pg_id.add_port(0)
883         port_pg_id.increase_pg_id()
884         # with self.assertRaises(RuntimeError) as raised:
885         vpp_rfc.send_traffic_on_tg([0, 1], port_pg_id, 30, 10000, True)
886         # self.assertIn('TRex stateless runtime error', str(raised.exception))
887
888
889 class TestTrexTrafficGenVpp(unittest.TestCase):
890     VNFD_0 = {
891         "benchmark": {
892             "kpi": [
893                 "rx_throughput_fps",
894                 "tx_throughput_fps",
895                 "tx_throughput_mbps",
896                 "rx_throughput_mbps",
897                 "in_packets",
898                 "out_packets",
899                 "min_latency",
900                 "max_latency",
901                 "avg_latency"
902             ]
903         },
904         "description": "TRex stateless traffic verifier",
905         "id": "TrexTrafficGenVpp",
906         "mgmt-interface": {
907             "ip": "10.10.10.10",
908             "password": "r00t",
909             "user": "root",
910             "vdu-id": "trexgen-baremetal"
911         },
912         "name": "trexverifier",
913         "short-name": "trexverifier",
914         "vdu": [
915             {
916                 "description": "TRex stateless traffic verifier",
917                 "external-interface": [
918                     {
919                         "name": "xe0",
920                         "virtual-interface": {
921                             "dpdk_port_num": 0,
922                             "driver": "igb_uio",
923                             "dst_ip": "192.168.100.2",
924                             "dst_mac": "90:e2:ba:7c:41:a8",
925                             "ifname": "xe0",
926                             "local_ip": "192.168.100.1",
927                             "local_mac": "90:e2:ba:7c:30:e8",
928                             "netmask": "255.255.255.0",
929                             "network": {},
930                             "node_name": "tg__0",
931                             "peer_ifname": "xe0",
932                             "peer_intf": {
933                                 "driver": "igb_uio",
934                                 "dst_ip": "192.168.100.1",
935                                 "dst_mac": "90:e2:ba:7c:30:e8",
936                                 "ifname": "xe0",
937                                 "local_ip": "192.168.100.2",
938                                 "local_mac": "90:e2:ba:7c:41:a8",
939                                 "netmask": "255.255.255.0",
940                                 "network": {},
941                                 "node_name": "vnf__0",
942                                 "peer_ifname": "xe0",
943                                 "peer_name": "tg__0",
944                                 "vld_id": "uplink_0",
945                                 "vpci": "0000:ff:06.0"
946                             },
947                             "peer_name": "vnf__0",
948                             "vld_id": "uplink_0",
949                             "vpci": "0000:81:00.0"
950                         },
951                         "vnfd-connection-point-ref": "xe0"
952                     },
953                     {
954                         "name": "xe1",
955                         "virtual-interface": {
956                             "dpdk_port_num": 1,
957                             "driver": "igb_uio",
958                             "dst_ip": "192.168.101.2",
959                             "dst_mac": "90:e2:ba:7c:41:a9",
960                             "ifname": "xe1",
961                             "local_ip": "192.168.101.1",
962                             "local_mac": "90:e2:ba:7c:30:e9",
963                             "netmask": "255.255.255.0",
964                             "network": {},
965                             "node_name": "tg__0",
966                             "peer_ifname": "xe0",
967                             "peer_intf": {
968                                 "driver": "igb_uio",
969                                 "dst_ip": "192.168.101.1",
970                                 "dst_mac": "90:e2:ba:7c:30:e9",
971                                 "ifname": "xe0",
972                                 "local_ip": "192.168.101.2",
973                                 "local_mac": "90:e2:ba:7c:41:a9",
974                                 "netmask": "255.255.255.0",
975                                 "network": {},
976                                 "node_name": "vnf__1",
977                                 "peer_ifname": "xe1",
978                                 "peer_name": "tg__0",
979                                 "vld_id": "downlink_0",
980                                 "vpci": "0000:ff:06.0"
981                             },
982                             "peer_name": "vnf__1",
983                             "vld_id": "downlink_0",
984                             "vpci": "0000:81:00.1"
985                         },
986                         "vnfd-connection-point-ref": "xe1"
987                     }
988                 ],
989                 "id": "trexgen-baremetal",
990                 "name": "trexgen-baremetal"
991             }
992         ]
993     }
994
995     VNFD = {
996         'vnfd:vnfd-catalog': {
997             'vnfd': [
998                 VNFD_0,
999             ],
1000         },
1001     }
1002
1003     def setUp(self):
1004         self._mock_ssh_helper = mock.patch.object(sample_vnf, 'VnfSshHelper')
1005         self.mock_ssh_helper = self._mock_ssh_helper.start()
1006         self.addCleanup(self._stop_mocks)
1007
1008     def _stop_mocks(self):
1009         self._mock_ssh_helper.stop()
1010
1011     def test___init__(self):
1012         trex_traffic_gen = tg_trex_vpp.TrexTrafficGenVpp(
1013             'tg0', self.VNFD_0)
1014         self.assertIsNotNone(
1015             trex_traffic_gen.resource_helper._terminated.value)
1016
1017     def test__check_status(self):
1018         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
1019         trex_traffic_gen = tg_trex_vpp.TrexTrafficGenVpp('tg0', vnfd)
1020         trex_traffic_gen.ssh_helper = mock.MagicMock()
1021         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
1022         trex_traffic_gen.resource_helper.ssh_helper.execute.return_value = 0, '', ''
1023         trex_traffic_gen.scenario_helper.scenario_cfg = {}
1024         self.assertEqual(0, trex_traffic_gen._check_status())
1025
1026     def test__start_server(self):
1027         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
1028         trex_traffic_gen = tg_trex_vpp.TrexTrafficGenVpp('tg0', vnfd)
1029         trex_traffic_gen.ssh_helper = mock.MagicMock()
1030         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
1031         trex_traffic_gen.scenario_helper.scenario_cfg = {}
1032         self.assertIsNone(trex_traffic_gen._start_server())
1033
1034     @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server',
1035                        return_value='mock_node')
1036     def test_collect_kpi(self, *args):
1037         trex_traffic_gen = tg_trex_vpp.TrexTrafficGenVpp(
1038             'tg0', self.VNFD_0)
1039         trex_traffic_gen.scenario_helper.scenario_cfg = {
1040             'nodes': {trex_traffic_gen.name: "mock"}
1041         }
1042         expected = {
1043             'physical_node': 'mock_node',
1044             'collect_stats': {},
1045         }
1046         self.assertEqual(trex_traffic_gen.collect_kpi(), expected)
1047
1048     @mock.patch.object(ctx_base.Context, 'get_context_from_server',
1049                        return_value='fake_context')
1050     def test_instantiate(self, *args):
1051         trex_traffic_gen = tg_trex_vpp.TrexTrafficGenVpp(
1052             'tg0', self.VNFD_0)
1053         trex_traffic_gen._start_server = mock.Mock(return_value=0)
1054         trex_traffic_gen.resource_helper = mock.MagicMock()
1055         trex_traffic_gen.setup_helper.setup_vnf_environment = mock.MagicMock()
1056
1057         scenario_cfg = {
1058             "tc": "tc_baremetal_rfc2544_ipv4_1flow_64B",
1059             "topology": 'nsb_test_case.yaml',
1060             'options': {
1061                 'packetsize': 64,
1062                 'traffic_type': 4,
1063                 'rfc2544': {
1064                     'allowed_drop_rate': '0.8 - 1',
1065                 },
1066                 'vnf__0': {
1067                     'rules': 'acl_1rule.yaml',
1068                     'vnf_config': {
1069                         'lb_config': 'SW',
1070                         'lb_count': 1,
1071                         'worker_config': '1C/1T',
1072                         'worker_threads': 1
1073                     },
1074                 },
1075             },
1076         }
1077         tg_trex_vpp.WAIT_TIME = 3
1078         scenario_cfg.update({"nodes": {"tg0": {}, "vnf0": {}}})
1079         self.assertIsNone(trex_traffic_gen.instantiate(scenario_cfg, {}))
1080
1081     @mock.patch.object(ctx_base.Context, 'get_context_from_server',
1082                        return_value='fake_context')
1083     def test_instantiate_error(self, *args):
1084         trex_traffic_gen = tg_trex_vpp.TrexTrafficGenVpp(
1085             'tg0', self.VNFD_0)
1086         trex_traffic_gen.resource_helper = mock.MagicMock()
1087         trex_traffic_gen.setup_helper.setup_vnf_environment = mock.MagicMock()
1088         scenario_cfg = {
1089             "tc": "tc_baremetal_rfc2544_ipv4_1flow_64B",
1090             "nodes": {
1091                 "tg0": {},
1092                 "vnf0": {}
1093             },
1094             "topology": 'nsb_test_case.yaml',
1095             'options': {
1096                 'packetsize': 64,
1097                 'traffic_type': 4,
1098                 'rfc2544': {
1099                     'allowed_drop_rate': '0.8 - 1',
1100                 },
1101                 'vnf__0': {
1102                     'rules': 'acl_1rule.yaml',
1103                     'vnf_config': {
1104                         'lb_config': 'SW',
1105                         'lb_count': 1,
1106                         'worker_config': '1C/1T',
1107                         'worker_threads': 1,
1108                     },
1109                 },
1110             },
1111         }
1112         trex_traffic_gen.instantiate(scenario_cfg, {})
1113
1114     @mock.patch(
1115         'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper')
1116     def test_wait_for_instantiate(self, ssh, *args):
1117         mock_ssh(ssh)
1118
1119         mock_process = mock.Mock(autospec=Process)
1120         mock_process.is_alive.return_value = True
1121         mock_process.exitcode = 432
1122
1123         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
1124         trex_traffic_gen = tg_trex_vpp.TrexTrafficGenVpp('tg0', vnfd)
1125         trex_traffic_gen.ssh_helper = mock.MagicMock()
1126         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
1127         trex_traffic_gen.resource_helper.ssh_helper.execute.return_value = 0, '', ''
1128         trex_traffic_gen.scenario_helper.scenario_cfg = {}
1129         trex_traffic_gen._tg_process = mock_process
1130         self.assertEqual(432, trex_traffic_gen.wait_for_instantiate())