A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 1 | import inspect |
| 2 | import unittest |
| 3 | import json |
| 4 | import os |
| 5 | from nose.tools import assert_not_equal |
A.R Karthick | 8f7f1b6 | 2017-04-06 18:25:07 -0700 | [diff] [blame] | 6 | from nose.plugins import Plugin |
| 7 | from CordTestUtils import log_test as log |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 8 | from CordTestUtils import running_on_pod |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 9 | from VolthaCtrl import voltha_setup, voltha_teardown |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 10 | from SSHTestAgent import SSHTestAgent |
A.R Karthick | 8f7f1b6 | 2017-04-06 18:25:07 -0700 | [diff] [blame] | 11 | log.setLevel('INFO') |
| 12 | |
| 13 | class CordTestConfigRestore(Plugin): |
| 14 | name = 'cordTestConfigRestore' |
| 15 | context = None |
| 16 | restore_methods = ('configRestore', 'config_restore',) |
| 17 | |
| 18 | def options(self, parser, env=os.environ): |
| 19 | super(CordTestConfigRestore, self).options(parser, env = env) |
| 20 | |
| 21 | def configure(self, options, conf): |
| 22 | self.enabled = True |
| 23 | |
| 24 | #just save the test case context on start |
| 25 | def startContext(self, context): |
| 26 | if inspect.isclass(context) and issubclass(context, unittest.TestCase): |
| 27 | if context.__name__.endswith('exchange'): |
| 28 | self.context = context |
| 29 | |
| 30 | #reset the context on exit |
A R Karthick | 9a16a11 | 2017-04-07 15:40:05 -0700 | [diff] [blame] | 31 | def stopContext(self, context): |
A.R Karthick | 8f7f1b6 | 2017-04-06 18:25:07 -0700 | [diff] [blame] | 32 | if inspect.isclass(context) and issubclass(context, unittest.TestCase): |
| 33 | if context.__name__.endswith('exchange'): |
| 34 | self.context = None |
| 35 | |
| 36 | def doFailure(self, test, exception): |
| 37 | if self.context: |
| 38 | log.info('Inside test case failure for test: %s' %self.context.__name__) |
| 39 | for restore_method in self.restore_methods: |
| 40 | if hasattr(self.context, restore_method): |
| 41 | method = getattr(self.context, restore_method) |
| 42 | #check only for class/static methods |
| 43 | if method.__self__ is self.context: |
| 44 | method() |
| 45 | break |
| 46 | |
| 47 | def addError(self, test, exception): |
| 48 | self.doFailure(test, exception) |
| 49 | |
| 50 | def addFailure(self, test, exception): |
| 51 | self.doFailure(test, exception) |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 52 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 53 | def get_test_class(module): |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 54 | class_test = None |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 55 | for name, obj in inspect.getmembers(module): |
| 56 | if inspect.isclass(obj) and issubclass(obj, unittest.TestCase): |
| 57 | if obj.__name__.endswith('exchange'): |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 58 | class_test = obj |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 59 | break |
| 60 | else: |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 61 | class_test = obj |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 62 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 63 | return class_test |
| 64 | |
| 65 | def setup_module(module): |
| 66 | class_test = get_test_class(module) |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 67 | assert_not_equal(class_test, None) |
| 68 | module_name = module.__name__.split('.')[-1] |
A R Karthick | 861da96 | 2017-02-08 16:21:36 -0800 | [diff] [blame] | 69 | cfg = '{}.json'.format(module_name) |
| 70 | module_config = os.path.join(os.path.dirname(module.__file__), cfg) |
| 71 | if os.access(module_config, os.F_OK): |
| 72 | with open(module_config) as f: |
| 73 | json_data = json.load(f) |
| 74 | for k, v in json_data.iteritems(): |
A.R Karthick | 9904482 | 2017-02-09 14:04:20 -0800 | [diff] [blame] | 75 | setattr(class_test, k, v) |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 76 | |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 77 | #check for voltha and configure as appropriate |
| 78 | voltha_attrs = dict(host='172.17.0.1', |
| 79 | rest_port = 8881, |
| 80 | config_fake = False, |
A R Karthick | 9dc6e92 | 2017-07-12 14:40:16 -0700 | [diff] [blame] | 81 | olt_type = 'ponsim_olt', |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 82 | olt_mac = '00:0c:e2:31:12:00', |
| 83 | uplink_vlan_map = { 'of:0000000000000001' : '222' } |
| 84 | ) |
| 85 | voltha_enabled = bool(int(os.getenv('VOLTHA_ENABLED', 0))) |
| 86 | voltha_configure = True |
| 87 | if hasattr(class_test, 'VOLTHA_AUTO_CONFIGURE'): |
| 88 | voltha_configure = getattr(class_test, 'VOLTHA_AUTO_CONFIGURE') |
| 89 | |
| 90 | if voltha_enabled and voltha_configure: |
| 91 | for k,v in voltha_attrs.iteritems(): |
| 92 | voltha_attr = 'VOLTHA_{}'.format(k.upper()) |
| 93 | if hasattr(class_test, voltha_attr): |
| 94 | v = getattr(class_test, voltha_attr) |
| 95 | voltha_attrs[k] = v |
| 96 | else: |
| 97 | setattr(class_test, voltha_attr, v) |
| 98 | ret = voltha_setup(**voltha_attrs) |
| 99 | if ret is not None: |
| 100 | #setup the stage to drop voltha on the way out |
| 101 | setattr(class_test, 'voltha_ctrl', ret[0]) |
| 102 | setattr(class_test, 'voltha_device', ret[1]) |
| 103 | setattr(class_test, 'voltha_switch_map', ret[2]) |
| 104 | |
| 105 | def teardown_module(module): |
| 106 | class_test = get_test_class(module) |
| 107 | if class_test is None: |
| 108 | return |
| 109 | if not hasattr(class_test, 'voltha_ctrl') or \ |
| 110 | not hasattr(class_test, 'voltha_device') or \ |
| 111 | not hasattr(class_test, 'voltha_switch_map'): |
| 112 | return |
| 113 | voltha_ctrl = getattr(class_test, 'voltha_ctrl') |
| 114 | voltha_device = getattr(class_test, 'voltha_device') |
| 115 | voltha_switch_map = getattr(class_test, 'voltha_switch_map') |
| 116 | voltha_teardown(voltha_ctrl, voltha_device, voltha_switch_map) |
| 117 | |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 118 | def running_on_ciab(): |
| 119 | if running_on_pod() is False: |
| 120 | return False |
| 121 | head_node = os.getenv('HEAD_NODE', 'prod') |
| 122 | HEAD_NODE = head_node + '.cord.lab' if len(head_node.split('.')) == 1 else head_node |
| 123 | agent = SSHTestAgent(host = HEAD_NODE, user = 'ubuntu', password = 'ubuntu') |
| 124 | #see if user ubuntu works |
| 125 | st, output = agent.run_cmd('sudo virsh list') |
| 126 | if st is False and output is not None: |
| 127 | #we are on real pod |
| 128 | return False |
| 129 | |
| 130 | #try vagrant |
| 131 | agent = SSHTestAgent(host = HEAD_NODE, user = 'vagrant', password = 'vagrant') |
| 132 | st, output = agent.run_cmd('sudo virsh list') |
| 133 | if st is True and output is not None: |
| 134 | return True |
| 135 | |
| 136 | return False |