blob: 56537736577c1fa8ac7d643447719091247f584f [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
21from nose.tools import assert_not_equal
A.R Karthick8f7f1b62017-04-06 18:25:07 -070022from nose.plugins import Plugin
23from CordTestUtils import log_test as log
A R Karthick19771192017-04-25 14:57:05 -070024from CordTestUtils import running_on_pod
A R Karthick32e711a2017-08-22 16:27:22 -070025from VolthaCtrl import voltha_setup, voltha_teardown, VolthaService, VolthaCtrl
A R Karthick19771192017-04-25 14:57:05 -070026from SSHTestAgent import SSHTestAgent
A.R Karthick8f7f1b62017-04-06 18:25:07 -070027log.setLevel('INFO')
28
29class CordTestConfigRestore(Plugin):
30 name = 'cordTestConfigRestore'
31 context = None
32 restore_methods = ('configRestore', 'config_restore',)
33
34 def options(self, parser, env=os.environ):
35 super(CordTestConfigRestore, self).options(parser, env = env)
36
37 def configure(self, options, conf):
38 self.enabled = True
39
40 #just save the test case context on start
41 def startContext(self, context):
42 if inspect.isclass(context) and issubclass(context, unittest.TestCase):
43 if context.__name__.endswith('exchange'):
44 self.context = context
45
46 #reset the context on exit
A R Karthick9a16a112017-04-07 15:40:05 -070047 def stopContext(self, context):
A.R Karthick8f7f1b62017-04-06 18:25:07 -070048 if inspect.isclass(context) and issubclass(context, unittest.TestCase):
49 if context.__name__.endswith('exchange'):
50 self.context = None
51
52 def doFailure(self, test, exception):
53 if self.context:
54 log.info('Inside test case failure for test: %s' %self.context.__name__)
55 for restore_method in self.restore_methods:
56 if hasattr(self.context, restore_method):
57 method = getattr(self.context, restore_method)
58 #check only for class/static methods
59 if method.__self__ is self.context:
60 method()
61 break
62
63 def addError(self, test, exception):
64 self.doFailure(test, exception)
65
66 def addFailure(self, test, exception):
67 self.doFailure(test, exception)
A R Karthick861da962017-02-08 16:21:36 -080068
A R Karthickdd064632017-07-12 13:02:17 -070069def get_test_class(module):
A.R Karthick99044822017-02-09 14:04:20 -080070 class_test = None
A R Karthick861da962017-02-08 16:21:36 -080071 for name, obj in inspect.getmembers(module):
72 if inspect.isclass(obj) and issubclass(obj, unittest.TestCase):
73 if obj.__name__.endswith('exchange'):
A.R Karthick99044822017-02-09 14:04:20 -080074 class_test = obj
A R Karthick861da962017-02-08 16:21:36 -080075 break
76 else:
A.R Karthick99044822017-02-09 14:04:20 -080077 class_test = obj
A R Karthick861da962017-02-08 16:21:36 -080078
A R Karthickdd064632017-07-12 13:02:17 -070079 return class_test
80
81def setup_module(module):
82 class_test = get_test_class(module)
A.R Karthick99044822017-02-09 14:04:20 -080083 assert_not_equal(class_test, None)
84 module_name = module.__name__.split('.')[-1]
A R Karthick861da962017-02-08 16:21:36 -080085 cfg = '{}.json'.format(module_name)
86 module_config = os.path.join(os.path.dirname(module.__file__), cfg)
87 if os.access(module_config, os.F_OK):
88 with open(module_config) as f:
89 json_data = json.load(f)
90 for k, v in json_data.iteritems():
A.R Karthick99044822017-02-09 14:04:20 -080091 setattr(class_test, k, v)
A R Karthick19771192017-04-25 14:57:05 -070092
A R Karthickdd064632017-07-12 13:02:17 -070093 #check for voltha and configure as appropriate
A R Karthick168e2342017-08-15 16:13:10 -070094 voltha_attrs = dict(host = VolthaService.DOCKER_HOST_IP,
95 ponsim_host = VolthaService.PONSIM_HOST,
A R Karthick32e711a2017-08-22 16:27:22 -070096 rest_port = VolthaCtrl.REST_PORT,
A R Karthickdd064632017-07-12 13:02:17 -070097 config_fake = False,
A R Karthick9dc6e922017-07-12 14:40:16 -070098 olt_type = 'ponsim_olt',
A R Karthickdd064632017-07-12 13:02:17 -070099 olt_mac = '00:0c:e2:31:12:00',
A R Karthick31a40172017-08-14 12:06:09 -0700100 olt_ip = None,
A R Karthick53442712017-07-27 12:23:30 -0700101 uplink_vlan_map = { 'of:0000000000000001' : '222' },
A R Karthick18d0fb62017-09-01 18:49:07 -0700102 uplink_vlan_start = 333,
103 teardown = True,
A R Karthickdd064632017-07-12 13:02:17 -0700104 )
105 voltha_enabled = bool(int(os.getenv('VOLTHA_ENABLED', 0)))
106 voltha_configure = True
107 if hasattr(class_test, 'VOLTHA_AUTO_CONFIGURE'):
108 voltha_configure = getattr(class_test, 'VOLTHA_AUTO_CONFIGURE')
109
A R Karthick168e2342017-08-15 16:13:10 -0700110 if hasattr(class_test, 'VOLTHA_HOST'):
111 #update the voltha host ip based on chameleon IP for rest interface
112 rest_interface = VolthaService.get_ip('chameleon')
113 if rest_interface:
114 log.info('Updating VOLTHA_HOST IP to %s' %rest_interface)
115 setattr(class_test, 'VOLTHA_HOST', rest_interface)
116
A R Karthickdd064632017-07-12 13:02:17 -0700117 if voltha_enabled and voltha_configure:
118 for k,v in voltha_attrs.iteritems():
119 voltha_attr = 'VOLTHA_{}'.format(k.upper())
120 if hasattr(class_test, voltha_attr):
121 v = getattr(class_test, voltha_attr)
122 voltha_attrs[k] = v
123 else:
124 setattr(class_test, voltha_attr, v)
125 ret = voltha_setup(**voltha_attrs)
126 if ret is not None:
127 #setup the stage to drop voltha on the way out
128 setattr(class_test, 'voltha_ctrl', ret[0])
129 setattr(class_test, 'voltha_device', ret[1])
130 setattr(class_test, 'voltha_switch_map', ret[2])
A R Karthick18d0fb62017-09-01 18:49:07 -0700131 voltha_driver_configured = ret[3]
132 setattr(class_test, 'voltha_preconfigured', voltha_driver_configured)
133 if voltha_driver_configured:
134 setattr(class_test, 'VOLTHA_TEARDOWN', False)
A R Karthickdd064632017-07-12 13:02:17 -0700135
136def teardown_module(module):
137 class_test = get_test_class(module)
138 if class_test is None:
139 return
140 if not hasattr(class_test, 'voltha_ctrl') or \
141 not hasattr(class_test, 'voltha_device') or \
A R Karthick18d0fb62017-09-01 18:49:07 -0700142 not hasattr(class_test, 'voltha_switch_map') or \
143 not hasattr(class_test, 'voltha_preconfigured') or \
144 not hasattr(class_test, 'VOLTHA_TEARDOWN'):
A R Karthickdd064632017-07-12 13:02:17 -0700145 return
146 voltha_ctrl = getattr(class_test, 'voltha_ctrl')
147 voltha_device = getattr(class_test, 'voltha_device')
148 voltha_switch_map = getattr(class_test, 'voltha_switch_map')
A R Karthick18d0fb62017-09-01 18:49:07 -0700149 voltha_preconfigured = getattr(class_test, 'voltha_preconfigured')
150 voltha_teardown = getattr(class_test, 'VOLTHA_TEARDOWN')
151 if voltha_preconfigured is False and voltha_teardown is True:
152 voltha_teardown(voltha_ctrl, voltha_device, voltha_switch_map)
A R Karthickdd064632017-07-12 13:02:17 -0700153
A R Karthick19771192017-04-25 14:57:05 -0700154def running_on_ciab():
155 if running_on_pod() is False:
156 return False
157 head_node = os.getenv('HEAD_NODE', 'prod')
158 HEAD_NODE = head_node + '.cord.lab' if len(head_node.split('.')) == 1 else head_node
159 agent = SSHTestAgent(host = HEAD_NODE, user = 'ubuntu', password = 'ubuntu')
160 #see if user ubuntu works
161 st, output = agent.run_cmd('sudo virsh list')
162 if st is False and output is not None:
163 #we are on real pod
164 return False
165
166 #try vagrant
167 agent = SSHTestAgent(host = HEAD_NODE, user = 'vagrant', password = 'vagrant')
168 st, output = agent.run_cmd('sudo virsh list')
169 if st is True and output is not None:
170 return True
171
172 return False