Merge "Add source and destination seed value in IXIA RFC2544"
[yardstick.git] / yardstick / tests / unit / network_services / traffic_profile / test_rfc2544.py
1 # Copyright (c) 2016-2017 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 datetime
16
17 import mock
18 from trex_stl_lib import api as Pkt
19 from trex_stl_lib import trex_stl_client
20 from trex_stl_lib import trex_stl_packet_builder_scapy
21 from trex_stl_lib import trex_stl_streams
22
23 from yardstick.common import constants
24 from yardstick.network_services.traffic_profile import rfc2544
25 from yardstick.tests.unit import base
26
27
28 class TestRFC2544Profile(base.BaseUnitTestCase):
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,
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                'downlink_0':
44                    {'ipv4':
45                         {'outer_l2':
46                              {'framesize':
47                                   {'64B': '100', '1518B': '0',
48                                    '128B': '0', '1400B': '0',
49                                    '256B': '0', '373b': '0',
50                                    '570B': '0'}},
51                          'outer_l3v4':
52                              {'dstip4': '1.1.1.1-1.15.255.255',
53                               'proto': 'udp',
54                               'srcip4': '90.90.1.1-90.105.255.255',
55                               'dscp': 0, 'ttl': 32, 'count': 1},
56                          'outer_l4':
57                              {'srcport': '2001',
58                               'dsrport': '1234', 'count': 1}}},
59                'uplink_0':
60                    {'ipv4':
61                         {'outer_l2':
62                              {'framesize':
63                                   {'64B': '100', '1518B': '0',
64                                    '128B': '0', '1400B': '0',
65                                    '256B': '0', '373b': '0',
66                                    '570B': '0'}},
67                          'outer_l3v4':
68                              {'dstip4': '9.9.1.1-90.105.255.255',
69                               'proto': 'udp',
70                               'srcip4': '1.1.1.1-1.15.255.255',
71                               'dscp': 0, 'ttl': 32, 'count': 1},
72                          'outer_l4':
73                              {'dstport': '2001',
74                               'srcport': '1234', 'count': 1}}},
75                'schema': 'isb:traffic_profile:0.1'}
76
77     def test___init__(self):
78         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
79         self.assertEqual(rfc2544_profile.max_rate, rfc2544_profile.rate)
80         self.assertEqual(0, rfc2544_profile.min_rate)
81
82     def test_stop_traffic(self):
83         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
84         mock_generator = mock.Mock()
85         rfc2544_profile.stop_traffic(traffic_generator=mock_generator)
86         mock_generator.client.stop.assert_called_once()
87         mock_generator.client.reset.assert_called_once()
88         mock_generator.client.remove_all_streams.assert_called_once()
89
90     def test_execute_traffic(self):
91         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
92         mock_generator = mock.Mock()
93         mock_generator.networks = {
94             'downlink_0': ['xe0', 'xe1'],
95             'uplink_0': ['xe2', 'xe3'],
96             'downlink_1': []}
97         mock_generator.port_num.side_effect = [10, 20, 30, 40]
98         mock_generator.rfc2544_helper.correlated_traffic = False
99         rfc2544_profile.params = {
100             'downlink_0': 'profile1',
101             'uplink_0': 'profile2'}
102
103         with mock.patch.object(rfc2544_profile, '_create_profile') as \
104                 mock_create_profile:
105             rfc2544_profile.execute_traffic(traffic_generator=mock_generator)
106         mock_create_profile.assert_has_calls([
107             mock.call('profile1', rfc2544_profile.rate, mock.ANY, False),
108             mock.call('profile1', rfc2544_profile.rate, mock.ANY, False),
109             mock.call('profile2', rfc2544_profile.rate, mock.ANY, False),
110             mock.call('profile2', rfc2544_profile.rate, mock.ANY, False)])
111         mock_generator.client.add_streams.assert_has_calls([
112             mock.call(mock.ANY, ports=[10]),
113             mock.call(mock.ANY, ports=[20]),
114             mock.call(mock.ANY, ports=[30]),
115             mock.call(mock.ANY, ports=[40])])
116         mock_generator.client.start(ports=[10, 20, 30, 40],
117                                     duration=rfc2544_profile.config.duration,
118                                     force=True)
119
120     @mock.patch.object(trex_stl_streams, 'STLProfile')
121     def test__create_profile(self, mock_stl_profile):
122         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
123         port_pg_id = mock.ANY
124         profile_data = {'packetid_1': {'outer_l2': {'framesize': 'imix_info'}}}
125         rate = 100
126         with mock.patch.object(rfc2544_profile, '_create_imix_data') as \
127                 mock_create_imix, \
128                 mock.patch.object(rfc2544_profile, '_create_vm') as \
129                 mock_create_vm, \
130                 mock.patch.object(rfc2544_profile, '_create_streams') as \
131                 mock_create_streams:
132             mock_create_imix.return_value = 'imix_data'
133             mock_create_streams.return_value = ['stream1']
134             rfc2544_profile._create_profile(profile_data, rate, port_pg_id,
135                                             True)
136
137         mock_create_imix.assert_called_once_with('imix_info')
138         mock_create_vm.assert_called_once_with(
139             {'outer_l2': {'framesize': 'imix_info'}})
140         mock_create_streams.assert_called_once_with('imix_data', 100,
141                                                     port_pg_id, True)
142         mock_stl_profile.assert_called_once_with(['stream1'])
143
144     def test__create_imix_data_mode_DIB(self):
145         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
146         data = {'64B': 50, '128B': 50}
147         self.assertEqual(
148             {'64': 50.0, '128': 50.0},
149             rfc2544_profile._create_imix_data(
150                 data, weight_mode=constants.DISTRIBUTION_IN_BYTES))
151         data = {'64B': 1, '128b': 3}
152         self.assertEqual(
153             {'64': 25.0, '128': 75.0},
154             rfc2544_profile._create_imix_data(
155                 data, weight_mode=constants.DISTRIBUTION_IN_BYTES))
156         data = {}
157         self.assertEqual(
158             {},
159             rfc2544_profile._create_imix_data(
160                 data, weight_mode=constants.DISTRIBUTION_IN_BYTES))
161
162     def test__create_imix_data_mode_DIP(self):
163         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
164         data = {'64B': 25, '128B': 25, '512B': 25, '1518B': 25}
165         byte_total = 64 * 25 + 128 * 25 + 512 * 25 + 1518 * 25
166         self.assertEqual(
167             {'64': 64 * 25.0 / byte_total, '128': 128 * 25.0 / byte_total,
168              '512': 512 * 25.0 / byte_total, '1518': 1518 * 25.0 / byte_total},
169             rfc2544_profile._create_imix_data(
170                 data, weight_mode=constants.DISTRIBUTION_IN_PACKETS))
171         data = {}
172         self.assertEqual(
173             {},
174             rfc2544_profile._create_imix_data(
175                 data, weight_mode=constants.DISTRIBUTION_IN_PACKETS))
176
177     def test__create_vm(self):
178         packet = {'outer_l2': 'l2_definition'}
179         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
180         with mock.patch.object(rfc2544_profile, '_set_outer_l2_fields') as \
181                 mock_l2_fileds:
182             rfc2544_profile._create_vm(packet)
183         mock_l2_fileds.assert_called_once_with('l2_definition')
184
185     @mock.patch.object(trex_stl_packet_builder_scapy, 'STLPktBuilder',
186                        return_value='packet')
187     def test__create_single_packet(self, mock_pktbuilder):
188         size = 128
189         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
190         rfc2544_profile.ether_packet = Pkt.Eth()
191         rfc2544_profile.ip_packet = Pkt.IP()
192         rfc2544_profile.udp_packet = Pkt.UDP()
193         rfc2544_profile.trex_vm = 'trex_vm'
194         base_pkt = (rfc2544_profile.ether_packet / rfc2544_profile.ip_packet /
195                     rfc2544_profile.udp_packet)
196         pad = (size - len(base_pkt)) * 'x'
197         output = rfc2544_profile._create_single_packet(size=size)
198         mock_pktbuilder.assert_called_once_with(pkt=base_pkt / pad,
199                                                 vm='trex_vm')
200         self.assertEqual(output, 'packet')
201
202     @mock.patch.object(trex_stl_packet_builder_scapy, 'STLPktBuilder',
203                        return_value='packet')
204     def test__create_single_packet_qinq(self, mock_pktbuilder):
205         size = 128
206         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
207         rfc2544_profile.ether_packet = Pkt.Eth()
208         rfc2544_profile.ip_packet = Pkt.IP()
209         rfc2544_profile.udp_packet = Pkt.UDP()
210         rfc2544_profile.trex_vm = 'trex_vm'
211         rfc2544_profile.qinq = True
212         rfc2544_profile.qinq_packet = Pkt.Dot1Q(vlan=1) / Pkt.Dot1Q(vlan=2)
213         base_pkt = (rfc2544_profile.ether_packet /
214                     rfc2544_profile.qinq_packet / rfc2544_profile.ip_packet /
215                     rfc2544_profile.udp_packet)
216         pad = (size - len(base_pkt)) * 'x'
217         output = rfc2544_profile._create_single_packet(size=size)
218         mock_pktbuilder.assert_called_once_with(pkt=base_pkt / pad,
219                                                 vm='trex_vm')
220         self.assertEqual(output, 'packet')
221
222     @mock.patch.object(trex_stl_streams, 'STLFlowLatencyStats')
223     @mock.patch.object(trex_stl_streams, 'STLTXCont')
224     @mock.patch.object(trex_stl_client, 'STLStream')
225     def test__create_streams(self, mock_stream, mock_txcont, mock_latency):
226         imix_data = {'64': 25, '512': 75}
227         rate = 35
228         port_pg_id = rfc2544.PortPgIDMap()
229         port_pg_id.add_port(10)
230         mock_stream.side_effect = ['stream1', 'stream2']
231         mock_txcont.side_effect = ['txcont1', 'txcont2']
232         mock_latency.side_effect = ['latency1', 'latency2']
233         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
234         with mock.patch.object(rfc2544_profile, '_create_single_packet'):
235             output = rfc2544_profile._create_streams(imix_data, rate,
236                                                      port_pg_id, True)
237         self.assertEqual(['stream1', 'stream2'], output)
238         mock_latency.assert_has_calls([
239             mock.call(pg_id=1), mock.call(pg_id=2)])
240         mock_txcont.assert_has_calls([
241             mock.call(percentage=float(25 * 35) / 100),
242             mock.call(percentage=float(75 * 35) / 100)], any_order=True)
243
244     def test_get_drop_percentage(self):
245         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
246         samples = [
247             {'xe1': {'tx_throughput_fps': 110,
248                      'rx_throughput_fps': 101,
249                      'out_packets': 2100,
250                      'in_packets': 2010,
251                      'timestamp': datetime.datetime(2000, 1, 1, 1, 1, 1, 1)},
252              'xe2': {'tx_throughput_fps': 210,
253                      'rx_throughput_fps': 201,
254                      'out_packets': 4100,
255                      'in_packets': 4010,
256                      'timestamp': datetime.datetime(2000, 1, 1, 1, 1, 1, 1)}},
257             {'xe1': {'tx_throughput_fps': 156,
258                      'rx_throughput_fps': 108,
259                      'out_packets': 2110,
260                      'in_packets': 2040,
261                      'latency': 'Latency1',
262                      'timestamp': datetime.datetime(2000, 1, 1, 1, 1, 1, 31)},
263              'xe2': {'tx_throughput_fps': 253,
264                      'rx_throughput_fps': 215,
265                      'out_packets': 4150,
266                      'in_packets': 4010,
267                      'latency': 'Latency2',
268                      'timestamp': datetime.datetime(2000, 1, 1, 1, 1, 1, 31)}}
269         ]
270         completed, output = rfc2544_profile.get_drop_percentage(
271             samples, 0, 0, False)
272         expected = {'DropPercentage': 50.0,
273                     'Latency': {'xe1': 'Latency1', 'xe2': 'Latency2'},
274                     'RxThroughput': 1000000.0,
275                     'TxThroughput': 2000000.0,
276                     'CurrentDropPercentage': 50.0,
277                     'Rate': 100.0,
278                     'Throughput': 1000000.0}
279         self.assertEqual(expected, output)
280         self.assertFalse(completed)
281
282
283 class PortPgIDMapTestCase(base.BaseUnitTestCase):
284
285     def test_add_port(self):
286         port_pg_id_map = rfc2544.PortPgIDMap()
287         port_pg_id_map.add_port(10)
288         self.assertEqual(10, port_pg_id_map._last_port)
289         self.assertEqual([], port_pg_id_map._port_pg_id_map[10])
290
291     def test_get_pg_ids(self):
292         port_pg_id_map = rfc2544.PortPgIDMap()
293         port_pg_id_map.add_port(10)
294         port_pg_id_map.increase_pg_id()
295         port_pg_id_map.increase_pg_id()
296         port_pg_id_map.add_port(20)
297         port_pg_id_map.increase_pg_id()
298         self.assertEqual([1, 2], port_pg_id_map.get_pg_ids(10))
299         self.assertEqual([3], port_pg_id_map.get_pg_ids(20))
300         self.assertEqual([], port_pg_id_map.get_pg_ids(30))
301
302     def test_increase_pg_id_no_port(self):
303         port_pg_id_map = rfc2544.PortPgIDMap()
304         self.assertIsNone(port_pg_id_map.increase_pg_id())
305
306     def test_increase_pg_id_last_port(self):
307         port_pg_id_map = rfc2544.PortPgIDMap()
308         port_pg_id_map.add_port(10)
309         self.assertEqual(1, port_pg_id_map.increase_pg_id())
310         self.assertEqual([1], port_pg_id_map.get_pg_ids(10))
311         self.assertEqual(10, port_pg_id_map._last_port)
312
313     def test_increase_pg_id(self):
314         port_pg_id_map = rfc2544.PortPgIDMap()
315         port_pg_id_map.add_port(10)
316         port_pg_id_map.increase_pg_id()
317         self.assertEqual(2, port_pg_id_map.increase_pg_id(port=20))
318         self.assertEqual([1], port_pg_id_map.get_pg_ids(10))
319         self.assertEqual([2], port_pg_id_map.get_pg_ids(20))
320         self.assertEqual(20, port_pg_id_map._last_port)