blob: cbebee526c5a5c7a1b430d4e75e1944eb1d456d5 [file] [log] [blame]
A R Karthick9313b762016-11-07 13:14:35 -08001from OnosLog import OnosLog
A.R Karthick2e99c472017-03-22 19:13:51 -07002import logging
3logging.getLogger('scapy.runtime').setLevel(logging.ERROR)
A R Karthick9313b762016-11-07 13:14:35 -08004from scapy.all import log
A R Karthicke14fc022016-12-08 14:50:29 -08005from onosclidriver import OnosCliDriver
A.R Karthickbe7768c2017-03-17 11:39:41 -07006from OnosCtrl import OnosCtrl
Thangavelu K Sdcb04332017-01-27 22:57:56 +00007from docker import Client
8from CordContainer import *
9import json
10import requests
A R Karthick9313b762016-11-07 13:14:35 -080011import unittest
A R Karthicke14fc022016-12-08 14:50:29 -080012import os
13import time
A.R Karthick2e99c472017-03-22 19:13:51 -070014import warnings
A R Karthick9313b762016-11-07 13:14:35 -080015
A R Karthick81ece152017-01-11 16:46:43 -080016def get_controller_names(controllers):
17 controller_names = [ 'cord-onos' if controllers.index(c) == 0 else 'cord-onos-{}'.format(controllers.index(c)+1) for c in controllers ]
18 return controller_names
19
20def get_controller_map(controllers):
21 controller_map = ( ('cord-onos' if controllers.index(c) == 0 else 'cord-onos-{}'.format(controllers.index(c)+1),c) for c in controllers )
22 return dict(controller_map)
23
A R Karthick9313b762016-11-07 13:14:35 -080024class CordLogger(unittest.TestCase):
25
A R Karthicke14fc022016-12-08 14:50:29 -080026 controllers = os.getenv('ONOS_CONTROLLER_IP', '').split(',')
A R Karthick81ece152017-01-11 16:46:43 -080027 controller_names = get_controller_names(controllers)
28 controller_map = get_controller_map(controllers)
A R Karthicke14fc022016-12-08 14:50:29 -080029 cliSessions = {}
30 onosLogLevel = 'INFO'
31 curLogLevel = onosLogLevel
32 testLogLevel = os.getenv('LOG_LEVEL', onosLogLevel)
Thangavelu K Sdcb04332017-01-27 22:57:56 +000033 setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../setup')
A R Karthicke8935c62016-12-08 18:17:17 -080034 archive_dir = os.path.join(setup_dir, 'test_logs')
A R Karthickeac16d72017-01-11 16:56:04 -080035 onos_data_dir = os.path.join(setup_dir, 'cord-onos-data')
A R Karthicke14fc022016-12-08 14:50:29 -080036
A.R Karthick2e99c472017-03-22 19:13:51 -070037 def __init__(self, *args, **kwargs):
38 warnings.simplefilter('ignore')
39 super(CordLogger, self).__init__(*args, **kwargs)
40
A R Karthicke14fc022016-12-08 14:50:29 -080041 @classmethod
42 def cliSessionEnter(cls):
43 try:
44 for controller in cls.controllers:
45 if not controller:
46 continue
47 retries = 0
A R Karthick6cc8b812016-12-09 10:24:40 -080048 while retries < 30:
A R Karthicke14fc022016-12-08 14:50:29 -080049 cli = OnosCliDriver(controller = controller, connect = True)
50 if cli.handle:
51 cls.cliSessions[controller] = cli
52 break
53 else:
54 retries += 1
55 time.sleep(2)
56 except:
57 pass
58
59 @classmethod
60 def cliSessionExit(cls):
61 try:
62 for controller, cli in cls.cliSessions.items():
63 if cli:
64 cli.disconnect()
65 except:
66 pass
67
A R Karthick9313b762016-11-07 13:14:35 -080068 def setUp(self):
69 '''Read the log buffer'''
A R Karthicke14fc022016-12-08 14:50:29 -080070 self.logSet()
A R Karthick9313b762016-11-07 13:14:35 -080071 try:
72 onosLog = OnosLog()
73 st, output = onosLog.get_log()
A R Karthick9313b762016-11-07 13:14:35 -080074 except: pass
75
76 def tearDown(self):
77 '''Dump the log buffer for ERRORS/warnings'''
A R Karthicke14fc022016-12-08 14:50:29 -080078 #reset the log level back to default log level after a test
79 self.logSet(level = self.onosLogLevel)
A R Karthick9313b762016-11-07 13:14:35 -080080 try:
81 onosLog = OnosLog()
82 st, output = onosLog.get_log( ('ERROR','WARN') )
A R Karthick9313b762016-11-07 13:14:35 -080083 if st and output:
84 log.info('\nTest %s has errors and warnings\n' %self._testMethodName)
85 log.info('%s' %output)
86 else:
87 log.info('\nTest %s has no errors and warnings in the logs' %self._testMethodName)
88 except: pass
A R Karthicke8935c62016-12-08 18:17:17 -080089 try:
90 self.archive_results(self._testMethodName)
91 except: pass
92
93 @classmethod
A.R Karthick53d92702017-03-13 10:10:38 -070094 def archive_results(cls, testName, controllers = None, iteration = None, archive_partition = False):
A R Karthick81ece152017-01-11 16:46:43 -080095 if not os.path.exists(cls.onos_data_dir):
96 return cls.archive_results_unshared(testName, controllers = controllers, iteration = iteration)
97 if not os.path.exists(cls.archive_dir):
98 os.mkdir(cls.archive_dir)
99 if controllers is None:
100 controllers = cls.controllers
101 controller_map = cls.controller_map
102 else:
103 controller_map = get_controller_map(controllers)
104
105 iteration_str = '' if iteration is None else '_{}'.format(iteration)
A.R Karthick53d92702017-03-13 10:10:38 -0700106 if archive_partition is False:
107 archive_target = 'log'
108 tar_options = ''
109 else:
110 archive_target = ''
111 tar_options = '--exclude=cache --exclude=tmp'
112
A R Karthick81ece152017-01-11 16:46:43 -0800113 for c in controller_map.keys():
114 archive_file = os.path.join(cls.archive_dir,
115 'logs_{}_{}{}.tar.gz'.format(controller_map[c], testName, iteration_str))
A.R Karthick53d92702017-03-13 10:10:38 -0700116 archive_path = os.path.join(cls.setup_dir, '{}-data'.format(c), archive_target)
117 cmd = 'cd {} && tar cvzf {} . {}'.format(archive_path, archive_file, tar_options)
A R Karthick81ece152017-01-11 16:46:43 -0800118 try:
119 os.system(cmd)
120 except: pass
121
122 @classmethod
123 def archive_results_unshared(cls, testName, controllers = None, iteration = None, cache_result = False):
A R Karthicke8935c62016-12-08 18:17:17 -0800124 log_map = {}
125 if controllers is None:
126 controllers = cls.controllers
127 else:
128 if type(controllers) in [ str, unicode ]:
129 controllers = [ controllers ]
130 try:
131 for controller in controllers:
132 onosLog = OnosLog(host = controller)
133 st, output = onosLog.get_log(cache_result = cache_result)
134 log_map[controller] = (st, output)
135 except:
136 return
137
138 if not os.path.exists(cls.archive_dir):
139 os.mkdir(cls.archive_dir)
140 for controller, results in log_map.items():
141 st, output = results
142 if st and output:
143 iteration_str = '' if iteration is None else '_{}'.format(iteration)
144 archive_file = os.path.join(cls.archive_dir,
145 'logs_{}_{}{}'.format(controller, testName, iteration_str))
146 archive_cmd = 'gzip -9 -f {}'.format(archive_file)
147 if os.access(archive_file, os.F_OK):
148 os.unlink(archive_file)
149 with open(archive_file, 'w') as fd:
150 fd.write(output)
151 try:
152 os.system(archive_cmd)
153 except: pass
A R Karthicke14fc022016-12-08 14:50:29 -0800154
155 @classmethod
156 def logSet(cls, level = None, app = 'org.onosproject', controllers = None, forced = False):
157 #explicit override of level is allowed to reset log levels
158 if level is None:
159 level = cls.testLogLevel
160 #if we are already at current/ONOS log level, there is nothing to do
161 if forced is False and level == cls.curLogLevel:
162 return
163 if controllers is None:
164 controllers = cls.controllers
165 else:
166 if type(controllers) in [str, unicode]:
167 controllers = [ controllers ]
168 cls.cliSessionEnter()
169 try:
170 for controller in controllers:
171 if cls.cliSessions.has_key(controller):
172 cls.cliSessions[controller].logSet(level = level, app = app)
173 cls.curLogLevel = level
174 except:
175 pass
176 cls.cliSessionExit()
Thangavelu K Sdcb04332017-01-27 22:57:56 +0000177
178 @classmethod
179 def stat_option(cls, stat = None, serverDetails = None):
180 # each stat option we can do some specific functions
181 if stat is None:
182 stat = cls.statOptionsList
183 if serverDetails is None:
184 serverDetails = cls.serverOptionsList
185 stat_choice = 'COLLECTD'
186 test_name = cls.testHostName
A R Karthickf7a613b2017-02-24 09:36:44 -0800187 test_image = 'cordtest/nose'
Thangavelu K Sdcb04332017-01-27 22:57:56 +0000188 if stat_choice in stat:
189 onos_ctrl = OnosCtrl('org.onosproject.cpman')
190 status, _ = onos_ctrl.activate()
191 if serverDetails is '':
192 ## default Test Container is used to install CollectD
193 pass
194 elif serverDetails in 'NEW':
195 test_image = 'cord-test/exserver'
196 test_name ='cord-collectd'
197 else:
198 pass
199 # cls.connect_server(serverDetails)
200 ## TO-DO for already up and running server, install collectd agent etc...
201 cls.start_collectd_agent_in_server(name = test_name, image = test_image)
202 for controller in cls.controllers:
203 if not controller:
204 continue
205 url_mem_stats = 'http://%s:8181/onos/cpman/controlmetrics/memory_metrics'%(controller)
206 url_cpu_stats = 'http://%s:8181/onos/cpman/controlmetrics/cpu_metrics'%(controller)
207 auth = ('karaf', 'karaf')
208 cls.collectd_agent_metrics(controller, auth, url = url_cpu_stats)
209 cls.collectd_agent_metrics(controller, auth, url = url_mem_stats)
210 return
211
212
213 @classmethod
214 def collectd_agent_metrics(cls,controller=None, auth =None, url = None):
215 '''This function is getting rules from ONOS with json format'''
216 if url:
217 resp = requests.get(url, auth = auth)
218 log.info('Collectd agent has provided metrics via ONOS controller, url = %s \nand status = %s' %(url,resp.json()))
219 return resp
220
221
222 @classmethod
223 def start_collectd_agent_in_server(cls, name = None, image = None):
224 container_cmd_exec = Container(name = name, image = image)
225 tty = False
226 dckr = Client()
227 cmd = 'sudo /etc/init.d/collectd start'
228 i = container_cmd_exec.execute(cmd = cmd, tty= tty, stream = True)
229 return
230
231 @classmethod
232 def disable_onos_apps(cls, stat = None, app = None):
233 stat_choice = 'COLLECTD'
234 if stat is None:
235 stat = cls.statOptionsList
236 if stat_choice in stat:
237 onos_ctrl = OnosCtrl('org.onosproject.cpman')
238 status, _ = onos_ctrl.deactivate()