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 |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 21 | import time |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 22 | from nose.tools import assert_not_equal |
A.R Karthick | 8f7f1b6 | 2017-04-06 18:25:07 -0700 | [diff] [blame] | 23 | from nose.plugins import Plugin |
| 24 | from CordTestUtils import log_test as log |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 25 | from CordTestUtils import running_on_pod |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 26 | from VolthaCtrl import voltha_setup, voltha_teardown, VolthaService, VolthaCtrl |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 27 | from OnosCtrl import OnosCtrl |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 28 | from SSHTestAgent import SSHTestAgent |
A.R Karthick | 8f7f1b6 | 2017-04-06 18:25:07 -0700 | [diff] [blame] | 29 | log.setLevel('INFO') |
| 30 | |
| 31 | class 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 Karthick | 9a16a11 | 2017-04-07 15:40:05 -0700 | [diff] [blame] | 49 | def stopContext(self, context): |
A.R Karthick | 8f7f1b6 | 2017-04-06 18:25:07 -0700 | [diff] [blame] | 50 | 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 Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 70 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 71 | def get_test_class(module): |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 72 | class_test = None |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 73 | 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 Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 76 | class_test = obj |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 77 | break |
| 78 | else: |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 79 | class_test = obj |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 80 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 81 | return class_test |
| 82 | |
| 83 | def setup_module(module): |
| 84 | class_test = get_test_class(module) |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 85 | assert_not_equal(class_test, None) |
| 86 | module_name = module.__name__.split('.')[-1] |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 87 | 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 Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 93 | setattr(class_test, k, v) |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 94 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 95 | #check for voltha and configure as appropriate |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 96 | voltha_attrs = dict(host = VolthaService.DOCKER_HOST_IP, |
| 97 | ponsim_host = VolthaService.PONSIM_HOST, |
A R Karthick | 32e711a | 2017-08-22 16:27:22 -0700 | [diff] [blame] | 98 | rest_port = VolthaCtrl.REST_PORT, |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 99 | config_fake = False, |
A R Karthick | 9dc6e92 | 2017-07-12 14:40:16 -0700 | [diff] [blame] | 100 | olt_type = 'ponsim_olt', |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 101 | olt_mac = '00:0c:e2:31:12:00', |
A R Karthick | 31a4017 | 2017-08-14 12:06:09 -0700 | [diff] [blame] | 102 | olt_ip = None, |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 103 | uplink_vlan_map = { 'of:0000000000000001' : '222' }, |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 104 | uplink_vlan_start = 333, |
| 105 | teardown = True, |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 106 | ) |
| 107 | voltha_enabled = bool(int(os.getenv('VOLTHA_ENABLED', 0))) |
| 108 | voltha_configure = True |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 109 | |
| 110 | olt_switch_map = {} |
| 111 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 112 | if hasattr(class_test, 'VOLTHA_AUTO_CONFIGURE'): |
| 113 | voltha_configure = getattr(class_test, 'VOLTHA_AUTO_CONFIGURE') |
| 114 | |
A R Karthick | ce96dde | 2017-10-05 16:41:52 -0700 | [diff] [blame] | 115 | tagged_traffic = False |
| 116 | if hasattr(class_test, 'TAGGED_TRAFFIC'): |
| 117 | tagged_traffic = getattr(class_test, 'TAGGED_TRAFFIC') |
| 118 | |
A R Karthick | 168e234 | 2017-08-15 16:13:10 -0700 | [diff] [blame] | 119 | 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 Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 126 | 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 Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 140 | olt_switch_map = ret[2] |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 141 | 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 Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 145 | |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 146 | #load the sadis and aaa config |
A R Karthick | ce96dde | 2017-10-05 16:41:52 -0700 | [diff] [blame] | 147 | OnosCtrl.sadis_load_config(olt_switch_map = olt_switch_map, tagged_traffic = tagged_traffic) |
A R Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 148 | OnosCtrl.aaa_load_config() |
A R Karthick | efcf1ab | 2017-09-08 18:24:16 -0700 | [diff] [blame] | 149 | #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 Karthick | 1555c7c | 2017-09-07 14:59:41 -0700 | [diff] [blame] | 155 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 156 | def 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 Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 162 | 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 Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 165 | 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 Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 169 | voltha_preconfigured = getattr(class_test, 'voltha_preconfigured') |
A R Karthick | 6708f56 | 2017-09-05 12:15:31 -0700 | [diff] [blame] | 170 | voltha_tear = getattr(class_test, 'VOLTHA_TEARDOWN') |
| 171 | if voltha_preconfigured is False and voltha_tear is True: |
A R Karthick | 18d0fb6 | 2017-09-01 18:49:07 -0700 | [diff] [blame] | 172 | voltha_teardown(voltha_ctrl, voltha_device, voltha_switch_map) |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 173 | |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 174 | def 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 |