U
    M[^+                     @   s   d dl Z d dlZd dlZd dlZd dlmZ zd dlmZmZm	Z	 W n e
k
rX   Y nX ejdd Zejdd Zejdd	 ZdS )
    NUAConfig)AnyDictOptionalc                    s   t | dtj}zHz| d W n tk
r<   | d Y nX  |  fdd}W nl tk
r   t }|	| t
t td | fdd}dd }| | Y nX |S )	a  
    A fixture that returns a function that returns caplog.text

    caplog isn't available in pytest in all of our target releases; this either
    uses caplog.text if available, or a shim which replicates what it does.

    Specifically, bionic is the first Ubuntu release to contain a version of
    pytest new enough for the caplog fixture to be present. In xenial, the
    python3-pytest-catchlog package provides the same functionality (this is
    the code that was later integrated in to pytest). For trusty, there is no
    packaged alternative to this shim.

    (It returns a function so that the requester can decide when to examine the
    logs; if it returned caplog.text directly, that would always be empty.)
    Zparamcaplogc                      s    j S N)text )r   r
   3/usr/lib/python3/dist-packages/uaclient/conftest.py_func+   s    zcaplog_text.<locals>._funcz7%(filename)-25s %(lineno)4d %(levelname)-8s %(message)sc                      s
    j  S r   )streamgetvaluer
   )handlerr
   r   r   :   s    c                   S   s   g t j_d S r   )loggingrootZhandlersr
   r
   r
   r   clear_handlers=   s    z#caplog_text.<locals>.clear_handlers)getattrr   INFOZgetfixturevalueAttributeErrorZgetfuncargvalueZ	set_levelLookupErrorZ	getLoggerZsetLevelZStreamHandlerioStringIOZsetFormatterZ	FormatterZ
addHandlerZaddfinalizer)ZrequestZ	log_levelr   r   r   r
   )r   r   r   caplog_text   s,    


r   c                  c   st   t t j} tjt d| N tjt jd| 0 tjt jdt |  d V  W 5 Q R X W 5 Q R X W 5 Q R X d S )Nr   Zmanager)r   Z
RootLoggerZWARNINGmockZpatchobjectZLoggerZManager)Zroot_loggerr
   r
   r   logging_sandboxD   s      r   c                    s   G  fdddt }|S )Nc                       s:   e Zd Zdd fddZed
edddd	Z  ZS )zFakeConfig.<locals>._FakeConfigN)returnc                    s   t  dji d S )NZdata_dir)super__init__Zstrpath)self)	__class__tmpdirr
   r   r   U   s    z(FakeConfig.<locals>._FakeConfig.__init__test_accountzDict[str, Any])account_namemachine_tokenc                 S   s:   |s$g dd|dddg ddd}|  }| d	| |S )
Nznot-nullzacct-1)idnameZcidZtest_contract)r&   r'   ZresourceEntitlements)ZaccountInfoZcontractInfo)ZavailableResourcesZmachineTokenZmachineTokenInfozmachine-token)Zwrite_cache)clsr$   r%   Zconfigr
   r
   r   for_attached_machineX   s    z4FakeConfig.<locals>._FakeConfig.for_attached_machine)r#   N)__name__
__module____qualname__r   classmethodstrr)   __classcell__r
   r"   )r!   r   _FakeConfigT   s     r1   r   )r"   r1   r
   r0   r   
FakeConfigR   s    r2   )r   r   r   ZpytestZuaclient.configr   typingr   r   r   ImportErrorZfixturer   Zyield_fixturer   r2   r
   r
   r
   r   <module>   s   
3
