A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 1 | from OnosLog import OnosLog |
| 2 | from scapy.all import log |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 3 | from onosclidriver import OnosCliDriver |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 4 | import unittest |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 5 | import os |
| 6 | import time |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 7 | |
A R Karthick | 81ece15 | 2017-01-11 16:46:43 -0800 | [diff] [blame] | 8 | def get_controller_names(controllers): |
| 9 | controller_names = [ 'cord-onos' if controllers.index(c) == 0 else 'cord-onos-{}'.format(controllers.index(c)+1) for c in controllers ] |
| 10 | return controller_names |
| 11 | |
| 12 | def get_controller_map(controllers): |
| 13 | controller_map = ( ('cord-onos' if controllers.index(c) == 0 else 'cord-onos-{}'.format(controllers.index(c)+1),c) for c in controllers ) |
| 14 | return dict(controller_map) |
| 15 | |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 16 | class CordLogger(unittest.TestCase): |
| 17 | |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 18 | controllers = os.getenv('ONOS_CONTROLLER_IP', '').split(',') |
A R Karthick | 81ece15 | 2017-01-11 16:46:43 -0800 | [diff] [blame] | 19 | controller_names = get_controller_names(controllers) |
| 20 | controller_map = get_controller_map(controllers) |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 21 | cliSessions = {} |
| 22 | onosLogLevel = 'INFO' |
| 23 | curLogLevel = onosLogLevel |
| 24 | testLogLevel = os.getenv('LOG_LEVEL', onosLogLevel) |
A R Karthick | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 25 | setup_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), '../setup') |
| 26 | archive_dir = os.path.join(setup_dir, 'test_logs') |
A R Karthick | eac16d7 | 2017-01-11 16:56:04 -0800 | [diff] [blame^] | 27 | onos_data_dir = os.path.join(setup_dir, 'cord-onos-data') |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 28 | |
| 29 | @classmethod |
| 30 | def cliSessionEnter(cls): |
| 31 | try: |
| 32 | for controller in cls.controllers: |
| 33 | if not controller: |
| 34 | continue |
| 35 | retries = 0 |
A R Karthick | 6cc8b81 | 2016-12-09 10:24:40 -0800 | [diff] [blame] | 36 | while retries < 30: |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 37 | cli = OnosCliDriver(controller = controller, connect = True) |
| 38 | if cli.handle: |
| 39 | cls.cliSessions[controller] = cli |
| 40 | break |
| 41 | else: |
| 42 | retries += 1 |
| 43 | time.sleep(2) |
| 44 | except: |
| 45 | pass |
| 46 | |
| 47 | @classmethod |
| 48 | def cliSessionExit(cls): |
| 49 | try: |
| 50 | for controller, cli in cls.cliSessions.items(): |
| 51 | if cli: |
| 52 | cli.disconnect() |
| 53 | except: |
| 54 | pass |
| 55 | |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 56 | def setUp(self): |
| 57 | '''Read the log buffer''' |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 58 | self.logSet() |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 59 | try: |
| 60 | onosLog = OnosLog() |
| 61 | st, output = onosLog.get_log() |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 62 | except: pass |
| 63 | |
| 64 | def tearDown(self): |
| 65 | '''Dump the log buffer for ERRORS/warnings''' |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 66 | #reset the log level back to default log level after a test |
| 67 | self.logSet(level = self.onosLogLevel) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 68 | try: |
| 69 | onosLog = OnosLog() |
| 70 | st, output = onosLog.get_log( ('ERROR','WARN') ) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 71 | if st and output: |
| 72 | log.info('\nTest %s has errors and warnings\n' %self._testMethodName) |
| 73 | log.info('%s' %output) |
| 74 | else: |
| 75 | log.info('\nTest %s has no errors and warnings in the logs' %self._testMethodName) |
| 76 | except: pass |
A R Karthick | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 77 | try: |
| 78 | self.archive_results(self._testMethodName) |
| 79 | except: pass |
| 80 | |
| 81 | @classmethod |
A R Karthick | 81ece15 | 2017-01-11 16:46:43 -0800 | [diff] [blame] | 82 | def archive_results(cls, testName, controllers = None, iteration = None): |
| 83 | if not os.path.exists(cls.onos_data_dir): |
| 84 | return cls.archive_results_unshared(testName, controllers = controllers, iteration = iteration) |
| 85 | if not os.path.exists(cls.archive_dir): |
| 86 | os.mkdir(cls.archive_dir) |
| 87 | if controllers is None: |
| 88 | controllers = cls.controllers |
| 89 | controller_map = cls.controller_map |
| 90 | else: |
| 91 | controller_map = get_controller_map(controllers) |
| 92 | |
| 93 | iteration_str = '' if iteration is None else '_{}'.format(iteration) |
| 94 | for c in controller_map.keys(): |
| 95 | archive_file = os.path.join(cls.archive_dir, |
| 96 | 'logs_{}_{}{}.tar.gz'.format(controller_map[c], testName, iteration_str)) |
| 97 | archive_path = os.path.join(cls.setup_dir, '{}-data'.format(c), 'log') |
| 98 | cmd = 'cd {} && tar cvzf {} .'.format(archive_path, archive_file) |
| 99 | try: |
| 100 | os.system(cmd) |
| 101 | except: pass |
| 102 | |
| 103 | @classmethod |
| 104 | def archive_results_unshared(cls, testName, controllers = None, iteration = None, cache_result = False): |
A R Karthick | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 105 | log_map = {} |
| 106 | if controllers is None: |
| 107 | controllers = cls.controllers |
| 108 | else: |
| 109 | if type(controllers) in [ str, unicode ]: |
| 110 | controllers = [ controllers ] |
| 111 | try: |
| 112 | for controller in controllers: |
| 113 | onosLog = OnosLog(host = controller) |
| 114 | st, output = onosLog.get_log(cache_result = cache_result) |
| 115 | log_map[controller] = (st, output) |
| 116 | except: |
| 117 | return |
| 118 | |
| 119 | if not os.path.exists(cls.archive_dir): |
| 120 | os.mkdir(cls.archive_dir) |
| 121 | for controller, results in log_map.items(): |
| 122 | st, output = results |
| 123 | if st and output: |
| 124 | iteration_str = '' if iteration is None else '_{}'.format(iteration) |
| 125 | archive_file = os.path.join(cls.archive_dir, |
| 126 | 'logs_{}_{}{}'.format(controller, testName, iteration_str)) |
| 127 | archive_cmd = 'gzip -9 -f {}'.format(archive_file) |
| 128 | if os.access(archive_file, os.F_OK): |
| 129 | os.unlink(archive_file) |
| 130 | with open(archive_file, 'w') as fd: |
| 131 | fd.write(output) |
| 132 | try: |
| 133 | os.system(archive_cmd) |
| 134 | except: pass |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 135 | |
| 136 | @classmethod |
| 137 | def logSet(cls, level = None, app = 'org.onosproject', controllers = None, forced = False): |
| 138 | #explicit override of level is allowed to reset log levels |
| 139 | if level is None: |
| 140 | level = cls.testLogLevel |
| 141 | #if we are already at current/ONOS log level, there is nothing to do |
| 142 | if forced is False and level == cls.curLogLevel: |
| 143 | return |
| 144 | if controllers is None: |
| 145 | controllers = cls.controllers |
| 146 | else: |
| 147 | if type(controllers) in [str, unicode]: |
| 148 | controllers = [ controllers ] |
| 149 | cls.cliSessionEnter() |
| 150 | try: |
| 151 | for controller in controllers: |
| 152 | if cls.cliSessions.has_key(controller): |
| 153 | cls.cliSessions[controller].logSet(level = level, app = app) |
| 154 | cls.curLogLevel = level |
| 155 | except: |
| 156 | pass |
| 157 | cls.cliSessionExit() |