admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 2 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3 | Created on 24-Oct-2012 |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 4 | Modified 2015 by ON.Lab |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 5 | |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 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> |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 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 |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 13 | ( at your option ) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 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 |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 22 | """ |
Jon Hall | fd8ce43 | 2015-07-21 13:07:53 -0700 | [diff] [blame] | 23 | import logging |
| 24 | |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 25 | |
| 26 | class Component( object ): |
| 27 | |
| 28 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 29 | This is the tempalte class for components |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 30 | """ |
| 31 | def __init__( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 32 | self.default = '' |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 33 | self.wrapped = sys.modules[ __name__ ] |
Jon Hall | eb3a684 | 2015-02-18 15:17:51 -0800 | [diff] [blame] | 34 | self.count = 0 |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 35 | self.prompt = "\$" |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 36 | |
| 37 | def __getattr__( self, name ): |
| 38 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 39 | This will invoke, if the attribute wasn't found the usual ways. |
Jon Hall | 20ead01 | 2015-02-18 14:00:42 -0800 | [diff] [blame] | 40 | Here it will look for assert_attribute and will execute when |
| 41 | AttributeError occurs. |
| 42 | It will return the result of the assert_attribute. |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 43 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 44 | try: |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 45 | return getattr( self.wrapped, name ) |
Jon Hall | 20ead01 | 2015-02-18 14:00:42 -0800 | [diff] [blame] | 46 | except AttributeError as error: |
Jon Hall | eb3a684 | 2015-02-18 15:17:51 -0800 | [diff] [blame] | 47 | # NOTE: The first time we load a driver module we get this error |
Jon Hall | 0fc0d45 | 2015-07-14 09:49:58 -0700 | [diff] [blame] | 48 | if "'module' object has no attribute '__path__'" in error: |
| 49 | pass |
Jon Hall | eb3a684 | 2015-02-18 15:17:51 -0800 | [diff] [blame] | 50 | else: |
| 51 | main.log.error( str(error.__class__) + " " + str(error) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 52 | try: |
Jon Hall | 20ead01 | 2015-02-18 14:00:42 -0800 | [diff] [blame] | 53 | def experimentHandling( *args, **kwargs ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 54 | if main.EXPERIMENTAL_MODE == main.TRUE: |
Jon Hall | 20ead01 | 2015-02-18 14:00:42 -0800 | [diff] [blame] | 55 | result = self.experimentRun( *args, **kwargs ) |
| 56 | main.log.info( "EXPERIMENTAL MODE. API " + |
| 57 | str( name ) + |
| 58 | " not yet implemented. " + |
| 59 | "Returning dummy values" ) |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 60 | return result |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 61 | else: |
| 62 | return main.FALSE |
| 63 | return experimentHandling |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 64 | except TypeError as e: |
Jon Hall | 20ead01 | 2015-02-18 14:00:42 -0800 | [diff] [blame] | 65 | main.log.error( "Arguments for experimental mode does not" + |
| 66 | " have key 'retruns'" + e ) |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 67 | |
| 68 | def connect( self ): |
| 69 | |
| 70 | vars( main )[ self.name + 'log' ] = logging.getLogger( self.name ) |
| 71 | |
| 72 | session_file = main.logdir + "/" + self.name + ".session" |
| 73 | self.log_handler = logging.FileHandler( session_file ) |
| 74 | self.log_handler.setLevel( logging.DEBUG ) |
| 75 | |
| 76 | vars( main )[ self.name + 'log' ].setLevel( logging.DEBUG ) |
| 77 | _formatter = logging.Formatter( |
| 78 | "%(asctime)s %(name)-10s: %(levelname)-8s: %(message)s" ) |
| 79 | self.log_handler.setFormatter( _formatter ) |
| 80 | vars( main )[ self.name + 'log' ].addHandler( self.log_handler ) |
| 81 | # Adding header for the component log |
| 82 | vars( main )[ self.name + 'log' ].info( main.logHeader ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 83 | # Opening the session log to append command's execution output |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 84 | self.logfile_handler = open( session_file, "a" ) |
| 85 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 86 | return "Dummy" |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 87 | |
| 88 | def execute( self, cmd ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 89 | return main.TRUE |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 90 | # import commands |
| 91 | # return commands.getoutput( cmd ) |
| 92 | |
| 93 | def disconnect( self ): |
| 94 | return main.TRUE |
| 95 | |
| 96 | def config( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 97 | self = self |
| 98 | # Need to update the configuration code |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 99 | |
| 100 | def cleanup( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 101 | return main.TRUE |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 102 | |
| 103 | def log( self, message ): |
| 104 | """ |
| 105 | Here finding the for the component to which the |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 106 | log message based on the called child object. |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 107 | """ |
| 108 | vars( main )[ self.name + 'log' ].info( "\n" + message + "\n" ) |
| 109 | |
| 110 | def close_log_handles( self ): |
| 111 | vars( main )[ self.name + 'log' ].removeHandler( self.log_handler ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 112 | if self.logfile_handler: |
| 113 | self.logfile_handler.close() |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 114 | |
| 115 | def get_version( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 116 | return "Version unknown" |
| 117 | |
Jon Hall | 20ead01 | 2015-02-18 14:00:42 -0800 | [diff] [blame] | 118 | def experimentRun( self, *args, **kwargs ): |
| 119 | # FIXME handle *args |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 120 | args = utilities.parse_args( [ "RETURNS" ], **kwargs ) |
| 121 | return args[ "RETURNS" ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 122 | |
| 123 | |
| 124 | if __name__ != "__main__": |
| 125 | import sys |
kelvin-onlab | 2f4ef84 | 2015-01-19 12:49:33 -0800 | [diff] [blame] | 126 | sys.modules[ __name__ ] = Component() |
| 127 | |