
    !8                         S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKJr	  SSK
Jr  SSK
Jr  SS	K
Jr  SSKJr  SS
KJr  SSKrS r " S S\5      rg)z2Create Android test matrices in Firebase Test Lab.    )absolute_import)division)unicode_literalsN)
exceptions)matrix_creator_common)
matrix_ops)util)logc                 x    [        XX#U5      nUR                  [        R                  " 5       R                  5      $ )a  Creates a new matrix test in Firebase Test Lab from the user's params.

Args:
  args: an argparse namespace. All the arguments that were provided to this
    gcloud command invocation (i.e. group and command arguments combined).
  context: {str:obj} dict containing the gcloud command context, which
    includes the Testing API client+messages libs generated by Apitools.
  history_id: {str} A history ID to publish Tool Results to.
  gcs_results_root: the root dir for a matrix within the GCS results bucket.
  release_track: the release track that the command is invoked from.

Returns:
  A TestMatrix object created from the supplied matrix configuration values.
)MatrixCreatorCreateTestMatrixuuiduuid4hex)argscontext
history_idgcs_results_rootrelease_trackcreators         Blib/googlecloudsdk/api_lib/firebase/test/android/matrix_creator.pyCreateMatrixr   #   s2     $')'		!	!$**,"2"2	33    c                   ~    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS rSrg)r   7   zCCreates a single test matrix based on user-supplied test arguments.c                     [         R                  " 5       U l        Xl        X0l        X@l        US   U l        US   U l        XPl        g)aB  Construct a MatrixCreator to be used to create a single test matrix.

Args:
  args: an argparse namespace. All the arguments that were provided to this
    gcloud command invocation (i.e. group and command arguments combined).
  context: {str:obj} dict containing the gcloud command context, which
    includes the Testing API client+messages libs generated by Apitools.
  history_id: {str} A history ID to publish Tool Results to.
  gcs_results_root: the root dir for a matrix within the GCS results bucket.
  release_track: the release track that the command is invoked from.
testing_clienttesting_messagesN)	r	   
GetProject_project_args_history_id_gcs_results_root_client	_messages_release_track)selfr   r   r   r   r   s         r   __init__MatrixCreator.__init__:   sC     OO%DMJ!-+,DL/0DN'r   c                    UR                  S5      (       aG  SU R                  R                  U R                  [        R
                  R                  U5      5      S94$ U R                  [        R
                  R                  U5      5      S4$ )zABuilds either a FileReference or an AppBundle message for a file.z.aabN)bundleLocation)endswithr%   	AppBundle_BuildFileReferenceospathbasenamer'   filenames     r   _BuildAppReference MatrixCreator._BuildAppReferenceO   s}      4>>++11"''2B2B82LM , O O O %%bgg&6&6x&@A4GGr   c                     U R                   R                  [        R                  R	                  U R
                  U5      S9$ )z9Build a FileReference pointing to the GCS copy of a file.gcsPath)r%   FileReferencer/   r0   joinr#   r2   s     r   r.   !MatrixCreator._BuildFileReferenceW   s8    >>''T33X> ( @ @r   c                     U R                   R                  R                  nU R                  R                  c  UR
                  $ U R                  R                  (       a  UR                  $ UR                  $ )N)r%   AndroidInstrumentationTest!OrchestratorOptionValueValuesEnumr!   use_orchestratorORCHESTRATOR_OPTION_UNSPECIFIEDUSE_ORCHESTRATORDO_NOT_USE_ORCHESTRATOR)r'   orchestrator_optionss     r   _GetOrchestratorOption$MatrixCreator._GetOrchestratorOption\   s]    11	*	*  zz""*!AAA		$	$!222!999r   c           
         / nU R                   R                  R                  nUR                  UR                  UR
                  S.n[        R                  " U=(       d    0 5       HV  u  pV[        R                  " U5      u  pxUR                  U R                   R                  UUUR                  U5      S95        MX     U$ )z9Build a list of RoboDirectives from the dictionary input.)clicktextignore)resourceName	inputText
