blob: bb9e571bfd86bd2def05a5e30bcffb01604d4d2f [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
A R Karthick81ece152017-01-11 16:46:43 -08008def 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
12def 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 Karthick9313b762016-11-07 13:14:35 -080016class CordLogger(unittest.TestCase):
17
A R Karthicke14fc022016-12-08 14:50:29 -080018 controllers = os.getenv('ONOS_CONTROLLER_IP', '').split(',')
A R Karthick81ece152017-01-11 16:46:43 -080019 controller_names = get_controller_names(controllers)
20 controller_map = get_controller_map(controllers)
A R Karthicke14fc022016-12-08 14:50:29 -080021 cliSessions = {}
22 onosLogLevel = 'INFO'
23 curLogLevel = onosLogLevel
24 testLogLevel = os.getenv('LOG_LEVEL', onosLogLevel)
A R Karthicke8935c62016-12-08 18:17:17 -080025 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 Karthickeac16d72017-01-11 16:56:04 -080027 onos_data_dir = os.path.join(setup_dir, 'cord-onos-data')
A R Karthicke14fc022016-12-08 14:50:29 -080028
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 Karthick6cc8b812016-12-09 10:24:40 -080036 while retries < 30:
A R Karthicke14fc022016-12-08 14:50:29 -080037 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 Karthick9313b762016-11-07 13:14:35 -080056 def setUp(self):
57 '''Read the log buffer'''
A R Karthicke14fc022016-12-08 14:50:29 -080058 self.logSet()
A R Karthick9313b762016-11-07 13:14:35 -080059 try:
60 onosLog = OnosLog()
61 st, output = onosLog.get_log()
A R Karthick9313b762016-11-07 13:14:35 -080062 except: pass
63
64 def tearDown(self):
65 '''Dump the log buffer for ERRORS/warnings'''
A R Karthicke14fc022016-12-08 14:50:29 -080066 #reset the log level back to default log level after a test
67 self.logSet(level = self.onosLogLevel)
A R Karthick9313b762016-11-07 13:14:35 -080068 try:
69 onosLog = OnosLog()
70 st, output = onosLog.get_log( ('ERROR','WARN') )
A R Karthick9313b762016-11-07 13:14:35 -080071 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 Karthicke8935c62016-12-08 18:17:17 -080077 try:
78 self.archive_results(self._testMethodName)
79 except: pass
80
81 @classmethod
A R Karthick81ece152017-01-11 16:46:43 -080082 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 Karthicke8935c62016-12-08 18:17:17 -0800105 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 Karthicke14fc022016-12-08 14:50:29 -0800135
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()