
    VhG                        d dl mZ 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e      Z G d d	e
j                        Z G d
 de      Zy)    )annotationsN)skipIf)logfileruntime)TestCasec                      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(                  j+                         d      dd       ZddZddZddZddZy)LogFileTestsz%
    Test the rotating log file.
    c                    | j                         | _        t        j                  | j                         d| _        t        j
                  j                  | j                  | j                        | _        y )Nztest.logmktempdirosmakedirsnamepathjoinselfs    I/home/dcms/DCMS/lib/python3.12/site-packages/twisted/test/test_logfile.pysetUpzLogFileTests.setUp   sD    ;;=
DHH	GGLL4995	    c                    t        j                  | j                  d       t         j                  j	                  | j                        r!t        j                  | j                  d       yy)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   s    r   tearDownzLogFileTests.tearDown   sA    
 	5!77>>$))$HHTYY& %r   c                    t        j                  | j                  | j                        }| j	                  |j
                         | j                  t        |j                         y)zf
        L{BaseLogFile.shouldRotate} is abstract and must be implemented by
        subclass.
        N)	r   BaseLogFiler   r   
addCleanupcloseassertRaisesNotImplementedErrorshouldRotater   logs     r   test_abstractShouldRotatez&LogFileTests.test_abstractShouldRotate$   sE    
 !!$))TXX6		"-s/?/?@r   c                   t        j                  t        j                  | j                  | j
                              5 }|j                  d       |j                  d       |j                          |j                  d       ddd       t        | j                        5 }| j                  |j                         d       ddd       y# 1 sw Y   HxY w# 1 sw Y   yxY w)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%   fs      r   test_writingzLogFileTests.test_writing-   s    
 		488 DE 	IIeIIeIIKIIf		 $))_ 	5QVVX|4	5 	5	 		5 	5s   AC	!C	CCc                j   t        j                  t        j                  | j                  | j
                  d            5 }|j                  d       |j                  d       |j                  d       | j                  t        j                  j                  | j                   d             | j                  t        j                  j                  | j                   d             |j                  d       | j                  t        j                  j                  | j                   d             | j                  t        j                  j                  | j                   d             | j                  t        j                  j                  | j                   d	             |j                  d
       | j                  t        j                  j                  | j                   d	             |j                          | j                  t        j                  j                  | j                   d	             | j                  t        j                  j                  | j                   d             ddd       | j                  j                         g d       y# 1 sw Y   ,xY w)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rotater2   listLogsr$   s     r   test_rotationzLogFileTests.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))J2c                l   t        j                  t        j                  | j                  | j
                              5 }|j                  d       ddd       t        j                  | j                  | j
                        }| j                  |j                         | j                  |j                  d       | j                  |j                  j                         |j                         |j                  d       |j                  d       d}| j                  |j                  t        |             | j                  |j                  j                         |j                         |j                  }|j                  dd       | j                  |j                         |       y# 1 sw Y   [xY w)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.
        
0123456789Nr8   abcs   defs   0123456789abcdefr   )r,   r-   r   r.   r   r   r/   r   r    r2   size_filetelllenseekr3   )r   r%   expectResultr5   s       r   test_appendzLogFileTests.test_appendX   s&    		488 DE 	$IIl#	$ oodii2		"2&)3884		%		*.3|#45)3884II	q!<0	$ 	$s   F))F3c                   t        j                  | j                  | j                        }| j	                  |j
                         |j                  d       |j                  d       |j                          |j                  d       |j                          | j                  |j                         dg       t        j                  |j                               5 }|j                  j                  d       | j                  |j!                         dg       | j                  |j!                         g        ddd       t        j                  |j#                  d            5 }| j                  |j!                         ddg       | j                  |j!                         g        ddd       | j%                  t&        |j"                  d       | j%                  t(        |j"                  d       |j                          | j                  |j                         ddg       t        j                  |j#                  d            5 }|j                  j                  d       | j                  |j!                         dg       | j                  |j!                         g        ddd       t        j                  |j#                  d            5 }| j                  |j!                         ddg       | j                  |j!                         g        ddd       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   yxY w)	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