actionType)r%   RoboDirectiveActionTypeValueValuesEnumSINGLE_CLICK
ENTER_TEXTIGNOREsix	iteritemsr	   ParseRoboDirectiveKeyappendget)	r'   robo_directives_dictrobo_directivesaction_typesaction_type_mappingkeyvalueaction_typeresource_names	            r   _BuildRoboDirectives"MatrixCreator._BuildRoboDirectivesg   s    O>>//IIL**''%%
 mm%9%?RA
%)%?%?%D"{
..
&
&(,00= ' ?@ B r   c                 *   U R                  5       nU R                  U R                  R                  5      u  p#U R                  R                  UUU R                  [        R                  R                  U R                  R                  5      5      U R                  R                  U R                  R                  U R                  R                  U R                  R                  =(       d    / U R                  5       U R!                  5       S9	Ul        U$ )z<Build a TestSpecification for an AndroidInstrumentationTest.)	appApk	appBundletestApkappPackageIdtestPackageIdtestRunnerClasstestTargetsorchestratorOptionshardingOption)_BuildGenericTestSpecr4   r!   appr%   r=   r.   r/   r0   r1   testapp_packagetest_packagetest_runner_classtest_targetsrD   _BuildShardingOptionandroidInstrumentationTestr'   specapp_apk
app_bundles       r   $_BuildAndroidInstrumentationTestSpec2MatrixCreator._BuildAndroidInstrumentationTestSpecy   s    %%'D11$**..AG&*nn&O&O(()9)9$**//)JKZZ++jj--

44ZZ,,2668002 'P 	'4D# Kr   c                    U R                  5       nU R                  U R                  R                  5      u  p#U R                  R
                  R                  n[        U R                  SS5      (       a  UR                  OUR                  nU R                  R                  UUU R                  R                  U R                  U R                  R                  5      US9Ul        [        U R                  SS5      (       aQ  U R                  [        R                   R#                  U R                  R$                  5      5      UR                  l        U$ )z1Build a TestSpecification for an AndroidRoboTest.resignT)rb   rc   re   roboDirectivesroboModerobo_scriptN)rk   r4   r!   rl   r%   AndroidRoboTestRoboModeValueValuesEnumgetattrROBO_VERSION_2ROBO_VERSION_1rn   r_   rX   androidRoboTestr.   r/   r0   r1   r~   
roboScript)r'   ru   rv   rw   
robo_modes	robo_modes         r   _BuildAndroidRoboTestSpec'MatrixCreator._BuildAndroidRoboTestSpec   s   %%'D11$**..AG//GGJ-4

Hd.$ .$
)))3)B)B >>99ZZ++001K1KL : D tzz=$//(,(@(@
''

