def CreateTestMatrix(self, request_id): """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. """ request = self._BuildTestMatrixRequest(request_id) log.debug('TestMatrices.Create request:\n{0}\n'.format(request)) try: response = self._client.projects_testMatrices.Create(request) log.debug('TestMatrices.Create response:\n{0}\n'.format(response)) except apitools_exceptions.HttpError as error: msg = 'Http error while creating test matrix: ' + util.GetError( error) raise exceptions.HttpException(msg) log.status.Print( 'Test [{id}] has been created in the Google Cloud.'.format( id=response.testMatrixId)) return response
def _ListSteps(self, page_token): """Lists one page of steps using the Tool Results service. Args: page_token: A page token to attach to the List request. Returns: A ListStepsResponse containing a single page's steps. Raises: HttpException if the Tool Results service reports a back-end error. """ request = (self._messages. ToolresultsProjectsHistoriesExecutionsStepsListRequest( projectId=self._project, historyId=self._history_id, executionId=self._execution_id, pageSize=100, pageToken=page_token)) try: response = self._client.projects_histories_executions_steps.List( request) log.debug( '\nToolResultsSteps.List response:\n{0}\n'.format(response)) return response except apitools_exceptions.HttpError as error: msg = 'Http error while listing test results: ' + util.GetError( error) raise exceptions.HttpException(msg)
def EnsureBucketExists(self, bucket_name): """Create a GCS bucket if it doesn't already exist. Args: bucket_name: the name of the GCS bucket to create if it doesn't exist. Raises: BadFileException if the bucket name is malformed, the user does not have access rights to the bucket, or the bucket can't be created. """ get_req = self._storage_messages.StorageBucketsGetRequest( bucket=bucket_name) try: self._storage_client.buckets.Get(get_req) return # The bucket exists and the user can access it. except apitools_exceptions.HttpError as err: code, err_msg = util.GetErrorCodeAndMessage(err) if code != HTTP_NOT_FOUND: raise exceptions.BadFileException( 'Could not access bucket [{b}]. Response error {c}: {e}. ' 'Please supply a valid bucket name or use the default bucket ' 'provided by Firebase Test Lab.'.format(b=bucket_name, c=code, e=err_msg)) # The bucket does not exist in any project, so create it in user's project. log.status.Print( 'Creating results bucket [{g}{b}] in project [{p}].'.format( g=GCS_PREFIX, b=bucket_name, p=self._project)) bucket_req = self._storage_messages.StorageBucketsInsertRequest acl = bucket_req.PredefinedAclValueValuesEnum.projectPrivate objacl = bucket_req.PredefinedDefaultObjectAclValueValuesEnum.projectPrivate insert_req = self._storage_messages.StorageBucketsInsertRequest( bucket=self._storage_messages.Bucket(name=bucket_name), predefinedAcl=acl, predefinedDefaultObjectAcl=objacl, project=self._project) try: self._storage_client.buckets.Insert(insert_req) return except apitools_exceptions.HttpError as err: code, err_msg = util.GetErrorCodeAndMessage(err) if code == HTTP_FORBIDDEN: msg = ('Permission denied while creating bucket [{b}]. ' 'Is billing enabled for project: [{p}]?'.format( b=bucket_name, p=self._project)) else: msg = ('Failed to create bucket [{b}] {e}'.format( b=bucket_name, e=util.GetError(err))) raise exceptions.BadFileException(msg)
def CancelTestMatrix(self): """Cancels an in-progress TestMatrix. Raises: HttpException if the Test service reports a back-end error. """ request = self._messages.TestingProjectsTestMatricesCancelRequest( projectId=self._project, testMatrixId=self.matrix_id) try: self._client.projects_testMatrices.Cancel(request) except apitools_exceptions.HttpError as error: msg = 'Http error from CancelTestMatrix: ' + util.GetError(error) raise exceptions.HttpException(msg)
def UploadFileToGcs(self, path): """Upload a file to the GCS results bucket using the storage API. Args: path: str, the absolute or relative path of the file to upload. File may be in located in GCS or the local filesystem. Raises: BadFileException if the file upload is not successful. """ log.status.Print( 'Uploading [{f}] to Firebase Test Lab...'.format(f=path)) try: if path.startswith(GCS_PREFIX): # Perform a GCS object to GCS object copy file_bucket, file_obj = _SplitBucketAndObject(path) copy_req = self._storage_messages.StorageObjectsCopyRequest( sourceBucket=file_bucket, sourceObject=file_obj, destinationBucket=self._results_bucket, destinationObject='{obj}/{name}'.format( obj=self._gcs_object_name, name=os.path.basename(file_obj))) self._storage_client.objects.Copy(copy_req) else: # Perform a GCS insert of a file which is not in GCS try: file_size = os.path.getsize(path) except os.error: raise exceptions.BadFileException( '[{0}] not found or not accessible'.format(path)) src_obj = self._storage_messages.Object(size=file_size) upload = transfer.Upload.FromFile( path, mime_type='application/vnd.android.package-archive') insert_req = self._storage_messages.StorageObjectsInsertRequest( bucket=self._results_bucket, name='{obj}/{name}'.format(obj=self._gcs_object_name, name=os.path.basename(path)), object=src_obj) response = self._storage_client.objects.Insert(insert_req, upload=upload) if response.size != file_size: raise exceptions.BadFileException( 'Cloud storage upload failure: Insert response.size={0} bytes ' 'but [{1}] contains {2} bytes.\nInsert response: {3}'. format(response.size, path, file_size, repr(response))) except apitools_exceptions.HttpError as err: raise exceptions.BadFileException( 'Could not copy [{f}] to [{gcs}] {e}.'.format( f=path, gcs=self.gcs_results_root, e=util.GetError(err)))
def GetTestMatrixStatus(self): """Fetch the response from the GetTestMatrix rpc. Returns: A TestMatrix message holding the current state of the created tests. Raises: HttpException if the Test service reports a backend error. """ request = self._messages.TestingProjectsTestMatricesGetRequest( projectId=self._project, testMatrixId=self.matrix_id) try: return self._client.projects_testMatrices.Get(request) except apitools_exceptions.HttpError as error: msg = 'Http error while monitoring test run: ' + util.GetError( error) raise exceptions.HttpException(msg)
def _ListHistoriesByName(self, history_name): """Lists histories by name using the Tool Results API. Args: history_name: string containing the history name. Returns: A list of histories matching the name. Raises: HttpException if the Tool Results service reports a backend error. """ request = self._messages.ToolresultsProjectsHistoriesListRequest( projectId=self._project, filterByName=history_name) try: response = self._client.projects_histories.List(request) log.debug('\nToolResultsHistories.List response:\n{0}\n'.format(response)) return response except apitools_exceptions.HttpError as error: msg = ('Http error while getting list of Tool Results Histories:\n{0}' .format(util.GetError(error))) raise exceptions.HttpException(msg)
def FetchMatrixRollupOutcome(self): """Gets a test execution's rolled-up outcome from the Tool Results service. Returns: The rolled-up test execution outcome (type: toolresults_v1beta3.Outcome). Raises: HttpException if the Tool Results service reports a back-end error. """ request = self._messages.ToolresultsProjectsHistoriesExecutionsGetRequest( projectId=self._project, historyId=self._history_id, executionId=self._execution_id) try: response = self._client.projects_histories_executions.Get(request) log.debug('\nTRHistoriesExecutions.Get response:\n{0}\n'.format( response)) return response.outcome except apitools_exceptions.HttpError as error: msg = 'Http error fetching test roll-up outcome: ' + util.GetError( error) raise exceptions.HttpException(msg)
def _CreateHistory(self, history_name): """Creates a History using the Tool Results API. Args: history_name: string containing the name of the history to create. Returns: The history id of the created history. Raises: HttpException if the Tool Results service reports a backend error. """ history = self._messages.History(name=history_name) request = self._messages.ToolresultsProjectsHistoriesCreateRequest( projectId=self._project, history=history) try: response = self._client.projects_histories.Create(request) log.debug('\nToolResultsHistories.Create response:\n{0}\n'.format( response)) return response except apitools_exceptions.HttpError as error: msg = ('Http error while creating a Tool Results History:\n{0}'. format(util.GetError(error))) raise exceptions.HttpException(msg)