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 |
Thangavelu K S | dcb0433 | 2017-01-27 22:57:56 +0000 | [diff] [blame] | 7 | from docker import Client |
| 8 | from CordContainer import * |
| 9 | import json |
| 10 | import requests |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 11 | import unittest |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 12 | import os |
| 13 | import time |
A.R Karthick | 2e99c47 | 2017-03-22 19:13:51 -0700 | [diff] [blame] | 14 | import warnings |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 15 | |
A R Karthick | 81ece15 | 2017-01-11 16:46:43 -0800 | [diff] [blame] | 16 | def 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 | |
| 20 | def 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 Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 24 | class CordLogger(unittest.TestCase): |
| 25 | |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 26 | controllers = os.getenv('ONOS_CONTROLLER_IP', '').split(',') |
A R Karthick | 81ece15 | 2017-01-11 16:46:43 -0800 | [diff] [blame] | 27 | controller_names = get_controller_names(controllers) |
| 28 | controller_map = get_controller_map(controllers) |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 29 | cliSessions = {} |
| 30 | onosLogLevel = 'INFO' |
| 31 | curLogLevel = onosLogLevel |
| 32 | testLogLevel = os.getenv('LOG_LEVEL', onosLogLevel) |
Thangavelu K S | dcb0433 | 2017-01-27 22:57:56 +0000 | [diff] [blame] | 33 | 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] | 34 | archive_dir = os.path.join(setup_dir, 'test_logs') |
A R Karthick | eac16d7 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 35 | onos_data_dir = os.path.join(setup_dir, 'cord-onos-data') |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 36 | |
A.R Karthick | 2e99c47 | 2017-03-22 19:13:51 -0700 | [diff] [blame] | 37 | def __init__(self, *args, **kwargs): |
| 38 | warnings.simplefilter('ignore') |
| 39 | super(CordLogger, self).__init__(*args, **kwargs) |
| 40 | |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 41 | @classmethod |
| 42 | def cliSessionEnter(cls): |
| 43 | try: |
| 44 | for controller in cls.controllers: |
| 45 | if not controller: |
| 46 | continue |
| 47 | retries = 0 |
A R Karthick | 6cc8b81 | 2016-12-09 10:24:40 -0800 | [diff] [blame] | 48 | while retries < 30: |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 49 | 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 Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 68 | def setUp(self): |
| 69 | '''Read the log buffer''' |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 70 | self.logSet() |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 71 | try: |
| 72 | onosLog = OnosLog() |
| 73 | st, output = onosLog.get_log() |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 74 | except: pass |
| 75 | |
| 76 | def tearDown(self): |
| 77 | '''Dump the log buffer for ERRORS/warnings''' |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 78 | #reset the log level back to default log level after a test |
| 79 | self.logSet(level = self.onosLogLevel) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 80 | try: |
| 81 | onosLog = OnosLog() |
| 82 | st, output = onosLog.get_log( ('ERROR','WARN') ) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 83 | 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 Karthick | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 89 | try: |
| 90 | self.archive_results(self._testMethodName) |
| 91 | except: pass |
| 92 | |
| 93 | @classmethod |
A.R Karthick | 53d9270 | 2017-03-13 10:10:38 -0700 | [diff] [blame] | 94 | 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] | 95 | 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 Karthick | 53d9270 | 2017-03-13 10:10:38 -0700 | [diff] [blame] | 106 | 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 Karthick | 81ece15 | 2017-01-11 16:46:43 -0800 | [diff] [blame] | 113 | 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 Karthick | 53d9270 | 2017-03-13 10:10:38 -0700 | [diff] [blame] | 116 | 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 Karthick | 81ece15 | 2017-01-11 16:46:43 -0800 | [diff] [blame] | 118 | 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 Karthick | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 124 | 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 Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 154 | |
| 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 S | dcb0433 | 2017-01-27 22:57:56 +0000 | [diff] [blame] | 177 | |
| 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 Karthick | f7a613b | 2017-02-24 09:36:44 -0800 | [diff] [blame] | 187 | test_image = 'cordtest/nose' |
Thangavelu K S | dcb0433 | 2017-01-27 22:57:56 +0000 | [diff] [blame] | 188 | 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() |