4::11
2)4d%Kr   c                    U R                  5       nU R                  U R                  R                  5      u  p#U R                  R                  UUU R                  R                  S9Ul        U R                  R                  (       a%  U R                  R                  UR                  l	        U R                  R                  (       a%  U R                  R                  UR                  l        U$ )z1Build a TestSpecification for an AndroidTestLoop.)rb   rc   re   )rk   r4   r!   rl   r%   AndroidTestLooprn   androidTestLoopscenario_numbers	scenariosscenario_labelsscenarioLabelsrt   s       r   _BuildAndroidGameLoopTestSpec+MatrixCreator._BuildAndroidGameLoopTestSpec   s    %%'D11$**..AG>>99ZZ++ : -D zz""'+zz'B'Bd$zz!!,0JJ,F,Fd)Kr   c                    / nU R                   R                  =(       d    /  Hq  n[        R                  R	                  U5      nUR                  U R                  R                  U R                  R                  UU R                  U5      S9S95        Ms     [        U R                   SS5      =(       d    0 nUR                  5        Hf  nUR                  U R                  R                  U R                  R                  U R                  [        R                  " U5      5      US9S95        Mh     / nU R                   R                  (       a[  [         R"                  " U R                   R                  5       H-  u  pxUR                  U R                  R%                  XxS95        M/     U R                   R&                  =(       d    / n	Sn
U R                   R(                  (       a1  U R                  R+                  U R                  R-                  5       S9n
[        U R                   S	/ 5      =(       d    /  Vs/ s HH  nU R                  R/                  U R                  [        R                  R	                  U5      5      S
9PMJ     nn[        U R                   SS5      S:H  nU R                  R1                  UU
UU	[        U R                   SS5      UU(       + S9nU R                  R3                  [4        R6                  " U R                   R8                  5      UU R                   R:                  (       + U R                   R<                  (       + S9$ s  snf )z>Build a generic TestSpecification without test-type specifics.)obbFileNameobb)obbFileother_filesN)content
devicePath)regularFile)r[   r\   )
googleAutoadditional_apks)locationgrant_permissionsallnetwork_profile)filesToPushaccountenvironmentVariablesdirectoriesToPullnetworkProfileadditionalApksdontAutograntPermissions)testTimeout	testSetupdisableVideoRecordingdisablePerformanceMetrics)r!   	obb_filesr/   r0   r1   rU   r%   
DeviceFileObbFiler.   r   keysRegularFiler	   GetRelativeDevicePathenvironment_variablesrR   rS   EnvironmentVariabledirectories_to_pullauto_google_loginAccount
GoogleAutoApk	TestSetupTestSpecificationr   ReformatDurationtimeoutrecord_videoperformance_metrics)r'   device_filesobb_fileobb_file_namer   device_pathr   r[   r\   r   r   additional_apkr   r   setups                  r   rk   #MatrixCreator._BuildGenericTestSpec   s   LJJ((.B.gg&&x0m
..
#
#nn,,+..}= - ? $ @A / $**mT:@bK"'')
..
#
#..442200=?( 5 * $ +, * zz''djj&F&FG*#$$NN..3.D	F H **88>BGzz##&&$..2K2K2M&Ng
 &djj2CRHNBN ON 	--bgg.>.>~.NO 	 	QN    

,? %'*/0 NN$$ 2-tzz+<dC&%6!6 % 8E >>++//

0B0BC"&**"9"99&*jj&D&D"D	 , F F%s   8AMc                 t   [        U R                  S0 5      (       aD  U R                  R                  U R                  R	                  U R                  R
                  S9S9$ [        U R                  S0 5      (       a<  U R                  R                  U R                  U R                  R                  5      S9$ g)z9Build a ShardingOption for an AndroidInstrumentationTest.num_uniform_shards)	numShards)uniformShardingtest_targets_for_shard)manualShardingN)r   r!   r%   ShardingOptionUniformShardingr   _BuildManualShardr   )r'   s    r   rr   "MatrixCreator._BuildShardingOption   s    tzz/44^^**..88

55 9 7 + 8 8 
5r	:	:^^**//jj//1 + 2 2 
;r   c                     U Vs/ s H  nU R                  U5      PM     nnU R                  R                  US9$ s  snf )z)Build a ManualShard for a ShardingOption.)testTargetsForShard)_BuildTestTargetsForShardr%   ManualSharding)r'   r   test_targetrq   s       r   r   MatrixCreator._BuildManualShard   sN     21K 	&&{31   >>((\(JJ	s   ;c                     U R                   R                  UR                  S5       Vs/ s H
  nUc  M  UPM     snS9$ s  snf )N;)rh   )r%   TestTargetsForShardsplit)r'   test_targets_for_each_shardtargets      r   r   'MatrixCreator._BuildTestTargetsForShard   sJ    >>--8>>sC;C6 	C;-   ;s   ??c                     US:X  a  U R                  5       $ US:X  a  U R                  5       $ US:X  a  U R                  5       $ [        R                  " SSR                  U5      5      e)zAMap a test type into its corresponding TestSpecification message.instrumentationroboz	game-looptypezUnknown test type "{}".)rx   r   r   r   InvalidArgumentExceptionformat)r'   	test_types     r   _TestSpecFromTypeMatrixCreator._TestSpecFromType   sl    %%6688	f	++--	k	!//11//
+229=? ?r   c           	         U R                   R                  (       ae  U R                   R                   Vs/ s H  o R                  U5      PM     nnU R                  R	                  U R                  R                  US9S9nOU R                  R	                  U R                  R                  U R                   R                  U R                   R                  U R                   R                  U R                   R                  S9S9nU R                  R                  U R                  S9nU R                  R                  U R                  U R                  S9nU R                  R!                  UUS9n["        R$                  " U R                  ['        U R                   S0 5      =(       d    0 U R(                  5      nU R                  R+                  UUUUU R                   R,                  =(       d    S	S
9$ s  snf )zBuild just the user-specified parts of a TestMatrix message.

Args:
  spec: a TestSpecification message corresponding to the test type.

Returns:
  A TestMatrix message.
)androidDevices)androidDeviceList)androidModelIdsandroidVersionIdslocalesorientations)androidMatrixr7   )	projectId	historyId)googleCloudStoragetoolResultsHistoryclient_detailsr   )testSpecificationenvironmentMatrix
clientInforesultStorageflakyTestAttempts)r!   device_BuildAndroidDevicer%   EnvironmentMatrixAndroidDeviceListAndroidMatrix
device_idsos_version_idsr   r   GoogleCloudStorager#   ToolResultsHistoryr    r"   ResultStorager   BuildClientInfor   r&   
TestMatrixnum_flaky_test_attempts)	r'   ru   ddevicesenvironment_matrixgcshistresultsclient_infos	            r   _BuildTestMatrixMatrixCreator._BuildTestMatrix  s    zz6:jj6G6GH6G))!,6GgH>>;; NN<<$ = & < '  >>;;44"jj33 $

 9 9jj((::22	 5 4 < 5 ..
+
+D4J4J
+
KC>>,,t}}7;7G7G - IDnn**c>B + DG (77

,b17R9L9LNK >>$$,**<<A % C C- Is   G3c                 R    U R                   R                  US   US   US   US   S9$ )Nmodelversionlocaleorientation)androidModelIdandroidVersionIdr  r  )r%   AndroidDevice)r'   
device_maps     r   r   !MatrixCreator._BuildAndroidDevice+  s>    >>''!'*#I.(#}-	 ( / /r   c                     U R                  U R                  R                  5      nU R                  R	                  U R
                  U R                  U5      US9$ )zBuild a TestingProjectsTestMatricesCreateRequest for a test matrix.

