Ejemplo n.º 1
0
    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_base.HttpError as error:
            msg = 'Http error while listing test results: ' + util.GetError(
                error)
            raise exceptions.HttpException(msg)
Ejemplo n.º 2
0
    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.
    """
        if self._storage_client is None:
            return  # Bypass so some tests won't need to mock all the API calls

        get_req = storage_v1.StorageBucketsGetRequest(bucket=bucket_name)
        try:
            self._storage_client.buckets.Get(get_req)
            return  # The bucket exists and the user can access it.
        except apitools_base.HttpError as err:
            code, err_msg = util.GetErrorCodeAndMessage(err)
            if code != ERROR_NOTFOUND:
                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 Google Cloud 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 = storage_v1.StorageBucketsInsertRequest
        acl = bucket_req.PredefinedAclValueValuesEnum.projectPrivate
        objacl = bucket_req.PredefinedDefaultObjectAclValueValuesEnum.projectPrivate

        insert_req = storage_v1.StorageBucketsInsertRequest(
            bucket=storage_v1.Bucket(name=bucket_name),
            predefinedAcl=acl,
            predefinedDefaultObjectAcl=objacl,
            project=self._project)
        try:
            self._storage_client.buckets.Insert(insert_req)
            return
        except apitools_base.HttpError as err:

            code, err_msg = util.GetErrorCodeAndMessage(err)
            if code == 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)
Ejemplo n.º 3
0
 def _GetDefaultBucket(self, tr_client, tr_messages):
     """Fetch the project's default GCS bucket name for storing tool results."""
     request = tr_messages.ToolresultsProjectsInitializeSettingsRequest(
         projectId=self._project)
     try:
         response = tr_client.projects.InitializeSettings(request)
         return response.defaultBucket
     except apitools_base.HttpError as error:
         msg = (
             'Http error while trying to fetch the default results bucket:\n{0}'
             .format(util.GetError(error)))
         raise exceptions.HttpException(msg)
Ejemplo n.º 4
0
    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_base.HttpError as error:
            msg = 'Http error from CancelTestMatrix: ' + util.GetError(error)
            raise exceptions.HttpException(msg)
Ejemplo n.º 5
0
    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 the Cloud 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 = storage_v1.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)
                    file_stream = open(path, 'r')
                except os.error:
                    raise exceptions.BadFileException(
                        '[{0}] not found or not accessible'.format(path))
                src_obj = storage_v1.Object(size=file_size)
                upload = apitools_base.Upload.FromStream(
                    file_stream,
                    mime_type='application/vnd.android.package-archive',
                    total_size=file_size)
                insert_req = storage_v1.StorageObjectsInsertRequest(
                    bucket=self._results_bucket,
                    name='{obj}/{name}'.format(obj=self._gcs_object_name,
                                               name=os.path.basename(path)),
                    object=src_obj)
                self._storage_client.objects.Insert(insert_req, upload=upload)
        except apitools_base.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)))
Ejemplo n.º 6
0
    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_base.HttpError as error:
            msg = 'Http error while monitoring test run: ' + util.GetError(
                error)
            raise exceptions.HttpException(msg)
Ejemplo n.º 7
0
  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_base.HttpError as error:
      msg = 'Http error fetching test roll-up outcome: ' + util.GetError(error)
      raise exceptions.HttpException(msg)
Ejemplo n.º 8
0
  def CreateTestMatrix(self):
    """Invoke the Testing service to create a test matrix from the user's args.

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

    Raises:
      HttpException if the test service reports an HttpError.
    """
    request = self._BuildTestMatrixRequest()
    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_base.HttpError as error:
      log.debug(
          'TestMatrices.Create reported HttpError:\n{0}'.format(error))
      raise exceptions.HttpException(util.GetError(error))

    log.status.Print('Test [{id}] has been created in the Google Cloud.'
                     .format(id=response.testMatrixId))
    return response
Ejemplo n.º 9
0
  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_base.HttpError as error:
      msg = ('Http error while getting list of Tool Results Histories:\n{0}'
             .format(util.GetError(error)))
      raise exceptions.HttpException(msg)
Ejemplo n.º 10
0
  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_base.HttpError as error:
      msg = ('Http error while creating a Tool Results History:\n{0}'
             .format(util.GetError(error)))
      raise exceptions.HttpException(msg)