You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | 2015-2016 |
| 4 | Copyright 2016 Open Networking Foundation (ONF) |
| 5 | |
| 6 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 7 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 8 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 9 | |
| 10 | TestON is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation, either version 2 of the License, or |
| 13 | ( at your option ) any later version. |
| 14 | |
| 15 | TestON is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | """ |
| 24 | import pexpect |
| 25 | import re |
| 26 | import sys |
| 27 | import types |
| 28 | import os |
| 29 | from drivers.common.cli.emulator.scapyclidriver import ScapyCliDriver |
| 30 | |
| 31 | |
| 32 | class MininetHostScapyCliDriver( ScapyCliDriver ): |
| 33 | |
| 34 | """ |
| 35 | This class is created as a standalone Mininet host scapy driver. It |
| 36 | could be used as driver for a mock physical host for proof-of-concept |
| 37 | testing in physical environment. |
| 38 | """ |
| 39 | def __init__( self ): |
| 40 | super( MininetHostScapyCliDriver, self ).__init__() |
| 41 | self.handle = self |
| 42 | self.name = None |
| 43 | self.home = None |
| 44 | self.wrapped = sys.modules[ __name__ ] |
| 45 | self.flag = 0 |
| 46 | # TODO: Refactor driver to use these everywhere |
| 47 | self.hostPrompt = "~#" |
| 48 | self.scapyPrompt = ">>>" |
| 49 | |
| 50 | def connect( self, **connectargs ): |
| 51 | """ |
| 52 | Here the main is the TestON instance after creating |
| 53 | all the log handles.""" |
| 54 | try: |
| 55 | for key in connectargs: |
| 56 | vars( self )[ key ] = connectargs[ key ] |
| 57 | self.home = self.options[ 'home' ] if 'home' in self.options.keys() else "~/" |
| 58 | self.name = self.options[ 'name' ] |
| 59 | self.ifaceName = self.options[ 'ifaceName' ] if 'ifaceName' in self.options.keys() else self.name + "-eth0" |
| 60 | |
| 61 | try: |
| 62 | if os.getenv( str( self.ip_address ) ) is not None: |
| 63 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 64 | else: |
| 65 | main.log.info( self.name + |
| 66 | ": Trying to connect to " + |
| 67 | self.ip_address ) |
| 68 | |
| 69 | except KeyError: |
| 70 | main.log.info( "Invalid host name," + |
| 71 | " connecting to local host instead" ) |
| 72 | self.ip_address = 'localhost' |
| 73 | except Exception as inst: |
| 74 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 75 | |
| 76 | self.handle = super( |
| 77 | ScapyCliDriver, |
| 78 | self ).connect( |
| 79 | user_name=self.user_name, |
| 80 | ip_address=self.ip_address, |
| 81 | port=None, |
| 82 | pwd=self.pwd ) |
| 83 | |
| 84 | if self.handle: |
| 85 | main.log.info( "Connection successful to the host " + |
| 86 | self.user_name + |
| 87 | "@" + |
| 88 | self.ip_address ) |
| 89 | self.handle.sendline( "~/mininet/util/m " + self.name ) |
| 90 | self.handle.sendline( "cd" ) |
| 91 | self.handle.expect( self.hostPrompt ) |
| 92 | self.handle.sendline( "" ) |
| 93 | self.handle.expect( self.hostPrompt ) |
| 94 | return main.TRUE |
| 95 | else: |
| 96 | main.log.error( "Connection failed to the host " + |
| 97 | self.user_name + |
| 98 | "@" + |
| 99 | self.ip_address ) |
| 100 | main.log.error( "Failed to connect to the Mininet Host" ) |
| 101 | return main.FALSE |
| 102 | except pexpect.EOF: |
| 103 | main.log.error( self.name + ": EOF exception found" ) |
| 104 | main.log.error( self.name + ": " + self.handle.before ) |
| 105 | main.cleanAndExit() |
| 106 | except Exception: |
| 107 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 108 | main.cleanAndExit() |
| 109 | |
| 110 | def disconnect( self ): |
| 111 | """ |
| 112 | Called when test is complete to disconnect the handle. |
| 113 | """ |
| 114 | try: |
| 115 | self.handle.sendline( '' ) |
| 116 | i = self.handle.expect( [ self.hostPrompt, pexpect.EOF, pexpect.TIMEOUT ], |
| 117 | timeout=2 ) |
| 118 | if i == 0: |
| 119 | return main.TRUE |
| 120 | elif i == 1: |
| 121 | return main.TRUE |
| 122 | else: |
| 123 | main.log.error( "Connection failed to the host" ) |
| 124 | return main.ERROR |
| 125 | except pexpect.EOF: |
| 126 | main.log.error( self.name + ": EOF exception found" ) |
| 127 | main.log.error( self.name + ": " + self.handle.before ) |
| 128 | main.cleanAndExit() |
| 129 | except Exception: |
| 130 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 131 | main.cleanAndExit() |
| 132 | |
| 133 | if __name__ != "__main__": |
| 134 | sys.modules[ __name__ ] = MininetHostScapyCliDriver() |