Merge "-refactored utility code to separate modules and classes -upgraded stress...
[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 class Comparator():
10
11     log = logging.getLogger('Comparator')
12
13     @staticmethod
14     def compare_results(actual, expected):
15         #print 'ACT: ', actual
16         #print 'EXP: ', expected
17
18         list_unused = list(set(actual.keys()) - set(expected.keys()))
19         if len(list_unused) > 0:
20             Comparator.log.info('unchecked tags: {}'.format(list_unused))
21
22         list_duration = ['duration','hard_timeout','idle_timeout']
23
24         Comparator.test_duration(actual, expected)
25
26         # compare results from actual flow (mn dump result) and expepected flow (stored result)
27         for k in expected.keys():
28             if k not in list_duration:
29                 assert k in actual, 'cannot find key {} in flow {}'.format(k, actual)
30                 assert actual[k] == expected[k], 'key:{}, actual:{} != expected:{}'.format(k, actual[k], expected[k])
31
32     @staticmethod
33     def test_duration(actual, expected):
34         duration_key = 'duration'
35         hard_to_key = 'hard_timeout'
36
37         if duration_key in expected.keys():
38             assert duration_key in actual.keys(), '{} is not set in {}'.format(duration_key, actual)
39             try:
40                 duration = float(expected['duration'].rstrip('s'))
41                 hard_timeout = int(actual['hard_timeout'])
42                 assert duration <= hard_timeout, 'duration is higher than hard_timeout, {} > {}'.format(duration, hard_timeout)
43             except KeyError as e:
44                 Comparator.log.warning('cannot find keys to test duration tag', exc_info=True)
45         else:
46             # VD - what should we do in this case
47             pass