blob: 8234cf02ee9690b6f1684716a3a3fd5864f5a990 [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
A R Karthick861da962017-02-08 16:21:36 -080017import inspect
18import unittest
19import json
20import os
A R Karthickefcf1ab2017-09-08 18:24:16 -070021import time
A R Karthick861da962017-02-08 16:21:36 -080022from nose.tools import assert_not_equal
A.R Karthick8f7f1b62017-04-06 18:25:07 -070023from nose.plugins import Plugin
24from CordTestUtils import log_test as log
A R Karthick19771192017-04-25 14:57:05 -070025from CordTestUtils import running_on_pod
A R Karthick32e711a2017-08-22 16:27:22 -070026from VolthaCtrl import voltha_setup, voltha_teardown, VolthaService, VolthaCtrl
A R Karthick1555c7c2017-09-07 14:59:41 -070027from OnosCtrl import OnosCtrl
A R Karthick19771192017-04-25 14:57:05 -070028from SSHTestAgent import SSHTestAgent
A.R Karthick8f7f1b62017-04-06 18:25:07 -070029log.setLevel('INFO')
30
31class CordTestConfigRestore(Plugin):
32 name = 'cordTestConfigRestore'
33 context = None
34 restore_methods = ('configRestore', 'config_restore',)
35
36 def options(self, parser, env=os.environ):
37 super(CordTestConfigRestore, self).options(parser, env = env)
38
39 def configure(self, options, conf):
40 self.enabled = True
41
42 #just save the test case context on start
43 def startContext(self, context):
44 if inspect.isclass(context) and issubclass(context, unittest.TestCase):
45 if context.__name__.endswith('exchange'):
46 self.context = context
47
48 #reset the context on exit
A R Karthick9a16a112017-04-07 15:40:05 -070049 def stopContext(self, context):
A.R Karthick8f7f1b62017-04-06 18:25:07 -070050 if inspect.isclass(context) and issubclass(context, unittest.TestCase):
51 if context.__name__.endswith('exchange'):
52 self.context = None
53
54 def doFailure(self, test, exception):
55 if self.context:
56 log.info('Inside test case failure for test: %s' %self.context.__name__)
57 for restore_method in self.restore_methods:
58 if hasattr(self.context, restore_method):
59 method = getattr(self.context, restore_method)
60 #check only for class/static methods
61 if method.__self__ is self.context:
62 method()
63 break
64
65 def addError(self, test, exception):
66 self.doFailure(test, exception)
67
68 def addFailure(self, test, exception):
69 self.doFailure(test, exception)
A R Karthick861da962017-02-08 16:21:36 -080070
A R Karthickdd064632017-07-12 13:02:17 -070071def get_test_class(module):
A.R Karthick99044822017-02-09 14:04:20 -080072 class_test = None
A R Karthick861da962017-02-08 16:21:36 -080073 for name, obj in inspect.getmembers(module):
74 if inspect.isclass(obj) and issubclass(obj, unittest.TestCase):
75 if obj.__name__.endswith('exchange'):
A.R Karthick99044822017-02-09 14:04:20 -080076 class_test = obj
A R Karthick861da962017-02-08 16:21:36 -080077 break
78 else:
A.R Karthick99044822017-02-09 14:04:20 -080079 class_test = obj
A R Karthick861da962017-02-08 16:21:36 -080080
A R Karthickdd064632017-07-12 13:02:17 -070081 return class_test
82
83def setup_module(module):
84 class_test = get_test_class(module)
A.R Karthick99044822017-02-09 14:04:20 -080085 assert_not_equal(class_test, None)
86 module_name = module.__name__.split('.')[-1]
A R Karthick861da962017-02-08 16:21:36 -080087 cfg = '{}.json'.format(module_name)
88 module_config = os.path.join(os.path.dirname(module.__file__), cfg)
89 if os.access(module_config, os.F_OK):
90 with open(module_config) as f:
91 json_data = json.load(f)
92 for k, v in json_data.iteritems():
A.R Karthick99044822017-02-09 14:04:20 -080093 setattr(class_test, k, v)
A R Karthick19771192017-04-25 14:57:05 -070094
A R Karthickdd064632017-07-12 13:02:17 -070095 #check for voltha and configure as appropriate
A R Karthick168e2342017-08-15 16:13:10 -070096 voltha_attrs = dict(host = VolthaService.DOCKER_HOST_IP,
97 ponsim_host = VolthaService.PONSIM_HOST,
A R Karthick32e711a2017-08-22 16:27:22 -070098 rest_port = VolthaCtrl.REST_PORT,
A R Karthickdd064632017-07-12 13:02:17 -070099 config_fake = False,
A R Karthick9dc6e922017-07-12 14:40:16 -0700100 olt_type = 'ponsim_olt',
A R Karthickdd064632017-07-12 13:02:17 -0700101 olt_mac = '00:0c:e2:31:12:00',
A R Karthick31a40172017-08-14 12:06:09 -0700102 olt_ip = None,
A R Karthick53442712017-07-27 12:23:30 -0700103 uplink_vlan_map = { 'of:0000000000000001' : '222' },
A R Karthick18d0fb62017-09-01 18:49:07 -0700104 uplink_vlan_start = 333,
105 teardown = True,
A R Karthickdd064632017-07-12 13:02:17 -0700106 )
107 voltha_enabled = bool(int(os.getenv('VOLTHA_ENABLED', 0)))
108 voltha_configure = True
A R Karthick1555c7c2017-09-07 14:59:41 -0700109
110 olt_switch_map = {}
111
A R Karthickdd064632017-07-12 13:02:17 -0700112 if hasattr(class_test, 'VOLTHA_AUTO_CONFIGURE'):
113 voltha_configure = getattr(class_test, 'VOLTHA_AUTO_CONFIGURE')
114
A R Karthick168e2342017-08-15 16:13:10 -0700115 if hasattr(class_test, 'VOLTHA_HOST'):
116 #update the voltha host ip based on chameleon IP for rest interface
117 rest_interface = VolthaService.get_ip('chameleon')
118 if rest_interface:
119 log.info('Updating VOLTHA_HOST IP to %s' %rest_interface)
120 setattr(class_test, 'VOLTHA_HOST', rest_interface)
121
A R Karthickdd064632017-07-12 13:02:17 -0700122 if voltha_enabled and voltha_configure:
123 for k,v in voltha_attrs.iteritems():
124 voltha_attr = 'VOLTHA_{}'.format(k.upper())
125 if hasattr(class_test, voltha_attr):
126 v = getattr(class_test, voltha_attr)
127 voltha_attrs[k] = v
128 else:
129 setattr(class_test, voltha_attr, v)
130 ret = voltha_setup(**voltha_attrs)
131 if ret is not None:
132 #setup the stage to drop voltha on the way out
133 setattr(class_test, 'voltha_ctrl', ret[0])
134 setattr(class_test, 'voltha_device', ret[1])
135 setattr(class_test, 'voltha_switch_map', ret[2])
A R Karthick1555c7c2017-09-07 14:59:41 -0700136 olt_switch_map = ret[2]
A R Karthick18d0fb62017-09-01 18:49:07 -0700137 voltha_driver_configured = ret[3]
138 setattr(class_test, 'voltha_preconfigured', voltha_driver_configured)
139 if voltha_driver_configured:
140 setattr(class_test, 'VOLTHA_TEARDOWN', False)
A R Karthickdd064632017-07-12 13:02:17 -0700141
A R Karthick1555c7c2017-09-07 14:59:41 -0700142 #load the sadis and aaa config
143 OnosCtrl.sadis_load_config(olt_switch_map = olt_switch_map)
144 OnosCtrl.aaa_load_config()
A R Karthickefcf1ab2017-09-08 18:24:16 -0700145 #OnosCtrl('org.opencord.aaa').deactivate()
146 #time.sleep(3)
147 #OnosCtrl('org.opencord.aaa').activate()
148 #time.sleep(3)
149 if voltha_enabled is False:
150 OnosCtrl.config_olt_access(VolthaCtrl.UPLINK_VLAN_START)
A R Karthick1555c7c2017-09-07 14:59:41 -0700151
A R Karthickdd064632017-07-12 13:02:17 -0700152def teardown_module(module):
153 class_test = get_test_class(module)
154 if class_test is None:
155 return
156 if not hasattr(class_test, 'voltha_ctrl') or \
157 not hasattr(class_test, 'voltha_device') or \
A R Karthick18d0fb62017-09-01 18:49:07 -0700158 not hasattr(class_test, 'voltha_switch_map') or \
159 not hasattr(class_test, 'voltha_preconfigured') or \
160 not hasattr(class_test, 'VOLTHA_TEARDOWN'):
A R Karthickdd064632017-07-12 13:02:17 -0700161 return
162 voltha_ctrl = getattr(class_test, 'voltha_ctrl')
163 voltha_device = getattr(class_test, 'voltha_device')
164 voltha_switch_map = getattr(class_test, 'voltha_switch_map')
A R Karthick18d0fb62017-09-01 18:49:07 -0700165 voltha_preconfigured = getattr(class_test, 'voltha_preconfigured')
A R Karthick6708f562017-09-05 12:15:31 -0700166 voltha_tear = getattr(class_test, 'VOLTHA_TEARDOWN')
167 if voltha_preconfigured is False and voltha_tear is True:
A R Karthick18d0fb62017-09-01 18:49:07 -0700168 voltha_teardown(voltha_ctrl, voltha_device, voltha_switch_map)
A R Karthickdd064632017-07-12 13:02:17 -0700169
A R Karthick19771192017-04-25 14:57:05 -0700170def running_on_ciab():
171 if running_on_pod() is False:
172 return False
173 head_node = os.getenv('HEAD_NODE', 'prod')
174 HEAD_NODE = head_node + '.cord.lab' if len(head_node.split('.')) == 1 else head_node
175 agent = SSHTestAgent(host = HEAD_NODE, user = 'ubuntu', password = 'ubuntu')
176 #see if user ubuntu works
177 st, output = agent.run_cmd('sudo virsh list')
178 if st is False and output is not None:
179 #we are on real pod
180 return False
181
182 #try vagrant
183 agent = SSHTestAgent(host = HEAD_NODE, user = 'vagrant', password = 'vagrant')
184 st, output = agent.run_cmd('sudo virsh list')
185 if st is True and output is not None:
186 return True
187
188 return False