def test_logger_custom_logger(tmpdir, caplog): """Test CustomLogger.""" log_fname = "convert2rhel.log" logger_module.initialize_logger(log_name=log_fname, log_dir=tmpdir) logger = logging.getLogger(__name__) logger.task("Some task") logger.file("Some task write to file") with pytest.raises(SystemExit): logger.critical("Critical error")
def setUp(self): # initialize class variables self.log_dir = logger.LOG_DIR self.log_file = "convert2rhel.log" self.test_msg = "testmsg" # remove the directory to ensure the content is clean if os.path.exists(logger.LOG_DIR): shutil.rmtree(logger.LOG_DIR) # initialize logger logger.initialize_logger(self.log_file)
def test_tools_opts_debug(tmpdir, read_std, is_py2): log_fname = "convert2rhel.log" logger_module.initialize_logger(log_name=log_fname, log_dir=tmpdir) logger = logging.getLogger(__name__) tool_opts.debug = True logger.debug("debug entry 1") stdouterr_out, stdouterr_err = read_std() # TODO should be in stdout, but this only works when running this test # alone (see https://github.com/pytest-dev/pytest/issues/5502) try: assert "debug entry 1" in stdouterr_out except AssertionError: if not is_py2: assert "debug entry 1" in stdouterr_err else: # this workaround is not working for py2 - passing pass tool_opts.debug = False logger.debug("debug entry 2") stdouterr_out, stdouterr_err = read_std() assert "debug entry 2" not in stdouterr_out
def test_logger_handlers(tmpdir, caplog, read_std, is_py2, capsys): """Test if the logger handlers emmits the events to the file and stdout.""" # initializing the logger first log_fname = "convert2rhel.log" tool_opts.debug = True # debug entries > stdout if True logger_module.initialize_logger(log_name=log_fname, log_dir=tmpdir) logger = logging.getLogger(__name__) # emitting some log entries logger.info("Test info") logger.debug("Test debug") # Test if logs were emmited to the file with open(os.path.join(tmpdir, log_fname)) as log_f: assert "Test info" in log_f.readline().rstrip() assert "Test debug" in log_f.readline().rstrip() # Test if logs were emmited to the stdout stdouterr_out, stdouterr_err = read_std() assert "Test info" in stdouterr_out assert "Test debug" in stdouterr_out
def main(): """Perform all steps for the entire conversion process.""" process_phase = ConversionPhase.INIT # initialize logging logger.initialize_logger("convert2rhel.log") # get module level logger (inherits from root logger) loggerinst = logging.getLogger(__name__) try: # handle command line arguments toolopts.CLI() process_phase = ConversionPhase.POST_CLI # the tool will not run if not executed under the root user utils.require_root() # license agreement loggerinst.task("Prepare: End user license agreement") user_to_accept_eula() # gather system information loggerinst.task("Prepare: Gather system information") systeminfo.system_info.resolve_system_info() loggerinst.task("Prepare: Determine RHEL variant") rhelvariant.determine_rhel_variant() # backup system release file before starting conversion process loggerinst.task("Prepare: Backup System") redhatrelease.system_release_file.backup() redhatrelease.yum_conf.backup() # begin conversion process process_phase = ConversionPhase.PRE_PONR_CHANGES pre_ponr_conversion() loggerinst.warning("The tool allows rollback of any action until this" " point.") loggerinst.warning("By continuing all further changes on the system" " will need to be reverted manually by the user," " if necessary.") utils.ask_to_continue() process_phase = ConversionPhase.POST_PONR_CHANGES post_ponr_conversion() # recommend non-interactive command loggerinst.task("Final: Non-interactive mode") toolopts.print_non_interactive_opts() # restart system if required utils.restart_system() except (Exception, SystemExit, KeyboardInterrupt), err: # Catching the three exception types separately due to python 2.4 # (RHEL 5) - 2.7 (RHEL 7) compatibility. utils.log_traceback(toolopts.tool_opts.debug) print("\n") if is_help_msg_exit(process_phase, err): return 0 elif process_phase in (ConversionPhase.INIT, ConversionPhase.POST_CLI): print("No changes were made to the system.") elif process_phase == ConversionPhase.PRE_PONR_CHANGES: rollback_changes() elif process_phase == ConversionPhase.POST_PONR_CHANGES: # After the process of subscription is done and the mass update of # packages is started convert2rhel will not be able to guarantee a # system rollback without user intervation. If a proper rollback # solution is necessary it will need to be future implemented here # or with the use of other backup tools. print("Conversion process interrupted and manual user intervation" " will be necessary.") return 1
def setup_logger(tmpdir): initialize_logger(log_name="convert2rhel", log_dir=tmpdir)
def main(): """Perform all steps for the entire conversion process.""" # handle command line arguments toolopts.CLI() # the tool will not run if not executed under the root user utils.require_root() process_phase = ConversionPhase.INIT # initialize logging logger.initialize_logger("convert2rhel.log") # get module level logger (inherits from root logger) try: process_phase = ConversionPhase.POST_CLI # license agreement loggerinst.task("Prepare: Show Red Hat software EULA") show_eula() # gather system information loggerinst.task("Prepare: Gather system information") systeminfo.system_info.resolve_system_info() # check the system prior the conversion (possible inhibit) loggerinst.task("Prepare: Initial system checks before conversion") checks.perform_pre_checks() # backup system release file before starting conversion process loggerinst.task("Prepare: Backup System") redhatrelease.system_release_file.backup() redhatrelease.yum_conf.backup() repo.backup_yum_repos() loggerinst.task("Prepare: Clear YUM/DNF version locks") pkghandler.clear_versionlock() # begin conversion process process_phase = ConversionPhase.PRE_PONR_CHANGES pre_ponr_conversion() loggerinst.warning( "********************************************************") loggerinst.warning( "The tool allows rollback of any action until this point.") loggerinst.warning("By continuing all further changes on the system" " will need to be reverted manually by the user," " if necessary.") loggerinst.warning( "********************************************************") utils.ask_to_continue() process_phase = ConversionPhase.POST_PONR_CHANGES post_ponr_conversion() loggerinst.task("Final: rpm files modified by the conversion") systeminfo.system_info.modified_rpm_files_diff() # recommend non-interactive command loggerinst.task("Final: Non-interactive mode") toolopts.print_non_interactive_opts() # restart system if required utils.restart_system() except (Exception, SystemExit, KeyboardInterrupt) as err: # Catching the three exception types separately due to python 2.4 # (RHEL 5) - 2.7 (RHEL 7) compatibility. utils.log_traceback(toolopts.tool_opts.debug) no_changes_msg = "No changes were made to the system." if is_help_msg_exit(process_phase, err): return 0 elif process_phase == ConversionPhase.INIT: print(no_changes_msg) elif process_phase == ConversionPhase.POST_CLI: loggerinst.info(no_changes_msg) elif process_phase == ConversionPhase.PRE_PONR_CHANGES: rollback_changes() elif process_phase == ConversionPhase.POST_PONR_CHANGES: # After the process of subscription is done and the mass update of # packages is started convert2rhel will not be able to guarantee a # system rollback without user intervention. If a proper rollback # solution is necessary it will need to be future implemented here # or with the use of other backup tools. loggerinst.warning( "Conversion process interrupted and manual user intervention will be necessary." ) return 1 return 0