backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / unit / app / test_tester.py
1 # Copyright (C) 2014 Nippon Telegraph and Telephone 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
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # vim: tabstop=4 shiftwidth=4 softtabstop=4
17
18 import unittest
19 from nose.tools import *
20
21 import binascii
22 import inspect
23 import json
24 import logging
25 import math
26 import netaddr
27 import os
28 import signal
29 import sys
30 import time
31 import traceback
32 from random import randint
33
34 from ryu import cfg
35
36 # import all packet libraries.
37 PKT_LIB_PATH = 'ryu.lib.packet'
38 for modname, moddef in sys.modules.items():
39     if not modname.startswith(PKT_LIB_PATH) or not moddef:
40         continue
41     for (clsname, clsdef, ) in inspect.getmembers(moddef):
42         if not inspect.isclass(clsdef):
43             continue
44         exec('from %s import %s' % (modname, clsname))
45
46 from ryu.base import app_manager
47 from ryu.controller import handler
48 from ryu.controller import ofp_event
49 from ryu.controller.handler import set_ev_cls
50 from ryu.exception import RyuException
51 from ryu.lib import dpid as dpid_lib
52 from ryu.lib import hub
53 from ryu.lib import stringify
54 from ryu.lib.packet import packet
55 from ryu.ofproto import ofproto_protocol
56 from ryu.ofproto import ofproto_v1_3
57 from ryu.ofproto import ofproto_v1_3_parser
58 from ryu.ofproto import ofproto_v1_4
59
60 from ryu.tests.switch.tester import TestPatterns
61 from ryu.tests.switch.tester import TestFile
62 from ryu.tests.switch.tester import OfTester
63
64 CONF = cfg.CONF
65
66 LOG = logging.getLogger('test_tester')
67
68 SAMPLE_DESC = "action: 00_OUTPUT"
69
70
71 class Test_tester(unittest.TestCase):
72
73     """ Test case for tester
74     """
75
76     # action/00_OUTPUT.json
77
78     test_json_1 = {
79         "description": "ethernet/ipv4/tcp-->'actions=output:2'",
80         "prerequisite": [
81             {
82                 "OFPFlowMod": {
83                     "table_id": 0,
84                     "instructions": [
85                         {
86                             "OFPInstructionActions": {
87                                 "actions": [
88                                     {
89                                         "OFPActionOutput": {
90                                             "port": "target_send_port_1"
91                                         }
92                                     }
93                                 ],
94                                 "type": 4
95                             }
96                         }
97                     ]
98                 }
99             }
100         ],
101         "tests": [
102             {
103                 "ingress": [
104                     "ethernet(dst='22:22:22:22:22:22', \
105                     src='12:11:11:11:11:11', ethertype=2048)",
106                     "ipv4(tos=32, proto=6, src='192.168.10.10', \
107                     dst='192.168.20.20', ttl=64)",
108                     "tcp(dst_port=2222, option=str('\\x00' * 4), \
109                     src_port=11111)",
110                     "'\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x0\
111                     8\\t\\n\\x0b\\x0c\\r\\x0e\\x0f\\x10\\x11\\x1\
112                     2\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1\
113                     b\\x1c\\x1d\\x1e\\x1f'"
114                 ],
115                 "egress":[
116                     "ethernet(dst='22:22:22:22:22:22', \
117                     src='12:11:11:11:11:11', ethertype=2048)",
118                     "ipv4(tos=32, proto=6, src='192.168.10.10', \
119                     dst='192.168.20.20', ttl=64)",
120                     "tcp(dst_port=2222, option=str('\\x00' * 4), \
121                     src_port=11111)",
122                     "'\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x0\
123                     8\\t\\n\\x0b\\x0c\\r\\x0e\\x0f\\x10\\x11\\x1\
124                     2\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1\
125                     b\\x1c\\x1d\\x1e\\x1f'"
126                 ]
127
128             }
129         ]
130     }
131
132     # group/00_ALL.json
133
134     test_json_2 = {
135         "description": "2Mbps(ethernet/ipv4/tcp)-->'in_port=1,\
136             actions=group:all(actions=output:2/actions=output:3)'",
137         "prerequisite": [
138             {
139                 "OFPGroupMod": {
140                     "group_id": 0,
141                     "buckets": [
142                         {
143                             "OFPBucket": {
144                                 "actions": [
145                                     {
146                                         "OFPActionOutput": {
147                                             "port": "target_send_port_1"
148                                         }
149                                     }
150                                 ]
151                             }
152                         },
153                         {
154                             "OFPBucket": {
155                                 "actions": [
156                                     {
157                                         "OFPActionOutput": {
158                                             "port": "target_send_port_2"
159                                         }
160                                     }
161                                 ]
162                             }
163                         }
164                     ]
165                 }
166             },
167             {
168                 "OFPFlowMod": {
169                     "match": {
170                         "OFPMatch": {
171                             "oxm_fields": [
172                                 {
173                                     "OXMTlv": {
174                                         "field": "in_port",
175                                         "value": "target_recv_port"
176                                     }
177                                 }
178                             ]
179                         }
180                     },
181                     "instructions": [
182                         {
183                             "OFPInstructionActions": {
184                                 "actions": [
185                                     {
186                                         "OFPActionGroup": {
187                                             "group_id": 0
188                                         }
189                                     }
190                                 ],
191                                 "type": 4
192                             }
193                         }
194                     ]
195                 }
196             }
197         ],
198         "tests": [
199             {
200                 "ingress": {
201                     "packets": {
202                         "data": [
203                             "ethernet(dst='22:22:22:22:22:22', \
204                             src='12:11:11:11:11:11', ethertype=2048)",
205                             "ipv4(proto=6)",
206                             "tcp()",
207                             "str('\\x11' * (1500 - 54))"
208                         ],
209                         "pktps":175,
210                         "duration_time":30
211                     }
212                 },
213                 "egress":{
214                     "throughput": [
215                         {
216                             "OFPMatch": {
217                                 "oxm_fields": [
218                                     {
219                                         "OXMTlv": {
220                                             "field": "in_port",
221                                             "value": "tester_recv_port_1"
222                                         }
223                                     }
224                                 ]
225                             },
226                             "kbps": 2000
227                         },
228                         {
229                             "OFPMatch": {
230                                 "oxm_fields": [
231                                     {
232                                         "OXMTlv": {
233                                             "field": "in_port",
234                                             "value": "tester_recv_port_2"
235                                         }
236                                     }
237                                 ]
238                             },
239                             "kbps": 2000
240                         }
241                     ]
242                 }
243             }
244         ]
245     }
246
247     # match/00_IN_PORT.json
248
249     test_json_3 = {
250         "description": "ethernet/ipv4/tcp-->'in_port=1,actions=output:2'",
251         "prerequisite": [
252             {
253                 "OFPFlowMod": {
254                     "table_id": 0,
255                     "match": {
256                         "OFPMatch": {
257                             "oxm_fields": [
258                                 {
259                                     "OXMTlv": {
260                                         "field": "in_port",
261                                         "value": "target_recv_port"
262                                     }
263                                 }
264                             ]
265                         }
266                     },
267                     "instructions": [
268                         {
269                             "OFPInstructionActions": {
270                                 "actions": [
271                                     {
272                                         "OFPActionOutput": {
273                                             "port": "target_send_port_1"
274                                         }
275                                     }
276                                 ],
277                                 "type": 4
278                             }
279                         }
280                     ]
281                 }
282             }
283         ],
284         "tests": [
285             {
286                 "ingress": [
287                     "ethernet(dst='22:22:22:22:22:22', \
288                     src='12:11:11:11:11:11', ethertype=2048)",
289                     "ipv4(tos=32, proto=6, src='192.168.10.10', \
290                     dst='192.168.20.20', ttl=64)",
291                     "tcp(dst_port=2222, option=str('\\x00' * 4), \
292                     src_port=11111)",
293                     "'\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x0\
294                     8\\t\\n\\x0b\\x0c\\r\\x0e\\x0f\\x10\\x11\\x1\
295                     2\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1\
296                     b\\x1c\\x1d\\x1e\\x1f'"
297                 ],
298                 "egress":[
299                     "ethernet(dst='22:22:22:22:22:22', \
300                     src='12:11:11:11:11:11', ethertype=2048)",
301                     "ipv4(tos=32, proto=6, src='192.168.10.10',\
302                      dst='192.168.20.20', ttl=64)",
303                     "tcp(dst_port=2222, option=str('\\x00' * 4), \
304                     src_port=11111)",
305                     "'\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x0\
306                     8\\t\\n\\x0b\\x0c\\r\\x0e\\x0f\\x10\\x11\\x1\
307                     2\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1\
308                     b\\x1c\\x1d\\x1e\\x1f'"
309                 ]
310             }
311         ]
312     }
313
314     # meter/01_DROP_00_KBPS_00_1M.json
315
316     test_json_4 = {
317         "description": "2Mbps(ethernet/ipv4/tcp)-->'in_port=1,\
318         actions=meter:1Mbps(drop),output:2'",
319         "prerequisite": [
320             {
321                 "OFPMeterMod": {
322                     "meter_id": 1,
323                     "bands": [
324                         {
325                             "OFPMeterBandDrop": {
326                                 "rate": 1000
327                             }
328                         }
329                     ]
330                 }
331             },
332             {
333                 "OFPFlowMod": {
334                     "match": {
335                         "OFPMatch": {
336                             "oxm_fields": [
337                                 {
338                                     "OXMTlv": {
339                                         "field": "in_port",
340                                         "value": "target_recv_port"
341                                     }
342                                 }
343                             ]
344                         }
345                     },
346                     "instructions": [
347                         {
348                             "OFPInstructionMeter": {
349                                 "meter_id": 1
350                             }
351                         },
352                         {
353                             "OFPInstructionActions": {
354                                 "actions": [
355                                     {
356                                         "OFPActionOutput": {
357                                             "port": "target_send_port_1"
358                                         }
359                                     }
360                                 ],
361                                 "type": 4
362                             }
363                         }
364                     ]
365                 }
366             }
367         ],
368         "tests": [
369             {
370                 "ingress": {
371                     "packets": {
372                         "data": [
373                             "ethernet(dst='22:22:22:22:22:22', \
374                             src='12:11:11:11:11:11', ethertype=2048)",
375                             "ipv4(proto=6)",
376                             "tcp()",
377                             "str('\\x11' * (1500 - 54))"
378                         ],
379                         "pktps":175,
380                         "duration_time":30
381                     }
382                 },
383                 "egress":{
384                     "throughput": [
385                         {
386                             "OFPMatch": {
387                                 "oxm_fields": [
388                                     {
389                                         "OXMTlv": {
390                                             "field": "in_port",
391                                             "value": "tester_recv_port_1"
392                                         }
393
394                                     }
395                                 ]
396                             },
397                             "kbps": 1000
398                         }
399                     ]
400                 }
401             }
402         ]
403     }
404
405     def setUp(self):
406         OfTester.tester_ver = ofproto_v1_3.OFP_VERSION
407         OfTester.target_ver = ofproto_v1_3.OFP_VERSION
408
409     def tearDown(self):
410         pass
411
412     def test__normalize_test_json(self):
413         self.tests = TestPatterns(
414             "../switch/of13/action/00_OUTPUT.json",
415             logging.getLogger("test_tester"))
416
417         self.tests[SAMPLE_DESC]._normalize_test_json(Test_tester.test_json_1)
418         self.tests[SAMPLE_DESC]._normalize_test_json(Test_tester.test_json_2)
419         self.tests[SAMPLE_DESC]._normalize_test_json(Test_tester.test_json_3)
420         self.tests[SAMPLE_DESC]._normalize_test_json(Test_tester.test_json_4)
421
422         # action/00_OUTPUT.json
423         eq_(Test_tester.test_json_1["prerequisite"][0]["OFPFlowMod"][
424             "instructions"][0]["OFPInstructionActions"][
425             "actions"][0]["OFPActionOutput"]["port"],
426             CONF['test-switch']['target_send_port_1'])
427
428         # group/00_ALL.json
429         eq_(Test_tester.test_json_2["prerequisite"][1]["OFPFlowMod"][
430             "match"]["OFPMatch"]["oxm_fields"][0]["OXMTlv"]["value"],
431             CONF['test-switch']['target_recv_port'])
432         eq_(Test_tester.test_json_2["prerequisite"][0]["OFPGroupMod"][
433             "buckets"][0]["OFPBucket"]["actions"][0]["OFPActionOutput"][
434             "port"], CONF['test-switch']['target_send_port_1'])
435         eq_(Test_tester.test_json_2["prerequisite"][0]["OFPGroupMod"][
436             "buckets"][1]["OFPBucket"]["actions"][0]["OFPActionOutput"][
437             "port"], CONF['test-switch']['target_send_port_2'])
438         eq_(Test_tester.test_json_2["tests"][0]["egress"]["throughput"][
439             0]["OFPMatch"]["oxm_fields"][0]["OXMTlv"]["value"],
440             CONF['test-switch']['tester_recv_port_1'])
441         eq_(Test_tester.test_json_2["tests"][0]["egress"]["throughput"][
442             1]["OFPMatch"]["oxm_fields"][0]["OXMTlv"]["value"],
443             CONF['test-switch']['tester_recv_port_2'])
444
445         # match/00_IN_PORT.json
446         eq_(Test_tester.test_json_3["prerequisite"][0]["OFPFlowMod"][
447             "match"]["OFPMatch"]["oxm_fields"][0]["OXMTlv"]["value"],
448             CONF['test-switch']['target_recv_port'])
449         eq_(Test_tester.test_json_3["prerequisite"][0]["OFPFlowMod"][
450             "instructions"][0]["OFPInstructionActions"]["actions"][0][
451             "OFPActionOutput"]["port"], CONF['test-switch'][
452             'target_send_port_1'])
453
454         # meter/01_DROP_00_KBPS_00_1M.json
455         eq_(Test_tester.test_json_4["prerequisite"][1]["OFPFlowMod"][
456             "match"]["OFPMatch"]["oxm_fields"][0]["OXMTlv"]["value"],
457             CONF['test-switch']['target_recv_port'])
458         eq_(Test_tester.test_json_4["prerequisite"][1]["OFPFlowMod"][
459             "instructions"][1]["OFPInstructionActions"]["actions"][0][
460             "OFPActionOutput"]["port"],
461             CONF['test-switch']['target_send_port_1'])
462         eq_(Test_tester.test_json_4["tests"][0]["egress"]["throughput"][
463             0]["OFPMatch"]["oxm_fields"][0]["OXMTlv"]["value"],
464             CONF['test-switch']['tester_recv_port_1'])