blob: ae7334ac38bf2f6628e20cf62b876cf60ffa579f [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 Karthick76a497a2017-04-12 10:59:39 -07004from CordTestUtils import log_test as 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
A R Karthick6f2ac6f2017-07-26 12:55:24 -07007try:
8 from docker import APIClient as Client
9except:
10 from docker import Client
Thangavelu K Sdcb04332017-01-27 22:57:56 +000011from CordContainer import *
12import json
13import requests
A R Karthick9313b762016-11-07 13:14:35 -080014import unittest
A R Karthicke14fc022016-12-08 14:50:29 -080015import os
16import time
A.R Karthick2e99c472017-03-22 19:13:51 -070017import warnings
A R Karthick9313b762016-11-07 13:14:35 -080018
A R Karthick81ece152017-01-11 16:46:43 -080019def get_controller_names(controllers):
20 controller_names = [ 'cord-onos' if controllers.index(c) == 0 else 'cord-onos-{}'.format(controllers.index(c)+1) for c in controllers ]
21 return controller_names
22
23def get_controller_map(controllers):
24 controller_map = ( ('cord-onos' if controllers.index(c) == 0 else 'cord-onos-{}'.format(controllers.index(c)+1),c) for c in controllers )
25 return dict(controller_map)
26
A R Karthick9313b762016-11-07 13:14:35 -080027class CordLogger(unittest.TestCase):
28
A R Karthicke14fc022016-12-08 14:50:29 -080029 controllers = os.getenv('ONOS_CONTROLLER_IP', '').split(',')
A R Karthick81ece152017-01-11 16:46:43 -080030 controller_names = get_controller_names(controllers)
31 controller_map = get_controller_map(controllers)
A R Karthicke14fc022016-12-08 14:50:29 -080032 cliSessions = {}
33 onosLogLevel = 'INFO'
34 curLogLevel = onosLogLevel
35 testLogLevel = os.getenv('LOG_LEVEL', onosLogLevel)
Thangavelu K Sdcb04332017-01-27 22:57:56 +000036 setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../setup')
A R Karthicke8935c62016-12-08 18:17:17 -080037 archive_dir = os.path.join(setup_dir, 'test_logs')
A R Karthickeac16d72017-01-11 16:56:04 -080038 onos_data_dir = os.path.join(setup_dir, 'cord-onos-data')
A R Karthicke14fc022016-12-08 14:50:29 -080039
A.R Karthick2e99c472017-03-22 19:13:51 -070040 def __init__(self, *args, **kwargs):
41 warnings.simplefilter('ignore')
42 super(CordLogger, self).__init__(*args, **kwargs)
43
A R Karthicke14fc022016-12-08 14:50:29 -080044 @classmethod
45 def cliSessionEnter(cls):
46 try:
47 for controller in cls.controllers:
48 if not controller:
49 continue
50 retries = 0
A R Karthick6cc8b812016-12-09 10:24:40 -080051 while retries < 30:
A R Karthicke14fc022016-12-08 14:50:29 -080052 cli = OnosCliDriver(controller = controller, connect = True)
53 if cli.handle:
54 cls.cliSessions[controller] = cli
55 break
56 else:
57 retries += 1
58 time.sleep(2)
59 except:
60 pass
61
62 @classmethod
63 def cliSessionExit(cls):
64 try:
65 for controller, cli in cls.cliSessions.items():
66 if cli:
67 cli.disconnect()
68 except:
69 pass
70
A R Karthick9313b762016-11-07 13:14:35 -080071 def setUp(self):
72 '''Read the log buffer'''
A R Karthicke14fc022016-12-08 14:50:29 -080073 self.logSet()
A R Karthick9313b762016-11-07 13:14:35 -080074 try:
75 onosLog = OnosLog()
76 st, output = onosLog.get_log()
A R Karthick9313b762016-11-07 13:14:35 -080077 except: pass
78
79 def tearDown(self):
80 '''Dump the log buffer for ERRORS/warnings'''
A R Karthicke14fc022016-12-08 14:50:29 -080081 #reset the log level back to default log level after a test
82 self.logSet(level = self.onosLogLevel)
A R Karthick9313b762016-11-07 13:14:35 -080083 try:
84 onosLog = OnosLog()
85 st, output = onosLog.get_log( ('ERROR','WARN') )
A R Karthick9313b762016-11-07 13:14:35 -080086 if st and output:
87 log.info('\nTest %s has errors and warnings\n' %self._testMethodName)
88 log.info('%s' %output)
89 else:
90 log.info('\nTest %s has no errors and warnings in the logs' %self._testMethodName)
91 except: pass
A R Karthicke8935c62016-12-08 18:17:17 -080092 try:
93 self.archive_results(self._testMethodName)
94 except: pass
95
96 @classmethod
A.R Karthick53d92702017-03-13 10:10:38 -070097 def archive_results(cls, testName, controllers = None, iteration = None, archive_partition = False):
A R Karthick81ece152017-01-11 16:46:43 -080098 if not os.path.exists(cls.onos_data_dir):
99 return cls.archive_results_unshared(testName, controllers = controllers, iteration = iteration)
100 if not os.path.exists(cls.archive_dir):
101 os.mkdir(cls.archive_dir)
102 if controllers is None:
103 controllers = cls.controllers
104 controller_map = cls.controller_map
105 else:
106 controller_map = get_controller_map(controllers)
107
108 iteration_str = '' if iteration is None else '_{}'.format(iteration)
A.R Karthick53d92702017-03-13 10:10:38 -0700109 if archive_partition is False:
110 archive_target = 'log'
111 tar_options = ''
112 else:
113 archive_target = ''
114 tar_options = '--exclude=cache --exclude=tmp'
115
A R Karthick81ece152017-01-11 16:46:43 -0800116 for c in controller_map.keys():
117 archive_file = os.path.join(cls.archive_dir,
118 'logs_{}_{}{}.tar.gz'.format(controller_map[c], testName, iteration_str))
A.R Karthick53d92702017-03-13 10:10:38 -0700119 archive_path = os.path.join(cls.setup_dir, '{}-data'.format(c), archive_target)
120 cmd = 'cd {} && tar cvzf {} . {}'.format(archive_path, archive_file, tar_options)
A R Karthick81ece152017-01-11 16:46:43 -0800121 try:
122 os.system(cmd)
123 except: pass
124
125 @classmethod
126 def archive_results_unshared(cls, testName, controllers = None, iteration = None, cache_result = False):
A R Karthicke8935c62016-12-08 18:17:17 -0800127 log_map = {}
128 if controllers is None:
129 controllers = cls.controllers
130 else:
131 if type(controllers) in [ str, unicode ]:
132 controllers = [ controllers ]
133 try:
134 for controller in controllers:
135 onosLog = OnosLog(host = controller)
136 st, output = onosLog.get_log(cache_result = cache_result)
137 log_map[controller] = (st, output)
138 except:
139 return
140
141 if not os.path.exists(cls.archive_dir):
142 os.mkdir(cls.archive_dir)
143 for controller, results in log_map.items():
144 st, output = results
145 if st and output:
146 iteration_str = '' if iteration is None else '_{}'.format(iteration)
147 archive_file = os.path.join(cls.archive_dir,
148 'logs_{}_{}{}'.format(controller, testName, iteration_str))
149 archive_cmd = 'gzip -9 -f {}'.format(archive_file)
150 if os.access(archive_file, os.F_OK):
151 os.unlink(archive_file)
152 with open(archive_file, 'w') as fd:
153 fd.write(output)
154 try:
155 os.system(archive_cmd)
156 except: pass
A R Karthicke14fc022016-12-08 14:50:29 -0800157
158 @classmethod
159 def logSet(cls, level = None, app = 'org.onosproject', controllers = None, forced = False):
160 #explicit override of level is allowed to reset log levels
161 if level is None:
162 level = cls.testLogLevel
163 #if we are already at current/ONOS log level, there is nothing to do
164 if forced is False and level == cls.curLogLevel:
165 return
166 if controllers is None:
167 controllers = cls.controllers
168 else:
169 if type(controllers) in [str, unicode]:
170 controllers = [ controllers ]
171 cls.cliSessionEnter()
172 try:
173 for controller in controllers:
174 if cls.cliSessions.has_key(controller):
175 cls.cliSessions[controller].logSet(level = level, app = app)
176 cls.curLogLevel = level
177 except:
178 pass
179 cls.cliSessionExit()
Thangavelu K Sdcb04332017-01-27 22:57:56 +0000180
181 @classmethod
182 def stat_option(cls, stat = None, serverDetails = None):
183 # each stat option we can do some specific functions
184 if stat is None:
185 stat = cls.statOptionsList
186 if serverDetails is None:
187 serverDetails = cls.serverOptionsList
188 stat_choice = 'COLLECTD'
189 test_name = cls.testHostName
A R Karthickf7a613b2017-02-24 09:36:44 -0800190 test_image = 'cordtest/nose'
Thangavelu K Sdcb04332017-01-27 22:57:56 +0000191 if stat_choice in stat:
192 onos_ctrl = OnosCtrl('org.onosproject.cpman')
193 status, _ = onos_ctrl.activate()
194 if serverDetails is '':
195 ## default Test Container is used to install CollectD
196 pass
197 elif serverDetails in 'NEW':
198 test_image = 'cord-test/exserver'
199 test_name ='cord-collectd'
200 else:
201 pass
202 # cls.connect_server(serverDetails)
203 ## TO-DO for already up and running server, install collectd agent etc...
204 cls.start_collectd_agent_in_server(name = test_name, image = test_image)
205 for controller in cls.controllers:
206 if not controller:
207 continue
208 url_mem_stats = 'http://%s:8181/onos/cpman/controlmetrics/memory_metrics'%(controller)
209 url_cpu_stats = 'http://%s:8181/onos/cpman/controlmetrics/cpu_metrics'%(controller)
210 auth = ('karaf', 'karaf')
211 cls.collectd_agent_metrics(controller, auth, url = url_cpu_stats)
212 cls.collectd_agent_metrics(controller, auth, url = url_mem_stats)
213 return
214
215
216 @classmethod
217 def collectd_agent_metrics(cls,controller=None, auth =None, url = None):
218 '''This function is getting rules from ONOS with json format'''
219 if url:
220 resp = requests.get(url, auth = auth)
221 log.info('Collectd agent has provided metrics via ONOS controller, url = %s \nand status = %s' %(url,resp.json()))
222 return resp
223
224
225 @classmethod
226 def start_collectd_agent_in_server(cls, name = None, image = None):
227 container_cmd_exec = Container(name = name, image = image)
228 tty = False
229 dckr = Client()
230 cmd = 'sudo /etc/init.d/collectd start'
231 i = container_cmd_exec.execute(cmd = cmd, tty= tty, stream = True)
232 return
233
234 @classmethod
235 def disable_onos_apps(cls, stat = None, app = None):
236 stat_choice = 'COLLECTD'
237 if stat is None:
238 stat = cls.statOptionsList
239 if stat_choice in stat:
240 onos_ctrl = OnosCtrl('org.onosproject.cpman')
241 status, _ = onos_ctrl.deactivate()