blob: 34dbb8580fb289dfedc4fa072d8b062fdc163786 [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 Karthickce96dde2017-10-05 16:41:52 -0700115 tagged_traffic = False
116 if hasattr(class_test, 'TAGGED_TRAFFIC'):
117 tagged_traffic = getattr(class_test, 'TAGGED_TRAFFIC')
118
A R Karthick168e2342017-08-15 16:13:10 -0700119 if hasattr(class_test, 'VOLTHA_HOST'):
120 #update the voltha host ip based on chameleon IP for rest interface
121 rest_interface = VolthaService.get_ip('chameleon')
122 if rest_interface:
123 log.info('Updating VOLTHA_HOST IP to %s' %rest_interface)
124 setattr(class_test, 'VOLTHA_HOST', rest_interface)
125
A R Karthickdd064632017-07-12 13:02:17 -0700126 if voltha_enabled and voltha_configure:
127 for k,v in voltha_attrs.iteritems():
128 voltha_attr = 'VOLTHA_{}'.format(k.upper())
129 if hasattr(class_test, voltha_attr):
130 v = getattr(class_test, voltha_attr)
131 voltha_attrs[k] = v
132 else:
133 setattr(class_test, voltha_attr, v)
134 ret = voltha_setup(**voltha_attrs)
135 if ret is not None:
136 #setup the stage to drop voltha on the way out
137 setattr(class_test, 'voltha_ctrl', ret[0])
138 setattr(class_test, 'voltha_device', ret[1])
139 setattr(class_test, 'voltha_switch_map', ret[2])
A R Karthick1555c7c2017-09-07 14:59:41 -0700140 olt_switch_map = ret[2]
A R Karthick18d0fb62017-09-01 18:49:07 -0700141 voltha_driver_configured = ret[3]
142 setattr(class_test, 'voltha_preconfigured', voltha_driver_configured)
143 if voltha_driver_configured:
144 setattr(class_test, 'VOLTHA_TEARDOWN', False)
A R Karthickdd064632017-07-12 13:02:17 -0700145
A R Karthick1555c7c2017-09-07 14:59:41 -0700146 #load the sadis and aaa config
A R Karthickce96dde2017-10-05 16:41:52 -0700147 OnosCtrl.sadis_load_config(olt_switch_map = olt_switch_map, tagged_traffic = tagged_traffic)
A R Karthick1555c7c2017-09-07 14:59:41 -0700148 OnosCtrl.aaa_load_config()
A R Karthickefcf1ab2017-09-08 18:24:16 -0700149 #OnosCtrl('org.opencord.aaa').deactivate()
150 #time.sleep(3)
151 #OnosCtrl('org.opencord.aaa').activate()
152 #time.sleep(3)
153 if voltha_enabled is False:
154 OnosCtrl.config_olt_access(VolthaCtrl.UPLINK_VLAN_START)
A R Karthick1555c7c2017-09-07 14:59:41 -0700155
A R Karthickdd064632017-07-12 13:02:17 -0700156def teardown_module(module):
157 class_test = get_test_class(module)
158 if class_test is None:
159 return
160 if not hasattr(class_test, 'voltha_ctrl') or \
161 not hasattr(class_test, 'voltha_device') or \
A R Karthick18d0fb62017-09-01 18:49:07 -0700162 not hasattr(class_test, 'voltha_switch_map') or \
163 not hasattr(class_test, 'voltha_preconfigured') or \
164 not hasattr(class_test, 'VOLTHA_TEARDOWN'):
A R Karthickdd064632017-07-12 13:02:17 -0700165 return
166 voltha_ctrl = getattr(class_test, 'voltha_ctrl')
167 voltha_device = getattr(class_test, 'voltha_device')
168 voltha_switch_map = getattr(class_test, 'voltha_switch_map')
A R Karthick18d0fb62017-09-01 18:49:07 -0700169 voltha_preconfigured = getattr(class_test, 'voltha_preconfigured')
A R Karthick6708f562017-09-05 12:15:31 -0700170 voltha_tear = getattr(class_test, 'VOLTHA_TEARDOWN')
171 if voltha_preconfigured is False and voltha_tear is True:
A R Karthick18d0fb62017-09-01 18:49:07 -0700172 voltha_teardown(voltha_ctrl, voltha_device, voltha_switch_map)
A R Karthickdd064632017-07-12 13:02:17 -0700173
A R Karthick19771192017-04-25 14:57:05 -0700174def running_on_ciab():
175 if running_on_pod() is False:
176 return False
177 head_node = os.getenv('HEAD_NODE', 'prod')
178 HEAD_NODE = head_node + '.cord.lab' if len(head_node.split('.')) == 1 else head_node
179 agent = SSHTestAgent(host = HEAD_NODE, user = 'ubuntu', password = 'ubuntu')
180 #see if user ubuntu works
181 st, output = agent.run_cmd('sudo virsh list')
182 if st is False and output is not None:
183 #we are on real pod
184 return False
185
186 #try vagrant
187 agent = SSHTestAgent(host = HEAD_NODE, user = 'vagrant', password = 'vagrant')
188 st, output = agent.run_cmd('sudo virsh list')
189 if st is True and output is not None:
190 return True
191
192 return False