o
    ¯bãE  ã                   @   s†   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZ G dd„ deƒZG dd„ dejƒZG d	d
„ d
eƒZdS )é    N)ÚskipIf)ÚlogfileÚruntime)ÚTestCasec                   @   s¸   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zeej ¡ dƒdd „ ƒZd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)S )*ÚLogFileTestsz%
    Test the rotating log file.
    c                 C   ó4   |   ¡ | _t | j¡ d| _tj | j| j¡| _d S )Nztest.log©ÚmktempÚdirÚosÚmakedirsÚnameÚpathÚjoin©Úself© r   ú;/usr/lib/python3/dist-packages/twisted/test/test_logfile.pyÚsetUp   ó   
zLogFileTests.setUpc                 C   s2   t  | jd¡ t j | j¡rt  | jd¡ dS dS )zž
        Restore back write rights on created paths: if tests modified the
        rights, that will allow the paths to be removed easily afterwards.
        éÿ  N)r   Úchmodr
   r   Úexistsr   r   r   r   ÚtearDown   s   ÿzLogFileTests.tearDownc                 C   s.   t  | j| j¡}|  |j¡ |  t|j¡ dS )zf
        L{BaseLogFile.shouldRotate} is abstract and must be implemented by
        subclass.
        N)	r   ÚBaseLogFiler   r
   Ú
addCleanupÚcloseÚassertRaisesÚNotImplementedErrorÚshouldRotate©r   Úlogr   r   r   Útest_abstractShouldRotate$   s   z&LogFileTests.test_abstractShouldRotatec                 C   sœ   t  t | j| j¡¡}| d¡ | d¡ | ¡  | d¡ W d  ƒ n1 s)w   Y  t| j	ƒ}|  
| ¡ d¡ W d  ƒ dS 1 sGw   Y  dS )zn
        Log files can be written to, flushed and closed. Closing a log file
        also flushes it.
        Ú123Ú456Ú7890NÚ
1234567890)Ú
contextlibÚclosingr   ÚLogFiler   r
   ÚwriteÚflushÚopenr   ÚassertEqualÚread©r   r!   Úfr   r   r   Útest_writing-   s   

ü"ÿzLogFileTests.test_writingc                 C   s\  t  tj| j| jdd¡}| d¡ | d¡ | d¡ |  tj	 
| j	› d¡¡ |  tj	 
| j	› d¡¡ | d¡ |  tj	 
| j	› d¡¡ |  tj	 
| j	› d¡¡ |  tj	 
| j	› d	¡¡ | d
¡ |  tj	 
| j	› d	¡¡ | ¡  |  tj	 
| j	› d	¡¡ |  tj	 
| j	› d¡¡ W d  ƒ n1 sw   Y  |  | ¡ g d¢¡ dS )zq
        Rotating log files autorotate after a period of time, and can also be
        manually rotated.
        é
   )ÚrotateLengthr#   Ú4567890Ú11111111111ú.1ú.2Ú ú.3Ú3ú.4N)é   é   é   )r'   r(   r   r)   r   r
   r*   Ú
assertTruer   r   r   ÚassertFalseÚrotater-   ÚlistLogsr    r   r   r   Útest_rotation;   s(   ÿ




ìzLogFileTests.test_rotationc                 C   sÐ   t  t | j| j¡¡}| d¡ W d  ƒ n1 sw   Y  t | j| j¡}|  |j¡ |  	|j
d¡ |  	|j ¡ |j
¡ | d¡ |  	|j
d¡ |  	|j ¡ |j
¡ |j}| dd¡ |  	| ¡ d¡ dS )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.
        Ú
0123456789Nr2   Úabcé   r   s   0123456789abc)r'   r(   r   r)   r   r
   r*   r   r   r-   ÚsizeÚ_fileÚtellÚseekr.   r/   r   r   r   Útest_appendY   s   ÿ
