blob: f41d0da64f5c021f495a58b345d09e4503c0d900 [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
A R Karthickdd064632017-07-12 13:02:17 -07009from VolthaCtrl import voltha_setup, voltha_teardown
A R Karthick19771192017-04-25 14:57:05 -070010from SSHTestAgent import SSHTestAgent
A.R Karthick8f7f1b62017-04-06 18:25:07 -070011log.setLevel('INFO')
12
13class CordTestConfigRestore(Plugin):
14 name = 'cordTestConfigRestore'
15 context = None
16 restore_methods = ('configRestore', 'config_restore',)
17
18 def options(self, parser, env=os.environ):
19 super(CordTestConfigRestore, self).options(parser, env = env)
20
21 def configure(self, options, conf):
22 self.enabled = True
23
24 #just save the test case context on start
25 def startContext(self, context):
26 if inspect.isclass(context) and issubclass(context, unittest.TestCase):
27 if context.__name__.endswith('exchange'):
28 self.context = context
29
30 #reset the context on exit
A R Karthick9a16a112017-04-07 15:40:05 -070031 def stopContext(self, context):
A.R Karthick8f7f1b62017-04-06 18:25:07 -070032 if inspect.isclass(context) and issubclass(context, unittest.TestCase):
33 if context.__name__.endswith('exchange'):
34 self.context = None
35
36 def doFailure(self, test, exception):
37 if self.context:
38 log.info('Inside test case failure for test: %s' %self.context.__name__)
39 for restore_method in self.restore_methods:
40 if hasattr(self.context, restore_method):
41 method = getattr(self.context, restore_method)
42 #check only for class/static methods
43 if method.__self__ is self.context:
44 method()
45 break
46
47 def addError(self, test, exception):
48 self.doFailure(test, exception)
49
50 def addFailure(self, test, exception):
51 self.doFailure(test, exception)
A R Karthick861da962017-02-08 16:21:36 -080052
A R Karthickdd064632017-07-12 13:02:17 -070053def get_test_class(module):
A.R Karthick99044822017-02-09 14:04:20 -080054 class_test = None
A R Karthick861da962017-02-08 16:21:36 -080055 for name, obj in inspect.getmembers(module):
56 if inspect.isclass(obj) and issubclass(obj, unittest.TestCase):
57 if obj.__name__.endswith('exchange'):
A.R Karthick99044822017-02-09 14:04:20 -080058 class_test = obj
A R Karthick861da962017-02-08 16:21:36 -080059 break
60 else:
A.R Karthick99044822017-02-09 14:04:20 -080061 class_test = obj
A R Karthick861da962017-02-08 16:21:36 -080062
A R Karthickdd064632017-07-12 13:02:17 -070063 return class_test
64
65def setup_module(module):
66 class_test = get_test_class(module)
A.R Karthick99044822017-02-09 14:04:20 -080067 assert_not_equal(class_test, None)
68 module_name = module.__name__.split('.')[-1]
A R Karthick861da962017-02-08 16:21:36 -080069 cfg = '{}.json'.format(module_name)
70 module_config = os.path.join(os.path.dirname(module.__file__), cfg)
71 if os.access(module_config, os.F_OK):
72 with open(module_config) as f:
73 json_data = json.load(f)
74 for k, v in json_data.iteritems():
A.R Karthick99044822017-02-09 14:04:20 -080075 setattr(class_test, k, v)
A R Karthick19771192017-04-25 14:57:05 -070076
A R Karthickdd064632017-07-12 13:02:17 -070077 #check for voltha and configure as appropriate
78 voltha_attrs = dict(host='172.17.0.1',
79 rest_port = 8881,
80 config_fake = False,
A R Karthick9dc6e922017-07-12 14:40:16 -070081 olt_type = 'ponsim_olt',
A R Karthickdd064632017-07-12 13:02:17 -070082 olt_mac = '00:0c:e2:31:12:00',
83 uplink_vlan_map = { 'of:0000000000000001' : '222' }
84 )
85 voltha_enabled = bool(int(os.getenv('VOLTHA_ENABLED', 0)))
86 voltha_configure = True
87 if hasattr(class_test, 'VOLTHA_AUTO_CONFIGURE'):
88 voltha_configure = getattr(class_test, 'VOLTHA_AUTO_CONFIGURE')
89
90 if voltha_enabled and voltha_configure:
91 for k,v in voltha_attrs.iteritems():
92 voltha_attr = 'VOLTHA_{}'.format(k.upper())
93 if hasattr(class_test, voltha_attr):
94 v = getattr(class_test, voltha_attr)
95 voltha_attrs[k] = v
96 else:
97 setattr(class_test, voltha_attr, v)
98 ret = voltha_setup(**voltha_attrs)
99 if ret is not None:
100 #setup the stage to drop voltha on the way out
101 setattr(class_test, 'voltha_ctrl', ret[0])
102 setattr(class_test, 'voltha_device', ret[1])
103 setattr(class_test, 'voltha_switch_map', ret[2])
104
105def teardown_module(module):
106 class_test = get_test_class(module)
107 if class_test is None:
108 return
109 if not hasattr(class_test, 'voltha_ctrl') or \
110 not hasattr(class_test, 'voltha_device') or \
111 not hasattr(class_test, 'voltha_switch_map'):
112 return
113 voltha_ctrl = getattr(class_test, 'voltha_ctrl')
114 voltha_device = getattr(class_test, 'voltha_device')
115 voltha_switch_map = getattr(class_test, 'voltha_switch_map')
116 voltha_teardown(voltha_ctrl, voltha_device, voltha_switch_map)
117
A R Karthick19771192017-04-25 14:57:05 -0700118def running_on_ciab():
119 if running_on_pod() is False:
120 return False
121 head_node = os.getenv('HEAD_NODE', 'prod')
122 HEAD_NODE = head_node + '.cord.lab' if len(head_node.split('.')) == 1 else head_node
123 agent = SSHTestAgent(host = HEAD_NODE, user = 'ubuntu', password = 'ubuntu')
124 #see if user ubuntu works
125 st, output = agent.run_cmd('sudo virsh list')
126 if st is False and output is not None:
127 #we are on real pod
128 return False
129
130 #try vagrant
131 agent = SSHTestAgent(host = HEAD_NODE, user = 'vagrant', password = 'vagrant')
132 st, output = agent.run_cmd('sudo virsh list')
133 if st is True and output is not None:
134 return True
135
136 return False