HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //proc/self/root/lib/python3/dist-packages/twisted/test/__pycache__/test_logfile.cpython-38.pyc
U


W[3G�@s�ddlmZmZddlZddlZddlZddlZddlZddlm	Z	ddl
mZmZGdd�de	j
�ZGdd�dej�ZGd	d
�d
e	j
�ZdS)�)�division�absolute_importN)�unittest)�logfile�runtimec@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zej��r�d e_d!d"�Zd#d$�Zd%d&�Zd'd(�Zd)S)*�LogFileTestsz%
    Test the rotating log file.
    cCs4|��|_t�|j�d|_tj�|j|j�|_dS)Nztest.log�Zmktemp�dir�os�makedirs�name�path�join��self�r�;/usr/lib/python3/dist-packages/twisted/test/test_logfile.py�setUps
zLogFileTests.setUpcCs.t�|jd�tj�|j�r*t�|jd�dS)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
�existsrrrr�tearDownszLogFileTests.tearDowncCs.t�|j|j�}|�|j�|�t|j�dS)zf
        L{BaseLogFile.shouldRotate} is abstract and must be implemented by
        subclass.
        N)	rZBaseLogFilerr	�
addCleanup�close�assertRaises�NotImplementedErrorZshouldRotate�r�logrrr�test_abstractShouldRotate%sz&LogFileTests.test_abstractShouldRotatec	Csrt�t�|j|j���,}|�d�|�d�|��|�d�W5QRXt|j	��}|�
|��d�W5QRXdS)zn
        Log files can be written to, flushed and closed. Closing a log file
        also flushes it.
        �123�456�7890�
1234567890N)�
contextlib�closingr�LogFilerr	�write�flush�openr
�assertEqual�read�rr�frrr�test_writing/s

zLogFileTests.test_writingc	CsLt�tj|j|jdd����}|�d�|�d�|�d�|�tj	�
d�|j	���|�tj	�
d�|j	���|�d�|�tj	�
d�|j	���|�tj	�
d�|j	���|�tj	�
d	�|j	���|�d
�|�tj	�
d	�|j	���|�
�|�tj	�
d	�|j	���|�tj	�
d�|j	���W5QRX|�|��dd
dg�dS)zq
        Rotating log files autorotate after a period of time, and can also be
        manually rotated.
        �
)�rotateLengthr�4567890�11111111111�{0}.1�{0}.2��{0}.3�3�{0}.4���N)r#r$rr%rr	r&�
assertTruer
r
r�format�assertFalse�rotater)�listLogsrrrr�
test_rotation>s&�




$zLogFileTests.test_rotationc	Cs�t�t�|j|j���}|�d�W5QRXt�|j|j�}|�|j�|�	|j
d�|�	|j��|j
�|�d�|�	|j
d�|�	|j��|j
�|j}|�
dd�|�	|��d�dS)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.
        �
0123456789r.�abc�
rs
0123456789abcN)r#r$rr%rr	r&rrr)�size�_file�tell�seekr*r+rrr�test_append\s
zLogFileTests.test_appendc	Cs�t�|j|j�}|�|j�|�d�|�d�|��|�d�|��|�	|�
�dg�t�|�
���4}|j�d�|�	|��dg�|�	|��g�W5QRXt�|�d���*}|�	|��ddg�|�	|��g�W5QRX|�t|jd�|�t|jd�|��|�	|�
�ddg�t�|�d���4}|j�d�|�	|��dg�|�	|��g�W5QRXt�|�d���*}|�	|��ddg�|�	|��g�W5QRXdS)	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
r8rr9�1N)rr%rr	rrr&r>r'r)r?r#r$Z
getCurrentLogrErG�	readLines�getLogr�
ValueError�	TypeError)rr�readerrrr�test_logReaderrs4



zLogFileTests.test_logReaderc	CsFt|jd��W5QRXt�|j�}|�|j�|�g|�d��dS)zB
        L{LogReader.readLines} supports reading no line.
        �wrN)r(r
rZ	LogReaderrrr)rJ)rrNrrr�test_LogReaderReadsZeroLine�s

z(LogFileTests.test_LogReaderReadsZeroLinecCs|t|jd���t�|jd�t�|j�tj}t�|j	|j
�}|�|j�|�d�|�
�|�|t�|j�tj�dS)zH
        Check rotated files have same permissions as original.
        rP��rBN)r(r
rr
r�stat�ST_MODErr%rr	rr&r>r))r�moderrrr�test_modePreservation�s
z"LogFileTests.test_modePreservationc	Cs�t�|j|j�}|�|j�|�d�t�|jd�zt	tj
�|jd�d�}Wntt
fk
rfYnX|��dS|��|�d�|��|j}|�|��d�|�dd�|�|��d	�dS)
zH
        Check it keeps working when permission on dir changes.
        rB�mZxxxrPN�def�rsabcdef)rr%rr	rrr&r
