Ejemplo n.º 1
0
def _GetTestPackageInfo(cache_url):
  """Retrieve the test package information at a specified resource URL."""
  stream = file_util.OpenFile(cache_url)
  test_suite_info = file_util.GetTestSuiteInfo(stream)
  if test_suite_info:
    return ndb_models.TestPackageInfo(
        build_number=test_suite_info.build_number,
        target_architecture=test_suite_info.target_architecture,
        name=test_suite_info.name,
        fullname=test_suite_info.fullname,
        version=test_suite_info.version)
  def testTrackTestRun(self, mock_log):
    # configure test run
    test_run = self._CreateMockTestRun()
    test_run.test_package_info = ndb_models.TestPackageInfo(
        name='name', version='version')

    # check that right data was logged
    test_kicker._TrackTestRun(test_run)
    mock_log.assert_called_with(analytics.TEST_RUN_CATEGORY,
                                analytics.START_ACTION,
                                test_name='name',
                                test_version='version',
                                is_rerun=False)
  def testListSummaries_filter(self):
    # Helper to fetch filtered test runs and verify/parse response.
    def FetchTestRuns(*args):
      filters = '&'.join(['filter_query=' + query for query in args])
      res = self.app.get('/_ah/api/mtt/v1/test_runs?' + filters)
      self.assertEqual('200 OK', res.status)
      return protojson.decode_message(messages.TestRunSummaryList,
                                      res.body).test_runs

    # Create placeholder test runs with a variety of property values
    runs1 = self._createMockTestRunSummaries(
        test=self._createMockTest(name='foo'),
        labels=['bar'],
        device_specs=['baz'])
    msg1 = messages.Convert(runs1[0], messages.TestRunSummary)
    runs2 = self._createMockTestRunSummaries(
        test=self._createMockTest(),
        labels=['common'],
        test_devices=[
            ndb_models.TestDeviceInfo(build_id='qux', product='spam')
        ])
    msg2 = messages.Convert(runs2[0], messages.TestRunSummary)
    runs3 = self._createMockTestRunSummaries(
        test=self._createMockTest(name='common'),
        test_package_info=ndb_models.TestPackageInfo(
            name='ham', version='eggs'))
    msg3 = messages.Convert(runs3[0], messages.TestRunSummary)

    # Can search using individual properties
    self.assertEqual([], FetchTestRuns('unknown'))
    self.assertEqual([msg1], FetchTestRuns('foo'))  # test name
    self.assertEqual([msg1], FetchTestRuns('bar'))  # label
    self.assertEqual([msg1], FetchTestRuns('baz'))  # run target
    self.assertEqual([msg2], FetchTestRuns('qux'))  # device build ID
    self.assertEqual([msg2], FetchTestRuns('spam'))  # device product
    self.assertEqual([msg3], FetchTestRuns('ham'))  # test package name
    self.assertEqual([msg3], FetchTestRuns('eggs'))  # test package version
    self.assertEqual([msg3, msg2], FetchTestRuns('common'))

    # Can search for union of multiple values
    self.assertEqual([msg1], FetchTestRuns('foo', 'bar', 'baz'))
    self.assertEqual([msg2], FetchTestRuns('common', 'qux', 'spam'))
    self.assertEqual([msg3], FetchTestRuns('common', 'ham', 'eggs'))
    self.assertEqual([], FetchTestRuns('foo', 'common'))
    def testTrackTestRun(self, mock_time, mock_get_request, mock_log):
        # mock current time
        now = datetime.datetime.now()
        mock_time.return_value = now

        # configure mock TFC request with 1 attempt and 2 devices
        request_id = self.mock_test_run.request_id
        mock_request = api_messages.RequestMessage(id=request_id)
        mock_request.command_attempts.append(
            api_messages.CommandAttemptMessage(
                device_serials=['1', '2'],
                start_time=now,
                end_time=now,
                state=common.CommandState.COMPLETED))
        mock_get_request.return_value = mock_request

        # update mock test run
        self.mock_test_run.test_package_info = ndb_models.TestPackageInfo(
            name='name', version='version')
        self.mock_test_run.state = ndb_models.TestRunState.COMPLETED
        self.mock_test_run.create_time = now - datetime.timedelta(seconds=123)
        self.mock_test_run.failed_test_run_count = 1
        self.mock_test_run.total_test_count = 2
        self.mock_test_run.failed_test_count = 3
        self.mock_test_run.put()

        # check that right data was logged
        tfc_event_handler._TrackTestRun(self.mock_test_run.key.id())
        mock_log.assert_called_with(analytics.TEST_RUN_CATEGORY,
                                    analytics.END_ACTION,
                                    test_name='name',
                                    test_version='version',
                                    state='COMPLETED',
                                    is_rerun=False,
                                    duration_seconds=123,
                                    device_count=2,
                                    attempt_count=1,
                                    failed_module_count=1,
                                    test_count=2,
                                    failed_test_count=3)
    def testTrackTestInvocation_manualRetry(self, mock_time, mock_log):
        # mock current time
        now = datetime.datetime.now()
        mock_time.return_value = now

        # configure mock TFC request with 1 attempt
        request_id = self.mock_test_run.request_id
        mock_request = api_messages.RequestMessage(id=request_id,
                                                   command_line='command')
        mock_request.command_attempts.append(
            api_messages.CommandAttemptMessage(
                state=common.CommandState.COMPLETED,
                device_serials=['2'],
                start_time=now - datetime.timedelta(seconds=200),
                end_time=now - datetime.timedelta(seconds=100),
                total_test_count=3,
                failed_test_run_count=2,
                failed_test_count=1))

        # create previous test runs
        first_test_run = ndb_models.TestRun(
            test_run_config=ndb_models.TestRunConfig(
                test_key=self.mock_test.key, run_target='run_target'),
            test=self.mock_test,
            request_id='request_id1',
            state=ndb_models.TestRunState.UNKNOWN,
            create_time=now - datetime.timedelta(seconds=5000),
            total_test_count=100,
            failed_test_run_count=90,
            failed_test_count=80,
            prev_test_context=ndb_models.TestContextObj())
        first_test_run.put()
        second_test_run = ndb_models.TestRun(
            test_run_config=ndb_models.TestRunConfig(
                test_key=self.mock_test.key, run_target='run_target'),
            test=self.mock_test,
            request_id='request_id2',
            state=ndb_models.TestRunState.UNKNOWN,
            create_time=now - datetime.timedelta(seconds=2000),
            total_test_count=10,
            failed_test_run_count=9,
            failed_test_count=8,
            prev_test_run_key=first_test_run.key)
        second_test_run.put()

        # update mock test run
        self.mock_test_run.test_package_info = ndb_models.TestPackageInfo(
            name='name', version='version')
        self.mock_test_run.state = ndb_models.TestRunState.COMPLETED
        self.mock_test_run.create_time = now - datetime.timedelta(seconds=1000)
        self.mock_test_run.failed_test_count = 1
        self.mock_test_run.prev_test_run_key = second_test_run.key

        # check that right data was logged
        tfc_event_handler._TrackTestInvocations(mock_request,
                                                self.mock_test_run)
        mock_log.assert_called_with(
            analytics.INVOCATION_CATEGORY,
            analytics.END_ACTION,
            command='command',
            test_run_command='mock test run command',
            test_run_retry_command='mock test run retry command',
            test_id=tfc_event_handler.LOCAL_ID_TAG,
            test_name='name',
            test_version='version',
            state='COMPLETED',
            failed_test_count_threshold=None,
            duration_seconds=100,
            elapsed_time_seconds=4900,
            device_count=1,
            test_count=3,
            failed_module_count=2,
            failed_test_count=1,
            prev_total_test_count=10,
            prev_failed_module_count=9,
            prev_failed_test_count=8,
            missing_previous_run=True,
            is_sequence_run=False)
    def testTrackTestInvocation(self, mock_time, mock_log):
        # mock current time
        now = datetime.datetime.now()
        mock_time.return_value = now

        # configure mock TFC request with 2 attempts
        request_id = self.mock_test_run.request_id
        mock_request = api_messages.RequestMessage(id=request_id,
                                                   command_line='command')
        mock_request.command_attempts.append(
            api_messages.CommandAttemptMessage(
                state=common.CommandState.COMPLETED,
                device_serials=['1', '2'],
                start_time=now - datetime.timedelta(seconds=400),
                end_time=now - datetime.timedelta(seconds=300),
                total_test_count=60,
                failed_test_run_count=50,
                failed_test_count=40))
        mock_request.command_attempts.append(
            api_messages.CommandAttemptMessage(
                state=common.CommandState.COMPLETED,
                device_serials=['2'],
                start_time=now - datetime.timedelta(seconds=200),
                end_time=now - datetime.timedelta(seconds=100),
                total_test_count=3,
                failed_test_run_count=2,
                failed_test_count=1))

        # set test id
        self.mock_test.key = ndb.Key(ndb_models.Test, 'some.test.id')
        self.mock_test.put()
        self.mock_test_run.test_run_config.test_key = self.mock_test.key

        # update mock test run
        self.mock_test_run.test_package_info = ndb_models.TestPackageInfo(
            name='name', version='version')
        self.mock_test_run.state = ndb_models.TestRunState.COMPLETED
        self.mock_test_run.create_time = now - datetime.timedelta(seconds=1000)
        self.mock_test_run.failed_test_count = 1
        self.mock_test_run.sequence_id = 'abc-def-ghi'

        # check that right data was logged
        tfc_event_handler._TrackTestInvocations(mock_request,
                                                self.mock_test_run)
        mock_log.assert_called_with(
            analytics.INVOCATION_CATEGORY,
            analytics.END_ACTION,
            command='command',
            test_run_command='mock test run command',
            test_run_retry_command='mock test run retry command',
            test_id='some.test.id',
            test_name='name',
            test_version='version',
            state='COMPLETED',
            failed_test_count_threshold='10',
            duration_seconds=100,
            elapsed_time_seconds=900,
            device_count=1,
            test_count=3,
            failed_module_count=2,
            failed_test_count=1,
            prev_total_test_count=60,
            prev_failed_module_count=50,
            prev_failed_test_count=40,
            missing_previous_run=False,
            is_sequence_run=True)