a }|gE@sdZddlZddlZddlmZddlZddlmZddlm Z ddlm Z ddl m Z Gdd d e jZed kreeejd degdS) z/Tests for certbot.plugins.storage.PluginStorageN)mock)errors) filesystem)os)utilcs`eZdZdZfddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ Z S)PluginStorageTestz.Test for certbot.plugins.storage.PluginStoragecs`ttj|_t|jjt d"|j|jdd|_ Wdn1sR0YdS)Ncertbot.reverter.util mockplugin)configname) supersetUp test_utilZDummyInstaller plugin_clsrmkdirr config_dirrpatchpluginself __class__P/usr/lib/python3.9/site-packages/certbot/_internal/tests/plugins/storage_test.pyr s   zPluginStorageTest.setUpc Cs*ttj|jjdd}|dWdn1s80Yt}t |_ tj|jjd|j j _ td|tjdddrtdHttj|j j Wdn1s0YWdn1s0YWdn1s0YWdn1s0YdS) N.pluginstorage.jsonwZdummyz builtins.openzcertbot.compat.os.path.isfileT)Z return_valuer)openrpathjoinr rwriter mock_openIOError side_effectrstorage _storagepathrpytestraisesrPluginStorageError_load)rfhr rrrtest_load_errors_cant_reads"(   z,PluginStorageTest.test_load_errors_cant_readc Csttj|jjdd}|dWdn1s80Ytd}td| |jd}Wdn1s|0Yt t |j dWdn1s0Y|jsJd|jd d vsJWdn1s0YdS) Nrrz$certbot.plugins.storage.logger.debugrr valuezno values loadedr)rrrrr rrrrrr%r&KeyErrorr#fetchZcalled call_args)rr)mock_logZ nocontentrrrtest_load_errors_empty's(  , * z(PluginStorageTest.test_load_errors_emptyc Csttj|jjdd}|dWdn1s80Ytd}td| |jd}Wdn1s|0Yt t j |jdWdn1s0Yd|jd d vsJWdn1s0YdS) Nrrz invalid json$certbot.plugins.storage.logger.errorrr r,z is corruptedr)rrrrr rrrrrr%r&rZ PluginErrorr#r.r/)rr)r0Z corruptedrrrtest_load_errors_corrupted3s(  ,*z,PluginStorageTest.test_load_errors_corruptedc Cstd~}d|jj_d|jj_|j|jj_t t j |jj Wdn1sZ0Yd|j ddvszJWdn1s0YdS)Nr2T /tmp/whateverzCould not serializer)rrrr# _initializedr$r_datar%r&rr'saver/)rr0rrrtest_save_errors_cant_serialize>s    *z1PluginStorageTest.test_save_errors_cant_serializec Cst}t|_td|td}ddi|jj_d|jj_d|jj_ t t j |jjWdn1sx0Yd|jddvsJWdn1s0YWdn1s0YdS) Nzcertbot.compat.filesystem.openr2ZvaliddataTr4zCould not writer)rr r!r"rrr#r6r5r$r%r&rr'r7r/)rr r0rrr%test_save_errors_unable_to_write_fileHs   *z7PluginStorageTest.test_save_errors_unable_to_write_filec CsntdPttj$||jdj Wdn1sB0YWdn1s`0YdS)Nrx) rrr%r&rr'rr r#r7rrrrtest_save_uninitializedTs z)PluginStorageTest.test_save_uninitializedcCstd,||jd}||jd}Wdn1s<0Y|jddtt|j dWdn1s0Ytt|j dWdn1s0Y|j ddksJdS)NrfirstsecondZ first_keyZ first_value) rrrr r#putr%r&r-r.)rZplugin1Zplugin2rrrtest_namespace_isolationYs , * *z*PluginStorageTest.test_namespace_isolationcCs|jjdd|jjtd||jd}Wdn1sJ0Y|jddkshJt t j |jj dd}|}Wdn1s0Yt|}d|vsJt|dksJ|dddksJdS)NZtestkeyZ testvaluerr rr)rr#r?r7rrrr r.rrrrrreadjsonloadskeyslen)rZanotherr)ZpsdataZpsjsonrrrtest_saved_stateds   ,& z"PluginStorageTest.test_saved_state)__name__ __module__ __qualname____doc__r r*r1r3r8r:r<r@rH __classcell__rrrrrs      r__main__rB)rLrDsysZunittestrr%ZcertbotrZcertbot.compatrrZ certbot.testsrrZConfigTestCaserrIexitmainargv__file__rrrrs     g