
    Vh]?                    0   d Z ddlm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mZmZmZ ddlmZ ddlmZ dd	lmZmZmZmZmZmZm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' ddl(m)Z)  G d de      Z*ejV                   G d d             Z,y)z.Sanity test to check integration test aliases.    )annotationsN   )SanitySingleVersionSanityMessageSanityFailureSanitySuccessSanityTargetsSANITY_ROOT   )
TestResult)SanityConfig)filter_targetswalk_posix_integration_targets walk_windows_integration_targetswalk_integration_targetswalk_module_targetsCompletionTargetIntegrationTargetType   )get_cloud_platforms)read_text_file)displayraw_command)get_docs_urlwrite_json_test_results
ResultType)PythonConfigc                       e Zd ZdZdZdZdZdZdZ e	d      Z
dZd	Zd
ZdZdZd fdZedd       Zedd       ZddZedd       ZdddZd dZd!dZd"dZd#dZ	 d$	 	 	 	 	 	 	 d%dZd&dZd'dZ xZS )(IntegrationAliasesTestz1Sanity test to evaluate integration test aliases.z$.azure-pipelines/azure-pipelines.yml	shippablez	disabled/z	unstable/zunsupported/z]https://docs.ansible.com/ansible-core/devel/dev_guide/testing/sanity/integration-aliases.htmlz
    The following integration tests are **disabled** [[explain]({explain_url}#disabled)]:

    {tests}

    Consider fixing the integration tests before or alongside changes.
    z
    The following integration tests are **unstable** [[explain]({explain_url}#unstable)]:

    {tests}

    Tests may need to be restarted due to failures unrelated to changes.
    z
    The following integration tests are **unsupported** [[explain]({explain_url}#unsupported)]:

    {tests}

    Consider running the tests manually or extending test infrastructure to add support.
    z
    The following modules have **no integration tests** [[explain]({explain_url}#untested)]:

    {tests}

    Consider adding integration tests before or alongside changes.
    Tc                >    t         |           i | _        i | _        y N)super__init__
_ci_config_ci_test_groups)self	__class__s    j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/commands/sanity/integration_aliases.pyr$   zIntegrationAliasesTest.__init__l   s    ,.57    c                     y)z)True if the test supports ignore entries.F r'   s    r)   
can_ignorez!IntegrationAliasesTest.can_ignorer   s     r*   c                     y)zPTrue if the test does not use test targets. Mutually exclusive with all_targets.Tr,   r-   s    r)   
no_targetsz!IntegrationAliasesTest.no_targetsw   s     r*   c                t    | j                   s!| j                  || j                        | _         | j                   S )z*Load and return the CI YAML configuration.)r%   	load_yamlCI_YML)r'   pythons     r)   load_ci_configz%IntegrationAliasesTest.load_ci_config|   s)    "nnVT[[ADOr*   c           
     >   | j                   sqi }| j                  d   D ]7  }|d   D ]+  }|j                  d      dk7  r|d   }|j                  dg       }|j                  dd      }|j                  d	d
      }|d   D ]  }|j                  d      xs |j                  d      }	|r/|D 
cg c]#  }
|j                  |      j                  |	|
      % }}
n|j                  |	      g}|D ]b  }|j	                  d      }|d   }|dv r	 t        |d         }
|
dk  s|
dkD  r8|j                  |t                     }|j                  |
       d  . : t        d |j                         D              | _         | j                   S c c}
