1 # Copyright (C) 2014 Nippon Telegraph and Telephone 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
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # vim: tabstop=4 shiftwidth=4 softtabstop=4
19 from nose.tools import *
32 from random import randint
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:
41 for (clsname, clsdef, ) in inspect.getmembers(moddef):
42 if not inspect.isclass(clsdef):
44 exec('from %s import %s' % (modname, clsname))
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
60 from ryu.tests.switch.tester import TestPatterns
61 from ryu.tests.switch.tester import TestFile
62 from ryu.tests.switch.tester import OfTester
66 LOG = logging.getLogger('test_tester')
68 SAMPLE_DESC = "action: 00_OUTPUT"
71 class Test_tester(unittest.TestCase):
73 """ Test case for tester
76 # action/00_OUTPUT.json
79 "description": "ethernet/ipv4/tcp-->'actions=output:2'",
86 "OFPInstructionActions": {
90 "port": "target_send_port_1"
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), \
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'"
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), \
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'"
135 "description": "2Mbps(ethernet/ipv4/tcp)-->'in_port=1,\
136 actions=group:all(actions=output:2/actions=output:3)'",
147 "port": "target_send_port_1"
158 "port": "target_send_port_2"
175 "value": "target_recv_port"
183 "OFPInstructionActions": {
203 "ethernet(dst='22:22:22:22:22:22', \
204 src='12:11:11:11:11:11', ethertype=2048)",
207 "str('\\x11' * (1500 - 54))"
221 "value": "tester_recv_port_1"
234 "value": "tester_recv_port_2"
247 # match/00_IN_PORT.json
250 "description": "ethernet/ipv4/tcp-->'in_port=1,actions=output:2'",
261 "value": "target_recv_port"
269 "OFPInstructionActions": {
273 "port": "target_send_port_1"
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), \
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'"
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), \
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'"
314 # meter/01_DROP_00_KBPS_00_1M.json
317 "description": "2Mbps(ethernet/ipv4/tcp)-->'in_port=1,\
318 actions=meter:1Mbps(drop),output:2'",
325 "OFPMeterBandDrop": {
340 "value": "target_recv_port"
348 "OFPInstructionMeter": {
353 "OFPInstructionActions": {
357 "port": "target_send_port_1"
373 "ethernet(dst='22:22:22:22:22:22', \
374 src='12:11:11:11:11:11', ethertype=2048)",
377 "str('\\x11' * (1500 - 54))"
391 "value": "tester_recv_port_1"
406 OfTester.tester_ver = ofproto_v1_3.OFP_VERSION
407 OfTester.target_ver = ofproto_v1_3.OFP_VERSION
412 def test__normalize_test_json(self):
413 self.tests = TestPatterns(
414 "../switch/of13/action/00_OUTPUT.json",
415 logging.getLogger("test_tester"))
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)
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'])
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'])
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'])
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'])