rB   r   NrC   1)r   r.   r   r   r   r    r/   rG   r0   r2   rH   r,   r-   getCurrentLogrN   rQ   	readLinesgetLogr!   
ValueError	TypeError)r   r%   readers      r   test_logReaderzLogFileTests.test_logReadero   s    oodii2		"		'		'

		'		 	!- 1 1 34 	5LLa V--/';V--/4	5 

1. 	5&V--/'71CDV--/4	5
 	*cjj!4)SZZ5 	

!Q0

1. 	5&LLa V--/';V--/4	5 

1. 	5&V--/'71CDV--/4	5 	5'	5 	5	5 	5	5 	5	5 	5s4   AL&AL+	AL8AML(+L58MMc                   t        | j                  d      5  	 ddd       t        j                  | j                        }| j	                  |j
                         | j                  g |j                  d             y# 1 sw Y   exY w)zB
        L{LogReader.readLines} supports reading no line.
        wNr   )r1   r   r   	LogReaderr   r    r2   rW   )r   r[   s     r   test_LogReaderReadsZeroLinez(LogFileTests.test_LogReaderReadsZeroLine   sk    
 $))S! 		 ""499-%V--a01	 	s   A==Bc                8   t        | j                  d      j                          t        j                  | j                  d       t        j
                  | j                        t
        j                     }t        j                  | j                  | j                        }| j                  |j                         |j                  d       |j                          | j                  |t        j
                  | j                        t
        j                            y)zH
        Check rotated files have same permissions as original.
        r^     rL   N)r1   r   r    r   r   statST_MODEr   r.   r   r   r   r/   rG   r2   )r   moder%   s      r   test_modePreservationz"LogFileTests.test_modePreservation   s     	TYY""$
E"wwtyy!$,,/oodii2		"		%

rwwtyy1$,,?@r   c                   t        j                  | j                  | j                        }| j	                  |j
                         |j                  d       t        j                  | j                  d       	 t        t        j                  j                  | j                  d      d      }|j                          y# t        $ r Y nw xY w|j                          |j                  d       |j                          |j                  }| j!                  |j#                         d       |j%                  dd       | j!                  |j'                         d	       y)
zH
        Check it keeps working when permission on dir changes.
        rL   m  xxxr^   Ndef   r   s   abcdef)r   r.   r   r   r   r    r/   r   r   r1   r   r   OSErrorrG   r0   rN   r2   rO   rQ   r3   r4   s      r   test_noPermissionzLogFileTests.test_noPermission   s     oodii2		"		% 	5!	RWW\\$((E2C8A GGI	  		 	

		%		II1%	q!9-s   84B= =	C	C	c                J   t        j                  | j                  | j                  dd      }| j	                  |j
                         |j                  d       |j                  d       | j                  t        j                  j                  | j                   d             |j                  d       | j                  t        j                  j                  | j                   d             |j                  d	       | j                  t        j                  j                  | j                   d
             t        | j                   d
      5 }| j                  |j                         d       ddd       |j                  d       t        | j                   d
      5 }| j                  |j                         d       ddd       | j                  t        j                  j                  | j                   d             y# 1 sw Y   xY w# 1 sw Y   QxY w)zl
        Test it respect the limit on the number of files when maxRotatedFiles
        is not None.
        r8   rD   )r9   maxRotatedFilesr;   22222222222r<   33333333333r=   44444444444r?   N55555555555rA   )r   r.   r   r   r   r    r/   rE   r   r   r   r1   r2   r3   rF   r   r%   fps      r   test_maxNumberOfLogz LogFileTests.test_maxNumberOfLog   s   
 oodiiTUV		"		(		($))B'789		($))B'789		($))B'789TYYKr"# 	2rRWWY1	2 			(TYYKr"# 	2rRWWY1	2499+R(89:	2 	2	2 	2s   !H(!HHH"c                   t        j                  | j                  | j                  dd      }| j	                  |j
                         t         j                  j                  | j                  dd      }| j	                  |j
                         | j                  |j                  |j                         | j                  t        j                  j                  |j                        |j                         | j                  |j                  |j                         | j                  |j                  |j                         y)z/
        Test the fromFullPath method.
        r8   r   defaultModeN)r   r.   r   r   r   r    fromFullPathr   r2   r   abspathr9   ry   )r   log1log2s      r   test_fromFullPathzLogFileTests.test_fromFullPath   s     tyy$((BEJ

#++DIIru+M

#DII.3TYY?**D,=,=>))4+;+;<r   c                P   t        | j                  d      5  t        j                  | j                  d       t	        j
                  t        j                  | j                        t        j                           }ddd       t        j                  | j                  | j                        }| j                  t	        j
                  t        j                  | j                        t        j                                  | j                  |j                         y# 1 sw Y   xY w)zx
        Test the default permission of the log file: if the file exist, it
        should keep the permission.
        wbrb   N)r1   r   r   r   rc   S_IMODErd   r   r.   r   r   r2   r   r    )r   currentModer|   s      r   test_defaultPermissionsz$LogFileTests.test_defaultPermissions   s    
 $))T" 	IHHTYY&,,rwwtyy'9$,,'GHK	I tyy$((3bggdii&8&FGU

