A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 1 | from OnosLog import OnosLog |
A.R Karthick | 2e99c47 | 2017-03-22 19:13:51 -0700 | [diff] [blame] | 2 | import logging |
| 3 | logging.getLogger('scapy.runtime').setLevel(logging.ERROR) |
A R Karthick | 76a497a | 2017-04-12 10:59:39 -0700 | [diff] [blame] | 4 | from CordTestUtils import log_test as log |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 5 | from onosclidriver import OnosCliDriver |
A.R Karthick | be7768c | 2017-03-17 11:39:41 -0700 | [diff] [blame] | 6 | from OnosCtrl import OnosCtrl |
A R Karthick | 6f2ac6f | 2017-07-26 12:55:24 -0700 | [diff] [blame] | 7 | try: |
| 8 | from docker import APIClient as Client |
| 9 | except: |
| 10 | from docker import Client |
Thangavelu K S | dcb0433 | 2017-01-27 22:57:56 +0000 | [diff] [blame] | 11 | from CordContainer import * |
| 12 | import json |
| 13 | import requests |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 14 | import unittest |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 15 | import os |
| 16 | import time |
A.R Karthick | 2e99c47 | 2017-03-22 19:13:51 -0700 | [diff] [blame] | 17 | import warnings |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 18 | |
A R Karthick | 81ece15 | 2017-01-11 16:46:43 -0800 | [diff] [blame] | 19 | def 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 | |
| 23 | def 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 Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 27 | class CordLogger(unittest.TestCase): |
| 28 | |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 29 | controllers = os.getenv('ONOS_CONTROLLER_IP', '').split(',') |
A R Karthick | 81ece15 | 2017-01-11 16:46:43 -0800 | [diff] [blame] | 30 | controller_names = get_controller_names(controllers) |
| 31 | controller_map = get_controller_map(controllers) |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 32 | cliSessions = {} |
| 33 | onosLogLevel = 'INFO' |
| 34 | curLogLevel = onosLogLevel |
| 35 | testLogLevel = os.getenv('LOG_LEVEL', onosLogLevel) |
Thangavelu K S | dcb0433 | 2017-01-27 22:57:56 +0000 | [diff] [blame] | 36 | setup_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../setup') |
A R Karthick | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 37 | archive_dir = os.path.join(setup_dir, 'test_logs') |
A R Karthick | eac16d7 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 38 | onos_data_dir = os.path.join(setup_dir, 'cord-onos-data') |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 39 | |
A.R Karthick | 2e99c47 | 2017-03-22 19:13:51 -0700 | [diff] [blame] | 40 | def __init__(self, *args, **kwargs): |
| 41 | warnings.simplefilter('ignore') |
| 42 | super(CordLogger, self).__init__(*args, **kwargs) |
| 43 | |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 44 | @classmethod |
| 45 | def cliSessionEnter(cls): |
| 46 | try: |
| 47 | for controller in cls.controllers: |
| 48 | if not controller: |
| 49 | continue |
| 50 | retries = 0 |
A R Karthick | 6cc8b81 | 2016-12-09 10:24:40 -0800 | [diff] [blame] | 51 | while retries < 30: |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 52 | 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 Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 71 | def setUp(self): |
| 72 | '''Read the log buffer''' |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 73 | self.logSet() |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 74 | try: |
| 75 | onosLog = OnosLog() |
| 76 | st, output = onosLog.get_log() |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 77 | except: pass |
| 78 | |
| 79 | def tearDown(self): |
| 80 | '''Dump the log buffer for ERRORS/warnings''' |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 81 | #reset the log level back to default log level after a test |
| 82 | self.logSet(level = self.onosLogLevel) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 83 | try: |
| 84 | onosLog = OnosLog() |
| 85 | st, output = onosLog.get_log( ('ERROR','WARN') ) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 86 | 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 Karthick | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 92 | try: |
| 93 | self.archive_results(self._testMethodName) |
| 94 | except: pass |
| 95 | |
| 96 | @classmethod |
A.R Karthick | 53d9270 | 2017-03-13 10:10:38 -0700 | [diff] [blame] | 97 | def archive_results(cls, testName, controllers = None, iteration = None, archive_partition = False): |
A R Karthick | 81ece15 | 2017-01-11 16:46:43 -0800 | [diff] [blame] | 98 | 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 Karthick | 53d9270 | 2017-03-13 10:10:38 -0700 | [diff] [blame] | 109 | 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 Karthick | 81ece15 | 2017-01-11 16:46:43 -0800 | [diff] [blame] | 116 | 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 Karthick | 53d9270 | 2017-03-13 10:10:38 -0700 | [diff] [blame] | 119 | 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 Karthick | 81ece15 | 2017-01-11 16:46:43 -0800 | [diff] [blame] | 121 | 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 Karthick | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 127 | 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 Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 157 | |
| 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 S | dcb0433 | 2017-01-27 22:57:56 +0000 | [diff] [blame] | 180 | |
| 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 Karthick | f7a613b | 2017-02-24 09:36:44 -0800 | [diff] [blame] | 190 | test_image = 'cordtest/nose' |
Thangavelu K S | dcb0433 | 2017-01-27 22:57:56 +0000 | [diff] [blame] | 191 | 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() |