w # t        $ r Y w xY w)z8Return a dictionary of CI test names and their group(s).stagesjobstemplateztemplates/matrix.yml
parametersgroups
testFormatz{0}groupFormatz	{0}/{{1}}targetstestname/r   )sanityunitsr   c   c              3  <   K   | ]  \  }}|t        |      f  y wr"   )sorted).0keyvalues      r)   	<genexpr>z8IntegrationAliasesTest.ci_test_groups.<locals>.<genexpr>   s     'cefUm(<'cs   )r&   r%   getformatsplitint
ValueError
setdefaultsetadddictitems)r'   test_groupsstagejobr:   r;   test_formattest_group_formattargetr?   grouptests_formattedtest_formattedpartsrI   	group_sets                   r)   ci_test_groupsz%IntegrationAliasesTest.ci_test_groups   s    ##/1K2 #1 = "1Cwwz*.DD !$\!2J'^^Hb9F",..u"EK(2}k(R%",Y"7 1%zz&1GVZZ5G!v|.}mr/@/G/G/T/[/[\`bg/h.}O.}/:/A/A$/G.HO.= 1N$2$8$8$=E"'(C"&99 ()(+E"I  %qyEBJ ((3(>(>sCE(JI%MM%0!11"1#1J $('c{O`O`Ob'c#cD ###1 /~ $. ) ()s   .(F
F	FFc                   | j                   j                  |d      }|r;t        |      dk7  r0t        j                  d|| j
                  t        |      fz  d       t        |      t        |      k7  r:t        j                  d|| j
                  t        |      t        |      fz  d       t        |      dkD  rL| j                  d|d	d
j                  d t        t        |      t        |      dz         D              d}|S t        |      dkD  r'd| j                  |t        |      t        |      fz  }|S d| j                  |t        |      fz  }|S |rd| j                  |dfz  }|S t        d|d| j
                        )z<Return a test group alias using the given name and fallback.Nr   z-Min test group "%s" in %s is %d instead of 1.T)uniquez.Max test group "%s" in %s is %d instead of %d.	   rA   z/group(|c              3  2   K   | ]  }t        |        y wr"   )str)rH   is     r)   rK   zAIntegrationAliasesTest.format_test_group_alias.<locals>.<genexpr>   s        UX`aUXYZU[  UXs   z)/z%s/%s/group[%d-%d]/z%s/%s/group%d/zcannot find test group "z" in )ra   rL   minr   warningr3   maxlenTEST_ALIAS_PREFIXjoinrange	Exception)r'   r@   fallbackgroup_numbersaliass        r)   format_test_group_aliasz.IntegrationAliasesTest.format_test_group_alias   s   ++//d;=!Q& OSWY]YdYdfijwfxRy y  CG  H=!S%77 PTXZ^ZeZegjkxgy{~  @M  |N  TO  !O  X\  ]=!A%.2.D.DdCHH  UXejkno|k}  @C  DQ  @R  UV  @V  fW  UX  MX  Y  ]#a'-1G1GsS`Oacfgtcu0vv  )D,B,BD#mJ\+]]  $(>(>!'LLE  4UVVr*   c                    t         j                  j                  t        | j                  d      }t        j                  t        |j                  |gt        |      d      d         S )z5Load the specified YAML file and return the contents.zyaml_to_json.pyT)datacapturer   )	ospathrn   r
   r@   jsonloadsr   r   )r'   r4   ry   yaml_to_json_paths       r)   r2   z IntegrationAliasesTest.load_yaml   sM    GGLLdiiARSzz+v{{4E&F^\`Makopqrsttr*   c                H   |j                   rt        | j                        S t        j                  j                  | j                        s-t        | j                  t        d| j                        g      S t        g i       }| j                  |       | j                  ||       t        t        j                  d|j                         g }|| j!                  |      z  }|| j#                         z  }|rt        | j                  |      S t        | j                        S )Nzfile missing)messagery   )messages)commentslabelszdata-sanity-ci.json)explainr   r@   rx   ry   isfiler3   r   r   Resultsr5   check_changesr   r   BOT__dict__check_posix_targetscheck_windows_targets)r'   argsr>   r4   resultsr   s         r)   r?   zIntegrationAliasesTest.test   s    << ++ww~~dkk* m&[[7 6  
 

 	F#4)
0EwGWGWXD,,T22D..00 X>>TYY''r*   c                
   t        t                     }t        ||      }|D cg c]  }d|z  	 }}t        t        |dgd            }t        t        ||dd            }g }|D ]k  }	|	j                  D ]Z  j                  d      sdk7  st        fd|D              r0|j                  t        dz  d|	j                  z               \ m || j                  t        t        |dd	| j                  z  gdd            | j                  d
      j                  d
d      d| j                  z  g      z  }|| j                  t        t        |d	| j                  z  gd            | j                  d            z  }|D ]  }|dk(  r2| j                  d
      j                  d
d      }
d| j                  z  g}n3| j                  |d      }
| j                  d|dd| j                  z  g}|| j                  t        t        |d|z  gd            |
|      z  } t        j                  dt        j                   dt        j"                  dt        j$                  di}|D ]z  }	|	j&                  dk(  r| j                   d|	j                  vr0|	j                  D cg c])  }t)        j*                  d| j                   d|      s(|+ }}||	j,                     D cg c]  }| j                   d| d }}|D cg c]	  }||v s| }}|D cg c]  t        fd|D              r }}|sQ|j                  t        d|	j,                  j&                   ddj/                  |       |	j                   d              |s*|j                  t        d|	j,                  j&                   d!dj/                  |       |	j                   d              } |S c c}w c c}w c c}w c c}w c c}w )"zOCheck POSIX integration test targets and return messages with any issues found.z	cloud/%s/zcloud/Ferrors)includer   c              3  @   K   | ]  }j                  |        y wr"   
startswith)rH   cloud_targetrs   s     r)   rK   z=IntegrationAliasesTest.check_posix_targets.<locals>.<genexpr>   s     \l5++L9\   zinvalid alias `%s`
%s/aliasesz%s/generic/linuxposixz%s/posix/incidental/r>   findfind_incidentalgeneric)r>   r   
httptesterrA   z/incidental/z%s/cloud/incidental/)r   r   )r         r,   zansible-test-containerz/posix/^z/posix/group[0-9]+/$z/posix/groupc              3  @   K   | ]  }j                  |        y wr"   r   )rH   expected_groupr\   s     r)   rK   z=IntegrationAliasesTest.check_posix_targets.<locals>.<genexpr>+  s&       IQn|IYIYZhIi  IQr   zTarget of type z* must be in at least one of these groups: z, z/aliasesz cannot be in these groups: )tupler   r   r   aliasesr   anyappendr   ry   check_ci_grouprm   rt   replacer   TARGET
CONTROLLERCONFLICTUNKNOWNr@   researchtarget_typern   )r'   r   posix_targetscloudscloudcloud_targetsall_cloud_targetsinvalid_cloud_targetsr   r[   r   r   target_type_groupsrs   found_groupsr\   expected_groupsvalid_groupsinvalid_groupss                ` `   r)   r   z*IntegrationAliasesTest.check_posix_targets   s   <>?$T=9:@Au,AA!.
SX"YZ %n5F_dmr&s t+ 	mF m##H-%82C\m\\ OOM2F2NP\_e_j_jPj$klm	m 	D''.=SWSiSiCi8jty  CH  I  J--g6>>wP3d6L6LLM ( 
 	
 	D''.I_I_9_8`inop--i8 ( 
 	

  	E$33G<DDWgV#9D<R<R#R"S33E9E:>:P:PRW#XZpsw  tJ  tJ  [J  #K++n][5=P<QZ_`a / ,  H	 "((&!,,i!**B!))2	
 $ 	IF{{66(()1G/5~~  EeQtOeOeNffzK{  ~C  BDE  EL  E\nou  pB  pB  ]C  DSX$"8"8!9eWAN  DO  D/;Xeu?WEXLX1=  RS  IQ  AP  IQ  FQe  RN  R@R@R@W@W?X  YC  DH  DM  DM  N]  D^  C_  /`17X.F!H I @R@R@W@W?XXtuyu~u~  @N  vO  uP  /Q17X.F!H I#	I( G Bl E DX Rs/   O,*)O1O1+O6
	O;O;P 9P c                    t        t                     }g }|| j                  || j                  d      d| j                  z  g      z  }|S )zQCheck Windows integration test targets and return messages with any issues found.windowsz%s/windows/incidental/r   )r   r   r   rt   rm   )r'   windows_targetsr   s      r)   r   z,IntegrationAliasesTest.check_windows_targets7  sZ     @ BCD''#--i858N8NNO ( 
 	
 r*   c                   t        d |D              }t        d t        ||gd      D              }t        d t        || j                  gd      D              }|rt        d t        ||d      D              }n
t               }||z
  |z
  |z
  }||z  }	d|j                  d      d	| j                  j                  d      d
}
d|j                  d      d| j                  j                  d      d
}g }|D ]&  }|dk(  r	|j	                  t        |
d|z               ( |	D ]   }|j	                  t        |d|z               " |S )zdCheck the CI groups set in the provided targets and return a list of messages with any issues found.c              3  4   K   | ]  }|j                     y wr"   ry   rH   r[   s     r)   rK   z8IntegrationAliasesTest.check_ci_group.<locals>.<genexpr>L  s     ::   c              3  4   K   | ]  }|j                     y wr"   r   r   s     r)   rK   z8IntegrationAliasesTest.check_ci_group.<locals>.<genexpr>M  s     fffkkfr   Fr   c              3  4   K   | ]  }|j                     y wr"   r   r   s     r)   rK   z8IntegrationAliasesTest.check_ci_group.<locals>.<genexpr>N  s     ttr   c              3  4   K   | ]  }|j                     y wr"   r   r   s     r)   rK   z8IntegrationAliasesTest.check_ci_group.<locals>.<genexpr>Q  s     "t66;;"tr   zmissing alias `rA   z` or ``zconflicting alias `z` and `z/test/integration/targets/ansible-test-containerr   )rR   r   UNSUPPORTEDstripr   r   )r'   r>   r   r   	all_pathssupported_pathsunsupported_pathsincidental_pathsunassigned_pathsconflicting_pathsunassigned_messageconflicting_messager   ry   s                 r)   r   z%IntegrationAliasesTest.check_ci_groupE  sZ    :'::	fwQUPV_d8efft.SWScScRdmr:stt""t^GUdmr=s"tt"u$69JJM]]+.??=AZZ_dN^N^NdNdehNijCG::c?TXTdTdTjTjknTop$ 	TDHHOOM*<lT>QRS		T & 	UDOOM*=|d?RST	U r*   c                   t        t                     }t        t                     }t        d |D              }t        d |D              }g }g }g }	|j                  j
                  j                  D 
