blob: 45527b60cae05582c5179663456f46af696038db [file] [log] [blame]
A R Karthick861da962017-02-08 16:21:36 -08001import inspect
2import unittest
3import json
4import os
5from nose.tools import assert_not_equal
A.R Karthick8f7f1b62017-04-06 18:25:07 -07006from nose.plugins import Plugin
7from CordTestUtils import log_test as log
8
9log.setLevel('INFO')
10
11class CordTestConfigRestore(Plugin):
12 name = 'cordTestConfigRestore'
13 context = None
14 restore_methods = ('configRestore', 'config_restore',)
15
16 def options(self, parser, env=os.environ):
17 super(CordTestConfigRestore, self).options(parser, env = env)
18
19 def configure(self, options, conf):
20 self.enabled = True
21
22 #just save the test case context on start
23 def startContext(self, context):
24 if inspect.isclass(context) and issubclass(context, unittest.TestCase):
25 if context.__name__.endswith('exchange'):
26 self.context = context
27
28 #reset the context on exit
A R Karthick9a16a112017-04-07 15:40:05 -070029 def stopContext(self, context):
A.R Karthick8f7f1b62017-04-06 18:25:07 -070030 if inspect.isclass(context) and issubclass(context, unittest.TestCase):
31 if context.__name__.endswith('exchange'):
32 self.context = None
33
34 def doFailure(self, test, exception):
35 if self.context:
36 log.info('Inside test case failure for test: %s' %self.context.__name__)
37 for restore_method in self.restore_methods:
38 if hasattr(self.context, restore_method):
39 method = getattr(self.context, restore_method)
40 #check only for class/static methods
41 if method.__self__ is self.context:
42 method()
43 break
44
45 def addError(self, test, exception):
46 self.doFailure(test, exception)
47
48 def addFailure(self, test, exception):
49 self.doFailure(test, exception)
A R Karthick861da962017-02-08 16:21:36 -080050
51def setup_module(module):
A.R Karthick99044822017-02-09 14:04:20 -080052 class_test = None
A R Karthick861da962017-02-08 16:21:36 -080053 for name, obj in inspect.getmembers(module):
54 if inspect.isclass(obj) and issubclass(obj, unittest.TestCase):
55 if obj.__name__.endswith('exchange'):
A.R Karthick99044822017-02-09 14:04:20 -080056 class_test = obj
A R Karthick861da962017-02-08 16:21:36 -080057 break
58 else:
A.R Karthick99044822017-02-09 14:04:20 -080059 class_test = obj
A R Karthick861da962017-02-08 16:21:36 -080060
A.R Karthick99044822017-02-09 14:04:20 -080061 assert_not_equal(class_test, None)
62 module_name = module.__name__.split('.')[-1]
A R Karthick861da962017-02-08 16:21:36 -080063 cfg = '{}.json'.format(module_name)
64 module_config = os.path.join(os.path.dirname(module.__file__), cfg)
65 if os.access(module_config, os.F_OK):
66 with open(module_config) as f:
67 json_data = json.load(f)
68 for k, v in json_data.iteritems():
A.R Karthick99044822017-02-09 14:04:20 -080069 setattr(class_test, k, v)