| 1 |  |  | """Module to help to create tests.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from unittest.mock import MagicMock, Mock, create_autospec | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | from kytos.core import Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from kytos.core.config import KytosConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from kytos.core.connection import Connection, ConnectionState | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from kytos.core.interface import Interface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | from kytos.core.switch import Switch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | def get_controller_mock(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     """Return a controller mock.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     options = KytosConfig().options['daemon'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     controller = Controller(options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     controller.log = Mock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     return controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | def get_interface_mock(interface_name, port, *args, **kwargs): | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     """Return a interface mock.""" | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     switch = get_switch_mock(0x04) | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     switch.connection = Mock() | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     iface = Interface(interface_name, port, switch, *args, **kwargs) | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     return iface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | def get_switch_mock(dpid): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     """Return a switch mock.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     return Switch(dpid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | def get_connection_mock(of_version, target_switch): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     connection = Connection(Mock(), Mock(), Mock()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     connection.switch = target_switch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     connection.protocol.version = of_version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     return connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | def get_kytos_event_mock(**kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     destination = kwargs.get('destination') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     message = kwargs.get('message') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     source = kwargs.get('source') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     event = MagicMock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     event.source = source | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     event.content = {'destination': destination, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                      'message': message} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 48 |  |  |     return event | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |  |