diff --git a/mininet/test/test_switchdpidassignment.py b/mininet/test/test_switchdpidassignment.py old mode 100644 new mode 100755 index 61833e94cb2d683acfe889acac87a1e2d88b1d2a..367866729c81cb3a8aa6fe971887080d41f9ed34 --- a/mininet/test/test_switchdpidassignment.py +++ b/mininet/test/test_switchdpidassignment.py @@ -5,8 +5,6 @@ import unittest import sys -from functools import partial -import re from mininet.net import Mininet from mininet.node import Host, Controller @@ -16,13 +14,16 @@ from mininet.util import quietRun from mininet.clean import cleanup -class testSwitchDpidAssignmentCommon ( object ): - """Verify Switch dpid assignment.""" - switchClass = None # overridden in subclasses +class TestSwitchDpidAssignmentOVS( unittest.TestCase ): + "Verify Switch dpid assignment." + + switchClass = OVSSwitch # overridden in subclasses def tearDown( self ): "Clean up if necessary" + # satisfy pylint + assert self if sys.exc_info != ( None, None, None ): cleanup() @@ -33,12 +34,19 @@ def testDefaultDpid ( self ): self.switchClass, Host, Controller ).addSwitch( 's1' ) self.assertEqual( switch.defaultDpid(), switch.dpid ) + def dpidFrom( self, num ): + "Compute default dpid from number" + fmt = ( '%0' + str( self.switchClass.dpidLen ) + 'x' ) + return fmt % num + def testActualDpidAssignment( self ): """Verify that Switch dpid is the actual dpid assigned if dpid is passed in switch creation.""" + dpid = self.dpidFrom( 0xABCD ) switch = Mininet( Topo(), self.switchClass, - Host, Controller ).addSwitch( 'A', dpid = '000000000000ABCD' ) - self.assertEqual( switch.dpid, '000000000000ABCD' ) + Host, Controller ).addSwitch( + 's1', dpid=dpid ) + self.assertEqual( switch.dpid, dpid ) def testDefaultDpidAssignmentFailure( self ): """Verify that Default dpid assignment raises an Exception if the @@ -58,49 +66,37 @@ def testDefaultDpidLen( self ): in switch name.""" switch = Mininet( Topo(), self.switchClass, Host, Controller ).addSwitch( 's123' ) - dpid = hex( int(re.findall( r'\d+', switch.name ) [0]) ) [ 2: ] - try: - if issubclass(UserSwitch, self.switchClass): - # Dpid lenght of UserSwitch = 12 - self.assertEqual( switch.dpid, - '0' * (12 - len(dpid)) + str(dpid) ) - else: - self.assertEqual( switch.dpid, - '0' * (16 - len(dpid)) + str(dpid) ) - except TypeError: - # Switch is OVS User Switch - self.assertEqual( switch.dpid, - '0' * (16 - len(dpid)) + str(dpid) ) - - -class testSwitchOVSKernel( testSwitchDpidAssignmentCommon, unittest.TestCase ): - """Test dpid assignnment of OVS Kernel Switch.""" - switchClass = OVSSwitch - -class testSwitchOVSUser( testSwitchDpidAssignmentCommon, unittest.TestCase ): - """Test dpid assignnment of OVS User Switch.""" - switchClass = partial(OVSSwitch, datapath = 'user') + + self.assertEqual( switch.dpid, self.dpidFrom( 123 ) ) + +class OVSUser( OVSSwitch): + "OVS User Switch convenience class" + def __init__( self, *args, **kwargs ): + kwargs.update( datapath='user' ) + OVSSwitch.__init__( self, *args, **kwargs ) + +class testSwitchOVSUser( TestSwitchDpidAssignmentOVS ): + "Test dpid assignnment of OVS User Switch." + switchClass = OVSUser @unittest.skipUnless( quietRun( 'which ovs-openflowd' ), 'OVS Legacy Kernel switch is not installed' ) -class testSwitchOVSLegacyKernel( testSwitchDpidAssignmentCommon, - unittest.TestCase ): - """Test dpid assignnment of OVS Legacy Kernel Switch.""" +class testSwitchOVSLegacyKernel( TestSwitchDpidAssignmentOVS ): + "Test dpid assignnment of OVS Legacy Kernel Switch." switchClass = OVSLegacyKernelSwitch -@unittest.skipUnless( quietRun( 'which ivs-ctl' ), 'IVS switch is not installed' ) -class testSwitchIVS( testSwitchDpidAssignmentCommon, - unittest.TestCase ): - """Test dpid assignment of IVS switch.""" +@unittest.skipUnless( quietRun( 'which ivs-ctl' ), + 'IVS switch is not installed' ) +class testSwitchIVS( TestSwitchDpidAssignmentOVS ): + "Test dpid assignment of IVS switch." switchClass = IVSSwitch -@unittest.skipUnless( quietRun( 'which ofprotocol' ), 'Reference user switch is not installed' ) -class testSwitchUserspace( testSwitchDpidAssignmentCommon, - unittest.TestCase ): - """Test dpid assignment of Userspace switch.""" +@unittest.skipUnless( quietRun( 'which ofprotocol' ), + 'Reference user switch is not installed' ) +class testSwitchUserspace( TestSwitchDpidAssignmentOVS ): + "Test dpid assignment of Userspace switch." switchClass = UserSwitch - if __name__ == '__main__': setLogLevel( 'warning' ) unittest.main()