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 |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 8 | from CordTestUtils import running_on_pod |
| 9 | from SSHTestAgent import SSHTestAgent |
A.R Karthick | 8f7f1b6 | 2017-04-06 18:25:07 -0700 | [diff] [blame] | 10 | log.setLevel('INFO') |
| 11 | |
| 12 | class CordTestConfigRestore(Plugin): |
| 13 | name = 'cordTestConfigRestore' |
| 14 | context = None |
| 15 | restore_methods = ('configRestore', 'config_restore',) |
| 16 | |
| 17 | def options(self, parser, env=os.environ): |
| 18 | super(CordTestConfigRestore, self).options(parser, env = env) |
| 19 | |
| 20 | def configure(self, options, conf): |
| 21 | self.enabled = True |
| 22 | |
| 23 | #just save the test case context on start |
| 24 | def startContext(self, context): |
| 25 | if inspect.isclass(context) and issubclass(context, unittest.TestCase): |
| 26 | if context.__name__.endswith('exchange'): |
| 27 | self.context = context |
| 28 | |
| 29 | #reset the context on exit |
A R Karthick | 9a16a11 | 2017-04-07 15:40:05 -0700 | [diff] [blame] | 30 | def stopContext(self, context): |
A.R Karthick | 8f7f1b6 | 2017-04-06 18:25:07 -0700 | [diff] [blame] | 31 | if inspect.isclass(context) and issubclass(context, unittest.TestCase): |
| 32 | if context.__name__.endswith('exchange'): |
| 33 | self.context = None |
| 34 | |
| 35 | def doFailure(self, test, exception): |
| 36 | if self.context: |
| 37 | log.info('Inside test case failure for test: %s' %self.context.__name__) |
| 38 | for restore_method in self.restore_methods: |
| 39 | if hasattr(self.context, restore_method): |
| 40 | method = getattr(self.context, restore_method) |
| 41 | #check only for class/static methods |
| 42 | if method.__self__ is self.context: |
| 43 | method() |
| 44 | break |
| 45 | |
| 46 | def addError(self, test, exception): |
| 47 | self.doFailure(test, exception) |
| 48 | |
| 49 | def addFailure(self, test, exception): |
| 50 | self.doFailure(test, exception) |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 51 | |
| 52 | def setup_module(module): |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 53 | class_test = None |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 54 | for name, obj in inspect.getmembers(module): |
| 55 | if inspect.isclass(obj) and issubclass(obj, unittest.TestCase): |
| 56 | if obj.__name__.endswith('exchange'): |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 57 | class_test = obj |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 58 | break |
| 59 | else: |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 60 | class_test = obj |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 61 | |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 62 | assert_not_equal(class_test, None) |
| 63 | module_name = module.__name__.split('.')[-1] |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 64 | cfg = '{}.json'.format(module_name) |
| 65 | module_config = os.path.join(os.path.dirname(module.__file__), cfg) |
| 66 | if os.access(module_config, os.F_OK): |
| 67 | with open(module_config) as f: |
| 68 | json_data = json.load(f) |
| 69 | for k, v in json_data.iteritems(): |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 70 | setattr(class_test, k, v) |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 71 | |
| 72 | def running_on_ciab(): |
| 73 | if running_on_pod() is False: |
| 74 | return False |
| 75 | head_node = os.getenv('HEAD_NODE', 'prod') |
| 76 | HEAD_NODE = head_node + '.cord.lab' if len(head_node.split('.')) == 1 else head_node |
| 77 | agent = SSHTestAgent(host = HEAD_NODE, user = 'ubuntu', password = 'ubuntu') |
| 78 | #see if user ubuntu works |
| 79 | st, output = agent.run_cmd('sudo virsh list') |
| 80 | if st is False and output is not None: |
| 81 | #we are on real pod |
| 82 | return False |
| 83 | |
| 84 | #try vagrant |
| 85 | agent = SSHTestAgent(host = HEAD_NODE, user = 'vagrant', password = 'vagrant') |
| 86 | st, output = agent.run_cmd('sudo virsh list') |
| 87 | if st is True and output is not None: |
| 88 | return True |
| 89 | |
| 90 | return False |