blob: 265ba13f93b1229632459ba18e3b2c272ce81783 [file] [log] [blame]
Jon Hall6e709752016-02-01 13:38:46 -08001def __init__( self ):
2 self.default = ''
3
4def consistentCheck():
5 """
6 Checks that TestON counters are consistent across all nodes.
7
8 Returns the tuple (onosCounters, consistent)
9 - onosCounters is the parsed json output of the counters command on all nodes
10 - consistent is main.TRUE if all "TestON" counters are consitent across all
11 nodes or main.FALSE
12 """
13 import json
14 try:
15 correctResults = main.TRUE
16 # Get onos counters results
17 onosCountersRaw = []
18 threads = []
19 for i in main.activeNodes:
20 t = main.Thread( target=utilities.retry,
21 name="counters-" + str( i ),
22 args=[ main.CLIs[i].counters, [ None ] ],
23 kwargs= { 'sleep': 5, 'attempts': 5,
24 'randomTime': True } )
25 threads.append( t )
26 t.start()
27 for t in threads:
28 t.join()
29 onosCountersRaw.append( t.result )
30 onosCounters = []
31 for i in range( len( main.activeNodes ) ):
32 try:
33 onosCounters.append( json.loads( onosCountersRaw[i] ) )
34 except ( ValueError, TypeError ):
35 main.log.error( "Could not parse counters response from ONOS" +
36 str( main.activeNodes[i] + 1 ) )
37 main.log.warn( repr( onosCountersRaw[ i ] ) )
38 onosCounters.append( [] )
39 #return ( onosCounters, main.FALSE )
40
41 testCounters = {}
42 # make a list of all the "TestON-*" counters in ONOS
43 # lookes like a dict whose keys are the name of the ONOS node and values
44 # are a list of the counters. I.E.
45 # { "ONOS1": [ {"name":"TestON-inMemory","value":56},
46 # {"name":"TestON-Partitions","value":56} ]
47 # }
48 # NOTE: There is an assumtion that all nodes are active
49 # based on the above for loops
50 for controller in enumerate( onosCounters ):
51 for dbType in controller[1]:
52 for dbName, items in dbType.iteritems():
53 for item in items:
54 if 'TestON' in item['name']:
55 node = 'ONOS' + str( main.activeNodes[ controller[0] ] + 1 )
56 try:
57 testCounters[node].append( item )
58 except KeyError:
59 testCounters[node] = [ item ]
60 # compare the counters on each node
61 firstV = testCounters.values()[0]
62 tmp = [ v == firstV for k, v in testCounters.iteritems() ]
63 if all( tmp ):
64 consistent = main.TRUE
65 else:
66 consistent = main.FALSE
67 main.log.error( "ONOS nodes have different values for counters:\n" +
68 testCounters )
69 return ( onosCounters, consistent )
70 except Exception:
71 main.log.exception( "" )
72 main.cleanup()
73 main.exit()
74
75def counterCheck( counterName, counterValue ):
76 """
77 Checks that TestON counters are consistent across all nodes and that
78 specified counter is in ONOS with the given value
79 """
80 import json
81 try:
82 correctResults = main.TRUE
83 # Get onos counters results and consistentCheck
84 onosCounters, consistent = main.Counters.consistentCheck()
85 # Check for correct values
86 for i in range( len( main.activeNodes ) ):
87 current = onosCounters[i]
88 onosValue = None
89 try:
90 for database in current:
91 database = database.values()[0]
92 for counter in database:
93 if counter.get( 'name' ) == counterName:
94 onosValue = counter.get( 'value' )
95 break
96 except AttributeError, e:
97 node = str( main.activeNodes[i] + 1 )
98 main.log.error( "ONOS" + node + " counters result " +
99 "is not as expected" )
100 correctResults = main.FALSE
101 if onosValue == counterValue:
102 main.log.info( counterName + " counter value is correct" )
103 else:
104 main.log.error( counterName + " counter value is incorrect," +
105 " expected value: " + str( counterValue )
106 + " current value: " + str( onosValue ) )
107 correctResults = main.FALSE
108 return consistent and correctResults
109 except Exception:
110 main.log.exception( "" )
111 main.cleanup()
112 main.exit()