B
    `k-                 @   sf  d Z ddlmZmZmZ e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 ddlmZ ddlmZmZ ddlmZmZmZmZ dd	lmZmZmZ dd
lmZmZm Z m!Z!m"Z"m#Z# ddl$m%Z%m&Z& ddl'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z. dd Z/dd Z0dd Z1dd Z2dd Z3dd Z4ej5dd Z6ej5dd Z7G d d! d!Z8G d"d# d#e*Z9dS )$z(Ansible integration test infrastructure.    )absolute_importdivisionprint_functionN   )types)to_bytes)'analyze_integration_target_dependencieswalk_integration_targets)IntegrationConfigNetworkIntegrationConfigPosixIntegrationConfigWindowsIntegrationConfig)	make_dirswrite_text_fileread_text_file)ApplicationErrordisplayCOVERAGE_CONFIG_NAMEMODE_DIRECTORYMODE_DIRECTORY_WRITE	MODE_FILE)named_temporary_file
ResultType)generate_coverage_config)CommonCache)CloudEnvironmentConfig)data_contextc             C   s~   | j r
dS t| t|t | jrztj|t}t	| }t
|| t|t tj|tjj}t| t|t dS )z;
    :type args: IntegrationConfig
    :type path: str
    N)explainosmkdirchmodr   Zcoveragepathjoinr   r   r   r   r   ZCOVERAGEnamer   )argsr!   Zcoverage_config_pathZcoverage_configZcoverage_output_path r%   Z/home/dcms/DCMS/lib/python3.7/site-packages/ansible_test/_internal/integration/__init__.pysetup_common_temp_dirB   s    


r'   c       	      C   s   t dd | D }t| }i }t }x\| D ]P\}}||}|sP|| q.x,|D ]$}||krlt ||< || | qVW q.W |rtddt| |S )zj
    :type integration_targets: list[IntegrationTarget]
    :rtype: dict[str, set[IntegrationTarget]]
    c             s   s   | ]}|j |fV  qd S )N)r#   ).0targetr%   r%   r&   	<genexpr>a   s    z*generate_dependency_map.<locals>.<genexpr>z$Non-existent target dependencies: %sz, )	dictr   setitemsgetaddr   r"   sorted)	integration_targetsZtargets_dicttarget_dependenciesdependency_mapZinvalid_targets
dependencyZ
dependentsZdependency_targetZ	dependentr%   r%   r&   generate_dependency_map\   s     



r5   c             C   sP   g }x| D ]}||j 7 }q
W tt|}dd |D }|rLtdd| |S )zR
    :type target_dependencies: list[IntegrationTarget]
    :rtype: list[str]
    c             S   s   g | ]}t j|s|qS r%   )r   r!   isfile)r(   r!   r%   r%   r&   
<listcomp>   s    z$get_files_needed.<locals>.<listcomp>z"Invalid "needs/file/*" aliases:
%s
)Z
needs_filer0   r,   r   r"   )r2   files_neededZtarget_dependencyZinvalid_pathsr%   r%   r&   get_files_neededz   s    
r:   c             C   s6   | j s| jr2tj|r2t|}d|kr2td dS )z%Check the given inventory for issues.Zansible_ssh_private_key_filez|Use of "ansible_ssh_private_key_file" in inventory with the --docker or --remote option is unsupported and will likely fail.N)Zdockerremoter   r!   existsr   r   warning)r$   inventory_path	inventoryr%   r%   r&   check_inventory   s
    r@   c             C   s,   t dtdtdi}tjt jj|t	|  S )zdReturn the inventory path used for the given integration configuration relative to the content root.r?   zinventory.winrmzinventory.networking)
r   r   r   r   r!   r"   r   contentintegration_pathtype)r$   Zinventory_namesr%   r%   r&   get_inventory_relative_path   s    rD   c                s,   t  trdS  fdd}t | dS )z5Make the given inventory available during delegation.Nc                s   t   f}tjr|| kr fdd| D }|rfx|D ]}| | q@W td f  ntd f  | | dS )z
        Add the inventory file to the payload file list.
        This will preserve the file during delegation even if it is ignored or is outside the content and install roots.
        c                s   g | ]}|d   kr|qS )   r%   )r(   item)r>   r%   r&   r7      s    zBdelegate_inventory.<locals>.inventory_callback.<locals>.<listcomp>z)Overriding inventory file "%s" with "%s".z'Sourcing inventory file "%s" from "%s".N)	rD   r   r!   r6   remover   r=   noticeappend)filesZinventory_tupleZ	originalsoriginal)r$   inventory_path_src)r>   r&   inventory_callback   s    
z.delegate_inventory.<locals>.inventory_callback)
isinstancer   r   Zregister_payload_callback)r$   rL   rM   r%   )r$   rL   r&   delegate_inventory   s    
rO   c          	   #   s,  |   }tjt jjd| j }| js2d|j	krt
d tjt jjt jj}tjt jjt jj}|}|}tjt jjt jj}	t|||||	V  dS tjtjjd}
d|j }d}| jsd|j	krt
d	 d
}| jrtj|
d||f nt|
 tj|||
dzt
jd dd t| }tj|}t| }t|gt|j|jt  }t |}tjt jj}tjt jj}tj|}tjt jjt jj}tjt jj}	||f||fg}tj!|r
|"||	f |fdd|D 7 }t jj  fdd|D }tt|}tt|}| jsft| xH|D ]@\}}t
jd||f dd | jslt#j$t%|t%|dd qlW xL|D ]D\}}t
jd||f dd | jsttj&| t#'|| qW t|||||	V  W d| js&t#( X dS )zm
    :type args: IntegrationConfig
    :type target: IntegrationTarget
    :type inventory_path_src: str
    z%s.cfgzno/temp_workdir/znDisabling the temp work dir is a temporary debugging feature that may be removed in the future without notice.NZintegrationz%s-u   -ÅÑŚÌβŁÈzno/temp_unicode/zyDisabling unicode in the temp work dir is a temporary debugging feature that may be removed in the future without notice.z-ansiblez%stemp%s)prefixsuffixdirz!Preparing temporary directory: %sr   )	verbosityc                s   g | ]}|t j |fqS r%   )r   r!   r"   )r(   r!   )temp_dirr%   r&   r7     s    z0integration_test_environment.<locals>.<listcomp>c                s.   g | ]&}t j |jt j |jfqS r%   )r   r!   r"   relative_path)r(   r)   )!integration_targets_relative_pathrT   r%   r&   r7     s   zCopying %s/ to %s/T)symlinkszCopying %s to %s))Zget_ansible_configr   r!   r"   r   rA   rB   commandZno_temp_workdiraliasesr   r=   rootZintegration_targets_pathZintegration_vars_pathIntegrationEnvironmentr   TMPr#   Zno_temp_unicoder   r   tempfilemkdtempinforD   IntegrationCacher0   listr3   r.   r,   r:   r<   rI   shutilcopytreer   dirnamecopy2rmtree)r$   r)   rL   Zansible_config_srcZansible_config_relativeintegration_dirtargets_dirr>   ansible_config	vars_fileZroot_temp_dirrP   rQ   Zinventory_relative_pathcacher2   r9   Zvars_file_srcZfile_copiesZdirectory_copiesZdir_srcZdir_dstZfile_srcZfile_dstr%   )rV   rT   r&   integration_test_environment   sr    


 

rl   c          	   c   s   |sdV  dS |j pi  }|tt|j|jdd tj|ddd}t| dd||0}t	j
||}tjd	||f d
d |V  W dQ R X dS )zs
    :type args: IntegrationConfig
    :type env_config: CloudEnvironmentConfig
    :type integration_dir: str
    N)environmentmodule_defaults)Zansible_test   T)indent	sort_keyszconfig-file-z.jsonz>>> Config File: %s
%s   )rS   )Zansible_varscopyupdater+   Zenv_varsrn   jsondumpsr   r   r!   relpathr   r_   )r$   Z
env_configrg   config_varsconfig_filer!   filenamer%   r%   r&   integration_test_config_file'  s    r{   c               @   s   e Zd ZdZdd ZdS )r[   z*Details about the integration environment.c             C   s"   || _ || _|| _|| _|| _d S )N)rg   rh   r>   ri   rj   )selfrg   rh   r>   ri   rj   r%   r%   r&   __init__G  s
    zIntegrationEnvironment.__init__N)__name__
__module____qualname____doc__r}   r%   r%   r%   r&   r[   E  s   r[   c               @   s(   e Zd ZdZedd Zedd ZdS )r`   zIntegration cache.c             C   s   |  ddd S )z1
        :rtype: list[IntegrationTarget]
        r1   c               S   s
   t t S )N)ra   r	   r%   r%   r%   r&   <lambda>V      z6IntegrationCache.integration_targets.<locals>.<lambda>)r.   )r|   r%   r%   r&   r1   Q  s    z$IntegrationCache.integration_targetsc                s     d fddS )z;
        :rtype: dict[str, set[IntegrationTarget]]
        r3   c                  s
   t  jS )N)r5   r1   r%   )r|   r%   r&   r   ]  r   z1IntegrationCache.dependency_map.<locals>.<lambda>)r.   )r|   r%   )r|   r&   r3   X  s    zIntegrationCache.dependency_mapN)r~   r   r   r   propertyr1   r3   r%   r%   r%   r&   r`   O  s   r`   ):r   
__future__r   r   r   rC   __metaclass__
contextlibru   r   rb   r]    r   tencodingr   r)   r   r	   configr
   r   r   r   ior   r   r   utilr   r   r   r   r   r   Zutil_commonr   r   Zcoverage_utilr   rk   r   Zcloudr   datar   r'   r5   r:   r@   rD   rO   contextmanagerrl   r{   r[   r`   r%   r%   r%   r&   <module>   s8    	
g
