Statistics
| Branch: | Tag: | Revision:

mininet / mininet / test / test_switchdpidassignment.py @ e0bf8ece

History | View | Annotate | Download (3.85 KB)

1 09b06509 moijes12
#!/usr/bin/env python
2
3
"""Package: mininet
4
   Regression tests for switch dpid assignment."""
5
6
import unittest
7 2fc5e46f moijes12
from functools import partial
8 09b06509 moijes12
import re
9
10
from mininet.net import Mininet
11 2fc5e46f moijes12
from mininet.node import Host, Controller
12
from mininet.node import UserSwitch, OVSSwitch, OVSLegacyKernelSwitch, IVSSwitch
13 09b06509 moijes12
from mininet.topo import Topo
14
from mininet.log import setLogLevel
15 2fc5e46f moijes12
from mininet.util import quietRun
16 09b06509 moijes12
17 2fc5e46f moijes12
18
class testSwitchDpidAssignmentCommon ( object ):
19 09b06509 moijes12
    """Verify Switch dpid assignment."""
20
21 2fc5e46f moijes12
    switchClass = None # overridden in subclasses
22
23 09b06509 moijes12
    def testDefaultDpid ( self ):
24
        """Verify that the default dpid is assigned using a valid provided
25
        canonical switchname if no dpid is passed in switch creation."""
26 2fc5e46f moijes12
        switch = Mininet( Topo(), self.switchClass, Host, Controller ).addSwitch( 's1' )
27 09b06509 moijes12
        self.assertEqual( switch.defaultDpid(), switch.dpid )
28
29
    def testActualDpidAssignment( self ):
30
        """Verify that Switch dpid is the actual dpid assigned if dpid is 
31
        passed in switch creation."""
32 2fc5e46f moijes12
        switch = Mininet( Topo(), self.switchClass, Host, Controller ).addSwitch( 'A', dpid = '000000000000ABCD' )
33 09b06509 moijes12
        self.assertEqual( switch.dpid, '000000000000ABCD' )
34
35
    def testDefaultDpidAssignmentFailure( self ):
36
        """Verify that Default dpid assignment raises an Exception if the 
37
        name of the switch does not contin a digit. Also verify the 
38
        exception message."""
39
        with self.assertRaises( Exception ) as raises_cm:
40 e0bf8ece Bob Lantz
            Mininet( Topo(), self.switchClass, Host, Controller ).addSwitch( 'A' )
41 09b06509 moijes12
        self.assertEqual(raises_cm.exception.message, 'Unable to derive '
42
                         'default datapath ID - please either specify a dpid '
43
                         'or use a canonical switch name such as s23.')
44
45
    def testDefaultDpidLen( self ):
46
        """Verify that Default dpid length is 16 characters consisting of
47
        16 - len(hex of first string of contiguous digits passed in switch
48
        name) 0's followed by hex of first string of contiguous digits passed
49
        in switch name."""
50 2fc5e46f moijes12
        switch = Mininet( Topo(), self.switchClass, Host, Controller ).addSwitch( 's123' )
51
        dpid = hex( int(re.findall( r'\d+', switch.name ) [0]) ) [ 2: ]
52
        try:
53
            if issubclass(UserSwitch, self.switchClass):
54
                # Dpid lenght of UserSwitch = 12
55
                self.assertEqual( switch.dpid, '0' * (12 - len(dpid)) + str(dpid) )
56
            else:
57
                self.assertEqual( switch.dpid, '0' * (16 - len(dpid)) + str(dpid) )
58
        except TypeError:
59
            # Switch is OVS User Switch
60
            self.assertEqual( switch.dpid, '0' * (16 - len(dpid)) + str(dpid) )
61
            
62
63
class testSwitchOVSKernel( testSwitchDpidAssignmentCommon, unittest.TestCase ):
64
    """Test dpid assignnment of OVS Kernel Switch."""
65
    switchClass = OVSSwitch
66
67
class testSwitchOVSUser( testSwitchDpidAssignmentCommon, unittest.TestCase ):
68
    """Test dpid assignnment of OVS User Switch."""
69
    switchClass = partial(OVSSwitch, datapath = 'user')
70
    
71
@unittest.skipUnless( quietRun( 'which ovs-openflowd' ), 'OVS Legacy Kernel switch is not installed' )
72
class testSwitchOVSLegacyKernel( testSwitchDpidAssignmentCommon, unittest.TestCase ):
73
    """Test dpid assignnment of OVS Legacy Kernel Switch."""
74
    switchClass = OVSLegacyKernelSwitch
75
    
76
@unittest.skipUnless( quietRun( 'which ivs-ctl' ), 'IVS switch is not installed' )
77
class testSwitchIVS( testSwitchDpidAssignmentCommon, unittest.TestCase ):
78
    """Test dpid assignment of IVS switch."""
79
    switchClass = IVSSwitch
80
81
@unittest.skipUnless( quietRun( 'which ofprotocol' ), 'Reference user switch is not installed' )
82
class testSwitchUserspace( testSwitchDpidAssignmentCommon, unittest.TestCase ):
83
    """Test dpid assignment of Userspace switch."""
84
    switchClass = UserSwitch
85 09b06509 moijes12
86
87
if __name__ == '__main__':
88
    setLogLevel( 'warning' )
89
    unittest.main()