zLogFileTests.test_appendc                 C   s   t  | j| j¡}|  |j¡ | d¡ | d¡ | ¡  | d¡ | ¡  |  	| 
¡ dg¡ t | ¡ ¡}|j d¡ |  	| ¡ dg¡ |  	| ¡ g ¡ W d  ƒ n1 sWw   Y  t | d¡¡}|  	| ¡ ddg¡ |  	| ¡ g ¡ W d  ƒ n1 sw   Y  |  t|jd¡ |  t|jd¡ | ¡  |  	| 
¡ ddg¡ t | d¡¡}|j d¡ |  	| ¡ dg¡ |  	| ¡ g ¡ W d  ƒ n1 sÎw   Y  t | d¡¡}|  	| ¡ ddg¡ |  	| ¡ g ¡ W d  ƒ dS 1 sùw   Y  dS )	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.
        zabc
zdef
zghi
r<   r   Nr=   Ú1)r   r)   r   r
   r   r   r*   rA   r+   r-   rB   r'   r(   ÚgetCurrentLogrH   rJ   Ú	readLinesÚgetLogr   Ú
ValueErrorÚ	TypeError)r   r!   Úreaderr   r   r   Útest_logReadern   s<   



ýþý"þzLogFileTests.test_logReaderc                 C   sZ   t | jdƒ W d  ƒ n1 sw   Y  t | j¡}|  |j¡ |  g | d¡¡ dS )zB
        L{LogReader.readLines} supports reading no line.
        ÚwNr   )r,   r   r   Ú	LogReaderr   r   r-   rN   )r   rR   r   r   r   Útest_LogReaderReadsZeroLine™   s   ÿz(LogFileTests.test_LogReaderReadsZeroLinec                 C   s|   t | jdƒ ¡  t | jd¡ t | j¡tj }t | j	| j
¡}|  |j¡ | d¡ | ¡  |  |t | j¡tj ¡ dS )zH
        Check rotated files have same permissions as original.
        rT   éÇ  rE   N)r,   r   r   r   r   ÚstatÚST_MODEr   r)   r   r
   r   r*   rA   r-   )r   Úmoder!   r   r   r   Útest_modePreservation¥   s   
z"LogFileTests.test_modePreservationc                 C   s¾   t  | j| j¡}|  |j¡ | d¡ t | jd¡ zt	tj
 | jd¡dƒ}W n	 ty0   Y nw | ¡  dS | ¡  | d¡ | ¡  |j}|  | ¡ d¡ | dd¡ |  | ¡ d	¡ dS )
zH
        Check it keeps working when permission on dir changes.
        rE   ém  ÚxxxrT   NÚdefé   r   s   abcdef)r   r)   r   r
   r   r   r*   r   r   r,   r   r   ÚOSErrorrA   r+   rH   r-   rI   rJ   r.   r/   r   r   r   Útest_noPermission²   s$   
ÿ
zLogFileTests.test_noPermissionc                 C   s@  t j| j| jddd}|  |j¡ | d¡ | d¡ |  tj	 
| j	› d¡¡ | d¡ |  tj	 
| j	› d¡¡ | d	¡ |  tj	 
| j	› d
¡¡ t| j	› d
ƒ}|  | ¡ d¡ W d  ƒ n1 sgw   Y  | d¡ t| j	› d
ƒ}|  | ¡ d¡ W d  ƒ n1 sŒw   Y  |  tj	 
| j	› d¡¡ dS )zl
        Test it respect the limit on the number of files when maxRotatedFiles
        is not None.
        r2   r>   )r3   ÚmaxRotatedFilesr5   Ú22222222222r6   Ú33333333333r7   Ú44444444444r9   NÚ55555555555r;   )r   r)   r   r
   r   r   r*   r?   r   r   r   r,   r-   r.   r@   ©r   r!   Úfpr   r   r   Útest_maxNumberOfLogÑ   s"   



