ó
Y[c           @   sã   d  Z  d d l 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 m	 Z	 m
 Z
 d „  Z d „  Z e j d „  ƒ Z d „  Z d e j f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ e d k rß e j ƒ  n  d S(   s    Tests for certbot.error_handler.iÿÿÿÿN(   t   Callablet   Dictt   Unionc         C   s   t  d „  |  Dƒ ƒ S(   s,   Get the handlers for an iterable of signums.c         s   s$   |  ] } | t  j | ƒ f Vq d  S(   N(   t   signalt	   getsignal(   t   .0t   s(    (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pys	   <genexpr>   s    (   t   dict(   t   signums(    (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyt   get_signals   s    c         C   s1   x* |  j  ƒ  D] \ } } t j | | ƒ q Wd S(   sD   Set the signal (keys) with the handler (values) from the input dict.N(   t   itemsR   (   t   sig_handler_dictR   t   h(    (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyt   set_signals   s    c         #   sE   g  ‰  t  |  ƒ } t t ‡  f d †  |  Dƒ ƒ ƒ ˆ  Vt | ƒ d S(   s    Context manager to catch signalsc         3   s$   |  ] } | ‡  f d  †  f Vq d S(   c            s   ˆ  j  |  ƒ S(   N(   t   append(   R   t   _(   t   signals(    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyt   <lambda>   s    N(    (   R   R   (   R   (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pys	   <genexpr>   s    N(   R	   R   R   (   R   t   prev_handlers(    (   R   sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyt   signal_receiver   s
     c         C   s   t  j t  j ƒ  |  ƒ d S(   s   Send the given signalN(   t   ost   killt   getpid(   t   signum(    (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyt   send_signal#   s    t   ErrorHandlerTestc           B   sM   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   s-   Tests for certbot.error_handler.ErrorHandler.c         C   so   d d l  m } t j ƒ  |  _ t d ƒ |  _ i d d 6|  _ | j |  j |  j |  j Ž |  _	 | j
 |  _ d  S(   Niÿÿÿÿ(   t   error_handleri*   t   bart   foo(   i*   (   t   certbotR   t   mockt	   MagicMockt	   init_funct   sett	   init_argst   init_kwargst   ErrorHandlert   handlert   _SIGNALSR   (   t   selfR   (    (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyt   setUp+   s    c         C   sd   t  } y |  j  t ‚ Wd  QXWn t k
 r9 t } n X|  j | ƒ |  j j |  j |  j Ž  d  S(   N(	   t   FalseR%   t
   ValueErrort   Truet
   assertTrueR    t   assert_called_once_withR"   R#   (   R'   t   exception_raised(    (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyt   test_context_manager8   s    

c         C   sÐ   t  |  j ƒ } t |  j ƒ 8 } |  j & d } t |  j d ƒ | d 9} Wd  QXWd  QX|  j d | ƒ |  j |  j d g | ƒ |  j j |  j |  j	 Ž  x. |  j D]# } |  j | | t
 j | ƒ ƒ q¥ Wd  S(   Ni*   i    i
   (   R	   R   R   R%   R   t   assertEqualR    R-   R"   R#   R   R   (   R'   t   init_signalst   signals_receivedt   should_be_42R   (    (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyt    test_context_manager_with_signalD   s    

c         C   sz   t  j d t g ƒ } |  j j | ƒ y |  j  t ‚ Wd  QXWn t k
 rR n X|  j j |  j |  j Ž  | j ƒ  d  S(   Nt   side_effect(	   R   R   R*   R%   t   registerR    R-   R"   R#   (   R'   t   bad_func(    (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyt   test_bad_recoveryV   s    

c      
      s´   |  j  d ‰  |  j  d } t j d ‡  f d †  ƒ } |  j j | ƒ t |  j  ƒ ! } |  j  t | ƒ Wd  QXWd  QX|  j | ˆ  g | ƒ |  j j	 |  j
 |  j Ž  | j	 ƒ  d  S(   Ni    iÿÿÿÿR5   c              s
   t  ˆ  ƒ S(   N(   R   (    (   t   sig1(    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyR   e   s    (   R   R   R   R%   R6   R   R   R0   R    R-   R"   R#   (   R'   t   sig2R7   R2   (    (   R9   sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyt   test_bad_recovery_with_signalb   s    

c         C   sL   y! |  j   t j d ƒ Wd  QXWn t k
 r4 n X|  j |  j j ƒ d  S(   Ni    (   R%   t   syst   exitt
   SystemExitt   assertFalseR    t   called(   R'   (    (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyt   test_sysexit_ignoredo   s    
c         C   sG   t  j ƒ  } |  j j | ƒ |  j  Wd  QX|  j j ƒ  | j ƒ  d  S(   N(   R   R   R%   R6   R    t   assert_not_called(   R'   t   func(    (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyt   test_regular_exitw   s    
(
   t   __name__t
   __module__t   __doc__R(   R/   R4   R8   R;   RA   RD   (    (    (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyR   (   s   						t   ExitHandlerTestc           B   s    e  Z d  Z d „  Z d „  Z RS(   s,   Tests for certbot.error_handler.ExitHandler.c         C   sH   d d l  m } t t |  ƒ j ƒ  | j |  j |  j |  j Ž |  _	 d  S(   Niÿÿÿÿ(   R   (
   R   R   t   superRH   R(   t   ExitHandlerR    R"   R#   R%   (   R'   R   (    (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyR(   ƒ   s
    c         C   sS   t  j ƒ  } |  j j | ƒ |  j  Wd  QX|  j j |  j |  j Ž  | j ƒ  d  S(   N(   R   R   R%   R6   R    R-   R"   R#   (   R'   RC   (    (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyRD   Š   s    

(   RE   RF   RG   R(   RD   (    (    (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyRH   €   s   	t   __main__(   RG   t
   contextlibR   R   R<   t   unittestR   t   acme.magic_typingR    R   R   R	   R   t   contextmanagerR   R   t   TestCaseR   RH   RE   t   main(    (    (    sD   /usr/lib/python2.7/dist-packages/certbot/tests/error_handler_test.pyt   <module>   s   		
	X