A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 1 | from OnosLog import OnosLog |
| 2 | from scapy.all import log |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 3 | from onosclidriver import OnosCliDriver |
Thangavelu K S | dcb0433 | 2017-01-27 22:57:56 +0000 | [diff] [blame] | 4 | from OnosCtrl import OnosCtrl, get_mac |
| 5 | from docker import Client |
| 6 | from CordContainer import * |
| 7 | import json |
| 8 | import requests |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 9 | import unittest |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 10 | import os |
| 11 | import time |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 12 | |
A R Karthick | 81ece15 | 2017-01-11 16:46:43 -0800 | [diff] [blame] | 13 | def 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 | |
| 17 | def 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 Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 21 | class CordLogger(unittest.TestCase): |
| 22 | |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 23 | controllers = os.getenv('ONOS_CONTROLLER_IP', '').split(',') |
A R Karthick | 81ece15 | 2017-01-11 16:46:43 -0800 | [diff] [blame] | 24 | controller_names = get_controller_names(controllers) |
| 25 | controller_map = get_controller_map(controllers) |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 26 | cliSessions = {} |
| 27 | onosLogLevel = 'INFO' |
| 28 | curLogLevel = onosLogLevel |
| 29 | testLogLevel = os.getenv('LOG_LEVEL', onosLogLevel) |
Thangavelu K S | dcb0433 | 2017-01-27 22:57:56 +0000 | [diff] [blame] | 30 | 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] | 31 | archive_dir = os.path.join(setup_dir, 'test_logs') |
A R Karthick | eac16d7 | 2017-01-11 16:56:04 -0800 | [diff] [blame] | 32 | onos_data_dir = os.path.join(setup_dir, 'cord-onos-data') |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 33 | |
| 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 Karthick | 6cc8b81 | 2016-12-09 10:24:40 -0800 | [diff] [blame] | 41 | while retries < 30: |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 42 | 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 Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 61 | def setUp(self): |
| 62 | '''Read the log buffer''' |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 63 | self.logSet() |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 64 | try: |
| 65 | onosLog = OnosLog() |
| 66 | st, output = onosLog.get_log() |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 67 | except: pass |
| 68 | |
| 69 | def tearDown(self): |
| 70 | '''Dump the log buffer for ERRORS/warnings''' |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 71 | #reset the log level back to default log level after a test |
| 72 | self.logSet(level = self.onosLogLevel) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 73 | try: |
| 74 | onosLog = OnosLog() |
| 75 | st, output = onosLog.get_log( ('ERROR','WARN') ) |
A R Karthick | 9313b76 | 2016-11-07 13:14:35 -0800 | [diff] [blame] | 76 | 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 Karthick | e8935c6 | 2016-12-08 18:17:17 -0800 | [diff] [blame] | 82 | try: |
| 83 | self.archive_results(self._testMethodName) |
| 84 | except: pass |
| 85 | |
| 86 | @classmethod |
A R Karthick | 81ece15 | 2017-01-11 16:46:43 -0800 | [diff] [blame] | 87 | def archive_results(cls, testName, controllers = None, iteration = None): |
| 88 | 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) |
| 99 | for c in controller_map.keys(): |
| 100 | archive_file = os.path.join(cls.archive_dir, |
| 101 | 'logs_{}_{}{}.tar.gz'.format(controller_map[c], testName, iteration_str)) |
| 102 | archive_path = os.path.join(cls.setup_dir, '{}-data'.format(c), 'log') |
| 103 | cmd = 'cd {} && tar cvzf {} .'.format(archive_path, archive_file) |
| 104 | try: |
| 105 | os.system(cmd) |
| 106 | except: pass |
| 107 | |
| 108 | @classmethod |
| 109 | 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] | 110 | log_map = {} |
| 111 | if controllers is None: |
| 112 | controllers = cls.controllers |
| 113 | else: |
| 114 | if type(controllers) in [ str, unicode ]: |
| 115 | controllers = [ controllers ] |
| 116 | try: |
| 117 | for controller in controllers: |
| 118 | onosLog = OnosLog(host = controller) |
| 119 | st, output = onosLog.get_log(cache_result = cache_result) |
| 120 | log_map[controller] = (st, output) |
| 121 | except: |
| 122 | return |
| 123 | |
| 124 | if not os.path.exists(cls.archive_dir): |
| 125 | os.mkdir(cls.archive_dir) |
| 126 | for controller, results in log_map.items(): |
| 127 | st, output = results |
| 128 | if st and output: |
| 129 | iteration_str = '' if iteration is None else '_{}'.format(iteration) |
| 130 | archive_file = os.path.join(cls.archive_dir, |
| 131 | 'logs_{}_{}{}'.format(controller, testName, iteration_str)) |
| 132 | archive_cmd = 'gzip -9 -f {}'.format(archive_file) |
| 133 | if os.access(archive_file, os.F_OK): |
| 134 | os.unlink(archive_file) |
| 135 | with open(archive_file, 'w') as fd: |
| 136 | fd.write(output) |
| 137 | try: |
| 138 | os.system(archive_cmd) |
| 139 | except: pass |
A R Karthick | e14fc02 | 2016-12-08 14:50:29 -0800 | [diff] [blame] | 140 | |
| 141 | @classmethod |
| 142 | def logSet(cls, level = None, app = 'org.onosproject', controllers = None, forced = False): |
| 143 | #explicit override of level is allowed to reset log levels |
| 144 | if level is None: |
| 145 | level = cls.testLogLevel |
| 146 | #if we are already at current/ONOS log level, there is nothing to do |
| 147 | if forced is False and level == cls.curLogLevel: |
| 148 | return |
| 149 | if controllers is None: |
| 150 | controllers = cls.controllers |
| 151 | else: |
| 152 | if type(controllers) in [str, unicode]: |
| 153 | controllers = [ controllers ] |
| 154 | cls.cliSessionEnter() |
| 155 | try: |
| 156 | for controller in controllers: |
| 157 | if cls.cliSessions.has_key(controller): |
| 158 | cls.cliSessions[controller].logSet(level = level, app = app) |
| 159 | cls.curLogLevel = level |
| 160 | except: |
| 161 | pass |
| 162 | cls.cliSessionExit() |
Thangavelu K S | dcb0433 | 2017-01-27 22:57:56 +0000 | [diff] [blame] | 163 | |
| 164 | @classmethod |
| 165 | def stat_option(cls, stat = None, serverDetails = None): |
| 166 | # each stat option we can do some specific functions |
| 167 | if stat is None: |
| 168 | stat = cls.statOptionsList |
| 169 | if serverDetails is None: |
| 170 | serverDetails = cls.serverOptionsList |
| 171 | stat_choice = 'COLLECTD' |
| 172 | test_name = cls.testHostName |
A R Karthick | f7a613b | 2017-02-24 09:36:44 -0800 | [diff] [blame^] | 173 | test_image = 'cordtest/nose' |
Thangavelu K S | dcb0433 | 2017-01-27 22:57:56 +0000 | [diff] [blame] | 174 | if stat_choice in stat: |
| 175 | onos_ctrl = OnosCtrl('org.onosproject.cpman') |
| 176 | status, _ = onos_ctrl.activate() |
| 177 | if serverDetails is '': |
| 178 | ## default Test Container is used to install CollectD |
| 179 | pass |
| 180 | elif serverDetails in 'NEW': |
| 181 | test_image = 'cord-test/exserver' |
| 182 | test_name ='cord-collectd' |
| 183 | else: |
| 184 | pass |
| 185 | # cls.connect_server(serverDetails) |
| 186 | ## TO-DO for already up and running server, install collectd agent etc... |
| 187 | cls.start_collectd_agent_in_server(name = test_name, image = test_image) |
| 188 | for controller in cls.controllers: |
| 189 | if not controller: |
| 190 | continue |
| 191 | url_mem_stats = 'http://%s:8181/onos/cpman/controlmetrics/memory_metrics'%(controller) |
| 192 | url_cpu_stats = 'http://%s:8181/onos/cpman/controlmetrics/cpu_metrics'%(controller) |
| 193 | auth = ('karaf', 'karaf') |
| 194 | cls.collectd_agent_metrics(controller, auth, url = url_cpu_stats) |
| 195 | cls.collectd_agent_metrics(controller, auth, url = url_mem_stats) |
| 196 | return |
| 197 | |
| 198 | |
| 199 | @classmethod |
| 200 | def collectd_agent_metrics(cls,controller=None, auth =None, url = None): |
| 201 | '''This function is getting rules from ONOS with json format''' |
| 202 | if url: |
| 203 | resp = requests.get(url, auth = auth) |
| 204 | log.info('Collectd agent has provided metrics via ONOS controller, url = %s \nand status = %s' %(url,resp.json())) |
| 205 | return resp |
| 206 | |
| 207 | |
| 208 | @classmethod |
| 209 | def start_collectd_agent_in_server(cls, name = None, image = None): |
| 210 | container_cmd_exec = Container(name = name, image = image) |
| 211 | tty = False |
| 212 | dckr = Client() |
| 213 | cmd = 'sudo /etc/init.d/collectd start' |
| 214 | i = container_cmd_exec.execute(cmd = cmd, tty= tty, stream = True) |
| 215 | return |
| 216 | |
| 217 | @classmethod |
| 218 | def disable_onos_apps(cls, stat = None, app = None): |
| 219 | stat_choice = 'COLLECTD' |
| 220 | if stat is None: |
| 221 | stat = cls.statOptionsList |
| 222 | if stat_choice in stat: |
| 223 | onos_ctrl = OnosCtrl('org.onosproject.cpman') |
| 224 | status, _ = onos_ctrl.deactivate() |