Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1 | """ |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame^] | 2 | Copyright 2015 Open Networking Foundation ( ONF ) |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 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 |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame^] | 11 | ( at your option ) any later version. |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 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 | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 34 | time.sleep( gossipTime * main.Cluster.numCtrls ) |
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, |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame^] | 38 | kwargs={ 'main': main, 'responses': responses }, |
| 39 | sleep=main.retrysleep, |
| 40 | attempts=main.retrytimes ) |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 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 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame^] | 47 | def checkNodeResponses( main, responses ): |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 48 | numberOfFailedNodes = 0 # Tracks the number of nodes that failed to get net configuration |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 49 | for ctrl in main.Cluster.active(): |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame^] | 50 | response = ctrl.REST.getNetCfg() |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 51 | responses.append( ctrl.REST.pprint( response ) ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 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 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame^] | 68 | |
Jeremy Ronquillo | 4cf537d | 2017-06-26 11:20:52 -0700 | [diff] [blame] | 69 | def checkDeviceAnnotations( main, jsonObj, sw ): |
| 70 | id = str( sw.get( 'id' ) ) |
| 71 | keys = [ 'name', 'owner', 'rackAddress' ] |
| 72 | correct = True |
| 73 | for k in keys: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame^] | 74 | if str( sw.get( 'annotations', {} ).get( k ) ) != str( jsonObj[ k ] ): |
Jeremy Ronquillo | 4cf537d | 2017-06-26 11:20:52 -0700 | [diff] [blame] | 75 | correct = False |
| 76 | main.log.debug( "{} is wrong on switch: ".format( k ) + id ) |
| 77 | if not correct: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame^] | 78 | main.log.error( "Annotations for switch " + id + " are incorrect: {}".format( sw ) ) |
Jeremy Ronquillo | 4cf537d | 2017-06-26 11:20:52 -0700 | [diff] [blame] | 79 | return correct |
| 80 | |
| 81 | |
| 82 | def checkAllDeviceAnnotations( main, json ): |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame^] | 83 | devices = main.Cluster.active( 0 ).REST.devices() |
Jeremy Ronquillo | 4cf537d | 2017-06-26 11:20:52 -0700 | [diff] [blame] | 84 | id = "of:0000000000000001" |
| 85 | i = 1 |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame^] | 86 | result = [] |
Jeremy Ronquillo | 4cf537d | 2017-06-26 11:20:52 -0700 | [diff] [blame] | 87 | try: |
| 88 | for sw in json.loads( devices ): |
| 89 | if id in sw.get( 'id' ): |
| 90 | jsonObj = getattr( main, "s" + str( i ) + "Json" ) |
| 91 | isDeviceAnnotationCorrect = checkDeviceAnnotations( main, jsonObj, sw ) |
| 92 | result.append( isDeviceAnnotationCorrect ) |
| 93 | i += 1 |
| 94 | id = "of:000000000000000" + str( i ) |
| 95 | except( TypeError, ValueError ): |
| 96 | main.log.error( "Problem loading device" ) |
| 97 | return False |
| 98 | return all( result ) |