A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 1 | import inspect |
| 2 | import unittest |
| 3 | import json |
| 4 | import os |
| 5 | from nose.tools import assert_not_equal |
A.R Karthick | 8f7f1b6 | 2017-04-06 18:25:07 -0700 | [diff] [blame] | 6 | from nose.plugins import Plugin |
| 7 | from CordTestUtils import log_test as log |
| 8 | |
| 9 | log.setLevel('INFO') |
| 10 | |
| 11 | class 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 Karthick | 9a16a11 | 2017-04-07 15:40:05 -0700 | [diff] [blame] | 29 | def stopContext(self, context): |
A.R Karthick | 8f7f1b6 | 2017-04-06 18:25:07 -0700 | [diff] [blame] | 30 | 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 Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 50 | |
| 51 | def setup_module(module): |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 52 | class_test = None |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 53 | 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 Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 56 | class_test = obj |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 57 | break |
| 58 | else: |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 59 | class_test = obj |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 60 | |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 61 | assert_not_equal(class_test, None) |
| 62 | module_name = module.__name__.split('.')[-1] |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 63 | 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 Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 69 | setattr(class_test, k, v) |