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