blob: 7aca5cb27d56bbe4280e97b5b149d175e3f8b2c6 [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
A R Karthick19771192017-04-25 14:57:05 -07008from CordTestUtils import running_on_pod
9from SSHTestAgent import SSHTestAgent
A.R Karthick8f7f1b62017-04-06 18:25:07 -070010log.setLevel('INFO')
11
12class 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 Karthick9a16a112017-04-07 15:40:05 -070030 def stopContext(self, context):
A.R Karthick8f7f1b62017-04-06 18:25:07 -070031 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 Karthick861da962017-02-08 16:21:36 -080051
52def setup_module(module):
A.R Karthick99044822017-02-09 14:04:20 -080053 class_test = None
A R Karthick861da962017-02-08 16:21:36 -080054 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 Karthick99044822017-02-09 14:04:20 -080057 class_test = obj
A R Karthick861da962017-02-08 16:21:36 -080058 break
59 else:
A.R Karthick99044822017-02-09 14:04:20 -080060 class_test = obj
A R Karthick861da962017-02-08 16:21:36 -080061
A.R Karthick99044822017-02-09 14:04:20 -080062 assert_not_equal(class_test, None)
63 module_name = module.__name__.split('.')[-1]
A R Karthick861da962017-02-08 16:21:36 -080064 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 Karthick99044822017-02-09 14:04:20 -080070 setattr(class_test, k, v)
A R Karthick19771192017-04-25 14:57:05 -070071
72def 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