cg c]	  }
d|
v s|
 c}
D ]  }
|j                  j
                  j                  |
   D ]  }| j                  ||   j                  v r|j                  |       0| j                  ||   j                  v r|j                  |       ]| j                  ||   j                  v sy|	j                  |         g }|j                  j
                  j                  D ]'  }|j                  |      }|s|j                  |       ) | j                  | j                  |      | j                  | j                   |      | j                  | j"                  |	      | j                  | j$                  |      g}|D cg c]  }|s|	 }}t        t'        |      t'        |      t'        |      t'        |	            }|xj(                  |z  c_        |j*                  j-                  |       yc c}
w c c}w )zBCheck changes and store results in the provided result dictionary.c              3  8   K   | ]  }|j                   |f  y wr"   )r@   r   s     r)   rK   z7IntegrationAliasesTest.check_changes.<locals>.<genexpr>m  s     *cVFKK+@*cs   c              3  L   K   | ]  }|j                   |j                  f  y wr"   )ry   moduler   s     r)   rK   z7IntegrationAliasesTest.check_changes.<locals>.<genexpr>n  s     #^VV[[&--$@#^s   "$integration)needs_testsdisabled_testsunstable_testsunsupported_testsN)listr   r   rT   metadatachange_descriptionfocused_command_targetsDISABLEDr   r   UNSTABLEr   no_integration_pathsrL   format_commentTEMPLATE_DISABLEDTEMPLATE_UNSTABLETEMPLATE_UNSUPPORTEDTEMPLATE_UNTESTEDboolr   r   update)r'   r   r   integration_targetsmodule_targetsintegration_targets_by_namemodule_names_by_pathdisabled_targetsunstable_targetsunsupported_targetscommandr[   untested_modulesry   r   r   commentr   s                     r)   r   z$IntegrationAliasesTest.check_changesh  sR   "#;#=>134&**cOb*c&c###^~#^^ /3}}/O/O/g/g  EGkx  }D  lD  E 	7G--::RRSZ[ 7==$?$G$O$OO$++F3]]&A&&I&Q&QQ$++F3%%)DV)L)T)TT'..v67	7 MM44II 	0D)--d3F ''/		0  6 68HI 6 68HI 9 9;NO 6 68HI	
 ,4?wG??-. 01 01"#67	
 	H$f%E E0 @s   3	I(=I(6I->I-c                    |sydj                  d |D              }t        | j                  |      } t        j                  |      j                         j                  di |}|S )zeFormat and return a comment based on the given template and targets, or None if there are no targets.N
