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', |
A R Karthick | 5344271 | 2017-07-27 12:23:30 -0700 | [diff] [blame] | 83 | uplink_vlan_map = { 'of:0000000000000001' : '222' }, |
| 84 | uplink_vlan_start = 333 |
A R Karthick | dd06463 | 2017-07-12 13:02:17 -0700 | [diff] [blame] | 85 | ) |
| 86 | voltha_enabled = bool(int(os.getenv('VOLTHA_ENABLED', 0))) |
| 87 | voltha_configure = True |
| 88 | if hasattr(class_test, 'VOLTHA_AUTO_CONFIGURE'): |
| 89 | voltha_configure = getattr(class_test, 'VOLTHA_AUTO_CONFIGURE') |
| 90 | |
| 91 | if voltha_enabled and voltha_configure: |
| 92 | for k,v in voltha_attrs.iteritems(): |
| 93 | voltha_attr = 'VOLTHA_{}'.format(k.upper()) |
| 94 | if hasattr(class_test, voltha_attr): |
| 95 | v = getattr(class_test, voltha_attr) |
| 96 | voltha_attrs[k] = v |
| 97 | else: |
| 98 | setattr(class_test, voltha_attr, v) |
| 99 | ret = voltha_setup(**voltha_attrs) |
| 100 | if ret is not None: |
| 101 | #setup the stage to drop voltha on the way out |
| 102 | setattr(class_test, 'voltha_ctrl', ret[0]) |
| 103 | setattr(class_test, 'voltha_device', ret[1]) |
| 104 | setattr(class_test, 'voltha_switch_map', ret[2]) |
| 105 | |
| 106 | def teardown_module(module): |
| 107 | class_test = get_test_class(module) |
| 108 | if class_test is None: |
| 109 | return |
| 110 | if not hasattr(class_test, 'voltha_ctrl') or \ |
| 111 | not hasattr(class_test, 'voltha_device') or \ |
| 112 | not hasattr(class_test, 'voltha_switch_map'): |
| 113 | return |
| 114 | voltha_ctrl = getattr(class_test, 'voltha_ctrl') |
| 115 | voltha_device = getattr(class_test, 'voltha_device') |
| 116 | voltha_switch_map = getattr(class_test, 'voltha_switch_map') |
| 117 | voltha_teardown(voltha_ctrl, voltha_device, voltha_switch_map) |
| 118 | |
A R Karthick | 1977119 | 2017-04-25 14:57:05 -0700 | [diff] [blame] | 119 | def running_on_ciab(): |
| 120 | if running_on_pod() is False: |
| 121 | return False |
| 122 | head_node = os.getenv('HEAD_NODE', 'prod') |
| 123 | HEAD_NODE = head_node + '.cord.lab' if len(head_node.split('.')) == 1 else head_node |
| 124 | agent = SSHTestAgent(host = HEAD_NODE, user = 'ubuntu', password = 'ubuntu') |
| 125 | #see if user ubuntu works |
| 126 | st, output = agent.run_cmd('sudo virsh list') |
| 127 | if st is False and output is not None: |
| 128 | #we are on real pod |
| 129 | return False |
| 130 | |
| 131 | #try vagrant |
| 132 | agent = SSHTestAgent(host = HEAD_NODE, user = 'vagrant', password = 'vagrant') |
| 133 | st, output = agent.run_cmd('sudo virsh list') |
| 134 | if st is True and output is not None: |
| 135 | return True |
| 136 | |
| 137 | return False |