Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1 | """ |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame^] | 2 | Copyright 2015 Open Networking Foundation (ONF) |
| 3 | |
| 4 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 5 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 6 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 7 | |
| 8 | TestON is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | TestON is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with TestON. If not, see <http://www.gnu.org/licenses/> |
| 20 | |
| 21 | |
| 22 | These functions are for use with the Network config system |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 23 | """ |
| 24 | import time |
| 25 | |
Jon Hall | bc080f9 | 2017-05-24 16:29:55 -0700 | [diff] [blame] | 26 | |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 27 | def compareCfg( main, gossipTime=None ): |
| 28 | """ |
| 29 | Compare the network configurations across all nodes in the network |
| 30 | gossipTime is the number of seconds each gossip round take for the netCfg maps |
| 31 | """ |
| 32 | main.step( "Check net config" ) |
| 33 | if gossipTime: |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 34 | time.sleep( gossipTime * len( main.RESTs ) ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 35 | responses = [] |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 36 | result = utilities.retry( f=checkNodeResponses, |
| 37 | retValue=False, |
| 38 | kwargs={'main' : main,'responses' : responses}, |
| 39 | sleep = main.retrysleep, |
| 40 | attempts = main.retrytimes ) |
| 41 | utilities.assert_equals( expect=True, |
| 42 | actual=result, |
| 43 | onpass="Net Cfg is the same on all nodes", |
| 44 | onfail="Check Net Cfg failed. Check above messages." ) |
| 45 | |
| 46 | |
| 47 | def checkNodeResponses ( main, responses ): |
| 48 | numberOfFailedNodes = 0 # Tracks the number of nodes that failed to get net configuration |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 49 | for node in main.RESTs: |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 50 | response = node.getNetCfg( ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 51 | responses.append( node.pprint( response ) ) |
| 52 | if response == main.FALSE: |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 53 | numberOfFailedNodes += 1 |
| 54 | |
Jon Hall | bc080f9 | 2017-05-24 16:29:55 -0700 | [diff] [blame] | 55 | compare = [ i == responses[ 0 ] for i in responses ] |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 56 | if numberOfFailedNodes == 0 and all( compare ): |
| 57 | return True |
| 58 | |
| 59 | # Failed, providing feedback on cli |
| 60 | if numberOfFailedNodes > 0: |
| 61 | main.log.warn( numberOfFailedNodes + " node(s) failed to GET Net Config" ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 62 | if not all( compare ): |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 63 | main.log.debug( "Net Cfg is different on some nodes. Net Config results:" ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 64 | for i in responses: |
| 65 | main.log.debug( i ) |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 66 | return False |
Jeremy Ronquillo | 4cf537d | 2017-06-26 11:20:52 -0700 | [diff] [blame] | 67 | |
| 68 | def checkDeviceAnnotations( main, jsonObj, sw ): |
| 69 | id = str( sw.get( 'id' ) ) |
| 70 | keys = [ 'name', 'owner', 'rackAddress' ] |
| 71 | correct = True |
| 72 | for k in keys: |
| 73 | if str( sw.get( 'annotations', {} ).get( k ) ) != str( jsonObj[ k ] ) : |
| 74 | correct = False |
| 75 | main.log.debug( "{} is wrong on switch: ".format( k ) + id ) |
| 76 | if not correct: |
| 77 | main.log.error( "Annotations for switch " + id + " are incorrect: {}".format( sw ) ) |
| 78 | return correct |
| 79 | |
| 80 | |
| 81 | def checkAllDeviceAnnotations( main, json ): |
| 82 | devices = main.ONOSrest1.devices( ) |
| 83 | id = "of:0000000000000001" |
| 84 | i = 1 |
| 85 | result = [ ] |
| 86 | try: |
| 87 | for sw in json.loads( devices ): |
| 88 | if id in sw.get( 'id' ): |
| 89 | jsonObj = getattr( main, "s" + str( i ) + "Json" ) |
| 90 | isDeviceAnnotationCorrect = checkDeviceAnnotations( main, jsonObj, sw ) |
| 91 | result.append( isDeviceAnnotationCorrect ) |
| 92 | i += 1 |
| 93 | id = "of:000000000000000" + str( i ) |
| 94 | except( TypeError, ValueError ): |
| 95 | main.log.error( "Problem loading device" ) |
| 96 | return False |
| 97 | return all( result ) |