U
    c6{i-  ã                   @   sb   d dl mZ d dlmZ zd dlZW n ek
r<   dZY nX eZd dlmZ G dd„ deƒZ	dS )é    )ÚFailure)ÚTestCaseN)Úsyslogc                   @   s€   e Zd ZdZdZedkr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S )ÚSyslogObserverTestszS
    Tests for L{SyslogObserver} which sends Twisted log events to the syslog.
    Nz(syslog is not supported on this platformc                 C   s4   |   tjd| j¡ |   tjd| j¡ t d¡| _d S )NÚopenlogr   r   )Úpatchr   ZSyslogObserverr   Úobserver©Úself© r   úC/tmp/pip-unpacked-wheel-hfbvu0bz/twisted/python/test/test_syslog.pyÚsetUp   s    zSyslogObserverTests.setUpc                 C   s   |||f| _ g | _d S ©N)Z	logOpenedÚevents)r
   ÚprefixÚoptionsÚfacilityr   r   r   r      s    zSyslogObserverTests.openlogc                 C   s   | j  ||f¡ d S r   )r   Úappend)r
   r   Úmessager   r   r   r   #   s    zSyslogObserverTests.syslogc                 C   s&   | j  ddddœ¡ |  | jg ¡ dS )zm
        L{SyslogObserver.emit} ignores events with an empty value for the
        C{'message'} key.
        r   Fú-©r   ÚisErrorÚsystemN)r   ÚemitÚassertEqualr   r	   r   r   r   Útest_emitWithoutMessage&   s    z+SyslogObserverTests.test_emitWithoutMessagec                 C   s2   | j  dddtjdœ¡ |  | jtjdfg¡ dS )ú¢
        L{SyslogObserver.emit} uses the value of the C{'syslogPriority'} as the
        syslog priority, if that key is present in the event dictionary.
        ©zhello, worldFr   )r   r   r   ÚsyslogPriorityú[-] hello, worldN)r   r   Ú	stdsyslogÚ	LOG_DEBUGr   r   r	   r   r   r   Útest_emitCustomPriority.   s    üÿz+SyslogObserverTests.test_emitCustomPriorityc                 C   s8   | j  dddttdƒƒdœ¡ |  | jtjdfg¡ dS )zd
        L{SyslogObserver.emit} uses C{LOG_ALERT} if the event represents an
        error.
        r   Tr   Zfoo)r   r   r   Úfailurer   N)r   r   r   Ú	Exceptionr   r   r    Ú	LOG_ALERTr	   r   r   r   Útest_emitErrorPriority=   s    
üÿz*SyslogObserverTests.test_emitErrorPriorityc              	   C   s<   | j  dddtjttdƒƒdœ¡ |  | jtjdfg¡ dS )z£
        L{SyslogObserver.emit} uses the value of the C{'syslogPriority'} key if
        it is specified even if the event dictionary represents an error.
        r   Tr   Úbar)r   r   r   r   r#   r   N)r   r   r    Ú
LOG_NOTICEr   r$   r   r   r	   r   r   r   Ú%test_emitCustomPriorityOverridesErrorL   s    
ûÿ	z9SyslogObserverTests.test_emitCustomPriorityOverridesErrorc                 C   s8   | j  dddtjdœ¡ |  | jtjtjB dfg¡ dS )r   r   Fr   )r   r   r   ZsyslogFacilityr   N)r   r   r    ÚLOG_CRONr   r   ÚLOG_INFOr	   r   r   r   Útest_emitCustomFacility\   s    üÿ ÿz+SyslogObserverTests.test_emitCustomFacilityc                 C   s.   | j  ddddœ¡ |  | jtjdfg¡ dS )zt
        L{SyslogObserver.emit} uses the value of the C{'system'} key to prefix
        the logged message.
        r   FZnonDefaultSystemr   z[nonDefaultSystem] hello, worldN©r   r   r   r   r    r+   r	   r   r   r   Útest_emitCustomSystemm   s    ýÿ 
ÿz)SyslogObserverTests.test_emitCustomSystemc                 C   s.   | j  ddddœ¡ |  | jtjdfg¡ dS )z‹
        L{SyslogObserver.emit} logs the value of the C{'message'} key of the
        event dictionary it is passed to the syslog.
        r   Fr   r   r   Nr-   r	   r   r   r   Útest_emitMessage}   s    
ÿz$SyslogObserverTests.test_emitMessagec                 C   s6   | j  ddddœ¡ |  | jtjdftjdfg¡ dS )zW
        Each line of a multiline message is emitted separately to the syslog.
        )zhello,
worldFr   r   ú
[-] hello,ú
[-] 	worldNr-   r	   r   r   r   Útest_emitMultilineMessage‡   s    
ÿþz-SyslogObserverTests.test_emitMultilineMessagec                 C   s6   | j  ddddœ¡ |  | jtjdftjdfg¡ dS )zw
        Trailing empty lines of a multiline message are omitted from the
        messages sent to the syslog.
        )zhello,
world

Fr   r   r0   r1   Nr-   r	   r   r   r   Ú!test_emitStripsTrailingEmptyLines“   s    
ÿþz5SyslogObserverTests.test_emitStripsTrailingEmptyLines)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    Úskipr   r   r   r   r"   r&   r)   r,   r.   r/   r2   r3   r   r   r   r   r      s    
r   )
Ztwisted.python.failurer   Ztwisted.trial.unittestr   r   Z
_stdsyslogÚImportErrorr    Ztwisted.pythonr   r   r   r   r   Ú<module>   s   
