
    [(                         d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ	 ddl
mZ ddl
mZ dd	l
mZ ddlmZ dd
lmZ d Z G d de      Zy)z.Create iOS test matrices in Firebase Test Lab.    )absolute_import)division)unicode_literalsN)
exceptions)matrix_creator_common)
matrix_ops)util)logc                 z    t        | ||||      }|j                  t        j                         j                        S )a  Creates a new iOS 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         >lib/googlecloudsdk/api_lib/firebase/test/ios/matrix_creator.pyCreateMatrixr   !   s7     $5E')'		!	!$**,"2"2	33    c                   T    e Zd ZdZd ZddZd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)r   zGCreates a single iOS test matrix based on user-supplied test arguments.c                     t        j                         | _        || _        || _        || _        |d   | _        |d   | _        || _        y)ag  Construct an 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__zMatrixCreator.__init__8   sK     OO%DMDJ!D-D+,DL/0DN'Dr   c                     |sy|rt         j                  j                  |      }t         j                  j                  | j                  |      }| j
                  j                  |      S )z0Build a FileReference pointing to a file in GCS.NgcsPath)ospathbasenamejoinr"   r$   FileReference)r&   filenameuse_basenamer,   s       r   _BuildFileReferencez!MatrixCreator._BuildFileReferenceM   sQ    !!(+h77<<..9D>>'''55r   c           
         t        | j                  dg       xs g D cg c]0  }| j                  t        j                  j                  |            2 }}g }t        | j                  dg       xs g D ]r  }d|v rA|j                  d      \  }}|j                  | j                  j                  ||             H|j                  | j                  j                  |             t g }t        | j                  dd      xs i }|j                         D ]  }	|	j                  d      }
|
dk7  r|	d|
 nd}|
dk7  r|	|
d	z   d n|	}|j                  | j                  j                  | j                  t        j                  |      d
      ||              | j                  j                  t        | j                  dd      |||      S c c}w )z&Build an IosTestSetup for an iOS test.additional_ipasdirectories_to_pull:)bundleId
devicePath)r8   other_filesN   F)r1   )contentr7   r8   network_profile)networkProfileadditionalIpas	pushFilespullDirectories)getattrr    r2   r+   r,   r-   splitappendr$   IosDeviceFilekeysfindr	   GetRelativeDevicePathIosTestSetup)r&   additional_ipar4   r5   	directorybundler,   device_filesr9   device_pathidx	bundle_ids               r   _BuildGenericTestSetupz$MatrixCreator._BuildGenericTestSetupV   s    &djj2CRHNBNNN 	  !1!1.!ABN   TZZ)>CIrI				 s+""NN((&T(J	L 	""NN((I(>	@ J L$**mT:@bK"'') S!c'*by+ds#di&)Ri[q"[d
..
&
&..,,T2 / H 	 '   * >>&&tzz+<dC&+	 ' - -7s   5G
c                    | j                   j                  | j                  j                   | j	                         t        j                  | j                  j                        | j                   j                  | j                  | j                  j                        | j                  | j                  j                        | j                  j                  t        | j                  dd                  }|S )+Build a TestSpecification for an IosXcTest.test_special_entitlementsF)testsZip	xctestrunxcodeVersiontestSpecialEntitlements)disableVideoRecordingiosTestSetuptestTimeout	iosXcTest)r$   TestSpecificationr    record_videorQ   r   ReformatDurationtimeout	IosXcTestr2   testxctestrun_filexcode_versionrB   r&   specs     r   _BuildIosXcTestSpecz!MatrixCreator._BuildIosXcTestSpecy   s    >>++"&**"9"99002//

0B0BC..**--djjoo>..tzz/H/HI11$+DJJ,G,1%3	 + 4	 , 
5D Kr   c           
      |   | j                   j                  | j                  j                   | j	                         t        j                  | j                  j                        | j                   j                  | j                  | j                  j                        | j                  j                              }|S )rS   )appIpa	scenarios)rY   rZ   r[   iosTestLoop)r$   r]   r    r^   rQ   r   r_   r`   IosTestLoopr2   appscenario_numbersre   s     r   _BuildIosTestLoopTestSpecz'MatrixCreator._BuildIosTestLoopTestSpec   s    >>++"&**"9"99002//

0B0BCNN..++DJJNN;jj11 / 3	 , 4D Kr   c           
      "   | j                   j                  | j                  j                   | j	                         t        j                  | j                  j                        | j                   j                  | j                  | j                  j                                    }t        | j                  dd      rQ| j                  t        j                  j                  | j                  j                              |j                   _        |S )z/Build a TestSpecification for an iOS Robo test.)ri   )rY   rZ   r[   iosRoboTestrobo_scriptN)r$   r]   r    r^   rQ   r   r_   r`   IosRoboTestr2   rm   rB   r+   r,   r-   rr   rq   
