blob: b026ab0241f1fb30fdcd4954a86c9da1a7faea72 [file] [log] [blame]
A R Karthick9313b762016-11-07 13:14:35 -08001from OnosLog import OnosLog
2from scapy.all import log
A R Karthicke14fc022016-12-08 14:50:29 -08003from onosclidriver import OnosCliDriver
A R Karthick9313b762016-11-07 13:14:35 -08004import unittest
A R Karthicke14fc022016-12-08 14:50:29 -08005import os
6import time
A R Karthick9313b762016-11-07 13:14:35 -08007
8class CordLogger(unittest.TestCase):
9
A R Karthicke14fc022016-12-08 14:50:29 -080010 controllers = os.getenv('ONOS_CONTROLLER_IP', '').split(',')
11 cliSessions = {}
12 onosLogLevel = 'INFO'
13 curLogLevel = onosLogLevel
14 testLogLevel = os.getenv('LOG_LEVEL', onosLogLevel)
A R Karthicke8935c62016-12-08 18:17:17 -080015 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 Karthicke14fc022016-12-08 14:50:29 -080017
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 Karthick6cc8b812016-12-09 10:24:40 -080025 while retries < 30:
A R Karthicke14fc022016-12-08 14:50:29 -080026 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 Karthick9313b762016-11-07 13:14:35 -080045 def setUp(self):
46 '''Read the log buffer'''
A R Karthicke14fc022016-12-08 14:50:29 -080047 self.logSet()
A R Karthick9313b762016-11-07 13:14:35 -080048 try:
49 onosLog = OnosLog()
50 st, output = onosLog.get_log()
A R Karthick9313b762016-11-07 13:14:35 -080051 except: pass
52
53 def tearDown(self):
54 '''Dump the log buffer for ERRORS/warnings'''
A R Karthicke14fc022016-12-08 14:50:29 -080055 #reset the log level back to default log level after a test
56 self.logSet(level = self.onosLogLevel)
A R Karthick9313b762016-11-07 13:14:35 -080057 try:
58 onosLog = OnosLog()
59 st, output = onosLog.get_log( ('ERROR','WARN') )
A R Karthick9313b762016-11-07 13:14:35 -080060 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 Karthicke8935c62016-12-08 18:17:17 -080066 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 Karthicke14fc022016-12-08 14:50:29 -0800102
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()