def do_show_nbar_stats(self, line): """Fetches NBAR classification stats from the platform.\nStats are available both as raw data and as percentage data.""" try: print self.platform.get_nbar_stats() print termstyle.green("*** End of show_nbar_stats output ***") except PlatformResponseMissmatch as inst: print termstyle.magenta(inst)
def do_show_nat_cfg (self, line): """Outputs the loaded nat provided configuration""" try: self.platform.dump_obj_config('nat') print termstyle.green("*** End of nat configuration ***") except UserWarning as inst: print termstyle.magenta(inst)
def do_show_reservation_status (self, line): """Prompts if TRex is currently reserved or not""" if self.trex.is_reserved(): print "TRex is reserved" else: print "TRex is NOT reserved" print termstyle.green("*** End of reservation status prompt ***")
def do_no_nat(self, arg): """Removes NAT configuration from all non-duplicated interfaces""" try: self.platform.config_no_nat() print termstyle.green("NAT configuration removed successfully.") except UserWarning as inst: print termstyle.magenta(inst)
def do_no_static_route(self, line): """Removes IPv4 static route configuration from all non-duplicated interfaces""" try: self.platform.config_no_static_routing() print termstyle.green("IPv4 static routing configuration removed successfully.") except UserWarning as inst: print termstyle.magenta(inst)
def do_show_cpu_util(self, line): """Fetches CPU utilization stats from the platform""" try: print self.platform.get_cpu_util() print termstyle.green("*** End of show_cpu_util output ***") except PlatformResponseMissmatch as inst: print termstyle.magenta(inst)
def do_show_static_route_cfg (self, line): """Outputs the loaded static route configuration""" try: self.platform.dump_obj_config('static_route') print termstyle.green("*** End of static route configuration ***") except UserWarning as inst: print termstyle.magenta(inst)
def __init__ (self, *args, **kwargs): sys.stdout.flush() unittest.TestCase.__init__(self, *args, **kwargs) if CTRexScenario.is_test_list: return # Point test object to scenario global object self.configuration = CTRexScenario.configuration self.benchmark = CTRexScenario.benchmark self.trex = CTRexScenario.trex self.stl_trex = CTRexScenario.stl_trex self.trex_crashed = CTRexScenario.trex_crashed self.modes = CTRexScenario.modes self.GAManager = CTRexScenario.GAManager self.no_daemon = CTRexScenario.no_daemon self.skipping = False self.fail_reasons = [] if not hasattr(self, 'unsupported_modes'): self.unsupported_modes = [] self.is_loopback = True if 'loopback' in self.modes else False self.is_virt_nics = True if 'virt_nics' in self.modes else False self.is_VM = True if 'VM' in self.modes else False if not CTRexScenario.is_init: if self.trex and not self.no_daemon: # stateful CTRexScenario.trex_version = self.trex.get_trex_version() if not self.is_loopback: # initilize the scenario based on received configuration, once per entire testing session CTRexScenario.router = CPlatform(CTRexScenario.router_cfg['silent_mode']) device_cfg = CDeviceCfg() device_cfg.set_platform_config(CTRexScenario.router_cfg['config_dict']) device_cfg.set_tftp_config(CTRexScenario.router_cfg['tftp_config_dict']) CTRexScenario.router.load_platform_data_from_file(device_cfg) CTRexScenario.router.launch_connection(device_cfg) if CTRexScenario.router_cfg['forceImageReload']: running_image = CTRexScenario.router.get_running_image_details()['image'] print('Current router image: %s' % running_image) needed_image = device_cfg.get_image_name() if not CTRexScenario.router.is_image_matches(needed_image): print('Setting router image: %s' % needed_image) CTRexScenario.router.config_tftp_server(device_cfg) CTRexScenario.router.load_platform_image(needed_image) CTRexScenario.router.set_boot_image(needed_image) CTRexScenario.router.reload_platform(device_cfg) CTRexScenario.router.launch_connection(device_cfg) running_image = CTRexScenario.router.get_running_image_details()['image'] # verify image if not CTRexScenario.router.is_image_matches(needed_image): self.fail('Unable to set router image: %s, current image is: %s' % (needed_image, running_image)) else: print('Matches needed image: %s' % needed_image) CTRexScenario.router_image = running_image if self.modes: print(termstyle.green('\t!!!\tRunning with modes: %s, not suitable tests will be skipped.\t!!!' % list(self.modes))) CTRexScenario.is_init = True print(termstyle.green("Done instantiating TRex scenario!\n")) # raise RuntimeError('CTRexScenario class is not initialized!') self.router = CTRexScenario.router
def do_stop_trex (self, line): """Try to stop TRex run (if TRex is currently running)""" print termstyle.green("*** Starting TRex termination ***") try: ret = self.trex.stop_trex() print termstyle.green("*** End of scenario (TRex is not running now) ***") except TRexException as inst: print termstyle.red(inst)
def do_start_and_return (self, line): """Start TRex run and once in 'Running' mode, return to cmd prompt""" print termstyle.green("*** Starting TRex run, wait until in 'Running' state ***") try: ret = self.trex.start_trex(**self.run_params) print termstyle.green("*** End of scenario (TRex is probably still running!) ***") except TRexException as inst: print termstyle.red(inst)
def do_reserve_trex (self, user): """Reserves the usage of TRex to a certain user""" try: if not user: ret = self.trex.reserve_trex() else: ret = self.trex.reserve_trex(user.split(' ')[0]) print termstyle.green("*** TRex reserved successfully ***") except TRexException as inst: print termstyle.red(inst)
def do_reload (self, line): """Reloads the platform""" ans = misc_methods.query_yes_no('This will reload the platform. Are you sure?', default = None) if ans: # user confirmed he wishes to reload the platform self.platform.reload_platform(self.device_cfg) print(termstyle.green("*** Platform reload completed ***")) else: print(termstyle.green("*** Platform reload aborted ***"))
def do_cancel_reservation (self, user): """Cancels a current reservation of TRex to a certain user""" try: if not user: ret = self.trex.cancel_reservation() else: ret = self.trex.cancel_reservation(user.split(' ')[0]) print termstyle.green("*** TRex reservation canceled successfully ***") except TRexException as inst: print termstyle.red(inst)
def do_kill_indiscriminately (self, line): """Force killing of running TRex process (if exists) on the server.""" print termstyle.green("*** Starting TRex termination ***") ret = self.trex.force_kill() if ret: print termstyle.green("*** End of scenario (TRex is not running now) ***") elif ret is None: print termstyle.magenta("*** End of scenario (TRex termination aborted) ***") else: print termstyle.red("*** End of scenario (TRex termination failed) ***")
def do_update_run_params(self, json_str): """Updates provided parameters on TRex running configuration. Provide using JSON string""" if json_str: try: upd_params = self.decoder.decode(json_str) self.run_params.update(upd_params) print termstyle.green("*** End of TRex parameters update ***") except ValueError as inst: print termstyle.magenta("Provided illegal JSON string. Please try again.\n[", inst,"]") else: print termstyle.magenta("JSON configuration string is missing. Please try again.")
def do_check_image_existence(self, arg): """Check if specific image file (usually *.bin) is already stored in platform drive""" if arg: try: res = self.platform.check_image_existence(arg.split(' ')[0]) print res print termstyle.green("*** Check image existence completed ***") except PlatformResponseAmbiguity as inst: print termstyle.magenta(inst) else: print termstyle.magenta("Please provide an image name in order to check for existance.")
def do_switch_cfg (self, cfg_file_path): """Switch the current platform interface configuration with another one""" if cfg_file_path: cfg_yaml_path = os.path.abspath(cfg_file_path) self.device_cfg = CDeviceCfg(cfg_yaml_path) self.platform.load_platform_data_from_file(self.device_cfg) if not self.virtual_mode: self.platform.reload_connection(self.device_cfg) print termstyle.green("Configuration switching completed successfully.") else: print termstyle.magenta("Configuration file is missing. Please try again.")
def do_poll_once (self, line): """Performs a single poll of TRex current data dump (if TRex is running) and prompts and short version of latest result_obj""" print termstyle.green("*** Trying TRex single poll ***") try: last_res = dict() if self.trex.is_running(dump_out = last_res): obj = self.trex.get_result_obj() print obj else: print termstyle.magenta("TRex isn't currently running.") print termstyle.green("*** End of scenario (TRex is posssibly still running!) ***") except TRexException as inst: print termstyle.red(inst)
def do_push_files (self, filepaths): """Pushes a custom file to be stored locally on TRex server.\nPush multiple files by spefiying their path separated by ' ' (space).""" try: filepaths = filepaths.split(' ') print termstyle.green("*** Starting pushing files ({trex_files}) to TRex. ***".format (trex_files = ', '.join(filepaths)) ) ret_val = self.trex.push_files(filepaths) if ret_val: print termstyle.green("*** End of TRex push_files method (success) ***") else: print termstyle.magenta("*** End of TRex push_files method (failed) ***") except IOError as inst: print termstyle.magenta(inst)
def do_run_until_finish (self, sample_rate): """Starts TRex and sample server until run is done.""" print termstyle.green("*** Starting TRex run_until_finish scenario ***") if not sample_rate: # use default sample rate if not passed sample_rate = 5 try: sample_rate = int(sample_rate) ret = self.trex.start_trex(**self.run_params) self.trex.sample_to_run_finish(sample_rate) print termstyle.green("*** End of TRex run ***") except ValueError as inst: print termstyle.magenta("Provided illegal sample rate value. Please try again.\n[", inst,"]") except TRexException as inst: print termstyle.red(inst)
def do_static_route(self, arg): """Apply IPv4 static routing configuration on all interfaces Specify no arguments will apply static routing with following config: 1. clients_start - 16.0.0.1 2. servers_start - 48.0.0.1 3. dual_port_mask - 1.0.0.0 4. client_destination_mask - 255.0.0.0 5. server_destination_mask - 255.0.0.0 """ if arg: stat_route_dict = load_object_config_file(arg) # else: # print termstyle.magenta("Unknown configutaion option requested. use 'help static_route' for further info.") else: stat_route_dict = { 'clients_start': '16.0.0.1', 'servers_start': '48.0.0.1', 'dual_port_mask': '1.0.0.0', 'client_destination_mask': '255.0.0.0', 'server_destination_mask': '255.0.0.0' } stat_route_obj = CStaticRouteConfig(stat_route_dict) self.platform.config_static_routing(stat_route_obj) print( termstyle.green( "IPv4 static routing configuration applied successfully."))
def do_nat(self, arg): """Apply NAT configuration on all non-duplicated interfaces Specify no arguments will apply NAT with following config: 1. clients_net_start - 16.0.0.0 2. client_acl_wildcard_mask - 0.0.0.255 3. dual_port_mask - 1.0.0.0 4. pool_start - 200.0.0.0 5. pool_netmask - 255.255.255.0 """ if arg: nat_dict = load_object_config_file(arg) # else: # print termstyle.magenta("Unknown nat configutaion option requested. use 'help nat' for further info.") else: # print termstyle.magenta("Specific nat configutaion is missing. use 'help nat' for further info.") nat_dict = { 'clients_net_start': '16.0.0.0', 'client_acl_wildcard_mask': '0.0.0.255', 'dual_port_mask': '1.0.0.0', 'pool_start': '200.0.0.0', 'pool_netmask': '255.255.255.0' } nat_obj = CNatConfig(nat_dict) self.platform.config_nat(nat_obj) print(termstyle.green("NAT configuration applied successfully."))
def _summarize(self): """summarize all tests - the number of failures, errors and successes""" self._line(termstyle.black) self._out("%s test%s run in %0.1f seconds" % ( self.total, self._plural(self.total), time.time() - self.start_time)) if self.total > self.success: self._outln(". ") additionals = [] if self.failure > 0: additionals.append(termstyle.red("%s FAILED" % ( self.failure,))) if self.error > 0: additionals.append(termstyle.yellow("%s error%s" % ( self.error, self._plural(self.error) ))) if self.skip > 0: additionals.append(termstyle.blue("%s skipped" % ( self.skip))) self._out(', '.join(additionals)) self._out(termstyle.green(" (%s test%s passed)" % ( self.success, self._plural(self.success) ))) self._outln()
def do_show_static_route_cfg(self, line): """Outputs the loaded static route configuration""" try: self.platform.dump_obj_config('static_route') print(termstyle.green("*** End of static route configuration ***")) except UserWarning as inst: print(termstyle.magenta(inst))
def do_no_nat(self, arg): """Removes NAT configuration from all non-duplicated interfaces""" try: self.platform.config_no_nat() print(termstyle.green("NAT configuration removed successfully.")) except UserWarning as inst: print(termstyle.magenta(inst))
def test_detailed_verbosity(self): results = [ 'Running main() from gtest_main.cc', 'Note: Google Test filter = core.ok', '[==========] Running 1 test from 1 test case.', '[----------] Global test environment set-up.', '[----------] 1 test from core', '[ RUN ] core.ok', '[ OK ] core.ok (0 ms)', '[----------] 1 test from core (0 ms total)', '', '[----------] Global test environment tear-down', '[==========] 1 test from 1 test case ran. (0 ms total)', '[ PASSED ] 1 test.', ] f = io.StringIO() gtest = GTest('/test/test_core.cc', 'test_core', term=Terminal(f, verbosity=1)) for line in results: gtest(sys.stdout, line) import termstyle expected_results = results[:2] for l in results[2:]: if l.startswith('['): expected_results.append('{}{}'.format( termstyle.green(termstyle.bold(l[:13])), l[13:] )) else: expected_results.append(l) assert f.getvalue() == os.linesep.join(expected_results) + os.linesep
def do_no_zbf(self, line): """Removes Zone-Based policy Firewall configuration from all interfaces""" self.platform.config_no_zbf() print( termstyle.green( "Zone-Based policy Firewall configuration removed successfully." ))
def do_show_cpu_util(self, line): """Fetches CPU utilization stats from the platform""" try: print(self.platform.get_cpu_util()) print(termstyle.green("*** End of show_cpu_util output ***")) except PlatformResponseMissmatch as inst: print(termstyle.magenta(inst))
def do_zbf(self, line): """Apply Zone-Based policy Firewall configuration on all interfaces""" self.platform.config_zbf() print( termstyle.green( "Zone-Based policy Firewall configuration applied successfully." ))
def do_show_nbar_stats(self, line): """Fetches NBAR classification stats from the platform.\nStats are available both as raw data and as percentage data.""" try: print(self.platform.get_nbar_stats()) print(termstyle.green("*** End of show_nbar_stats output ***")) except PlatformResponseMissmatch as inst: print(termstyle.magenta(inst))
def printSummary(self, start, stop): # noqa """Summarize all tests - the number of failures, errors and successes.""" self._line(termstyle.black) self._out("%s test%s run in %0.3f seconds" % (self.total, self._plural(self.total), stop - start)) if self.total > self.success: self._outln(". ") additionals = [ {"color": termstyle.red, "count": self.failure, "message": "%s FAILED"}, { "color": termstyle.yellow, "count": self.error, "message": "%s error%s" % ("%s", self._plural(self.error)), }, {"color": termstyle.blue, "count": self.skip, "message": "%s skipped"}, {"color": termstyle.green, "count": self.expected_failure, "message": "%s expected_failures"}, {"color": termstyle.cyan, "count": self.unexpected_success, "message": "%s unexpected_successes"}, ] additionals_to_print = [ additional["color"](additional["message"] % (additional["count"])) for additional in additionals if additional["count"] > 0 ] self._out(", ".join(additionals_to_print)) self._out(termstyle.green(" (%s test%s passed)" % (self.success, self._plural(self.success)))) self._outln()
def do_show_nat_cfg(self, line): """Outputs the loaded nat provided configuration""" try: self.platform.dump_obj_config('nat') print(termstyle.green("*** End of nat configuration ***")) except UserWarning as inst: print(termstyle.magenta(inst))
def do_push_files(self, filepaths): """Pushes a custom file to be stored locally on TRex server.\nPush multiple files by spefiying their path separated by ' ' (space).""" try: filepaths = filepaths.split(' ') print termstyle.green( "*** Starting pushing files ({trex_files}) to TRex. ***". format(trex_files=', '.join(filepaths))) ret_val = self.trex.push_files(filepaths) if ret_val: print termstyle.green( "*** End of TRex push_files method (success) ***") else: print termstyle.magenta( "*** End of TRex push_files method (failed) ***") except IOError as inst: print termstyle.magenta(inst)
def do_run_until_finish(self, sample_rate): """Starts TRex and sample server until run is done.""" print termstyle.green( "*** Starting TRex run_until_finish scenario ***") if not sample_rate: # use default sample rate if not passed sample_rate = 5 try: sample_rate = int(sample_rate) ret = self.trex.start_trex(**self.run_params) self.trex.sample_until_finish(sample_rate) print termstyle.green("*** End of TRex run ***") except ValueError as inst: print termstyle.magenta( "Provided illegal sample rate value. Please try again.\n[", inst, "]") except TRexException as inst: print termstyle.red(inst)
def do_no_static_route(self, line): """Removes IPv4 static route configuration from all non-duplicated interfaces""" try: self.platform.config_no_static_routing() print( termstyle.green( "IPv4 static routing configuration removed successfully.")) except UserWarning as inst: print(termstyle.magenta(inst))
def do_run_until_condition (self, sample_rate): """Starts TRex and sample server until condition is satisfied.""" print termstyle.green("*** Starting TRex run until condition is satisfied scenario ***") def condition (result_obj): return result_obj.get_current_tx_rate()['m_tx_pps'] > 200000 if not sample_rate: # use default sample rate if not passed sample_rate = 5 try: sample_rate = int(sample_rate) ret = self.trex.start_trex(**self.run_params) ret_val = self.trex.sample_until_condition(condition, sample_rate) print ret_val print termstyle.green("*** End of TRex run ***") except ValueError as inst: print termstyle.magenta("Provided illegal sample rate value. Please try again.\n[", inst,"]") except TRexException as inst: print termstyle.red(inst)
def _fmt_message_sqlalchemy_line(self, line, match, color): logger = match.group(1) level = match.group(2) verb = match.group(3) rest = match.group(4) if len(match.groups()) > 3 else '' if verb in ['SELECT']: head = color("%s: %s:" % (logger, level)) self._in_multiline_sql = verb self._in_multiline_sql_color = termstyle.cyan return "%s %s %s" % (head, termstyle.bold(termstyle.cyan(verb)), termstyle.cyan(rest)) elif verb in ['BEGIN', 'SAVEPOINT', 'RELEASE', 'ROLLBACK', 'COMMIT']: head = color("%s: %s:" % (logger, level)) self._in_multiline_sql = verb self._in_multiline_sql_color = termstyle.green return "%s %s %s" % (head, termstyle.bold(termstyle.green(verb)), termstyle.green(rest)) elif verb in ['INSERT', 'UPDATE', 'DELETE', 'TRUNCATE']: head = color("%s: %s:" % (logger, level)) self._in_multiline_sql = verb self._in_multiline_sql_color = termstyle.red return "%s %s %s" % (head, termstyle.bold(termstyle.red(verb)), termstyle.red(rest)) return color(line)
def printErrors(self): # noqa if not self.verbose: self._outln() if self.immediate: self._outln() for x in range(0, 4): self._outln() self._outln(termstyle.green("TEST RESULT OUTPUT:")) for (test_id, flavour, test, coloured_output_lines) in self.test_failures_and_exceptions: self._printError(flavour=flavour, test=test, coloured_output_lines=coloured_output_lines, test_id=test_id)
def start_master_daemon(): if master_daemon.is_running(): raise Exception('Master daemon is already running') proc = multiprocessing.Process(target = start_master_daemon_func) proc.daemon = True proc.start() for i in range(50): if master_daemon.is_running(): print(termstyle.green('Master daemon is started')) os._exit(0) sleep(0.1) fail(termstyle.red('Master daemon failed to run'))
def do_run_and_poll (self, sample_rate): """Starts TRex and sample server manually until run is done.""" print termstyle.green("*** Starting TRex run and manually poll scenario ***") if not sample_rate: # use default sample rate if not passed sample_rate = 5 try: sample_rate = int(sample_rate) ret = self.trex.start_trex(**self.run_params) last_res = dict() while self.trex.is_running(dump_out = last_res): obj = self.trex.get_result_obj() if (self.verbose): print obj # do WHATEVER here time.sleep(sample_rate) print termstyle.green("*** End of TRex run ***") except ValueError as inst: print termstyle.magenta("Provided illegal sample rate value. Please try again.\n[", inst,"]") except TRexException as inst: print termstyle.red(inst)
def do_static_route(self, arg): """Apply IPv4 static routing configuration on all interfaces Specify no arguments will apply static routing with following config: 1. clients_start - 16.0.0.1 2. servers_start - 48.0.0.1 3. dual_port_mask - 1.0.0.0 4. client_destination_mask - 255.0.0.0 5. server_destination_mask - 255.0.0.0 """ if arg: stat_route_dict = load_object_config_file(arg) # else: # print termstyle.magenta("Unknown configutaion option requested. use 'help static_route' for further info.") else: stat_route_dict = { 'clients_start' : '16.0.0.1', 'servers_start' : '48.0.0.1', 'dual_port_mask': '1.0.0.0', 'client_destination_mask' : '255.0.0.0', 'server_destination_mask' : '255.0.0.0' } stat_route_obj = CStaticRouteConfig(stat_route_dict) self.platform.config_static_routing(stat_route_obj) print termstyle.green("IPv4 static routing configuration applied successfully.")
def tearDown(self): if not self.trex.is_idle(): print 'Warning: TRex is not idle at tearDown, trying to stop it.' self.trex.force_kill(confirm = False) if not self.skipping: # print server logs of test run if CTRexScenario.server_logs: try: print termstyle.green('\n>>>>>>>>>>>>>>> Daemon log <<<<<<<<<<<<<<<') daemon_log = self.trex.get_trex_daemon_log() log_size = len(daemon_log) print ''.join(daemon_log[CTRexScenario.daemon_log_lines:]) CTRexScenario.daemon_log_lines = log_size except Exception as e: print "Can't get TRex daemon log:", e try: print termstyle.green('>>>>>>>>>>>>>>>> Trex log <<<<<<<<<<<<<<<<') print ''.join(self.trex.get_trex_log()) except Exception as e: print "Can't get TRex log:", e if len(self.fail_reasons): raise Exception('The test is failed, reasons:\n%s' % '\n'.join(self.fail_reasons))
def do_nat(self, arg): """Apply NAT configuration on all non-duplicated interfaces Specify no arguments will apply NAT with following config: 1. clients_net_start - 16.0.0.0 2. client_acl_wildcard_mask - 0.0.0.255 3. dual_port_mask - 1.0.0.0 4. pool_start - 200.0.0.0 5. pool_netmask - 255.255.255.0 """ if arg: nat_dict = load_object_config_file(arg) # else: # print termstyle.magenta("Unknown nat configutaion option requested. use 'help nat' for further info.") else: # print termstyle.magenta("Specific nat configutaion is missing. use 'help nat' for further info.") nat_dict = { 'clients_net_start' : '16.0.0.0', 'client_acl_wildcard_mask' : '0.0.0.255', 'dual_port_mask' : '1.0.0.0', 'pool_start' : '200.0.0.0', 'pool_netmask' : '255.255.255.0' } nat_obj = CNatConfig(nat_dict) self.platform.config_nat(nat_obj) print termstyle.green("NAT configuration applied successfully.")
def tearDown(self): if not self.stl_trex and not self.astf_trex and not self.bird_trex and not self.trex.is_idle(): print('Warning: TRex is not idle at tearDown, trying to stop it.') self.trex.force_kill(confirm = False) if not self.skipping: # print server logs of test run if self.trex and CTRexScenario.server_logs and not self.no_daemon: try: print(termstyle.green('\n>>>>>>>>>>>>>>> Daemon log <<<<<<<<<<<<<<<')) daemon_log = self.trex.get_trex_daemon_log() log_size = len(daemon_log) print(''.join(daemon_log[CTRexScenario.daemon_log_lines:])) CTRexScenario.daemon_log_lines = log_size except Exception as e: print("Can't get TRex daemon log:", e) try: print(termstyle.green('>>>>>>>>>>>>>>>> Trex log <<<<<<<<<<<<<<<<')) print(''.join(self.trex.get_trex_log())) except Exception as e: print("Can't get TRex log:", e) if len(self.fail_reasons): sys.stdout.flush() raise Exception('Test failed. Reasons:\n%s' % '\n'.join(self.fail_reasons)) sys.stdout.flush()
def do_switch_cfg(self, cfg_file_path): """Switch the current platform interface configuration with another one""" if cfg_file_path: cfg_yaml_path = os.path.abspath(cfg_file_path) self.device_cfg = CDeviceCfg(cfg_yaml_path) self.platform.load_platform_data_from_file(self.device_cfg) if not self.virtual_mode: self.platform.reload_connection(self.device_cfg) print( termstyle.green( "Configuration switching completed successfully.")) else: print( termstyle.magenta( "Configuration file is missing. Please try again."))
def do_check_image_existence(self, arg): """Check if specific image file (usually *.bin) is already stored in platform drive""" if arg: try: res = self.platform.check_image_existence(arg.split(' ')[0]) print(res) print( termstyle.green("*** Check image existence completed ***")) except PlatformResponseAmbiguity as inst: print(termstyle.magenta(inst)) else: print( termstyle.magenta( "Please provide an image name in order to check for existance." ))
def s(*a, **kw): desc = "\t%s %s" % (prefix, ' '.join([ name, termstyle.bold(' '.join(map(repr, a))), ' '.join( ["%s=%r" % (k, termstyle.bold(v)) for k, v in kw.items()]) ])) try: ret = func(*a, **kw) print >> sys.stderr, termstyle.green(desc) return ret except: print >> sys.stderr, termstyle.red(desc) import traceback traceback.print_exc(file=sys.stderr) raise
def printSummary(self, start, stop): # noqa """Summarize all tests - the number of failures, errors and successes.""" self._line(termstyle.black) self._out("%s test%s run in %0.3f seconds" % (self.total, self._plural(self.total), stop - start)) if self.total > self.success: self._outln(". ") additionals = [ { "color": termstyle.red, "count": self.failure, "message": "%s FAILED" }, { "color": termstyle.yellow, "count": self.error, "message": "%s error%s" % ("%s", self._plural(self.error)) }, { "color": termstyle.blue, "count": self.skip, "message": "%s skipped" }, { "color": termstyle.green, "count": self.expected_failure, "message": "%s expected_failures" }, { "color": termstyle.cyan, "count": self.unexpected_success, "message": "%s unexpected_successes" }, ] additionals_to_print = [ additional["color"](additional["message"] % (additional["count"])) for additional in additionals if additional["count"] > 0 ] self._out(', '.join(additionals_to_print)) self._out( termstyle.green(" (%s test%s passed)" % (self.success, self._plural(self.success)))) self._outln()
def test_file_logger(self): """Tests if a logger for a file can be generated with log().""" with tempfile.NamedTemporaryFile(suffix=".log", mode='w+') as handle: filename = handle.name name = os.path.splitext(os.path.split(filename)[-1])[0] logger = utils.log(name, log_path=tempfile.tempdir, stream=io.StringIO()) logger.info(termstyle.green(LINES['spare'][0])) logger.error(LINES['spare'][1]) logger.critical(LINES['spare'][2]) handle.seek(0) file_lines = handle.readlines() line_format = [line.format(name) for line in LINES['long']] for ind, line in enumerate(file_lines): self.assertRegex(line.strip(), line_format[ind])
def test_report_watchstate(self): f = io.StringIO() r = TerminalReporter( watch_path=None, build_path=None, terminal=Terminal(stream=f) ) r.report_watchstate(WatchState(["create"], ["delete"], ["modify"], 1.0)) assert ( f.getvalue() == os.linesep.join( [ termstyle.green("# CREATED create"), termstyle.yellow("# MODIFIED modify"), termstyle.red("# DELETED delete"), "### Scan time: 1.000s", ] ) + os.linesep )
def test_report_all_passed(self): f = io.StringIO() r = TerminalReporter( watch_path=None, build_path=None, terminal=Terminal(stream=f) ) results = { "total_runtime": 2.09, "total_passed": 1, "total_failed": 0, "failures": [], } r.report_results(results) assert f.getvalue() == ( termstyle.bold( termstyle.green( "".ljust(26, "=") + " 1 passed in 2.09 seconds " + "".ljust(26, "=") ) ) + os.linesep )
def printErrors(self): # noqa if not self.verbose: self._outln() if self.immediate: self._outln() for x in range(0, 4): self._outln() self._outln(termstyle.green("TEST RESULT OUTPUT:")) for (test_id, flavour, test, coloured_output_lines) in (self.test_failures_and_exceptions): self._printError(flavour=flavour, test=test, coloured_output_lines=coloured_output_lines, test_id=test_id) # Copied from the parent function. self._outln() for cls in self.errorClasses.keys(): storage, label, isfail = self.errorClasses[cls] if isfail: self.printErrorList(label, storage) # Might get patched into a result with no config if hasattr(self, 'config'): self.config.plugins.report(self.stream)
def _summarize(self): """summarize all tests - the number of failures, errors and successes""" self._line(termstyle.black) self._out("%s test%s run in %0.1f seconds" % (self.total, self._plural( self.total), time.time() - self.start_time)) if self.total > self.success: self._outln(". ") additionals = [] if self.failure > 0: additionals.append( termstyle.red("%s FAILED" % (self.failure, ))) if self.error > 0: additionals.append( termstyle.yellow("%s error%s" % (self.error, self._plural(self.error)))) if self.skip > 0: additionals.append(termstyle.blue("%s skipped" % (self.skip))) self._out(', '.join(additionals)) self._out( termstyle.green(" (%s test%s passed)" % (self.success, self._plural(self.success)))) self._outln()
def main(): parser = argparse.ArgumentParser( prog=APPNAME, formatter_class=argparse.RawDescriptionHelpFormatter, usage=USAGE, description=DESCRIPTION, epilog=EPILOG, ) parser.add_argument('botid', metavar='botid', nargs='?', default=None, help='botid to inspect dumps of') args = parser.parse_args() ctl = intelmqctl.IntelMQContoller() if args.botid is None: filenames = glob.glob(os.path.join(DEFAULT_LOGGING_PATH, '*.dump')) if not len(filenames): print(green('Nothing to recover from, no dump files found!')) exit(0) filenames = [(fname, fname[len(DEFAULT_LOGGING_PATH):-5]) for fname in sorted(filenames)] length = max([len(value[1]) for value in filenames]) print( bold("{c:>3}: {s:{l}} {i}".format(c='id', s='name (bot id)', i='content', l=length))) for count, (fname, shortname) in enumerate(filenames): info = dump_info(fname) print("{c:3}: {s:{l}} {i}".format(c=count, s=shortname, i=info, l=length)) try: botid = input( inverted('Which dump file to process (id or name)?') + ' ') except EOFError: exit(0) else: botid = botid.strip() if botid == 'q' or not botid: exit(0) try: fname, botid = filenames[int(botid)] except ValueError: fname = os.path.join(DEFAULT_LOGGING_PATH, botid) + '.dump' else: botid = args.botid fname = os.path.join(DEFAULT_LOGGING_PATH, botid) + '.dump' if not os.path.isfile(fname): print(bold('Given file does not exist: {}'.format(fname))) exit(1) answer = None while True: info = dump_info(fname) print('Processing {}: {}'.format(bold(botid), info)) if info.startswith(str(red)): available_opts = [item[0] for item in ACTIONS.values() if item[2]] print('Restricted actions.') else: # don't display list after 'show' and 'recover' command if not (answer and isinstance(answer, list) and answer[0] in ['s', 'r']): with open(fname, 'rt') as handle: content = json.load(handle) meta = load_meta(content) available_opts = [item[0] for item in ACTIONS.values()] for count, line in enumerate(meta): print('{:3}: {} {}'.format(count, *line)) # Determine bot status bot_status = ctl.bot_status(botid) if bot_status == 'running': print(red('Attention: This bot is currently running!')) elif bot_status == 'error': print(red('Attention: This bot is not defined!')) try: answer = input(inverted(', '.join(available_opts) + '?') + ' ').split() except EOFError: break else: if not answer: continue if any([answer[0] == char for char in AVAILABLE_IDS]) and len(answer) > 1: ids = [int(item) for item in answer[1].split(',')] else: ids = [] queue_name = None if answer[0] == 'a': # recover all -> recover all by ids answer[0] = 'r' ids = range(len(meta)) if len(answer) > 1: queue_name = answer[1] if answer[0] == 'q': break elif answer[0] == 'e': # Delete entries for entry in ids: del content[meta[entry][0]] save_file(fname, content) elif answer[0] == 'r': if bot_status == 'running': # See https://github.com/certtools/intelmq/issues/574 print(red('Recovery for running bots not possible.')) continue # recover entries default = utils.load_configuration(DEFAULTS_CONF_FILE) runtime = utils.load_configuration(RUNTIME_CONF_FILE) params = utils.load_parameters(default, runtime) pipe = pipeline.PipelineFactory.create(params) try: for i, (key, entry) in enumerate([ item for (count, item) in enumerate(content.items()) if count in ids ]): if entry['message']: msg = entry['message'] else: print('No message here, deleting entry.') del content[key] continue if queue_name is None: if len(answer) == 3: queue_name = answer[2] else: queue_name = entry['source_queue'] try: pipe.set_queues(queue_name, 'destination') pipe.connect() pipe.send(msg) except exceptions.PipelineError: print( red('Could not reinject into queue {}: {}' ''.format(queue_name, traceback.format_exc()))) else: del content[key] print(green('Recovered dump {}.'.format(i))) finally: save_file(fname, content) if not content: os.remove(fname) print('Deleted empty file {}'.format(fname)) break elif answer[0] == 'd': # delete dumpfile os.remove(fname) print('Deleted file {}'.format(fname)) break elif answer[0] == 's': # Show entries by id for count, (key, value) in enumerate(content.items()): if count not in ids: continue print('=' * 100, '\nShowing id {} {}\n'.format(count, key), '-' * 50) if isinstance(value['message'], (bytes, str)): value['message'] = json.loads(value['message']) if ('raw' in value['message'] and len(value['message']['raw']) > 1000): value['message']['raw'] = value['message'][ 'raw'][:1000] + '...[truncated]' if type(value['traceback']) is not list: value['traceback'] = value['traceback'].splitlines() pprint.pprint(value)
def setUpClass(cls): cls.GAManager = CTRexScenario.GAManager cls.astf_trex = CTRexScenario.astf_trex cls.benchmark = CTRexScenario.benchmark cls.configuration = CTRexScenario.configuration cls.elk = CTRexScenario.elk cls.modes = CTRexScenario.modes cls.no_daemon = CTRexScenario.no_daemon cls.stl_trex = CTRexScenario.stl_trex cls.bird_trex = CTRexScenario.bird_trex cls.trex = CTRexScenario.trex cls.trex_crashed = CTRexScenario.trex_crashed cls.is_VM = 'VM' in cls.modes cls.is_dummy_ports = 'dummy' in cls.modes cls.is_linux_stack = 'linux_stack' in cls.modes cls.is_bird = 'bird' in cls.modes cls.is_loopback = 'loopback' in cls.modes cls.is_lowend = 'lowend' in cls.modes cls.is_vdev = 'vdev' in cls.modes cls.is_vf_nics = 'vf_nics' in cls.modes cls.is_virt_nics = 'virt_nics' in cls.modes if not CTRexScenario.is_init: if cls.trex and not cls.no_daemon: # stateful CTRexScenario.trex_version = cls.trex.get_trex_version() #update elk const object if cls.elk: timediff = timedelta(hours=2) # workaround to get IL timezone date_str = CTRexScenario.trex_version['Date'].strip() timestamp = datetime.strptime(date_str, '%b %d %Y , %H:%M:%S') - timediff img = CTRexScenario.elk_info['info']['image'] img['sha'] = CTRexScenario.trex_version['Git SHA'] img['build_time'] = timestamp.strftime("%Y/%m/%d %H:%M:%S") img['version'] = CTRexScenario.trex_version['Version'] setup = CTRexScenario.elk_info['info']['setup'] if cls.is_loopback: setup['dut'] = 'loopback' else: setup['dut'] = 'router' if cls.is_VM: setup['baremetal'] = False setup['hypervisor'] = 'ESXi' #TBD else: setup['baremetal'] = True if not cls.is_loopback: # initilize the scenario based on received configuration, once per entire testing session CTRexScenario.router = CPlatform( CTRexScenario.router_cfg['silent_mode']) device_cfg = CDeviceCfg() device_cfg.set_platform_config( CTRexScenario.router_cfg['config_dict']) device_cfg.set_tftp_config( CTRexScenario.router_cfg['tftp_config_dict']) CTRexScenario.router.load_platform_data_from_file(device_cfg) CTRexScenario.router.launch_connection(device_cfg) if CTRexScenario.router_cfg['forceImageReload']: image_d = CTRexScenario.router.get_running_image_details() running_image = image_d['image'] print('Current router image: %s' % running_image) if cls.elk: setup['dut'] = image_d.get('model', 'router') print('Current router model : %s' % setup['dut']) needed_image = device_cfg.get_image_name() if not CTRexScenario.router.is_image_matches(needed_image): print('Setting router image: %s' % needed_image) CTRexScenario.router.config_tftp_server(device_cfg) CTRexScenario.router.load_platform_image(needed_image) CTRexScenario.router.set_boot_image(needed_image) CTRexScenario.router.reload_platform(device_cfg) CTRexScenario.router.launch_connection(device_cfg) running_image = CTRexScenario.router.get_running_image_details( )['image'] # verify image if not CTRexScenario.router.is_image_matches( needed_image): cls.fail( 'Unable to set router image: %s, current image is: %s' % (needed_image, running_image)) else: print('Matches needed image: %s' % needed_image) CTRexScenario.router_image = running_image if cls.modes: print( termstyle.green( '\t!!!\tRunning with modes: %s, not suitable tests will be skipped.\t!!!' % list(cls.modes))) CTRexScenario.is_init = True print(termstyle.green("Done instantiating TRex scenario!\n")) cls.router = CTRexScenario.router
def main(): parser = argparse.ArgumentParser( prog=APPNAME, formatter_class=argparse.RawDescriptionHelpFormatter, usage=USAGE, description=DESCRIPTION, epilog=EPILOG, ) parser.add_argument('botid', metavar='botid', nargs='?', default=None, help='botid to inspect dumps of') args = parser.parse_args() # Try to get log_level from defaults_configuration, else use default try: log_level = utils.load_configuration( DEFAULTS_CONF_FILE)['logging_level'] except Exception: log_level = DEFAULT_LOGGING_LEVEL try: logger = utils.log('intelmqdump', log_level=log_level) except (FileNotFoundError, PermissionError) as exc: logger = utils.log('intelmqdump', log_level=log_level, log_path=False) logger.error('Not logging to file: %s', exc) ctl = intelmqctl.IntelMQController() readline.parse_and_bind("tab: complete") readline.set_completer_delims('') pipeline_config = utils.load_configuration(PIPELINE_CONF_FILE) pipeline_pipes = {} for bot, pipes in pipeline_config.items(): pipeline_pipes[pipes.get('source-queue', '')] = bot if args.botid is None: filenames = glob.glob(os.path.join(DEFAULT_LOGGING_PATH, '*.dump')) if not len(filenames): print(green('Nothing to recover from, no dump files found!')) sys.exit(0) filenames = [(fname, fname[len(DEFAULT_LOGGING_PATH):-5]) for fname in sorted(filenames)] length = max([len(value[1]) for value in filenames]) print( bold("{c:>3}: {s:{length}} {i}".format(c='id', s='name (bot id)', i='content', length=length))) for count, (fname, shortname) in enumerate(filenames): info = dump_info(fname) print("{c:3}: {s:{length}} {i}".format(c=count, s=shortname, i=info, length=length)) try: bot_completer = Completer( possible_values=[f[1] for f in filenames]) readline.set_completer(bot_completer.complete) botid = input( inverted('Which dump file to process (id or name)?') + ' ') except EOFError: sys.exit(0) else: botid = botid.strip() if botid == 'q' or not botid: exit(0) try: fname, botid = filenames[int(botid)] except ValueError: fname = os.path.join(DEFAULT_LOGGING_PATH, botid) + '.dump' else: botid = args.botid fname = os.path.join(DEFAULT_LOGGING_PATH, botid) + '.dump' if not os.path.isfile(fname): print(bold('Given file does not exist: {}'.format(fname))) exit(1) answer = None delete_file = False while True: with open(fname, 'r+') as handle: try: fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB) except BlockingIOError: print(red('Dump file is currently locked. Stopping.')) break info = dump_info(fname, file_descriptor=handle) handle.seek(0) available_answers = ACTIONS.keys() print('Processing {}: {}'.format(bold(botid), info)) if info.startswith(str(red)): available_opts = [ item[0] for item in ACTIONS.values() if item[2] ] available_answers = [k for k, v in ACTIONS.items() if v[2]] print('Restricted actions.') else: # don't display list after 'show', 'recover' & edit commands if not (answer and isinstance(answer, list) and answer[0] in ['s', 'r', 'v']): content = json.load(handle) handle.seek(0) content = OrderedDict( sorted(content.items(), key=lambda t: t[0])) # sort by key here, #1280 meta = load_meta(content) available_opts = [item[0] for item in ACTIONS.values()] for count, line in enumerate(meta): print('{:3}: {} {}'.format(count, *line)) # Determine bot status try: bot_status = ctl.bot_status(botid) if bot_status[1] == 'running': print( red('This bot is currently running, the dump file is now locked and ' 'the bot can\'t write it.')) except KeyError: bot_status = 'error' print(red('Attention: This bot is not defined!')) available_opts = [ item[0] for item in ACTIONS.values() if item[2] ] available_answers = [k for k, v in ACTIONS.items() if v[2]] print('Restricted actions.') try: possible_answers = list(available_answers) for id_action in ['r', 'a']: if id_action in possible_answers: possible_answers[possible_answers.index( id_action)] = id_action + ' ' action_completer = Completer(possible_answers, queues=pipeline_pipes.keys()) readline.set_completer(action_completer.complete) answer = input( inverted(', '.join(available_opts) + '?') + ' ').split() except EOFError: break else: if not answer: continue if len(answer) == 0 or answer[0] not in available_answers: print('Action not allowed.') continue if any([answer[0] == char for char in AVAILABLE_IDS]) and len(answer) > 1: ids = [int(item) for item in answer[1].split(',')] else: ids = [] queue_name = None if answer[0] == 'a': # recover all -> recover all by ids answer[0] = 'r' ids = range(len(meta)) if len(answer) > 1: queue_name = answer[1] if answer[0] == 'q': break elif answer[0] == 'e': # Delete entries for entry in ids: del content[meta[entry][0]] save_file(handle, content) elif answer[0] == 'r': # recover entries default = utils.load_configuration(DEFAULTS_CONF_FILE) runtime = utils.load_configuration(RUNTIME_CONF_FILE) params = utils.load_parameters(default, runtime) pipe = pipeline.PipelineFactory.create(params, logger) try: for i, (key, entry) in enumerate([ item for (count, item) in enumerate(content.items()) if count in ids ]): if entry['message']: msg = copy.copy( entry['message'] ) # otherwise the message field gets converted if isinstance(msg, dict): msg = json.dumps(msg) else: print('No message here, deleting entry.') del content[key] continue if queue_name is None: if len(answer) == 3: queue_name = answer[2] else: queue_name = entry['source_queue'] if queue_name in pipeline_pipes: if runtime[pipeline_pipes[queue_name]][ 'group'] == 'Parser' and json.loads( msg)['__type'] == 'Event': print( 'Event converted to Report automatically.') msg = message.Report( message.MessageFactory.unserialize( msg)).serialize() try: pipe.set_queues(queue_name, 'destination') pipe.connect() pipe.send(msg) except exceptions.PipelineError: print( red('Could not reinject into queue {}: {}' ''.format(queue_name, traceback.format_exc()))) else: del content[key] print(green('Recovered dump {}.'.format(i))) finally: save_file(handle, content) if not content: delete_file = True print('Deleting empty file {}'.format(fname)) break elif answer[0] == 'd': # delete dumpfile delete_file = True print('Deleting empty file {}'.format(fname)) break elif answer[0] == 's': # Show entries by id for count, (key, orig_value) in enumerate(content.items()): value = copy.copy( orig_value) # otherwise the raw field gets truncated if count not in ids: continue print('=' * 100, '\nShowing id {} {}\n'.format(count, key), '-' * 50) if value.get('message_type') == 'base64': if len(value['message']) > 1000: value['message'] = value[ 'message'][:1000] + '...[truncated]' else: if isinstance(value['message'], (bytes, str)): value['message'] = json.loads(value['message']) if ('raw' in value['message'] and len(value['message']['raw']) > 1000): value['message']['raw'] = value['message'][ 'raw'][:1000] + '...[truncated]' if type(value['traceback']) is not list: value['traceback'] = value['traceback'].splitlines() pprint.pprint(value) elif answer[0] == 'v': # edit given id if not ids: print(red('Edit mode needs an id')) continue for entry in ids: if content[meta[entry][0]].get('message_type') == 'base64': with tempfile.NamedTemporaryFile( mode='w+b', suffix='.txt') as tmphandle: filename = tmphandle.name tmphandle.write( base64.b64decode( content[meta[entry][0]]['message'])) tmphandle.flush() proc = subprocess.call( ['sensible-editor', filename]) if proc != 0: print(red('Calling editor failed.')) else: tmphandle.seek(0) new_content = tmphandle.read() try: new_content = new_content.decode() except UnicodeDecodeError as exc: print( red("Could not write the new message because of the following error:" )) print( red( exceptions.DecodingError( exception=exc))) else: del content[meta[entry][0]]['message_type'] content[meta[entry] [0]]['message'] = new_content save_file(handle, content) else: with tempfile.NamedTemporaryFile( mode='w+t', suffix='.json') as tmphandle: filename = tmphandle.name utils.write_configuration( configuration_filepath=filename, content=json.loads( content[meta[entry][0]]['message']), new=True, backup=False) proc = subprocess.call( ['sensible-editor', filename]) if proc != 0: print(red('Calling editor failed.')) else: tmphandle.seek(0) content[meta[entry] [0]]['message'] = tmphandle.read() save_file(handle, content) if delete_file: os.remove(fname)
# if arg=='--collect-only': # this is a user trying simply to view the available tests. removing xunit param from nose args # nose_argv[5:7] = [] try: result = nose.run(argv=nose_argv, addplugins=[RedNose()]) if (result == True): print termstyle.green(""" ..::''''::.. .;'' ``;. :: :: :: :: :: :: :: :: :: :: :: :: :: .:' :: :: `:. :: :: : : :: :: `:. .:' :: `;..``::::''..;' ``::,,,,::'' ___ ___ __________ / _ \/ _ | / __/ __/ / / ___/ __ |_\ \_\ \/_/ /_/ /_/ |_/___/___(_) """) sys.exit(0) else: sys.exit(-1) finally: pass
def green(self, text): self._restoreColor() return termstyle.green(text)