roboScriptre   s     r   _BuildIosRoboTestSpecz#MatrixCreator._BuildIosRoboTestSpec   s    >>++"&**"9"99002//

0B0BCNN..++DJJNN; / =	 , >D tzz=$/$($<$<
''

4::11
2%4d!Kr   c                     |dk(  r| j                         S |dk(  r| j                         S |dk(  r| j                         S t        j                  ddj                  |            )zBMap a test type into its corresponding TestSpecification message .xctestz	game-looprobotypezUnknown test type "{}".)rg   ro   ru   r   InvalidArgumentExceptionformat)r&   	test_types     r   _TestSpecFromTypezMatrixCreator._TestSpecFromType   sk    H%%''	k	!++--	f	''))//
+229=? ?r   c                    | j                   j                  D cg c]  }| j                  |       }}| j                  j	                  | j                  j                  |            }| j                  j                  | j                        }| j                  j                  | j                  | j                        }| j                  j                  ||      }t        j                  | j                  t        | j                   di       xs i | j                        }| j                  j!                  ||||| j                   j"                  xs d      S c c}w )	zBuild just the user-specified parts of an iOS TestMatrix message.

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

    Returns:
      A TestMatrix message.
    )
iosDevices)iosDeviceListr)   )	projectId	historyId)googleCloudStoragetoolResultsHistoryclient_detailsr   )testSpecificationenvironmentMatrix
clientInforesultStorageflakyTestAttempts)r    device_BuildIosDevicer$   EnvironmentMatrixIosDeviceListGoogleCloudStorager"   ToolResultsHistoryr   r!   ResultStorager   BuildClientInforB   r%   
TestMatrixnum_flaky_test_attempts)	r&   rf   ddevicesenvironment_matrixgcshistresultsclient_infos	            r   _BuildTestMatrixzMatrixCreator._BuildTestMatrix   s@    15

0A0AB0A1t##A&0AGB99nn22g2F : H ..
+
+D4J4J
+
KC>>,,--4+;+; - =Dnn**4 + 9G (77

,b17R9L9LNK >>$$,**<<A % C C Cs   Ec                 X    | j                   j                  |d   |d   |d   |d         S )Nmodelversionlocaleorientation)
iosModelIdiosVersionIdr   r   )r$   	IosDevice)r&   
device_maps     r   r   zMatrixCreator._BuildIosDevice   s>    >>##g&	*(#}-	 $ / /r   c                     | j                  | j                  j                        }| j                  j	                  | j
                  | j                  |      |      S )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    ry   r$   (TestingProjectsTestMatricesCreateRequestr   r   )r&   
request_idrf   s      r   _BuildTestMatrixRequestz%MatrixCreator._BuildTestMatrixRequest   sQ     !!$**//2D>>BB--((. C  r   c                    | j                  |      }t        j                  dj                  |             	 | j                  j
                  j                  |      }t        j                  dj                  |             t        j                  j                  dj                  |j                               |S # t        j                  $ r2}dt        j                  |      z   }t        j                  |      d}~ww xY w)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   zMatrixCreator.CreateTestMatrix   s     **:6GII3::7CD*33::7Ch	ii6==hGH
 JJHOO   P " #O (( *5e8LLc$$S))*s   A	B; ;D -C;;D N)T)__name__
__module____qualname____doc__r'   r2   rQ   rg   ro   ru   r}   r   r   r   r    r   r   r   r   5   s>    O(*6!-F	
?C</r   r   )r   
__future__r   r   r   r+   r   apitools.base.pyr   r   $googlecloudsdk.api_lib.firebase.testr   r   r	   googlecloudsdk.calliopegooglecloudsdk.corer
   r   objectr   r   r   r   <module>r      s>    5 &  ' 	  > F ; 5 . #4(BF Br   