a }|ägM>ã@sxdZddlZddlZddlZddlZddlZddlmZddlZddlm Z ddl Z ddl m Z ddl mZddl mZddlmZdd lmZdd lmZddlmZGd d „d ejƒZGd d„dejƒZGdd„dejƒZGdd„dejƒZGdd„dejƒZGdd„dejƒZGdd„dejƒZGdd„dejƒZ Gdd„dej!ƒZ"e#dkrte $e  %ej&dd…e'g¡¡dS)z Tests for certbot._internal.log.éN)ÚOptional)Úmock)Úmessages)Úerrors)Úutil)Ú constants)Ú filesystem)Úoscs\eZdZdZedd„ƒZ‡fdd„Ze d¡e d¡e d¡e d ¡d d „ƒƒƒƒZ ‡Z S) ÚPreArgParseSetupTestz4Tests for certbot._internal.log.pre_arg_parse_setup.cOsddlm}|ƒS)Nr)Úpre_arg_parse_setup)Úcertbot._internal.logr )ÚclsÚargsÚkwargsr ©rúD/usr/lib/python3.9/site-packages/certbot/_internal/tests/log_test.pyÚ_calls zPreArgParseSetupTest._callcst ¡tƒ ¡dS©N)ÚloggingÚshutdownÚsuperÚtearDown©Úself©Ú __class__rrrszPreArgParseSetupTest.tearDownúcertbot._internal.log.sysz/certbot._internal.log.pre_arg_parse_except_hookú'certbot._internal.log.logging.getLoggerz*certbot._internal.log.util.atexit_registerc CsÔdg|_tj|_| ¡|ƒ}|j tj¡|jj dksrUZ return_valuer&r'rrJr rRÚjoinÚlogs_dirZ removeHandlerr)r5r+ÚcalledÚexistsrSr1r"r#rPÚlevelrZQUIET_LOGGING_LEVELZDEFAULT_LOGGING_LEVEL)rZmock_get_loggerZexcept_hook_pathr3r4r$r\rrrÚ test_commonbs.   fÿ þ z!PostArgParseSetupTest.test_commoncCsd|j_| ¡dS©NT)rJr"r]rrrrÚ test_debug}sz PostArgParseSetupTest.test_debugcCsd|j_| ¡dSr^)rJr#r]rrrrÚ test_quietsz PostArgParseSetupTest.test_quiet) r9r:r;r<r=rrIrr]r_r`r?rrrrr@As   r@csheZdZdZedd„ƒZ‡fdd„Ze d¡dd„ƒZ d d „Z d d „Z d d„Z e d¡dd„ƒZ ‡ZS)ÚSetupLogFileHandlerTestz7Tests for certbot._internal.log.setup_log_file_handler.cOsddlm}||i|¤ŽS)Nr)Úsetup_log_file_handler)r rb)r rrrbrrrr‰s zSetupLogFileHandlerTest._callcstƒ ¡d|j_dS)Né*)rrIrJrKrrrrrIŽs zSetupLogFileHandlerTest.setUpz;certbot._internal.main.logging.handlers.RotatingFileHandlerc Cs`t|_z| |jdd¡Wn6tjyP}zdt|ƒvsrmrqrrrprxr?rrrrra†s   rac@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) ÚColoredStreamHandlerTestz4Tests for certbot._internal.log.ColoredStreamHandlercCsXt ¡|_dd„|j_t ¡|_|j tj¡ddl m }||jƒ|_ |j  |j ¡dS)NcSsdSr^rrrrrÚ¾óz0ColoredStreamHandlerTest.setUp..rrD) rNrOÚstreamÚisattyrÚ getLoggerÚloggerr(r*r rEr7r+)rrErrrrI¼s     zColoredStreamHandlerTest.setUpcCs|j ¡dSr©r7rVrrrrrÆsz!ColoredStreamHandlerTest.tearDowncCs,d}|j |¡|j ¡d |¡ks(J‚dS)Nz I did a thingz{0} )rr"r|ÚgetvalueÚformat©rÚmsgrrrÚ test_formatÉs z$ColoredStreamHandlerTest.test_formatcCs>d}tj|j_|j |¡|j ¡d t j |t j ¡ks:J‚dS)NzI did another thingz {0}{1}{2} ) rr*r7Z red_levelrr"r|rr‚rZ ANSI_SGR_REDZANSI_SGR_RESETrƒrrrÚtest_format_and_red_levelÎs  þÿz2ColoredStreamHandlerTest.test_format_and_red_levelN)r9r:r;r<rIrr…r†rrrrry¹s  ryc@s@eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dS)ÚMemoryHandlerTestz-Tests for certbot._internal.log.MemoryHandlercCsbt t¡|_|j tj¡d|_t ¡|_ t  |j ¡|_ ddl m }||j ƒ|_|j |j¡dS)Nzhi thererrF)rr~r9rr(r*r„rNrOr|r0rPr r.r7r+)rr.rrrrIÛs    zMemoryHandlerTest.setUpcCs|j ¡|j ¡dSr)r7rVrPrrrrræs zMemoryHandlerTest.tearDowncCs2| ¡|jjdd|j ¡|jdks.J‚dS)NT©ÚforceÚ )Ú_test_log_debugr7Úflushr|rr„rrrrÚ test_flushêszMemoryHandlerTest.test_flushcCs.|j |j¡|j ¡|j ¡dks*J‚dS)NÚ)rZcriticalr„r7rŒr|rrrrrÚtest_not_flushedïs z"MemoryHandlerTest.test_not_flushedcCsh| ¡t ¡}t |¡}|j |¡|jjdd|j  ¡dksFJ‚|  ¡|j dks\J‚|  ¡dS)NTrˆrŽrŠ) r‹rNrOrr0r7Z setTargetrŒr|rr„rV)rZ new_streamZnew_stream_handlerrrrÚtest_target_resetõs  z#MemoryHandlerTest.test_target_resetcCs|j |j¡dSr)rr"r„rrrrr‹sz!MemoryHandlerTest._test_log_debugN) r9r:r;r<rIrrrrr‹rrrrr‡Ùs  r‡c@s8eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd S) ÚTempHandlerTestz,Tests for certbot._internal.log.TempHandler.cCsd|_ddlm}|ƒ|_dS)NFrrG)Úclosedr rHr7)rrHrrrrIs zTempHandlerTest.setUpcCs|j ¡dSrr€rrrrr szTempHandlerTest.tearDowncCst |jjd¡sJ‚dS)Ni€)rZcheck_permissionsr7rRrrrrÚtest_permissionssz TempHandlerTest.test_permissionscCs"|j ¡tj |jj¡rJ‚dSr)r7rVr rRr[rrrrÚ test_deletes zTempHandlerTest.test_deletecCs@|j t ¡¡|j ¡tj |jj¡s.J‚t |jj¡dSr) r7ÚemitrrTrVr rRr[ÚremoverrrrÚtest_no_deletes zTempHandlerTest.test_no_deleteN) r9r:r;r<rIrr“r”r—rrrrr‘s r‘c@s.eZdZdZedd„ƒZe d¡dd„ƒZdS)ÚPreArgParseExceptHookTestz:Tests for certbot._internal.log.pre_arg_parse_except_hook.cOsddlm}||i|¤ŽS)Nr)Úpre_arg_parse_except_hook)r r™)r rrr™rrrrs zPreArgParseExceptHookTest._callrWcCsNt ¡}d}ddi}|j|g|¢Ri|¤Ž|j|i|¤Ž|jjdddS)N)ÚsomerršrTrˆ)rrTrr)rŒ)rZmock_post_arg_parse_except_hookr5rrrrrr8#sÿÿz!PreArgParseExceptHookTest.test_itN) r9r:r;r<r=rrr>r8rrrrr˜s  r˜c@s~eZdZdZedd„ƒZdd„Zdd„Zdd „Zd d „Z d d „Z dd„Z dd„Z dd„Z ddd„Zdd„Zdd„Zdd„ZdS)ÚPostArgParseExceptHookTestz;Tests for certbot._internal.log.post_arg_parse_except_hook.cOsddlm}||i|¤ŽS)Nr)Úpost_arg_parse_except_hook)r rœ)r rrrœrrrr2s z PostArgParseExceptHookTest._callcCsd|_d|_dS)Nztest error messagezfoo.log)Ú error_msgr$rrrrrI7sz PostArgParseExceptHookTest.setUpcCs2t}|j|dd\}}| |j|¡| |¡dS©NF©r")Ú BaseExceptionÚ _test_commonÚ_assert_exception_loggedÚerrorÚ_assert_logfile_output©rÚexc_typeÚ mock_loggerÚoutputrrrÚtest_base_exception;sz.PostArgParseExceptHookTest.test_base_exceptioncCs2t}|j|dd\}}| |j|¡| |¡dS)NTrŸ)Ú ValueErrorr¡r¢r£r¤r¥rrrr_Asz%PostArgParseExceptHookTest.test_debugcCs6t}|j|ddd\}}| |j|¡d|vs2J‚dS)NT)r"r#úSee the logfile)rªr¡r¢r£r¥rrrr`Gsz%PostArgParseExceptHookTest.test_quietcCs6tj}|j|dd\}}| |j|¡| ||¡dSrž)rZ PluginErrorr¡r¢r"Ú_assert_quiet_outputr¥rrrÚtest_custom_errorMsz,PostArgParseExceptHookTest.test_custom_errorcsZtttjƒƒ‰‡fdd„}|j|dd\}}| |jtj¡| ||¡tj |vsVJ‚dS)Ncstjjˆ|dS)z6Wraps ACME errors so the constructor takes only a msg.)Zdetail)rrhZ with_code)r„©Z acme_coderrÚget_acme_errorWszBPostArgParseExceptHookTest.test_acme_error..get_acme_errorFrŸ) ÚnextÚiterrZ ERROR_CODESr¡r¢r"rhr¬Z ERROR_PREFIX)rr¯r§r¨rr®rÚtest_acme_errorSs   z*PostArgParseExceptHookTest.test_acme_errorcCs4t}|j|dd\}}| |j|¡| ||¡dSrž)rªr¡r¢r"r¬r¥rrrÚtest_other_error`sz+PostArgParseExceptHookTest.test_other_errorcCs&t}|j|dd\}}|j d¡dS)NFrŸzExiting due to user request.)ÚKeyboardInterruptr¡r£r)r¥rrrÚtest_keyboardinterruptfsz1PostArgParseExceptHookTest.test_keyboardinterruptFc st ¡‰‡fdd„}z||jƒ‚WnÐtyòt ¡}t d¡œ}||j_ t dˆ¡hz|j ||||j dœŽWn2t y¦}zˆ  t|ƒ¡WYd}~nd}~00| d¡Wdƒn1sÆ0YWdƒn1sä0YYn0ˆ ¡}||fS)z,Returns the mocked logger and stderr output.csˆ |d¡dS)zWrite error to mock_err.rN)Úwrite)rZ unused_kwargs©Zmock_errrrÚ write_errosz:PostArgParseExceptHookTest._test_common..write_errzcertbot._internal.log.loggerz certbot._internal.log.sys.stderrr!NzSystemExit not raised.)rNrOrr r&Úexc_inforr>r£rgrr$Ú SystemExitr¶rirjr) rZ error_typer"r#r¸r¹r§Zexit_errr¨rr·rr¡ks$    ÿ $Lz'PostArgParseExceptHookTest._test_commoncCsF|js J‚|jd}d|vs J‚|d}|tjtjf}||ksBJ‚dS)Nrr¹)rZrwrr2)rZlog_funcr¦Z call_kwargsZactual_exc_infoZexpected_exc_inforrrr¢…s    z3PostArgParseExceptHookTest._assert_exception_loggedcCsd|vs J‚|j|vsJ‚dS)Nr«)r$)rr¨rrrr¤Žs z1PostArgParseExceptHookTest._assert_logfile_outputcCs.|jjdusJ‚|jjsJ‚|j|vs*J‚dS)NF)Ú exceptionrZr"r)rr§r¨rrrr¬’s z/PostArgParseExceptHookTest._assert_quiet_outputN)F)r9r:r;r<r=rrIr©r_r`r­r²r³rµr¡r¢r¤r¬rrrrr›0s    r›c@s4eZdZdZedd„ƒZdd„Zdd„Zdd „Zd S) ÚExitWithAdviceTestz1Tests for certbot._internal.log.exit_with_advice.cOsddlm}||i|¤ŽS)Nr)Úexit_with_advice)r r½)r rrr½rrrršs zExitWithAdviceTest._callcCsDtj |jd¡}t|dƒ ¡| |¡}d|vs4J‚||vs@J‚dS)NrdrCÚlogfiles)r rRrXÚtempdirrLrVr¡)rrvÚerr_strrrrÚ test_log_fileŸs   z ExitWithAdviceTest.test_log_filecCs*| |j¡}d|vsJ‚|j|vs&J‚dS)Nr¾)r¡r¿)rrÀrrrÚ test_log_dir§s  zExitWithAdviceTest.test_log_dirc OsPz|j|i|¤ŽWn,ty@}zt|ƒWYd}~Sd}~00| d¡dS)NzSystemExit was not raised.)rrºrirj)rrrrlrrrr¡­s zExitWithAdviceTest._test_commonN) r9r:r;r<r=rrÁrÂr¡rrrrr¼˜s  r¼Ú__main__r)(r<rNrZlogging.handlersr&rtÚtypingrZunittestrZpytestZacmerZcertbotrrZcertbot._internalrZcertbot.compatrr Z certbot.testsZ test_utilZTestCaser ZConfigTestCaser@raryr‡r‘r˜r›ZTempDirTestCaser¼r9ÚexitÚmainr%Ú__file__rrrrÚs6         +E3 +h