Fixed acquiring meter statistics from device
[openflowplugin.git] / test-scripts / openvswitch / compare_tools.py
1 '''
2 Created on Jan 24, 2014
3
4 @author: vdemcak
5 '''
6
7 import logging
8
9
10 class Comparator():
11
12     log = logging.getLogger('Comparator')
13
14     @staticmethod
15     def compare_results(actual, expected):
16         #print 'ACT: ', actual
17         #print 'EXP: ', expected
18
19         list_unused = list(set(actual.keys()) - set(expected.keys()))
20         if len(list_unused) > 0:
21             Comparator.log.info('unchecked tags: {}'.format(list_unused))
22
23         list_duration = ['duration','hard_timeout','idle_timeout']
24
25         Comparator.test_duration(actual, expected)
26
27         # compare results from actual flow (mn dump result) and expepected flow (stored result)
28         for k in expected.keys():
29             if k not in list_duration:
30                 assert k in actual, 'cannot find key {} in flow {}'.format(k, actual)
31                 assert actual[k] == expected[k], 'key:{}, actual:{} != expected:{}'.format(k, actual[k], expected[k])
32
33     @staticmethod
34     def test_duration(actual, expected):
35         duration_key = 'duration'
36         hard_to_key = 'hard_timeout'
37
38         if duration_key in expected.keys():
39             assert duration_key in actual.keys(), '{} is not set in {}'.format(duration_key, actual)
40             try:
41                 duration = float(expected['duration'].rstrip('s'))
42                 hard_timeout = int(actual['hard_timeout'])
43                 assert duration <= hard_timeout, 'duration is higher than hard_timeout, {} > {}'.format(duration, hard_timeout)
44             except KeyError as e:
45                 Comparator.log.warning('cannot find keys to test duration tag', exc_info=True)
46         else:
47             # VD - what should we do in this case
48             pass