Ϫf(GddlmZddlZddlZddlZddlZddlZddlmZddl m Z m Z ddl m Z Gdde ZGdd e jZGd d e Zy) ) annotationsN)skipIf)logfileruntime)TestCaseceZdZdZddZddZddZddZddZddZ ddZ dd Z dd Z dd Z dd Zdd ZddZddZeej(j+dddZddZddZddZddZy) LogFileTestsz% Test the rotating log file. c|j|_tj|jd|_tj j |j|j|_y)Nztest.logmktempdirosmakedirsnamepathjoinselfs ;/usr/lib/python3/dist-packages/twisted/test/test_logfile.pysetUpzLogFileTests.setUpsD;;= DHH GGLL4995 ctj|jdtjj |jr!tj|jdyy)z Restore back write rights on created paths: if tests modified the rights, that will allow the paths to be removed easily afterwards. N)rchmodr rexistsrs rtearDownzLogFileTests.tearDownsA 5! 77>>$)) $ HHTYY & %rctj|j|j}|j |j |j t|jy)zf L{BaseLogFile.shouldRotate} is abstract and must be implemented by subclass. N) r BaseLogFilerr addCleanupclose assertRaisesNotImplementedError shouldRotaterlogs rtest_abstractShouldRotatez&LogFileTests.test_abstractShouldRotate$sE !!$))TXX6  " -s/?/?@rctjtj|j|j 5}|j d|j d|j|j ddddt|j5}|j|jddddy#1swYHxYw#1swYyxYw)zn Log files can be written to, flushed and closed. Closing a log file also flushes it. 1234567890N 1234567890) contextlibclosingrLogFilerr writeflushopenr assertEqualreadrr%fs r test_writingzLogFileTests.test_writing-s    488 D E  IIe  IIe  IIK IIf    $))_ 5   QVVX| 4 5 5     5 5sAC !C CCcjtjtj|j|j d5}|j d|j d|j d|jtjj|jd|jtjj|jd|j d|jtjj|jd|jtjj|jd|jtjj|jd |j d |jtjj|jd |j|jtjj|jd |jtjj|jd d d d |jjgd y #1swY,xYw)zq Rotating log files autorotate after a period of time, and can also be manually rotated. ) rotateLengthr(4567890 11111111111.1.2.33.4N))r,r-rr.rr r/ assertTruerrr assertFalserotater2listLogsr$s r test_rotationzLogFileTests.test_rotation;s    OODIItxxb A  ?  IIe  IIi IIh  OOBGGNNdii[+;< =   RWW^^tyyk,<= > IIbM OOBGGNNdii[+;< = OOBGGNNdii[+;< =   RWW^^tyyk,<= > IIcN   RWW^^tyyk,<= > JJL OOBGGNNdii[+;< =   RWW^^tyyk,<= >' ?* 3+ ? ?s H>J))J2c4tjtj|j|j 5}|j ddddtj|j|j }|j|j|j|jd|j|jj|j|j d|j|jd|j|jj|j|j}|jdd|j|jdy#1swY?xYw)a Log files can be written to, closed. Their size is the number of bytes written to them. Everything that was written to them can be read, even if the writing happened on separate occasions, and even if the log file was closed in between. 0123456789Nr8abc rs 0123456789abc)r,r-rr.rr r/rr r2size_filetellseekr3r4s r test_appendzLogFileTests.test_appendXs   488 D E $ IIl # $oodii2  " 2& )3884 % 2& )3884 II q!  #34 $ $s F  Fctj|j|j}|j |j |j d|j d|j|j d|j|j|jdgtj|j5}|jjd|j|j!dg|j|j!gdddtj|j#d5}|j|j!ddg|j|j!gddd|j%t&|j"d|j%t(|j"d|j|j|jddgtj|j#d5}|jjd|j|j!dg|j|j!gdddtj|j#d5}|j|j!ddg|j|j!gdddy#1swYxYw#1swYxYw#1swYxYw#1swYyxYw) a[ Various tests for log readers. First of all, log readers can get logs by number and read what was written to those log files. Getting nonexistent log files raises C{ValueError}. Using anything other than an integer index raises C{TypeError}. As logs get older, their log numbers increase. zabc zdef zghi rBrNrC1)rr.rr rr r/rGr0r2rHr,r- getCurrentLogrOrQ readLinesgetLogr! ValueError TypeError)rr%readers rtest_logReaderzLogFileTests.test_logReadermsoodii2  " ' '  '  !-    1 1 3 4 5 LL  a   V--/' ;   V--/ 4 5   1 . 5&   V--/'71C D   V--/ 4 5 *cjj!4 )SZZ5  !Q0    1 . 5& LL  a   V--/' ;   V--/ 4 5   1 . 5&   V--/'71C D   V--/ 4 5 5' 5 5 5 5 5 5 5 5s4AL&AL+ AL8AML(+L58MM ct|jd5 dddtj|j}|j |j |j g|jdy#1swYexYw)zB L{LogReader.readLines} supports reading no line. wNr)r1rr LogReaderrr r2rV)rrZs rtest_LogReaderReadsZeroLinez(LogFileTests.test_LogReaderReadsZeroLinesk $))S !   ""499-  % V--a01   s A==Bc8t|jdjtj|jdtj |jt j }tj|j|j}|j|j|jd|j|j|tj |jt j y)zH Check rotated files have same permissions as original. r]rLN)r1rr rrstatST_MODErr.rr rr/rGr2)rmoder%s rtest_modePreservationz"LogFileTests.test_modePreservations TYY""$ E"wwtyy!$,,/oodii2  " %  rwwtyy1$,,?@rctj|j|j}|j |j |j dtj|jd ttjj|jdd}|j y#t$rYnwxYw|j|j d|j|j}|j!|j#d|j%dd|j!|j'd y) zH Check it keeps working when permission on dir changes. rLmxxxr]Ndefrsabcdef)rr.rr rr r/rrr1rrOSErrorrGr0rOr2rPrQr3r4s rtest_noPermissionzLogFileTests.test_noPermissionsoodii2  " % 5! RWW\\$((E2C8A GGI       % II 1% q!  9-s84B== C C cJtj|j|jdd}|j |j |j d|j d|jtjj|jd|j d|jtjj|jd|j d |jtjj|jd t|jd 5}|j|jdd d d |j d t|jd 5}|j|jdd d d |jtjj|jd y #1swYxYw#1swYQxYw)zl Test it respect the limit on the number of files when maxRotatedFiles is not None. r8rD)r9maxRotatedFilesr; 22222222222r< 33333333333r= 44444444444r?N 55555555555rA)rr.rr rr r/rErrrr1r2r3rFrr%fps rtest_maxNumberOfLogz LogFileTests.test_maxNumberOfLogs oodiiTUV  " ( ( $))B'789 ( $))B'789 ( $))B'789 TYYKr" # 2r   RWWY 1 2 ( TYYKr" # 2r   RWWY 1 2 499+R(89:  2 2 2 2s!H (!H HH"ctj|j|jdd}|j |j tjj |jdd}|j |j |j|j|j|jtjj|j|j|j|j|j|j|j|jy)z/ Test the fromFullPath method. r8r defaultModeN) rr.rr rr fromFullPathrr2rabspathr9rx)rlog1log2s rtest_fromFullPathzLogFileTests.test_fromFullPathstyy$((BEJ  #++DIIru+M  # DII. 3TYY? **D,=,=> ))4+;+;r)nullz/devz /dev/nullrgrwN) rrrrr.rr r2rrx)rr directory expectedPathr%s rtest_cantChangeFileModez$LogFileTests.test_cantChangeFileMode)s|    % % ''OD) L,OD)&LoodI5A  " <0 %0rctj|j|j}|j |j t |jdd5}|jddddt |jdd5}|jdddd|jdg|jy#1swY^xYw#1swY7xYw)zl L{LogFile.listLogs} doesn't choke if it encounters a file with an unexpected name. r<r]r(Nz .bad-filerB) rr.rr rr r1rr/r2rHrss r test_listLogsWithBadlyNamedFilesz-LogFileTests.test_listLogsWithBadlyNamedFiles;s oodii2  " SXXJb/3 ' 2 HHUO  SXXJi(# . " HHUO  !clln-     sCCCC#cvtj|j|j}|j |j t ddD]7}t|jd|d5}|jdddd9|jddg|jy#1swYfxYw) zR L{LogFile.listLogs} ignores log files which rotated suffix is 0. rrD.r]r(NrBrC) rr.rr rr ranger1rr/r2rH)rr%irts r%test_listLogsIgnoresZeroSuffixedFilesz2LogFileTests.test_listLogsIgnoresZeroSuffixedFilesJsoodii2  "q! A !A3'-     !Q0  s 0B//B8 NreturnNone)__name__ __module__ __qualname____doc__rrr&r6rIrRr[r_rerlrur}rrrrrrrrrrrrrr r s6 'A 54:5*)5V 2 A.>;0 = $ * G   & & (*HI1J1"01$ . 1rr c eZdZdZddZddZy)RiggedDailyLogFilecltjj||j|_y)N)r DailyLogFile _openFiletoDatelastDaters rrzRiggedDailyLogFile._openFile[s#&&t,  rct|rtj|ddStj|jddS)NrD)timegmtime_clock)rargss rrzRiggedDailyLogFile.toDate`s5 ;;%bq) ){{4;;'++rNr)rfloatrztuple[int, int, int])rrrrrrrrrrrXs F& ,rrceZdZdZddZddZddZddZddZe e jjdddZ dd Zdd Zdd Zdd Zy )DailyLogFileTestsz! Test rotating log file. c|j|_tj|jd|_tj j |j|j|_y)Nz testdaily.logr rs rrzDailyLogFileTests.setUpksD;;= DHH# GGLL4995 rctjt|j|j5}|j d|j d|j |j ddddt|j5}|j|jddddy#1swYHxYw#1swYyxYw)zO A daily log file can be written to like an ordinary log file. r(r)r*Nr+) r,r-rrr r/r0r1rr2r3r4s rr6zDailyLogFileTests.test_writingqs   2499dhh G H C IIe  IIe  IIK IIf    $))_ 5   QVVX| 4 5 5     5 5sAB?!C ?C Cc4t|j|j}|j|jt dDcgc]&}|j dz|j|dzz(}}d|_|jdd|_|jdd|_|jd|jtj j|d |jtj j|d d |_|jd |jtj j|d |jtj j|d |jtj j|d d|_|jd|jtj j|d ycc}w)z/ Daily log files rotate daily. rDrQrr(ir:r;rrBir>rCir@N)rrr rr rrsuffixrr/rErrrF)rr%daydayss rrIzDailyLogFileTests.test_rotation~sr!DHH5  "GLQxPS3::cEk#::PP  %  )  ( tAw/0 Q01  "  tAw/0 tAw/0 Q01  # Q01%Qs +Hcgd}t|j|j}|j|j|D]}|j ||j |jd}|j|j|j||j|jt|jdd|_ |j |j|jd}|j|j|j||jy)zH Test retrieving log files with L{DailyLogFile.getLog}. )z1 z2 z3 rriQrN)rrr rr r/r0rWr2rVr!rXrrG)rdatar%drs r test_getLogzDailyLogFileTests.test_getLogs% DHH5  " A IIaL   JJsO   q{{}- *cjj%8    JJqM   q{{}-rct|j|j}|j|j|j d|j |j}t|d5}|jdddd|j}|j|j||jy#1swYBxYw)zu L{DailyLogFile.rotate} doesn't do anything if they new log file already exists on the disk. rr]r(N) rrr rr rrrr1r/rOrGr2)rr% newFilePathrt previousFiles rtest_rotateAlreadyExistsz*DailyLogFileTests.test_rotateAlreadyExistss !DHH5  " !CJJs||$<#=> +s # r HHUO yy   syy1   s 2CCzVMaking read-only directories on Windows is too complex for this test to reasonably do.ctj|j|j}|j |j t j|jd|j t j|jd|j}|j|j||jy)z L{DailyLogFile.rotate} doesn't do anything if the directory containing the log files can't be written to. riN) rrrr rr rrrrOrGr2rr%rs r#test_rotatePermissionDirectoryNotOkz5DailyLogFileTests.test_rotatePermissionDirectoryNotOks~""499dhh7  " & #--7yy   syy1rc>tj|j|j}|j |j t j|jd|j}|j|j||jy)zi L{DailyLogFile.rotate} doesn't do anything if the log file can't be written to. rN) rrrr rr rrrrOrGr2rs rtest_rotatePermissionFileNotOkz0DailyLogFileTests.test_rotatePermissionFileNotOksf ""499dhh7  " 5!yy   syy1rctj|j|j}|j |j t jd}|jd|j|y)z~ Test that L{DailyLogFile.toDate} converts its timestamp argument to a time tuple (year, month, day). ) rBrBrrrrrr)rrBrBN) rrrr rr rmktimer2r)rr% timestamps r test_toDatezDailyLogFileTests.test_toDatesV ""499dhh7  "KK >?  szz)'<=rcdfd }tjjj}j |j j td||j}jgd|y)a Test that L{DailyLogFile.toDate} returns today's date by default. By mocking L{time.localtime}, we ensure that L{DailyLogFile.toDate} returns the first 3 values of L{time.localtime} which is the current date. Note that we don't compare the *real* result of L{DailyLogFile.toDate} to the *real* current date, as there's a slight possibility that the date changes between the 2 function calls. cRjd|ttddS)Nrr )r2listr)rrs rmock_localtimezADailyLogFileTests.test_toDateDefaultToday..mock_localtimes$   R &a $ $r localtime)rrBrCN)robjectrz list[int]) rrrr rr patchrrr2)rrr%logDates` rtest_toDateDefaultTodayz)DailyLogFileTests.test_toDateDefaultTodays_ %""499dhh7  " 4n5**, G,rctj|j|j}|j |j d}t j|dz}|j|}|j||y)zc Test that L{DailyLogFile.toDate} uses its arguments to create a new date. )ir8)rrrrrrN) rrrr rr rrrr2)rr%datesecondsrs r#test_toDateUsesArgumentsToMakeADatez5DailyLogFileTests.test_toDateUsesArgumentsToMakeADatesc ""499dhh7  "++dXo.**W% w'rNr)rrrrrr6rIrrrrrrrrrrrrrrrrfsd6 524.62" ""$ ! 2  2 2 >-0 (rr) __future__rr,rrrbrunittestrtwisted.pythonrrtwisted.trial.unittestrr rrrrrrrsQ# ++E18E1P ,-- ,n(n(r