def testGetMetadata_remoteAncestry(self, mock_open_file):
    # create hierarchy of test_runs
    test = self._createMockTest()
    child, parent = self._createMockTestRuns(test=test, count=2)  
    with tempfile.NamedTemporaryFile(suffix='.zip') as tmp_stream:
      # write parent metadata to a temporary zip file
      with zipfile.ZipFile(tmp_stream, 'w') as context_file:
        res = self.app.get('/_ah/api/mtt/v1/test_runs/%s/metadata' %
                           parent.key.id())
        context_file.writestr('mtt.json', res.body)
      tmp_stream.seek(0)
      mock_open_file.return_value = tmp_stream

      # mark child as a remote return
      child.prev_test_context = ndb_models.TestContextObj(test_resources=[
          ndb_models.TestResourceObj(name='mtt.json', url='url')
      ])
      child.put()

      res = self.app.get('/_ah/api/mtt/v1/test_runs/%s/metadata' %
                         child.key.id())

      # metadata is sent back w/ ordered ancestry information
      metadata = protojson.decode_message(messages.TestRunMetadataList,
                                          res.body)
      expected = messages.TestRunMetadataList(
          test_runs=[
              messages.TestRunMetadata(
                  test_run=messages.Convert(child, messages.TestRun)),
              messages.TestRunMetadata(
                  test_run=messages.Convert(parent, messages.TestRun))
          ],
          server_version=env.VERSION)
      self.assertEqual(expected, metadata)
  def testGetMetadata_ancestry(self, mock_get_request):
    mock_get_request.return_value = mock.MagicMock()
    test = self._createMockTest()

    # create hierarchy of test_runs
    child, parent, grandparent = self._createMockTestRuns(test=test, count=3)      child.prev_test_run_key = parent.key
    child.prev_test_context = ndb_models.TestContextObj()
    child.put()
    parent.prev_test_run_key = grandparent.key
    parent.prev_test_context = ndb_models.TestContextObj()
    parent.put()

    res = self.app.get('/_ah/api/mtt/v1/test_runs/%s/metadata' % child.key.id())

    # metadata is sent back w/ ordered ancestry information
    metadata = protojson.decode_message(messages.TestRunMetadataList, res.body)
    expected = messages.TestRunMetadataList(
        test_runs=[
            messages.TestRunMetadata(
                test_run=messages.Convert(child, messages.TestRun)),
            messages.TestRunMetadata(
                test_run=messages.Convert(parent, messages.TestRun)),
            messages.TestRunMetadata(
                test_run=messages.Convert(grandparent, messages.TestRun))
        ],
        server_version=env.VERSION)
    self.assertEqual(expected, metadata)
  def testGetMetadata_returnsCompletedAttempts(self, mock_get_request):
    # request w/ RUNNING and COMPLETED attempts
    mock_request = mock.MagicMock()
    mock_get_request.return_value = mock_request
    running_attempt = self._createAttempt(CommandState.RUNNING)
    completed_attempt = self._createAttempt(CommandState.COMPLETED)
    mock_request.command_attempts = [running_attempt, completed_attempt]

    test = self._createMockTest()
    test_run = self._createMockTestRuns(test=test)[0]
    test_run.request_id = 'request'
    test_run.put()
    res = self.app.get(
        '/_ah/api/mtt/v1/test_runs/%s/metadata' % test_run.key.id())

    # metadata is sent back w/ only COMPLETED attempts
    metadata = protojson.decode_message(messages.TestRunMetadataList, res.body)
    expected = messages.TestRunMetadataList(
        test_runs=[
            messages.TestRunMetadata(
                test_run=messages.Convert(test_run, messages.TestRun),
                command_attempts=[completed_attempt])
        ],
        server_version=env.VERSION)
    self.assertEqual(expected, metadata)
Beispiel #4
0
    def GetMetadata(self, request):
        """Returns a test run's metadata.

    Parameters:
      test_run_id: Test run ID
    """
        test_run = ndb_models.TestRun.get_by_id(request.test_run_id)
        if not test_run:
            raise endpoints.NotFoundException('No test run found for ID %s' %
                                              request.test_run_id)
        return mtt_messages.TestRunMetadataList(
            test_runs=self._GetMetadataList(test_run),
            server_version=env.VERSION)
  def testGetMetadata(self, mock_get_request):
    # request w/o attempts
    mock_get_request.return_value = mock.MagicMock()

    test = self._createMockTest()
    test_run = self._createMockTestRuns(test=test)[0]
    test_run.request_id = 'request'
    test_run.put()
    res = self.app.get(
        '/_ah/api/mtt/v1/test_runs/%s/metadata' % test_run.key.id())

    # metadata is sent back w/o attempts
    metadata = protojson.decode_message(messages.TestRunMetadataList, res.body)
    expected = messages.TestRunMetadataList(
        test_runs=[
            messages.TestRunMetadata(
                test_run=messages.Convert(test_run, messages.TestRun))
        ],
        server_version=env.VERSION)
    self.assertEqual(expected, metadata)