ÿ
ÿz LogFileTests.test_maxNumberOfLogc                 C   sŽ   t j| j| jddd}|  |j¡ t jj| jddd}|  |j¡ |  |j|j¡ |  t	j 
|j¡|j¡ |  |j|j¡ |  |j|j¡ dS )z/
        Test the fromFullPath method.
        r2   r   ©ÚdefaultModeN)r   r)   r   r
   r   r   ÚfromFullPathr   r-   r   Úabspathr3   rk   )r   Úlog1Úlog2r   r   r   Útest_fromFullPathé   s   zLogFileTests.test_fromFullPathc                 C   s’   t | jdƒ t | jd¡ t t | j¡tj ¡}W d  ƒ n1 s$w   Y  t | j	| j
¡}|  t t | j¡tj ¡|¡ |  |j¡ dS )zx
        Test the default permission of the log file: if the file exist, it
        should keep the permission.
        ÚwbrW   N)r,   r   r   r   rX   ÚS_IMODErY   r   r)   r   r
   r-   r   r   )r   ÚcurrentModern   r   r   r   Útest_defaultPermissionsö   s   þ z$LogFileTests.test_defaultPermissionsc                 C   sb   t j| j| jdd}|  |j¡ t t | j	¡tj
 ¡}tj ¡ r)|  |d¡ dS |  |d¡ dS )zG
        Test specifying the permissions used on the log file.
        é6   rj   é$  N)r   r)   r   r
   r   r   rX   rr   r   r   rY   r   ÚplatformÚ	isWindowsr-   )r   rn   rZ   r   r   r   Útest_specifiedPermissions  s   
z&LogFileTests.test_specifiedPermissionszCan't test reopen on Windowsc                 C   sè   t  t | j| j¡¡%}| d¡ tj 	| jd¡}t 
| j|¡ | ¡  | d¡ W d  ƒ n1 s3w   Y  t| jƒ}|  | ¡ d¡ W d  ƒ n1 sPw   Y  t|ƒ}|  | ¡ d¡ W d  ƒ dS 1 smw   Y  dS )z‹
        L{logfile.LogFile.reopen} allows to rename the currently used file and
        make L{logfile.LogFile} create a new file.
        Úhello1zsave.logÚhello2N)r'   r(   r   r)   r   r
   r*   r   r   r   ÚrenameÚreopenr,   r-   r.   )r   rn   ÚsavePathr0   r   r   r   Útest_reopen  s   
