blob: f17fcf8f20dc21d1a4796acca2ba745d2d55a9b9 [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',
A R Karthick53442712017-07-27 12:23:30 -070083 uplink_vlan_map = { 'of:0000000000000001' : '222' },
84 uplink_vlan_start = 333
A R Karthickdd064632017-07-12 13:02:17 -070085 )
86 voltha_enabled = bool(int(os.getenv('VOLTHA_ENABLED', 0)))
87 voltha_configure = True
88 if hasattr(class_test, 'VOLTHA_AUTO_CONFIGURE'):
89 voltha_configure = getattr(class_test, 'VOLTHA_AUTO_CONFIGURE')
90
91 if voltha_enabled and voltha_configure:
92 for k,v in voltha_attrs.iteritems():
93 voltha_attr = 'VOLTHA_{}'.format(k.upper())
94 if hasattr(class_test, voltha_attr):
95 v = getattr(class_test, voltha_attr)
96 voltha_attrs[k] = v
97 else:
98 setattr(class_test, voltha_attr, v)
99 ret = voltha_setup(**voltha_attrs)
100 if ret is not None:
101 #setup the stage to drop voltha on the way out
102 setattr(class_test, 'voltha_ctrl', ret[0])
103 setattr(class_test, 'voltha_device', ret[1])
104 setattr(class_test, 'voltha_switch_map', ret[2])
105
106def teardown_module(module):
107 class_test = get_test_class(module)
108 if class_test is None:
109 return
110 if not hasattr(class_test, 'voltha_ctrl') or \
111 not hasattr(class_test, 'voltha_device') or \
112 not hasattr(class_test, 'voltha_switch_map'):
113 return
114 voltha_ctrl = getattr(class_test, 'voltha_ctrl')
115 voltha_device = getattr(class_test, 'voltha_device')
116 voltha_switch_map = getattr(class_test, 'voltha_switch_map')
117 voltha_teardown(voltha_ctrl, voltha_device, voltha_switch_map)
118
A R Karthick19771192017-04-25 14:57:05 -0700119def running_on_ciab():
120 if running_on_pod() is False:
121 return False
122 head_node = os.getenv('HEAD_NODE', 'prod')
123 HEAD_NODE = head_node + '.cord.lab' if len(head_node.split('.')) == 1 else head_node
124 agent = SSHTestAgent(host = HEAD_NODE, user = 'ubuntu', password = 'ubuntu')
125 #see if user ubuntu works
126 st, output = agent.run_cmd('sudo virsh list')
127 if st is False and output is not None:
128 #we are on real pod
129 return False
130
131 #try vagrant
132 agent = SSHTestAgent(host = HEAD_NODE, user = 'vagrant', password = 'vagrant')
133 st, output = agent.run_cmd('sudo virsh list')
134 if st is True and output is not None:
135 return True
136
137 return False