1 # Copyright (c) 2016-2017 Intel Corporation
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
23 from yardstick.common import constants
24 from yardstick.network_services.traffic_profile import rfc2544
25 from yardstick.tests.unit import base
28 class TestRFC2544Profile(base.BaseUnitTestCase):
30 "schema": "isb:traffic_profile:0.1",
32 "description": "Fixed traffic profile to run UDP traffic",
34 "traffic_type": "FixedTraffic",
39 PROFILE = {'description': 'Traffic profile to run RFC2544 latency',
41 'traffic_profile': {'traffic_type': 'RFC2544Profile',
47 {'64B': '100', '1518B': '0',
48 '128B': '0', '1400B': '0',
49 '256B': '0', '373b': '0',
52 {'dstip4': '1.1.1.1-1.15.255.255',
54 'srcip4': '90.90.1.1-90.105.255.255',
55 'dscp': 0, 'ttl': 32, 'count': 1},
58 'dsrport': '1234', 'count': 1}}},
63 {'64B': '100', '1518B': '0',
64 '128B': '0', '1400B': '0',
65 '256B': '0', '373b': '0',
68 {'dstip4': '9.9.1.1-90.105.255.255',
70 'srcip4': '1.1.1.1-1.15.255.255',
71 'dscp': 0, 'ttl': 32, 'count': 1},
74 'srcport': '1234', 'count': 1}}},
75 'schema': 'isb:traffic_profile:0.1'}
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)
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()
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'],
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'}
103 with mock.patch.object(rfc2544_profile, '_create_profile') as \
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,
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'}}}
126 with mock.patch.object(rfc2544_profile, '_create_imix_data') as \
128 mock.patch.object(rfc2544_profile, '_create_vm') as \
130 mock.patch.object(rfc2544_profile, '_create_streams') as \
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,
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,
142 mock_stl_profile.assert_called_once_with(['stream1'])
144 def test__create_imix_data_mode_DIB(self):
145 rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
146 data = {'64B': 50, '128B': 50}
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}
153 {'64': 25.0, '128': 75.0},
154 rfc2544_profile._create_imix_data(
155 data, weight_mode=constants.DISTRIBUTION_IN_BYTES))
159 rfc2544_profile._create_imix_data(
160 data, weight_mode=constants.DISTRIBUTION_IN_BYTES))
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
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))
174 rfc2544_profile._create_imix_data(
175 data, weight_mode=constants.DISTRIBUTION_IN_PACKETS))
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 \
182 rfc2544_profile._create_vm(packet)
183 mock_l2_fileds.assert_called_once_with('l2_definition')
185 @mock.patch.object(trex_stl_packet_builder_scapy, 'STLPktBuilder',
186 return_value='packet')
187 def test__create_single_packet(self, mock_pktbuilder):
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,
200 self.assertEqual(output, 'packet')
202 @mock.patch.object(trex_stl_packet_builder_scapy, 'STLPktBuilder',
203 return_value='packet')
204 def test__create_single_packet_qinq(self, mock_pktbuilder):
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,
220 self.assertEqual(output, 'packet')
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}
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,
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)
244 def test_get_drop_percentage(self):
245 rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
247 {'xe1': {'tx_throughput_fps': 110,
248 'rx_throughput_fps': 101,
251 'timestamp': datetime.datetime(2000, 1, 1, 1, 1, 1, 1)},
252 'xe2': {'tx_throughput_fps': 210,
253 'rx_throughput_fps': 201,
256 'timestamp': datetime.datetime(2000, 1, 1, 1, 1, 1, 1)}},
257 {'xe1': {'tx_throughput_fps': 156,
258 'rx_throughput_fps': 108,
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,
267 'latency': 'Latency2',
268 'timestamp': datetime.datetime(2000, 1, 1, 1, 1, 1, 31)}}
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,
278 'Throughput': 1000000.0}
279 self.assertEqual(expected, output)
280 self.assertFalse(completed)
283 class PortPgIDMapTestCase(base.BaseUnitTestCase):
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])
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))
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())
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)
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)