Merge "Adding intel_pmu tools plugin for collectd"
[yardstick.git] / tests / unit / network_services / traffic_profile / test_traffic_profile.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
20 import unittest
21 import mock
22
23 from tests.unit import STL_MOCKS
24
25
26 STLClient = mock.MagicMock()
27 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
28 stl_patch.start()
29
30 if stl_patch:
31     from yardstick.network_services.traffic_profile.base import TrafficProfile
32     from yardstick.network_services.traffic_profile.traffic_profile import \
33         TrexProfile
34
35
36 class TestTrexProfile(unittest.TestCase):
37     TRAFFIC_PROFILE = {
38         "schema": "isb:traffic_profile:0.1",
39         "name": "fixed",
40         "description": "Fixed traffic profile to run UDP traffic",
41         "traffic_profile": {
42             "traffic_type": "FixedTraffic",
43             "frame_rate": 100,  # pps
44             "flow_number": 10,
45             "frame_size": 64}}
46
47     PROFILE = {'description': 'Traffic profile to run RFC2544 latency',
48                'name': 'rfc2544',
49                'traffic_profile': {'traffic_type': 'RFC2544Profile',
50                                    'frame_rate': 100},
51                'public': {'ipv4': {'outer_l2': {'framesize': {'64B': '100',
52                                                               '1518B': '0',
53                                                               '128B': '0',
54                                                               '1400B': '0',
55                                                               '256B': '0',
56                                                               '373b': '0',
57                                                               '570B': '0'},
58                                                 "srcmac": "00:00:00:00:00:02",
59                                                 "dstmac": "00:00:00:00:00:01"},
60                                    'outer_l3v4': {'dstip4': '1.1.1.1-1.1.2.2',
61                                                   'proto': 'udp',
62                                                   'srcip4': '9.9.1.1-90.1.2.2',
63                                                   'dscp': 0, 'ttl': 32,
64                                                   'count': 1},
65                                    'outer_l4': {'srcport': '2001',
66                                                 'dsrport': '1234',
67                                                 'count': 1}}},
68                'private': {'ipv4':
69                            {'outer_l2': {'framesize':
70                                          {'64B': '100', '1518B': '0',
71                                           '128B': '0', '1400B': '0',
72                                           '256B': '0', '373b': '0',
73                                           '570B': '0'},
74                                          "srcmac": "00:00:00:00:00:01",
75                                          "dstmac": "00:00:00:00:00:02"},
76                             'outer_l3v4': {'dstip4': '9.9.1.1-90.105.255.255',
77                                            'proto': 'udp',
78                                            'srcip4': '1.1.1.1-1.15.255.255',
79                                            'dscp': 0, 'ttl': 32, 'count': 1},
80                             'outer_l4': {'dstport': '2001',
81                                          'srcport': '1234',
82                                          'count': 1}}},
83                'schema': 'isb:traffic_profile:0.1'}
84     PROFILE_v6 = {'description': 'Traffic profile to run RFC2544 latency',
85                   'name': 'rfc2544',
86                   'traffic_profile': {'traffic_type': 'RFC2544Profile',
87                                       'frame_rate': 100},
88                   'public': {'ipv6': {'outer_l2': {'framesize':
89                                                    {'64B': '100', '1518B': '0',
90                                                     '128B': '0', '1400B': '0',
91                                                     '256B': '0', '373b': '0',
92                                                     '570B': '0'},
93                                                    "srcmac": "00:00:00:00:00:02",
94                                                    "dstmac": "00:00:00:00:00:01"},
95                                       'outer_l3v4': {'dstip6': '0064:ff9b:0:0:0:0:9810:6414-0064:ff9b:0:0:0:0:9810:6420',
96                                                      'proto': 'udp',
97                                                      'srcip6': '0064:ff9b:0:0:0:0:9810:2814-0064:ff9b:0:0:0:0:9810:2820',
98                                                      'dscp': 0, 'ttl': 32,
99                                                      'count': 1},
100                                       'outer_l4': {'srcport': '2001',
101                                                    'dsrport': '1234',
102                                                    'count': 1}}},
103                   'private':
104                   {'ipv6': {'outer_l2': {'framesize':
105                                          {'64B': '100', '1518B': '0',
106                                           '128B': '0', '1400B': '0',
107                                           '256B': '0', '373b': '0',
108                                           '570B': '0'},
109                                          "srcmac": "00:00:00:00:00:01",
110                                          "dstmac": "00:00:00:00:00:02"},
111                             'outer_l3v4': {'dstip6': '0064:ff9b:0:0:0:0:9810:2814-0064:ff9b:0:0:0:0:9810:2820',
112                                            'proto': 'udp',
113                                            'srcip6': '0064:ff9b:0:0:0:0:9810:6414-0064:ff9b:0:0:0:0:9810:6420',
114                                            'dscp': 0, 'ttl': 32,
115                                            'count': 1},
116                             'outer_l4': {'dstport': '2001',
117                                          'srcport': '1234',
118                                          'count': 1}}},
119                   'schema': 'isb:traffic_profile:0.1'}
120
121     def test___init__(self):
122         TrafficProfile.params = self.PROFILE
123         trex_profile = \
124             TrexProfile(TrafficProfile)
125         self.assertEqual(trex_profile.pps, 100)
126
127     def test_execute(self):
128         trex_profile = \
129             TrexProfile(TrafficProfile)
130         self.assertEqual(None, trex_profile.execute({}))
131
132     def test_set_src_mac(self):
133         src_mac = "00:00:00:00:00:01"
134         trex_profile = \
135             TrexProfile(TrafficProfile)
136         self.assertEqual(None, trex_profile.set_src_mac(src_mac))
137
138         src_mac = "00:00:00:00:00:01-00:00:00:00:00:02"
139         self.assertEqual(None, trex_profile.set_src_mac(src_mac))
140
141     def test_set_dst_mac(self):
142         dst_mac = "00:00:00:00:00:03"
143         trex_profile = \
144             TrexProfile(TrafficProfile)
145         self.assertEqual(None, trex_profile.set_dst_mac(dst_mac))
146
147         dst_mac = "00:00:00:00:00:03-00:00:00:00:00:04"
148         self.assertEqual(None, trex_profile.set_dst_mac(dst_mac))
149
150     def test_set_src_ip4(self):
151         src_ipv4 = "152.16.100.20"
152         trex_profile = \
153             TrexProfile(TrafficProfile)
154         self.assertEqual(None, trex_profile.set_src_ip4(src_ipv4))
155
156         src_ipv4 = "152.16.100.20-152.16.100.30"
157         self.assertEqual(None, trex_profile.set_src_ip4(src_ipv4))
158
159     def test_set_dst_ip4(self):
160         dst_ipv4 = "152.16.100.20"
161         trex_profile = \
162             TrexProfile(TrafficProfile)
163         self.assertEqual(None, trex_profile.set_dst_ip4(dst_ipv4))
164
165         dst_ipv4 = "152.16.100.20-152.16.100.30"
166         self.assertEqual(None, trex_profile.set_dst_ip4(dst_ipv4))
167
168     def test_set_src_ip6(self):
169         src_ipv6 = "0064:ff9b:0:0:0:0:9810:6414"
170         trex_profile = \
171             TrexProfile(TrafficProfile)
172         self.assertEqual(None, trex_profile.set_src_ip6(src_ipv6))
173
174         src_ipv6 = "0064:ff9b:0:0:0:0:9810:6414-0064:ff9b:0:0:0:0:9810:6420"
175         self.assertEqual(None, trex_profile.set_src_ip6(src_ipv6))
176
177     def test_set_dst_ip6(self):
178         dst_ipv6 = "0064:ff9b:0:0:0:0:9810:6414"
179         trex_profile = \
180             TrexProfile(TrafficProfile)
181         self.assertEqual(None, trex_profile.set_dst_ip6(dst_ipv6))
182
183         dst_ipv6 = "0064:ff9b:0:0:0:0:9810:6414-0064:ff9b:0:0:0:0:9810:6420"
184         self.assertEqual(None, trex_profile.set_dst_ip6(dst_ipv6))
185
186     def test_dscp(self):
187         dscp = "0"
188         trex_profile = \
189             TrexProfile(TrafficProfile)
190         self.assertEqual(None, trex_profile.set_dscp(dscp))
191
192         dscp = "0-1"
193         self.assertEqual(None, trex_profile.set_dscp(dscp))
194
195     def test_src_port(self):
196         port = "1234"
197         trex_profile = \
198             TrexProfile(TrafficProfile)
199         self.assertEqual(None, trex_profile.set_src_port(port))
200
201         port = "1234-5678"
202         self.assertEqual(None, trex_profile.set_src_port(port))
203
204     def test_dst_port(self):
205         port = "1234"
206         trex_profile = \
207             TrexProfile(TrafficProfile)
208         self.assertEqual(None, trex_profile.set_dst_port(port))
209
210         port = "1234-5678"
211         self.assertEqual(None, trex_profile.set_dst_port(port))
212
213     def test_qinq(self):
214         qinq = {"S-VLAN": {"id": 128, "priority": 0, "cfi": 0},
215                 "C-VLAN": {"id": 512, "priority": 0, "cfi": 0}}
216
217         trex_profile = \
218             TrexProfile(TrafficProfile)
219         self.assertEqual(None, trex_profile.set_qinq(qinq))
220
221         qinq = {"S-VLAN": {"id": "128-130", "priority": 0, "cfi": 0},
222                 "C-VLAN": {"id": "512-515", "priority": 0, "cfi": 0}}
223         self.assertEqual(None, trex_profile.set_qinq(qinq))
224
225     def test_set_outer_l2_fields(self):
226         trex_profile = \
227             TrexProfile(TrafficProfile)
228         qinq = {"S-VLAN": {"id": 128, "priority": 0, "cfi": 0},
229                 "C-VLAN": {"id": 512, "priority": 0, "cfi": 0}}
230         outer_l2 = self.PROFILE['private']['ipv4']['outer_l2']
231         outer_l2['QinQ'] = qinq
232         self.assertEqual(None, trex_profile.set_outer_l2_fields(outer_l2))
233
234     def test_set_outer_l3v4_fields(self):
235         trex_profile = \
236             TrexProfile(TrafficProfile)
237         outer_l3v4 = self.PROFILE['private']['ipv4']['outer_l3v4']
238         outer_l3v4['proto'] = 'tcp'
239         self.assertEqual(None, trex_profile.set_outer_l3v4_fields(outer_l3v4))
240
241     def test_set_outer_l3v6_fields(self):
242         trex_profile = \
243             TrexProfile(TrafficProfile)
244         outer_l3v6 = self.PROFILE_v6['private']['ipv6']['outer_l3v4']
245         outer_l3v6['proto'] = 'tcp'
246         outer_l3v6['tc'] = 1
247         outer_l3v6['hlim'] = 10
248         self.assertEqual(None, trex_profile.set_outer_l3v6_fields(outer_l3v6))
249
250     def test_set_outer_l4_fields(self):
251         trex_profile = \
252             TrexProfile(TrafficProfile)
253         outer_l4 = self.PROFILE['private']['ipv4']['outer_l4']
254         self.assertEqual(None, trex_profile.set_outer_l4_fields(outer_l4))
255
256     def test_get_streams(self):
257         trex_profile = \
258             TrexProfile(TrafficProfile)
259         trex_profile.params = self.PROFILE
260         profile_data = self.PROFILE["private"]
261         self.assertIsNotNone(trex_profile.get_streams(profile_data))
262         trex_profile.pg_id = 1
263         self.assertIsNotNone(trex_profile.get_streams(profile_data))
264         trex_profile.params = self.PROFILE_v6
265         trex_profile.profile_data = self.PROFILE_v6["private"]
266         self.assertIsNotNone(trex_profile.get_streams(profile_data))
267         trex_profile.pg_id = 1
268         self.assertIsNotNone(trex_profile.get_streams(profile_data))
269
270     def test_generate_packets(self):
271         trex_profile = \
272             TrexProfile(TrafficProfile)
273         trex_profile.fsize = 10
274         trex_profile.base_pkt = [10]
275         self.assertIsNone(trex_profile.generate_packets())
276
277     def test_generate_imix_data_error(self):
278         trex_profile = \
279             TrexProfile(TrafficProfile)
280         self.assertEqual({}, trex_profile.generate_imix_data(False))
281
282     def test__get_start_end_ipv6(self):
283         trex_profile = \
284             TrexProfile(TrafficProfile)
285         self.assertRaises(SystemExit, trex_profile._get_start_end_ipv6,
286                           "1.1.1.3", "1.1.1.1")