Adding generic traffic profiles for trex traffic generator
[yardstick.git] / tests / unit / network_services / traffic_profile / test_rfc2544.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016-2017 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 from __future__ import absolute_import
19 import unittest
20 import mock
21
22 from yardstick.network_services.traffic_profile.traffic_profile \
23     import TrexProfile
24 from yardstick.network_services.traffic_profile.rfc2544 import \
25     RFC2544Profile
26
27
28 class TestRFC2544Profile(unittest.TestCase):
29     TRAFFIC_PROFILE = {
30         "schema": "isb:traffic_profile:0.1",
31         "name": "fixed",
32         "description": "Fixed traffic profile to run UDP traffic",
33         "traffic_profile": {
34             "traffic_type": "FixedTraffic",
35             "frame_rate": 100,  # pps
36             "flow_number": 10,
37             "frame_size": 64}}
38
39     PROFILE = {'description': 'Traffic profile to run RFC2544 latency',
40                'name': 'rfc2544',
41                'traffic_profile': {'traffic_type': 'RFC2544Profile',
42                                    'frame_rate': 100},
43                'public': {'ipv4':
44                           {'outer_l2': {'framesize':
45                                         {'64B': '100', '1518B': '0',
46                                          '128B': '0', '1400B': '0',
47                                          '256B': '0', '373b': '0',
48                                          '570B': '0'}},
49                            'outer_l3v4': {'dstip4': '1.1.1.1-1.15.255.255',
50                                           'proto': 'udp',
51                                           'srcip4': '90.90.1.1-90.105.255.255',
52                                           'dscp': 0, 'ttl': 32},
53                            'outer_l4': {'srcport': '2001',
54                                         'dsrport': '1234'}}},
55                'private': {'ipv4':
56                            {'outer_l2': {'framesize':
57                                          {'64B': '100', '1518B': '0',
58                                           '128B': '0', '1400B': '0',
59                                           '256B': '0', '373b': '0',
60                                           '570B': '0'}},
61                             'outer_l3v4': {'dstip4': '9.9.1.1-90.105.255.255',
62                                            'proto': 'udp',
63                                            'srcip4': '1.1.1.1-1.15.255.255',
64                                            'dscp': 0, 'ttl': 32},
65                             'outer_l4': {'dstport': '2001',
66                                          'srcport': '1234'}}},
67                'schema': 'isb:traffic_profile:0.1'}
68
69     def test___init__(self):
70         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
71         assert r_f_c2544_profile.rate
72
73     def test_execute(self):
74         traffic_generator = mock.Mock(autospec=TrexProfile)
75         traffic_generator.my_ports = [0, 1]
76         traffic_generator.client = \
77             mock.Mock(return_value=True)
78         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
79         r_f_c2544_profile.params = self.PROFILE
80         r_f_c2544_profile.first_run = True
81         self.assertEqual(None, r_f_c2544_profile.execute(traffic_generator))
82
83     def test_get_drop_percentage(self):
84         traffic_generator = mock.Mock(autospec=TrexProfile)
85         traffic_generator.my_ports = [0, 1]
86         traffic_generator.client = \
87             mock.Mock(return_value=True)
88         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
89         r_f_c2544_profile.params = self.PROFILE
90         self.assertEqual(None, r_f_c2544_profile.execute(traffic_generator))
91         samples = {}
92         for ifname in range(1):
93             name = "xe{}".format(ifname)
94             samples[name] = {"rx_throughput_fps": 20,
95                              "tx_throughput_fps": 20,
96                              "rx_throughput_mbps": 10,
97                              "tx_throughput_mbps": 10,
98                              "in_packets": 1000,
99                              "out_packets": 1000}
100         tol_min = 100.0
101         tolerance = 0.0
102         expected = {'DropPercentage': 0.0, 'RxThroughput': 33,
103                     'TxThroughput': 33, 'CurrentDropPercentage': 0.0,
104                     'Throughput': 33,
105                     'xe0': {'tx_throughput_fps': 20, 'in_packets': 1000,
106                             'out_packets': 1000, 'rx_throughput_mbps': 10,
107                             'tx_throughput_mbps': 10, 'rx_throughput_fps': 20}}
108         self.assertDictEqual(expected,
109                              r_f_c2544_profile.get_drop_percentage(
110                                  traffic_generator, samples,
111                                  tol_min, tolerance))
112
113     def test_get_drop_percentage_update(self):
114         traffic_generator = mock.Mock(autospec=TrexProfile)
115         traffic_generator.my_ports = [0, 1]
116         traffic_generator.client = \
117             mock.Mock(return_value=True)
118         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
119         r_f_c2544_profile.params = self.PROFILE
120         self.assertEqual(None, r_f_c2544_profile.execute(traffic_generator))
121         samples = {}
122         for ifname in range(1):
123             name = "xe{}".format(ifname)
124             samples[name] = {"rx_throughput_fps": 20,
125                              "tx_throughput_fps": 20,
126                              "rx_throughput_mbps": 10,
127                              "tx_throughput_mbps": 10,
128                              "in_packets": 1000,
129                              "out_packets": 1002}
130         tol_min = 0.0
131         tolerance = 1.0
132         expected = {'DropPercentage': 0.2, 'RxThroughput': 33,
133                     'TxThroughput': 33, 'CurrentDropPercentage': 0.2,
134                     'Throughput': 33,
135                     'xe0': {'tx_throughput_fps': 20, 'in_packets': 1000,
136                             'out_packets': 1002, 'rx_throughput_mbps': 10,
137                             'tx_throughput_mbps': 10, 'rx_throughput_fps': 20}}
138         self.assertDictEqual(expected,
139                              r_f_c2544_profile.get_drop_percentage(
140                                  traffic_generator, samples,
141                                  tol_min, tolerance))
142
143     def test_get_drop_percentage_div_zero(self):
144         traffic_generator = mock.Mock(autospec=TrexProfile)
145         traffic_generator.my_ports = [0, 1]
146         traffic_generator.client = \
147             mock.Mock(return_value=True)
148         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
149         r_f_c2544_profile.params = self.PROFILE
150         self.assertEqual(None, r_f_c2544_profile.execute(traffic_generator))
151         samples = {}
152         for ifname in range(1):
153             name = "xe{}".format(ifname)
154             samples[name] = {"rx_throughput_fps": 20,
155                              "tx_throughput_fps": 20,
156                              "rx_throughput_mbps": 10,
157                              "tx_throughput_mbps": 10,
158                              "in_packets": 1000,
159                              "out_packets": 0}
160         tol_min = 0.0
161         tolerance = 0.0
162         r_f_c2544_profile.tmp_throughput = 0
163         expected = {'DropPercentage': 100.0, 'RxThroughput': 33,
164                     'TxThroughput': 0, 'CurrentDropPercentage': 100.0,
165                     'Throughput': 33,
166                     'xe0': {'tx_throughput_fps': 20, 'in_packets': 1000,
167                             'out_packets': 0, 'rx_throughput_mbps': 10,
168                             'tx_throughput_mbps': 10, 'rx_throughput_fps': 20}}
169         self.assertDictEqual(expected,
170                              r_f_c2544_profile.get_drop_percentage(
171                                  traffic_generator, samples,
172                                  tol_min, tolerance))
173
174     def test_get_multiplier(self):
175         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
176         r_f_c2544_profile.max_rate = 100
177         r_f_c2544_profile.min_rate = 100
178         self.assertEqual("1.0", r_f_c2544_profile.get_multiplier())
179
180 if __name__ == '__main__':
181     unittest.main()