Args:
  request_id: {str} a unique ID for the CreateTestMatrixRequest.

Returns:
  A TestingProjectsTestMatricesCreateRequest message.
)r   
testMatrix	requestId)r   r!   r   r%   (TestingProjectsTestMatricesCreateRequestr    r  )r'   
request_idru   s      r   _BuildTestMatrixRequest%MatrixCreator._BuildTestMatrixRequest2  sQ     !!$**//2D>>BB--((. C  r   c                 
   U R                  U5      n[        R                  " SR                  U5      5         U R                  R
                  R                  U5      n[        R                  " SR                  U5      5        [        R                  R                  SR                  UR                  S95        U$ ! [        R                   a4  nS[        R                  " U5      -   n[        R                  " U5      eSnAff = f)a"  Invoke the Testing service to create a test matrix from the user's args.

Args:
  request_id: {str} a unique ID for the CreateTestMatrixRequest.

Returns:
  The TestMatrix response message from the TestMatrices.Create rpc.

Raises:
  HttpException if the test service reports an HttpError.
z!TestMatrices.Create request:
{0}
z"TestMatrices.Create response:
{0}
z'Http error while creating test matrix: Nz1Test [{id}] has been created in the Google Cloud.)id)r  r
   debugr   r$   projects_testMatricesCreateapitools_exceptions	HttpErrorr	   GetErrorr   HttpExceptionstatusPrinttestMatrixId)r'   r  requestresponseerrormsgs         r   r   MatrixCreator.CreateTestMatrixA  s     **:6GII3::7CD*33::7Ch	ii6==hGH
 JJHf 5 5f68O (( *5e8LLc$$S))*s   A
B: :D/C==D)r!   r$   r#   r"   r%   r    r&   N)__name__
__module____qualname____firstlineno____doc__r(   r4   r.   rD   r_   rx   r   r   rk   rr   r   r   r   r  r   r  r   __static_attributes__ r   r   r   r   7   sa    K(*H@
	:$ $4Fl	2K
?%CN/r   r   )r4  
__future__r   r   r   r/   r   apitools.base.pyr   r$  $googlecloudsdk.api_lib.firebase.testr   r   r	   googlecloudsdk.calliopegooglecloudsdk.corer
   rR   r   objectr   r6  r   r   <module>r=     sA     9 &  ' 	  > F ; 5 . # 
4(aF ar   