blob: e58e4ff3c446d1a3ba95c714df3d7ddc46118151 [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 Karthickbe7768c2017-03-17 11:39:41 -07004from OnosCtrl import OnosCtrl
Thangavelu K Sdcb04332017-01-27 22:57:56 +00005from docker import Client
6from CordContainer import *
7import json
8import requests
A R Karthick9313b762016-11-07 13:14:35 -08009import unittest
A R Karthicke14fc022016-12-08 14:50:29 -080010import os
11import time
A R Karthick9313b762016-11-07 13:14:35 -080012
A R Karthick81ece152017-01-11 16:46:43 -080013def get_controller_names(controllers):
14 controller_names = [ 'cord-onos' if controllers.index(c) == 0 else 'cord-onos-{}'.format(controllers.index(c)+1) for c in controllers ]
15 return controller_names
16
17def get_controller_map(controllers):
18 controller_map = ( ('cord-onos' if controllers.index(c) == 0 else 'cord-onos-{}'.format(controllers.index(c)+1),c) for c in controllers )
19 return dict(controller_map)
20
A R Karthick9313b762016-11-07 13:14:35 -080021class CordLogger(unittest.TestCase):
22
A R Karthicke14fc022016-12-08 14:50:29 -080023 controllers = os.getenv('ONOS_CONTROLLER_IP', '').split(',')
A R Karthick81ece152017-01-11 16:46:43 -080024 controller_names = get_controller_names(controllers)
25 controller_map = get_controller_map(controllers)
A R Karthicke14fc022016-12-08 14:50:29 -080026 cliSessions = {}
27 onosLogLevel = 'INFO'
28 curLogLevel = onosLogLevel
29 testLogLevel = os.getenv('LOG_LEVEL', onosLogLevel)
Thangavelu K Sdcb04332017-01-27 22:57:56 +000030 setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../setup')
A R Karthicke8935c62016-12-08 18:17:17 -080031 archive_dir = os.path.join(setup_dir, 'test_logs')
A R Karthickeac16d72017-01-11 16:56:04 -080032 onos_data_dir = os.path.join(setup_dir, 'cord-onos-data')
A R Karthicke14fc022016-12-08 14:50:29 -080033
34 @classmethod
35 def cliSessionEnter(cls):
36 try:
37 for controller in cls.controllers:
38 if not controller:
39 continue
40 retries = 0
A R Karthick6cc8b812016-12-09 10:24:40 -080041 while retries < 30:
A R Karthicke14fc022016-12-08 14:50:29 -080042 cli = OnosCliDriver(controller = controller, connect = True)
43 if cli.handle:
44 cls.cliSessions[controller] = cli
45 break
46 else:
47 retries += 1
48 time.sleep(2)
49 except:
50 pass
51
52 @classmethod
53 def cliSessionExit(cls):
54 try:
55 for controller, cli in cls.cliSessions.items():
56 if cli:
57 cli.disconnect()
58 except:
59 pass
60
A R Karthick9313b762016-11-07 13:14:35 -080061 def setUp(self):
62 '''Read the log buffer'''
A R Karthicke14fc022016-12-08 14:50:29 -080063 self.logSet()
A R Karthick9313b762016-11-07 13:14:35 -080064 try:
65 onosLog = OnosLog()
66 st, output = onosLog.get_log()
A R Karthick9313b762016-11-07 13:14:35 -080067 except: pass
68
69 def tearDown(self):
70 '''Dump the log buffer for ERRORS/warnings'''
A R Karthicke14fc022016-12-08 14:50:29 -080071 #reset the log level back to default log level after a test
72 self.logSet(level = self.onosLogLevel)
A R Karthick9313b762016-11-07 13:14:35 -080073 try:
74 onosLog = OnosLog()
75 st, output = onosLog.get_log( ('ERROR','WARN') )
A R Karthick9313b762016-11-07 13:14:35 -080076 if st and output:
77 log.info('\nTest %s has errors and warnings\n' %self._testMethodName)
78 log.info('%s' %output)
79 else:
80 log.info('\nTest %s has no errors and warnings in the logs' %self._testMethodName)
81 except: pass
A R Karthicke8935c62016-12-08 18:17:17 -080082 try:
83 self.archive_results(self._testMethodName)
84 except: pass
85
86 @classmethod
A.R Karthick53d92702017-03-13 10:10:38 -070087 def archive_results(cls, testName, controllers = None, iteration = None, archive_partition = False):
A R Karthick81ece152017-01-11 16:46:43 -080088 if not os.path.exists(cls.onos_data_dir):
89 return cls.archive_results_unshared(testName, controllers = controllers, iteration = iteration)
90 if not os.path.exists(cls.archive_dir):
91 os.mkdir(cls.archive_dir)
92 if controllers is None:
93 controllers = cls.controllers
94 controller_map = cls.controller_map
95 else:
96 controller_map = get_controller_map(controllers)
97
98 iteration_str = '' if iteration is None else '_{}'.format(iteration)
A.R Karthick53d92702017-03-13 10:10:38 -070099 if archive_partition is False:
100 archive_target = 'log'
101 tar_options = ''
102 else:
103 archive_target = ''
104 tar_options = '--exclude=cache --exclude=tmp'
105
A R Karthick81ece152017-01-11 16:46:43 -0800106 for c in controller_map.keys():
107 archive_file = os.path.join(cls.archive_dir,
108 'logs_{}_{}{}.tar.gz'.format(controller_map[c], testName, iteration_str))
A.R Karthick53d92702017-03-13 10:10:38 -0700109 archive_path = os.path.join(cls.setup_dir, '{}-data'.format(c), archive_target)
110 cmd = 'cd {} && tar cvzf {} . {}'.format(archive_path, archive_file, tar_options)
A R Karthick81ece152017-01-11 16:46:43 -0800111 try:
112 os.system(cmd)
113 except: pass
114
115 @classmethod
116 def archive_results_unshared(cls, testName, controllers = None, iteration = None, cache_result = False):
A R Karthicke8935c62016-12-08 18:17:17 -0800117 log_map = {}
118 if controllers is None:
119 controllers = cls.controllers
120 else:
121 if type(controllers) in [ str, unicode ]:
122 controllers = [ controllers ]
123 try:
124 for controller in controllers:
125 onosLog = OnosLog(host = controller)
126 st, output = onosLog.get_log(cache_result = cache_result)
127 log_map[controller] = (st, output)
128 except:
129 return
130
131 if not os.path.exists(cls.archive_dir):
132 os.mkdir(cls.archive_dir)
133 for controller, results in log_map.items():
134 st, output = results
135 if st and output:
136 iteration_str = '' if iteration is None else '_{}'.format(iteration)
137 archive_file = os.path.join(cls.archive_dir,
138 'logs_{}_{}{}'.format(controller, testName, iteration_str))
139 archive_cmd = 'gzip -9 -f {}'.format(archive_file)
140 if os.access(archive_file, os.F_OK):
141 os.unlink(archive_file)
142 with open(archive_file, 'w') as fd:
143 fd.write(output)
144 try:
145 os.system(archive_cmd)
146 except: pass
A R Karthicke14fc022016-12-08 14:50:29 -0800147
148 @classmethod
149 def logSet(cls, level = None, app = 'org.onosproject', controllers = None, forced = False):
150 #explicit override of level is allowed to reset log levels
151 if level is None:
152 level = cls.testLogLevel
153 #if we are already at current/ONOS log level, there is nothing to do
154 if forced is False and level == cls.curLogLevel:
155 return
156 if controllers is None:
157 controllers = cls.controllers
158 else:
159 if type(controllers) in [str, unicode]:
160 controllers = [ controllers ]
161 cls.cliSessionEnter()
162 try:
163 for controller in controllers:
164 if cls.cliSessions.has_key(controller):
165 cls.cliSessions[controller].logSet(level = level, app = app)
166 cls.curLogLevel = level
167 except:
168 pass
169 cls.cliSessionExit()
Thangavelu K Sdcb04332017-01-27 22:57:56 +0000170
171 @classmethod
172 def stat_option(cls, stat = None, serverDetails = None):
173 # each stat option we can do some specific functions
174 if stat is None:
175 stat = cls.statOptionsList
176 if serverDetails is None:
177 serverDetails = cls.serverOptionsList
178 stat_choice = 'COLLECTD'
179 test_name = cls.testHostName
A R Karthickf7a613b2017-02-24 09:36:44 -0800180 test_image = 'cordtest/nose'
Thangavelu K Sdcb04332017-01-27 22:57:56 +0000181 if stat_choice in stat:
182 onos_ctrl = OnosCtrl('org.onosproject.cpman')
183 status, _ = onos_ctrl.activate()
184 if serverDetails is '':
185 ## default Test Container is used to install CollectD
186 pass
187 elif serverDetails in 'NEW':
188 test_image = 'cord-test/exserver'
189 test_name ='cord-collectd'
190 else:
191 pass
192 # cls.connect_server(serverDetails)
193 ## TO-DO for already up and running server, install collectd agent etc...
194 cls.start_collectd_agent_in_server(name = test_name, image = test_image)
195 for controller in cls.controllers:
196 if not controller:
197 continue
198 url_mem_stats = 'http://%s:8181/onos/cpman/controlmetrics/memory_metrics'%(controller)
199 url_cpu_stats = 'http://%s:8181/onos/cpman/controlmetrics/cpu_metrics'%(controller)
200 auth = ('karaf', 'karaf')
201 cls.collectd_agent_metrics(controller, auth, url = url_cpu_stats)
202 cls.collectd_agent_metrics(controller, auth, url = url_mem_stats)
203 return
204
205
206 @classmethod
207 def collectd_agent_metrics(cls,controller=None, auth =None, url = None):
208 '''This function is getting rules from ONOS with json format'''
209 if url:
210 resp = requests.get(url, auth = auth)
211 log.info('Collectd agent has provided metrics via ONOS controller, url = %s \nand status = %s' %(url,resp.json()))
212 return resp
213
214
215 @classmethod
216 def start_collectd_agent_in_server(cls, name = None, image = None):
217 container_cmd_exec = Container(name = name, image = image)
218 tty = False
219 dckr = Client()
220 cmd = 'sudo /etc/init.d/collectd start'
221 i = container_cmd_exec.execute(cmd = cmd, tty= tty, stream = True)
222 return
223
224 @classmethod
225 def disable_onos_apps(cls, stat = None, app = None):
226 stat_choice = 'COLLECTD'
227 if stat is None:
228 stat = cls.statOptionsList
229 if stat_choice in stat:
230 onos_ctrl = OnosCtrl('org.onosproject.cpman')
231 status, _ = onos_ctrl.deactivate()