4 Regression tests for switch dpid assignment."""
9 from mininet.net import Mininet
10 from mininet.node import Host, Controller
11 from mininet.node import ( UserSwitch, OVSSwitch, IVSSwitch )
12 from mininet.topo import Topo
13 from mininet.log import setLogLevel
14 from mininet.util import quietRun
15 from mininet.clean import cleanup
18 class TestSwitchDpidAssignmentOVS( unittest.TestCase ):
19 "Verify Switch dpid assignment."
21 switchClass = OVSSwitch # overridden in subclasses
24 "Clean up if necessary"
27 if sys.exc_info != ( None, None, None ):
30 def testDefaultDpid( self ):
31 """Verify that the default dpid is assigned using a valid provided
32 canonical switchname if no dpid is passed in switch creation."""
33 net = Mininet( Topo(), self.switchClass, Host, Controller )
34 switch = net.addSwitch( 's1' )
35 self.assertEqual( switch.defaultDpid(), switch.dpid )
38 def dpidFrom( self, num ):
39 "Compute default dpid from number"
40 fmt = ( '%0' + str( self.switchClass.dpidLen ) + 'x' )
43 def testActualDpidAssignment( self ):
44 """Verify that Switch dpid is the actual dpid assigned if dpid is
45 passed in switch creation."""
46 dpid = self.dpidFrom( 0xABCD )
47 net = Mininet( Topo(), self.switchClass, Host, Controller )
48 switch = net.addSwitch( 's1', dpid=dpid )
49 self.assertEqual( switch.dpid, dpid )
52 def testDefaultDpidAssignmentFailure( self ):
53 """Verify that Default dpid assignment raises an Exception if the
54 name of the switch does not contin a digit. Also verify the
56 net = Mininet( Topo(), self.switchClass, Host, Controller )
57 with self.assertRaises( Exception ) as raises_cm:
59 self.assertTrue( 'Unable to derive '
60 'default datapath ID - please either specify a dpid '
61 'or use a canonical switch name such as s23.'
62 in str( raises_cm.exception ) )
65 def testDefaultDpidLen( self ):
66 """Verify that Default dpid length is 16 characters consisting of
67 16 - len(hex of first string of contiguous digits passed in switch
68 name) 0's followed by hex of first string of contiguous digits passed
70 net = Mininet( Topo(), self.switchClass, Host, Controller )
71 switch = net.addSwitch( 's123' )
72 self.assertEqual( switch.dpid, self.dpidFrom( 123 ) )
76 class OVSUser( OVSSwitch):
77 "OVS User Switch convenience class"
78 def __init__( self, *args, **kwargs ):
79 kwargs.update( datapath='user' )
80 OVSSwitch.__init__( self, *args, **kwargs )
82 class testSwitchOVSUser( TestSwitchDpidAssignmentOVS ):
83 "Test dpid assignnment of OVS User Switch."
86 @unittest.skipUnless( quietRun( 'which ivs-ctl' ),
87 'IVS switch is not installed' )
88 class testSwitchIVS( TestSwitchDpidAssignmentOVS ):
89 "Test dpid assignment of IVS switch."
90 switchClass = IVSSwitch
92 @unittest.skipUnless( quietRun( 'which ofprotocol' ),
93 'Reference user switch is not installed' )
94 class testSwitchUserspace( TestSwitchDpidAssignmentOVS ):
95 "Test dpid assignment of Userspace switch."
96 switchClass = UserSwitch
98 if __name__ == '__main__':
99 setLogLevel( 'warning' )