blob: ec9b3512007ff8318c77d130aabfa62068537f31 [file] [log] [blame]
Rich Laneb90a1c42012-10-05 09:16:05 -07001"""
2Base classes for test cases
3
4Tests will usually inherit from one of these classes to have the controller
5and/or dataplane automatically set up.
6"""
7
8import logging
9import unittest
Rich Lane472aaea2013-08-27 09:27:38 -070010import os
Rich Laneb90a1c42012-10-05 09:16:05 -070011
Rich Lane2c7812c2012-12-27 17:52:23 -080012import oftest
Rich Laneb90a1c42012-10-05 09:16:05 -070013from oftest import config
14import oftest.controller as controller
Rich Laneb90a1c42012-10-05 09:16:05 -070015import oftest.dataplane as dataplane
Rich Lane9fd05682013-01-10 15:30:38 -080016import ofp
Flavio Castrob01d0aa2016-07-20 16:14:48 -070017from ofdpa_utils import *
Rich Laneb90a1c42012-10-05 09:16:05 -070018
Rich Lane69fd8e02013-08-23 16:23:42 -070019class BaseTest(unittest.TestCase):
20 def __str__(self):
21 return self.id().replace('.runTest', '')
22
23 def setUp(self):
Flavio Castrob01d0aa2016-07-20 16:14:48 -070024 if config["force_ofdpa_restart"]:
25 logging.info("Restarting OFDPA")
26 forceOfdpaRestart( config["force_ofdpa_restart"]);
Rich Lane69fd8e02013-08-23 16:23:42 -070027 oftest.open_logfile(str(self))
28 logging.info("** START TEST CASE " + str(self))
29
30 def tearDown(self):
31 logging.info("** END TEST CASE " + str(self))
Flavio Castrob01d0aa2016-07-20 16:14:48 -070032 if config["force_ofdpa_restart"]:
33 forceOfdpaStop( config["force_ofdpa_restart"]);
34
Rich Lane69fd8e02013-08-23 16:23:42 -070035
36class SimpleProtocol(BaseTest):
Rich Laneb90a1c42012-10-05 09:16:05 -070037 """
38 Root class for setting up the controller
39 """
40
41 def setUp(self):
Rich Lane69fd8e02013-08-23 16:23:42 -070042 BaseTest.setUp(self)
Rich Laneb90a1c42012-10-05 09:16:05 -070043 self.controller = controller.Controller(
Dan Talayco69ca4d62012-11-15 11:50:22 -080044 switch=config["switch_ip"],
Rich Laneb90a1c42012-10-05 09:16:05 -070045 host=config["controller_host"],
46 port=config["controller_port"])
Rich Laneb90a1c42012-10-05 09:16:05 -070047 self.controller.start()
Rich Laneb90a1c42012-10-05 09:16:05 -070048
Rich Laned5915042012-12-31 14:58:35 -080049 try:
50 #@todo Add an option to wait for a pkt transaction to ensure version
51 # compatibilty?
52 self.controller.connect(timeout=20)
53
54 # By default, respond to echo requests
55 self.controller.keep_alive = True
Rich Laned5915042012-12-31 14:58:35 -080056 if not self.controller.active:
57 raise Exception("Controller startup failed")
58 if self.controller.switch_addr is None:
59 raise Exception("Controller startup failed (no switch addr)")
60 logging.info("Connected " + str(self.controller.switch_addr))
Rich Lane78ef8b92013-01-10 12:19:23 -080061 request = ofp.message.features_request()
Rich Laned5915042012-12-31 14:58:35 -080062 reply, pkt = self.controller.transact(request)
63 self.assertTrue(reply is not None,
64 "Did not complete features_request for handshake")
Rich Laneb73808c2013-03-11 15:22:23 -070065 if reply.version == 1:
Rich Laneaf428152013-01-10 12:24:44 -080066 self.supported_actions = reply.actions
67 logging.info("Supported actions: " + hex(self.supported_actions))
Rich Laned5915042012-12-31 14:58:35 -080068 except:
69 self.controller.kill()
70 del self.controller
71 raise
Rich Laneb90a1c42012-10-05 09:16:05 -070072
73 def inheritSetup(self, parent):
74 """
75 Inherit the setup of a parent
76
77 This allows running at test from within another test. Do the
78 following:
79
80 sub_test = SomeTestClass() # Create an instance of the test class
81 sub_test.inheritSetup(self) # Inherit setup of parent
82 sub_test.runTest() # Run the test
83
84 Normally, only the parent's setUp and tearDown are called and
85 the state after the sub_test is run must be taken into account
86 by subsequent operations.
87 """
88 logging.info("** Setup " + str(self) + " inheriting from "
89 + str(parent))
90 self.controller = parent.controller
91 self.supported_actions = parent.supported_actions
92
93 def tearDown(self):
Rich Laneb90a1c42012-10-05 09:16:05 -070094 self.controller.shutdown()
Rich Lane7c64a422012-12-31 13:46:34 -080095 self.controller.join()
Rich Lanee7b0ecb2012-12-26 10:01:01 -080096 del self.controller
Rich Lane69fd8e02013-08-23 16:23:42 -070097 BaseTest.tearDown(self)
Rich Laneb90a1c42012-10-05 09:16:05 -070098
Rich Laneb90a1c42012-10-05 09:16:05 -070099 def assertTrue(self, cond, msg):
100 if not cond:
101 logging.error("** FAILED ASSERTION: " + msg)
102 unittest.TestCase.assertTrue(self, cond, msg)
103
104class SimpleDataPlane(SimpleProtocol):
105 """
106 Root class that sets up the controller and dataplane
107 """
108 def setUp(self):
109 SimpleProtocol.setUp(self)
Rich Lane2c7812c2012-12-27 17:52:23 -0800110 self.dataplane = oftest.dataplane_instance
111 self.dataplane.flush()
Rich Lane472aaea2013-08-27 09:27:38 -0700112 if config["log_dir"] != None:
113 filename = os.path.join(config["log_dir"], str(self)) + ".pcap"
114 self.dataplane.start_pcap(filename)
Rich Laneb90a1c42012-10-05 09:16:05 -0700115
116 def inheritSetup(self, parent):
117 """
118 Inherit the setup of a parent
119
120 See SimpleProtocol.inheritSetup
121 """
122 SimpleProtocol.inheritSetup(self, parent)
123 self.dataplane = parent.dataplane
124
125 def tearDown(self):
Rich Lane472aaea2013-08-27 09:27:38 -0700126 if config["log_dir"] != None:
127 self.dataplane.stop_pcap()
Rich Laneb90a1c42012-10-05 09:16:05 -0700128 SimpleProtocol.tearDown(self)
Rich Laneb90a1c42012-10-05 09:16:05 -0700129
Rich Lane69fd8e02013-08-23 16:23:42 -0700130class DataPlaneOnly(BaseTest):
Rich Laneb90a1c42012-10-05 09:16:05 -0700131 """
132 Root class that sets up only the dataplane
133 """
134
135 def setUp(self):
Rich Lane69fd8e02013-08-23 16:23:42 -0700136 BaseTest.setUp(self)
Rich Lane2c7812c2012-12-27 17:52:23 -0800137 self.dataplane = oftest.dataplane_instance
138 self.dataplane.flush()
Rich Lane472aaea2013-08-27 09:27:38 -0700139 if config["log_dir"] != None:
140 filename = os.path.join(config["log_dir"], str(self)) + ".pcap"
141 self.dataplane.start_pcap(filename)
Rich Laneb90a1c42012-10-05 09:16:05 -0700142
143 def tearDown(self):
Rich Lane472aaea2013-08-27 09:27:38 -0700144 if config["log_dir"] != None:
145 self.dataplane.stop_pcap()
Rich Lane69fd8e02013-08-23 16:23:42 -0700146 BaseTest.tearDown(self)