rr(r
r�OSError�IOErrorr>r'rEr)rFrGr*r+rrr�test_noPermission�s"

zLogFileTests.test_noPermissionc	Cstj|j|jddd�}|�|j�|�d�|�d�|�tj	�
d�|j	���|�d�|�tj	�
d�|j	���|�d	�|�tj	�
d
�|j	���td
�|j	���}|�
|��d�W5QRX|�d�td
�|j	���}|�
|��d�W5QRX|�tj	�
d�|j	���d
S)zl
        Test it respect the limit on the number of files when maxRotatedFiles
        is not None.
        r.r:)r/ZmaxRotatedFilesr1Z22222222222r2Z33333333333r3Z44444444444r5Z55555555555r7N)rr%rr	rrr&r;r
r
rr<r(r)r*r=�rr�fprrr�test_maxNumberOfLog�s"�




z LogFileTests.test_maxNumberOfLogcCs�tj|j|jddd�}|�|j�tjj|jddd�}|�|j�|�|j|j�|�t	j�
|j�|j�|�|j|j�|�|j|j�dS)z/
        Test the fromFullPath method.
        r.r��defaultModeN)
rr%rr	rrZfromFullPathr
r)r
�abspathr/ra)r�log1Zlog2rrr�test_fromFullPath�szLogFileTests.test_fromFullPathc	Cs~t|jd��,t�|jd�t�t�|j�tj�}W5QRXt�|j	|j
�}|�t�t�|j�tj�|�|�|j
�dS)zx
        Test the default permission of the log file: if the file exist, it
        should keep the permission.
        �wbrRN)r(r
r
rrS�S_IMODErTrr%rr	r)rr)rZcurrentModercrrr�test_defaultPermissionss"�z$LogFileTests.test_defaultPermissionscCs`tj|j|jdd�}|�|j�t�t�|j	�tj
�}tj�
�rP|�|d�n|�|d�dS)zG
        Test specifying the permissions used on the log file.
        �6r`�$N)rr%rr	rrrSrfr
r
rTr�platform�	isWindowsr))rrcrUrrr�test_specifiedPermissionss
z&LogFileTests.test_specifiedPermissionsc	Cs�t�t�|j|j���@}|�d�tj�	|jd�}t�
|j|�|��|�d�W5QRXt|j��}|�
|��d�W5QRXt|��}|�
|��d�W5QRXdS)z�
        L{logfile.LogFile.reopen} allows to rename the currently used file and
        make L{logfile.LogFile} create a new file.
        Zhello1zsave.logZhello2N)r#r$rr%rr	r&r
r
r�renameZreopenr(r)r*)rrcZsavePathr,rrr�test_reopens

zLogFileTests.test_reopenzCan't test reopen on WindowscCs(|�ttj|jd�}|�|jtj�dS)zR
        Specifying an invalid directory to L{LogFile} raises C{IOError}.
        Zthis_dir_does_not_existN)rr[rr%rr)�errnoZENOENT)r�errr�test_nonExistentDir2s�z LogFileTests.test_nonExistentDircCs`tj��rd\}}d}nd\}}d}tj||dd�}|�|j�|�|j|�|�|j	d�dS)z�
        Opening a L{LogFile} which can be read and write but whose mode can't
        be changed doesn't trigger an error.
        )�NULr4rr)Znullz/devz	/dev/nullrWr`N)
rrjrkrr%rrr)r
ra)rr�	directoryZexpectedPathrrrr�test_cantChangeFileMode;s
z$LogFileTests.test_cantChangeFileModec	Cs�t�|j|j�}|�|j�td�|j�d��}|�	d�W5QRXtd�|j�d��}|�	d�W5QRX|�
dg|���dS)zl
        L{LogFile.listLogs} doesn't choke if it encounters a file with an
        unexpected name.
        r2rPrz{0}.bad-filer8N)rr%rr	rrr(r<r
r&r)r?r]rrr� test_listLogsWithBadlyNamedFilesNsz-LogFileTests.test_listLogsWithBadlyNamedFilesc
Csnt�|j|j�}|�|j�tdd�D].}td�|j	|�d��}|�
d�W5QRXq&|�ddg|���dS)	zR
        L{LogFile.listLogs} ignores log files which rotated suffix is 0.
        rr:�{0}.{1}rPrr8r9N)
rr%rr	rr�ranger(r<r
r&r)r?)rr�ir^rrr�%test_listLogsIgnoresZeroSuffixedFiles^sz2LogFileTests.test_listLogsIgnoresZeroSuffixedFilesN)�__name__�
__module__�__qualname__�__doc__rrrr-r@rHrOrQrVr\r_rdrgrlrnrrjrk�skiprqrtruryrrrrrs,

,
 
	rc@s eZdZdZdd�Zdd�ZdS)�RiggedDailyLogFile�cCstj�|�|��|_dS)N)r�DailyLogFile�	_openFile�toDate�lastDaterrrrr�pszRiggedDailyLogFile._openFilecGs*|rtj|�dd�St�|j�dd�S)Nr:)�time�gmtime�_clock)r�argsrrrr�vszRiggedDailyLogFile.toDateN)rzr{r|r�r�r�rrrrrmsrc@speZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	e
j��rLde	_
dd�Zdd�Zdd�Zdd�ZdS)�DailyLogFileTestsz!
    Test rotating log file.
    cCs4|��|_t�|j�d|_tj�|j|j�|_dS)Nz
testdaily.logrrrrrr�s
zDailyLogFileTests.setUpc	Cspt�t|j|j���,}|�d�|�d�|��|�d�W5QRXt|j��}|�	|�
�d�W5QRXdS)zO
        A daily log file can be written to like an ordinary log file.
        rr r!r"N)r#r$rrr	r&r'r(r
r)r*r+rrrr-�s

zDailyLogFileTests.test_writingcs
t�j�j�����j���fdd�td�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��dS)z/
        Daily log files rotate daily.
        cs$g|]}�jd��|d��qS)�.�Q)r
�suffix)�.0Zday�rrrr�
<listcomp>�sz3DailyLogFileTests.test_rotation.<locals>.<listcomp>r:r�ri��r0r�r1rr8i�r4r9i�r6N)
rrr	rrrwr�r&r;r
r
rr=)rZdaysrr�rr@�s&




zDailyLogFileTests.test_rotationcCs�dddg}t|j|j�}|�|j�|D]}|�|�q(|��|�d�}|�|j�|�||�	��|�
t|jd�d|_|��|�
�|�d�}|�|j�|�||�	��dS)	zH
        Test retrieving log files with L{DailyLogFile.getLog}.
        z1
z2
z3
r�r�i�QrN)rrr	rrr&r'rKr)rJrrLr�r>)r�datar�d�rrrr�test_getLog�s 


zDailyLogFileTests.test_getLogc	Cspt|j|j�}|�|j�d�|j|�|j��}t	|d��}|�
d�W5QRX|j}|��|�
||j�dS)zu
        L{DailyLogFile.rotate} doesn't do anything if they new log file already
        exists on the disk.
        rvrPrN)rrr	rrr<r
r�r�r(r&rEr>r))rrZnewFilePathr^�previousFilerrr�test_rotateAlreadyExists�sz*DailyLogFileTests.test_rotateAlreadyExistscCs\t�|j|j�}|�|j�t�|jd�|�tj|jd�|j	}|�
�|�||j	�dS)z�
        L{DailyLogFile.rotate} doesn't do anything if the directory containing
        the log files can't be written to.
        rii�N)rr�rr	rrr
rrsrEr>r)�rrr�rrr�#test_rotatePermissionDirectoryNotOk�sz5DailyLogFileTests.test_rotatePermissionDirectoryNotOkzVMaking read-only directories on Windows is too complex for this test to reasonably do.cCsJt�|j|j�}|�|j�t�|jd�|j	}|�
�|�||j	�dS)zi
        L{DailyLogFile.rotate} doesn't do anything if the log file can't be
        written to.
        riN)rr�rr	rrr
rr
rEr>r)r�rrr�test_rotatePermissionFileNotOk�sz0DailyLogFileTests.test_rotatePermissionFileNotOkcCs<t�|j|j�}|�|j�t�d�}|�d|�	|��dS)z~
        Test that L{DailyLogFile.toDate} converts its timestamp argument to a
        time tuple (year, month, day).
        )	��r8r8rrrrrr)r�r8r8N)
rr�rr	rrr��mktimer)r�)rrZ	timestamprrr�test_toDates
zDailyLogFileTests.test_toDatecsT�fdd�}t��j�j�}��|j���td|�|��}��	dddg|�dS)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.
        cs��d|�ttdd��S)Nrr�	)r)�listrw)r�rrr�mock_localtimeszADailyLogFileTests.test_toDateDefaultToday.<locals>.mock_localtime�	localtimerr8r9N)
rr�rr	rrZpatchr�r�r))rr�r�logDaterrr�test_toDateDefaultTodaysz)DailyLogFileTests.test_toDateDefaultTodaycCsHt�|j|j�}|�|j�d}t�|d�}|�|�}|�	||�dS)zc
        Test that L{DailyLogFile.toDate} uses its arguments to create a new
        date.
        )i�r.�)rrrrrrN)
rr�rr	rrr�r�r�r))rr�dateZsecondsr�rrr�#test_toDateUsesArgumentsToMakeADate&s
z5DailyLogFileTests.test_toDateUsesArgumentsToMakeADateN)rzr{r|r}rr-r@r�r�r�rrjrkr~r�r�r�r�rrrrr�}s
�r�)Z
__future__rrr#ror
rSr�Z
twisted.trialrZtwisted.pythonrrZTestCaserr�rr�rrrr�<module>s_