Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 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 Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 17 | import inspect |
| 18 | import unittest |
| 19 | import json |
| 20 | import os |
| 21 | from nose.tools import assert_not_equal |
A.R Karthick | 8f7f1b6 | 2017-04-06 18:25:07 -0700 | [diff] [blame] | 22 | from nose.plugins import Plugin |
| 23 | from CordTestUtils import log_test as log |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 24 | from CordTestUtils import running_on_pod |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 25 | from VolthaCtrl import voltha_setup, voltha_teardown, VolthaService, VolthaCtrl |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 26 | from OnosCtrl import OnosCtrl |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 27 | from SSHTestAgent import SSHTestAgent |
A.R Karthick | 8f7f1b6 | 2017-04-06 18:25:07 -0700 | [diff] [blame] | 28 | log.setLevel('INFO') |
| 29 | |
| 30 | class CordTestConfigRestore(Plugin): |
| 31 | name = 'cordTestConfigRestore' |
| 32 | context = None |
| 33 | restore_methods = ('configRestore', 'config_restore',) |
| 34 | |
| 35 | def options(self, parser, env=os.environ): |
| 36 | super(CordTestConfigRestore, self).options(parser, env = env) |
| 37 | |
| 38 | def configure(self, options, conf): |
| 39 | self.enabled = True |
| 40 | |
| 41 | #just save the test case context on start |
| 42 | def startContext(self, context): |
| 43 | if inspect.isclass(context) and issubclass(context, unittest.TestCase): |
| 44 | if context.__name__.endswith('exchange'): |
| 45 | self.context = context |
| 46 | |
| 47 | #reset the context on exit |
A R Karthick | 9a16a11 | 2017-04-07 15:40:05 -0700 | [diff] [blame] | 48 | def stopContext(self, context): |
A.R Karthick | 8f7f1b6 | 2017-04-06 18:25:07 -0700 | [diff] [blame] | 49 | if inspect.isclass(context) and issubclass(context, unittest.TestCase): |
| 50 | if context.__name__.endswith('exchange'): |
| 51 | self.context = None |
| 52 | |
| 53 | def doFailure(self, test, exception): |
| 54 | if self.context: |
| 55 | log.info('Inside test case failure for test: %s' %self.context.__name__) |
| 56 | for restore_method in self.restore_methods: |
| 57 | if hasattr(self.context, restore_method): |
| 58 | method = getattr(self.context, restore_method) |
| 59 | #check only for class/static methods |
| 60 | if method.__self__ is self.context: |
| 61 | method() |
| 62 | break |
| 63 | |
| 64 | def addError(self, test, exception): |
| 65 | self.doFailure(test, exception) |
| 66 | |
| 67 | def addFailure(self, test, exception): |
| 68 | self.doFailure(test, exception) |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 69 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 70 | def get_test_class(module): |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 71 | class_test = None |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 72 | for name, obj in inspect.getmembers(module): |
| 73 | if inspect.isclass(obj) and issubclass(obj, unittest.TestCase): |
| 74 | if obj.__name__.endswith('exchange'): |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 75 | class_test = obj |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 76 | break |
| 77 | else: |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 78 | class_test = obj |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 79 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 80 | return class_test |
| 81 | |
| 82 | def setup_module(module): |
| 83 | class_test = get_test_class(module) |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 84 | assert_not_equal(class_test, None) |
| 85 | module_name = module.__name__.split('.')[-1] |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 86 | cfg = '{}.json'.format(module_name) |
| 87 | module_config = os.path.join(os.path.dirname(module.__file__), cfg) |
| 88 | if os.access(module_config, os.F_OK): |
| 89 | with open(module_config) as f: |
| 90 | json_data = json.load(f) |
| 91 | for k, v in json_data.iteritems(): |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 92 | setattr(class_test, k, v) |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 93 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 94 | #check for voltha and configure as appropriate |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 95 | voltha_attrs = dict(host = VolthaService.DOCKER_HOST_IP, |
| 96 | ponsim_host = VolthaService.PONSIM_HOST, |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 97 | rest_port = VolthaCtrl.REST_PORT, |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 98 | config_fake = False, |
A R Karthick | 9dc6e92 | 2017-07-12 14:40:16 -0700 | [diff] [blame] | 99 | olt_type = 'ponsim_olt', |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 100 | olt_mac = '00:0c:e2:31:12:00', |
A R Karthick | 31a4017 | 2017-08-14 12:06:09 -0700 | [diff] [blame] | 101 | olt_ip = None, |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 102 | uplink_vlan_map = { 'of:0000000000000001' : '222' }, |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 103 | uplink_vlan_start = 333, |
| 104 | teardown = True, |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 105 | ) |
| 106 | voltha_enabled = bool(int(os.getenv('VOLTHA_ENABLED', 0))) |
| 107 | voltha_configure = True |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 108 | |
| 109 | olt_switch_map = {} |
| 110 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 111 | if hasattr(class_test, 'VOLTHA_AUTO_CONFIGURE'): |
| 112 | voltha_configure = getattr(class_test, 'VOLTHA_AUTO_CONFIGURE') |
| 113 | |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 114 | if hasattr(class_test, 'VOLTHA_HOST'): |
| 115 | #update the voltha host ip based on chameleon IP for rest interface |
| 116 | rest_interface = VolthaService.get_ip('chameleon') |
| 117 | if rest_interface: |
| 118 | log.info('Updating VOLTHA_HOST IP to %s' %rest_interface) |
| 119 | setattr(class_test, 'VOLTHA_HOST', rest_interface) |
| 120 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 121 | if voltha_enabled and voltha_configure: |
| 122 | for k,v in voltha_attrs.iteritems(): |
| 123 | voltha_attr = 'VOLTHA_{}'.format(k.upper()) |
| 124 | if hasattr(class_test, voltha_attr): |
| 125 | v = getattr(class_test, voltha_attr) |
| 126 | voltha_attrs[k] = v |
| 127 | else: |
| 128 | setattr(class_test, voltha_attr, v) |
| 129 | ret = voltha_setup(**voltha_attrs) |
| 130 | if ret is not None: |
| 131 | #setup the stage to drop voltha on the way out |
| 132 | setattr(class_test, 'voltha_ctrl', ret[0]) |
| 133 | setattr(class_test, 'voltha_device', ret[1]) |
| 134 | setattr(class_test, 'voltha_switch_map', ret[2]) |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 135 | olt_switch_map = ret[2] |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 136 | voltha_driver_configured = ret[3] |
| 137 | setattr(class_test, 'voltha_preconfigured', voltha_driver_configured) |
| 138 | if voltha_driver_configured: |
| 139 | setattr(class_test, 'VOLTHA_TEARDOWN', False) |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 140 | |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 141 | #load the sadis and aaa config |
| 142 | OnosCtrl.sadis_load_config(olt_switch_map = olt_switch_map) |
| 143 | OnosCtrl.aaa_load_config() |
| 144 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 145 | def teardown_module(module): |
| 146 | class_test = get_test_class(module) |
| 147 | if class_test is None: |
| 148 | return |
| 149 | if not hasattr(class_test, 'voltha_ctrl') or \ |
| 150 | not hasattr(class_test, 'voltha_device') or \ |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 151 | not hasattr(class_test, 'voltha_switch_map') or \ |
| 152 | not hasattr(class_test, 'voltha_preconfigured') or \ |
| 153 | not hasattr(class_test, 'VOLTHA_TEARDOWN'): |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 154 | return |
| 155 | voltha_ctrl = getattr(class_test, 'voltha_ctrl') |
| 156 | voltha_device = getattr(class_test, 'voltha_device') |
| 157 | voltha_switch_map = getattr(class_test, 'voltha_switch_map') |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 158 | voltha_preconfigured = getattr(class_test, 'voltha_preconfigured') |
A R Karthick | 6708f56 | 2017-09-05 12:15:31 -0700 | [diff] [blame] | 159 | voltha_tear = getattr(class_test, 'VOLTHA_TEARDOWN') |
| 160 | if voltha_preconfigured is False and voltha_tear is True: |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 161 | voltha_teardown(voltha_ctrl, voltha_device, voltha_switch_map) |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 162 | |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 163 | def running_on_ciab(): |
| 164 | if running_on_pod() is False: |
| 165 | return False |
| 166 | head_node = os.getenv('HEAD_NODE', 'prod') |
| 167 | HEAD_NODE = head_node + '.cord.lab' if len(head_node.split('.')) == 1 else head_node |
| 168 | agent = SSHTestAgent(host = HEAD_NODE, user = 'ubuntu', password = 'ubuntu') |
| 169 | #see if user ubuntu works |
| 170 | st, output = agent.run_cmd('sudo virsh list') |
| 171 | if st is False and output is not None: |
| 172 | #we are on real pod |
| 173 | return False |
| 174 | |
| 175 | #try vagrant |
| 176 | agent = SSHTestAgent(host = HEAD_NODE, user = 'vagrant', password = 'vagrant') |
| 177 | st, output = agent.run_cmd('sudo virsh list') |
| 178 | if st is True and output is not None: |
| 179 | return True |
| 180 | |
| 181 | return False |