
    Zvjr                         d Z ddlZddlmc mZ ddlZddlZddl	m
Z
 ddlmZ ddlmZmZmZmZmZ  G d d      Z G d d	      Z G d
 d      Zy)z+Tests for PeerDrop Daemon lifecycle module.    N)Path)	MagicMock)Daemonkill_daemonread_pid_fileremove_pid_filewrite_pid_filec                   $    e Zd Zd Zd Zd Zd Zy)TestPidFilec                    t        j                         5 }t        |      dz  }t        |       |j                  } |       }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            d x}}t        |      }t        j                  } |       }||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      nddt        j                         v st        j                  t              rt        j                  t              ndt        j                  |      t        j                  |      d	z  }d
d|iz  }	t        t        j                  |	            d x}x}}d d d        y # 1 sw Y   y xY w)Ntest.pidAassert %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.exists
}()
}pid_pathpy0py2py4)==)zE%(py0)s == %(py6)s
{%(py6)s = %(py4)s
{%(py4)s = %(py2)s.getpid
}()
}pidos)r   r   r   py6zassert %(py8)spy8)tempfileTemporaryDirectoryr   r	   exists@py_builtinslocals
@pytest_ar_should_repr_global_name	_safereprAssertionError_format_explanationr   r   getpid_call_reprcompare)
selftmpdirr   @py_assert1@py_assert3@py_format5r   @py_assert5@py_format7@py_format9s
             =/Users/sumanjeet/code/PeerDrop/tests/test_daemon_lifecycle.pytest_write_and_readzTestPidFile.test_write_and_read   s-   ((* 	&fF|j0H8$??$?$$$$$$$$8$$$8$$$?$$$$$$$$$$)C))%)+%3+%%%%3+%%%%%%3%%%3%%%%%%"%%%"%%%)%%%+%%%%%%%	& 	& 	&s   G%HHc                 v   t        t        d            }d }||u }|st        j                  d|fd||f      dt	        j
                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }dd|iz  }t        t        j                  |            d x}}y )N/nonexistent/path.pidisz%(py0)s is %(py3)sr   r   py3assert %(py5)spy5)
r   r   r   r$   r   r   r   r    r!   r"   )r%   r   @py_assert2r'   @py_format4@py_format6s         r-   test_read_nonexistentz!TestPidFile.test_read_nonexistent   sj    D!89:sd{sdssd    c                 @   t        j                         5 }t        |      dz  }t        |       |j                  } |       }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            d x}}t        |       |j                  } |       }| }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            d x}x}}d d d        y # 1 sw Y   y xY w)Nr   r   r   r   Eassert not %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.exists
}()
})r   r   r   r	   r   r   r   r   r   r    r!   r"   r   )r%   r&   r   r'   r(   r)   r*   r:   s           r-   test_remove_pid_filez TestPidFile.test_remove_pid_file   s    ((* 	)fF|j0H8$??$?$$$$$$$$8$$$8$$$?$$$$$$$$$$H%((((((((((((x(((x(((((((((((((	) 	) 	)s   E6FFc                 ,    t        t        d             y )Nr0   )r   r   )r%   s    r-   test_remove_nonexistentz#TestPidFile.test_remove_nonexistent(   s    456r<   N)__name__
__module____qualname__r.   r;   r?   rA    r<   r-   r   r      s    &)7r<   r   c                       e Zd Zd Zd Zd Zy)
TestDaemonc                 z   t        j                         5 }t        |      dz  }t        j                  t              }||_        |j                  } |       }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }	t        t        j                  |	            d x}x}x}}d d d        y # 1 sw Y   y xY w)	Nr   Fr1   zI%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.is_running
}()
} is %(py7)sdaemonr   r   r   py7assert %(py9)spy9)r   r   r   r   __new__	_pid_path
is_runningr   r$   r   r   r   r    r!   r"   )
r%   r&   r   rJ   r'   r(   @py_assert6r*   @py_format8@py_format10s
             r-   test_is_running_no_pid_filez&TestDaemon.test_is_running_no_pid_file.   s    ((* 	0fF|j0H^^F+F'F$$/$&/%/&%////&%//////6///6///$///&///%///////		0 	0 	0s   DD11D:c                    t        j                         5 }t        |      dz  }|j                  d       t	        j
                  t              }||_        |j                  } |       }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      dz  }dd	|iz  }	t        t        j                  |	            d x}x}x}}|j                   } |       }| }|sd
dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }
t        t        j                  |
            d x}x}}d d d        y # 1 sw Y   y xY w)Nr   	999999999Fr1   rI   rJ   rK   rM   rN   r>   r   r   )r   r   r   
write_textr   rO   rP   rQ   r   r$   r   r   r   r    r!   r"   r   )r%   r&   r   rJ   r'   r(   rR   r*   rS   rT   r:   s              r-   test_is_running_stale_pidz$TestDaemon.test_is_running_stale_pid5   sA   ((* 		)fF|j0H,^^F+F'F$$/$&/%/&%////&%//////6///6///$///&///%///////((((((((((((x(((x(((((((((((((		) 		) 		)s   GG--G6c                 8   t        j                         5 }t        |      dz  }t        |      dz  }t        j                  t              }||_        ||_        t               |_        d|j                  _	        |j                         }|d   }d}||u }|slt        j                  d|fd||f      t        j                  |      t        j                  |      dz  }	dd	|	iz  }
t        t        j                  |
            d x}x}}|d
   }d }||u }|slt        j                  d|fd||f      t        j                  |      t        j                  |      dz  }	dd	|	iz  }
t        t        j                  |
            d x}x}}d d d        y # 1 sw Y   y xY w)Nr   z	test.sockFrunningr1   )z%(py1)s is %(py4)s)py1r   zassert %(py6)sr   peer_id)r   r   r   r   rO   rP   
_sock_pathr   _engine_started
get_statusr   r$   r    r!   r"   )r%   r&   r   	sock_pathrJ   status@py_assert0r(   r8   r)   r+   s              r-   test_get_status_not_startedz&TestDaemon.test_get_status_not_startedA   s&   ((* 	-fF|j0HV{2I^^F+F'F )F&[FN&+FNN#&&(F)$--$----$---$----------)$,,$,,,,$,,,$,,,,,,,,,,	- 	- 	-s   E2FFN)rB   rC   rD   rU   rY   re   rE   r<   r-   rG   rG   -   s    0
)-r<   rG   c                       e Zd Zd Zd Zy)TestKillDaemonc                 v   t        t        d            }d}||u }|st        j                  d|fd||f      dt	        j
                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }dd|iz  }t        t        j                  |            d x}}y )	Nr0   Fr1   r3   resultr4   r6   r7   )
r   r   r   r$   r   r   r   r    r!   r"   )r%   ri   r8   r'   r9   r:   s         r-   test_kill_no_pid_filez$TestKillDaemon.test_kill_no_pid_fileR   sj    T"9:;vvvvr<   c                 J   t        j                         5 }t        |      dz  }|j                  d       t	        |      }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }dd	|iz  }t        t        j                  |            d x}}|j                  } |       }| }	|	sd
dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            d x}x}}	d d d        y # 1 sw Y   y xY w)Nr   rW   Fr1   r3   ri   r4   r6   r7   r>   r   r   )r   r   r   rX   r   r   r$   r   r   r   r    r!   r"   r   )
r%   r&   r   ri   r8   r'   r9   r:   r(   r*   s
             r-   test_kill_stale_pidz"TestKillDaemon.test_kill_stale_pidV   s   ((* 	)fF|j0H, *F""6U?"""6U""""""6"""6"""U"""""""((((((((((((x(((x(((((((((((((	) 	) 	)s   E;FF"N)rB   rC   rD   rj   rl   rE   r<   r-   rg   rg   Q   s    )r<   rg   )__doc__builtinsr   _pytest.assertion.rewrite	assertionrewriter   r   r   pathlibr   unittest.mockr   peerdrop.daemon.lifecycler   r   r   r   r	   r   rG   rg   rE   r<   r-   <module>ru      sA    1   	   # 7 78!- !-H) )r<   