ûÿ
"ÿzLogFileTests.test_reopenc                 C   s(   |   ttj| jd¡}|  |jtj¡ dS )zR
        Specifying an invalid directory to L{LogFile} raises C{IOError}.
        Úthis_dir_does_not_existN)r   ÚIOErrorr   r)   r   r-   ÚerrnoÚENOENT)r   Úer   r   r   Útest_nonExistentDir!  s   ÿz LogFileTests.test_nonExistentDirc                 C   s`   t j ¡ rd\}}d}nd\}}d}tj||dd}|  |j¡ |  |j|¡ |  |j	d¡ dS )z„
        Opening a L{LogFile} which can be read and write but whose mode can't
        be changed doesn't trigger an error.
        )ÚNULr8   r†   )Únullz/devz	/dev/nullr\   rj   N)
r   rw   rx   r   r)   r   r   r-   r   rk   )r   r   Ú	directoryÚexpectedPathr!   r   r   r   Útest_cantChangeFileMode*  s   
z$LogFileTests.test_cantChangeFileModec                 C   sª   t  | j| j¡}|  |j¡ t|j› ddƒ}| d¡ W d  ƒ n1 s'w   Y  t|j› ddƒ}| d¡ W d  ƒ n1 sEw   Y  |  	dg| 
¡ ¡ dS )zl
        L{LogFile.listLogs} doesn't choke if it encounters a file with an
        unexpected name.
        r6   rT   r#   Nz	.bad-filer<   )r   r)   r   r
   r   r   r,   r   r*   r-   rB   rg   r   r   r   Ú test_listLogsWithBadlyNamedFiles<  s   ÿÿz-LogFileTests.test_listLogsWithBadlyNamedFilesc              	   C   s„   t  | j| j¡}|  |j¡ tddƒD ]"}t|j› d|› dƒ}| 	d¡ W d  ƒ n1 s0w   Y  q|  
ddg| ¡ ¡ dS )	zR
        L{LogFile.listLogs} ignores log files which rotated suffix is 0.
        r   r>   Ú.rT   r#   Nr<   r=   )r   r)   r   r
   r   r   Úranger,   r   r*   r-   rB   )r   r!   Úirh   r   r   r   Ú%test_listLogsIgnoresZeroSuffixedFilesK  s   ÿ€z2LogFileTests.test_listLogsIgnoresZeroSuffixedFilesN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r"   r1   rC   rK   rS   rV   r[   ra   ri   rp   rt   ry   r   r   rw   rx   r   r…   rŠ   r‹   r   r   r   r   r   r      s,    		+
	r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚRiggedDailyLogFileç        c                 C   s   t j | ¡ |  ¡ | _d S )N)r   ÚDailyLogFileÚ	_openFileÚtoDateÚlastDater   r   r   r   r—   \  s   zRiggedDailyLogFile._openFilec                 G   s*   |rt j|Ž d d… S t  | j¡d d… S )Nr>   )ÚtimeÚgmtimeÚ_clock)r   Úargsr   r   r   r˜   a  s   zRiggedDailyLogFile.toDateN)r   r‘   r’   rœ   r—   r˜   r   r   r   r   r”   Y  s    r”   c                   @   sp   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Ze	e
j ¡ dƒdd„ ƒZdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚDailyLogFileTestsz!
    Test rotating log file.
    c                 C   r   )Nztestdaily.logr   r   r   r   r   r   l  r   zDailyLogFileTests.setUpc                 C   sš   t  t| j| jƒ¡}| d¡ | d¡ | ¡  | d¡ W d  ƒ n1 s(w   Y  t| jƒ}|  	| 
¡ d¡ W d  ƒ dS 1 sFw   Y  dS )zO
        A daily log file can be written to like an ordinary log file.
        r#   r$   r%   Nr&   )r'   r(   r”   r   r
   r*   r+   r,   r   r-   r.   r/   r   r   r   r1   r  s   

ü"ÿzDailyLogFileTests.test_writingc                    s
  t ˆjˆjƒ‰ ˆ ˆ j¡ ‡ ‡fdd„tdƒD ƒ}dˆ _ˆ  d¡ dˆ _ˆ  d¡ dˆ _ˆ  d	¡ ˆ t	j
 |d
 ¡¡ ˆ t	j
 |d ¡¡ dˆ _ˆ  d¡ ˆ t	j
 |d
 ¡¡ ˆ t	j
 |d ¡¡ ˆ t	j
 |d ¡¡ dˆ _ˆ  d¡ ˆ t	j
 |d ¡¡ dS )z/
        Daily log files rotate daily.
        c                    s$   g | ]}ˆj d  ˆ  |d ¡ ‘qS )rŒ   é€Q )r   Úsuffix)Ú.0Úday©r!   r   r   r   Ú
<listcomp>…  s   $ z3DailyLogFileTests.test_rotation.<locals>.<listcomp>r>   r•   r#   iÀ¨  r4   rŸ   r5   r   r<   i £ r8   r=   iô r:   N)r”   r   r
   r   r   r   rœ   r*   r?   r   r   r   r@   )r   Údaysr   r£   r   rC     s&   




zDailyLogFileTests.test_rotationc                 C   s´   g d¢}t | j| jƒ}|  |j¡ |D ]}| |¡ q| ¡  | d¡}|  |j¡ |  || 	¡ ¡ |  
t|jd¡ d|_| ¡  | ¡  | d¡}|  |j¡ |  || 	¡ ¡ dS )zH
        Test retrieving log files with L{DailyLogFile.getLog}.
        )z1
z2
z3
r•   rŸ   iQ r   N)r”   r   r
   r   r   r*   r+   rO   r-   rN   r   rP   rœ   rA   )r   Údatar!   ÚdÚrr   r   r   Útest_getLog™  s    

zDailyLogFileTests.test_getLogc                 C   s†   t | j| jƒ}|  |j¡ |j› d| |j¡› }t|dƒ}| 	d¡ W d  ƒ n1 s.w   Y  |j
}| ¡  |  ||j
¡ dS )zu
        L{DailyLogFile.rotate} doesn't do anything if they new log file already
        exists on the disk.
        rŒ   rT   r#   N)r”   r   r
   r   r   r   r    r™   r,   r*   rH   rA   r-   )r   r!   ÚnewFilePathrh   ÚpreviousFiler   r   r   Útest_rotateAlreadyExists´  s   ÿz*DailyLogFileTests.test_rotateAlreadyExistszVMaking read-only directories on Windows is too complex for this test to reasonably do.c                 C   s\   t  | j| j¡}|  |j¡ t |jd¡ |  tj|jd¡ |j	}| 
¡  |  ||j	¡ dS )zƒ
        L{DailyLogFile.rotate} doesn't do anything if the directory containing
        the log files can't be written to.
        rv   ií  N)r   r–   r   r
   r   r   r   r   rˆ   rH   rA   r-   ©r   r!   r«   r   r   r   Ú#test_rotatePermissionDirectoryNotOkÅ  s   
z5DailyLogFileTests.test_rotatePermissionDirectoryNotOkc                 C   sJ   t  | j| j¡}|  |j¡ t |jd¡ |j	}| 
¡  |  ||j	¡ dS )zi
        L{DailyLogFile.rotate} doesn't do anything if the log file can't be
        written to.
        rv   N)r   r–   r   r
   r   r   r   r   r   rH   rA   r-   r­   r   r   r   Útest_rotatePermissionFileNotOkÙ  s   z0DailyLogFileTests.test_rotatePermissionFileNotOkc                 C   s<   t  | j| j¡}|  |j¡ t d¡}|  d| 	|¡¡ dS )z~
        Test that L{DailyLogFile.toDate} converts its timestamp argument to a
        time tuple (year, month, day).
        )	éÐ  r<   r<   r   r   r   r   r   r   )r°   r<   r<   N)
r   r–   r   r
   r   r   rš   Úmktimer-   r˜   )r   r!   Ú	timestampr   r   r   Útest_toDateæ  s   
zDailyLogFileTests.test_toDatec                    sR   ‡ fdd„}t  ˆ jˆ j¡}ˆ  |j¡ ˆ  td|¡ | ¡ }ˆ  	g d¢|¡ dS )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.
        c                     s   ˆ   d| ¡ ttddƒƒS )Nr   r   é	   )r-   Úlistr   )r   r   r   r   Úmock_localtimeþ  s   zADailyLogFileTests.test_toDateDefaultToday.<locals>.mock_localtimeÚ	localtime)r   r<   r=   N)
r   r–   r   r
   r   r   Úpatchrš   r˜   r-   )r   r¶   r!   ÚlogDater   r   r   Útest_toDateDefaultTodayñ  s   z)DailyLogFileTests.test_toDateDefaultTodayc                 C   sH   t  | j| j¡}|  |j¡ d}t |d ¡}| |¡}|  	||¡ dS )zc
        Test that L{DailyLogFile.toDate} uses its arguments to create a new
        date.
        )iÞ  r2   é   )r   r   r   r   r   r   N)
r   r–   r   r
   r   r   rš   r±   r˜   r-   )r   r!   ÚdateÚsecondsr¹   r   r   r   Ú#test_toDateUsesArgumentsToMakeADate	  s   
z5DailyLogFileTests.test_toDateUsesArgumentsToMakeADateN)r   r‘   r’   r“   r   r1   rC   r©   r¬   r   r   rw   rx   r®   r¯   r³   rº   r¾   r   r   r   r   rž   g  s     þ
rž   )r'   r‚   r   rX   rš   Úunittestr   Útwisted.pythonr   r   Útwisted.trial.unittestr   r   r–   r”   rž   r   r   r   r   Ú<module>   s     K