#	I 	Is   A$DD%c                   t        j                  | j                  | j                  d      }| j	                  |j
                         t        j                  t        j                  | j                        t        j                           }t        j                  j                         r| j                  |d       y| j                  |d       y)zG
        Test specifying the permissions used on the log file.
        6   rx   $  N)r   r.   r   r   r   r    rc   r   r   r   rd   r   platform	isWindowsr2   )r   r|   re   s      r   test_specifiedPermissionsz&LogFileTests.test_specifiedPermissions  s     tyy$((F

#||BGGDII.t||<=%%'T5)T5)r   zCan't test reopen on Windowsc                   t        j                  t        j                  | j                  | j
                              5 }|j                  d       t        j                  j                  | j
                  d      }t        j                  | j                  |       |j                          |j                  d       ddd       t        | j                        5 }| j                  |j                         d       ddd       t              5 }| j                  |j                         d       ddd       y# 1 sw Y   |xY w# 1 sw Y   JxY w# 1 sw Y   yxY w)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reopenr1   r2   r3   )r   r|   savePathr5   s       r   test_reopenzLogFileTests.test_reopen  s     		488 DE 	!JJx ww||DHHj9HIIdii*KKMJJx 	! $))_ 	1QVVXx0	1(^ 	1qQVVXx0	1 	1	! 	!	1 	1	1 	1s$   A=D6!E!E6D?EEc                    | j                  t        t        j                  | j                  d      }| j                  |j                  t        j                         y)zR
        Specifying an invalid directory to L{LogFile} raises C{IOError}.
        this_dir_does_not_existN)r!   IOErrorr   r.   r   r2   errnoENOENT)r   es     r   test_nonExistentDirz LogFileTests.test_nonExistentDir"  s@     W__dii1J
 	%,,/r   c                4   t         j                  j                         rd\  }}d}nd\  }}d}t        j                  ||d      }| j                  |j                         | j                  |j                  |       | j                  |j                  d       y)z
        Opening a L{LogFile} which can be read and write but whose mode can't
        be changed doesn't trigger an error.
        )NULr>   r   )nullz/devz	/dev/nullrh   rx   N)
r   r   r   r   r.   r   r    r2   r   ry   )r   r   	directoryexpectedPathr%   s        r   test_cantChangeFileModez$LogFileTests.test_cantChangeFileMode+  s|    
 %%''OD) L,OD)&LoodI5A		"<0%0r   c                   t        j                  | j                  | j                        }| j	                  |j
                         t        |j                   dd      5 }|j                  d       ddd       t        |j                   dd      5 }|j                  d       ddd       | j                  dg|j                                y# 1 sw Y   ^xY w# 1 sw Y   7xY w)zl
        L{LogFile.listLogs} doesn't choke if it encounters a file with an
        unexpected name.
        r<   r^   r(   Nz	.bad-filerB   )r   r.   r   r   r   r    r1   r   r/   r2   rH   rt   s      r    test_listLogsWithBadlyNamedFilesz-LogFileTests.test_listLogsWithBadlyNamedFiles=  s    
 oodii2		"SXXJb/3' 	2HHUO	SXXJi(#. 	"HHUO	 	!clln-	 		 	s   CCCC#c                v   t        j                  | j                  | j                        }| j	                  |j
                         t        dd      D ]7  }t        |j                   d| d      5 }|j                  d       ddd       9 | j                  ddg|j                                y# 1 sw Y   fxY w)	zR
        L{LogFile.listLogs} ignores log files which rotated suffix is 0.
        r   rD   .r^   r(   NrB   rC   )r   r.   r   r   r   r    ranger1   r   r/   r2   rH   )r   r%   iru   s       r   %test_listLogsIgnoresZeroSuffixedFilesz2LogFileTests.test_listLogsIgnoresZeroSuffixedFilesL  s     oodii2		"q! 	 A
