3
 aG                 @   s   d dl Z d dlZd dlZd dlZd dlmZ y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}yHy| jd W n tk
r<   | jd Y nX  j|  fdd}W nl tk
r   tj }|j	| tj
tj jtjd |j fdd}dd }| j| 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 j S )N)streamgetvaluer   )handlerr   r	   r
   :   s    c               S   s   g t j_d S )N)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 jt j} tjjt d| N tjjt jd| 0 tjjt jdt j|  d V  W d Q R X W d Q R X W d 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                   sD   e Zd Zddd fddZededddd	Zd
d Z  ZS )zFakeConfig.<locals>._FakeConfigN)returnc                s   t  jdji d S )NZdata_dir)super__init__Zstrpath)selffeatures_override)	__class__tmpdirr   r	   r   U   s    z(FakeConfig.<locals>._FakeConfig.__init__test_accountzDict[str, Any])account_namemachine_tokenc             S   sT   |s>g ddd|ddgddgdd	d
dddg dgddd}|  }|j d| |S )Nznot-nullZtest_machine_idzacct-1z2019-06-14T06:45:50ZZid1ZAWS)ZIDsZOrigin)idname	createdAtZexternalAccountIDsZcidZtest_contractz2020-05-08T19:02:26Zz2000-05-08T19:02:26Zz2040-05-08T19:02:26ZZfree)r%   r&   r'   ZeffectiveFromZeffectiveToZresourceEntitlementsZproducts)Z	machineIdZaccountInfoZcontractInfo)ZavailableResourcesZmachineTokenZmachineTokenInfozmachine-token)Zwrite_cache)clsr#   r$   configr   r   r	   for_attached_machineX   s$    z4FakeConfig.<locals>._FakeConfig.for_attached_machinec             S   s   |d k	r| j jd|i d S )NZfeatures)Zcfgupdate)r   r   r   r   r	   override_features{   s    z1FakeConfig.<locals>._FakeConfig.override_features)N)r"   N)	__name__
__module____qualname__r   classmethodstrr*   r,   __classcell__r   )r!   )r    r	   _FakeConfigT   s
    r3   )r   )r!   r3   r   )r!   r	   
FakeConfigR   s    +r4   )r   r   r   ZpytestZuaclient.configr   Ztypingr   r   r   ImportErrorZfixturer   Zyield_fixturer   r4   r   r   r   r	   <module>   s   4