c              3  &   K   | ]	  }d |z    yw)z- %sNr,   r   s     r)   rK   z8IntegrationAliasesTest.format_comment.<locals>.<genexpr>  s     @f&6/@s   )explain_urltestsr,   )rn   rT   EXPLAIN_URLtextwrapdedentr   rM   )r'   r9   r>   r   rv   r~   s         r)   r   z%IntegrationAliasesTest.format_comment  s`    		@@@((

 ;(//(+113::BTBr*   )returnNone)r   r   )r4   r   r   dict[str, t.Any])r   zdict[str, list[int]]) )r@   rg   rq   rg   r   rg   )r4   r   ry   rg   r   r   )r   r   r>   r	   r4   r   r   r   )r   r   r   list[SanityMessage])r   r   r"   )r>   ztuple[CompletionTarget, ...]r   rg   r   zt.Optional[list[str]]r   r   )r   r   r   r   r   r   )r9   rg   r>   	list[str]r   zt.Optional[str])__name__
__module____qualname____doc__r3   rm   r   r   r   r   r   r   r   r   r   ansible_onlyr$   propertyr.   r0   r5   ra   rt   r2   r?   r   r   r   r   r   __classcell__)r(   s   @r)   r   r   >   s    ;3F#HH K~K L8     ,$ ,$\0u
(<HT$ 26	!-! ! /	!
 
!F.&`r*   r   c                  &    e Zd ZU dZded<   ded<   y)r   zCheck results.r   r   zdict[str, bool]r   N)r   r   r  r  __annotations__r,   r*   r)   r   r     s    r*   r   )-r  
__future__r   dataclassesrz   r   rx   r   typingtr   r   r   r   r   r	   r
   r?   r   configr   r[   r   r   r   r   r   r   r   integration.cloudr   ior   utilr   r   util_commonr   r   r   host_configsr   r   	dataclassr   r,   r*   r)   <module>r     s    4 "    	 	    
 
h0 hV   r*   