Ϫf]2|dZddlZddlmZddlmZmZddlm Z m Z m Z ddl m Z ddlmZddlZddlmZdd lmZdd lmZd d lmZd dlmZmZmZmZmZmZmZde de fde de ffdZGddej@jBjDZ#Gddej@jBjDZ$y)z3 Tests for L{twisted.application.runner._pidfile}. N)wraps)getpidname)AnyCallableOptional) verifyObject)NoReturn)FilePath)platform)SkipTest)_pidfile)AlreadyRunningErrorInvalidPIDFileErrorIPIDFile NonePIDFile NoPIDFoundPIDFileStalePIDFileErrorf.returnc `tdtdtdtdtffd }|S)a= Decorator for tests that are not expected to work on all platforms. Calling L{PIDFile.isRunning} currently raises L{NotImplementedError} on non-POSIX platforms. On an unsupported platform, we expect to see any test that calls L{PIDFile.isRunning} to raise either L{NotImplementedError}, L{SkipTest}, or C{self.failureException}. (C{self.failureException} may occur in a test that checks for a specific exception but it gets NotImplementedError instead.) @param f: The test method to decorate. @return: The wrapped callable. selfargskwargsrctjdk(}|r |g|i|S|jtt|j f|g|i|}t |tr*|jt|jdyy)Nposixz isRunning is not implemented on ) r getType assertRaisesNotImplementedErrorr failureException isinstance assertTruestr startswith)rrr supporteders N/usr/lib/python3/dist-packages/twisted/application/runner/test/test_pidfile.pywrapperz$ifPlatformSupported..wrapper3s$$&'1 T+D+F+ +!!!$h0E0EF   A!01A 1 12T UV2)rr)rr+s` r*ifPlatformSupportedr-!sD$ 1XWcW#WWWW Nr,c&eZdZdZddeedeefdZddZ ddZ ddZ dd Z dd Z dd Zdd Zdd ZddZddZddZeddZeddZeddZeddZeddZeddZddZddZeddZeddZy) PIDFileTestsz Tests for L{PIDFile}. Ncontentrc^t|j}||j||SN)r mktemp setContent)rr0filePaths r*r5zPIDFileTests.filePathLs+DKKM*      (r,cVt|j}tt|y)z5 L{PIDFile} conforms to L{IPIDFile}. N)rr5r rrpidFiles r*test_interfacezPIDFileTests.test_interfaceRs$--/*Xw'r,cP|jtjddy)zR L{PIDFile._format} returns the expected format when given a PID. 9pids1337 N) assertEqualr_format)rs r*test_formatWithPIDzPIDFileTests.test_formatWithPIDYs T2I>r,cd}t|jtj|}|j||j y)zK L{PIDFile.read} returns the PID from the given file path. r;r<N)rr5r?r>readrr=r8s r*test_readWithPIDzPIDFileTests.test_readWithPID_s;$--C(@AB glln-r,cd}t|jd}|jt|j}|j t |d|y)f L{PIDFile.read} raises L{InvalidPIDFileError} when given an empty file path. r,#non-integer PID value in PID file: Nrr5r!rrBr>r&rpidValuer8r)s r*test_readEmptyPIDzPIDFileTests.test_readEmptyPIDisO $--,-   17<< @ Q#Fxl!STr,cd}t|j|}|jt|j}|j t |d|y)rFs$foo!rGNrHrIs r*test_readWithBogusPIDz"PIDFileTests.test_readWithBogusPIDtsO $--12   17<< @ Q#Fxl!STr,ct|j}|jt|j}|j t |dy)zc L{PIDFile.read} raises L{NoPIDFound} when given a non-existing file path. PID file does not existN)rr5r!rrBr>r&rr8r)s r*test_readDoesntExistz!PIDFileTests.test_readDoesntExists? $--/*   j',, 7 Q!:;r,cddtdtfd}|jtd|t |j }|j t|j}|j|jtjy)z L{PIDFile.read} re-raises L{OSError} if the associated C{errno} is anything other than L{errno.ENOENT}. moderc6ttjd)Nz I/O error)OSErrorerrnoEIO)rSs r*oopsz>PIDFileTests.test_readOpenRaisesOSErrorNotENOENT..oopss%))[1 1r,openN)r) r&r patchr rr5r!rUrBr>rVrW)rrXr8errors r*#test_readOpenRaisesOSErrorNotENOENTz0PIDFileTests.test_readOpenRaisesOSErrorNotENOENTse  2s 2X 2 8VT*$--/*!!'7<<8 eii0r,cd}t|j}|j||j|j |y)z9 L{PIDFile._write} stores the given PID. iN)rr5_writer>rBrCs r* test_writePIDzPIDFileTests.test_writePIDs;$--/*s -r,cxt|j}|jt|jdy)zS L{PIDFile._write} raises L{ValueError} when given an invalid PID. burpN)rr5r! ValueErrorr_r7s r*test_writePIDInvalidz!PIDFileTests.test_writePIDInvalids*$--/* *gnnf=r,ct|j}|j|j|j t y)zT L{PIDFile.writeRunningPID} stores the PID for the current process. N)rr5writeRunningPIDr>rBrr7s r*test_writeRunningPIDz!PIDFileTests.test_writeRunningPIDs8$--/*! 2r,ct|jd}|j|jj|j |j |jjy)z9 L{PIDFile.remove} removes the PID file. r,N)rr5r%existsremove assertFalser7s r* test_removezPIDFileTests.test_removesY$--,- ((//12 ))0023r,ct|j}|jddtdtddfd}|j t d||j |jy)zR L{PIDFile.isRunning} returns true for a process that does exist. r;r=signalrNcyr2r=rns r*killz2PIDFileTests.test_isRunningDoesExist..kill r,rrrr5r_intr[rr% isRunningrr8rrs r*test_isRunningDoesExistz$PIDFileTests.test_isRunningDoesExistsa $--/*t c 3 4  8VT* ))+,r,ct|j}|j|j|j y)a@ L{PIDFile.isRunning} returns true for this process (which is running). @note: This differs from L{PIDFileTests.test_isRunningDoesExist} in that it actually invokes the C{kill} system call, which is useful for testing of our chosen method for probing the existence of a process. N)rr5rfr%rvr7s r*test_isRunningThiszPIDFileTests.test_isRunningThiss5$--/*! ))+,r,ct|j}|jddtdtddfd}|j t d||j t|jy)z{ L{PIDFile.isRunning} raises L{StalePIDFileError} for a process that does not exist (errno=ESRCH). r;r=rnrNc6ttjdNzNo such processrUrVESRCHrqs r*rrz5PIDFileTests.test_isRunningDoesNotExist..kill%++'89 9r,rr) rr5r_rur[rr!rrvrws r*test_isRunningDoesNotExistz'PIDFileTests.test_isRunningDoesNotExistsc $--/*t :c :3 :4 : 8VT* +W->->?r,ct|j}|jddtdtddfd}|j t d||j |jy)zx L{PIDFile.isRunning} returns true for a process that we are not allowed to kill (errno=EPERM). r;r=rnrNc6ttjd)NzOperation not permitted)rUrVEPERMrqs r*rrz3PIDFileTests.test_isRunningNotAllowed..kills%++'@A Ar,rrrtrws r*test_isRunningNotAllowedz%PIDFileTests.test_isRunningNotAllowedse $--/*t Bc B3 B4 B 8VT* ))+,r,ctdk7r tdt|j}|j d|j |j y)ac L{PIDFile.isRunning} returns true for a process that we are not allowed to kill (errno=EPERM). @note: This differs from L{PIDFileTests.test_isRunningNotAllowed} in that it actually invokes the C{kill} system call, which is useful for testing of our chosen method for probing the existence of a process that we are not allowed to kill. @note: In this case, we try killing C{init}, which is process #1 on POSIX systems, so this test is not portable. C{init} should always be running and should not be killable by non-root users. rzThis test assumes POSIXN) SYSTEM_NAMEr rr5r_r%rvr7s r*test_isRunningInitzPIDFileTests.test_isRunningInitsI ' !45 5$--/*q ))+,r,ct|j}|jdtdtddfd}|j t d||j t|jy)z L{PIDFile.isRunning} re-raises L{OSError} if the attached C{errno} value from L{os.kill} is not an expected one. r=rnrNc6ttjd)Nz File exists)rUrVEEXISTrqs r*rrz5PIDFileTests.test_isRunningUnknownErrno..kills%,, 6 6r,rr) rr5rfrur[rr!rUrvrws r*test_isRunningUnknownErrnoz'PIDFileTests.test_isRunningUnknownErrnosb $--/*! 7c 73 74 7 8VT* '7#4#45r,ctt|j}|j|jy)zS L{PIDFile.isRunning} returns false if the PID file doesn't exist. N)rr5rkrvr7s r*test_isRunningNoPIDFilez$PIDFileTests.test_isRunningNoPIDFile#s+$--/* **,-r,ct|j}|j|jj|5|j |jj|j |j tddd|j|jjy#1swY3xYw)z When used as a context manager, a L{PIDFile} will store the current pid on entry, then removes the PID file on exit. N)rr5rkrir%r>rBrr7s r*test_contextManagerz PIDFileTests.test_contextManager+s $--/* ))0023  7 OOG,,335 6   W\\^VX 6 7 ))0023  7 7s AC  Cct|j}|jddtdtddfd}|j t d||j t|j}|jt|d|5|j|jtdddy#1swYyxYw) z When used as a context manager, a L{PIDFile} will replace the underlying PIDFile rather than raising L{AlreadyRunningError} if the contained PID file exists but refers to a non-running PID. r;r=rnrNc6ttjdr}r~rqs r*rrz9PIDFileTests.test_contextManagerDoesntExist..killCrr,rrz'PID file refers to non-existing process) rr5r_rur[rr!rrvr>r&rBr)rr8rrr)s r*test_contextManagerDoesntExistz+PIDFileTests.test_contextManagerDoesntExist9s$--/*t :c :3 :4 : 8VT*   /1B1B C Q!JK  7   W\\^VX 6 7 7 7s )CC c(t|j}|jddtdtddfd}|j t d||j |j|jt|jy)z When used as a context manager, a L{PIDFile} will raise L{AlreadyRunningError} if the there is already a running process with the contained PID. r;r=rnrNcyr2rprqs r*rrz.killXrsr,rr) rr5r_rur[rr%rvr!r __enter__rws r*!test_contextManagerAlreadyRunningz.PIDFileTests.test_contextManagerAlreadyRunningNsx$--/*t c 3 4  8VT* ))+, -w/@/@Ar,r2rN)__name__ __module__ __qualname____doc__rbytesr r&r5r9r@rDrKrMrQr]r`rdrgrlr-rxrzrrrrrrrrrpr,r*r/r/Gs 8C= (? . U U<1 .>34 - - - - @ @ - ---, 6 6. 477(BBr,r/cHeZdZdZd dZd dZd dZd dZd dZd dZ d d Z y) NonePIDFileTestsz# Tests for L{NonePIDFile}. Nc8t}tt|y)z9 L{NonePIDFile} conforms to L{IPIDFile}. N)rr rr7s r*r9zNonePIDFileTests.test_interfacegs-Xw'r,ct}|jt|j}|j t |dy)z; L{NonePIDFile.read} raises L{NoPIDFound}. rON)rr!rrBr>r&rPs r* test_readzNonePIDFileTests.test_readns6-   j',, 7 Q!:;r,ct}|jt|jd}|j |j t j y)zZ L{NonePIDFile._write} raises L{OSError} with an errno of L{errno.EPERM}. rN)rr!rUr_r>rVrrr8r\s r* test_writezNonePIDFileTests.test_writews;-!!'7>>1= ekk2r,ct}|jt|j}|j |j t j y)zk L{NonePIDFile.writeRunningPID} raises L{OSError} with an errno of L{errno.EPERM}. N)rr!rUrfr>rVrrs r*rgz%NonePIDFileTests.test_writeRunningPIDs; -!!'7+B+BC ekk2r,ct}|jt|j}|j |j t j y)zZ L{NonePIDFile.remove} raises L{OSError} with an errno of L{errno.EPERM}. N)rr!rUrjr>rVENOENTrs r*rlzNonePIDFileTests.test_removes9-!!'7>>: ell3r,cXt}|j|jdy)z< L{NonePIDFile.isRunning} returns L{False}. FN)rr>rvr7s r*test_isRunningzNonePIDFileTests.test_isRunnings$- **,e4r,cHt}|5 dddy#1swYyxYw)zo When used as a context manager, a L{NonePIDFile} doesn't raise, despite not existing. N)rr7s r*rz$NonePIDFileTests.test_contextManagers& -      s!r) rrrrr9rrrgrlrrrpr,r*rrbs*(<3345r,r)%rrV functoolsrosrrrtypingrrrzope.interface.verifyr r twisted.trial.unittesttwistedtwisted.python.filepathr twisted.python.runtimer r runnerrrrrrrrrr-trialunittestTestCaser/rrpr,r*rs ***.,++#8CH-#(382D#LXB7==))22XBvAw}}--66Ar,