!A3'-     	  	!Q0   s   0B//B8	NreturnNone)__name__
__module____qualname____doc__r   r   r&   r6   rI   rS   r\   r`   rf   rm   rv   r~   r   r   r   r   r   r   r   r   r   r   r    r   r   r	   r	      s    6'A54:1.)5V
2A.>;0=
$* G&&(*HI1 J1"01$.1r   r	   c                       e Zd ZdZddZddZy)RiggedDailyLogFile        c                l    t         j                  j                  |        | j                         | _        y )N)r   DailyLogFile	_openFiletoDatelastDater   s    r   r   zRiggedDailyLogFile._openFile]  s#    &&t,r   c                t    |rt        j                  | d d S t        j                  | j                        d d S )NrD   )timegmtime_clock)r   argss     r   r   zRiggedDailyLogFile.toDateb  s5    ;;%bq)){{4;;'++r   Nr   )r   floatr   ztuple[int, int, int])r   r   r   r   r   r   r   r   r   r   r   Z  s    F&
,r   r   c                      e Zd ZdZddZddZddZddZddZ e	e
j                  j                         d      dd       Zdd	Zdd
ZddZddZy)DailyLogFileTestsz!
    Test rotating log file.
    c                    | j                         | _        t        j                  | j                         d| _        t        j
                  j                  | j                  | j                        | _        y )Nztestdaily.logr   r   s    r   r   zDailyLogFileTests.setUpm  sD    ;;=
DHH#	GGLL4995	r   c                   t        j                  t        | j                  | j                              5 }|j                  d       |j                  d       |j                          |j                  d       ddd       t        | j                        5 }| j                  |j                         d       ddd       y# 1 sw Y   HxY w# 1 sw Y   yxY w)zO
        A daily log file can be written to like an ordinary log file.
        r(   r)   r*   Nr+   )r,   r-   r   r   r   r/   r0   r1   r   r2   r3   r4   s      r   r6   zDailyLogFileTests.test_writings  s      2499dhh GH 	CIIeIIeIIKIIf		 $))_ 	5QVVX|4	5 	5	 		5 	5s   AB?!C?CCc                4   t        | j                  | j                        }| j                  |j                         t        d      D cg c]&  }| j                  dz   |j                  |dz        z   ( }}d|_        |j                  d       d|_        |j                  d       d|_        |j                  d       | j                  t        j                  j                  |d	                | j                  t        j                  j                  |d
                d|_        |j                  d       | j                  t        j                  j                  |d	                | j                  t        j                  j                  |d
                | j                  t        j                  j                  |d                d|_        |j                  d       | j                  t        j                  j                  |d                yc c}w )z/
        Daily log files rotate daily.
        rD   r   Q r   r(   i  r:   r;   r   rB   i  r>   rC   i r@   N)r   r   r   r   r    r   r   suffixr   r/   rE   r   r   rF   )r   r%   daydayss       r   rI   zDailyLogFileTests.test_rotation  sr    !DHH5		"GLQxPS3::cEk#::PP 
		%
		)
		(tAw/0Q01
		"tAw/0tAw/0Q01
		#Q01% Qs   	+Hc                   g d}t        | j                  | j                        }| j                  |j                         |D ]  }|j                  |        |j                          |j                  d      }| j                  |j                         | j                  ||j                                | j                  t        |j                  d       d|_        |j	                          |j                          |j                  d      }| j                  |j                         | j                  ||j                                y)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/   r0   rX   r2   rW   r!   rY   r   rG   )r   datar%   drs        r   test_getLogzDailyLogFileTests.test_getLog  s     % DHH5		" 	AIIaL			 JJsO q{{}- 	*cjj%8
		

JJqM q{{}-r   c                   t        | j                  | j                        }| j                  |j                         |j
                   d|j                  |j                         }t        |d      5 }|j                  d       ddd       |j                  }|j                          | j                  ||j                         y# 1 sw Y   BxY w)zu
        L{DailyLogFile.rotate} doesn't do anything if they new log file already
        exists on the disk.
        r   r^   r(   N)r   r   r   r   r    r   r   r   r1   r/   rN   rG   r2   )r   r%   newFilePathru   previousFiles        r   test_rotateAlreadyExistsz*DailyLogFileTests.test_rotateAlreadyExists  s    
 !DHH5		" 
!CJJs||$<#=>+s# 	rHHUO	yy

syy1		 	s   2CCzVMaking read-only directories on Windows is too complex for this test to reasonably do.c                   t        j                  | j                  | j                        }| j	                  |j
                         t        j                  |j                  d       | j	                  t        j                  |j                  d       |j                  }|j                          | j                  ||j                         y)z
        L{DailyLogFile.rotate} doesn't do anything if the directory containing
        the log files can't be written to.
        r   i  N)r   r   r   r   r   r    r   r   r   rN   rG   r2   r   r%   r   s      r   #test_rotatePermissionDirectoryNotOkz5DailyLogFileTests.test_rotatePermissionDirectoryNotOk  s~     ""499dhh7		"
&#--7yy

syy1r   c                >   t        j                  | j                  | j                        }| j	                  |j
                         t        j                  |j                  d       |j                  }|j                          | j                  ||j                         y)zi
        L{DailyLogFile.rotate} doesn't do anything if the log file can't be
        written to.
        r   N)r   r   r   r   r   r    r   r   r   rN   rG   r2   r   s      r   test_rotatePermissionFileNotOkz0DailyLogFileTests.test_rotatePermissionFileNotOk  sf    
 ""499dhh7		"
5!yy

syy1r   c                    t        j                  | j                  | j                        }| j	                  |j
                         t        j                  d      }| j                  d|j                  |             y)z~
        Test that L{DailyLogFile.toDate} converts its timestamp argument to a
        time tuple (year, month, day).
        )	  rB   rB   r   r   r   r   r   r   )r   rB   rB   N)
r   r   r   r   r   r    r   mktimer2   r   )r   r%   	timestamps      r   test_toDatezDailyLogFileTests.test_toDate  sV    
 ""499dhh7		"KK >?	szz)'<=r   c                    d fd}t        j                   j                   j                        } j	                  |j
                          j                  t        d|       |j                         } j                  g d|       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.
        c                 R    j                  d|        t        t        dd            S )Nr   r   	   )r2   listr   )r   r   s    r   mock_localtimezADailyLogFileTests.test_toDateDefaultToday.<locals>.mock_localtime  s$    R&a$$r   	localtime)r   rB   rC   N)r   objectr   z	list[int])
r   r   r   r   r   r    patchr   r   r2   )r   r   r%   logDates   `   r   test_toDateDefaultTodayz)DailyLogFileTests.test_toDateDefaultToday  s_    	% ""499dhh7		"

4n5**,G,r   c                   t        j                  | j                  | j                        }| j	                  |j
                         d}t        j                  |dz         }|j                  |      }| j                  ||       y)zc
        Test that L{DailyLogFile.toDate} uses its arguments to create a new
        date.
        )i  r8      )r   r   r   r   r   r   N)
r   r   r   r   r   r    r   r   r   r2   )r   r%   datesecondsr   s        r   #test_toDateUsesArgumentsToMakeADatez5DailyLogFileTests.test_toDateUsesArgumentsToMakeADate
  sc    
 ""499dhh7		"++dXo.**W%w'r   Nr   )r   r   r   r   r   r6   rI   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   h  sd    6524.62" ""$	!
2
22	>-0(r   r   )
__future__r   r,   r   r   rc   r   unittestr   twisted.pythonr   r   twisted.trial.unittestr   r	   r   r   r   r   r   r   <module>r      sQ    #   	    + +G18 G1T
,-- ,n( n(r   