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 | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 25 | from VolthaCtrl import voltha_setup, voltha_teardown |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 26 | from SSHTestAgent import SSHTestAgent |
A.R Karthick | 8f7f1b6 | 2017-04-06 18:25:07 -0700 | [diff] [blame] | 27 | log.setLevel('INFO') |
| 28 | |
| 29 | class 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 Karthick | 9a16a11 | 2017-04-07 15:40:05 -0700 | [diff] [blame] | 47 | def stopContext(self, context): |
A.R Karthick | 8f7f1b6 | 2017-04-06 18:25:07 -0700 | [diff] [blame] | 48 | 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 Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 68 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 69 | def get_test_class(module): |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 70 | class_test = None |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 71 | 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 Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 74 | class_test = obj |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 75 | break |
| 76 | else: |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 77 | class_test = obj |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 78 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 79 | return class_test |
| 80 | |
| 81 | def setup_module(module): |
| 82 | class_test = get_test_class(module) |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 83 | assert_not_equal(class_test, None) |
| 84 | module_name = module.__name__.split('.')[-1] |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 85 | 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 Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 91 | setattr(class_test, k, v) |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 92 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 93 | #check for voltha and configure as appropriate |
| 94 | voltha_attrs = dict(host='172.17.0.1', |
| 95 | rest_port = 8881, |
| 96 | config_fake = False, |
A R Karthick | 9dc6e92 | 2017-07-12 14:40:16 -0700 | [diff] [blame] | 97 | olt_type = 'ponsim_olt', |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 98 | olt_mac = '00:0c:e2:31:12:00', |
A R Karthick | 31a4017 | 2017-08-14 12:06:09 -0700 | [diff] [blame] | 99 | olt_ip = None, |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 100 | uplink_vlan_map = { 'of:0000000000000001' : '222' }, |
| 101 | uplink_vlan_start = 333 |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 102 | ) |
| 103 | voltha_enabled = bool(int(os.getenv('VOLTHA_ENABLED', 0))) |
| 104 | voltha_configure = True |
| 105 | if hasattr(class_test, 'VOLTHA_AUTO_CONFIGURE'): |
| 106 | voltha_configure = getattr(class_test, 'VOLTHA_AUTO_CONFIGURE') |
| 107 | |
| 108 | if voltha_enabled and voltha_configure: |
| 109 | for k,v in voltha_attrs.iteritems(): |
| 110 | voltha_attr = 'VOLTHA_{}'.format(k.upper()) |
| 111 | if hasattr(class_test, voltha_attr): |
| 112 | v = getattr(class_test, voltha_attr) |
| 113 | voltha_attrs[k] = v |
| 114 | else: |
| 115 | setattr(class_test, voltha_attr, v) |
| 116 | ret = voltha_setup(**voltha_attrs) |
| 117 | if ret is not None: |
| 118 | #setup the stage to drop voltha on the way out |
| 119 | setattr(class_test, 'voltha_ctrl', ret[0]) |
| 120 | setattr(class_test, 'voltha_device', ret[1]) |
| 121 | setattr(class_test, 'voltha_switch_map', ret[2]) |
| 122 | |
| 123 | def teardown_module(module): |
| 124 | class_test = get_test_class(module) |
| 125 | if class_test is None: |
| 126 | return |
| 127 | if not hasattr(class_test, 'voltha_ctrl') or \ |
| 128 | not hasattr(class_test, 'voltha_device') or \ |
| 129 | not hasattr(class_test, 'voltha_switch_map'): |
| 130 | return |
| 131 | voltha_ctrl = getattr(class_test, 'voltha_ctrl') |
| 132 | voltha_device = getattr(class_test, 'voltha_device') |
| 133 | voltha_switch_map = getattr(class_test, 'voltha_switch_map') |
| 134 | voltha_teardown(voltha_ctrl, voltha_device, voltha_switch_map) |
| 135 | |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 136 | def running_on_ciab(): |
| 137 | if running_on_pod() is False: |
| 138 | return False |
| 139 | head_node = os.getenv('HEAD_NODE', 'prod') |
| 140 | HEAD_NODE = head_node + '.cord.lab' if len(head_node.split('.')) == 1 else head_node |
| 141 | agent = SSHTestAgent(host = HEAD_NODE, user = 'ubuntu', password = 'ubuntu') |
| 142 | #see if user ubuntu works |
| 143 | st, output = agent.run_cmd('sudo virsh list') |
| 144 | if st is False and output is not None: |
| 145 | #we are on real pod |
| 146 | return False |
| 147 | |
| 148 | #try vagrant |
| 149 | agent = SSHTestAgent(host = HEAD_NODE, user = 'vagrant', password = 'vagrant') |
| 150 | st, output = agent.run_cmd('sudo virsh list') |
| 151 | if st is True and output is not None: |
| 152 | return True |
| 153 | |
| 154 | return False |