second try
[vsorcdistro/.git] / mininet / mininet / test / test_switchdpidassignment.py
1 #!/usr/bin/env python
2
3 """Package: mininet
4    Regression tests for switch dpid assignment."""
5
6 import unittest
7 import sys
8
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
16
17
18 class TestSwitchDpidAssignmentOVS( unittest.TestCase ):
19     "Verify Switch dpid assignment."
20
21     switchClass = OVSSwitch  # overridden in subclasses
22
23     def tearDown( self ):
24         "Clean up if necessary"
25         # satisfy pylint
26         assert self
27         if sys.exc_info != ( None, None, None ):
28             cleanup()
29
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 )
36         net.stop()
37
38     def dpidFrom( self, num ):
39         "Compute default dpid from number"
40         fmt = ( '%0' + str( self.switchClass.dpidLen ) + 'x' )
41         return fmt % num
42
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 )
50         net.stop()
51
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
55         exception message."""
56         net = Mininet( Topo(), self.switchClass, Host, Controller )
57         with self.assertRaises( Exception ) as raises_cm:
58             net.addSwitch( 'A' )
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 ) )
63         net.stop()
64
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
69         in switch name."""
70         net = Mininet( Topo(), self.switchClass, Host, Controller )
71         switch = net.addSwitch( 's123' )
72         self.assertEqual( switch.dpid, self.dpidFrom( 123 ) )
73         net.stop()
74
75
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 )
81
82 class testSwitchOVSUser( TestSwitchDpidAssignmentOVS ):
83     "Test dpid assignnment of OVS User Switch."
84     switchClass = OVSUser
85
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
91
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
97
98 if __name__ == '__main__':
99     setLogLevel( 'warning' )
100     unittest.main()
101     cleanup()