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