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